Skip to content

Commit 9d1fbda

Browse files
authored
Merge pull request #2377 from sarahmarshy/flag_revision
Pass only relevant defines at each stage of compilation
2 parents 150fd07 + 7089062 commit 9d1fbda

File tree

8 files changed

+87
-83
lines changed

8 files changed

+87
-83
lines changed

tools/export/exporters.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, target, inputDir, program_name, build_url_resolver, extra_sym
4343
self.build_url_resolver = build_url_resolver
4444
jinja_loader = FileSystemLoader(os.path.dirname(os.path.abspath(__file__)))
4545
self.jinja_environment = Environment(loader=jinja_loader)
46-
self.extra_symbols = extra_symbols
46+
self.extra_symbols = extra_symbols if extra_symbols else []
4747
self.config_macros = []
4848
self.sources_relative = sources_relative
4949
self.config_header = None
@@ -59,6 +59,11 @@ def flags(self):
5959
def progen_flags(self):
6060
if not hasattr(self, "_progen_flag_cache") :
6161
self._progen_flag_cache = dict([(key + "_flags", value) for key,value in self.flags.iteritems()])
62+
asm_defines = ["-D"+symbol for symbol in self.toolchain.get_symbols(True)]
63+
c_defines = ["-D" + symbol for symbol in self.toolchain.get_symbols()]
64+
self._progen_flag_cache['asm_flags'] += asm_defines
65+
self._progen_flag_cache['c_flags'] += c_defines
66+
self._progen_flag_cache['cxx_flags'] += c_defines
6267
if self.config_header:
6368
self._progen_flag_cache['c_flags'] += self.toolchain.get_config_option(self.config_header)
6469
self._progen_flag_cache['cxx_flags'] += self.toolchain.get_config_option(self.config_header)
@@ -214,11 +219,16 @@ def get_symbols(self, add_extra_symbols=True):
214219
""" This function returns symbols which must be exported.
215220
Please add / overwrite symbols in each exporter separately
216221
"""
217-
symbols = self.toolchain.get_symbols() + self.config_macros
222+
218223
# We have extra symbols from e.g. libraries, we want to have them also added to export
219-
if add_extra_symbols:
220-
if self.extra_symbols is not None:
221-
symbols.extend(self.extra_symbols)
224+
extra = self.extra_symbols if add_extra_symbols else []
225+
if hasattr(self, "MBED_CONFIG_HEADER_SUPPORTED") and self.MBED_CONFIG_HEADER_SUPPORTED:
226+
# If the config header is supported, we will preinclude it and do not not
227+
# need the macros as preprocessor flags
228+
return extra
229+
230+
symbols = self.toolchain.get_symbols(True) + self.toolchain.get_symbols() \
231+
+ self.config_macros + extra
222232
return symbols
223233

224234
def zip_working_directory_and_clean_up(tempdirectory=None, destination=None, program_name=None, clean=True):

tools/export/iar.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ def generate(self, progen_build=False):
7676
project_data['tool_specific']['iar'].setdefault("misc", {})
7777
project_data['tool_specific']['iar'].update(tool_specific['iar'])
7878
project_data['tool_specific']['iar']['misc'].update(self.progen_flags)
79-
project_data['tool_specific']['iar']['misc']['asm_flags'].extend(
80-
['-D%s' % d for d in self.toolchain.get_symbols()])
8179
# VLA is enabled via template IccAllowVLA
8280
project_data['tool_specific']['iar']['misc']['c_flags'].remove("--vla")
8381
project_data['common']['build_dir'] = os.path.join(project_data['common']['build_dir'], 'iar_arm')

tools/export/uvision4.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ def generate(self, progen_build=False):
7373

7474
# get flags from toolchain and apply
7575
project_data['tool_specific']['uvision']['misc'] = {}
76-
# asm flags only, common are not valid within uvision project, they are armcc specific
77-
project_data['tool_specific']['uvision']['misc']['asm_flags'] = list(set(self.progen_flags['asm_flags']))
76+
# need to make this a string for progen. Only adds preprocessor when "macros" set
77+
asm_flag_string = '--cpreproc --cpreproc_opts=-D__ASSERT_MSG,' + ",".join(
78+
list(set(self.progen_flags['asm_flags'])))
79+
project_data['tool_specific']['uvision']['misc']['asm_flags'] = [asm_flag_string]
7880
# cxx flags included, as uvision have them all in one tab
79-
project_data['tool_specific']['uvision']['misc']['c_flags'] = list(set(self.progen_flags['common_flags'] + self.progen_flags['c_flags'] + self.progen_flags['cxx_flags']))
81+
project_data['tool_specific']['uvision']['misc']['c_flags'] = list(set(
82+
['-D__ASSERT_MSG'] + self.progen_flags['common_flags'] + self.progen_flags['c_flags'] + self.progen_flags[
83+
'cxx_flags']))
8084
# not compatible with c99 flag set in the template
8185
project_data['tool_specific']['uvision']['misc']['c_flags'].remove("--c99")
8286
# cpp is not required as it's implicit for cpp files
@@ -85,17 +89,6 @@ def generate(self, progen_build=False):
8589
project_data['tool_specific']['uvision']['misc']['c_flags'].remove("--no_vla")
8690
project_data['tool_specific']['uvision']['misc']['ld_flags'] = self.progen_flags['ld_flags']
8791

88-
i = 0
89-
for macro in project_data['common']['macros']:
90-
# armasm does not like floating numbers in macros, timestamp to int
91-
if macro.startswith('MBED_BUILD_TIMESTAMP'):
92-
timestamp = macro[len('MBED_BUILD_TIMESTAMP='):]
93-
project_data['common']['macros'][i] = 'MBED_BUILD_TIMESTAMP=' + str(int(float(timestamp)))
94-
# armasm does not even accept MACRO=string
95-
if macro.startswith('MBED_USERNAME'):
96-
project_data['common']['macros'].pop(i)
97-
i += 1
98-
project_data['common']['macros'].append('__ASSERT_MSG')
9992
project_data['common']['build_dir'] = project_data['common']['build_dir'] + '\\' + 'uvision4'
10093
if progen_build:
10194
self.progen_gen_file('uvision', project_data, True)

tools/export/uvision5.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ def generate(self, progen_build=False):
7373

7474
# get flags from toolchain and apply
7575
project_data['tool_specific']['uvision5']['misc'] = {}
76-
# asm flags only, common are not valid within uvision project, they are armcc specific
77-
project_data['tool_specific']['uvision5']['misc']['asm_flags'] = list(set(self.progen_flags['asm_flags']))
76+
77+
# need to make this a string got progen. Only adds preprocessor when "macros" set
78+
asm_flag_string = '--cpreproc --cpreproc_opts=-D__ASSERT_MSG,' + ",".join(list(set(self.progen_flags['asm_flags'])))
79+
project_data['tool_specific']['uvision5']['misc']['asm_flags'] = [asm_flag_string]
7880
# cxx flags included, as uvision have them all in one tab
79-
project_data['tool_specific']['uvision5']['misc']['c_flags'] = list(set(self.progen_flags['common_flags'] + self.progen_flags['c_flags'] + self.progen_flags['cxx_flags']))
81+
project_data['tool_specific']['uvision5']['misc']['c_flags'] = list(set(['-D__ASSERT_MSG']+self.progen_flags['common_flags'] + self.progen_flags['c_flags'] + self.progen_flags['cxx_flags']))
8082
# not compatible with c99 flag set in the template
8183
project_data['tool_specific']['uvision5']['misc']['c_flags'].remove("--c99")
8284
# cpp is not required as it's implicit for cpp files
@@ -85,17 +87,6 @@ def generate(self, progen_build=False):
8587
project_data['tool_specific']['uvision5']['misc']['c_flags'].remove("--no_vla")
8688
project_data['tool_specific']['uvision5']['misc']['ld_flags'] = self.progen_flags['ld_flags']
8789

88-
i = 0
89-
for macro in project_data['common']['macros']:
90-
# armasm does not like floating numbers in macros, timestamp to int
91-
if macro.startswith('MBED_BUILD_TIMESTAMP'):
92-
timestamp = macro[len('MBED_BUILD_TIMESTAMP='):]
93-
project_data['common']['macros'][i] = 'MBED_BUILD_TIMESTAMP=' + str(int(float(timestamp)))
94-
# armasm does not even accept MACRO=string
95-
if macro.startswith('MBED_USERNAME'):
96-
project_data['common']['macros'].pop(i)
97-
i += 1
98-
project_data['common']['macros'].append('__ASSERT_MSG')
9990
project_data['common']['build_dir'] = project_data['common']['build_dir'] + '\\' + 'uvision5'
10091
if progen_build:
10192
self.progen_gen_file('uvision5', project_data, True)

tools/toolchains/__init__.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
230230
self.macros = macros or []
231231

232232
# Macros generated from toolchain and target rules/features
233-
self.symbols = None
233+
self.asm_symbols = None
234+
self.cxx_symbols = None
234235

235236
# Labels generated from toolchain and target rules/features (used for selective build)
236237
self.labels = None
@@ -372,36 +373,50 @@ def notify(self, event):
372373
event['toolchain'] = self
373374
return self.notify_fun(event, self.silent)
374375

375-
def get_symbols(self):
376-
if self.symbols is None:
377-
# Target and Toolchain symbols
378-
labels = self.get_labels()
379-
self.symbols = ["TARGET_%s" % t for t in labels['TARGET']]
380-
self.symbols.extend(["TOOLCHAIN_%s" % t for t in labels['TOOLCHAIN']])
381-
382-
# Cortex CPU symbols
383-
if self.target.core in mbedToolchain.CORTEX_SYMBOLS:
384-
self.symbols.extend(mbedToolchain.CORTEX_SYMBOLS[self.target.core])
385-
386-
# Symbols defined by the on-line build.system
387-
self.symbols.extend(['MBED_BUILD_TIMESTAMP=%s' % self.timestamp, 'TARGET_LIKE_MBED', '__MBED__=1'])
388-
if MBED_ORG_USER:
389-
self.symbols.append('MBED_USERNAME=' + MBED_ORG_USER)
390-
391-
# Add target's symbols
392-
self.symbols += self.target.macros
393-
# Add target's hardware
394-
self.symbols += ["DEVICE_" + data + "=1" for data in self.target.device_has]
395-
# Add target's features
396-
self.symbols += ["FEATURE_" + data + "=1" for data in self.target.features]
397-
# Add extra symbols passed via 'macros' parameter
398-
self.symbols += self.macros
399-
400-
# Form factor variables
401-
if hasattr(self.target, 'supported_form_factors'):
402-
self.symbols.extend(["TARGET_FF_%s" % t for t in self.target.supported_form_factors])
403-
404-
return list(set(self.symbols)) # Return only unique symbols
376+
def get_symbols(self, for_asm=False):
377+
if for_asm:
378+
if self.asm_symbols is None:
379+
self.asm_symbols = []
380+
381+
# Cortex CPU symbols
382+
if self.target.core in mbedToolchain.CORTEX_SYMBOLS:
383+
self.asm_symbols.extend(mbedToolchain.CORTEX_SYMBOLS[self.target.core])
384+
385+
# Add target's symbols
386+
self.asm_symbols += self.target.macros
387+
# Add extra symbols passed via 'macros' parameter
388+
self.asm_symbols += self.macros
389+
return list(set(self.asm_symbols)) # Return only unique symbols
390+
else:
391+
if self.cxx_symbols is None:
392+
# Target and Toolchain symbols
393+
labels = self.get_labels()
394+
self.cxx_symbols = ["TARGET_%s" % t for t in labels['TARGET']]
395+
self.cxx_symbols.extend(["TOOLCHAIN_%s" % t for t in labels['TOOLCHAIN']])
396+
397+
# Cortex CPU symbols
398+
if self.target.core in mbedToolchain.CORTEX_SYMBOLS:
399+
self.cxx_symbols.extend(mbedToolchain.CORTEX_SYMBOLS[self.target.core])
400+
401+
# Symbols defined by the on-line build.system
402+
self.cxx_symbols.extend(['MBED_BUILD_TIMESTAMP=%s' % self.timestamp, 'TARGET_LIKE_MBED', '__MBED__=1'])
403+
if MBED_ORG_USER:
404+
self.cxx_symbols.append('MBED_USERNAME=' + MBED_ORG_USER)
405+
406+
# Add target's symbols
407+
self.cxx_symbols += self.target.macros
408+
# Add target's hardware
409+
self.cxx_symbols += ["DEVICE_" + data + "=1" for data in self.target.device_has]
410+
# Add target's features
411+
self.cxx_symbols += ["FEATURE_" + data + "=1" for data in self.target.features]
412+
# Add extra symbols passed via 'macros' parameter
413+
self.cxx_symbols += self.macros
414+
415+
# Form factor variables
416+
if hasattr(self.target, 'supported_form_factors'):
417+
self.cxx_symbols.extend(["TARGET_FF_%s" % t for t in self.target.supported_form_factors])
418+
419+
return list(set(self.cxx_symbols)) # Return only unique symbols
405420

406421
# Extend the internal list of macros
407422
def add_macros(self, new_macros):

tools/toolchains/arm.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ def get_dep_option(self, object):
128128
def get_config_option(self, config_header):
129129
return ['--preinclude=' + config_header]
130130

131-
def get_compile_options(self, defines, includes):
131+
def get_compile_options(self, defines, includes, for_asm=False):
132132
opts = ['-D%s' % d for d in defines]
133133
if self.RESPONSE_FILES:
134134
opts += ['--via', self.get_inc_file(includes)]
135135
else:
136136
opts += ["-I%s" % i for i in includes]
137137

138-
config_header = self.get_config_header()
139-
if config_header is not None:
140-
opts = opts + self.get_config_option(config_header)
138+
if not for_asm:
139+
config_header = self.get_config_header()
140+
if config_header is not None:
141+
opts = opts + self.get_config_option(config_header)
141142
return opts
142143

143144
@hook_tool
@@ -148,7 +149,7 @@ def assemble(self, source, object, includes):
148149
tempfile = join(dir, basename(object) + '.E.s')
149150

150151
# Build preprocess assemble command
151-
cmd_pre = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-E", "-o", tempfile, source]
152+
cmd_pre = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-E", "-o", tempfile, source]
152153

153154
# Build main assemble command
154155
cmd = self.asm + ["-o", object, tempfile]

tools/toolchains/gcc.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,23 @@ def get_dep_option(self, object):
170170
def get_config_option(self, config_header):
171171
return ['-include', config_header]
172172

173-
def get_compile_options(self, defines, includes):
173+
def get_compile_options(self, defines, includes, for_asm=False):
174174
opts = ['-D%s' % d for d in defines]
175175
if self.RESPONSE_FILES:
176176
opts += ['@%s' % self.get_inc_file(includes)]
177177
else:
178178
opts += ["-I%s" % i for i in includes]
179179

180-
config_header = self.get_config_header()
181-
if config_header is not None:
182-
opts = opts + self.get_config_option(config_header)
180+
if not for_asm:
181+
config_header = self.get_config_header()
182+
if config_header is not None:
183+
opts = opts + self.get_config_option(config_header)
183184
return opts
184185

185186
@hook_tool
186187
def assemble(self, source, object, includes):
187188
# Build assemble command
188-
cmd = self.asm + self.get_compile_options(self.get_symbols(), includes) + ["-o", object, source]
189+
cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-o", object, source]
189190

190191
# Call cmdline hook
191192
cmd = self.hook.get_cmdline_assembler(cmd)

tools/toolchains/iar.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,7 @@ def get_compile_options(self, defines, includes, for_asm=False):
151151
else:
152152
opts += ["-I%s" % i for i in includes]
153153

154-
config_header = self.get_config_header()
155-
if for_asm:
156-
# The assembler doesn't support '--preinclude', so we need to add
157-
# the macros directly
158-
opts = opts + ['-D%s' % d for d in self.get_config_macros()]
159-
else:
154+
if not for_asm:
160155
config_header = self.get_config_header()
161156
if config_header is not None:
162157
opts = opts + self.get_config_option(config_header)
@@ -165,7 +160,7 @@ def get_compile_options(self, defines, includes, for_asm=False):
165160
@hook_tool
166161
def assemble(self, source, object, includes):
167162
# Build assemble command
168-
cmd = self.asm + self.get_compile_options(self.get_symbols(), includes, for_asm=True) + ["-o", object, source]
163+
cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes, True) + ["-o", object, source]
169164

170165
# Call cmdline hook
171166
cmd = self.hook.get_cmdline_assembler(cmd)

0 commit comments

Comments
 (0)