Skip to content

Commit 243b9df

Browse files
author
Cruz Monrreal
authored
Merge pull request #8982 from bridadan/fix_duplicate_macros
Ensure macros and parameters with the same name are not repeated.
2 parents 78d6018 + fa4ead0 commit 243b9df

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

tools/config/__init__.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -650,21 +650,21 @@ def _get_mem_specs(self, memories, cmsis_part, exception_text):
650650
raise ConfigException(exception_text)
651651

652652
def get_all_active_memories(self, memory_list):
653-
"""Get information of all available rom/ram memories in the form of dictionary
654-
{Memory: [start_addr, size]}. Takes in the argument, a list of all available
653+
"""Get information of all available rom/ram memories in the form of dictionary
654+
{Memory: [start_addr, size]}. Takes in the argument, a list of all available
655655
regions within the ram/rom memory"""
656656
# Override rom_start/rom_size
657657
#
658658
# This is usually done for a target which:
659659
# 1. Doesn't support CMSIS pack, or
660660
# 2. Supports TrustZone and user needs to change its flash partition
661-
661+
662662
available_memories = {}
663663
# Counter to keep track of ROM/RAM memories supported by target
664664
active_memory_counter = 0
665665
# Find which memory we are dealing with, RAM/ROM
666666
active_memory = 'ROM' if any('ROM' in mem_list for mem_list in memory_list) else 'RAM'
667-
667+
668668
try:
669669
cmsis_part = self._get_cmsis_part()
670670
except ConfigException:
@@ -683,7 +683,7 @@ def get_all_active_memories(self, memory_list):
683683

684684
present_memories = set(cmsis_part['memory'].keys())
685685
valid_memories = set(memory_list).intersection(present_memories)
686-
686+
687687
for memory in valid_memories:
688688
mem_start, mem_size = self._get_mem_specs(
689689
[memory],
@@ -709,7 +709,7 @@ def get_all_active_memories(self, memory_list):
709709
mem_start = int(mem_start, 0)
710710
mem_size = int(mem_size, 0)
711711
available_memories[memory] = [mem_start, mem_size]
712-
712+
713713
return available_memories
714714

715715
@property
@@ -1092,17 +1092,37 @@ def _check_required_parameters(params):
10921092
"' doesn't have a value")
10931093

10941094
@staticmethod
1095-
def parameters_to_macros(params):
1096-
""" Encode the configuration parameters as C macro definitions.
1095+
def _parameters_and_config_macros_to_macros(params, macros):
1096+
""" Return the macro definitions generated for a dictionary of
1097+
ConfigParameters and a dictionary of ConfigMacros (as returned by
1098+
get_config_data). The ConfigParameters override any matching macros set
1099+
by the ConfigMacros.
10971100
10981101
Positional arguments:
10991102
params - a dictionary mapping a name to a ConfigParameter
1103+
macros - a dictionary mapping a name to a ConfigMacro
11001104
1101-
Return: a list of strings that encode the configuration parameters as
1102-
C pre-processor macros
1105+
Return: a list of strings that are the C pre-processor macros
11031106
"""
1104-
return ['%s=%s' % (m.macro_name, m.value) for m in params.values()
1105-
if m.value is not None]
1107+
all_macros = {
1108+
m.macro_name: m.macro_value for m in macros.values()
1109+
}
1110+
1111+
parameter_macros = {
1112+
p.macro_name: p.value for p in params.values() if p.value is not None
1113+
}
1114+
1115+
all_macros.update(parameter_macros)
1116+
macro_list = []
1117+
for name, value in all_macros.items():
1118+
# If the macro does not have a value, just append the name.
1119+
# Otherwise, append the macro as NAME=VALUE
1120+
if value is None:
1121+
macro_list.append(name)
1122+
else:
1123+
macro_list.append("%s=%s" % (name, value))
1124+
1125+
return macro_list
11061126

11071127
@staticmethod
11081128
def config_macros_to_macros(macros):
@@ -1126,8 +1146,7 @@ def config_to_macros(config):
11261146
"""
11271147
params, macros = config[0], config[1]
11281148
Config._check_required_parameters(params)
1129-
return Config.config_macros_to_macros(macros) + \
1130-
Config.parameters_to_macros(params)
1149+
return Config._parameters_and_config_macros_to_macros(params, macros)
11311150

11321151
def get_config_data_macros(self):
11331152
""" Convert a Config object to a list of C macros

tools/test/config/config_test.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from os.path import join, isfile, dirname, abspath
2626
from tools.build_api import get_config
2727
from tools.targets import set_targets_json_location, Target, TARGET_NAMES
28-
from tools.config import ConfigException, Config
28+
from tools.config import ConfigException, Config, ConfigParameter, ConfigMacro
2929

3030
def compare_config(cfg, expected):
3131
"""Compare the output of config against a dictionary of known good results
@@ -196,3 +196,31 @@ def test_basic_regions(target, overrides):
196196
assert r.size >= 0
197197
except ConfigException:
198198
pass
199+
200+
def test_parameters_and_config_macros_to_macros():
201+
"""
202+
Test that checks that parameter-generated macros override set macros
203+
"""
204+
205+
params = {
206+
"test_lib.parameter_with_macro": ConfigParameter(
207+
"parameter_with_macro",
208+
{
209+
"macro_name": "CUSTOM_MACRO_NAME",
210+
"value": 1
211+
},
212+
"test_lib",
213+
"library"
214+
)
215+
}
216+
217+
macros = {
218+
"CUSTOM_MACRO_NAME": ConfigMacro(
219+
"CUSTOM_MACRO_NAME=2",
220+
"dummy",
221+
"application"
222+
)
223+
}
224+
225+
macro_list = Config._parameters_and_config_macros_to_macros(params, macros)
226+
assert macro_list == ["CUSTOM_MACRO_NAME=1"]

0 commit comments

Comments
 (0)