Added command line hooking option

compiler, linker, assembler and binary command lines can now be modified
using the hooks mechanism. Also, '--any_placement=first_fit' linker option
is now used only on LPC4088 using this mechanism, in order to preserve
compatibility with the other targets.
pull/124/head
Bogdan Marinescu 2013-12-05 13:48:42 +02:00
parent 6959ef77f4
commit 697acd34c0
6 changed files with 68 additions and 15 deletions

View File

@ -11,7 +11,7 @@ _hooks = {}
_running_hooks = {}
# Available hook types
_hook_types = ["binary"]
_hook_types = ["binary", "compile", "link", "assemble"]
# Available hook steps
_hook_steps = ["pre", "replace", "post"]
@ -59,10 +59,12 @@ def hook_tool(function):
class Hook:
def __init__(self, target, toolchain):
_hooks.clear()
self._cmdline_hooks = {}
self.toolchain = toolchain
target.init_hooks(self, toolchain.__class__.__name__)
def hook_add(self, hook_type, hook_step, function):
# Hook various functions directly
def _hook_add(self, hook_type, hook_step, function):
if not hook_type in _hook_types or not hook_step in _hook_steps:
return False
if not hook_type in _hooks:
@ -70,8 +72,54 @@ class Hook:
_hooks[hook_type][hook_step] = function
return True
def hook_add_compiler(self, hook_step, function):
return self._hook_add("compile", hook_step, function)
def hook_add_linker(self, hook_step, function):
return self._hook_add("link", hook_step, function)
def hook_add_assembler(self, hook_step, function):
return self._hook_add("assemble", hook_step, function)
def hook_add_binary(self, hook_step, function):
return self.hook_add("binary", hook_step, function)
return self._hook_add("binary", hook_step, function)
# Hook command lines
def _hook_cmdline(self, hook_type, function):
if not hook_type in _hook_types:
return False
self._cmdline_hooks[hook_type] = function
return True
def hook_cmdline_compiler(self, function):
return self._hook_cmdline("compile", function)
def hook_cmdline_linker(self, function):
return self._hook_cmdline("link", function)
def hook_cmdline_assembler(self, function):
return self._hook_cmdline("assemble", function)
def hook_cmdline_binary(self, function):
return self._hook_cmdline("binary", function)
# Return the command line after applying the hook
def _get_cmdline(self, hook_type, cmdline):
if self._cmdline_hooks.has_key(hook_type):
cmdline = self._cmdline_hooks[hook_type](self.toolchain.__class__.__name__, cmdline)
return cmdline
def get_cmdline_compiler(self, cmdline):
return self._get_cmdline("compile", cmdline)
def get_cmdline_linker(self, cmdline):
return self._get_cmdline("link", cmdline)
def get_cmdline_assembler(self, cmdline):
return self._get_cmdline("assemble", cmdline)
def get_cmdline_binary(self, cmdline):
return self._get_cmdline("binary", cmdline)
################################################################################

View File

@ -175,6 +175,11 @@ class LPC4088(Target):
def init_hooks(self, hook, toolchain_name):
if toolchain_name in ['ARM_STD', 'ARM_MICRO']:
hook.hook_add_binary("post", self.binary_hook)
hook.hook_cmdline_linker(self.cmdline_hook)
@staticmethod
def cmdline_hook(toolchain, cmdline):
return cmdline + ["--any_placement=first_fit"]
@staticmethod
def binary_hook(t_self, elf, binf):

View File

@ -403,7 +403,7 @@ class mbedToolchain:
command.extend(self.cc_extra(base))
self.debug(command)
_, stderr, rc = run_cmd(command, dirname(object))
_, stderr, rc = run_cmd(self.hook.get_cmdline_compiler(command), dirname(object))
# Parse output for Warnings and Errors
self.parse_output(stderr)

View File

@ -81,7 +81,7 @@ class ARM(mbedToolchain):
# Preprocess first, then assemble
tempfile = object + '.E.s'
self.default_cmd(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.default_cmd(self.asm + ["-o", object, tempfile])
self.default_cmd(self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile]))
def parse_dependencies(self, dep_path):
dependencies = []
@ -114,15 +114,15 @@ class ARM(mbedToolchain):
self.default_cmd([self.ar, '-r', lib_path] + objects)
def link(self, output, objects, libraries, lib_dirs, mem_map):
args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt", "--any_placement=first_fit"]
args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
if mem_map:
args.extend(["--scatter", mem_map])
self.default_cmd(self.ld + args + objects + libraries + self.sys_libs)
self.default_cmd(self.hook.get_cmdline_linker(self.ld + args + objects + libraries + self.sys_libs))
@hook_tool
def binary(self, elf, bin):
self.default_cmd([self.elf2bin, '--bin', '-o', bin, elf])
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, '--bin', '-o', bin, elf]))
class ARM_STD(ARM):

View File

@ -79,7 +79,7 @@ class GCC(mbedToolchain):
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
def assemble(self, source, object, includes):
self.default_cmd(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source])
self.default_cmd(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 = []
@ -155,11 +155,11 @@ class GCC(mbedToolchain):
if self.CIRCULAR_DEPENDENCIES:
libs.extend(libs)
self.default_cmd(self.ld + ["-T%s" % mem_map, "-o", output] +
objects + ["-L%s" % L for L in lib_dirs] + 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))
def binary(self, elf, bin):
self.default_cmd([self.elf2bin, "-O", "binary", elf, bin])
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, "-O", "binary", elf, bin]))
class GCC_ARM(GCC):

View File

@ -91,7 +91,7 @@ class IAR(mbedToolchain):
if (path and not path.isspace())]
def assemble(self, source, object, includes):
self.default_cmd(self.asm + ['-D%s' % s for s in self.get_symbols() + self.macros] + ["-I%s" % i for i in includes] + ["-o", object, source])
self.default_cmd(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 archive(self, objects, lib_path):
if exists(lib_path):
@ -100,7 +100,7 @@ class IAR(mbedToolchain):
def link(self, output, objects, libraries, lib_dirs, mem_map):
args = [self.ld, "-o", output, "--config", mem_map]
self.default_cmd(args + objects + libraries)
self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries))
def binary(self, elf, bin):
self.default_cmd([self.elf2bin, '--bin', elf, bin])
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, '--bin', elf, bin]))