-
Notifications
You must be signed in to change notification settings - Fork 3k
/
options.py
154 lines (128 loc) · 6.21 KB
/
options.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
mbed SDK
Copyright (c) 2011-2013 ARM Limited
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import print_function, division, absolute_import
from json import load
from os.path import join, dirname
from os import listdir
from argparse import ArgumentParser, ArgumentTypeError
from .toolchains import TOOLCHAINS, EXTRA_TOOLCHAIN_NAMES
from .targets import TARGET_NAMES, Target, update_target_data
from .utils import (argparse_force_uppercase_type, argparse_deprecate,
argparse_lowercase_hyphen_type, argparse_many,
argparse_filestring_type, args_error,
argparse_profile_filestring_type)
FLAGS_DEPRECATION_MESSAGE = "Please use the --profile argument instead.\n"\
"Documentation may be found in "\
"docs/Toolchain_Profiles.md"
def get_toolchain_list():
toolchainlist = list(TOOLCHAINS)
toolchainlist.extend(EXTRA_TOOLCHAIN_NAMES)
toolchainlist.sort()
return toolchainlist
def get_default_options_parser(add_clean=True, add_options=True,
add_app_config=False):
"""Create a new options parser with the default compiler options added
Keyword arguments:
add_clean - add the clean argument?
add_options - add the options argument?
"""
parser = ArgumentParser()
targetnames = TARGET_NAMES
targetnames.sort()
toolchainlist = get_toolchain_list()
parser.add_argument("-m", "--mcu",
help=("build for the given MCU (%s)" %
', '.join(targetnames)),
metavar="MCU")
parser.add_argument("--custom-targets",
help="Specify directory containing custom_targets.json",
type=argparse_filestring_type,
dest="custom_targets_directory",
action="append",
default=None)
parser.add_argument("-t", "--tool",
help=("build using the given TOOLCHAIN (%s)" %
', '.join(toolchainlist)),
metavar="TOOLCHAIN",
type=argparse_many(
argparse_force_uppercase_type(
toolchainlist, "toolchain")))
parser.add_argument("--color",
help="print Warnings, and Errors in color",
action="store_true", default=False)
parser.add_argument("--cflags",
type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE),
help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE)
parser.add_argument("--asmflags",
type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE),
help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE)
parser.add_argument("--ldflags",
type=argparse_deprecate(FLAGS_DEPRECATION_MESSAGE),
help="Deprecated. " + FLAGS_DEPRECATION_MESSAGE)
if add_clean:
parser.add_argument("-c", "--clean", action="store_true", default=False,
help="clean the build directory")
if add_options:
parser.add_argument("--profile", dest="profile", action="append",
type=argparse_profile_filestring_type,
help="Build profile to use. Can be either path to json" \
"file or one of the default one ({})".format(", ".join(list_profiles())),
default=[])
if add_app_config:
parser.add_argument("--app-config", default=None, dest="app_config",
type=argparse_filestring_type,
help="Path of an app configuration file (Default is to look for 'mbed_app.json')")
return parser
def list_profiles():
"""Lists available build profiles
Checks default profile directory (mbed-os/tools/profiles/) for all the json files and return list of names only
"""
return [fn.replace(".json", "") for fn in listdir(join(dirname(__file__), "profiles")) if fn.endswith(".json")]
def extract_profile(parser, options, toolchain, fallback="develop"):
"""Extract a Toolchain profile from parsed options
Positional arguments:
parser - parser used to parse the command line arguments
options - The parsed command line arguments
toolchain - the toolchain that the profile should be extracted for
"""
profiles = []
filenames = options.profile or [join(dirname(__file__), "profiles",
fallback + ".json")]
for filename in filenames:
contents = load(open(filename))
if toolchain not in contents:
args_error(parser, ("argument --profile: toolchain {} is not"
" supported by profile {}").format(toolchain,
filename))
profiles.append(contents)
return profiles
def extract_mcus(parser, options):
try:
if options.custom_targets_directory:
for custom_targets_directory in options.custom_targets_directory:
Target.add_extra_targets(custom_targets_directory)
update_target_data()
elif options.source_dir:
for source_dir in options.source_dir:
Target.add_extra_targets(source_dir)
update_target_data()
except KeyError:
pass
targetnames = TARGET_NAMES
targetnames.sort()
try:
return argparse_many(argparse_force_uppercase_type(targetnames, "MCU"))(options.mcu)
except ArgumentTypeError as exc:
args_error(parser, "argument -m/--mcu: {}".format(str(exc)))