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