BA / Mbed OS BaBoRo1
Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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