Skip to content

Commit bf08b10

Browse files
pi-anlandrewleech
authored andcommitted
Add custom_targets.json file contents to targets
Avoid duplication of update_target_data() code Keep "custom_targets.json" filename definition in Targets()
1 parent 2c4475c commit bf08b10

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

tools/options.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ def mcu_is_enabled(parser, mcu):
130130

131131
def extract_mcus(parser, options):
132132
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)
133+
for source_dir in options.source_dir:
134+
Target.add_extra_targets(source_dir)
136135
update_target_data()
137136
except KeyError:
138137
pass

tools/targets/__init__.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class Target(namedtuple("Target", "name json_data resolution_order resolution_or
125125
# Current/new location of the 'targets.json' file
126126
__targets_json_location = None
127127

128+
# Extra custom targets files
129+
__extra_target_json_files = []
130+
128131
@staticmethod
129132
def _merge_dict(dct, merge_dct):
130133
""" Recursive dict merge. Inspired by `dict.update()` however instead of
@@ -148,13 +151,18 @@ def get_json_target_data():
148151
"""Load the description of JSON target data"""
149152
targets = json_file_to_dict(Target.__targets_json_location or
150153
Target.__targets_json_location_default)
154+
155+
for extra_target in Target.__extra_target_json_files:
156+
Target._merge_dict(targets, json_file_to_dict(extra_target))
157+
151158
return targets
152159

153160
@staticmethod
154-
@cached
155-
def add_extra_targets(extra):
156-
if os.path.exists(extra):
157-
Target._merge_dict(targets, json_file_to_dict(extra))
161+
def add_extra_targets(source_dir):
162+
extra_targets_file = os.path.join(source_dir, "custom_targets.json")
163+
if os.path.exists(extra_targets_file):
164+
Target.__extra_target_json_files.append(extra_targets_file)
165+
CACHES.clear()
158166

159167
@staticmethod
160168
def set_targets_json_location(location=None):
@@ -531,14 +539,20 @@ def binary_hook(t_self, resources, elf, binf):
531539
################################################################################
532540

533541
# Instantiate all public targets
534-
TARGETS = [Target.get_target(name) for name, value
535-
in Target.get_json_target_data().items()
536-
if value.get("public", True)]
542+
def update_target_data():
543+
TARGETS[:] = [Target.get_target(tgt) for tgt, obj
544+
in Target.get_json_target_data().items()
545+
if obj.get("public", True)]
546+
# Map each target name to its unique instance
547+
TARGET_MAP.clear()
548+
TARGET_MAP.update(dict([(tgt.name, tgt) for tgt in TARGETS]))
549+
TARGET_NAMES[:] = TARGET_MAP.keys()
537550

538-
# Map each target name to its unique instance
539-
TARGET_MAP = dict([(t.name, t) for t in TARGETS])
551+
TARGETS = []
552+
TARGET_MAP = dict()
553+
TARGET_NAMES = []
540554

541-
TARGET_NAMES = TARGET_MAP.keys()
555+
update_target_data()
542556

543557
# Some targets with different name have the same exporters
544558
EXPORT_MAP = {}
@@ -563,10 +577,3 @@ def set_targets_json_location(location=None):
563577
# "from tools.targets import TARGET_NAMES"
564578
update_target_data()
565579

566-
def update_target_data():
567-
TARGETS[:] = [Target.get_target(tgt) for tgt, obj
568-
in Target.get_json_target_data().items()
569-
if obj.get("public", True)]
570-
TARGET_MAP.clear()
571-
TARGET_MAP.update(dict([(tgt.name, tgt) for tgt in TARGETS]))
572-
TARGET_NAMES[:] = TARGET_MAP.keys()

0 commit comments

Comments
 (0)