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