Skip to content

Commit 85eca37

Browse files
author
Bogdan Marinescu
committed
Added toolchain support for configuration in prefix headers
This commit uses the previously introduced feature of generating configuration data as a C header file rather than as command line macro definitions. Each toolchain was modified to use prefix headers if requested, and build_api.py was modified to set up the toolchain's prefix header content using the data generated by the config system. Tested by compiling blinky for GCC and ARMCC. I'm having a few issues with my IAR license currently, but both ARMCC and IAR use the same `--preinclude` option for prefix headers, so this shouldn't be an issue. Note that at the moment all exporters still use the previous configuration data mechanism (individual macro definitions as opposed to a prefix header). Exporters will be updated in one or more PRs that will follow.
1 parent 80a70fc commit 85eca37

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

tools/build_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ def build_project(src_path, build_path, target, toolchain_name,
232232
prev_features = features
233233
config.validate_config()
234234

235-
# And add the configuration macros to the toolchain
236-
toolchain.add_macros(config.get_config_data_macros())
235+
# Set the toolchain's prefix header with the config data
236+
toolchain.set_prefix_header_content(config.get_config_data_header())
237237

238238
# Compile Sources
239239
for path in src_paths:
@@ -403,8 +403,8 @@ def build_library(src_paths, build_path, target, toolchain_name,
403403
prev_features = features
404404
config.validate_config()
405405

406-
# And add the configuration macros to the toolchain
407-
toolchain.add_macros(config.get_config_data_macros())
406+
# Set the toolchain's prefix header with the config data
407+
toolchain.set_prefix_header_content(config.get_config_data_header())
408408

409409
# Compile Sources
410410
for path in src_paths:

tools/toolchains/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
265265

266266
self.flags = deepcopy(self.DEFAULT_FLAGS)
267267

268+
# prefix_header_content will hold the content of the prefix header (if used)
269+
self.prefix_header_content = None
270+
268271
def get_output(self):
269272
return self.output
270273

@@ -867,6 +870,26 @@ def mem_stats(self, map):
867870
map_csv = splitext(map)[0] + "_map.csv"
868871
memap.generate_output('csv-ci', map_csv)
869872

873+
# "Prefix headers" are automatically included by the compiler at the beginning of
874+
# each source file.
875+
# header_content - the content of the prefix header file.
876+
def set_prefix_header_content(self, header_content):
877+
self.prefix_header_content = header_content
878+
879+
# Return the location of the prefix header. This function will create the prefix
880+
# header first if needed. The header will be written in a file called "mbed_prefix.h"
881+
# located in the project's build directory.
882+
# If prefix headers are not used (self.prefix_header_content is None), the function
883+
# returns None
884+
def get_prefix_header(self):
885+
if self.prefix_header_content is None:
886+
return None
887+
prefix_file = join(self.build_dir, "mbed_prefix.h")
888+
if not exists(prefix_file):
889+
with open(prefix_file, "wt") as f:
890+
f.write(self.prefix_header_content)
891+
return prefix_file
892+
870893

871894
from tools.settings import ARM_BIN
872895
from tools.settings import GCC_ARM_PATH, GCC_CR_PATH

tools/toolchains/arm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ def get_dep_option(self, object):
115115
return ["--depend", dep_path]
116116

117117
def get_compile_options(self, defines, includes):
118-
return ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
118+
opts = ['-D%s' % d for d in defines] + ['--via', self.get_inc_file(includes)]
119+
prefix_header = self.get_prefix_header()
120+
if prefix_header is not None:
121+
opts = opts + ['--preinclude', prefix_header]
122+
return opts
119123

120124
@hook_tool
121125
def assemble(self, source, object, includes):

tools/toolchains/gcc.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ def get_dep_option(self, object):
166166
return ["-MD", "-MF", dep_path]
167167

168168
def get_compile_options(self, defines, includes):
169-
return ['-D%s' % d for d in defines] + ['@%s' % self.get_inc_file(includes)]
169+
opts = ['-D%s' % d for d in defines] + ['@%s' % self.get_inc_file(includes)]
170+
prefix_header = self.get_prefix_header()
171+
if prefix_header is not None:
172+
opts = opts + ['-include', prefix_header]
173+
return opts
170174

171175
@hook_tool
172176
def assemble(self, source, object, includes):

tools/toolchains/iar.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ def cc_extra(self, object):
119119
return ["-l", base + '.s.txt']
120120

121121
def get_compile_options(self, defines, includes):
122-
return ['-D%s' % d for d in defines] + ['-f', self.get_inc_file(includes)]
122+
opts = ['-D%s' % d for d in defines] + ['-f', self.get_inc_file(includes)]
123+
prefix_header = self.get_prefix_header()
124+
if prefix_header is not None:
125+
opts = opts + ['--preinclude', prefix_header]
126+
return opts
123127

124128
@hook_tool
125129
def assemble(self, source, object, includes):

0 commit comments

Comments
 (0)