Skip to content

Commit 927caca

Browse files
committed
Introduce response files for ARMCC and GCC toolchains and also document code
1 parent 5828ebd commit 927caca

File tree

3 files changed

+98
-27
lines changed

3 files changed

+98
-27
lines changed

tools/toolchains/arm.py

Lines changed: 48 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
@@ -82,14 +82,6 @@ def remove_option(self, option):
8282
if option in tool:
8383
tool.remove(option)
8484

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

120+
@hook_tool
121+
def assemble(self, source, object, includes):
122+
# Preprocess first, then assemble
123+
tempfile = object + '.E.s'
124+
125+
# Build preprocess assemble command
126+
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]
127+
128+
# Build main assemble command
129+
cmd = self.asm + ["-o", object, tempfile]
130+
131+
# Call cmdline hook
132+
cmd_pre = self.hook.get_cmdline_assembler(cmd_pre)
133+
cmd = self.hook.get_cmdline_assembler(cmd)
134+
135+
# Return command array, don't execute
136+
return [cmd_pre, cmd]
137+
138+
139+
@hook_tool
128140
def link(self, output, objects, libraries, lib_dirs, mem_map):
129141
if len(lib_dirs):
130142
args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
@@ -134,26 +146,44 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
134146
if mem_map:
135147
args.extend(["--scatter", mem_map])
136148

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

140-
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])
141168

142169
@hook_tool
143170
def binary(self, resources, elf, bin):
144-
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)
145176

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

149-
self.default_cmd(args)
150180

151181
class ARM_STD(ARM):
152182
def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False):
153183
ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose)
154184
self.cc += ["-D__ASSERT_MSG"]
155185
self.cppc += ["-D__ASSERT_MSG"]
156-
self.ld.append("--libpath=%s" % ARM_LIB)
186+
self.ld.extend(["--libpath", ARM_LIB])
157187

158188

159189
class ARM_MICRO(ARM):
@@ -184,4 +214,4 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
184214
elif target.core in ["Cortex-M0", "Cortex-M0+"]:
185215
self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]])
186216
else:
187-
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
@@ -96,9 +96,6 @@ def __init__(self, target, options=None, notify=None, macros=None, silent=False,
9696
self.ar = join(tool_path, "arm-none-eabi-ar")
9797
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
9898

99-
def assemble(self, source, object, includes):
100-
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])]
101-
10299
def parse_dependencies(self, dep_path):
103100
dependencies = []
104101
buff = open(dep_path).readlines()
@@ -164,8 +161,24 @@ def parse_output(self, output):
164161
)
165162

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

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

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

190229

191230
class GCC_ARM(GCC):

tools/toolchains/iar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,13 @@ def parse_dependencies(self, dep_path):
105105
def assemble(self, source, object, includes):
106106
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])]
107107

108+
@hook_tool
108109
def archive(self, objects, lib_path):
109110
if exists(lib_path):
110111
remove(lib_path)
111112
self.default_cmd([self.ar, lib_path] + objects)
112113

114+
@hook_tool
113115
def link(self, output, objects, libraries, lib_dirs, mem_map):
114116
args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"]
115117
self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries))

0 commit comments

Comments
 (0)