-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmk2cmake.py
executable file
·338 lines (274 loc) · 12.3 KB
/
mk2cmake.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
import string
import random
import os
import argparse
# Makefileにインジェクトする文字列ファイルをランダムなファイル名で生成する
# classが破壊されるとともに,ファイルは削除される
class makefileInjectant:
def __init__(self):
#ランダム文字列の注入物質ファイル名を生成
self.injectant = """print-%:
@echo '$($*)'"""
self.path_to_injectant =\
os.getcwd() + '/.__'+''.join([random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in range(10)])\
+ '.mk'
f = open(self.path_to_injectant, 'w')
f.write(self.injectant)
f.close()
def getPathToInjectant(self):
return self.path_to_injectant
def __del__(self):
os.remove(self.path_to_injectant)
# Makefileへのパスと変数名から,変数内容をテキストで出力 utf-8で出力
def getMakefileVariableText(path_to_makefile_, variable_):
inj = makefileInjectant()
path_to_injectant = inj.getPathToInjectant()
# print(path_to_injectant)
ord_val = 'print-' + variable_
mkout = subprocess.check_output(['make','-C', os.path.dirname(path_to_makefile_), '-f', path_to_injectant, '-f', path_to_makefile_, ord_val]).decode('utf8')
return mkout
# Makefileへのパスと変数名から,変数内容を配列で出力
def getMakefileVariable(path_to_makefile_, variable_):
val_text = getMakefileVariableText(path_to_makefile_, variable_)
val_text_split = val_text.split(' ')
vals = []
for v in val_text_split:
if((v == '') | (v == '\n')):
continue
vals.append(v.replace('\n', ''))
return vals
def main():
# 引数
# パーサの作成
psr = argparse.ArgumentParser()
psr.add_argument('--path_to_makefile', '-i', \
default='./Makefile', \
type=str, \
help='Path to CubeMX generated Makefile')
psr.add_argument('--path_to_output_directory', '-o', \
default='.', \
type=str, \
nargs=1, \
help='Path to otuput directory of CMakeists.txt')
arg = psr.parse_args()
############################################
# SET DEFAULF TOOL CHAIN PATH
############################################
path_to_default_toolchain = '/opt/env/gcc-arm-none-eabi-6-2017-q2-update'
path_to_makefile = arg.path_to_makefile
path_to_cmakelists_template = './template/CMakeLists.txt'
path_to_output = arg.path_to_output_directory[0] + '/CMakeLists.txt'
# print(path_to_makefile)
# print(path_to_output)
# path_to_makefile = './Makefile'
# path_to_output = './CMakeLists.txt'
# inj = makefileInjectant()
# mkout = subprocess.check_output(['make', '-f', 'inj.mk', '-f', 'Makefile', 'print-C_SOURCES']).decode('utf8')
# print(mkout)
# print(mkout.replace(' ', '\n'))
# print(mkout.split(' '))
# print(getMakefileVariableText('Makefile', 'C_SOURCES').split(' '))
# print(getMakefileVariable(path_to_makefile, 'OBJECTS'))
# for i in getMakefileVariable('Makefile', 'TARGET'):
# print(i)
root_dir = "${PROJECT_SOURCE_DIR}"
# CMakeLists.txtのテンプレートを読み込み
cmake_template_ = open(path_to_cmakelists_template).read()
cmake_template = cmake_template_
# 出力用ファイルハンドラ
f_out = open(path_to_output, 'w')
##############################################
# プロジェクト設定
##############################################
target_name = getMakefileVariable(path_to_makefile, 'TARGET')[0]
set_project = 'PROJECT(' + target_name + ' C CXX ASM)'
#テンプレートを置換
cmake_template = cmake_template.replace("###%PROJECT%###", set_project)
##############################################
# ソース,インクルードディレクトリ関係の情報をあつめる
##############################################
# C source の設定
set_c_sources = 'SET(C_SOURCES '
c_sources = getMakefileVariable(path_to_makefile, 'C_SOURCES')
for i in c_sources:
set_c_sources = set_c_sources + root_dir + '/' + i + ' '
set_c_sources = set_c_sources + ')'
print('###########################################')
print(set_c_sources)
#テンプレートを置換
cmake_template = cmake_template.replace('###%C_SOURCES%###', set_c_sources)
# ASM source の設定
set_asm_sources = 'SET(ASM_SOURCES '
asm_sources = getMakefileVariable(path_to_makefile, 'ASM_SOURCES')
for i in asm_sources:
set_asm_sources = set_asm_sources + root_dir + '/' + i + ' '
set_asm_sources = set_asm_sources + ')'
print('###########################################')
print(set_asm_sources)
#テンプレートを置換
cmake_template = cmake_template.replace('###%ASM_SOURCES%###', set_asm_sources)
# C include dirs の設定
set_c_includes = 'SET(C_INCLUDES '
c_includes = getMakefileVariable(path_to_makefile, 'C_INCLUDES')
for i in c_includes:
inc_d = root_dir + '/' + i.replace('-I', '') + ' '
set_c_includes = set_c_includes + inc_d
set_c_includes = set_c_includes + ')'
# include dirs 設定コマンド
set_include_directories_c = 'INCLUDE_DIRECTORIES(${C_INCLUDES})'
print('###########################################')
print(set_c_includes)
#テンプレートを置換
cmake_template = cmake_template.replace('###%C_INCLUDES%###', set_c_includes)
# ASM include dirs の設定
set_as_includes = 'SET(AS_INCLUDES '
as_includes = getMakefileVariable(path_to_makefile, 'AS_INCLUDES')
for i in as_includes:
inc_d = root_dir + '/' + i.replace('-I', '') + ' '
set_as_includes = set_as_includes + inc_d
set_as_includes = set_as_includes + ')'
# include dirs 設定コマンド
set_include_directories_as = 'INCLUDE_DIRECTORIES(${AS_INCLUDES})'
print('###########################################')
print(set_as_includes)
#テンプレートを置換
cmake_template = cmake_template.replace('###%ASM_INCLDUES%###', set_as_includes)
##############################################
# define系の設定
##############################################
as_defines = getMakefileVariable(path_to_makefile, 'AS_DEFS')
c_defines = getMakefileVariable(path_to_makefile, 'C_DEFS')
set_add_definitions = ''
for i in as_defines:
set_add_definitions = set_add_definitions + 'add_definitions(' + i + ')\n'
for i in c_defines:
set_add_definitions = set_add_definitions + 'add_definitions(' + i + ')\n'
print('###########################################')
print(set_add_definitions)
#テンプレートを置換
cmake_template = cmake_template.replace('###%DEFINITIONS%###', set_add_definitions)
##############################################
# コンパイラのフラグ関係の情報を集める
##############################################
# MCU関係
set_mcu = 'SET(MCU "'
mcu_options = getMakefileVariable(path_to_makefile, 'MCU')
for i in mcu_options:
set_mcu = set_mcu + i + ' '
set_mcu = set_mcu + '")'
print('###########################################')
print(set_mcu)
#テンプレートを置換
cmake_template = cmake_template.replace('###%MCU%###', set_mcu)
# 最適化オプション
set_opt = 'SET(OPT "'
opt_options = getMakefileVariable(path_to_makefile, 'OPT')
for i in opt_options:
set_opt = set_opt + i + ' '
set_opt = set_opt + '")'
print('###########################################')
print(set_opt)
#テンプレートを置換
cmake_template = cmake_template.replace('###%OPT%###', set_opt)
# アセンブラフラグ
set_asflags = 'SET(ASFLAGS "-x assembler-with-cpp ${MCU} ${OPT} -Wall -fdata-sections -ffunction-sections")'
set_cmake_asm_flags = 'SET(CMAKE_ASM_FLAGS ${ASFLAGS})'
print('###########################################')
print(set_asflags)
print(set_cmake_asm_flags)
#テンプレートを置換
cmake_template = cmake_template.replace('###%ASM_FLAGS%###', set_asflags)
# Cコンパイラフラグ
set_cflags = 'SET(CFLAGS "${MCU} ${OPT} -Wall -fdata-sections -ffunction-sections -g -gdwarf-2")'
set_cmake_c_flags = 'SET(CMAKE_C_FLAGS ${CFLAGS})'
print('###########################################')
print(set_cflags)
print(set_cmake_c_flags)
#テンプレートを置換
cmake_template = cmake_template.replace('###%C_FLAGS%###', set_cflags)
#### リンカフラグ ####
# リンカスクリプトパス
set_ldscript = 'SET(LDSCRIPT '
ldscripts = getMakefileVariable(path_to_makefile, 'LDSCRIPT')
for i in ldscripts:
p2ldscript = root_dir + '/' + i + ' '
set_ldscript = set_ldscript + p2ldscript
set_ldscript = set_ldscript + ')'
print('###########################################')
print(set_ldscript)
#テンプレートを置換
cmake_template = cmake_template.replace('###%LDSCRIPTS%###', set_ldscript)
# ライブラリ
set_libs = 'SET(LIBS "'
libs = getMakefileVariable(path_to_makefile, 'LIBS')
for i in libs:
set_libs = set_libs + i + ' '
set_libs = set_libs + '")'
print('###########################################')
print(set_libs)
#テンプレートを置換
cmake_template = cmake_template.replace('###%LIBS%###', set_libs)
# リンカフラグ
set_ldflags = 'SET(LDFLAGS "${MCU} -specs=nano.specs -T${LDSCRIPT} ${LIBS} -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections")'
print('###########################################')
print(set_ldflags)
#テンプレートを置換
cmake_template = cmake_template.replace('###%LDFLAGS%###', set_ldflags)
# リンカフラグ設定
set_cmake_exe_linker_flags = 'SET(CMAKE_EXE_LINKER_FLAGS ${LDFLAGS})'
##############################################
# クロスコンパイラのパス
##############################################
path_to_bin_dirs = getMakefileVariable(path_to_makefile, 'BINPATH')
path_to_bin_dir = ''
path_to_bin_dir_cxx = ''
if(len(path_to_bin_dirs) == 0) :
path_to_bin_dir = path_to_default_toolchain + "/bin"
path_to_bin_dir_cxx = path_to_bin_dir
else :
path_to_bin_dir = ''
path_to_bin_dir_cxx = path_to_bin_dirs[0]
bin_prefix = getMakefileVariable(path_to_makefile, 'PREFIX')[0]
path_to_cc = path_to_bin_dir + getMakefileVariable(path_to_makefile, 'CC')[0]
path_to_cxx = path_to_bin_dir_cxx + '/' + bin_prefix + 'g++'
path_to_as = path_to_cc
path_to_cp = path_to_bin_dir + getMakefileVariable(path_to_makefile, 'CP')[0]
path_to_ar = path_to_bin_dir + getMakefileVariable(path_to_makefile, 'AR')[0]
path_to_sz = path_to_bin_dir + getMakefileVariable(path_to_makefile, 'SZ')[0]
print('###########################################')
print(path_to_bin_dir)
print(bin_prefix)
print(path_to_cc)
print(path_to_cxx)
print(path_to_as)
print(path_to_cp)
print(path_to_ar)
print(path_to_sz)
set_cmake_c_compiler = 'SET(CMAKE_C_COMPILER ' + path_to_cc + ')'
set_cmake_cxx_compiler = 'SET(CMAKE_CXX_COMPILER ' + path_to_cxx + ')'
set_cmake_asm_compiler = 'SET(CMAKE_ASM_COMPILER ' + path_to_as + ')'
set_cp = 'SET(OBJCP ' + path_to_cp + ')'
print('###########################################')
print(set_cmake_asm_compiler)
print(set_cmake_c_compiler)
print(set_cmake_cxx_compiler)
print(set_cp)
#テンプレートを置換
cmake_template = cmake_template.replace('###%C_COMPILER%###', set_cmake_c_compiler)
#テンプレートを置換
cmake_template = cmake_template.replace('###%CXX_COMPILER%###', set_cmake_cxx_compiler)
#テンプレートを置換
cmake_template = cmake_template.replace('###%ASM_COMPILER%###', set_cmake_asm_compiler)
#テンプレートを置換
cmake_template = cmake_template.replace('###%CP%###', set_cp)
##############################################
# 置換テンプレートを保存
##############################################
print('############################################')
print(cmake_template)
f_out.write(cmake_template)
f_out.close()
if __name__ == '__main__': main()