-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
130 lines (103 loc) · 5.19 KB
/
Makefile
File metadata and controls
130 lines (103 loc) · 5.19 KB
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
# ──────────────────────────────────────────────────────────────
# notlinuxkernel — Makefile
# ──────────────────────────────────────────────────────────────
#
# Build toolchain overview (all 32-bit, freestanding, no stdlib):
#
# NASM — assembles boot/boot.asm → boot.o
# GCC (-m32) — compiles kernel/*.c → *.o (freestanding)
# LD — links all .o files using linker.ld → kernel binary
# grub-mkrescue — wraps the binary into a bootable ISO
# QEMU — boots the ISO in a virtual i386 PC
#
# ──────────────────────────────────────────────────────────────
# --- Tools ---------------------------------------------------------
ASM = nasm
CC = gcc
LD = ld
GRUB = grub-mkrescue
QEMU = qemu-system-i386
# --- Flags ---------------------------------------------------------
ASMFLAGS = -f elf32
# -m32 : generate 32-bit code
# -ffreestanding : no hosted environment (no libc)
# -fno-builtin : don't use GCC builtins that assume libc
# -nostdlib : don't link the standard library
# -Wall -Wextra : all warnings
CFLAGS = -m32 -std=gnu99 -ffreestanding -fno-builtin -nostdlib \
-Wall -Wextra -O2 -g
LDFLAGS = -m elf_i386 -T linker.ld
# --- Files ---------------------------------------------------------
ASM_SRC = boot/boot.asm
C_SRC = $(wildcard kernel/*.c)
ASM_OBJ = $(patsubst boot/%.asm, build/%.o, $(ASM_SRC))
C_OBJ = $(patsubst kernel/%.c, build/%.o, $(C_SRC))
OBJECTS = $(ASM_OBJ) $(C_OBJ)
KERNEL = build/notlinuxkernel.bin
ISO = build/notlinuxkernel.iso
# --- Targets -------------------------------------------------------
.PHONY: all clean run iso test-host test-qemu
all: $(ISO)
# Assemble boot stub
build/%.o: boot/%.asm | build
$(ASM) $(ASMFLAGS) $< -o $@
# Compile kernel C files
build/%.o: kernel/%.c | build
$(CC) $(CFLAGS) -c $< -o $@
# Link everything into a flat binary (ELF)
$(KERNEL): $(OBJECTS)
$(LD) $(LDFLAGS) -o $@ $^
# Build a bootable ISO with GRUB
$(ISO): $(KERNEL) grub/grub.cfg
@mkdir -p build/isodir/boot/grub
cp $(KERNEL) build/isodir/boot/notlinuxkernel.bin
cp grub/grub.cfg build/isodir/boot/grub/grub.cfg
$(GRUB) -o $@ build/isodir
# Boot in QEMU
run: $(ISO)
$(QEMU) -cdrom $(ISO)
build:
@mkdir -p build
clean:
rm -rf build
# ──────────────────────────────────────────────────────────────
# Host tests — compile natively, run on the host OS
# ──────────────────────────────────────────────────────────────
TEST_HOST_CFLAGS = -std=gnu99 -Wall -Wextra -O2 -g -DTEST_HOST -Ikernel -Itest
TEST_HOST_BIN = build/test_host
test-host: $(TEST_HOST_BIN)
@echo "=== Running host tests ==="
@./$(TEST_HOST_BIN)
$(TEST_HOST_BIN): test/test_host_main.c test/test_kprintf.c test/test_string.c test/test_vga.c \
kernel/kernel.h kernel/kprintf.c kernel/kprintf.h kernel/string.c kernel/string.h \
test/test.h test/test_suites.h | build
$(CC) $(TEST_HOST_CFLAGS) -fno-builtin -o $@ \
test/test_host_main.c test/test_kprintf.c test/test_string.c test/test_vga.c kernel/kprintf.c kernel/string.c
# ──────────────────────────────────────────────────────────────
# QEMU tests — compile into the kernel, boot in QEMU
# ──────────────────────────────────────────────────────────────
QEMU_TEST_CFLAGS = $(CFLAGS) -DTEST_QEMU -Ikernel -Itest
QEMU_TEST_KERNEL = build/test/notlinuxkernel-test.bin
QEMU_TEST_ISO = build/test/notlinuxkernel-test.iso
QEMU_TEST_OBJECTS = build/boot.o \
build/test/kernel.o build/test/serial.o build/test/string.o build/test/terminal.o build/test/kprintf.o \
build/test/test_qemu_main.o build/test/test_kprintf.o build/test/test_string.o \
build/test/test_vga.o
build/test/%.o: kernel/%.c | build/test
$(CC) $(QEMU_TEST_CFLAGS) -c $< -o $@
build/test/%.o: test/%.c | build/test
$(CC) $(QEMU_TEST_CFLAGS) -c $< -o $@
$(QEMU_TEST_KERNEL): $(QEMU_TEST_OBJECTS)
$(LD) $(LDFLAGS) -o $@ $^
$(QEMU_TEST_ISO): $(QEMU_TEST_KERNEL) grub/grub.cfg
@mkdir -p build/test/isodir/boot/grub
cp $(QEMU_TEST_KERNEL) build/test/isodir/boot/notlinuxkernel.bin
cp grub/grub.cfg build/test/isodir/boot/grub/grub.cfg
$(GRUB) -o $@ build/test/isodir
test-qemu: $(QEMU_TEST_ISO)
@echo "=== Running QEMU tests ==="
@$(QEMU) -cdrom $(QEMU_TEST_ISO) -serial stdio -display none \
-device isa-debug-exit,iobase=0xf4,iosize=0x04; \
test $$? -eq 1
build/test:
@mkdir -p build/test