switch build system to generated ninja file

This commit is contained in:
Benji Dial 2025-07-05 20:11:15 -04:00
parent c7ed7a2508
commit ce37be750d
5 changed files with 88 additions and 75 deletions

4
.gitignore vendored
View file

@ -1,2 +1,4 @@
build/
dependencies/ dependencies/
build.ninja
.ninja_log
build/

View file

@ -2,13 +2,8 @@
LIMINE_VERSION="9.3.4" LIMINE_VERSION="9.3.4"
if [ -e dependencies/.got-all ]; then
echo already got dependencies.
exit
fi
if [ -e dependencies ]; then if [ -e dependencies ]; then
echo it looks like we started getting the dependencies, and did not finish. delete the dependencies directory and run this again to try again. echo dependencies directory already exists.
exit exit
fi fi
@ -21,5 +16,3 @@ curl -L https://github.com/limine-bootloader/limine/archive/refs/tags/v${LIMINE_
mv limine-${LIMINE_VERSION}-binary limine mv limine-${LIMINE_VERSION}-binary limine
make -C limine make -C limine
cd .. cd ..
touch dependencies/.got-all

67
make-build.sh Normal file
View file

@ -0,0 +1,67 @@
#!/bin/sh
COMMON_CC_EXTRA_FLAGS="-O3 -Wall -Wextra"
COMMON_CC_FLAGS="-std=c23 ${COMMON_CC_EXTRA_FLAGS}"
KERNEL_CC_FLAGS="-I dependencies/limine -I kernel/include -mno-sse -ffreestanding ${COMMON_CC_FLAGS}"
if [ -e build.ninja ]; then
echo build.ninja already exists.
exit
fi
set -e
echo "rule nasm" >> build.ninja
echo " command = nasm -f elf64 \$in -o \$out" >> build.ninja
echo "rule kernel_cc" >> build.ninja
echo " depfile = \$out.d" >> build.ninja
echo " command = cc -c -MD -MF \$out.d ${KERNEL_CC_FLAGS} \$in -o \$out" >> build.ninja
echo "rule kernel_ld" >> build.ninja
echo " command = ld -T kernel/link.ld \$in -o \$out" >> build.ninja
ALL_KERNEL_OBJECTS=""
for f in kernel/src/*.asm; do
f=$(echo $f | sed -e 's/^kernel\/src\///')
echo "build build/kernel/$f.o: nasm kernel/src/$f" >> build.ninja
ALL_KERNEL_OBJECTS="${ALL_KERNEL_OBJECTS} build/kernel/$f.o"
done
for f in kernel/src/*.c; do
f=$(echo $f | sed -e 's/^kernel\/src\///')
echo "build build/kernel/$f.o: kernel_cc kernel/src/$f" >> build.ninja
ALL_KERNEL_OBJECTS="${ALL_KERNEL_OBJECTS} build/kernel/$f.o"
done
echo "build build/kernel/kernel.elf: kernel_ld ${ALL_KERNEL_OBJECTS}" >> build.ninja
echo "rule initfs" >> build.ninja
echo " command =" \
"rm -rf build/initfs &&" \
"mkdir -p build/initfs/resx &&" \
"echo Hello! > build/initfs/resx/hello.txt &&" \
"cd build/initfs &&" \
"tar cf ../initfs.tar *" >> build.ninja
echo "build build/initfs.tar: initfs" >> build.ninja
echo "rule disk" >> build.ninja
echo " command =" \
"rm -rf build/disk &&" \
"cp -r disk build/disk &&" \
"cp dependencies/limine/limine-uefi-cd.bin build/disk/limine/ &&" \
"cp dependencies/limine/limine-bios-cd.bin build/disk/limine/ &&" \
"cp dependencies/limine/limine-bios.sys build/disk/limine/ &&"\
"mkdir -p build/disk/EFI/BOOT &&" \
"cp dependencies/limine/BOOTX64.EFI build/disk/EFI/BOOT/ &&" \
"mkdir build/disk/calcite &&" \
"cp build/kernel/kernel.elf build/disk/calcite/ &&" \
"cp build/initfs.tar build/disk/calcite/ &&" \
"xorriso -as mkisofs -R -r -J -b limine/limine-bios-cd.bin -no-emul-boot -boot-load-size 4 -boot-info-table -hfsplus -apm-block-size 2048 --efi-boot limine/limine-uefi-cd.bin -efi-boot-part --efi-boot-image --protective-msdos-label build/disk -o build/disk.iso &&" \
"dependencies/limine/limine bios-install build/disk.iso" >> build.ninja
echo "build build/disk.iso: disk | build/kernel/kernel.elf build/initfs.tar" >> build.ninja
echo "default build/disk.iso" >> build.ninja

View file

@ -1,57 +0,0 @@
COMMON_CC_EXTRA_FLAGS = -O3 -Wall -Wextra
COMMON_CC_FLAGS = -std=c23 ${CC_EXTRA_FLAGS}
KERNEL_CC_FLAGS = -mno-sse -ffreestanding ${COMMON_CC_FLAGS}
.PHONY: default
default: build/disk.iso
.PHONY: debug
debug: build/disk.iso
gdb -x qemu.gdb
.PHONY: clean
clean:
rm -rf build
# kernel
KERNEL_SOURCES = entry.c initfs.c paging.asm paging.c
build/kernel/%.asm.o: kernel/src/%.asm
@mkdir -p ${@D}
nasm -f elf64 $^ -o $@
build/kernel/%.c.o: kernel/src/%.c
@mkdir -p ${@D}
cc -c ${KERNEL_CC_FLAGS} -I dependencies/limine -I kernel/include $^ -o $@
build/kernel/kernel.elf: ${KERNEL_SOURCES:%=build/kernel/%.o}
ld -T kernel/link.ld $^ -o $@
# initfs
build/initfs.tar:
rm -rf build/initfs
mkdir -p build/initfs/resx
echo Hello! > build/initfs/resx/hello.txt
cd build/initfs; tar cf ../initfs.tar *
# disk
build/disk.iso: build/kernel/kernel.elf build/initfs.tar
rm -rf build/disk
cp -r disk build/disk
cp dependencies/limine/limine-uefi-cd.bin build/disk/limine/
cp dependencies/limine/limine-bios-cd.bin build/disk/limine/
cp dependencies/limine/limine-bios.sys build/disk/limine/
mkdir -p build/disk/EFI/BOOT
cp dependencies/limine/BOOTX64.EFI build/disk/EFI/BOOT/
mkdir build/disk/calcite
cp build/kernel/kernel.elf build/disk/calcite/
cp build/initfs.tar build/disk/calcite/
xorriso \
-as mkisofs -R -r -J -b limine/limine-bios-cd.bin -no-emul-boot \
-boot-load-size 4 -boot-info-table -hfsplus -apm-block-size 2048 \
--efi-boot limine/limine-uefi-cd.bin -efi-boot-part --efi-boot-image \
--protective-msdos-label build/disk -o build/disk.iso
dependencies/limine/limine bios-install build/disk.iso

View file

@ -15,17 +15,21 @@ Calcite requires some software to be installed before it can be built:
* curl * curl
* GCC (Any C compiler supporting both C99 and C23 should work.) * GCC (Any C compiler supporting both C99 and C23 should work.)
* GNU Binutils (Specifically, "ld" is used to link the kernel.) * GNU Binutils (Specifically, "ld" is used to link the kernel.)
* GNU Make (I don't think I have used any GNU extensions.)
* GNU tar (Any POSIX tar should be fine.) * GNU tar (Any POSIX tar should be fine.)
* GNU xorriso * GNU xorriso
* NASM * NASM
* Ninja build system
On Debian, it is sufficient to run this command: On Debian, it is sufficient to run this command:
apt install binutils curl gcc make nasm tar xorriso apt install binutils curl gcc nasm ninja-build tar xorriso
To build Calcite, first run "sh get-dependencies.sh", then run "make". To build Calcite, run these commands:
This will build a disk image at "build/disk.iso" that can be booted sh get-dependencies.sh
with either BIOS or UEFI. sh make-build.sh
ninja
This will build a disk image at "build/disk.iso" that can be booted with
either BIOS or UEFI.
=== Debugging === Debugging
@ -36,12 +40,16 @@ Once Calcite has been built, some more software is required to debug it:
On Debian, it is sufficient to run this command: On Debian, it is sufficient to run this command:
apt install gdb qemu-system-x86 apt install gdb qemu-system-x86
Then, just run "make debug". Calcite will be booted in QEMU, and GDB will be Then, just run "gdb -x qemu.gdb". Calcite will be booted in QEMU, and GDB will
attached to it. Use "c" or "continue" in GDB to start execution. be attached to it. Use "c" or "continue" in GDB to start execution.
While debugging, it is useful to disable optimizations and enable debugging While debugging, it is useful to disable optimizations and enable debugging
information. In the makefile, under CC_EXTRA_FLAGS, you can change "-O3" to information. In "make-build.sh", under COMMON_CC_EXTRA_FLAGS, you can change
"-Og -ggdb". Then, run "make clean" and then "make" again. "-O3" to "-Og -ggdb". Then, run these commands to rebuild:
rm -r build
rm build.ninja
sh make-build.sh
ninja
=== License === License