Skip to content

Commit 117b21d

Browse files
author
Bogdan Marinescu
committed
It's now possible to specify the location of targets.json
With this change, it becomes possible to use targets.py with any targets.json, not just the default one in ../hal/targets.json. targets.py will still be initialized with the default targets.json, but the code can then call "set_targets_json_location" to specify the new location of targets.json. set_targets_json_location modifies all the data "in place"; that is, it doesn't create a new TARGET_MAP, TARGET_NAMES or alike, but rather modified the existing ones. This is important, because code using this construct: from tools.targets import TARGET_MAP can work unmodified with this change.
1 parent 50dbce9 commit 117b21d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

tools/targets.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ class Target:
6161
# List of targets that were added dynamically using "add_py_targets" (see below)
6262
__py_targets = set()
6363

64+
# Location of the 'targets.json' file
65+
__targets_json_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'hal', 'targets.json')
66+
6467
# Load the description of JSON target data
6568
@staticmethod
6669
@cached
6770
def get_json_target_data():
68-
return json_file_to_dict(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'hal', 'targets.json'))
71+
return json_file_to_dict(Target.__targets_json_location)
72+
73+
# Set the location of the targets.json file
74+
@staticmethod
75+
def set_targets_json_location(location):
76+
Target.__targets_json_location = location
77+
# Invalidate caches, since the location of the JSON file changed
78+
caches.clear()
6979

7080
# Get the members of this module using Python's "inspect" module
7181
@staticmethod
@@ -410,3 +420,15 @@ def get_target_detect_codes():
410420
for detect_code in target.detect_code:
411421
result[detect_code] = target.name
412422
return result
423+
424+
# Sets the location of the JSON file that contains the targets
425+
def set_targets_json_location(location):
426+
# First instruct Target about the new location
427+
Target.set_targets_json_location(location)
428+
# Then re-initialize TARGETS, TARGET_MAP and TARGET_NAMES
429+
# The re-initialization does not create new variables, it keeps the old ones instead
430+
# This ensures compatibility with code that does "from tools.targets import TARGET_NAMES"
431+
TARGETS[:] = [Target.get_target(name) for name, value in Target.get_json_target_data().items() if value.get("public", True)]
432+
TARGET_MAP.clear()
433+
TARGET_MAP.update(dict([(t.name, t) for t in TARGETS]))
434+
TARGET_NAMES[:] = TARGET_MAP.keys()

0 commit comments

Comments
 (0)