]>
git.gir.st - tmk_keyboard.git/blob - 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.
4 ################################################################################
5 # Hooks for the various parts of the build process
7 # Internal mapping of hooks per tool
10 # Internal mapping of running hooks
13 # Available hook types
14 _hook_types
= ["binary", "compile", "link", "assemble"]
16 # Available hook steps
17 _hook_steps
= ["pre", "replace", "post"]
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
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
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
55 _running_hooks
[tool
] = False
60 def __init__(self
, target
, toolchain
):
62 self
._cmdline
_hooks
= {}
63 self
.toolchain
= toolchain
64 target
.init_hooks(self
, toolchain
.__class
__.__name
__)
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
:
70 if not hook_type
in _hooks
:
71 _hooks
[hook_type
] = {}
72 _hooks
[hook_type
][hook_step
] = function
75 def hook_add_compiler(self
, hook_step
, function
):
76 return self
._hook
_add
("compile", hook_step
, function
)
78 def hook_add_linker(self
, hook_step
, function
):
79 return self
._hook
_add
("link", hook_step
, function
)
81 def hook_add_assembler(self
, hook_step
, function
):
82 return self
._hook
_add
("assemble", hook_step
, function
)
84 def hook_add_binary(self
, hook_step
, function
):
85 return self
._hook
_add
("binary", hook_step
, function
)
88 def _hook_cmdline(self
, hook_type
, function
):
89 if not hook_type
in _hook_types
:
91 self
._cmdline
_hooks
[hook_type
] = function
94 def hook_cmdline_compiler(self
, function
):
95 return self
._hook
_cmdline
("compile", function
)
97 def hook_cmdline_linker(self
, function
):
98 return self
._hook
_cmdline
("link", function
)
100 def hook_cmdline_assembler(self
, function
):
101 return self
._hook
_cmdline
("assemble", function
)
103 def hook_cmdline_binary(self
, function
):
104 return self
._hook
_cmdline
("binary", function
)
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
)
112 def get_cmdline_compiler(self
, cmdline
):
113 return self
._get
_cmdline
("compile", cmdline
)
115 def get_cmdline_linker(self
, cmdline
):
116 return self
._get
_cmdline
("link", cmdline
)
118 def get_cmdline_assembler(self
, cmdline
):
119 return self
._get
_cmdline
("assemble", cmdline
)
121 def get_cmdline_binary(self
, cmdline
):
122 return self
._get
_cmdline
("binary", cmdline
)
124 ################################################################################