Skip to content

Commit 2c4475c

Browse files
theotherjimmyandrewleech
authored andcommitted
Find extra targets in all source folders
1 parent 58c52fa commit 2c4475c

File tree

7 files changed

+52
-32
lines changed

7 files changed

+52
-32
lines changed

tools/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from tools.targets import TARGET_NAMES, TARGET_MAP
3333
from tools.options import get_default_options_parser
3434
from tools.options import extract_profile
35+
from tools.options import extract_mcus
3536
from tools.build_api import build_library, build_mbed_libs, build_lib
3637
from tools.build_api import mcu_toolchain_matrix
3738
from tools.build_api import print_build_results
@@ -134,7 +135,7 @@
134135

135136

136137
# Get target list
137-
targets = options.mcu if options.mcu else TARGET_NAMES
138+
targets = extract_mcus(parser, options) if options.mcu else TARGET_NAMES
138139

139140
# Get toolchains list
140141
toolchains = options.tool if options.tool else TOOLCHAINS

tools/get_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from tools.utils import args_error
2828
from tools.options import get_default_options_parser
29+
from tools.options import extract_mcus
2930
from tools.build_api import get_config
3031
from config import Config
3132
from utils import argparse_filestring_type
@@ -49,7 +50,7 @@
4950
# Target
5051
if options.mcu is None :
5152
args_error(parser, "argument -m/--mcu is required")
52-
target = options.mcu[0]
53+
target = extract_mcus(parser, options)[0]
5354

5455
# Toolchain
5556
if options.tool is None:

tools/make.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from tools.targets import TARGET_MAP
4343
from tools.options import get_default_options_parser
4444
from tools.options import extract_profile
45+
from tools.options import extract_mcus
4546
from tools.build_api import build_project
4647
from tools.build_api import mcu_toolchain_matrix
4748
from tools.build_api import mcu_toolchain_list
@@ -200,7 +201,7 @@
200201
# Target
201202
if options.mcu is None :
202203
args_error(parser, "argument -m/--mcu is required")
203-
mcu = options.mcu[0]
204+
mcu = extract_mcus(parser, options)[0]
204205

205206
# Toolchain
206207
if options.tool is None:

tools/options.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
from json import load
1818
from os.path import join, dirname
1919
from os import listdir
20-
from argparse import ArgumentParser
20+
from argparse import ArgumentParser, ArgumentTypeError
2121
from tools.toolchains import TOOLCHAINS
22-
from tools.targets import TARGET_NAMES
22+
from tools.targets import TARGET_NAMES, Target, update_target_data
2323
from tools.utils import argparse_force_uppercase_type, \
2424
argparse_lowercase_hyphen_type, argparse_many, \
2525
argparse_filestring_type, args_error, argparse_profile_filestring_type,\
@@ -47,10 +47,7 @@ def get_default_options_parser(add_clean=True, add_options=True,
4747
parser.add_argument("-m", "--mcu",
4848
help=("build for the given MCU (%s)" %
4949
', '.join(targetnames)),
50-
metavar="MCU",
51-
type=argparse_many(
52-
argparse_force_uppercase_type(
53-
targetnames, "MCU")))
50+
metavar="MCU")
5451

5552
parser.add_argument("-t", "--tool",
5653
help=("build using the given TOOLCHAIN (%s)" %
@@ -130,3 +127,19 @@ def mcu_is_enabled(parser, mcu):
130127
"See https://developer.mbed.org/platforms/Renesas-GR-PEACH/#important-notice "
131128
"for more information") % (mcu, mcu))
132129
return True
130+
131+
def extract_mcus(parser, options):
132+
try:
133+
extra_targets = [join(src, "custom_targets.json") for src in options.source_dir]
134+
for filename in extra_targets:
135+
Target.add_extra_targets(filename)
136+
update_target_data()
137+
except KeyError:
138+
pass
139+
targetnames = TARGET_NAMES
140+
targetnames.sort()
141+
try:
142+
return argparse_many(argparse_force_uppercase_type(targetnames, "MCU"))(options.mcu)
143+
except ArgumentTypeError as exc:
144+
args_error(parser, "argument -m/--mcu: {}".format(str(exc)))
145+

tools/project.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from tools.utils import argparse_force_lowercase_type
2121
from tools.utils import argparse_force_uppercase_type
2222
from tools.utils import print_large_string
23-
from tools.options import extract_profile, list_profiles
23+
from tools.options import extract_profile, list_profiles, extract_mcus
2424

2525
def setup_project(ide, target, program=None, source_dir=None, build=None, export_path=None):
2626
"""Generate a name, if not provided, and find dependencies
@@ -247,7 +247,8 @@ def main():
247247
profile = extract_profile(parser, options, toolchain_name, fallback="debug")
248248
if options.clean:
249249
rmtree(BUILD_DIR)
250-
export(options.mcu, options.ide, build=options.build,
250+
mcu = extract_mcus(parser, options)[0]
251+
export(mcu, options.ide, build=options.build,
251252
src=options.source_dir, macros=options.macros,
252253
project_id=options.program, zip_proj=zip_proj,
253254
build_profile=profile, app_config=options.app_config)

tools/targets/__init__.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,35 +127,35 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
127127

128128
@staticmethod
129129
def _merge_dict(dct, merge_dct):
130-
""" Recursive dict merge. Inspired by `dict.update()` however instead of
131-
updating only top-level keys, dict_merge recurses down into dicts nested
132-
to an arbitrary depth, updating keys.
133-
The provided ``merge_dct`` is merged into ``dct`` in place.
134-
:param dct: dict onto which the merge is executed
135-
:param merge_dct: dct merged into dct
136-
:return: None
137-
"""
138-
for k, v in merge_dct.iteritems():
139-
if (k in dct and isinstance(dct[k], dict)
140-
and isinstance(merge_dct[k], Mapping)):
141-
Target._merge_dict(dct[k], merge_dct[k])
142-
else:
143-
dct[k] = merge_dct[k]
130+
""" Recursive dict merge. Inspired by `dict.update()` however instead of
131+
updating only top-level keys, dict_merge recurses down into dicts nested
132+
to an arbitrary depth, updating keys.
133+
The provided ``merge_dct`` is merged into ``dct`` in place.
134+
:param dct: dict onto which the merge is executed
135+
:param merge_dct: dct merged into dct
136+
:return: None
137+
"""
138+
for k, v in merge_dct.iteritems():
139+
if (k in dct and isinstance(dct[k], dict)
140+
and isinstance(merge_dct[k], Mapping)):
141+
Target._merge_dict(dct[k], merge_dct[k])
142+
else:
143+
dct[k] = merge_dct[k]
144144

145145
@staticmethod
146146
@cached
147147
def get_json_target_data():
148148
"""Load the description of JSON target data"""
149149
targets = json_file_to_dict(Target.__targets_json_location or
150-
Target.__targets_json_location_default)
150+
Target.__targets_json_location_default)
151+
return targets
151152

152-
# If extra_targets.json exists in working directory load it over the top
153-
extra = os.path.join('.', 'extra_targets.json')
153+
@staticmethod
154+
@cached
155+
def add_extra_targets(extra):
154156
if os.path.exists(extra):
155157
Target._merge_dict(targets, json_file_to_dict(extra))
156158

157-
return targets
158-
159159
@staticmethod
160160
def set_targets_json_location(location=None):
161161
"""Set the location of the targets.json file"""
@@ -561,6 +561,9 @@ def set_targets_json_location(location=None):
561561
# re-initialization does not create new variables, it keeps the old ones
562562
# instead. This ensures compatibility with code that does
563563
# "from tools.targets import TARGET_NAMES"
564+
update_target_data()
565+
566+
def update_target_data():
564567
TARGETS[:] = [Target.get_target(tgt) for tgt, obj
565568
in Target.get_json_target_data().items()
566569
if obj.get("public", True)]

tools/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from tools.config import ConfigException
3030
from tools.test_api import test_path_to_name, find_tests, print_tests, build_tests, test_spec_from_test_builds
31-
from tools.options import get_default_options_parser, extract_profile
31+
from tools.options import get_default_options_parser, extract_profile, extract_mcus
3232
from tools.build_api import build_project, build_library
3333
from tools.build_api import print_build_memory_usage
3434
from tools.build_api import merge_build_data
@@ -114,7 +114,7 @@
114114
# Target
115115
if options.mcu is None :
116116
args_error(parser, "argument -m/--mcu is required")
117-
mcu = options.mcu[0]
117+
mcu = extract_mcus(parser, options)[0]
118118

119119
# Toolchain
120120
if options.tool is None:

0 commit comments

Comments
 (0)