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