-
Notifications
You must be signed in to change notification settings - Fork 6
/
SConstruct
270 lines (226 loc) · 5.64 KB
/
SConstruct
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
#!/usr/bin/env python
EnsureSConsVersion(2, 5)
#
# Imports
#
import os
import re
from subprocess import call, check_call, Popen
#
# Builders
#
# Builder :: CrystalProgram
_builder_crystal = Builder(
action='$CC build $CCFLAGS -o $TARGET $SOURCE'
,src_suffix='.cr'
,single_source=True
)
# Builder :: Link
_builder_link = Builder(
action='$LD -o $TARGET $SOURCES $LDFLAGS'
,suffix='.o'
,src_suffix='.o'
)
#
# Defaults
#
default_arch = 'i386'
default_host = 'i686-elf'
#
# Options
#
AddOption(
'--arch'
,dest='arch'
,type='string'
,action='store'
,help='Target architecture'
,default=default_arch
)
AddOption(
'--host'
,dest='host'
,type='string'
,action='store'
,help='Target host'
,default=default_host
)
AddOption(
'--qemu'
,dest='qemu'
,action='store_const'
,const='normal'
)
AddOption(
'--qemu-curses'
,dest='qemu'
,action='store_const'
,const='curses'
)
AddOption(
'--test'
,dest='test'
,action='store_true'
)
#
# Globals
#
qemu = GetOption('qemu')
test = GetOption('test')
target = {
'arch': GetOption('arch'),
'host': GetOption('host'),
'triple': '{}-unknown-nuummite-none'.format(GetOption('arch')),
}
dirs = {
'conf': 'conf',
'build': 'build',
'kernel': 'kernel',
'kernel:src': 'kernel/src',
'runtime': 'kernel/runtime',
'sysroot': 'sysroot',
'sysroot:boot': 'sysroot/boot',
'arch': 'kernel/arch/{}'.format(target['arch']),
'asm': 'kernel/arch/{}/asm'.format(target['arch']),
'iso': 'build/isodir',
'iso:boot': 'build/isodir/boot',
'iso:grub': 'build/isodir/boot/grub',
'iso:tree': 'build/isodir/boot/grub',
'grub': 'conf/grub',
}
files = {
# Source files
'src': {
'grub.cfg': os.path.join(dirs['grub'], 'grub.cfg'),
'kernel': os.path.join(dirs['kernel:src'], 'kernel.cr'),
},
# Destination files
'dst': {
'grub.cfg': os.path.join(dirs['iso:grub'], 'grub.cfg'),
'kernel': os.path.join(dirs['iso:boot'], 'nuummite.kern'),
'iso': '{build}/nuummite-{arch}.iso'.format(build=dirs['build'], arch=target['arch']),
'boot:kernel': '{sysroot}/kernel.o'.format(sysroot=dirs['sysroot:boot']),
},
# Object files
'obj': {
'kernel': '{build}/nuummite-{arch}.o'.format(build=dirs['build'], arch=target['arch']),
},
}
# List of all objects
objects = [
os.path.join(dirs['kernel:src'], 'kernel.o'),
]
#
# SConstruct Environment
#
env = Environment(
ENV=os.environ,
BUILDERS={
'CrystalProgram': _builder_crystal,
'Link': _builder_link
}
)
# Assembler
env['AS'] = 'nasm'
env['ASFLAGS'] = '-felf32'
# Linker
env['LD'] = '{host}-gcc'.format(host=target['host'])
env['LDFLAGS'] = (
' -T{ldscript}'
' -ffreestanding'
' -nostdlib'
' -lgcc'
' -Wl,--nmagic,--gc-sections'
).format(ldscript=os.path.join(dirs['arch'], 'linker.ld'))
# Compiler
env['CC'] = 'crystal'
env['CCFLAGS'] = (
' --emit=obj'
' --cross-compile'
' --target={target}'
' --prelude=empty'
).format(target=target['triple'])
#
# Configuration
#
if not env.GetOption('clean'):
conf = Configure(env)
for prog in [env['AS'], env['LD'], env['CC'], 'grub-mkrescue']:
if conf.CheckProg(prog): continue
print('Unable to locate `{prog}`.'.format(prog=prog))
Exit(1)
env = conf.Finish()
#
# Commands
#
# Command :: Validate architecture
def ValidateArchitecture(*args, **kwargs):
if not os.path.exists(dirs['arch']) or not os.path.isdir(dirs['arch']):
print('Unknown architecture: `{arch}`.'.format(arch=target['arch']))
Exit(2)
print('Target: [arch: `{arch}`; host: `{host}`]'.format(
arch=target['arch'],
host=target['host']))
# Command :: Build kernel image
def BuildKernelImage(target, source, env):
args = [
'grub-mkrescue'
,'--output={iso}'.format(iso=files['dst']['iso'])
,'{isodir}'.format(isodir=dirs['iso'])
]
retc = check_call(args)
if retc == 0:
return
print("Unable to build the kernel image.")
Exit(3)
# Command :: Run QEMU
def RunQEMU(*args, **kwargs):
args = 'qemu-system-{arch} -cdrom {iso} {curses}'.format(
arch=target['arch'],
iso=files['dst']['iso'],
curses=('--curses' if qemu == 'curses' else ''))
call(args, shell=True)
# Command :: Run Tests
def RunTests(*args, **kwargs):
args = 'crystal spec {runtime} -Dnostd'.format(runtime=dirs['runtime'])
call(args, shell=True)
#
# Build Preparation
#
if not env.GetOption('clean'):
Execute([
ValidateArchitecture,
Mkdir(dirs['iso:tree']),
Mkdir(dirs['sysroot:boot']),
])
#
# Build Rules
#
# Rule :: Assemble sources
asm_sources = [p for p in os.listdir(dirs['asm']) if p.endswith('.asm')]
for f in asm_sources:
f = os.path.join(dirs['asm'], f)
o = env.StaticObject(source=f)
objects.append(o[0])
# Rule :: Compile kernel
crystal = env.CrystalProgram(source=files['src']['kernel'])
# Rule :: Link objects
kernel = env.Link(target=files['obj']['kernel'], source=objects)
Requires(kernel, crystal)
# Rule :: Build kernel image
iso = env.Command(
files['dst']['iso'],
kernel, [
# Update grub configuration
Copy(files['dst']['grub.cfg'], files['src']['grub.cfg']),
# Copy the kernel object to its destinations
Copy(files['dst']['kernel'], files['obj']['kernel']),
Copy(files['dst']['boot:kernel'], files['dst']['kernel']),
# Build the kernel image
BuildKernelImage,
]
)
# Rule :: Run QEMU
if qemu: env.Command('__qemu', iso, RunQEMU)
# Rule :: Run tests
env.Alias('test', env.Command('__test', None, RunTests))