-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.sh
executable file
·303 lines (278 loc) · 8.93 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
# Default architecture and build type
ARCH=i386
BUILD_TYPE=Debug
TOOLCHAIN=llvm
if [[ $ARCH == "i386" ]]; then
ARCH_SUFFIX="-m32"
else
ARCH_SUFFIX="-m64"
fi
if [[ $TOOLCHAIN != "llvm" ]]; then
echo "There is a GCC build profile but it is totally untested. Use LLVM toolchain unless you want to try to make GCC work."
exit 1
fi
args=(${@,,})
if [[ ${args[@]} =~ "release" ]]; then
BUILD_TYPE=Release
elif [[ ${args[@]} =~ "reldbginfo" ]]; then
BUILD_TYPE=RelWithDebInfo
fi
if [[ ${args[@]} =~ "amd64" ]]; then
ARCH=amd64
fi
if [[ ${ARCH} == "i386" ]]; then
CLANG_ARCH=i686
SEL4_ARCH=ia32
RTLIB_ARCH=i386
elif [[ ${ARCH} == "amd64" ]]; then
CLANG_ARCH=x86_64
SEL4_ARCH=x86_64
RTLIB_ARCH=x86_64
else
echo "Unsupported arch ${ARCH}"
exit 1
fi
BUILDDIR="build-$ARCH-${BUILD_TYPE,,}"
IMAGEDIR="images-$ARCH-${BUILD_TYPE,,}"
function build_failed
{
echo "#################################"
echo " Build failed."
echo "#################################"
exit 1
}
echo
echo "####################################################"
echo " Building ${BUILD_TYPE} version for ${ARCH}"
echo "####################################################"
cd "$(dirname "$0")"
RTLIB=$(echo ${PWD}/compiler-rt/libclang_rt.builtins-${RTLIB_ARCH}.a)
mkdir -p $BUILDDIR/{host,elf,pe_inc,ntdll,wdm,base,drivers,initcpio,ndk_lib,ddk_lib,$IMAGEDIR}
cd $BUILDDIR
PE_INC=$(echo ${PWD}/pe_inc)
SPEC2DEF_PATH=$(echo ${PWD}/host/spec2def/spec2def)
UTF16LE_PATH=$(echo ${PWD}/host/utf16le/utf16le)
# Build spec2def with the native toolchain
cd host
echo
echo "---- Building native targets ----"
echo
cmake ../../tools -G Ninja
ninja
# Build ntos with the ELF toolchain
cd ../elf
echo
echo "---- Building ELF targets ----"
echo
cmake ../../private/ntos \
-DCLANG_ARCH=${CLANG_ARCH} \
-DTRIPLE=${CLANG_ARCH}-pc-linux-gnu \
-DCMAKE_TOOLCHAIN_FILE=../../sel4/${TOOLCHAIN}.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PWD}/../../sel4/libsel4/sel4_arch_include/${SEL4_ARCH}" \
-DSEL4_GENERATED_HEADERS_DIR=${PWD} \
-DSTRUCTURES_GEN_H_ORIG=${PWD}/kernel/generated/arch/object/structures_gen.h \
-DSTRUCTURES_GEN_DIR=${PWD} \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-DKernelSel4Arch=$SEL4_ARCH -G Ninja
ninja all-elf || build_failed
# For amd64 PE targets, since the ELF toolchain assumes sizeof(long) == 8
# and the PE toolchain assumes sizeof(long) == 4, we need to modify the
# libsel4 sel4_arch headers to undefine the macro SEL4_INT64_IS_LONG and
# define SEL4_INT64_IS_LONG_LONG. So far this seems to work and produce
# valid seL4 system calls. However we need to be very careful.
cd ../pe_inc
echo
echo "---- Building private PE targets ----"
echo
mkdir -p {kernel,libsel4}
cp ../elf/structures_gen.h . || build_failed
cp -r ../../sel4/libsel4/sel4_arch_include/$SEL4_ARCH sel4_arch_include || build_failed
cp -r ../elf/kernel/gen_config kernel || build_failed
for i in gen_config autoconf include arch_include sel4_arch_include; do
cp -r ../elf/libsel4/$i libsel4 || build_failed
done
if [[ $ARCH == "amd64" ]]; then
cat <<EOF > sel4_arch_include/sel4/sel4_arch/simple_types.h
#pragma once
#define SEL4_WORD_IS_UINT64
#define SEL4_INT64_IS_LONG_LONG
EOF
sed -i '/assert_size_correct(long/d' libsel4/include/interfaces/sel4_client.h || build_failed
sed -i '/assert_size_correct(seL4_X86_VMAttributes,/d' libsel4/include/interfaces/sel4_client.h || build_failed
fi
# Build ntdll.dll with the PE toolchain
cd ../ntdll
cmake ../../private/ntdll \
-DArch=${ARCH} \
-DCLANG_ARCH=${CLANG_ARCH} \
-DTRIPLE=${CLANG_ARCH}-pc-windows-msvc \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PE_INC}/sel4_arch_include" \
-DSEL4_GENERATED_HEADERS_DIR="${PE_INC}" \
-DSTRUCTURES_GEN_DIR="${PE_INC}" \
-DSPEC2DEF_PATH=${SPEC2DEF_PATH} \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGENINC_PATH=${PWD}/../host/geninc/geninc \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja || build_failed
cp ntdll.lib ../ndk_lib || build_failed
cp ntdllp.lib ../ndk_lib || build_failed
echo
# Build wdm.dll with the PE toolchain
cd ../wdm
cmake ../../private/wdm \
-DArch=${ARCH} \
-DCLANG_ARCH=${CLANG_ARCH} \
-DTRIPLE=${CLANG_ARCH}-pc-windows-msvc \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DSANITIZED_SEL4_ARCH_INCLUDE_DIR="${PE_INC}/sel4_arch_include" \
-DSEL4_GENERATED_HEADERS_DIR="${PE_INC}" \
-DSTRUCTURES_GEN_DIR="${PE_INC}" \
-DSPEC2DEF_PATH=${SPEC2DEF_PATH} \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGENINC_PATH=${PWD}/../host/geninc/geninc \
-DNDK_LIB_PATH=${PWD}/../ndk_lib \
-DGEN_INC_DIR=${PWD}/../ntdll \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja || build_failed
cp wdm.lib ../ddk_lib || build_failed
# Build drivers with the PE toolchain
cd ../drivers
echo
echo "---- Building drivers ----"
echo
cmake ../../drivers \
-DArch=${ARCH} \
-DCLANG_ARCH=${CLANG_ARCH} \
-DTRIPLE=${CLANG_ARCH}-pc-windows-msvc \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DNDK_LIB_PATH=${PWD}/../ndk_lib \
-DDDK_LIB_PATH=${PWD}/../ddk_lib \
-DSPEC2DEF_PATH=${SPEC2DEF_PATH} \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja || build_failed
# Build base NT clients with the PE toolchain
cd ../base
echo
echo "---- Building base NT clients ----"
echo
cmake ../../base \
-DCLANG_ARCH=${CLANG_ARCH} \
-DTRIPLE=${CLANG_ARCH}-pc-windows-msvc \
-DCMAKE_TOOLCHAIN_FILE=../../${TOOLCHAIN}-pe.cmake \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DNDK_LIB_PATH=${PWD}/../ndk_lib \
-DUTF16LE_PATH=${UTF16LE_PATH} \
-DGIT_HEAD_SHA_SHORT="$(git rev-parse --short HEAD)" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=1 \
-G Ninja
ninja || build_failed
# Build initcpio
echo
echo "---- Building INITCPIO ----"
echo
cd ../initcpio
if [[ ${CLANG_ARCH} == i686 ]]; then
ELF_TARGET=elf32-i386
ELF_ARCH=i386
LLD_TARGET=elf_i386
elif [[ ${CLANG_ARCH} == x86_64 ]]; then
ELF_TARGET=elf64-x86-64
ELF_ARCH=i386:x86-64
LLD_TARGET=elf_x86_64
else
echo "Unsupported architecture ${CLANG_ARCH}"
exit 1
fi
PE_COPY_LIST='ntdll/ntdll.dll wdm/wdm.dll'
BASE_COPY_LIST='smss/smss.exe ntcmd/ntcmd.exe'
DRIVER_COPY_LIST='base/null/null.sys base/beep/beep.sys base/pnp/pnp.sys
bus/acpi/acpi.sys bus/pci/pci.sys input/kbdclass/kbdclass.sys
input/i8042prt/i8042prt.sys storage/fdc/fdc.sys filesystems/fatfs/fatfs.sys'
for i in ${PE_COPY_LIST}; do
cp ../$i . || build_failed
done
for i in ${BASE_COPY_LIST}; do
cp ../base/$i . || build_failed
done
for i in ${DRIVER_COPY_LIST}; do
cp ../drivers/$i . || build_failed
done
{ for i in ${PE_COPY_LIST}; do echo $(basename $i); done } > image-list
{ for i in ${BASE_COPY_LIST}; do echo $(basename $i); done } >> image-list
{ for i in ${DRIVER_COPY_LIST}; do echo $(basename $i); done } >> image-list
cpio -H newc -o < image-list > initcpio || build_failed
llvm-objcopy -I binary -O ${ELF_TARGET} --binary-architecture ${ELF_ARCH} \
--rename-section .data=initcpio,CONTENTS,ALLOC,LOAD,READONLY,DATA \
initcpio initcpio.o
if [[ $? == 0 ]]; then
echo "Success."
else
build_failed
fi
# Link ntos and initcpio into final ntos image
echo
echo "---- Linking NTOS image ----"
echo
cd ../$IMAGEDIR
cp ../elf/kernel-$SEL4_ARCH-pc99 kernel || build_failed
if [[ ${BUILD_TYPE} == Release ]]; then
LLD_OPTIONS="--gc-sections -O 3"
else
LLD_OPTIONS=""
fi
ld.lld -m ${LLD_TARGET} ${LLD_OPTIONS} ${RTLIB} \
--allow-multiple-definition \
../elf/libntos.a \
../elf/rtl/librtl.a \
../initcpio/initcpio.o \
-T ../../private/ntos/ntos.lds \
-o ntos
if [[ $? == 0 ]]; then
echo "Success."
else
build_failed
fi
echo
echo "---- Stripping symbols for ${BUILD_TYPE} build ----"
echo
STRIP=llvm-strip
if [[ ${BUILD_TYPE} == Release ]]; then
cp kernel kernel-no-strip
cp ntos ntos-no-strip
$STRIP kernel && $STRIP ntos
if [[ $? == 0 ]]; then
echo "Success."
fi
else
$STRIP kernel -o kernel-stripped && $STRIP ntos -o ntos-stripped
if [[ $? == 0 ]]; then
echo "Success."
fi
fi
echo
echo "---- Merge compile_commands.json ----"
echo
if [[ $(which jq) ]]; then
jq -s add ../*/*.json > ../compile_commands.json
rm ../*/compile_commands.json
echo "Done."
else
echo "You'll need to install jq (https://jqlang.github.io/jq)."
fi
echo
echo "####################################"
echo " Build successful!"
echo "####################################"