Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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