Alessandro Angelino / mbed-tools

Fork of mbed-tools by Morpheus

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hooks.py Source File

hooks.py

00001 # Configurable hooks in the build system. Can be used by various platforms
00002 # to customize the build process.
00003 
00004 ################################################################################
00005 # Hooks for the various parts of the build process
00006 
00007 # Internal mapping of hooks per tool
00008 _hooks = {}
00009 
00010 # Internal mapping of running hooks
00011 _running_hooks = {}
00012 
00013 # Available hook types
00014 _hook_types = ["binary", "compile", "link", "assemble"]
00015 
00016 # Available hook steps
00017 _hook_steps = ["pre", "replace", "post"]
00018 
00019 # Hook the given function. Use this function as a decorator
00020 def hook_tool(function):
00021     tool = function.__name__
00022     tool_flag = "_" + tool + "_done"
00023     def wrapper(t_self, *args, **kwargs):
00024         # if a hook for this tool is already running, it's most likely
00025         # coming from a derived class, so don't hook the super class version
00026         if _running_hooks.get(tool, False):
00027             return function(t_self, *args, **kwargs)
00028         _running_hooks[tool] = True
00029         # If this tool isn't hooked, return original function
00030         if not _hooks.has_key(tool):
00031             res = function(t_self, *args, **kwargs)
00032             _running_hooks[tool] = False
00033             return res
00034         tooldesc = _hooks[tool]
00035         setattr(t_self, tool_flag, False)
00036         # If there is a replace hook, execute the replacement instead
00037         if tooldesc.has_key("replace"):
00038             res = tooldesc["replace"](t_self, *args, **kwargs)
00039         # If the replacement has set the "done" flag, exit now
00040         # Otherwise continue as usual
00041         if getattr(t_self, tool_flag, False):
00042             _running_hooks[tool] = False
00043             return res
00044         # Execute pre-function before main function if specified
00045         if tooldesc.has_key("pre"):
00046             tooldesc["pre"](t_self, *args, **kwargs)
00047         # Execute the main function now
00048         res = function(t_self, *args, **kwargs)
00049         # Execute post-function after main function if specified
00050         if tooldesc.has_key("post"):
00051             post_res = tooldesc["post"](t_self, *args, **kwargs)
00052             _running_hooks[tool] = False
00053             return post_res or res
00054         else:
00055             _running_hooks[tool] = False
00056             return res
00057     return wrapper
00058 
00059 class Hook:
00060     def __init__(self, target, toolchain):
00061         _hooks.clear()
00062         self._cmdline_hooks = {}
00063         self.toolchain = toolchain
00064         target.init_hooks(self, toolchain.__class__.__name__)
00065 
00066     # Hook various functions directly
00067     def _hook_add(self, hook_type, hook_step, function):
00068         if not hook_type in _hook_types or not hook_step in _hook_steps:
00069             return False
00070         if not hook_type in _hooks:
00071             _hooks[hook_type] = {}
00072         _hooks[hook_type][hook_step] = function
00073         return True
00074 
00075     def hook_add_compiler(self, hook_step, function):
00076         return self._hook_add("compile", hook_step, function)
00077 
00078     def hook_add_linker(self, hook_step, function):
00079         return self._hook_add("link", hook_step, function)
00080 
00081     def hook_add_assembler(self, hook_step, function):
00082         return self._hook_add("assemble", hook_step, function)
00083 
00084     def hook_add_binary(self, hook_step, function):
00085         return self._hook_add("binary", hook_step, function)
00086 
00087     # Hook command lines
00088     def _hook_cmdline(self, hook_type, function):
00089         if not hook_type in _hook_types:
00090             return False
00091         self._cmdline_hooks[hook_type] = function
00092         return True
00093 
00094     def hook_cmdline_compiler(self, function):
00095         return self._hook_cmdline("compile", function)
00096 
00097     def hook_cmdline_linker(self, function):
00098         return self._hook_cmdline("link", function)
00099 
00100     def hook_cmdline_assembler(self, function):
00101         return self._hook_cmdline("assemble", function)
00102 
00103     def hook_cmdline_binary(self, function):
00104         return self._hook_cmdline("binary", function)
00105 
00106     # Return the command line after applying the hook
00107     def _get_cmdline(self, hook_type, cmdline):
00108         if self._cmdline_hooks.has_key(hook_type):
00109             cmdline = self._cmdline_hooks[hook_type](self.toolchain.__class__.__name__, cmdline)
00110         return cmdline
00111 
00112     def get_cmdline_compiler(self, cmdline):
00113         return self._get_cmdline("compile", cmdline)
00114 
00115     def get_cmdline_linker(self, cmdline):
00116         return self._get_cmdline("link", cmdline)
00117 
00118     def get_cmdline_assembler(self, cmdline):
00119         return self._get_cmdline("assemble", cmdline)
00120 
00121     def get_cmdline_binary(self, cmdline):
00122         return self._get_cmdline("binary", cmdline)
00123 
00124 ################################################################################
00125