init

Dependencies:   mbed

Committer:
Nathan Yonkee
Date:
Fri Mar 02 07:16:49 2018 -0700
Revision:
10:46a4cf51ee38
Parent:
9:d58e77ebd769
remove mbed-os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nathan Yonkee 9:d58e77ebd769 1 """ Configurable hooks in the build system. Can be used by various platforms
Nathan Yonkee 9:d58e77ebd769 2 to customize the build process.
Nathan Yonkee 9:d58e77ebd769 3 """
Nathan Yonkee 9:d58e77ebd769 4
Nathan Yonkee 9:d58e77ebd769 5 ################################################################################
Nathan Yonkee 9:d58e77ebd769 6 # Hooks for the various parts of the build process
Nathan Yonkee 9:d58e77ebd769 7
Nathan Yonkee 9:d58e77ebd769 8 # Internal mapping of hooks per tool
Nathan Yonkee 9:d58e77ebd769 9 _HOOKS = {}
Nathan Yonkee 9:d58e77ebd769 10
Nathan Yonkee 9:d58e77ebd769 11 # Internal mapping of running hooks
Nathan Yonkee 9:d58e77ebd769 12 _RUNNING_HOOKS = {}
Nathan Yonkee 9:d58e77ebd769 13
Nathan Yonkee 9:d58e77ebd769 14 # Available hook types
Nathan Yonkee 9:d58e77ebd769 15 _HOOK_TYPES = ["binary", "compile", "link", "assemble"]
Nathan Yonkee 9:d58e77ebd769 16
Nathan Yonkee 9:d58e77ebd769 17 # Available hook steps
Nathan Yonkee 9:d58e77ebd769 18 _HOOK_STEPS = ["pre", "replace", "post"]
Nathan Yonkee 9:d58e77ebd769 19
Nathan Yonkee 9:d58e77ebd769 20 # Hook the given function. Use this function as a decorator
Nathan Yonkee 9:d58e77ebd769 21 def hook_tool(function):
Nathan Yonkee 9:d58e77ebd769 22 """Decorate a function as a tool that may be hooked"""
Nathan Yonkee 9:d58e77ebd769 23 tool = function.__name__
Nathan Yonkee 9:d58e77ebd769 24 tool_flag = "_" + tool + "_done"
Nathan Yonkee 9:d58e77ebd769 25 def wrapper(t_self, *args, **kwargs):
Nathan Yonkee 9:d58e77ebd769 26 """The hooked function itself"""
Nathan Yonkee 9:d58e77ebd769 27 # if a hook for this tool is already running, it's most likely
Nathan Yonkee 9:d58e77ebd769 28 # coming from a derived class, so don't hook the super class version
Nathan Yonkee 9:d58e77ebd769 29 if _RUNNING_HOOKS.get(tool, False):
Nathan Yonkee 9:d58e77ebd769 30 return function(t_self, *args, **kwargs)
Nathan Yonkee 9:d58e77ebd769 31 _RUNNING_HOOKS[tool] = True
Nathan Yonkee 9:d58e77ebd769 32 # If this tool isn't hooked, return original function
Nathan Yonkee 9:d58e77ebd769 33 if tool not in _HOOKS:
Nathan Yonkee 9:d58e77ebd769 34 res = function(t_self, *args, **kwargs)
Nathan Yonkee 9:d58e77ebd769 35 _RUNNING_HOOKS[tool] = False
Nathan Yonkee 9:d58e77ebd769 36 return res
Nathan Yonkee 9:d58e77ebd769 37 tooldesc = _HOOKS[tool]
Nathan Yonkee 9:d58e77ebd769 38 setattr(t_self, tool_flag, False)
Nathan Yonkee 9:d58e77ebd769 39 # If there is a replace hook, execute the replacement instead
Nathan Yonkee 9:d58e77ebd769 40 if "replace" in tooldesc:
Nathan Yonkee 9:d58e77ebd769 41 res = tooldesc["replace"](t_self, *args, **kwargs)
Nathan Yonkee 9:d58e77ebd769 42 # If the replacement has set the "done" flag, exit now
Nathan Yonkee 9:d58e77ebd769 43 # Otherwise continue as usual
Nathan Yonkee 9:d58e77ebd769 44 if getattr(t_self, tool_flag, False):
Nathan Yonkee 9:d58e77ebd769 45 _RUNNING_HOOKS[tool] = False
Nathan Yonkee 9:d58e77ebd769 46 return res
Nathan Yonkee 9:d58e77ebd769 47 # Execute pre-function before main function if specified
Nathan Yonkee 9:d58e77ebd769 48 if "pre" in tooldesc:
Nathan Yonkee 9:d58e77ebd769 49 tooldesc["pre"](t_self, *args, **kwargs)
Nathan Yonkee 9:d58e77ebd769 50 # Execute the main function now
Nathan Yonkee 9:d58e77ebd769 51 res = function(t_self, *args, **kwargs)
Nathan Yonkee 9:d58e77ebd769 52 # Execute post-function after main function if specified
Nathan Yonkee 9:d58e77ebd769 53 if "post" in tooldesc:
Nathan Yonkee 9:d58e77ebd769 54 post_res = tooldesc["post"](t_self, *args, **kwargs)
Nathan Yonkee 9:d58e77ebd769 55 _RUNNING_HOOKS[tool] = False
Nathan Yonkee 9:d58e77ebd769 56 return post_res or res
Nathan Yonkee 9:d58e77ebd769 57 else:
Nathan Yonkee 9:d58e77ebd769 58 _RUNNING_HOOKS[tool] = False
Nathan Yonkee 9:d58e77ebd769 59 return res
Nathan Yonkee 9:d58e77ebd769 60 return wrapper
Nathan Yonkee 9:d58e77ebd769 61
Nathan Yonkee 9:d58e77ebd769 62 class Hook(object):
Nathan Yonkee 9:d58e77ebd769 63 """A compiler class that may be hooked"""
Nathan Yonkee 9:d58e77ebd769 64 def __init__(self, target, toolchain):
Nathan Yonkee 9:d58e77ebd769 65 _HOOKS.clear()
Nathan Yonkee 9:d58e77ebd769 66 self._cmdline_hooks = {}
Nathan Yonkee 9:d58e77ebd769 67 self.toolchain = toolchain
Nathan Yonkee 9:d58e77ebd769 68 target.init_hooks(self, toolchain)
Nathan Yonkee 9:d58e77ebd769 69
Nathan Yonkee 9:d58e77ebd769 70 # Hook various functions directly
Nathan Yonkee 9:d58e77ebd769 71 @staticmethod
Nathan Yonkee 9:d58e77ebd769 72 def _hook_add(hook_type, hook_step, function):
Nathan Yonkee 9:d58e77ebd769 73 """Add a hook to a compile function
Nathan Yonkee 9:d58e77ebd769 74
Nathan Yonkee 9:d58e77ebd769 75 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 76 hook_type - one of the _HOOK_TYPES
Nathan Yonkee 9:d58e77ebd769 77 hook_step - one of the _HOOK_STEPS
Nathan Yonkee 9:d58e77ebd769 78 function - the function to add to the list of hooks
Nathan Yonkee 9:d58e77ebd769 79 """
Nathan Yonkee 9:d58e77ebd769 80 if hook_type not in _HOOK_TYPES or hook_step not in _HOOK_STEPS:
Nathan Yonkee 9:d58e77ebd769 81 return False
Nathan Yonkee 9:d58e77ebd769 82 if hook_type not in _HOOKS:
Nathan Yonkee 9:d58e77ebd769 83 _HOOKS[hook_type] = {}
Nathan Yonkee 9:d58e77ebd769 84 _HOOKS[hook_type][hook_step] = function
Nathan Yonkee 9:d58e77ebd769 85 return True
Nathan Yonkee 9:d58e77ebd769 86
Nathan Yonkee 9:d58e77ebd769 87 def hook_add_compiler(self, hook_step, function):
Nathan Yonkee 9:d58e77ebd769 88 """Add a hook to the compiler
Nathan Yonkee 9:d58e77ebd769 89
Nathan Yonkee 9:d58e77ebd769 90 Positional Arguments:
Nathan Yonkee 9:d58e77ebd769 91 hook_step - one of the _HOOK_STEPS
Nathan Yonkee 9:d58e77ebd769 92 function - the function to add to the list of hooks
Nathan Yonkee 9:d58e77ebd769 93 """
Nathan Yonkee 9:d58e77ebd769 94 return self._hook_add("compile", hook_step, function)
Nathan Yonkee 9:d58e77ebd769 95
Nathan Yonkee 9:d58e77ebd769 96 def hook_add_linker(self, hook_step, function):
Nathan Yonkee 9:d58e77ebd769 97 """Add a hook to the linker
Nathan Yonkee 9:d58e77ebd769 98
Nathan Yonkee 9:d58e77ebd769 99 Positional Arguments:
Nathan Yonkee 9:d58e77ebd769 100 hook_step - one of the _HOOK_STEPS
Nathan Yonkee 9:d58e77ebd769 101 function - the function to add to the list of hooks
Nathan Yonkee 9:d58e77ebd769 102 """
Nathan Yonkee 9:d58e77ebd769 103 return self._hook_add("link", hook_step, function)
Nathan Yonkee 9:d58e77ebd769 104
Nathan Yonkee 9:d58e77ebd769 105 def hook_add_assembler(self, hook_step, function):
Nathan Yonkee 9:d58e77ebd769 106 """Add a hook to the assemble
Nathan Yonkee 9:d58e77ebd769 107
Nathan Yonkee 9:d58e77ebd769 108 Positional Arguments:
Nathan Yonkee 9:d58e77ebd769 109 hook_step - one of the _HOOK_STEPS
Nathan Yonkee 9:d58e77ebd769 110 function - the function to add to the list of hooks
Nathan Yonkee 9:d58e77ebd769 111 """
Nathan Yonkee 9:d58e77ebd769 112 return self._hook_add("assemble", hook_step, function)
Nathan Yonkee 9:d58e77ebd769 113
Nathan Yonkee 9:d58e77ebd769 114 def hook_add_binary(self, hook_step, function):
Nathan Yonkee 9:d58e77ebd769 115 """Add a hook to the elf to binary tool
Nathan Yonkee 9:d58e77ebd769 116
Nathan Yonkee 9:d58e77ebd769 117 Positional Arguments:
Nathan Yonkee 9:d58e77ebd769 118 hook_step - one of the _HOOK_STEPS
Nathan Yonkee 9:d58e77ebd769 119 function - the function to add to the list of hooks
Nathan Yonkee 9:d58e77ebd769 120 """
Nathan Yonkee 9:d58e77ebd769 121 return self._hook_add("binary", hook_step, function)
Nathan Yonkee 9:d58e77ebd769 122
Nathan Yonkee 9:d58e77ebd769 123 # Hook command lines
Nathan Yonkee 9:d58e77ebd769 124 def _hook_cmdline(self, hook_type, function):
Nathan Yonkee 9:d58e77ebd769 125 """Add a hook to a command line function
Nathan Yonkee 9:d58e77ebd769 126
Nathan Yonkee 9:d58e77ebd769 127 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 128 hook_type - one of the _HOOK_TYPES
Nathan Yonkee 9:d58e77ebd769 129 function - the function to add to the list of hooks
Nathan Yonkee 9:d58e77ebd769 130 """
Nathan Yonkee 9:d58e77ebd769 131 if hook_type not in _HOOK_TYPES:
Nathan Yonkee 9:d58e77ebd769 132 return False
Nathan Yonkee 9:d58e77ebd769 133 self._cmdline_hooks[hook_type] = function
Nathan Yonkee 9:d58e77ebd769 134 return True
Nathan Yonkee 9:d58e77ebd769 135
Nathan Yonkee 9:d58e77ebd769 136 def hook_cmdline_compiler(self, function):
Nathan Yonkee 9:d58e77ebd769 137 """Add a hook to the compiler command line
Nathan Yonkee 9:d58e77ebd769 138
Nathan Yonkee 9:d58e77ebd769 139 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 140 function - the function to call
Nathan Yonkee 9:d58e77ebd769 141 """
Nathan Yonkee 9:d58e77ebd769 142 return self._hook_cmdline("compile", function)
Nathan Yonkee 9:d58e77ebd769 143
Nathan Yonkee 9:d58e77ebd769 144 def hook_cmdline_linker(self, function):
Nathan Yonkee 9:d58e77ebd769 145 """Add a hook to the linker command line
Nathan Yonkee 9:d58e77ebd769 146
Nathan Yonkee 9:d58e77ebd769 147 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 148 function - the function to call
Nathan Yonkee 9:d58e77ebd769 149 """
Nathan Yonkee 9:d58e77ebd769 150 return self._hook_cmdline("link", function)
Nathan Yonkee 9:d58e77ebd769 151
Nathan Yonkee 9:d58e77ebd769 152 def hook_cmdline_assembler(self, function):
Nathan Yonkee 9:d58e77ebd769 153 """Add a hook to the assembler command line
Nathan Yonkee 9:d58e77ebd769 154
Nathan Yonkee 9:d58e77ebd769 155 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 156 function - the function to call
Nathan Yonkee 9:d58e77ebd769 157 """
Nathan Yonkee 9:d58e77ebd769 158 return self._hook_cmdline("assemble", function)
Nathan Yonkee 9:d58e77ebd769 159
Nathan Yonkee 9:d58e77ebd769 160 def hook_cmdline_binary(self, function):
Nathan Yonkee 9:d58e77ebd769 161 """Add a hook to the elf to bin tool command line
Nathan Yonkee 9:d58e77ebd769 162
Nathan Yonkee 9:d58e77ebd769 163 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 164 function - the function to call
Nathan Yonkee 9:d58e77ebd769 165 """
Nathan Yonkee 9:d58e77ebd769 166 return self._hook_cmdline("binary", function)
Nathan Yonkee 9:d58e77ebd769 167
Nathan Yonkee 9:d58e77ebd769 168 # Return the command line after applying the hook
Nathan Yonkee 9:d58e77ebd769 169 def _get_cmdline(self, hook_type, cmdline):
Nathan Yonkee 9:d58e77ebd769 170 """Get the command line after running all hooks
Nathan Yonkee 9:d58e77ebd769 171
Nathan Yonkee 9:d58e77ebd769 172 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 173 hook_type - one of the _HOOK_TYPES
Nathan Yonkee 9:d58e77ebd769 174 cmdline - the initial command line
Nathan Yonkee 9:d58e77ebd769 175 """
Nathan Yonkee 9:d58e77ebd769 176 if hook_type in self._cmdline_hooks:
Nathan Yonkee 9:d58e77ebd769 177 cmdline = self._cmdline_hooks[hook_type](
Nathan Yonkee 9:d58e77ebd769 178 self.toolchain.__class__.__name__, cmdline)
Nathan Yonkee 9:d58e77ebd769 179 return cmdline
Nathan Yonkee 9:d58e77ebd769 180
Nathan Yonkee 9:d58e77ebd769 181 def get_cmdline_compiler(self, cmdline):
Nathan Yonkee 9:d58e77ebd769 182 """Get the compiler command line after running all hooks
Nathan Yonkee 9:d58e77ebd769 183
Nathan Yonkee 9:d58e77ebd769 184 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 185 cmdline - the initial command line
Nathan Yonkee 9:d58e77ebd769 186 """
Nathan Yonkee 9:d58e77ebd769 187 return self._get_cmdline("compile", cmdline)
Nathan Yonkee 9:d58e77ebd769 188
Nathan Yonkee 9:d58e77ebd769 189 def get_cmdline_linker(self, cmdline):
Nathan Yonkee 9:d58e77ebd769 190 """Get the linker command line after running all hooks
Nathan Yonkee 9:d58e77ebd769 191
Nathan Yonkee 9:d58e77ebd769 192 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 193 cmdline - the initial command line
Nathan Yonkee 9:d58e77ebd769 194 """
Nathan Yonkee 9:d58e77ebd769 195 return self._get_cmdline("link", cmdline)
Nathan Yonkee 9:d58e77ebd769 196
Nathan Yonkee 9:d58e77ebd769 197 def get_cmdline_assembler(self, cmdline):
Nathan Yonkee 9:d58e77ebd769 198 """Get the assmebler command line after running all hooks
Nathan Yonkee 9:d58e77ebd769 199
Nathan Yonkee 9:d58e77ebd769 200 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 201 cmdline - the initial command line
Nathan Yonkee 9:d58e77ebd769 202 """
Nathan Yonkee 9:d58e77ebd769 203 return self._get_cmdline("assemble", cmdline)
Nathan Yonkee 9:d58e77ebd769 204
Nathan Yonkee 9:d58e77ebd769 205 def get_cmdline_binary(self, cmdline):
Nathan Yonkee 9:d58e77ebd769 206 """Get the binary command line after running all hooks
Nathan Yonkee 9:d58e77ebd769 207
Nathan Yonkee 9:d58e77ebd769 208 Positional arguments:
Nathan Yonkee 9:d58e77ebd769 209 cmdline - the initial command line
Nathan Yonkee 9:d58e77ebd769 210 """
Nathan Yonkee 9:d58e77ebd769 211 return self._get_cmdline("binary", cmdline)
Nathan Yonkee 9:d58e77ebd769 212
Nathan Yonkee 9:d58e77ebd769 213 ################################################################################
Nathan Yonkee 9:d58e77ebd769 214