BuildSystem: Rewrite whole build system structure

Now you have to use script/build.sh for building and running banan-os
This commit is contained in:
2023-10-23 13:27:23 +03:00
parent f3d9da9549
commit ce87e0a605
14 changed files with 320 additions and 201 deletions

28
script/bochs.sh Executable file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
set -e
BOCHS_CONFIG_FILE=bochsrc
COM1_TERMINAL=kitty
COM1_DEVICE_FILE=com1-term-device
$COM1_TERMINAL sh -c "tty > $COM1_DEVICE_FILE && sleep infinity" &
COM1_TERM_PID=$(echo $!)
sleep 1
COM1_DEVICE=$(cat $COM1_DEVICE_FILE)
rm $COM1_DEVICE_FILE
cat > $BOCHS_CONFIG_FILE << EOF
ata0-master: type=disk, path=$BANAN_DISK_IMAGE_PATH, status=inserted
boot: disk
clock: sync=realtime, time0=local
display_library: x, options="gui_debug"
magic_break: enabled=1
megs: 128
com1: enabled=1, mode=term, dev=$COM1_DEVICE
EOF
bochs -qf $BOCHS_CONFIG_FILE
kill $COM1_TERM_PID
rm $BOCHS_CONFIG_FILE

107
script/build.sh Executable file
View File

@@ -0,0 +1,107 @@
#!/bin/bash
set -e
if [[ -z $BANAN_ARCH ]]; then
export BANAN_ARCH=x86_64
fi
export BANAN_SCRIPT_DIR=$(dirname $(realpath $0))
source $BANAN_SCRIPT_DIR/config.sh
make_build_dir () {
if ! [[ -d $BANAN_BUILD_DIR ]]; then
mkdir -p $BANAN_BUILD_DIR
cd $BANAN_BUILD_DIR
cmake --toolchain=$BANAN_TOOLCHAIN_DIR/Toolchain.txt -G Ninja $BANAN_ROOT_DIR $BANAN_CMAKE_ARGS
fi
}
build_target () {
make_build_dir
if [[ -z $1 ]]; then
echo "No target provided"
exit 1
fi
cd $BANAN_BUILD_DIR
ninja $1
}
build_toolchain () {
$BANAN_TOOLCHAIN_DIR/build.sh
build_target libc-install
$BANAN_TOOLCHAIN_DIR/build.sh libstdc++
}
create_image () {
build_target install-sysroot
if [[ "$1" == "full" ]]; then
$BANAN_SCRIPT_DIR/image-full.sh
else
$BANAN_SCRIPT_DIR/image.sh
fi
}
run_qemu () {
create_image
$BANAN_SCRIPT_DIR/qemu.sh $@
}
run_bochs () {
create_image
$BANAN_SCRIPT_DIR/bochs.sh $@
}
if [[ "$1" == "toolchain" ]]; then
if [[ -f $BANAN_TOOLCHAIN_PREFIX/bin/$BANAN_TOOLCHAIN_TRIPLE_PREFIX-gcc ]]; then
echo "You already seem to have build toolchain."
read -e -p "Do you want to rebuild it [y/N]? " choice
if ! [[ "$choice" == [Yy]* ]]; then
echo "Aborting toolchain rebuild"
exit 0
fi
fi
build_toolchain
exit 0
fi
if [[ "$1" == "image" ]]; then
create_image
exit 0
fi
if [[ "$1" == "image-full" ]]; then
create_image full
exit 0
fi
if [[ "$(uname)" == "Linux" ]]; then
QEMU_ACCEL="-accel kvm"
fi
if [[ "$1" == "qemu" ]]; then
run_qemu -serial stdio $QEMU_ACCEL
exit 0
fi
if [[ "$1" == "qemu-nographic" ]]; then
run_qemu -nographic $QEMU_ACCEL
exit 0
fi
if [[ "$1" == "qemu-debug" ]]; then
run_qemu -serial stdio -d int -no-reboot
exit 0
fi
if [[ "$1" == "bochs" ]]; then
run_bochs
exit 0
fi
if [[ "$1" == "check-fs" ]]; then
$BANAN_SCRIPT_DIR/check-fs.sh
exit 0
fi
build_target $1

14
script/check-fs.sh Executable file
View File

@@ -0,0 +1,14 @@
#!/bin/bash
set -e
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
echo "You must set BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
LOOP_DEV=$(sudo losetup -f --show $BANAN_DISK_IMAGE_PATH)
sudo partprobe $LOOP_DEV
sudo fsck.ext2 -fn ${LOOP_DEV}p2 || true
sudo losetup -d $LOOP_DEV

27
script/config.sh Normal file
View File

@@ -0,0 +1,27 @@
if [[ -z $BANAN_ROOT_DIR ]]; then
if [[ -z $BANAN_SCRIPT_DIR ]]; then
export BANAN_ROOT_DIR=$BANAN_SCRIPT_DIR/..
else
echo "You must set the BANAN_ROOT_DIR environment variable" >&2
exit 1
fi
fi
if [[ -z $BANAN_ARCH ]]; then
echo "You must set the BANAN_ARCH environment variable" >&2
exit 1
fi
export BANAN_TOOLCHAIN_DIR=$BANAN_ROOT_DIR/toolchain
export BANAN_TOOLCHAIN_PREFIX=$BANAN_TOOLCHAIN_DIR/local
export BANAN_TOOLCHAIN_TRIPLE_PREFIX=$BANAN_ARCH-banan_os
export BANAN_BUILD_DIR=$BANAN_ROOT_DIR/build
export BANAN_SYSROOT=$BANAN_BUILD_DIR/sysroot
export BANAN_DISK_IMAGE_PATH=$BANAN_BUILD_DIR/banan-os.img
if [[ -z $BANAN_UEFI_BOOT ]]; then
export BANAN_UEFI_BOOT=0
fi

97
script/image-full.sh Executable file
View File

@@ -0,0 +1,97 @@
#!/bin/bash
set -e
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
echo "You must set the BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_SYSROOT ]]; then
echo "You must set the BANAN_SYSROOT environment variable" >&2
exit 1
fi
if [[ -z $BANAN_TOOLCHAIN_PREFIX ]]; then
echo "You must set the BANAN_TOOLCHAIN_PREFIX environment variable" >&2
exit 1
fi
if [[ -z $BANAN_ARCH ]]; then
echo "You must set the BANAN_ARCH environment variable" >&2
exit 1
fi
DISK_SIZE=$[50 * 1024 * 1024]
MOUNT_DIR=/mnt
truncate -s 0 "$BANAN_DISK_IMAGE_PATH"
truncate -s $DISK_SIZE "$BANAN_DISK_IMAGE_PATH"
if [ "$BANAN_UEFI_BOOT" == "1" ]; then
sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk "$BANAN_DISK_IMAGE_PATH" > /dev/null
g # gpt
n # new partition
1 # partition number 1
# default (from the beginning of the disk)
+16M # efi system size
n # new partition
2 # partition number 2
# default (right after efi system partition)
# default (to the end of disk)
t # set type
1 # ... of partition 1
1 # efi system
t # set type
2 # ... of partition 2
20 # Linux filesystem
w # write changes
EOF
else
sed -e 's/\s*\([-\+[:alnum:]]*\).*/\1/' << EOF | fdisk "$BANAN_DISK_IMAGE_PATH" > /dev/null
g # gpt
n # new partition
1 # partition number 1
# default (from the beginning of the disk)
+1M # bios boot partition size
n # new partition
2 # partition number 2
# default (right after bios partition)
# default (to the end of disk)
t # set type
1 # ... of partition 1
4 # bios boot partition
t # set type
2 # ... of partition 2
20 # Linux filesystem
w # write changes
EOF
fi
LOOP_DEV=$(sudo losetup -f --show "$BANAN_DISK_IMAGE_PATH")
sudo partprobe $LOOP_DEV
PARTITION1=${LOOP_DEV}p1
PARTITION2=${LOOP_DEV}p2
sudo mkfs.ext2 -d $BANAN_SYSROOT -b 1024 -q $PARTITION2
if [[ "$BANAN_UEFI_BOOT" == "1" ]]; then
sudo mkfs.fat $PARTITION1 > /dev/null
sudo mount $PARTITION1 "$MOUNT_DIR"
sudo mkdir -p "$MOUNT_DIR/EFI/BOOT"
sudo "$BANAN_TOOLCHAIN_PREFIX/bin/grub-mkstandalone" -O "$BANAN_ARCH-efi" -o "$MOUNT_DIR/EFI/BOOT/BOOTX64.EFI" "boot/grub/grub.cfg=$BANAN_TOOLCHAIN_PREFIX/grub-memdisk.cfg"
sudo umount "$MOUNT_DIR"
sudo mount $PARTITION2 "$MOUNT_DIR"
sudo mkdir -p "$MOUNT_DIR/boot/grub"
sudo cp "$BANAN_TOOLCHAIN_PREFIX/grub-uefi.cfg" "$MOUNT_DIR/boot/grub/grub.cfg"
sudo umount "$MOUNT_DIR"
else
sudo mount $PARTITION2 "$MOUNT_DIR"
sudo grub-install --no-floppy --target=i386-pc --modules="normal ext2 multiboot" --boot-directory="$MOUNT_DIR/boot" $LOOP_DEV
sudo mkdir -p "$MOUNT_DIR/boot/grub"
sudo cp "$BANAN_TOOLCHAIN_PREFIX/grub-legacy-boot.cfg" "$MOUNT_DIR/boot/grub/grub.cfg"
sudo umount "$MOUNT_DIR"
fi
sudo losetup -d $LOOP_DEV

45
script/image.sh Executable file
View File

@@ -0,0 +1,45 @@
#!/bin/bash
if [[ -z $BANAN_ROOT_DIR ]]; then
echo "You must set the BANAN_ROOT_DIR environment variable" >&2
exit 1
fi
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
echo "You must set the BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
if [[ -z $BANAN_SYSROOT ]]; then
echo "You must set the BANAN_SYSROOT environment variable" >&2
exit 1
fi
if [[ ! -f $BANAN_DISK_IMAGE_PATH ]]; then
$BANAN_SCRIPT_DIR/image-full.sh
exit 0
fi
fdisk -l $BANAN_DISK_IMAGE_PATH | grep -q 'EFI System'; IMAGE_IS_UEFI=$?
[[ $BANAN_UEFI_BOOT == 1 ]]; CREATE_IS_UEFI=$?
if [[ $IMAGE_IS_UEFI -ne $CREATE_IS_UEFI ]]; then
echo Converting disk image to/from UEFI
$BANAN_SCRIPT_DIR/image-full.sh
exit 0
fi
MOUNT_DIR=/mnt
LOOP_DEV=$(sudo losetup -f --show $BANAN_DISK_IMAGE_PATH)
sudo partprobe $LOOP_DEV
ROOT_PARTITON=${LOOP_DEV}p2
sudo mount $ROOT_PARTITON $MOUNT_DIR
sudo rsync -a ${BANAN_SYSROOT}/* ${MOUNT_DIR}/
sudo umount $MOUNT_DIR
sudo losetup -d $LOOP_DEV

24
script/qemu.sh Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
set -e
if [[ -z $BANAN_DISK_IMAGE_PATH ]]; then
echo "You must set the BANAN_DISK_IMAGE_PATH environment variable" >&2
exit 1
fi
if [[ -z $OVMF_PATH ]]; then
OVMF_PATH="/usr/share/ovmf/x64/OVMF.fd"
fi
if [[ "$BANAN_UEFI_BOOT" == "1" ]]; then
BIOS_ARGS="-bios $OVMF_PATH -net none"
fi
qemu-system-$BANAN_ARCH \
-m 128 \
-smp 2 \
$BIOS_ARGS \
-drive format=raw,id=disk,file=${BANAN_DISK_IMAGE_PATH},if=none \
-device ahci,id=ahci \
-device ide-hd,drive=disk,bus=ahci.0 \
$@ \