From 927caca09fc6e3201df71ac98d0b20a05e912a1b Mon Sep 17 00:00:00 2001 From: Mihail Stoyanov Date: Tue, 12 Apr 2016 11:37:15 +0100 Subject: [PATCH 1/2] Introduce response files for ARMCC and GCC toolchains and also document code --- tools/toolchains/arm.py | 66 ++++++++++++++++++++++++++++++----------- tools/toolchains/gcc.py | 55 +++++++++++++++++++++++++++++----- tools/toolchains/iar.py | 2 ++ 3 files changed, 97 insertions(+), 26 deletions(-) diff --git a/tools/toolchains/arm.py b/tools/toolchains/arm.py index 4e7d82d3f6..4535439629 100644 --- a/tools/toolchains/arm.py +++ b/tools/toolchains/arm.py @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. """ import re -from os.path import join +from os.path import join, dirname from tools.toolchains import mbedToolchain from tools.settings import ARM_BIN, ARM_INC, ARM_LIB, MY_ARM_CLIB, ARM_CPPLIB @@ -82,14 +82,6 @@ class ARM(mbedToolchain): if option in tool: tool.remove(option) - def assemble(self, source, object, includes): - # Preprocess first, then assemble - tempfile = object + '.E.s' - return [ - self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-E", "-o", tempfile, source], - self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile]) - ] - def parse_dependencies(self, dep_path): dependencies = [] for line in open(dep_path).readlines(): @@ -125,6 +117,26 @@ class ARM(mbedToolchain): def archive(self, objects, lib_path): self.default_cmd([self.ar, '-r', lib_path] + objects) + @hook_tool + def assemble(self, source, object, includes): + # Preprocess first, then assemble + tempfile = object + '.E.s' + + # Build preprocess assemble command + 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] + + # Build main assemble command + cmd = self.asm + ["-o", object, tempfile] + + # Call cmdline hook + cmd_pre = self.hook.get_cmdline_assembler(cmd_pre) + cmd = self.hook.get_cmdline_assembler(cmd) + + # Return command array, don't execute + return [cmd_pre, cmd] + + + @hook_tool def link(self, output, objects, libraries, lib_dirs, mem_map): if len(lib_dirs): args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"] @@ -134,26 +146,44 @@ class ARM(mbedToolchain): if mem_map: args.extend(["--scatter", mem_map]) - if hasattr(self.target, "link_cmdline_hook"): - args = self.target.link_cmdline_hook(self.__class__.__name__, args) + # Build linker command + cmd = self.ld + args + objects + libraries + self.sys_libs + print self.ld - self.default_cmd(self.ld + args + objects + libraries + self.sys_libs) + # Call cmdline hook + cmd = self.hook.get_cmdline_linker(cmd) + + # Split link command to linker executable + response file + link_files = join(dirname(output), ".link_files.txt") + with open(link_files, "wb") as f: + cmd_linker = cmd[0] + cmd_list = [] + for c in cmd[1:]: + cmd_list.append(('"%s"' % c) if not c.startswith('-') else c) + string = " ".join(cmd_list).replace("\\", "/") + f.write(string) + + # Exec command + self.default_cmd([cmd_linker, '--via', link_files]) @hook_tool def binary(self, resources, elf, bin): - args = [self.elf2bin, '--bin', '-o', bin, elf] + # Build binary command + cmd = [self.elf2bin, '--bin', '-o', bin, elf] - if hasattr(self.target, "binary_cmdline_hook"): - args = self.target.binary_cmdline_hook(self.__class__.__name__, args) + # Call cmdline hook + cmd = self.hook.get_cmdline_binary(cmd) + + # Exec command + self.default_cmd(cmd) - self.default_cmd(args) class ARM_STD(ARM): def __init__(self, target, options=None, notify=None, macros=None, silent=False, extra_verbose=False): ARM.__init__(self, target, options, notify, macros, silent, extra_verbose=extra_verbose) self.cc += ["-D__ASSERT_MSG"] self.cppc += ["-D__ASSERT_MSG"] - self.ld.append("--libpath=%s" % ARM_LIB) + self.ld.extend(["--libpath", ARM_LIB]) class ARM_MICRO(ARM): @@ -184,4 +214,4 @@ class ARM_MICRO(ARM): elif target.core in ["Cortex-M0", "Cortex-M0+"]: self.sys_libs.extend([join(ARM_CPPLIB, lib+".l") for lib in ["cpp_ps", "cpprt_p"]]) else: - self.ld.append("--libpath=%s" % ARM_LIB) + self.ld.extend(["--libpath", ARM_LIB]) diff --git a/tools/toolchains/gcc.py b/tools/toolchains/gcc.py index 82f4584685..20d09550b6 100644 --- a/tools/toolchains/gcc.py +++ b/tools/toolchains/gcc.py @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. """ import re -from os.path import join, basename, splitext +from os.path import join, basename, splitext, dirname from tools.toolchains import mbedToolchain from tools.settings import GCC_ARM_PATH, GCC_CR_PATH @@ -96,9 +96,6 @@ class GCC(mbedToolchain): self.ar = join(tool_path, "arm-none-eabi-ar") self.elf2bin = join(tool_path, "arm-none-eabi-objcopy") - def assemble(self, source, object, includes): - 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])] - def parse_dependencies(self, dep_path): dependencies = [] buff = open(dep_path).readlines() @@ -164,8 +161,24 @@ class GCC(mbedToolchain): ) def archive(self, objects, lib_path): - self.default_cmd([self.ar, "rcs", lib_path] + objects) + # Build archive command + cmd = [self.ar, "rcs", lib_path] + objects + + # Exec cmd + self.default_cmd(cmd) + @hook_tool + def assemble(self, source, object, includes): + # Build assemble command + cmd = self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source] + + # Call cmdline hook + cmd = self.hook.get_cmdline_assembler(cmd) + + # Return command array, don't execute + return [cmd] + + @hook_tool def link(self, output, objects, libraries, lib_dirs, mem_map): libs = [] for l in libraries: @@ -179,13 +192,39 @@ class GCC(mbedToolchain): # image is not correctly retargeted if self.CIRCULAR_DEPENDENCIES: libs.extend(libs) + + # Build linker command + cmd = self.ld + ["-T", mem_map, "-o", output] + objects + for L in lib_dirs: + cmd.extend(['-L', L]) + cmd.extend(libs) - self.default_cmd(self.hook.get_cmdline_linker(self.ld + ["-T%s" % mem_map, "-o", output] + - objects + ["-L%s" % L for L in lib_dirs] + libs)) + # Call cmdline hook + cmd = self.hook.get_cmdline_linker(cmd) + + # Split link command to linker executable + response file + link_files = join(dirname(output), ".link_files.txt") + with open(link_files, "wb") as f: + cmd_linker = cmd[0] + cmd_list = [] + for c in cmd[1:]: + cmd_list.append(('"%s"' % c) if not c.startswith('-') else c) + string = " ".join(cmd_list).replace("\\", "/") + f.write(string) + + # Exec command + self.default_cmd([cmd_linker, "@%s" % link_files]) @hook_tool def binary(self, resources, elf, bin): - self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, "-O", "binary", elf, bin])) + # Build binary command + cmd = [self.elf2bin, "-O", "binary", elf, bin] + + # Call cmdline hook + cmd = self.hook.get_cmdline_binary(cmd) + + # Exec command + self.default_cmd(cmd) class GCC_ARM(GCC): diff --git a/tools/toolchains/iar.py b/tools/toolchains/iar.py index b795c4e520..6c3342bfad 100644 --- a/tools/toolchains/iar.py +++ b/tools/toolchains/iar.py @@ -105,11 +105,13 @@ class IAR(mbedToolchain): def assemble(self, source, object, includes): 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])] + @hook_tool def archive(self, objects, lib_path): if exists(lib_path): remove(lib_path) self.default_cmd([self.ar, lib_path] + objects) + @hook_tool def link(self, output, objects, libraries, lib_dirs, mem_map): args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"] self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries)) From 61298334c2a846f68d5032257d2f15131b9df28e Mon Sep 17 00:00:00 2001 From: Mihail Stoyanov Date: Tue, 12 Apr 2016 12:11:22 +0100 Subject: [PATCH 2/2] Introduce response file for linking with IAR toolchain --- tools/toolchains/arm.py | 1 - tools/toolchains/iar.py | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/tools/toolchains/arm.py b/tools/toolchains/arm.py index b70bf6088c..72f672e2b3 100644 --- a/tools/toolchains/arm.py +++ b/tools/toolchains/arm.py @@ -149,7 +149,6 @@ class ARM(mbedToolchain): # Build linker command cmd = self.ld + args + objects + libraries + self.sys_libs - print self.ld # Call cmdline hook cmd = self.hook.get_cmdline_linker(cmd) diff --git a/tools/toolchains/iar.py b/tools/toolchains/iar.py index 820e28a0f2..49beadcf6b 100644 --- a/tools/toolchains/iar.py +++ b/tools/toolchains/iar.py @@ -16,7 +16,7 @@ limitations under the License. """ import re from os import remove -from os.path import join, exists +from os.path import join, exists, dirname from tools.toolchains import mbedToolchain from tools.settings import IAR_PATH @@ -103,8 +103,16 @@ class IAR(mbedToolchain): return [path.strip() for path in open(dep_path).readlines() if (path and not path.isspace())] + @hook_tool def assemble(self, source, object, includes): - 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])] + # Build assemble command + cmd = self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source] + + # Call cmdline hook + cmd = self.hook.get_cmdline_assembler(cmd) + + # Return command array, don't execute + return [cmd] @hook_tool def archive(self, objects, lib_path): @@ -114,9 +122,32 @@ class IAR(mbedToolchain): @hook_tool def link(self, output, objects, libraries, lib_dirs, mem_map): - args = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"] - self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries)) + # Build linker command + cmd = [self.ld, "-o", output, "--config", mem_map, "--skip_dynamic_initialization"] + objects + libraries + + # Call cmdline hook + cmd = self.hook.get_cmdline_linker(cmd) + + # Split link command to linker executable + response file + link_files = join(dirname(output), ".link_files.txt") + with open(link_files, "wb") as f: + cmd_linker = cmd[0] + cmd_list = [] + for c in cmd[1:]: + cmd_list.append(('"%s"' % c) if not c.startswith('-') else c) + string = " ".join(cmd_list).replace("\\", "/") + f.write(string) + + # Exec command + self.default_cmd([cmd_linker, '-f', link_files]) @hook_tool def binary(self, resources, elf, bin): - self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, '--bin', elf, bin])) + # Build binary command + cmd = [self.elf2bin, "--bin", elf, bin] + + # Call cmdline hook + cmd = self.hook.get_cmdline_binary(cmd) + + # Exec command + self.default_cmd(cmd)