Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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