]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/workspace_tools/hooks.py
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / workspace_tools / hooks.py
1 # Configurable hooks in the build system. Can be used by various platforms
2 # to customize the build process.
3
4 ################################################################################
5 # Hooks for the various parts of the build process
6
7 # Internal mapping of hooks per tool
8 _hooks = {}
9
10 # Internal mapping of running hooks
11 _running_hooks = {}
12
13 # Available hook types
14 _hook_types = ["binary", "compile", "link", "assemble"]
15
16 # Available hook steps
17 _hook_steps = ["pre", "replace", "post"]
18
19 # Hook the given function. Use this function as a decorator
20 def hook_tool(function):
21 tool = function.__name__
22 tool_flag = "_" + tool + "_done"
23 def wrapper(t_self, *args, **kwargs):
24 # if a hook for this tool is already running, it's most likely
25 # coming from a derived class, so don't hook the super class version
26 if _running_hooks.get(tool, False):
27 return function(t_self, *args, **kwargs)
28 _running_hooks[tool] = True
29 # If this tool isn't hooked, return original function
30 if not _hooks.has_key(tool):
31 res = function(t_self, *args, **kwargs)
32 _running_hooks[tool] = False
33 return res
34 tooldesc = _hooks[tool]
35 setattr(t_self, tool_flag, False)
36 # If there is a replace hook, execute the replacement instead
37 if tooldesc.has_key("replace"):
38 res = tooldesc["replace"](t_self, *args, **kwargs)
39 # If the replacement has set the "done" flag, exit now
40 # Otherwise continue as usual
41 if getattr(t_self, tool_flag, False):
42 _running_hooks[tool] = False
43 return res
44 # Execute pre-function before main function if specified
45 if tooldesc.has_key("pre"):
46 tooldesc["pre"](t_self, *args, **kwargs)
47 # Execute the main function now
48 res = function(t_self, *args, **kwargs)
49 # Execute post-function after main function if specified
50 if tooldesc.has_key("post"):
51 post_res = tooldesc["post"](t_self, *args, **kwargs)
52 _running_hooks[tool] = False
53 return post_res or res
54 else:
55 _running_hooks[tool] = False
56 return res
57 return wrapper
58
59 class Hook:
60 def __init__(self, target, toolchain):
61 _hooks.clear()
62 self._cmdline_hooks = {}
63 self.toolchain = toolchain
64 target.init_hooks(self, toolchain.__class__.__name__)
65
66 # Hook various functions directly
67 def _hook_add(self, hook_type, hook_step, function):
68 if not hook_type in _hook_types or not hook_step in _hook_steps:
69 return False
70 if not hook_type in _hooks:
71 _hooks[hook_type] = {}
72 _hooks[hook_type][hook_step] = function
73 return True
74
75 def hook_add_compiler(self, hook_step, function):
76 return self._hook_add("compile", hook_step, function)
77
78 def hook_add_linker(self, hook_step, function):
79 return self._hook_add("link", hook_step, function)
80
81 def hook_add_assembler(self, hook_step, function):
82 return self._hook_add("assemble", hook_step, function)
83
84 def hook_add_binary(self, hook_step, function):
85 return self._hook_add("binary", hook_step, function)
86
87 # Hook command lines
88 def _hook_cmdline(self, hook_type, function):
89 if not hook_type in _hook_types:
90 return False
91 self._cmdline_hooks[hook_type] = function
92 return True
93
94 def hook_cmdline_compiler(self, function):
95 return self._hook_cmdline("compile", function)
96
97 def hook_cmdline_linker(self, function):
98 return self._hook_cmdline("link", function)
99
100 def hook_cmdline_assembler(self, function):
101 return self._hook_cmdline("assemble", function)
102
103 def hook_cmdline_binary(self, function):
104 return self._hook_cmdline("binary", function)
105
106 # Return the command line after applying the hook
107 def _get_cmdline(self, hook_type, cmdline):
108 if self._cmdline_hooks.has_key(hook_type):
109 cmdline = self._cmdline_hooks[hook_type](self.toolchain.__class__.__name__, cmdline)
110 return cmdline
111
112 def get_cmdline_compiler(self, cmdline):
113 return self._get_cmdline("compile", cmdline)
114
115 def get_cmdline_linker(self, cmdline):
116 return self._get_cmdline("link", cmdline)
117
118 def get_cmdline_assembler(self, cmdline):
119 return self._get_cmdline("assemble", cmdline)
120
121 def get_cmdline_binary(self, cmdline):
122 return self._get_cmdline("binary", cmdline)
123
124 ################################################################################
125
Imprint / Impressum