Maxim nexpaq / nexpaq_dev
Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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