|
| 1 | +C_SOURCES = $(wildcard kernel/*.c drivers/*.c cpu/*.c libc/*.c) |
| 2 | +HEADERS = $(wildcard kernel/*.h drivers/*.h cpu/*.h libc/*.h) |
| 3 | +# Nice syntax for file extension replacement |
| 4 | +OBJ = ${C_SOURCES:.c=.o cpu/interrupt.o} |
| 5 | + |
| 6 | +# Change this if your cross-compiler is somewhere else |
| 7 | +CC = /usr/local/i386elfgcc/bin/i386-elf-gcc |
| 8 | +GDB = /usr/local/i386elfgcc/bin/i386-elf-gdb |
| 9 | +# -g: Use debugging symbols in gcc |
| 10 | +CFLAGS = -g -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs \ |
| 11 | + -Wall -Wextra -Werror |
| 12 | + |
| 13 | +# First rule is run by default |
| 14 | +os-image.bin: boot/bootsect.bin kernel.bin |
| 15 | + cat $^ > os-image.bin |
| 16 | + |
| 17 | +# '--oformat binary' deletes all symbols as a collateral, so we don't need |
| 18 | +# to 'strip' them manually on this case |
| 19 | +kernel.bin: boot/kernel_entry.o ${OBJ} |
| 20 | + i386-elf-ld -o $@ -Ttext 0x1000 $^ --oformat binary |
| 21 | + |
| 22 | +# Used for debugging purposes |
| 23 | +kernel.elf: boot/kernel_entry.o ${OBJ} |
| 24 | + i386-elf-ld -o $@ -Ttext 0x1000 $^ |
| 25 | + |
| 26 | +run: os-image.bin |
| 27 | + qemu-system-i386 -fda os-image.bin |
| 28 | + |
| 29 | +# Open the connection to qemu and load our kernel-object file with symbols |
| 30 | +debug: os-image.bin kernel.elf |
| 31 | + qemu-system-i386 -s -fda os-image.bin -d guest_errors,int & |
| 32 | + ${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf" |
| 33 | + |
| 34 | +# Generic rules for wildcards |
| 35 | +# To make an object, always compile from its .c |
| 36 | +%.o: %.c ${HEADERS} |
| 37 | + ${CC} ${CFLAGS} -ffreestanding -c $< -o $@ |
| 38 | + |
| 39 | +%.o: %.asm |
| 40 | + nasm $< -f elf -o $@ |
| 41 | + |
| 42 | +%.bin: %.asm |
| 43 | + nasm $< -f bin -o $@ |
| 44 | + |
| 45 | +clean: |
| 46 | + rm -rf *.bin *.dis *.o os-image.bin *.elf |
| 47 | + rm -rf kernel/*.o boot/*.bin drivers/*.o boot/*.o cpu/*.o libc/*.o |
0 commit comments