-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·476 lines (430 loc) · 14.5 KB
/
build.sh
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
#!/bin/sh -e
. ./settings.sh
OUTDIR=target # cross-compiled binaries end up here
OUTDIR_EXT2=${OUTDIR}/ext2 # used to generate ext2 image
OUTDIR_EFI=${OUTDIR}/efi # used to generate efi image
OUTDIR_IMAGES=${OUTDIR}/images # images end up here
MAKE_ARGS="-j$(nproc)"
status() {
YELLOW=$(tput setaf 3)
BOLD=$(tput bold)
NORM=$(tput sgr0)
echo "${BOLD}${YELLOW}$1${NORM}"
}
setup_toolchain() {
if [ ! -f "toolchain/bin/${TARGET}-gcc" ]; then
status "*** Toolchain not available - building it"
devenv/build-toolchain.sh
cp -R ${TOOLCHAIN} toolchain
fi
if [ ! -f "${TOOLCHAIN}/bin/${TARGET}-gcc" ]; then
status "*** Copying toolchain"
cp -R toolchain/* ${TOOLCHAIN}
fi
if [ ! -f "${MESON_TOOLCHAIN_FILE}" ]; then
status "*** Setting up Meson toolchain file"
echo "[constants]
toolchain = '${TOOLCHAIN}'
arch = '${TARGET}'
common_flags = ['--sysroot=' + toolchain]
[binaries]
c = toolchain / 'bin' / arch + '-gcc'
cpp = toolchain / 'bin' / arch + '-g++'
ar = toolchain / 'bin' / arch + '-ar'
strip = toolchain / 'bin' / arch + '-strip'
[host_machine]
system = 'dogfood'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'
" >>${MESON_TOOLCHAIN_FILE}
fi
}
setup_meson() {
# Maybe this should be part of the toolchain instead; for now just leave it
# here, in case we really need the local Meson version
MESON=${TOOLCHAIN}/bin/meson
if [ ! -f "${MESON}" ]; then
status "*** Creating Meson zipapp (toolchain/target)"
cd userland/meson-${MESON_VERSION}
packaging/create_zipapp.py --outfile ${MESON} --interpreter "/usr/bin/env python3" .
mkdir -p ${OUTDIR_EXT2}/usr/bin
packaging/create_zipapp.py --outfile ${OUTDIR_EXT2}/usr/bin/meson --interpreter "/usr/bin/python3" .
cd ../..
fi
}
do_headers() {
status "*** Installing kernel headers (sysroot/target)"
cd kernel-headers
if [ ! -d build/headers-sysroot ]; then
${MESON} setup ../build/headers-sysroot --cross-file ${MESON_TOOLCHAIN_FILE}
fi
${MESON} compile -C ../build/headers-sysroot
${MESON} install -C ../build/headers-sysroot --destdir ${SYSROOT}
${MESON} install -C ../build/headers-target --destdir ${OUTDIR_EXT2}
cd ..
}
do_target_sysroot() {
status "*** Building newlib (sysroot/target)"
cd lib/newlib-3.1.0
if [ ! -d ../../build/newlib ]; then
${MESON} setup ../../build/newlib --cross-file ${MESON_TOOLCHAIN_FILE}
fi
${MESON} compile -C ../../build/newlib
${MESON} install -C ../../build/newlib --destdir ${SYSROOT}
${MESON} install -C ../../build/newlib --destdir ${OUTDIR_EXT2}
cd ../..
if [ ! -f "${TOOLCHAIN}/lib/gcc/${TARGET}/${GCC_VERSION}/libgcc.a" ]; then
status "*** Building libgcc (sysroot)"
rm -rf build/gcc-libgcc
mkdir -p build/gcc-libgcc
cd build/gcc-libgcc
../../userland/gcc-${GCC_VERSION}/configure --target=${TARGET} --disable-nls --without-headers --enable-languages='c,c++' --prefix=${TOOLCHAIN} --disable-libstdcxx --disable-build-with-cxx --disable-libssp --disable-libquadmath --with-sysroot=${SYSROOT} --with-gxx-include-dir=${SYSROOT}/usr/include/c++/${GCC_VERSION}
make ${MAKE_ARGS} all-target-libgcc
make ${MAKE_ARGS} install-target-libgcc
cd ../..
fi
if [ ! -f "${SYSROOT}/usr/lib/libstdc++.a" ]; then
status "*** Building libstdcxx (sysroot)"
rm -rf build/libstdcxx
mkdir -p build/libstdcxx
cd build/libstdcxx
CFLAGS="--sysroot ${SYSROOT}" ../../userland/gcc-${GCC_VERSION}/libstdc++-v3/configure --host=${TARGET} --target=${TARGET} --prefix=${SYSROOT}/usr
make ${MAKE_ARGS}
make ${MAKE_ARGS} install
cd ../..
fi
}
do_kernel() {
status "*** Building kernel"
cd kernel
if [ ! -d ../build/kernel ]; then
mkdir -p ../build/kernel
${MESON} setup ../build/kernel --cross-file ${MESON_TOOLCHAIN_FILE}
fi
${MESON} compile -C ../build/kernel
${MESON} install -C ../build/kernel --destdir ${OUTDIR_EXT2}
cd ..
TARGET_DIRTY=1
}
do_loader() {
status "*** Building loader"
cd loader
if [ ! -d ../build/loader ]; then
mkdir -p ../build/loader
${MESON} setup ../build/loader --cross-file ${MESON_TOOLCHAIN_FILE}
fi
${MESON} compile -C ../build/loader
${MESON} install -C ../build/loader --destdir ${OUTDIR_EXT2}
mkdir -p ${OUTDIR_EFI}/efi/boot
cp ${OUTDIR_EXT2}/boot/loader.efi ${OUTDIR_EFI}/efi/boot/bootx64.efi
cd ..
LOADER_DIRTY=1
}
do_target_dash() {
if [ ! -f "${OUTDIR_EXT2}/bin/sh" ]; then
status "*** Building dash (target)"
cd userland/dash-0.5.10.2
make clean || true
CFLAGS="--sysroot ${SYSROOT} -DJOBS=0" ./configure --host=${TARGET} --prefix=${OUTDIR_EXT2}
make ${MAKE_ARGS}
make install
mv ${OUTDIR_EXT2}/bin/dash ${OUTDIR_EXT2}/bin/sh
cd ../..
TARGET_DIRTY=1
fi
}
do_target_coreutils() {
if [ ! -f "${OUTDIR_EXT2}/bin/ls" ]; then
status "*** Building coreutils (target)"
cd userland/coreutils-8.31
make clean || true
CFLAGS="--sysroot ${SYSROOT} -DOK_TO_USE_1S_CLOCK" ./configure --host=${TARGET} --prefix=${OUTDIR_EXT2}
make ${MAKE_ARGS}
make install
cd ../..
TARGET_DIRTY=1
fi
}
do_target_sysutils() {
status "*** Building sysutils (target)"
if [ ! -d build/sysutils ]; then
${MESON} setup build/sysutils sysutils --cross-file ${MESON_TOOLCHAIN_FILE}
fi
${MESON} install -C build/sysutils --destdir ${OUTDIR_EXT2}
TARGET_DIRTY=1
}
do_target_doom() {
status "*** Building doom (target)"
cd userland/doomgeneric
if [ ! -d ../../build/doom ]; then
${MESON} setup ../../build/doom --cross-file ${MESON_TOOLCHAIN_FILE}
fi
${MESON} compile -C../../build/doom
${MESON} install -C../../build/doom --destdir ${OUTDIR_EXT2}
cd ../..
TARGET_DIRTY=1
}
do_target_binutils() {
if [ ! -f "${OUTDIR_EXT2}/usr/bin/ld" ]; then
status "*** Building binutils (target)"
rm -rf build/binutils-target
mkdir -p build/binutils-target
cd build/binutils-target
CFLAGS="--sysroot ${SYSROOT}" ../../userland/binutils-${BINUTILS_VERSION}/configure --host=${TARGET} --target=${TARGET} --disable-nls --disable-werror --prefix=/usr
make ${MAKE_ARGS}
make ${MAKE_ARGS} install DESTDIR=${OUTDIR_EXT2}
cd ../..
TARGET_DIRTY=1
fi
}
do_sysroot_libs() {
if [ ! -f "${SYSROOT}/lib/libgmp.a" ]; then
status "*** Building gmp (sysroot)"
rm -rf build/gmp-sysroot
mkdir -p build/gmp-sysroot
cd build/gmp-sysroot
CFLAGS="--sysroot ${SYSROOT}" ../../userland/gmp-${GMP_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=${SYSROOT}
make ${MAKE_ARGS}
make ${MAKE_ARGS} install
cd ../..
TARGET_DIRTY=1
fi
if [ ! -f "${SYSROOT}/lib/libmpfr.a" ]; then
status "*** Building mpfr (sysroot)"
rm -rf build/mpfr-sysroot
mkdir -p build/mpfr-sysroot
cd build/mpfr-sysroot
CFLAGS="--sysroot ${SYSROOT}" ../../userland/mpfr-${MPFR_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=${SYSROOT} --with-gmp=${SYSROOT}
make ${MAKE_ARGS}
make ${MAKE_ARGS} install
cd ../..
TARGET_DIRTY=1
fi
if [ ! -f "${SYSROOT}/lib/libmpc.a" ]; then
status "*** Building mpc (target)"
rm -rf build/mpc-sysroot
mkdir -p build/mpc-sysroot
cd build/mpc-sysroot
CFLAGS="--sysroot ${SYSROOT}" ../../userland/mpc-${MPC_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=${SYSROOT} --with-gmp=${SYSROOT}
make ${MAKE_ARGS}
make ${MAKE_ARGS} install
cd ../..
TARGET_DIRTY=1
fi
if [ ! -f "${SYSROOT}/usr/lib/pkgconfig/zlib.pc" ]; then
status "*** Building zlib (sysroot)"
rm -rf build/zlib-sysroot
mkdir -p build/zlib-sysroot
cd build/zlib-sysroot
CC=${TARGET}-gcc CFLAGS="--sysroot ${SYSROOT}" ../../userland/zlib-${ZLIB_VERSION}/configure --prefix=/usr
make ${MAKE_ARGS}
make ${MAKE_ARGS} install DESTDIR=${SYSROOT}
cd ../..
TARGET_DIRTY=1
fi
if [ ! -f "${SYSROOT}/usr/lib/libncurses.a" ]; then
status "*** Building ncurses (sysroot)"
rm -rf build/ncurses-sysroot
mkdir -p build/ncurses-sysroot
cd build/ncurses-sysroot
CFLAGS="--sysroot ${SYSROOT}" ../../userland/ncurses-${NCURSES_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=${SYSROOT}/usr --disable-widec
make ${MAKE_ARGS}
make ${MAKE_ARGS} install
cd ../..
TARGET_DIRTY=1
fi
}
do_target_gcc() {
if [ ! -f "${OUTDIR_EXT2}/usr/bin/gcc" ]; then
status "*** Building gcc (target)"
rm -rf build/gcc-target
mkdir -p build/gcc-target
cd build/gcc-target
CFLAGS="--sysroot ${SYSROOT}" CXXFLAGS="--sysroot ${SYSROOT}" ../../userland/gcc-${GCC_VERSION}/configure --host=${TARGET} --target=${TARGET} --disable-nls --disable-werror --enable-languages='c,c++' --prefix=/usr --with-gmp=${SYSROOT}
# XXX for some reason, we need to explicitely configure/build the host
# libcpp; otherwise it inherits our sysroot which messes things up...
make ${MAKE_ARGS} configure-build-libcpp
(cd build-x86_64-pc-linux-gnu/libcpp && make ${MAKE_ARGS})
make ${MAKE_ARGS}
make ${MAKE_ARGS} install DESTDIR=${OUTDIR_EXT2}
cd ../..
TARGET_DIRTY=1
fi
}
do_target_make() {
if [ ! -f "${OUTDIR_EXT2}/usr/bin/make" ]; then
status "*** Building make (target)"
rm -rf build/make-target
mkdir -p build/make-target
cd build/make-target
CFLAGS="--sysroot ${SYSROOT}" ../../userland/make-${MAKE_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=/usr
make ${MAKE_ARGS}
make ${MAKE_ARGS} install DESTDIR=${OUTDIR_EXT2}
cd ../..
TARGET_DIRTY=1
fi
}
do_target_sed() {
if [ ! -f "${OUTDIR_EXT2}/usr/bin/sed" ]; then
status "*** Building sed (target)"
rm -rf build/sed-target
mkdir -p build/sed-target
cd build/sed-target
CFLAGS="--sysroot ${SYSROOT}" ../../userland/sed-${SED_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=/usr
make ${MAKE_ARGS}
make ${MAKE_ARGS} install DESTDIR=${OUTDIR_EXT2}
cd ../..
TARGET_DIRTY=1
fi
}
do_target_python() {
if [ ! -f "${OUTDIR_EXT2}/usr/bin/python3" ]; then
status "*** Building python (target)"
rm -rf build/python
mkdir -p build/python
cd build/python
PKG_CONFIG_PATH=${SYSROOT}/usr/lib/pkgconfig PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig CONFIG_SITE=../../userland/Python-${PYTHON_VERSION}/config.site.dogfood CFLAGS="--sysroot ${SYSROOT}" ../../userland/Python-${PYTHON_VERSION}/configure --build=x86_64-pc-linux-gnu --host=${TARGET} --target=${TARGET} --prefix=/usr --with-build-python=python3 --disable-ipv6
make ${MAKE_ARGS}
make ${MAKE_ARGS} install DESTDIR=${OUTDIR_EXT2}
cd ../..
TARGET_DIRTY=1
fi
}
do_target_ninja() {
if [ ! -f "${OUTDIR_EXT2}/usr/bin/ninja" ]; then
status "*** Building ninja (target)"
rm -rf build/ninja
mkdir -p build/ninja
cd build/ninja
CXX=${TARGET}-g++ CXXFLAGS="--sysroot ${SYSROOT}" ../../userland/ninja-${NINJA_VERSION}/configure.py --force-pselect
ninja
cp ninja ${OUTDIR_EXT2}/usr/bin
cd ../..
fi
}
create_run_sh() {
if [ ! -f "${OUTDIR_IMAGES}/run.sh" ]; then
echo "qemu-system-x86_64 \\" >${OUTDIR_IMAGES}/run.sh
echo " -bios /usr/share/qemu/OVMF.fd \\" >>${OUTDIR_IMAGES}/run.sh
# virtio-block
echo " -drive format=raw,file=dogfood.img,if=virtio,media=disk \\" >>${OUTDIR_IMAGES}/run.sh
# virtio-console
echo " -device virtio-serial-pci,id=virtio-serial0 \\" >>${OUTDIR_IMAGES}/run.sh
#echo " -chardev socket,server=on,wait=off,host=127.0.0.1,port=9999,id=charconsole0 \\">>${OUTDIR_IMAGES}/run.sh
echo " -chardev stdio,id=charconsole0 \\" >>${OUTDIR_IMAGES}/run.sh
echo " -device virtconsole,chardev=charconsole0,id=console0 \\" >>${OUTDIR_IMAGES}/run.sh
echo " \$@" >>${OUTDIR_IMAGES}/run.sh
chmod 755 ${OUTDIR_IMAGES}/run.sh
fi
}
# parse options
CLEAN_TARGET=0
BUILD_KERNEL=0
while [ "$1" != "" ]; do
P="$1"
case "$P" in
-h)
echo "usage: build.sh [-hc] [-k]"
echo ""
echo " -h this help"
echo " -c clean target"
echo ""
echo " -k (re)build kernel"
exit 1
;;
-c)
CLEAN_TARGET=1
;;
-k)
BUILD_KERNEL=1
;;
*)
echo "unexpected parameter '$P'; use '-h' for help"
exit 1
;;
esac
shift
done
# clean
if [ "$CLEAN_TARGET" -ne 0 ]; then
rm -rf ${OUTDIR}
fi
mkdir -p ${TOOLCHAIN}
mkdir -p ${OUTDIR_EXT2}
mkdir -p ${OUTDIR_EFI}
mkdir -p ${OUTDIR_IMAGES}
TOOLCHAIN=$(realpath ${TOOLCHAIN})
OUTDIR_EXT2=$(realpath ${OUTDIR_EXT2})
OUTDIR_EFI=$(realpath ${OUTDIR_EFI})
TARGET_DIRTY=0
LOADER_DIRTY=0
mkdir -p build
MESON_TOOLCHAIN_FILE=${TOOLCHAIN}/meson.txt
setup_toolchain
# toolchain is available; make sure we can find it
export PATH=${TOOLCHAIN}/bin:${PATH}
setup_meson
do_headers
do_target_sysroot
do_kernel
do_loader
do_target_dash
do_target_coreutils
do_target_sysutils
do_target_binutils
do_sysroot_libs
do_target_gcc
do_target_make
do_target_sed
do_target_python
do_target_ninja
#if [ "$BUILD_TARGET" -ne "0" -o ! -f "${OUTDIR_EXT2}/usr/bin/gdb" ]; then
# echo "*** Building gdb (target)"
# rm -rf build/gdb-target
# mkdir -p build/gdb-target
# cd build/gdb-target
# CFLAGS="--sysroot ${SYSROOT}" ../../userland/gdb-${GDB_VERSION}/configure --host=${TARGET} --target=${TARGET} --prefix=/usr --with-libgmp-prefix=${SYSROOT}
# CFLAGS="--sysroot /tmp/dogfood-sysroot" ../../userland/gdb-12.1/configure --host=x86_64-elf-dogfood --target=x86_64-elf-dogfood --prefix=/usr --with-libgmp-prefix=/tmp/dogfood-sysroot
# make ${MAKE_ARGS}
# make ${MAKE_ARGS} install DESTDIR=${OUTDIR_EXT2}
# cd ../..
# TARGET_DIRTY=1
#fi
#if [ "$BUILD_TARGET" -ne "0" -o ! -f "${OUTDIR_EXT2}/usr/bin/vi" ]; then
# echo "*** Building openvi (target)"
# rm -rf build/openvi
# mkdir -p build/openvi
# cd userland/OpenVi-${OPENVI_VERSION}
# ${MESON} setup ../../build/openvi --cross-file ${MESON_TOOLCHAIN_FILE}
# ${MESON} compile -C../../build/openvi
# ${MESON} install -C../../build/openvi --destdir ${OUTDIR_EXT2}
# cd ../..
# TARGET_DIRTY=1
#fi
if [ ! -f "${OUTDIR_IMAGES}/dogfood.img" ]; then
echo "*** Creating Dogfood image..."
dd if=/dev/zero of=${OUTDIR_IMAGES}/dogfood.img bs=1M count=2080
# new gpt schema, make 2 partition, first is 32MB, change first partition to EFI system
printf 'g\nn\n1\n2048\n+32M\nn\n2\n\n\nt\n1\n1\nw\n' | /sbin/fdisk ${OUTDIR_IMAGES}/dogfood.img
# ensure we update the loader/target
LOADER_DIRTY=1
TARGET_DIRTY=1
fi
if [ "$TARGET_DIRTY" -ne "0" -o ! -f "${OUTDIR_IMAGES}/ext2.img" ]; then
echo "*** Creating ext2 image..."
EXT2_SECTOR=$(/sbin/fdisk -l ${OUTDIR_IMAGES}/dogfood.img | grep "Linux filesystem" | awk '{print $2*512}')
rm -f ${OUTDIR_IMAGES}/ext2.img
mkfs.ext2 -E offset=${EXT2_SECTOR} -d ${OUTDIR_EXT2} ${OUTDIR_IMAGES}/dogfood.img 2g
fi
if [ "$LOADER_DIRTY" -ne "0" -o ! -f "${OUTDIR_IMAGES}/efi.img" ]; then
echo "*** Creating EFI image..."
EFI_IMAGE=${OUTDIR_IMAGES}/efi.img
dd if=/dev/zero of=${EFI_IMAGE} bs=1M count=32
mformat -i ${EFI_IMAGE} ::/
mcopy -i ${EFI_IMAGE} -s ${OUTDIR_EFI}/* ::/
# copy EFI content to image
EFI_SECTOR=$(/sbin/fdisk -l ${OUTDIR_IMAGES}/dogfood.img | grep "EFI System" | awk '{print $2}')
dd if=${OUTDIR_IMAGES}/efi.img of=${OUTDIR_IMAGES}/dogfood.img bs=512 seek=${EFI_SECTOR} conv=notrunc
fi
create_run_sh