User | Revision | Line number | New contents of line |
switches |
0:0e018d759a2a
|
1
|
"""
|
switches |
0:0e018d759a2a
|
2
|
mbed SDK
|
switches |
0:0e018d759a2a
|
3
|
Copyright (c) 2011-2013 ARM Limited
|
switches |
0:0e018d759a2a
|
4
|
|
switches |
0:0e018d759a2a
|
5
|
Licensed under the Apache License, Version 2.0 (the "License");
|
switches |
0:0e018d759a2a
|
6
|
you may not use this file except in compliance with the License.
|
switches |
0:0e018d759a2a
|
7
|
You may obtain a copy of the License at
|
switches |
0:0e018d759a2a
|
8
|
|
switches |
0:0e018d759a2a
|
9
|
http://www.apache.org/licenses/LICENSE-2.0
|
switches |
0:0e018d759a2a
|
10
|
|
switches |
0:0e018d759a2a
|
11
|
Unless required by applicable law or agreed to in writing, software
|
switches |
0:0e018d759a2a
|
12
|
distributed under the License is distributed on an "AS IS" BASIS,
|
switches |
0:0e018d759a2a
|
13
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
switches |
0:0e018d759a2a
|
14
|
See the License for the specific language governing permissions and
|
switches |
0:0e018d759a2a
|
15
|
limitations under the License.
|
switches |
0:0e018d759a2a
|
16
|
"""
|
switches |
0:0e018d759a2a
|
17
|
from json import load
|
switches |
0:0e018d759a2a
|
18
|
from os.path import join, dirname
|
switches |
0:0e018d759a2a
|
19
|
from os import listdir
|
switches |
0:0e018d759a2a
|
20
|
from argparse import ArgumentParser
|
switches |
0:0e018d759a2a
|
21
|
from tools.toolchains import TOOLCHAINS
|
switches |
0:0e018d759a2a
|
22
|
from tools.targets import TARGET_NAMES
|
switches |
0:0e018d759a2a
|
23
|
from tools.utils import argparse_force_uppercase_type, \
|
switches |
0:0e018d759a2a
|
24
|
argparse_lowercase_hyphen_type, argparse_many, \
|
switches |
0:0e018d759a2a
|
25
|
argparse_filestring_type, args_error, argparse_profile_filestring_type
|
switches |
0:0e018d759a2a
|
26
|
|
switches |
0:0e018d759a2a
|
27
|
def get_default_options_parser(add_clean=True, add_options=True,
|
switches |
0:0e018d759a2a
|
28
|
add_app_config=False):
|
switches |
0:0e018d759a2a
|
29
|
"""Create a new options parser with the default compiler options added
|
switches |
0:0e018d759a2a
|
30
|
|
switches |
0:0e018d759a2a
|
31
|
Keyword arguments:
|
switches |
0:0e018d759a2a
|
32
|
add_clean - add the clean argument?
|
switches |
0:0e018d759a2a
|
33
|
add_options - add the options argument?
|
switches |
0:0e018d759a2a
|
34
|
"""
|
switches |
0:0e018d759a2a
|
35
|
parser = ArgumentParser()
|
switches |
0:0e018d759a2a
|
36
|
|
switches |
0:0e018d759a2a
|
37
|
targetnames = TARGET_NAMES
|
switches |
0:0e018d759a2a
|
38
|
targetnames.sort()
|
switches |
0:0e018d759a2a
|
39
|
toolchainlist = list(TOOLCHAINS)
|
switches |
0:0e018d759a2a
|
40
|
toolchainlist.sort()
|
switches |
0:0e018d759a2a
|
41
|
|
switches |
0:0e018d759a2a
|
42
|
parser.add_argument("-m", "--mcu",
|
switches |
0:0e018d759a2a
|
43
|
help=("build for the given MCU (%s)" %
|
switches |
0:0e018d759a2a
|
44
|
', '.join(targetnames)),
|
switches |
0:0e018d759a2a
|
45
|
metavar="MCU",
|
switches |
0:0e018d759a2a
|
46
|
type=argparse_many(
|
switches |
0:0e018d759a2a
|
47
|
argparse_force_uppercase_type(
|
switches |
0:0e018d759a2a
|
48
|
targetnames, "MCU")))
|
switches |
0:0e018d759a2a
|
49
|
|
switches |
0:0e018d759a2a
|
50
|
parser.add_argument("-t", "--tool",
|
switches |
0:0e018d759a2a
|
51
|
help=("build using the given TOOLCHAIN (%s)" %
|
switches |
0:0e018d759a2a
|
52
|
', '.join(toolchainlist)),
|
switches |
0:0e018d759a2a
|
53
|
metavar="TOOLCHAIN",
|
switches |
0:0e018d759a2a
|
54
|
type=argparse_many(
|
switches |
0:0e018d759a2a
|
55
|
argparse_force_uppercase_type(
|
switches |
0:0e018d759a2a
|
56
|
toolchainlist, "toolchain")))
|
switches |
0:0e018d759a2a
|
57
|
|
switches |
0:0e018d759a2a
|
58
|
parser.add_argument("--color",
|
switches |
0:0e018d759a2a
|
59
|
help="print Warnings, and Errors in color",
|
switches |
0:0e018d759a2a
|
60
|
action="store_true", default=False)
|
switches |
0:0e018d759a2a
|
61
|
|
switches |
0:0e018d759a2a
|
62
|
parser.add_argument("--cflags", default=[], action="append",
|
switches |
0:0e018d759a2a
|
63
|
help="Extra flags to provide to the C compiler")
|
switches |
0:0e018d759a2a
|
64
|
|
switches |
0:0e018d759a2a
|
65
|
parser.add_argument("--asmflags", default=[], action="append",
|
switches |
0:0e018d759a2a
|
66
|
help="Extra flags to provide to the assembler")
|
switches |
0:0e018d759a2a
|
67
|
|
switches |
0:0e018d759a2a
|
68
|
parser.add_argument("--ldflags", default=[], action="append",
|
switches |
0:0e018d759a2a
|
69
|
help="Extra flags to provide to the linker")
|
switches |
0:0e018d759a2a
|
70
|
|
switches |
0:0e018d759a2a
|
71
|
if add_clean:
|
switches |
0:0e018d759a2a
|
72
|
parser.add_argument("-c", "--clean", action="store_true", default=False,
|
switches |
0:0e018d759a2a
|
73
|
help="clean the build directory")
|
switches |
0:0e018d759a2a
|
74
|
|
switches |
0:0e018d759a2a
|
75
|
if add_options:
|
switches |
0:0e018d759a2a
|
76
|
parser.add_argument("--profile", dest="profile", action="append",
|
switches |
0:0e018d759a2a
|
77
|
type=argparse_profile_filestring_type,
|
switches |
0:0e018d759a2a
|
78
|
help="Build profile to use. Can be either path to json" \
|
switches |
0:0e018d759a2a
|
79
|
"file or one of the default one ({})".format(", ".join(list_profiles())),
|
switches |
0:0e018d759a2a
|
80
|
default=[])
|
switches |
0:0e018d759a2a
|
81
|
if add_app_config:
|
switches |
0:0e018d759a2a
|
82
|
parser.add_argument("--app-config", default=None, dest="app_config",
|
switches |
0:0e018d759a2a
|
83
|
type=argparse_filestring_type,
|
switches |
0:0e018d759a2a
|
84
|
help="Path of an app configuration file (Default is to look for 'mbed_app.json')")
|
switches |
0:0e018d759a2a
|
85
|
|
switches |
0:0e018d759a2a
|
86
|
return parser
|
switches |
0:0e018d759a2a
|
87
|
|
switches |
0:0e018d759a2a
|
88
|
def list_profiles():
|
switches |
0:0e018d759a2a
|
89
|
"""Lists available build profiles
|
switches |
0:0e018d759a2a
|
90
|
|
switches |
0:0e018d759a2a
|
91
|
Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only
|
switches |
0:0e018d759a2a
|
92
|
"""
|
switches |
0:0e018d759a2a
|
93
|
return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")]
|
switches |
0:0e018d759a2a
|
94
|
|
switches |
0:0e018d759a2a
|
95
|
def extract_profile(parser, options, toolchain):
|
switches |
0:0e018d759a2a
|
96
|
"""Extract a Toolchain profile from parsed options
|
switches |
0:0e018d759a2a
|
97
|
|
switches |
0:0e018d759a2a
|
98
|
Positional arguments:
|
switches |
0:0e018d759a2a
|
99
|
parser - parser used to parse the command line arguments
|
switches |
0:0e018d759a2a
|
100
|
options - The parsed command line arguments
|
switches |
0:0e018d759a2a
|
101
|
toolchain - the toolchain that the profile should be extracted for
|
switches |
0:0e018d759a2a
|
102
|
"""
|
switches |
0:0e018d759a2a
|
103
|
profile = {'c': [], 'cxx': [], 'ld': [], 'common': [], 'asm': []}
|
switches |
0:0e018d759a2a
|
104
|
filenames = options.profile or [join(dirname(__file__), "profiles",
|
switches |
0:0e018d759a2a
|
105
|
"default.json")]
|
switches |
0:0e018d759a2a
|
106
|
for filename in filenames:
|
switches |
0:0e018d759a2a
|
107
|
contents = load(open(filename))
|
switches |
0:0e018d759a2a
|
108
|
try:
|
switches |
0:0e018d759a2a
|
109
|
for key in profile.iterkeys():
|
switches |
0:0e018d759a2a
|
110
|
profile[key] += contents[toolchain][key]
|
switches |
0:0e018d759a2a
|
111
|
except KeyError:
|
switches |
0:0e018d759a2a
|
112
|
args_error(parser, ("argument --profile: toolchain {} is not"
|
switches |
0:0e018d759a2a
|
113
|
" supported by profile {}").format(toolchain,
|
switches |
0:0e018d759a2a
|
114
|
filename))
|
switches |
0:0e018d759a2a
|
115
|
return profile
|