Skip to content

Commit 2de453c

Browse files
authored
Merge pull request #3706 from c1728p9/pre_process
Use the C Pre-Processor on GCC Linker scripts
2 parents 942cf8c + 3155e71 commit 2de453c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

tools/export/makefile/Makefile.tmpl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ CC = {{cc_cmd}}
6767
CPP = {{cppc_cmd}}
6868
LD = {{ld_cmd}}
6969
ELF2BIN = {{elf2bin_cmd}}
70+
{% if pp_cmd -%}
71+
PREPROC = {{pp_cmd}}
72+
{%- endif %}
7073
{% if hex_files %}
7174
SREC_CAT = srec_cat
7275
{%- endif %}
@@ -119,8 +122,13 @@ all: $(PROJECT).bin $(PROJECT).hex size
119122
+@echo "Compile: $(notdir $<)"
120123
@$(CPP) $(CXX_FLAGS) $(INCLUDE_PATHS) -o $@ $<
121124

125+
{% if pp_cmd %}
126+
$(PROJECT).link_script{{link_script_ext}}: $(LINKER_SCRIPT)
127+
@$(PREPROC) $< -o $@
128+
{% endif %}
129+
122130
{% block target_project_elf %}
123-
$(PROJECT).elf: $(OBJECTS) $(SYS_OBJECTS) $(LINKER_SCRIPT)
131+
$(PROJECT).elf: $(OBJECTS) $(SYS_OBJECTS) {% if pp_cmd -%} $(PROJECT).link_script{{link_script_ext}} {% else%} $(LINKER_SCRIPT) {% endif %}
124132
+@echo "link: $(notdir $@)"
125133
@$(LD) $(LD_FLAGS) {{link_script_option}} $(filter %{{link_script_ext}}, $^) $(LIBRARY_PATHS) --output $@ $(filter %.o, $^) $(LIBRARIES) $(LD_SYS_LIBS)
126134
{% endblock %}

tools/export/makefile/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ def generate(self):
8787
'user_library_flag': self.USER_LIBRARY_FLAG,
8888
}
8989

90+
if hasattr(self.toolchain, "preproc"):
91+
ctx['pp_cmd'] = " ".join(["\'" + part + "\'" for part
92+
in ([basename(self.toolchain.preproc[0])] +
93+
self.toolchain.preproc[1:] +
94+
self.toolchain.ld[1:])])
95+
else:
96+
ctx['pp_cmd'] = None
97+
9098
for key in ['include_paths', 'library_paths', 'linker_script',
9199
'hex_files']:
92100
if isinstance(ctx[key], list):

tools/toolchains/gcc.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
limitations under the License.
1616
"""
1717
import re
18-
from os.path import join, basename, splitext
18+
from os.path import join, basename, splitext, dirname
1919

2020
from tools.toolchains import mbedToolchain, TOOLCHAIN_PATHS
2121
from tools.hooks import hook_tool
@@ -93,6 +93,7 @@ def __init__(self, target, notify=None, macros=None,
9393
self.flags['ld'] += self.cpu
9494
self.ld = [join(tool_path, "arm-none-eabi-gcc")] + self.flags['ld']
9595
self.sys_libs = ["stdc++", "supc++", "m", "c", "gcc"]
96+
self.preproc = [join(tool_path, "arm-none-eabi-cpp"), "-E", "-P"]
9697

9798
self.ar = join(tool_path, "arm-none-eabi-ar")
9899
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
@@ -213,6 +214,15 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
213214
libs.append("-l%s" % name[3:])
214215
libs.extend(["-l%s" % l for l in self.sys_libs])
215216

217+
# Preprocess
218+
if mem_map:
219+
preproc_output = join(dirname(output), ".link_script.ld")
220+
cmd = (self.preproc + [mem_map] + self.ld[1:] +
221+
[ "-o", preproc_output])
222+
self.cc_verbose("Preproc: %s" % ' '.join(cmd))
223+
self.default_cmd(cmd)
224+
mem_map = preproc_output
225+
216226
# Build linker command
217227
map_file = splitext(output)[0] + ".map"
218228
cmd = self.ld + ["-o", output, "-Wl,-Map=%s" % map_file] + objects + ["-Wl,--start-group"] + libs + ["-Wl,--end-group"]

0 commit comments

Comments
 (0)