-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.py
288 lines (252 loc) · 9.08 KB
/
make.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
import sys
import os
import os.path
import subprocess
import attachmod
'''
COMMAND LINE SUPPORT
'''
def __executecmd(cwd, args):
p = subprocess.Popen(args, cwd = cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
return (p.stdout.read().decode('utf-8'), p.stderr.read().decode('utf-8'))
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def executecmd(cwd, args, cmdshow=True):
if cmdshow or True:
print('\t\t[%s] %s' % (cwd, args))
args = args.split(' ')
so, se = __executecmd(cwd, args)
no = []
parts = se.split('\n')
for part in parts:
no.append('\t%s\n' % part)
no = ''.join(no)
if len(no.strip()) > 0:
print(bcolors.HEADER + bcolors.WARNING + no + bcolors.ENDC)
if len(se) > 0:
return False
return True
'''
GENERIC DIRECTORY COMPILATION WITH GCC COMPATIBLE COMPILER
'''
def compileDirectory(cfg, dir):
nodes = os.listdir(dir)
_hdrpaths = []
for hdrpath in cfg['hdrpaths']:
_hdrpaths.append('-I%s' % hdrpath)
hdrpaths = ' '.join(_hdrpaths)
objs = []
for node in nodes:
if node.find('.') < 1:
continue
ext = node[node.find('.') + 1:]
base = node[0:node.find('.')]
if ext == 'c':
if not isNewerThan('%s/%s.o' % (dir, base), '%s/%s.c' % (dir, base)):
print(' [CC] %s' % (node))
if executecmd(dir, '%s %s -c %s %s' % (cfg['CC'], cfg['CCFLAGS'], node, hdrpaths), cmdshow=cfg['cmdshow']) is False:
return (False, objs)
else:
print(' [GOOD] %s' % (node))
objs.append('%s.o' % base)
return (True, objs)
'''
SPECIFIC BUILD SUPPORT
Certain parts of the system are built a differenty, and these
provide a solution to these in a modular way.
'''
def makeModule(cfg, dir, out):
print((bcolors.HEADER + bcolors.OKGREEN + 'module-make [%s]' + bcolors.ENDC) % (out))
# i want some things enabled for modules but not the kernel
old = cfg['CCFLAGS']
cfg['CCFLAGS'] = '-Wimplicit-function-declaration %s' % cfg['CCFLAGS']
res, objs = compileDirectory(cfg, dir)
cfg['CCFLAGS'] = old
objs = ' '.join(objs)
# the -N switch has to prevent the file_offset in the elf32 from being
# equal to the VMA address which was bloating the modules way too much
# now they should be aligned to a 4K boundary or something similar
if executecmd(dir, '%s -T ../../module.link -L%s -N -o ../%s.mod ../../corelib/vmessage.o ../../corelib/linkhelper.o ../../corelib/linklist.o ../../corelib/kheap_bm.o ../../corelib/atomicsh.o ../../corelib/core.o ../../corelib/rb.o %s -lgcc' % (cfg['LD'], cfg['LIBGCCPATH'], out, objs), cmdshow=cfg['cmdshow']) is False:
return False
#if executecmd(dir, '%s -S ../%s.mod ../%s.mod' % (cfg['OBJCOPY'], out, out), cmdshow=cfg['cmdshow']) is False:
# return False
pass
def compileCoreLIB(cfg, dir):
print(bcolors.HEADER + bcolors.OKGREEN + 'CORELIB-compile' + bcolors.ENDC)
res, objs = compileDirectory(cfg, dir)
return res
def isNewerThan(obj, source):
if os.path.exists(obj) is False:
return False
if os.path.getmtime(obj) > os.path.getmtime(source):
return True
return False
def makeKernel(cfg, dir, out, bobjs):
print(bcolors.HEADER + bcolors.OKGREEN + 'kernel-compile' + bcolors.ENDC)
# compile all source files
old_ccflags = cfg['CCFLAGS']
cfg['CCFLAGS'] = '%s -DKERNEL' % cfg['CCFLAGS']
if executecmd(dir, '%s %s -c %s -o ./corelib/kheap_bm_kernel.o' % (cfg['CC'], cfg['CCFLAGS'], './corelib/kheap_bm.c'), cmdshow=cfg['cmdshow']) is False:
cfg['CCFLAGS'] = old_ccflags
return False
res, objs = compileDirectory(cfg, dir)
cfg['CCFLAGS'] = old_ccflags
if res is False:
return False
# make sure main.o is linked first
_objs = []
for obj in objs:
if obj != 'main.o':
_objs.append(obj)
objs = _objs
objs = ' '.join(objs)
bobjs = ' '.join(bobjs)
tmp = '__armos.bin'
# link it
# %s/libgcc.a
# cfg['LIBGCCPATH'],-L%s
if executecmd(dir, '%s -T link.ld -o %s main.o -L%s ./corelib/kheap_bm_kernel.o ./corelib/linklist.o ./corelib/atomicsh.o ./corelib/rb.o %s %s -lgcc' % (cfg['LD'], tmp, cfg['LIBGCCPATH'], objs, bobjs), cmdshow=cfg['cmdshow']) is False:
return False
# strip it
if executecmd(dir, '%s -j .text -O binary %s %s' % (cfg['OBJCOPY'], tmp, out), cmdshow=cfg['cmdshow']) is False:
return False
return True
def clean(dir):
print(bcolors.HEADER + bcolors.OKGREEN + ('CLEAN [%s]' % dir) + bcolors.ENDC)
nodes = os.listdir(dir)
for node in nodes:
if node[0] == '.':
continue
if os.path.isdir('%s/%s' % (dir, node)):
clean('%s/%s' % (dir, node))
if node.find('.') < 1:
continue
ext = node[node.find('.') + 1:]
base = node[0:node.find('.')]
if ext == 'o':
print(' [DELETED] %s' % (node))
os.remove('%s/%s' % (dir, node))
'''
This is the main method to make the system.
'''
def make(cfg):
board = cfg['board']
modules= cfg['modules']
# compile corelib
if compileCoreLIB(cfg, dir = './corelib') is False:
return False
# special object used by modules
# TODO: fix cant find CC executable
#if executecmd('./', '%s %s -c %s %s' % (cfg['CC'], cfg['CCFLAGS'], 'atomicsh.c', ''), cmdshow=cfg['cmdshow']) is False:
# return False
nodes = os.listdir(cfg['dirofmodules'])
# compile modules
for mdir in nodes:
if os.path.isdir('%s/%s' % (cfg['dirofmodules'], mdir)) is False:
continue
res = makeModule( cfg = cfg,
dir = '%s/%s' % (cfg['dirofmodules'], mdir),
out = mdir
)
if res is False:
return False
nodes = os.listdir(cfg['dirofboards'])
# compile boards
for bdir in nodes:
if os.path.isdir('%s/%s' %(cfg['dirofboards'], bdir)) is False:
continue
# at the moment i just compile all boards but in the future this could
# be changed to compile just the target board because some boards could
# be architecture specific use inline assembly which would fail and clutter
# up the output with errors.. unless we change the way we handle things
print((bcolors.HEADER + bcolors.OKGREEN + 'board-make [%s]' + bcolors.ENDC) % (bdir))
oldcc = cfg['CCFLAGS']
cfg['CCFLAGS'] = '%s -DKERNEL' % cfg['CCFLAGS']
res, objs = compileDirectory(cfg = cfg, dir = '%s/%s' % (cfg['dirofboards'], bdir))
cfg['CCFLAGS'] = oldcc
if res is False:
return False
# if this board module is to be included in the kernel then
# we want to track the object files produced so we can directly
# include them into the kernel linking process
if bdir == cfg['board']:
bobjs = []
for o in objs:
bobjs.append('%s/%s/%s' % (cfg['dirofboards'], bdir, o))
# compile kernel
if makeKernel(cfg, dir = './', out = cfg['kimg'], bobjs = bobjs) is False:
return False
# attach modules
for mod in modules:
sz = attachmod.attach('%s/%s.mod' % (cfg['dirofmodules'], mod), cfg['kimg'], 1)
print((bcolors.HEADER + bcolors.OKBLUE + 'attached [' + bcolors.OKGREEN + '%s' + bcolors.OKBLUE + '] @ %s bytes' + bcolors.ENDC) % (mod, sz))
return True
def readfile(path):
fd = open(path, 'r')
d = fd.read().strip()
fd.close()
return d
cfg = {}
# configuration specific (may want to take these from environment vars if existing)
cfg['CC'] = 'arm-eabi-gcc'
cfg['LD'] = 'arm-eabi-ld'
cfg['AR'] = 'arm-eabi-ar'
cfg['OBJCOPY'] = 'arm-eabi-objcopy'
# -save-temps -save-temps=cwd
cfg['CCFLAGS'] = '-O3 -mcpu=cortex-a9 -fno-builtin-free -fno-builtin-printf -fno-builtin-sprintf -fno-builtin-memset -fno-builtin-memcpy -fno-builtin-malloc'
cfg['hdrpaths'] = ['../../', '../', './corelib/']
cfg['dirofboards'] = './boards'
cfg['dirofmodules'] = './modules'
cfg['board'] = 'realview-pb-a'
cfg['modules'] = ['testuelf', 'fs']
cfg['kimg'] = './armos.bin'
cfg['ldflags'] = ''
cfg['cmdshow'] = False
cfg['LIBGCCPATH'] = '/mnt/host/old/armos/thintest/toolhelp_x86_64'
def showHelp():
print('%-20sdisplays list of modules' % 'showmodules')
print('%-20sdisplays list of boards' % 'showboards')
print('%-20sbuilds the system' % 'make <board> <modules..>')
if len(sys.argv) < 2:
showHelp()
else:
farg = sys.argv[1]
if farg == 'showmodules':
nodes = os.listdir(cfg['dirofmodules'])
for node in nodes:
if os.path.isdir('%s/%s' % (cfg['dirofmodules'], node)) is False:
continue
info = readfile('%s/%s/info' % (cfg['dirofmodules'], node))
print((bcolors.HEADER + bcolors.OKGREEN + '%-20s%s' + bcolors.ENDC) % (node, info))
exit()
if farg == 'showboards':
nodes = os.listdir(cfg['dirofboards'])
for node in nodes:
if os.path.isdir('%s/%s' % (cfg['dirofboards'], node)) is False:
continue
info = readfile('%s/%s/info' % (cfg['dirofboards'], node))
print((bcolors.HEADER + bcolors.OKGREEN + '%-20s%s' + bcolors.ENDC) % (node, info))
exit()
if farg == 'clean':
clean('./')
exit()
if farg == 'make':
if len(sys.argv) < 3:
print('missing <board> argument (first argument)')
exit()
cfg['board'] = sys.argv[2]
mods = []
x = 3
while x < len(sys.argv):
mods.append(sys.argv[x])
x = x + 1
cfg['modules'] = mods
if make(cfg) is False:
exit(-1)
exit()
showHelp()