Skip to content

Commit 697acd3

Browse files
author
Bogdan Marinescu
committed
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.
1 parent 6959ef7 commit 697acd3

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

workspace_tools/hooks.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
_running_hooks = {}
1212

1313
# Available hook types
14-
_hook_types = ["binary"]
14+
_hook_types = ["binary", "compile", "link", "assemble"]
1515

1616
# Available hook steps
1717
_hook_steps = ["pre", "replace", "post"]
@@ -59,19 +59,67 @@ def wrapper(t_self, *args, **kwargs):
5959
class Hook:
6060
def __init__(self, target, toolchain):
6161
_hooks.clear()
62+
self._cmdline_hooks = {}
6263
self.toolchain = toolchain
6364
target.init_hooks(self, toolchain.__class__.__name__)
6465

65-
def hook_add(self, hook_type, hook_step, function):
66+
# Hook various functions directly
67+
def _hook_add(self, hook_type, hook_step, function):
6668
if not hook_type in _hook_types or not hook_step in _hook_steps:
6769
return False
6870
if not hook_type in _hooks:
6971
_hooks[hook_type] = {}
7072
_hooks[hook_type][hook_step] = function
7173
return True
7274

75+
def hook_add_compiler(self, hook_step, function):
76+
return self._hook_add("compile", hook_step, function)
77+
78+
def hook_add_linker(self, hook_step, function):
79+
return self._hook_add("link", hook_step, function)
80+
81+
def hook_add_assembler(self, hook_step, function):
82+
return self._hook_add("assemble", hook_step, function)
83+
7384
def hook_add_binary(self, hook_step, function):
74-
return self.hook_add("binary", hook_step, function)
85+
return self._hook_add("binary", hook_step, function)
86+
87+
# Hook command lines
88+
def _hook_cmdline(self, hook_type, function):
89+
if not hook_type in _hook_types:
90+
return False
91+
self._cmdline_hooks[hook_type] = function
92+
return True
93+
94+
def hook_cmdline_compiler(self, function):
95+
return self._hook_cmdline("compile", function)
96+
97+
def hook_cmdline_linker(self, function):
98+
return self._hook_cmdline("link", function)
99+
100+
def hook_cmdline_assembler(self, function):
101+
return self._hook_cmdline("assemble", function)
102+
103+
def hook_cmdline_binary(self, function):
104+
return self._hook_cmdline("binary", function)
105+
106+
# Return the command line after applying the hook
107+
def _get_cmdline(self, hook_type, cmdline):
108+
if self._cmdline_hooks.has_key(hook_type):
109+
cmdline = self._cmdline_hooks[hook_type](self.toolchain.__class__.__name__, cmdline)
110+
return cmdline
111+
112+
def get_cmdline_compiler(self, cmdline):
113+
return self._get_cmdline("compile", cmdline)
114+
115+
def get_cmdline_linker(self, cmdline):
116+
return self._get_cmdline("link", cmdline)
117+
118+
def get_cmdline_assembler(self, cmdline):
119+
return self._get_cmdline("assemble", cmdline)
120+
121+
def get_cmdline_binary(self, cmdline):
122+
return self._get_cmdline("binary", cmdline)
75123

76124
################################################################################
77125

workspace_tools/targets.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ def __init__(self):
175175
def init_hooks(self, hook, toolchain_name):
176176
if toolchain_name in ['ARM_STD', 'ARM_MICRO']:
177177
hook.hook_add_binary("post", self.binary_hook)
178+
hook.hook_cmdline_linker(self.cmdline_hook)
179+
180+
@staticmethod
181+
def cmdline_hook(toolchain, cmdline):
182+
return cmdline + ["--any_placement=first_fit"]
178183

179184
@staticmethod
180185
def binary_hook(t_self, elf, binf):

workspace_tools/toolchains/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def compile(self, cc, source, object, includes):
403403
command.extend(self.cc_extra(base))
404404

405405
self.debug(command)
406-
_, stderr, rc = run_cmd(command, dirname(object))
406+
_, stderr, rc = run_cmd(self.hook.get_cmdline_compiler(command), dirname(object))
407407

408408
# Parse output for Warnings and Errors
409409
self.parse_output(stderr)

workspace_tools/toolchains/arm.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def assemble(self, source, object, includes):
8181
# Preprocess first, then assemble
8282
tempfile = object + '.E.s'
8383
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])
84-
self.default_cmd(self.asm + ["-o", object, tempfile])
84+
self.default_cmd(self.hook.get_cmdline_assembler(self.asm + ["-o", object, tempfile]))
8585

8686
def parse_dependencies(self, dep_path):
8787
dependencies = []
@@ -114,15 +114,15 @@ def archive(self, objects, lib_path):
114114
self.default_cmd([self.ar, '-r', lib_path] + objects)
115115

116116
def link(self, output, objects, libraries, lib_dirs, mem_map):
117-
args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt", "--any_placement=first_fit"]
117+
args = ["-o", output, "--userlibpath", ",".join(lib_dirs), "--info=totals", "--list=.link_totals.txt"]
118118
if mem_map:
119119
args.extend(["--scatter", mem_map])
120120

121-
self.default_cmd(self.ld + args + objects + libraries + self.sys_libs)
121+
self.default_cmd(self.hook.get_cmdline_linker(self.ld + args + objects + libraries + self.sys_libs))
122122

123123
@hook_tool
124124
def binary(self, elf, bin):
125-
self.default_cmd([self.elf2bin, '--bin', '-o', bin, elf])
125+
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, '--bin', '-o', bin, elf]))
126126

127127

128128
class ARM_STD(ARM):

workspace_tools/toolchains/gcc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def __init__(self, target, options=None, notify=None, macros=None, tool_path="")
7979
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")
8080

8181
def assemble(self, source, object, includes):
82-
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])
82+
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]))
8383

8484
def parse_dependencies(self, dep_path):
8585
dependencies = []
@@ -155,11 +155,11 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
155155
if self.CIRCULAR_DEPENDENCIES:
156156
libs.extend(libs)
157157

158-
self.default_cmd(self.ld + ["-T%s" % mem_map, "-o", output] +
159-
objects + ["-L%s" % L for L in lib_dirs] + libs)
158+
self.default_cmd(self.hook.get_cmdline_linker(self.ld + ["-T%s" % mem_map, "-o", output] +
159+
objects + ["-L%s" % L for L in lib_dirs] + libs))
160160

161161
def binary(self, elf, bin):
162-
self.default_cmd([self.elf2bin, "-O", "binary", elf, bin])
162+
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, "-O", "binary", elf, bin]))
163163

164164

165165
class GCC_ARM(GCC):

workspace_tools/toolchains/iar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def parse_dependencies(self, dep_path):
9191
if (path and not path.isspace())]
9292

9393
def assemble(self, source, object, includes):
94-
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])
94+
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]))
9595

9696
def archive(self, objects, lib_path):
9797
if exists(lib_path):
@@ -100,7 +100,7 @@ def archive(self, objects, lib_path):
100100

101101
def link(self, output, objects, libraries, lib_dirs, mem_map):
102102
args = [self.ld, "-o", output, "--config", mem_map]
103-
self.default_cmd(args + objects + libraries)
103+
self.default_cmd(self.hook.get_cmdline_linker(args + objects + libraries))
104104

105105
def binary(self, elf, bin):
106-
self.default_cmd([self.elf2bin, '--bin', elf, bin])
106+
self.default_cmd(self.hook.get_cmdline_binary([self.elf2bin, '--bin', elf, bin]))

0 commit comments

Comments
 (0)