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