-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
332 lines (298 loc) · 13.7 KB
/
build.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
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
#----------------------------------------------------------------------
#--- Python Build file for application blink
#--- automatically generated by goil on Mon May 15 12:11:08 2023
#--- from root OIL file pfeMouhoubi.oil
#---
#--- Compiling a Trampoline application is done in 2 stages
#--- 1 - From the OIL file, a set of files is generated as long as
#--- build options. Among these files are the build.py and make.py
#--- files. make.py contains compilation rules when the OIL file
#--- and the included OIL files are modified. make.py is only
#--- written once when the goil command is typed.
#--- 2 - From all the C/assembly files (OS, Application, librairies,
#--- configuration, ...), the objects and the executable are
#--- produced. This is done by the build.py file.
#--- build.py is generated each time goil is called by make.py
#----------------------------------------------------------------------
import sys, os, subprocess, string, distutils.spawn
from string import Template
#--- Add some function for Python3 support
if sys.version_info[0] >=3 :
def bytesdecode(obj) :
return obj.decode(sys.stdout.encoding)
else :
def bytesdecode(obj) :
return obj
#--- Add the location of makefile.py to the import path
sys.path.append("/usr/share/trampoline-master//make")
#--- Import the makefile system in python
import makefile, projfile
#--- To work with relative paths
scriptDir = os.path.dirname (os.path.abspath (sys.argv[0]))
os.chdir (scriptDir)
#--- Get goal as first argument
askedGoal = "all"
if len (sys.argv) > 1 :
askedGoal = sys.argv [1]
if askedGoal == "all" or askedGoal == "clean" :
goal = askedGoal
else :
goal = "all"
#--- Get max parallel jobs as second argument
maxParallelJobs = 0 # 0 means use host processor count
if len (sys.argv) > 2 :
maxParallelJobs = int (sys.argv [2])
#--- Instanciate a new makefile object
make = makefile.Make(goal)
#----------------------------------------------------------------------
#--- Various variables used after
#----------------------------------------------------------------------
compiler = r"arm-none-eabi-gcc"
cppCompiler = r"g++"
linker = r"arm-none-eabi-ld"
assembler = r"arm-none-eabi-as"
autosar = False
autosar_sc = 0
autosar_osapplications = False
with_ioc = False
with_com = False
scheduler = "osek"
trampoline_base_path = "/usr/share/trampoline-master//"
cflags = []
cppflags = []
ldflags = []
asflags = []
# flags that should appear at the beginning of the command.
precflags = []
precppflags = []
preldflags = []
preasflags = []
includeDirs = []
cflags += "-I../board/iutNantes/include".split()
cflags += "-O3 -std=c99".split()
cppflags += "-fno-rtti".split()
cppflags += "-felide-constructors".split()
cppflags += "-fno-threadsafe-statics".split()
cppflags += "-fno-use-cxa-get-exception-ptr".split()
cppflags += "-fno-enforce-eh-specs".split()
cflags += "-D__SAMD21J18A__".split()
cppflags += "-D__SAMD21J18A__".split()
cflags += "-g".split()
cppflags += "-g".split()
cflags += "-Wall".split()
cppflags += "-Wall".split()
cflags += "-Wno-unused-but-set-variable".split()
cppflags += "-Wno-unused-but-set-variable".split()
cflags += "-Wmissing-field-initializers".split()
cppflags += "-Wmissing-field-initializers".split()
cflags += "-mcpu=cortex-m0plus".split()
cppflags += "-mcpu=cortex-m0plus".split()
cflags += "-mthumb".split()
cppflags += "-mthumb".split()
cflags += "-mfloat-abi=soft".split()
cppflags += "-mfloat-abi=soft".split()
cflags += "-nostartfiles".split()
cppflags += "-nostartfiles".split()
cflags += "-fno-builtin".split()
cppflags += "-fno-builtin".split()
cflags += "--specs=nosys.specs".split()
cppflags += "--specs=nosys.specs".split()
cflags += "-fno-exceptions".split()
cppflags += "-fno-exceptions".split()
cflags += "-nostdlib".split()
cppflags += "-nostdlib".split()
cflags += "-ffunction-sections".split()
cppflags += "-ffunction-sections".split()
cflags += "-fdata-sections".split()
cppflags += "-fdata-sections".split()
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex/armv6m/samd21/XPlainedPro"]
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex"]
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex/armv6m"]
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex/CMSIS/Include"]
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex/armv6m/samd21/CMSIS-Atmel/Device/ATMEL"]
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex/armv6m/samd21"]
includeDirs += ["-I","/usr/share/trampoline-master//machines/cortex/armv6m/samd21/utils"]
if with_ioc:
includeDirs += ["-I", "/usr/share/trampoline-master//ioc"]
includeDirs += ["-I", "/usr/share/trampoline-master//os"]
includeDirs += ["-I", "/usr/share/trampoline-master//debug"]
includeDirs += ["-I", "pfeMouhoubi"]
cflags += includeDirs
cppflags += includeDirs
asflags += includeDirs
ldflags += "--fatal-warnings".split()
ldflags += "--warn-common".split()
ldflags += "--no-undefined".split()
ldflags += "--gc-sections".split()
ldflags += ["-L", "pfeMouhoubi"]
ldflags += ["-T", "script.ld"]
asflags += "-g".split()
asflags += "-Wall".split()
asflags += "-mcpu=cortex-m0plus".split()
asflags += "-mthumb".split()
asflags += "--fatal-warnings".split()
asflags += "-mfloat-abi=soft".split()
#----------------------------------------------------------------------
#--- Try to detect automatically the location of libc and libgcc
#--- This supposes the compiler has a (something)-gcc name
#--- for non posix target
#----------------------------------------------------------------------
if "gcc" in compiler:
compilerFullPath = makefile.find_executable(compiler)
if compilerFullPath != None:
#--- Extract the (something) from the compiler executable name
compilerParts = compiler.split('-')
compilerParts.pop()
compilerPrefix = "-".join(compilerParts)
#--- Get the full path of the compiler
compilerToolChainPath = os.path.dirname(os.path.dirname(compilerFullPath))
#--- Get the version of the compiler, [:-1] removes the carriage return
compilerVersion = subprocess.check_output([compiler, '-dumpversion'])[:-1]
#--- Build the libc and libgcc paths
procLibc =subprocess.Popen([compilerFullPath,"-print-file-name=libc.a"]+cflags,stdout=subprocess.PIPE)
procLibgcc=subprocess.Popen([compilerFullPath,"-print-libgcc-file-name"]+cflags,stdout=subprocess.PIPE)
procLibc.wait()
procLibgcc.wait()
libcPath = os.path.dirname(procLibc.stdout.readline().strip())
libgccPath = os.path.dirname(procLibgcc.stdout.readline().strip())
#--- Add both to linker flags
ldflags += ['-L' + bytesdecode(libcPath), '-lc']
ldflags += ['-L' + bytesdecode(libgccPath), '-lgcc']
#----------------------------------------------------------------------
#--- Build the source files list
#----------------------------------------------------------------------
cSourceList = []
cppSourceList = []
sSourceList = []
oilSourceList = []
#--- OIL file
oilSourceList.append("pfeMouhoubi.oil")
#--- Kernel files
cSourceList.append(projfile.ProjectFile("os/tpl_os_kernel.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_timeobj_kernel.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_action.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_error.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_os_kernel.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_os.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_interrupt_kernel.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_task_kernel.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_resource_kernel.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("os/tpl_os_alarm_kernel.c", trampoline_base_path))
#--- Add C files of the application
cSourceList.append(projfile.ProjectFile("../board/iutNantes/src/adc.c"))
cSourceList.append(projfile.ProjectFile("../board/iutNantes/src/oled.c"))
cSourceList.append(projfile.ProjectFile("../board/iutNantes/src/serial.c"))
cSourceList.append(projfile.ProjectFile("../board/iutNantes/src/dac.c"))
cSourceList.append(projfile.ProjectFile("../board/iutNantes/src/redirectPrintf.c"))
cSourceList.append(projfile.ProjectFile("../board/iutNantes/src/timer.c"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi.c"))
#--- Add generated files
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_app_config.c"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_dispatch_table.c"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_invoque.S"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_primary_irq.S"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_vectors.c"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_external_interrupts.c"))
cSourceList.append(projfile.ProjectFile("pfeMouhoubi/tpl_app_interrupts.c"))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/samd21/XPlainedPro/system_samd21_XPlainedPro_48MHz.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/tpl_machine_cortex.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/tpl_sc_handler.S", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/tpl_startup.S", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/sys_handlers.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/tpl_ctx_switch.S", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/tpl_ctx_switch_under_it.S", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/tpl_interrupts.S", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/samd21/tpl_machine_samd.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/samd21/startup_samd.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/samd21/utils/pinAccess.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/samd21/utils/pinMux.c", trampoline_base_path))
cSourceList.append(projfile.ProjectFile("machines/cortex/armv6m/samd21/utils/eic.c", trampoline_base_path))
#----------------------------------------------------------------------
#--- Build the object list and the compiler dependancies
#----------------------------------------------------------------------
objectList = []
for sourceFile in cSourceList:
source = sourceFile.src()
object = sourceFile.obj("build")
depObject = sourceFile.dep("build")
objectList.append(object)
rule = makefile.Rule([object], "Compiling " + source)
rule.deleteTargetDirectoryOnClean()
rule.mDependences.append(source)
rule.mCommand.append(compiler)
rule.mCommand += precflags
rule.mCommand += ["-c", source]
rule.mCommand += ["-o", object]
rule.mCommand += ["-MD", "-MP", "-MF", depObject]
rule.mCommand += cflags
rule.enterSecondaryDependanceFile (depObject, make)
make.addRule(rule)
for sourceFile in cppSourceList:
source = sourceFile.src()
object = sourceFile.obj("build")
depObject = sourceFile.dep("build")
objectList.append(object)
rule = makefile.Rule([object], "Compiling " + source)
rule.deleteTargetDirectoryOnClean()
rule.mDependences.append(source)
rule.mCommand.append(cppCompiler)
rule.mCommand += precppflags
rule.mCommand += ["-c", source]
rule.mCommand += ["-o", object]
rule.mCommand += ["-MD", "-MP", "-MF", depObject]
rule.mCommand += cppflags
rule.enterSecondaryDependanceFile (depObject, make)
make.addRule(rule)
for sourceFile in sSourceList:
source = sourceFile.src()
object = sourceFile.obj("build")
objectList.append(object)
rule = makefile.Rule([object], "Assembling " + source)
rule.mDependences.append(source)
rule.mCommand.append(assembler)
rule.mCommand += preasflags
rule.mCommand += ["-c", source]
rule.mCommand += ["-o", object]
rule.mCommand += asflags
make.addRule(rule)
product = "pfeMouhoubi.elf"
rule = makefile.Rule ([product], "Linking " + product)
rule.deleteTargetFileOnClean()
rule.mDeleteTargetOnError = True
rule.mDependences += objectList
rule.mCommand += [linker]
rule.mCommand += ["-o", product]
rule.mCommand += preldflags
rule.mCommand += objectList
rule.mCommand += ldflags
make.addRule (rule)
make.addGoal("all", [product], "Building all")
make.addGoal("compile", objectList, "Compile source files")
make.runGoal(maxParallelJobs, maxParallelJobs == 1)
postVariableMapping = dict(
MACHINE_PATH='/usr/share/trampoline-master//machines',
ARCH_PATH='/usr/share/trampoline-master//machines/cortex',
BOARD_PATH='/usr/share/trampoline-master//machines/cortex/armv6m/samd21/XPlainedPro',
TARGET='cortex/armv6m/samd21/XPlainedPro'
)
#----------------------------------------------------------------------
#--- post commands
#----------------------------------------------------------------------
if make.errorCount() == 0:
if askedGoal == "flash" :
commandLine = "openocd -f board/atmel_samd21_xplained_pro.cfg -c \"program "
commandLine += "pfeMouhoubi.elf "
commandLine += " verify reset exit\" "
commandLineTemplate = Template(commandLine)
commandLine = commandLineTemplate.safe_substitute(postVariableMapping)
if makefile.find_executable("openocd") != None:
print("\033[1m\033[95mFlashing pfeMouhoubi.elf on cortex/armv6m/samd21/XPlainedPro\033[0m")
subprocess.call(commandLine, shell=True)
else:
print("Command 'openocd' not in PATH")
make.printErrorCountAndExitOnError()
#----------------------------------------------------------------------
#-- End of build.py
#----------------------------------------------------------------------