mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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