Skip to content

Commit 9704edf

Browse files
author
Bogdan Marinescu
committed
Fix add_py_targets and tests
Previously, add_py_targets assumed that it is not an error to add an existing target if that target was previously added using add_py_targets. This was done to aid testing, but it was weird, error prone and broke with the latest changes to the caching mechanism. With this commit: - it is always an error to add a target that was previously added, which is much more consistent. - tests for the configuration mechanism use the newly added 'set_targets_json_location' function to clear the internal caches in targets.py (and thus all previously added custom targets). As a side effect, this commit also tests the 'set_targets_json_location' function itself.
1 parent 117b21d commit 9704edf

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

tools/targets.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ class Target:
5858
# need to be computed differently than regular attributes
5959
__cumulative_attributes = ['extra_labels', 'macros', 'device_has', 'features']
6060

61-
# List of targets that were added dynamically using "add_py_targets" (see below)
62-
__py_targets = set()
63-
6461
# Location of the 'targets.json' file
6562
__targets_json_location = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'hal', 'targets.json')
6663

@@ -175,35 +172,20 @@ def __getattr__(self, attrname):
175172
return v
176173

177174
# Add one or more new target(s) represented as a Python dictionary in 'new_targets'
178-
# It it an error to add a target with a name that exists in "targets.json"
179-
# However, it is OK to add a target that was previously added via "add_py_targets"
180-
# (this makes testing easier without changing the regular semantics)
175+
# It is an error to add a target with a name that already exists.
181176
@staticmethod
182177
def add_py_targets(new_targets):
183178
crt_data = Target.get_json_target_data()
184-
# First add all elemnts to the internal dictionary
185179
for tk, tv in new_targets.items():
186-
if crt_data.has_key(tk) and (not tk in Target.__py_targets):
180+
if crt_data.has_key(tk):
187181
raise Exception("Attempt to add target '%s' that already exists" % tk)
182+
# Add target data to the internal target dictionary
188183
crt_data[tk] = tv
189-
Target.__py_targets.add(tk)
190-
# Then create the new instances and update global variables if needed
191-
for tk, tv in new_targets.items():
192-
# Is the target already created?
193-
old_target = Target.__target_map.get(tk, None)
194-
# Instantiate this target. If it is public, update the data in
195-
# in TARGETS, TARGET_MAP, TARGET_NAMES
184+
# Create the new target and add it to the relevant data structures
196185
new_target = Target(tk)
197-
if tv.get("public", True):
198-
if old_target: # remove the old target from TARGETS and TARGET_NAMES
199-
TARGETS.remove(old_target)
200-
TARGET_NAMES.remove(tk)
201-
# Add the new target
202-
TARGETS.append(new_target)
203-
TARGET_MAP[tk] = new_target
204-
TARGET_NAMES.append(tk)
205-
# Update the target cache
206-
Target.__target_map[tk] = new_target
186+
TARGETS.append(new_target)
187+
TARGET_MAP[tk] = new_target
188+
TARGET_NAMES.append(tk)
207189

208190
# Return the target instance starting from the target name
209191
@staticmethod

tools/test/config_test/config_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
from tools.build_api import get_config
19+
from tools.targets import set_targets_json_location, Target
1920
from tools.config import ConfigException, Config
2021
import os, sys
2122

@@ -43,6 +44,8 @@ def test_tree(full_name, name):
4344
sys.stdout.flush()
4445
err_msg = None
4546
try:
47+
# Use 'set_targets_json_location' to remove the previous custom targets from the target list
48+
set_targets_json_location(Target._Target__targets_json_location)
4649
cfg, macros, features = get_config(full_name, target, "GCC_ARM")
4750
macros = Config.config_macros_to_macros(macros)
4851
except ConfigException as e:

0 commit comments

Comments
 (0)