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