-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify_makefile.py
executable file
·267 lines (220 loc) · 8.57 KB
/
modify_makefile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python3
import argparse
import os
import sys
CUBE_PROGEMMER = '/usr/local/STMicroelectronics/STM32Cube/STM32CubeProgrammer/bin/STM32CubeProgrammer'
CMD_FLASH = \
"""
#######################################
# flash
#######################################
flash:
\t@{} -c port=SWD reset=HWrst -w build/$(TARGET).bin 0x08000000 -v -rst
"""
CMD_BUILD_AND_FLASH = \
"""
build_and_flash: all
\t@{} -c port=SWD reset=HWrst -w build/$(TARGET).bin 0x08000000 -v -rst
"""
CMD_OBJECTS_APPEND_CPP = \
"""
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(CPP_SOURCES:.cpp=.o)))
vpath %.cpp $(sort $(dir $(CPP_SOURCES)))
"""
CMD_BUILD_CPP = \
"""
$(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
\t$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.cpp=.lst)) $< -o $@
"""
CFLAGS = (
'$(MCU)',
'-std=c++11',
'-Wno-write-strings',
'-specs=nano.specs',
'-specs=nosys.specs',
'$(C_DEFS)',
'$(C_INCLUDES)',
'$(OPT)',
'-Wall',
'-fdata-sections',
'-ffunction-sections',
)
LDFLAGS = (
'$(MCU)',
'-Wl,--no-wchar-size-warning',
'-specs=nosys.specs',
'-specs=nano.specs',
'-T$(LDSCRIPT)',
'$(LIBDIR)',
'$(LIBS)',
)
TAG_GENERIC = '# Generic Makefile (based on gcc)'
TAG_MODIFY_CPP = '# Modified for C++'
#TAG_SOURCES_PATH = '# source path'
TAG_SOURCES_C = '# C sources'
TAG_SOURCES_CPP = '# C++ sources'
TAG_SOURCES_ASM = '# ASM sources'
TAG_INCLUDES_C = '# C includes'
TAG_INCLUDES_ASM = '# AS includes'
TAG_DEFINES_C = '# C defines'
TAG_DEFINES_ASM = '# AS defines'
TAG_LIST_OF_OBJECTS = '# list of objects'
TAG_LIST_OF_CPP_OBJECTS = '# list of C++ objects'
TAG_LIFT_OF_ASM_OBJECTS = '# list of ASM program objects'
TAG_EOF = '# *** EOF ***'
class NotExist(Exception):
def __init__(self, name: str):
self.error_text = '{} is not defined!'.format(name)
def __str__(self):
return self.error_text
class MakefileIsModified(Exception):
def __str__(self):
return 'This Makefile was already mofified!'
class ProgrammerNotFound(Exception):
def __str__(self):
return 'Install STM32 Programmer CLI for Flash firmware.'
class Makefile(object):
def __init__(self, path: str='Makefile'):
self.MAKEFILE_LOCATION = path
with open(self.MAKEFILE_LOCATION, 'r+') as fr:
self.makefile = fr.read()
self.modify()
def __del__(self):
with open(self.MAKEFILE_LOCATION, 'w') as fw:
fw.write(self.makefile)
def update(self, *content: str):
self.makefile = ''.join(content)
def replace(self, old: str, new: str):
self.update(self.makefile.replace(old, new))
def unix_end_line(self):
self.replace('\r\n', '\n')
def get_position_front(self, expression: str) -> int:
return self.makefile.find(expression) - 1
def get_position(self, expression: str) -> int:
return self.makefile.find(expression)
def get_position_behind(self, expression: str) -> int:
return self.makefile.find(expression) + len(expression) + 1
def flags(self, list_of_flags: tuple) -> str:
return ''.join(map(lambda x: x + ' ', list_of_flags))[:-1]
def check_existence(self, name: str):
if self.get_position(name) == -1:
raise NotExist(name)
def set_variable(self, name: str, value: str):
self.check_existence(name)
position_start = self.get_position_behind(name + ' ')
i = position_start
while self.makefile[i] != '\n':
i += 1
position_end = i
self.update(self.makefile[:position_start], ' ', value, self.makefile[position_end:])
def check_was_modified(self):
if self.get_position(TAG_MODIFY_CPP) != -1:
raise MakefileIsModified
else:
position = self.get_position_behind(TAG_GENERIC)
self.update(self.makefile[:position], TAG_MODIFY_CPP, '\n', self.makefile[position:])
def block_get_position(self, tag: str) -> tuple:
self.check_existence(tag)
position_start = self.get_position_behind(tag)
i = position_start
while self.makefile[i:i + 2] != '\n\n' and self.makefile[i:i + 2] != '\n#':
i += 1
position_end = i
return (position_start, position_end)
def block_get(self, tag: str) -> list:
position_start, position_end = self.block_get_position(tag)
return self.makefile[position_start:position_end].split('\n')
def block_set(self, tag: str, content: list):
position_start, position_end = self.block_get_position(tag)
code = self.block_get(tag)
var = code[0] + '\n'
if content:
code = sorted(
list(set(map(
lambda x: x.replace('\\', '').strip(),
content,
)))
)
code = ''.join(list(map(lambda x: x + ' \\\n', code[:-1])) + [code[-1] + '\n'])
else:
code = ''
self.update(self.makefile[:position_start], var, code, self.makefile[position_end:])
def block_append(self, tag: str, content: list):
code = self.block_get(tag)
var = code[0] + '\n'
code = code[1:] + content
self.block_set(tag, code)
def repair_multiple_definition(self, tag: str):
self.block_set(tag, self.block_get(tag)[1:])
def update_toolchain(self):
self.replace('PREFIX = arm-none-eabi-', 'PREFIX = arm-none-eabi-\nGCC_PATH = /opt/gcc-arm-none-eabi/bin\n')
def support_cpp(self):
self.set_variable('CC', '$(GCC_PATH)/$(PREFIX)g++')
self.set_variable('CFLAGS', self.flags(CFLAGS))
self.set_variable('LDFLAGS', self.flags(LDFLAGS))
position = self.get_position_front(TAG_SOURCES_ASM)
self.update(self.makefile[:position], TAG_SOURCES_CPP, '\nCPP_SOURCES = $(wildcard Src/*.cpp)\n\n', self.makefile[position:])
position = self.get_position(TAG_LIFT_OF_ASM_OBJECTS)
self.update(self.makefile[:position], TAG_LIST_OF_CPP_OBJECTS, CMD_OBJECTS_APPEND_CPP, self.makefile[position:])
position = self.get_position_front('$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)')
self.update(self.makefile[:position], CMD_BUILD_CPP, self.makefile[position:])
def chip_family(self):
position_start = self.get_position('-DSTM32F')
position_end = self.get_position_behind('-DSTM32F')
define = self.makefile[position_start:position_end].replace('F', '_F')
self.block_append(TAG_DEFINES_C, [define])
def alohal(self):
self.block_append(TAG_INCLUDES_C, ['-IALOHAL'])
def hide_command(self, cmd: str):
self.replace('\t' + cmd, '\t@' + cmd)
def show_command(self, cmd: str):
self.replace('\t@' + cmd, '\t' + cmd)
def stm32_programmer(self):
if os.path.isfile(CUBE_PROGEMMER):
position = self.get_position_front(TAG_EOF)
self.update(self.makefile[:position], CMD_FLASH.format(CUBE_PROGEMMER), self.makefile[position:])
position = self.get_position_front(TAG_EOF)
self.update(self.makefile[:position], CMD_BUILD_AND_FLASH.format(CUBE_PROGEMMER), self.makefile[position:])
else:
raise ProgrammerNotFound
def modify(self):
try:
self.unix_end_line()
self.check_was_modified()
self.update_toolchain()
self.support_cpp()
self.chip_family()
#self.alohal()
self.set_variable('OPT', '-Os')
self.hide_command('$(CC)')
self.hide_command('$(AS)')
self.hide_command('$(CP)')
self.hide_command('$(AR)')
self.hide_command('$(SZ)')
self.hide_command('$(HEX)')
self.hide_command('$(BIN)')
self.stm32_programmer()
except NotExist as e:
print(str(e), file=sys.stderr)
except MakefileIsModified as e:
print(str(e), file=sys.stderr)
except ProgrammerNotFound as e:
print(str(e), file=sys.stderr)
finally:
#self.repair_multiple_definition(TAG_SOURCES_PATH)
self.repair_multiple_definition(TAG_SOURCES_C)
self.repair_multiple_definition(TAG_SOURCES_ASM)
self.repair_multiple_definition(TAG_INCLUDES_C)
self.repair_multiple_definition(TAG_DEFINES_C)
self.repair_multiple_definition(TAG_DEFINES_ASM)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-f',
'--file',
dest='path',
action='store',
default='Makefile',
help='destination makefile',
)
Makefile(parser.parse_args().path)