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