Skip to content

Commit 7996649

Browse files
committed
Merge pull request #43 from screamerbg/master
Add reponse files for GCC, ARMCC and IAR linking
2 parents 76f6374 + 6129833 commit 7996649

File tree

3 files changed

+133
-32
lines changed

3 files changed

+133
-32
lines changed

tools/toolchains/arm.py

Lines changed: 47 additions & 18 deletions
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
18+
from os.path import join, dirname
1919

2020
from tools.toolchains import mbedToolchain
2121
from tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB
@@ -83,14 +83,6 @@ def remove_option(self, option):
8383
if option in tool:
8484
tool.remove(option)
8585

86-
def assemble(self, source, object, includes):
87-
# Preprocess first, then assemble
88-
tempfile = object + '.E.s'
89-
return [
90-
self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source],
91-
self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile])
92-
]
93-
9486
def parse_dependencies(self, dep_path):
9587
dependencies = []
9688
for line in open(dep_path).readlines():
@@ -126,6 +118,26 @@ def get_dep_opt(self, dep_path):
126118
def archive(self, objects, lib_path):
127119
self.default_cmd([self.ar, '-r', lib_path] + objects)
128120

121+
@hook_tool
122+
def assemble(self, source, object, includes):
123+
# Preprocess first, then assemble
124+
tempfile = object + '.E.s'
125+
126+
# Build preprocess assemble command
127+
cmd_pre = self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source]
128+
129+
# Build main assemble command
130+
cmd = self.asm + ["-o", object, tempfile]
131+
132+
# Call cmdline hook
133+
cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
134+
cmd = self.hook.get_cmdline_assembler(cmd)
135+
136+
# Return command array, don't execute
137+
return [cmd_pre, cmd]
138+
139+
140+
@hook_tool
129141
def link(self, output, objects, libraries, lib_dirs, mem_map):
130142
if len(lib_dirs):
131143
args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
@@ -135,26 +147,43 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
135147
if mem_map:
136148
args.extend(["--scatter", mem_map])
137149

138-
if hasattr(self.target, "link_cmdline_hook"):
139-
args = self.target.link_cmdline_hook(self.__class__.__name__, args)
150+
# Build linker command
151+
cmd = self.ld + args + objects + libraries + self.sys_libs
152+
153+
# Call cmdline hook
154+
cmd = self.hook.get_cmdline_linker(cmd)
140155

141-
self.default_cmd(self.ld + args + objects + libraries + self.sys_libs)
156+
# Split link command to linker executable + response file
157+
link_files = join(dirname(output), ".link_files.txt")
158+
with open(link_files, "wb") as f:
159+
cmd_linker = cmd[0]
160+
cmd_list = []
161+
for c in cmd[1:]:
162+
cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
163+
string = " ".join(cmd_list).replace("\\", "/")
164+
f.write(string)
165+
166+
# Exec command
167+
self.default_cmd([cmd_linker, '--via', link_files])
142168

143169
@hook_tool
144170
def binary(self, resources, elf, bin):
145-
args = [self.elf2bin, '--bin', '-o', bin, elf]
171+
# Build binary command
172+
cmd = [self.elf2bin, '--bin', '-o', bin, elf]
173+
174+
# Call cmdline hook
175+
cmd = self.hook.get_cmdline_binary(cmd)
146176

147-
if hasattr(self.target, "binary_cmdline_hook"):
148-
args = self.target.binary_cmdline_hook(self.__class__.__name__, args)
177+
# Exec command
178+
self.default_cmd(cmd)
149179

150-
self.default_cmd(args)
151180

152181
class ARM_STD(ARM):
153182
def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
154183
ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
155184
self.cc += ["-D__ASSERT_MSG"]
156185
self.cppc += ["-D__ASSERT_MSG"]
157-
self.ld.append("--libpath=%s" % ARM_LIB)
186+
self.ld.extend(["--libpath", ARM_LIB])
158187

159188

160189
class ARM_MICRO(ARM):
@@ -185,4 +214,4 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
185214
elif target.core in ["Cortex-M0", "Cortex-M0+"]:
186215
self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
187216
else:
188-
self.ld.append("--libpath=%s" % ARM_LIB)
217+
self.ld.extend(["--libpath", ARM_LIB])

tools/toolchains/gcc.py

Lines changed: 48 additions & 9 deletions
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
2121
from tools.settings import GCC_ARM_PATH, GCC_CR_PATH
@@ -97,9 +97,6 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
9797
self.ar = join(tool_path, "arm-none-eabi-ar")
9898
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
9999

100-
def assemble(self, source, object, includes):
101-
return [self.hook.get_cmdline_assembler(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source])]
102-
103100
def parse_dependencies(self, dep_path):
104101
dependencies = []
105102
buff = open(dep_path).readlines()
@@ -165,8 +162,24 @@ def parse_output(self, output):
165162
)
166163

167164
def archive(self, objects, lib_path):
168-
self.default_cmd([self.ar, "rcs", lib_path] + objects)
165+
# Build archive command
166+
cmd = [self.ar, "rcs", lib_path] + objects
167+
168+
# Exec cmd
169+
self.default_cmd(cmd)
170+
171+
@hook_tool
172+
def assemble(self, source, object, includes):
173+
# Build assemble command
174+
cmd = self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source]
175+
176+
# Call cmdline hook
177+
cmd = self.hook.get_cmdline_assembler(cmd)
169178

179+
# Return command array, don't execute
180+
return [cmd]
181+
182+
@hook_tool
170183
def link(self, output, objects, libraries, lib_dirs, mem_map):
171184
libs = []
172185
for l in libraries:
@@ -180,13 +193,39 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
180193
# image is not correctly retargeted
181194
if self.CIRCULAR_DEPENDENCIES:
182195
libs.extend(libs)
183-
184-
self.default_cmd(self.hook.get_cmdline_linker(self.ld + ["-T%s" % mem_map, "-o", output] +
185-
objects + ["-L%s" % L for L in lib_dirs] + libs))
196+
197+
# Build linker command
198+
cmd = self.ld + ["-T", mem_map, "-o", output] + objects
199+
for L in lib_dirs:
200+
cmd.extend(['-L', L])
201+
cmd.extend(libs)
202+
203+
# Call cmdline hook
204+
cmd = self.hook.get_cmdline_linker(cmd)
205+
206+
# Split link command to linker executable + response file
207+
link_files = join(dirname(output), ".link_files.txt")
208+
with open(link_files, "wb") as f:
209+
cmd_linker = cmd[0]
210+
cmd_list = []
211+
for c in cmd[1:]:
212+
cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
213+
string = " ".join(cmd_list).replace("\\", "/")
214+
f.write(string)
215+
216+
# Exec command
217+
self.default_cmd([cmd_linker, "@%s" % link_files])
186218

187219
@hook_tool
188220
def binary(self, resources, elf, bin):
189-
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, "-O", "binary", elf, bin]))
221+
# Build binary command
222+
cmd = [self.elf2bin, "-O", "binary", elf, bin]
223+
224+
# Call cmdline hook
225+
cmd = self.hook.get_cmdline_binary(cmd)
226+
227+
# Exec command
228+
self.default_cmd(cmd)
190229

191230

192231
class GCC_ARM(GCC):

tools/toolchains/iar.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"""
1717
import re
1818
from os import remove
19-
from os.path import join, exists
19+
from os.path import join, exists, dirname
2020

2121
from tools.toolchains import mbedToolchain
2222
from tools.settings import IAR_PATH
@@ -103,18 +103,51 @@ def parse_dependencies(self, dep_path):
103103
return [path.strip() for path in open(dep_path).readlines()
104104
if (path and not path.isspace())]
105105

106+
@hook_tool
106107
def assemble(self, source, object, includes):
107-
return [self.hook.get_cmdline_assembler(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source])]
108+
# Build assemble command
109+
cmd = self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source]
110+
111+
# Call cmdline hook
112+
cmd = self.hook.get_cmdline_assembler(cmd)
113+
114+
# Return command array, don't execute
115+
return [cmd]
108116

117+
@hook_tool
109118
def archive(self, objects, lib_path):
110119
if exists(lib_path):
111120
remove(lib_path)
112121
self.default_cmd([self.ar, lib_path] + objects)
113122

123+
@hook_tool
114124
def link(self, output, objects, libraries, lib_dirs, mem_map):
115-
args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"]
116-
self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries))
125+
# Build linker command
126+
cmd = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"] + objects + libraries
127+
128+
# Call cmdline hook
129+
cmd = self.hook.get_cmdline_linker(cmd)
130+
131+
# Split link command to linker executable + response file
132+
link_files = join(dirname(output), ".link_files.txt")
133+
with open(link_files, "wb") as f:
134+
cmd_linker = cmd[0]
135+
cmd_list = []
136+
for c in cmd[1:]:
137+
cmd_list.append(('"%s"' % c) if not c.startswith('-') else c)
138+
string = " ".join(cmd_list).replace("\\", "/")
139+
f.write(string)
140+
141+
# Exec command
142+
self.default_cmd([cmd_linker, '-f', link_files])
117143

118144
@hook_tool
119145
def binary(self, resources, elf, bin):
120-
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, '--bin', elf, bin]))
146+
# Build binary command
147+
cmd = [self.elf2bin, "--bin", elf, bin]
148+
149+
# Call cmdline hook
150+
cmd = self.hook.get_cmdline_binary(cmd)
151+
152+
# Exec command
153+
self.default_cmd(cmd)

0 commit comments

Comments
 (0)