-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.sh
executable file
·215 lines (185 loc) · 5.11 KB
/
make.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
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
#
# See README.md and LICENSE.txt for license details.
#
# Copyright (C) 2024 Isak Evaldsson
#
# make.sh: Project Islay OS build script
#
# Resposilble for configuring the build environment as well as calling the
# makefiles with the subfolders.
set -e
# All subdirs including makefiles
PROJECTS="kernel initrd"
# All subdirs containging system headers
SYSTEM_HEADER_PROJECTS="kernel"
# Options configured by the input parameters
USE_GDB=false
ARCH=i386
COMPILER_PREFIX=i686-elf # Depends on arch
# Display script help text
function help() {
echo "Usage: $0 command [args]"
echo "Commands [supported args]:"
echo " clean: Calls make clean on all sub-projects, useful when needing to"
echo " do a full re-build."
echo ""
echo " build --arch <arch>: Builds the kernel/OS by calling make build on"
echo " all subprojects."
echo ""
echo " qemu --arch <arch> --gdb: Build and run kernel i qemu."
echo ""
echo "Args:"
echo " --arch <arch>: Target architecutre, uses $ARCH as default"
echo " --gdb|-g: attch qemu to gdb"
}
# clean(): clean everything to force a full re-build
function clean() {
echo "Calling make clean on all sub-projects..."
for PROJECT in $PROJECTS; do
(cd $PROJECT && DESTDIR="$SYSROOT" $MAKE clean)
done
rm -rf sysroot
rm -rf isodir
rm -rf islayos.iso
}
function config() {
echo "Configuring environment for the $ARCH / $COMPILER_PREFIX tool-chain"
# Export ARCH to called makefiles
export ARCH=$ARCH
# TODO: Stack smashing protector complains about the task switch code, so we disable it for know.
# The long term solution is to make sure that only c code is compiled with stack-smash protection
export CFLAGS='-Wextra -Wall -Og -g' #-fstack-protector-all'
export CPPFLAGS=''
# Define tool-chain
export AR=${COMPILER_PREFIX}-ar
export AS=${COMPILER_PREFIX}-as
export CC=${COMPILER_PREFIX}-gcc
export MAKE=${MAKE:-make}
# Do we need to export?, why are them even needed?
export PREFIX=/usr
export EXEC_PREFIX=$PREFIX
export BOOTDIR=/boot
export LIBDIR=$EXEC_PREFIX/lib
export INCLUDEDIR=$PREFIX/include
# Configure the cross-compiler to use the desired system root.
export SYSROOT="$(pwd)/sysroot"
export CC="$CC --sysroot=$SYSROOT"
# Work around that the -elf gcc targets doesn't have a system include directory
# because it was configured with --without-headers rather than --with-sysroot.
if echo "$COMPILER_PREFIX" | grep -Eq -- '-elf($|-)'; then
export CC="$CC -isystem=$INCLUDEDIR"
fi
}
function build_iso() {
echo "Building iso..."
# Install headers into sysroot
mkdir -p "$SYSROOT"
for PROJECT in $SYSTEM_HEADER_PROJECTS; do
(cd $PROJECT && DESTDIR="$SYSROOT" $MAKE install-headers)
done
# Build all the subprojects
for PROJECT in $PROJECTS; do
(cd $PROJECT && DESTDIR="$SYSROOT" $MAKE install)
done
# Merge them into an ISO
mkdir -p isodir
mkdir -p isodir/boot
mkdir -p isodir/boot/grub
cp -r sysroot/boot/. isodir/boot/.
cat > isodir/boot/grub/grub.cfg << EOF
menuentry "Project Islay" {
multiboot /boot/kernel.elf
module /boot/project_islay.initrd
}
EOF
grub-mkrescue -o islayos.iso isodir
}
# Starts the qemu session
function qemu() {
echo "Lanuching kernel in qemu..."
local BUILD="kernel/build"
local gdb_opts=(
"$BUILD/kernel.elf"
-ex 'target remote localhost:1234'
-ex 'layout src'
-ex 'layout regs'
-ex 'break *_start'
-ex 'continue'
)
local qemu_opts=(
-cdrom islayos.iso
-no-shutdown
-no-reboot
-d int
-D $BUILD/log.txt
-serial file:$BUILD/serial
)
if [ $USE_GDB = true ]; then
echo "Running with gdb..."
qemu-system-$ARCH "${qemu_opts[@]}" -S -s &
gdb "${gdb_opts[@]}"
else
qemu-system-$ARCH "${qemu_opts[@]}"
fi
}
if [ $# -lt 1 ]; then
help
exit 1
fi
# Store here since it will be shited out
cmd=$1
shift
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-a|--arch)
case $2 in
i386)
;&
x86)
;&
i686)
ARCH=i386
COMPILER_PREFIX=i686-elf
;;
*)
echo "error: unkown architecture '$2'"
exit 1
;;
esac
shift
shift
;;
-g|--gdb)
USE_GDB=true
shift
;;
*)
echo "error: unkown option '$1'"
echo ""
help
exit 1
esac
done
# Exectue commads
config # Always needed since some makefiles depends on varibles defined here
case $cmd in
clean)
clean
;;
build)
build_iso
;;
qemu)
build_iso
qemu
;;
*)
echo "error: unkown command '$cmd'"
echo ""
help
exit 1
;;
esac