-
Notifications
You must be signed in to change notification settings - Fork 1
/
umake.py
executable file
·524 lines (430 loc) · 17.8 KB
/
umake.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#!/usr/bin/python3
# This file is part of the hid_bootloader_console_client distribution
# (https: // github.com/nimo-labs/hid_bootloader_console_client).
# Copyright(c) 2021 Nimolabs Ltd. www.nimo.uk
#
# This program is free software: you can redistribute it and / or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see < http: // www.gnu.org/licenses/>.
import json
import os
import sys
from sys import exit
import platform
import shutil
# setting these to True inhibits generation of the corrisponding default target
inhibDefaultTgtMain = False
inhibDefaultTgtReset = False
inhibDefaultTgtChipErase = False
inhibDefaultTgtSize = False
inhibDefaultTgtClean = False
inhibDefaultTgtProgram = False
projectLinkerFile = False
def WORKING_DIR():
return "umake"
def DEP_FILE():
return "depfile"
def processLibs(umakefileJson, makefileHandle, depfileHandle):
for lib in umakefileJson["libraries"]:
currentLib = lib["libName"]
print("Processing library: %s" % currentLib)
if "checkout" in lib:
cmd = "git clone --quiet %s" % lib["libPath"]
else:
cmd = "git clone --quiet --depth 1 %s" % lib["libPath"]
print(cmd)
if 0 != os.system(cmd):
# We've already cloned the repo, so check for updates
os.chdir(currentLib)
os.system("git pull")
os.chdir("..")
# Check to see if umakefile specifies a checkout hash
if "checkout" in lib:
os.chdir(currentLib)
os.system("git checkout %s" % lib["checkout"])
os.chdir("..")
# Check to see if umkaefile specifies a checkout branch
if "branch" in lib:
os.chdir(currentLib)
cmd = "git checkout -b %s" % lib["branch"]
# print(cmd)
os.system(cmd)
# Make sure we get the latest branch commits
# os.system("git reset --hard origin/" + lib["branch"])
cmd = "git fetch origin " + lib["branch"]
# print(cmd)
os.system(cmd)
os.system("git reset --hard FETCH_HEAD")
os.chdir("..")
os.chdir(currentLib)
makefileHandle.write("# %s: books\n" % currentLib)
if "books" in lib:
for book in lib["books"]:
currentBook = book
try:
os.chdir(currentBook)
print("Processing book: %s/%s" % (currentLib, currentBook))
bookHandle = open(currentBook+".json", 'r')
bookJson = json.load(bookHandle)
if bookJson['book'] != currentBook:
print("Book: %s doesn't match current book: %s" %
(bookJson['book'], currentBook))
exit()
makefileHandle.write("# %s include paths\n" % currentBook)
makefileHandle.write(
"INCLUDES += -I ./umake/%s/%s/\n" % (currentLib, currentBook))
if "includes" in bookJson:
for includes in bookJson['includes']:
makefileHandle.write(
"INCLUDES += -I ./umake/%s/%s/%s\n" % (currentLib, currentBook, includes))
if "files" in bookJson:
makefileHandle.write(
"# %s source files\n" % currentBook)
for files in bookJson['files']:
bookFnLst = files["fileName"].split('/')
bookFn = bookFnLst[len(bookFnLst)-1]
if files["language"] == 'c':
makefileHandle.write(
"SRCS += ./umake/%s/%s/%s\n" % (currentLib, currentBook, files["fileName"]))
depfileHandle.write(
"./build/%s.o: ./umake/%s/%s/%s\n" % (bookFn[:-2], currentLib, currentBook, files["fileName"]))
depfileHandle.write("\t$(UMAKE_MAKEC)\n")
elif files["language"] == 'cpp':
makefileHandle.write(
"CPPSRCS += ./umake/%s/%s/%s\n" % (currentLib, currentBook, files["fileName"]))
elif files["language"] == 'asm':
makefileHandle.write(
"ARCS += ./umake/%s/%s/%s\n" % (currentLib, currentBook, files["fileName"]))
else:
print("Unknown language for %s/%s/%s" %
(currentLib, currentBook, files['fileName']))
if "cflags" in bookJson:
makefileHandle.write("# Book CFLAGS\n")
for cflags in bookJson["cflags"]:
makefileHandle.write("CFLAGS += %s\n" % cflags)
if "ldflags" in bookJson:
makefileHandle.write("# Book LDFLAGS\n")
for ldflags in bookJson["ldflags"]:
makefileHandle.write(
"LDFLAGS += %s\n" % ldflags)
makefileHandle.write("\n")
bookHandle.close()
except:
print("Unknown book %s in %s" %
(currentBook, currentLib))
exit(1)
os.chdir("..")
os.chdir("..")
def processUc(umakefileJson, makefileHandle, depfileHandle, ucLib):
microcontroller = umakefileJson['microcontroller']
restoreDir = os.getcwd()
try:
os.chdir(ucLib)
uCHandle = open("uc_"+microcontroller+"-linux-gcc.json", 'r')
uCJson = json.load(uCHandle)
if uCJson['microcontroller'] != microcontroller:
print("Microcontroller: %s doesn't match requested: %s" %
(uCJson['microcontroller'], microcontroller))
exit()
if "includes" in uCJson:
makefileHandle.write("\n# Microcontroller includes\n")
for uCIncludes in uCJson["includes"]:
makefileHandle.write("INCLUDES += -I %s\n" % uCIncludes)
if "defines" in uCJson:
makefileHandle.write("\n# Microcontroller defines\n")
for uCDefines in uCJson["defines"]:
makefileHandle.write("DEFINES += -D %s\n" % uCDefines)
if "cflags" in uCJson:
makefileHandle.write("\n# Microcontroller CFLAGS\n")
for uCCflags in uCJson["cflags"]:
makefileHandle.write("CFLAGS += %s\n" % uCCflags)
if "ldflags" in uCJson:
makefileHandle.write("\n# Microcontroller LDFLAGS\n")
for uCLdflags in uCJson["ldflags"]:
makefileHandle.write("LDFLAGS += %s\n" % uCLdflags)
if False == projectLinkerFile:
makefileHandle.write("\n# Linker file\n")
makefileHandle.write("LDFLAGS += -Wl,--script=%s\n" %
uCJson['linkerFile'])
except:
print("Unknown microcontroller: %s" %
(microcontroller))
exit(1)
# supportFiles superseeds startupFiles
if "supportFiles" in uCJson:
makefileHandle.write("\n# Support files\n")
for uCSupportFiles in uCJson["supportFiles"]:
filename, fileExt = os.path.splitext(uCSupportFiles)
if ".c" == fileExt:
makefileHandle.write("SRCS += %s\n" % uCSupportFiles)
if ".cpp" == fileExt:
makefileHandle.write("CPPSRCS += %s\n" % uCSupportFiles)
elif ".S" == fileExt:
makefileHandle.write("ASRCS += %s\n" % uCSupportFiles)
elif ".s" == fileExt:
makefileHandle.write("ASRCS += %s\n" % uCSupportFiles)
elif ".asm" == fileExt:
makefileHandle.write("ASRCS += %s\n" % uCSupportFiles)
startupFnLst = uCSupportFiles.split('/')
startupFn = startupFnLst[len(startupFnLst)-1]
depfileHandle.write(
"./build/%s.o: ./%s\n" % (startupFn[:-2], uCSupportFiles))
depfileHandle.write("\t$(UMAKE_MAKEC)\n")
else:
# This can probably be removed once supportFiles is implemented
print("Warning: The startupFile entry in uC books has been deprecated, use supportFiles instead")
makefileHandle.write("\n# Startup file\n")
makefileHandle.write("SRCS += %s\n" % uCJson['startupFile'])
startupFnLst = uCJson['startupFile'].split('/')
startupFn = startupFnLst[len(startupFnLst)-1]
depfileHandle.write(
"./build/%s.o: ./%s\n" % (startupFn[:-2], uCJson['startupFile']))
depfileHandle.write("\t$(UMAKE_MAKEC)\n")
os.chdir(restoreDir)
def writeBoilerPlate(makefileHandle):
makefileHandle.write("""CFLAGS += $(INCLUDES) $(DEFINES)
AASRCS = $(subst .s,.S, $(notdir $(ASRCS)))
ABSRCS = $(subst .asm,.S, $(notdir $(AASRCS)))
OBJS = $(addprefix $(BUILD)/, $(subst .c,.o, $(notdir $(SRCS))))
OBJS += $(addprefix $(BUILD)/, $(subst .S,.o, $(notdir $(ABSRCS))))
OBJS += $(addprefix $(BUILD)/, $(subst .cpp,.o, $(notdir $(CPPSRCS))))
define UMAKE_MAKEC
@echo CC $@
@${CC} $(CPPFLAGS) $(CFLAGS) -c $< -o $(addprefix ${BUILD}/, $(notdir $@))
endef
define UMAKE_MAKECPP
@echo CPP $@
@${CPP} $(CPPFLAGS) $(CFLAGS) -c $< -o $(addprefix ${BUILD}/, $(notdir $@))
endef
$(BUILD)/$(BIN).elf: $(OBJS)
@echo LD $@
@$(CPP) $(LDFLAGS) $(addprefix ${BUILD}/, $(notdir ${OBJS})) $(LIBS) -o $@
$(BUILD)/$(BIN).hex: $(BUILD)/$(BIN).elf
@echo OBJCOPY $@
@$(OBJCOPY) -O ihex $^ $@
$(BUILD)/$(BIN).bin: $(BUILD)/$(BIN).elf
@echo OBJCOPY $@
@$(OBJCOPY) -O binary $^ $@""")
def defaultTgtMain():
makefileHandle.write(
"_all: $(BUILD)/$(BIN).hex $(BUILD)/$(BIN).bin size")
def defaultTgtReset():
makefileHandle.write(
"""
reset: $(BUILD)/$(BIN).hex
killall -s 9 openocd || true
openocd -d1 -f ./openocd.cfg -c init -c \"reset\" -c \"exit\"""")
def defaultTgtChipErase():
makefileHandle.write(
"""
chiperase:
killall -s 9 openocd || true
openocd -d1 -f ./openocd.cfg -c init -c \"at91samd chip-erase\" -c \"exit\"""")
def defaultTgtSize():
makefileHandle.write(
"""
size: $(BUILD)/$(BIN).elf
@echo size:
@$(SIZE) -t $^""")
def defaultTgtClean():
if "Linux" == platform.system():
makefileHandle.write(
"""
clean:
\t@echo clean
\tfind ./build ! -name 'depfile' -type f -exec rm -f {} +
\t@-rm -rf ../*~""")
else:
makefileHandle.write(
"""
clean:
@echo clean
find ./build ! -name 'depfile' -type f -exec del -Force -Recurse {} +
@-del -Force -Recurse ../*~""")
def defaultTgtProgram():
makefileHandle.write(
"""
program: all
hidBoot w 0x3000 $(BUILD)/$(BIN).bin""")
def remove_readonly(func, path, _):
"Clear the readonly bit and reattempt the removal"
os.chmod(path, stat.S_IWRITE)
func(path)
def umakeClean(umakefileJson):
os.system("make clean")
if "Windows" == platform.system():
shutil.rmtree(WORKING_DIR(), onerror=remove_readonly)
shutil.rmtree(umakefileJson["buildDir"], onerror=remove_readonly)
shutil.rmtree("./Makefile", onerror=remove_readonly)
else:
shutil.rmtree(WORKING_DIR())
shutil.rmtree(umakefileJson["buildDir"])
os.remove("./Makefile")
# MAIN
try:
handle = open('./umakefile', 'r')
except:
print("./umakefile not found.")
exit(0)
try:
umakefileJson = json.load(handle)
except:
print("JSON load failed.")
exit(0)
try:
os.makedirs(WORKING_DIR(), exist_ok=True)
except OSError:
print("Creation of the directory %s failed" % WORKING_DIR())
if(len(sys.argv) > 1):
# We have command line args
if "clean" == sys.argv[1]:
umakeClean(umakefileJson)
exit(0)
makefileHandle = open('Makefile', 'w')
makefileHandle.write(
'# File autogenerated by umake, DO NOT hand edit. Changes will be overwritten\n')
# Create build directory
buildDir = umakefileJson['buildDir']
try:
os.makedirs(buildDir, exist_ok=True)
except OSError:
print("Creation of the directory %s failed" % buildDir)
depfileHandle = open(buildDir+"/"+DEP_FILE(), 'w')
depfileHandle.write(
'# File autogenerated by umake, DO NOT hand edit. Changes will be overwritten\n\n')
os.chdir(WORKING_DIR())
# Process libraries
processLibs(umakefileJson, makefileHandle, depfileHandle)
# Add phony statement
makefileHandle.write("\n.PHONY: all clean size\n")
# Add build dir
makefileHandle.write("\nBUILD = %s\n" % buildDir)
# Add binary filename
makefileHandle.write("BIN = %s\n\n" % umakefileJson['target'])
# Add project level sources
makefileHandle.write("# Project sources\n")
makefileHandle.write("# C sources\n")
for projSources in umakefileJson["c_sources"]:
rawFileName = os.path.basename(projSources)
filename, fileExt = os.path.splitext(projSources)
if ".c" == fileExt:
makefileHandle.write("SRCS += %s\n" % projSources)
rawFileName = rawFileName[:-2]
depfileHandle.write("./build/"+rawFileName+".o: "+projSources+"\n")
depfileHandle.write("\t$(UMAKE_MAKEC)\n")
elif ".S" == fileExt:
makefileHandle.write("ASRCS += %s\n" % projSources)
rawFileName = rawFileName[:-2]
depfileHandle.write("./build/"+rawFileName+".o: "+projSources+"\n")
depfileHandle.write("\t$(UMAKE_MAKEC)\n")
elif ".asm" == fileExt:
makefileHandle.write("ASRCS += %s\n" % projSources)
rawFileName = rawFileName[:-4]
depfileHandle.write("./build/"+rawFileName+".o: "+projSources+"\n")
depfileHandle.write("\t$(UMAKE_MAKEC)\n")
elif ".cpp" == fileExt:
makefileHandle.write("CPPSRCS += %s\n" % projSources)
rawFileName = rawFileName[:-4]
depfileHandle.write("./build/"+rawFileName+".o: "+projSources+"\n")
depfileHandle.write("\t$(UMAKE_MAKECPP)\n")
makefileHandle.write("\n")
makefileHandle.write("# Project include directories\n")
for projIncludes in umakefileJson["includes"]:
makefileHandle.write("INCLUDES += -I %s" % projIncludes)
makefileHandle.write("\n")
makefileHandle.write("\n\n")
# Project level compiler defines
if "defines" in umakefileJson:
makefileHandle.write("# Project level compiler defines\n")
for defines in umakefileJson["defines"]:
makefileHandle.write("DEFINES += %s\n" % defines)
makefileHandle.write("\n")
# Project level LD FLAGS
if "ldflags" in umakefileJson:
makefileHandle.write("# Project level linker flags\n")
for ldFlags in umakefileJson["ldflags"]:
makefileHandle.write("LDFLAGS += %s\n" % ldFlags)
makefileHandle.write("\n")
# Custom linker file
if "linkerFile" in umakefileJson:
projectLinkerFile = True
makefileHandle.write("# custom linkerfile\n")
makefileHandle.write("LDFLAGS += -Wl,--script=%s\n" %
umakefileJson["linkerFile"])
makefileHandle.write("\n")
# Setup micro-controller library
if "ucLib" in umakefileJson:
ucLib = umakefileJson["ucLib"]
else:
print("Error, ucLib not defined")
exit(1)
# Process microcontroller
processUc(umakefileJson, makefileHandle, depfileHandle, ucLib)
# Define toolchain
makefileHandle.write("# Define toolchain\n")
makefileHandle.write("CC = " + umakefileJson["toolchain"]+"-gcc\n")
makefileHandle.write("CPP = " + umakefileJson["toolchain"]+"-g++\n")
makefileHandle.write("OBJCOPY = " + umakefileJson["toolchain"]+"-objcopy\n")
makefileHandle.write("SIZE = " + umakefileJson["toolchain"]+"-size\n")
# Primary ALL target
makefileHandle.write("\n# Primary target\n")
makefileHandle.write("all: _all\n\n")
# Custom targets
makefileHandle.write("# Custom targets\n")
if "targets" in umakefileJson:
for custTargs in umakefileJson["targets"]:
# Check if there is a default for this target, if so disable its generation
if(custTargs["targetName"].lower() == "main"):
inhibDefaultTgtMain = True
if(custTargs["targetName"].lower() == "reset"):
inhibDefaultTgtReset = True
if(custTargs["targetName"].lower() == "chiperase"):
inhibDefaultTgtChipErase = True
if(custTargs["targetName"].lower() == "size"):
inhibDefaultTgtSize = True
if(custTargs["targetName"].lower() == "clean"):
inhibDefaultTgtClean = True
if(custTargs["targetName"].lower() == "program"):
inhibDefaultTgtProgram = True
# Generate custom target
try:
makefileHandle.write("%s: %s\n" %
(custTargs["targetName"], custTargs["depends"]))
except:
print("Error, Target: \"%s\" missing depends key." %
custTargs["targetName"])
exit(0)
for content in custTargs["content"]:
makefileHandle.write("\t%s\n" % content)
makefileHandle.write("\n")
# Default targets
makefileHandle.write("# Default targets\n")
if False == inhibDefaultTgtMain:
defaultTgtMain()
if False == inhibDefaultTgtReset:
defaultTgtReset()
if False == inhibDefaultTgtChipErase:
defaultTgtChipErase()
if False == inhibDefaultTgtSize:
defaultTgtSize()
if False == inhibDefaultTgtClean:
defaultTgtClean()
if False == inhibDefaultTgtProgram:
defaultTgtProgram()
makefileHandle.write("\n\n")
# Boiler plate
makefileHandle.write("# Boiler plate\n")
writeBoilerPlate(makefileHandle)
# Depfile
makefileHandle.write("\n\n# Depfile\n")
makefileHandle.write("include "+buildDir+"/"+DEP_FILE()+"\n")
makefileHandle.close()
depfileHandle.close()