Skip to content

Enable target-specific toolchain options hook #3894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions tools/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
_RUNNING_HOOKS = {}

# Available hook types
_HOOK_TYPES = ["binary", "compile", "link", "assemble"]
_HOOK_TYPES = ["target", "binary", "compile", "link", "assemble"]

# Available hook steps
_HOOK_STEPS = ["pre", "replace", "post"]
Expand Down Expand Up @@ -65,7 +65,9 @@ def __init__(self, target, toolchain):
_HOOKS.clear()
self._cmdline_hooks = {}
self.toolchain = toolchain
target.init_hooks(self, toolchain.__class__.__name__)
for hook_step in _HOOK_STEPS:
for hook_type in _HOOK_TYPES:
target.init_hooks(self, hook_step, hook_type, toolchain.__class__.__name__)

# Hook various functions directly
@staticmethod
Expand All @@ -84,7 +86,17 @@ def _hook_add(hook_type, hook_step, function):
_HOOKS[hook_type][hook_step] = function
return True

def hook_add_compiler(self, hook_step, function):
def hook_add_target(self, hook_step, function):
"""Add a hook to the init option

Positional Arguments:
hook_step - target hook is limited to 'post'
function - the function to add to the list of hooks
"""
hook_step = 'post'
return self._hook_add("target", hook_step, function)

def hook_add_compile(self, hook_step, function):
"""Add a hook to the compiler

Positional Arguments:
Expand All @@ -93,7 +105,7 @@ def hook_add_compiler(self, hook_step, function):
"""
return self._hook_add("compile", hook_step, function)

def hook_add_linker(self, hook_step, function):
def hook_add_link(self, hook_step, function):
"""Add a hook to the linker

Positional Arguments:
Expand All @@ -102,7 +114,7 @@ def hook_add_linker(self, hook_step, function):
"""
return self._hook_add("link", hook_step, function)

def hook_add_assembler(self, hook_step, function):
def hook_add_assemble(self, hook_step, function):
"""Add a hook to the assemble

Positional Arguments:
Expand Down Expand Up @@ -133,23 +145,23 @@ def _hook_cmdline(self, hook_type, function):
self._cmdline_hooks[hook_type] = function
return True

def hook_cmdline_compiler(self, function):
def hook_cmdline_compile(self, function):
"""Add a hook to the compiler command line

Positional arguments:
function - the function to call
"""
return self._hook_cmdline("compile", function)

def hook_cmdline_linker(self, function):
def hook_cmdline_link(self, function):
"""Add a hook to the linker command line

Positional arguments:
function - the function to call
"""
return self._hook_cmdline("link", function)

def hook_cmdline_assembler(self, function):
def hook_cmdline_assemble(self, function):
"""Add a hook to the assembler command line

Positional arguments:
Expand All @@ -165,6 +177,17 @@ def hook_cmdline_binary(self, function):
"""
return self._hook_cmdline("binary", function)

def post_target_hook(self, t_self):
"""Run the post init target-specific hook

Positional arguments:
t_self - the toolchain object
"""
try:
_HOOKS['target']['post'](t_self)
except:
return

# Return the command line after applying the hook
def _get_cmdline(self, hook_type, cmdline):
"""Get the command line after running all hooks
Expand All @@ -178,23 +201,23 @@ def _get_cmdline(self, hook_type, cmdline):
self.toolchain.__class__.__name__, cmdline)
return cmdline

def get_cmdline_compiler(self, cmdline):
def get_cmdline_compile(self, cmdline):
"""Get the compiler command line after running all hooks

Positional arguments:
cmdline - the initial command line
"""
return self._get_cmdline("compile", cmdline)

def get_cmdline_linker(self, cmdline):
def get_cmdline_link(self, cmdline):
"""Get the linker command line after running all hooks

Positional arguments:
cmdline - the initial command line
"""
return self._get_cmdline("link", cmdline)

def get_cmdline_assembler(self, cmdline):
def get_cmdline_assemble(self, cmdline):
"""Get the assmebler command line after running all hooks

Positional arguments:
Expand Down
12 changes: 6 additions & 6 deletions tools/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,16 @@ def labels(self):
labels.append("UVISOR_UNSUPPORTED")
return labels

def init_hooks(self, hook, toolchain_name):
"""Initialize the post-build hooks for a toolchain. For now, this
function only allows "post binary" hooks (hooks that are executed
after the binary image is extracted from the executable file)
def init_hooks(self, hook, hook_step, hook_type, toolchain_name):
"""Initialize hooks for a toolchain.
"""

# If there's no hook, simply return
try:
hook_data = self.post_binary_hook
hook_data = getattr(self, hook_step + '_' + hook_type + '_hook')
except AttributeError:
return

# A hook was found. The hook's name is in the format
# "classname.functionname"
temp = hook_data["function"].split(".")
Expand Down Expand Up @@ -332,7 +331,8 @@ def init_hooks(self, hook, toolchain_name):
(toolchain_name not in toolchain_restrictions):
return
# Finally, hook the requested function
hook.hook_add_binary("post", getattr(cls, function_name))
hook_func = getattr(hook, 'hook_add_' + hook_type)
hook_func(hook_step, getattr(cls, function_name))

################################################################################
# Target specific code goes in this section
Expand Down
10 changes: 6 additions & 4 deletions tools/toolchains/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def __init__(self, target, notify=None, macros=None,
self.ar = join(ARM_BIN, "armar")
self.elf2bin = join(ARM_BIN, "fromelf")

self.hook.post_target_hook(self)

def parse_dependencies(self, dep_path):
dependencies = []
for line in open(dep_path).readlines():
Expand Down Expand Up @@ -147,8 +149,8 @@ def assemble(self, source, object, includes):
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)
cmd_pre = self.hook.get_cmdline_assemble(cmd_pre)
cmd = self.hook.get_cmdline_assemble(cmd)

# Return command array, don't execute
return [cmd_pre, cmd]
Expand All @@ -163,7 +165,7 @@ def compile(self, cc, source, object, includes):
cmd.extend(["-o", object, source])

# Call cmdline hook
cmd = self.hook.get_cmdline_compiler(cmd)
cmd = self.hook.get_cmdline_compile(cmd)

return [cmd]

Expand All @@ -190,7 +192,7 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
cmd = self.ld + args + objects + libraries + self.sys_libs

# Call cmdline hook
cmd = self.hook.get_cmdline_linker(cmd)
cmd = self.hook.get_cmdline_link(cmd)

if self.RESPONSE_FILES:
# Split link command to linker executable + response file
Expand Down
8 changes: 5 additions & 3 deletions tools/toolchains/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ def __init__(self, target, notify=None, macros=None,
self.ar = join(tool_path, "arm-none-eabi-ar")
self.elf2bin = join(tool_path, "arm-none-eabi-objcopy")

self.hook.post_target_hook(self)

def parse_dependencies(self, dep_path):
dependencies = []
buff = open(dep_path).readlines()
Expand Down Expand Up @@ -181,7 +183,7 @@ def assemble(self, source, object, includes):
cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes) + ["-o", object, source]

# Call cmdline hook
cmd = self.hook.get_cmdline_assembler(cmd)
cmd = self.hook.get_cmdline_assemble(cmd)

# Return command array, don't execute
return [cmd]
Expand All @@ -196,7 +198,7 @@ def compile(self, cc, source, object, includes):
cmd.extend(["-o", object, source])

# Call cmdline hook
cmd = self.hook.get_cmdline_compiler(cmd)
cmd = self.hook.get_cmdline_compile(cmd)

return [cmd]

Expand Down Expand Up @@ -234,7 +236,7 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
cmd.extend(libs)

# Call cmdline hook
cmd = self.hook.get_cmdline_linker(cmd)
cmd = self.hook.get_cmdline_link(cmd)

if self.RESPONSE_FILES:
# Split link command to linker executable + response file
Expand Down
8 changes: 5 additions & 3 deletions tools/toolchains/iar.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ def __init__(self, target, notify=None, macros=None,
self.ar = join(IAR_BIN, "iarchive")
self.elf2bin = join(IAR_BIN, "ielftool")

self.hook.post_target_hook(self)

def parse_dependencies(self, dep_path):
return [(self.CHROOT if self.CHROOT else '')+path.strip() for path in open(dep_path).readlines()
if (path and not path.isspace())]
Expand Down Expand Up @@ -159,7 +161,7 @@ def assemble(self, source, object, includes):
cmd = self.asm + self.get_compile_options(self.get_symbols(True), includes, True) + ["-o", object, source]

# Call cmdline hook
cmd = self.hook.get_cmdline_assembler(cmd)
cmd = self.hook.get_cmdline_assemble(cmd)

# Return command array, don't execute
return [cmd]
Expand All @@ -176,7 +178,7 @@ def compile(self, cc, source, object, includes):
cmd.extend(["-o", object, source])

# Call cmdline hook
cmd = self.hook.get_cmdline_compiler(cmd)
cmd = self.hook.get_cmdline_compile(cmd)

return [cmd]

Expand All @@ -196,7 +198,7 @@ def link(self, output, objects, libraries, lib_dirs, mem_map):
cmd.extend(["--config", mem_map])

# Call cmdline hook
cmd = self.hook.get_cmdline_linker(cmd)
cmd = self.hook.get_cmdline_link(cmd)

if self.RESPONSE_FILES:
# Split link command to linker executable + response file
Expand Down