Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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