Skip to content

Commit 74fc369

Browse files
committed
[tools] Delay validation of mcu's until after loading config
1 parent d2dbce0 commit 74fc369

File tree

9 files changed

+109
-27
lines changed

9 files changed

+109
-27
lines changed

tools/build.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@
2929

3030
from tools.toolchains import TOOLCHAINS
3131
from tools.toolchains import mbedToolchain
32-
from tools.targets import TARGET_NAMES, TARGET_MAP
32+
from tools.targets import TARGET_NAMES, TARGET_MAP, Target
3333
from tools.options import get_default_options_parser
3434
from tools.build_api import build_library, build_mbed_libs, build_lib
3535
from tools.build_api import mcu_toolchain_matrix
3636
from tools.build_api import static_analysis_scan, static_analysis_scan_lib, static_analysis_scan_library
3737
from tools.build_api import print_build_results
3838
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT
3939
from utils import argparse_filestring_type
40+
from utils import argparse_force_uppercase_type
41+
from utils import argparse_many
42+
from utils import run_type_after_parse
4043
from tools.settings import CPPCHECK_CMD, CPPCHECK_MSG_FORMAT, CLI_COLOR_MAP
4144
from utils import argparse_filestring_type, argparse_dir_not_parent
45+
from tools.config import Config
4246

4347
if __name__ == '__main__':
4448
start = time()
@@ -156,13 +160,23 @@
156160

157161
options = parser.parse_args()
158162

163+
if options.source_dir:
164+
config = Config.add_target_config(options.source_dir)
165+
if "custom_targets" in config:
166+
Target.add_py_targets(config["custom_targets"])
167+
159168
# Only prints matrix of supported toolchains
160169
if options.supported_toolchains:
161170
print mcu_toolchain_matrix(platform_filter=options.general_filter_regex)
162171
exit(0)
163172

164173
# Get target list
165-
targets = options.mcu if options.mcu else TARGET_NAMES
174+
if options.mcu:
175+
targets = run_type_after_parse(
176+
argparse_many(argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU")),
177+
parser, options.mcu, "-m")
178+
else:
179+
targets = TARGET_NAMES
166180

167181
# Get toolchains list
168182
toolchains = options.tool if options.tool else TOOLCHAINS

tools/config.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,30 +190,35 @@ class Config:
190190
"UVISOR", "BLE", "CLIENT", "IPV4", "IPV6"
191191
]
192192

193-
# The initialization arguments for Config are:
194-
# target: the name of the mbed target used for this configuration instance
195-
# top_level_dirs: a list of top level source directories (where mbed_abb_config.json could be found)
196-
# __init__ will look for the application configuration file in top_level_dirs.
197-
# If found once, it'll parse it and check if it has a custom_targets function.
198-
# If it does, it'll update the list of targets if need.
199-
# If found more than once, an exception is raised
200-
# top_level_dirs can be None (in this case, mbed_app_config.json will not be searched)
201-
def __init__(self, target, top_level_dirs = []):
193+
@staticmethod
194+
def add_target_config(top_level_dirs):
202195
app_config_location = None
203196
for s in (top_level_dirs or []):
204-
full_path = os.path.join(s, self.__mbed_app_config_name)
197+
full_path = os.path.join(s, Config.__mbed_app_config_name)
205198
if os.path.isfile(full_path):
206199
if app_config_location is not None:
207-
raise ConfigException("Duplicate '%s' file in '%s' and '%s'" % (self.__mbed_app_config_name, app_config_location, full_path))
200+
raise ConfigException("Duplicate '%s' file in '%s' and '%s'" % (Config.__mbed_app_config_name, app_config_location, full_path))
208201
else:
209202
app_config_location = full_path
210-
self.app_config_data = json_file_to_dict(app_config_location) if app_config_location else {}
203+
app_config_data = json_file_to_dict(app_config_location) if app_config_location else {}
211204
# Check the keys in the application configuration data
212-
unknown_keys = set(self.app_config_data.keys()) - self.__allowed_keys["application"]
205+
unknown_keys = set(app_config_data.keys()) - Config.__allowed_keys["application"]
213206
if unknown_keys:
214-
raise ConfigException("Unknown key(s) '%s' in %s" % (",".join(unknown_keys), self.__mbed_app_config_name))
207+
raise ConfigException("Unknown key(s) '%s' in %s" % (",".join(unknown_keys), Config.__mbed_app_config_name))
215208
# Update the list of targets with the ones defined in the application config, if applicable
216-
Target.add_py_targets(self.app_config_data.get("custom_targets", {}))
209+
return app_config_data
210+
211+
212+
# The initialization arguments for Config are:
213+
# target: the name of the mbed target used for this configuration instance
214+
# top_level_dirs: a list of top level source directories (where mbed_abb_config.json could be found)
215+
# __init__ will look for the application configuration file in top_level_dirs.
216+
# If found once, it'll parse it and check if it has a custom_targets function.
217+
# If it does, it'll update the list of targets if need.
218+
# If found more than once, an exception is raised
219+
# top_level_dirs can be None (in this case, mbed_app_config.json will not be searched)
220+
def __init__(self, target, top_level_dirs = []):
221+
self.app_config_data = Config.add_target_config(top_level_dirs)
217222
self.lib_config_data = {}
218223
# Make sure that each config is processed only once
219224
self.processed_configs = {}

tools/get_config.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727
from tools.utils import args_error
2828
from tools.options import get_default_options_parser
2929
from tools.build_api import get_config
30+
from tools.targets import TARGET_NAMES
31+
from tools.targets import Target
3032
from config import Config
3133
from utils import argparse_filestring_type
34+
from utils import argparse_force_uppercase_type
35+
from utils import run_type_after_parse
3236
try:
3337
import tools.private_settings as ps
3438
except:
@@ -46,10 +50,18 @@
4650

4751
options = parser.parse_args()
4852

53+
if options.source_dir:
54+
config = Config.add_target_config(options.source_dir)
55+
if "custom_targets" in config:
56+
Target.add_py_targets(config["custom_targets"])
57+
4958
# Target
50-
if options.mcu is None :
59+
if options.mcu:
60+
target = run_type_after_parse(
61+
argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU"),
62+
parser, options.mcu, "-m")
63+
else:
5164
args_error(parser, "[ERROR] You should specify an MCU")
52-
target = options.mcu[0]
5365

5466
# Toolchain
5567
if options.tool is None:

tools/make.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,20 @@
4040
from tools.tests import TEST_MBED_LIB
4141
from tools.tests import test_known, test_name_known
4242
from tools.targets import TARGET_MAP
43+
from tools.targets import TARGET_NAMES
44+
from tools.targets import Target
4345
from tools.options import get_default_options_parser
4446
from tools.build_api import build_project
4547
from tools.build_api import mcu_toolchain_matrix
4648
from utils import argparse_filestring_type
4749
from utils import argparse_many
4850
from utils import argparse_dir_not_parent
51+
from utils import argparse_force_uppercase_type
52+
from utils import run_type_after_parse
4953
from argparse import ArgumentTypeError
5054
from tools.toolchains import mbedToolchain
5155
from tools.settings import CLI_COLOR_MAP
56+
from tools.config import Config
5257

5358
if __name__ == '__main__':
5459
# Parse Options
@@ -196,6 +201,9 @@
196201

197202
# force program to "0" if a source dir is specified
198203
if options.source_dir is not None:
204+
config = Config.add_target_config(options.source_dir)
205+
if "custom_targets" in config:
206+
Target.add_py_targets(config["custom_targets"])
199207
p = 0
200208
else:
201209
# Program Number or name
@@ -206,9 +214,13 @@
206214
p = [p]
207215

208216
# Target
209-
if options.mcu is None :
217+
# Get target list
218+
if options.mcu:
219+
mcu = run_type_after_parse(
220+
argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU"),
221+
parser, options.mcu, "-m")
222+
else:
210223
args_error(parser, "[ERROR] You should specify an MCU")
211-
mcu = options.mcu[0]
212224

213225
# Toolchain
214226
if options.tool is None:

tools/options.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def get_default_options_parser(add_clean=True, add_options=True):
2929

3030
parser.add_argument("-m", "--mcu",
3131
help="build for the given MCU (%s)" % ', '.join(targetnames),
32-
metavar="MCU",
33-
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")))
32+
metavar="MCU")
3433

3534
parser.add_argument("-t", "--tool",
3635
help="build using the given TOOLCHAIN (%s)" % ', '.join(toolchainlist),

tools/project.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
from tools.export import export, EXPORTERS, mcu_ide_matrix
1212
from tools.tests import TESTS, TEST_MAP
1313
from tools.tests import test_known, test_name_known
14-
from tools.targets import TARGET_NAMES
14+
from tools.targets import TARGET_NAMES, Target
1515
from tools.libraries import LIBRARIES
1616
from utils import argparse_filestring_type, argparse_many
1717
from utils import argparse_force_lowercase_type, argparse_force_uppercase_type, argparse_dir_not_parent
18+
from utils import run_type_after_parse
19+
from tools.config import Config
1820
from project_api import setup_project, perform_export, print_results, get_lib_symbols
1921

2022

@@ -31,7 +33,6 @@
3133
parser.add_argument("-m", "--mcu",
3234
metavar="MCU",
3335
default='LPC1768',
34-
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")),
3536
help="generate project for the given MCU (%s)"% ', '.join(targetnames))
3637

3738
parser.add_argument("-i",
@@ -99,6 +100,11 @@
99100
print '\n'.join(map(str, sorted(TEST_MAP.values())))
100101
sys.exit()
101102

103+
if options.source_dir:
104+
config = Config.add_target_config(options.source_dir)
105+
if "custom_targets" in config:
106+
Target.add_py_targets(config["custom_targets"])
107+
102108
# Only prints matrix of supported IDEs
103109
if options.supported_ides:
104110
print mcu_ide_matrix()
@@ -120,6 +126,11 @@
120126
raise
121127
exit(0)
122128

129+
if options.mcu:
130+
mcus = run_type_after_parse(
131+
argparse_many(argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU")),
132+
parser, options.mcu, "-m")
133+
123134
# Clean Export Directory
124135
if options.clean:
125136
if exists(EXPORT_DIR):
@@ -132,7 +143,7 @@
132143
# source_dir = use relative paths, otherwise sources are copied
133144
sources_relative = True if options.source_dir else False
134145

135-
for mcu in options.mcu:
146+
for mcu in mcus:
136147
# Program Number or name
137148
p, src, ide = options.program, options.source_dir, options.ide
138149
try:

tools/test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131
from tools.build_api import build_project, build_library
3232
from tools.build_api import print_build_memory_usage_results
3333
from tools.targets import TARGET_MAP
34+
from tools.targets import TARGET_NAMES
35+
from tools.targets import Target
3436
from tools.utils import mkdir, ToolException, NotSupportedException
3537
from tools.test_exporters import ReportExporter, ResultExporterType
3638
from utils import argparse_filestring_type, argparse_lowercase_type, argparse_many
3739
from utils import argparse_dir_not_parent
40+
from utils import argparse_force_uppercase_type
41+
from utils import run_type_after_parse
42+
from utils import args_error
3843
from tools.toolchains import mbedToolchain
3944
from tools.settings import CLI_COLOR_MAP
45+
from tools.config import Config
4046

4147
if __name__ == '__main__':
4248
try:
@@ -96,6 +102,11 @@
96102

97103
options = parser.parse_args()
98104

105+
if options.source_dir:
106+
config = Config.add_target_config(options.source_dir)
107+
if "custom_targets" in config:
108+
Target.add_py_targets(config["custom_targets"])
109+
99110
# Filter tests by path if specified
100111
if options.paths:
101112
all_paths = options.paths
@@ -152,7 +163,13 @@
152163
base_source_paths = ['.']
153164

154165

155-
target = options.mcu[0]
166+
# Get target list
167+
if options.mcu:
168+
target = run_type_after_parse(
169+
argparse_force_uppercase_type(sorted(TARGET_NAMES), "MCU"),
170+
parser, options.mcu, "-m")
171+
else:
172+
args_error(parser, "[ERROR] You should specify an MCU")
156173

157174
build_report = {}
158175
build_properties = {}

tools/test/config_test/config_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ def test_tree(full_name, name):
4646
try:
4747
# Use 'set_targets_json_location' to remove the previous custom targets from the target list
4848
set_targets_json_location(Target._Target__targets_json_location)
49+
config = Config.add_target_config([full_name])
50+
if "custom_targets" in config:
51+
Target.add_py_targets(config["custom_targets"])
4952
cfg, macros, features = get_config(full_name, target, "GCC_ARM")
5053
macros = Config.config_macros_to_macros(macros)
5154
except ConfigException as e:

tools/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ def argparse_filestring_type(string) :
291291
else :
292292
raise argparse.ArgumentTypeError("{0}"" does not exist in the filesystem.".format(string))
293293

294+
def run_type_after_parse(type_fn, parser, args, switch):
295+
try:
296+
if not isinstance(args, list):
297+
return type_fn(args)
298+
else:
299+
return sum([type_fn(arg) for arg in args], [])
300+
except argparse.ArgumentTypeError as e:
301+
parser.error("argument {}: {}".format(switch, str(e)))
302+
294303
# render a list of strings as a in a bunch of columns
295304
def columnate(strings, seperator=", ", chars=80):
296305
col_width = max(len(s) for s in strings)

0 commit comments

Comments
 (0)