-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
65 lines (49 loc) · 1.77 KB
/
Makefile
File metadata and controls
65 lines (49 loc) · 1.77 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
# ===================== Makefile =====================
CC = gcc
CFLAGS = -m32 -ffreestanding -nostdlib -fno-pie -Ikernel -Ilib -Wall -Wextra
LD = ld
ASM = nasm
OBJCOPY = objcopy
BUILD_DIR = build
KERNEL_OBJS = \
$(BUILD_DIR)/kernel_entry.o \
$(BUILD_DIR)/kernel.o \
$(BUILD_DIR)/cli.o \
$(BUILD_DIR)/stdio.o \
$(BUILD_DIR)/string.o
.PHONY: all clean run
# === Build everything ===
all: $(BUILD_DIR)/os-image.img
# === Ensure build directory exists ===
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# === Step 1: Assemble bootloader (ensure build dir exists first) ===
$(BUILD_DIR)/bootloader.bin: bootloader.asm | $(BUILD_DIR)
$(ASM) $< -f bin -o $@
# === Step 2: Kernel Assembly and C files ===
$(BUILD_DIR)/kernel_entry.o: kernel/kernel_entry.asm | $(BUILD_DIR)
$(ASM) $< -f elf -o $@
$(BUILD_DIR)/kernel.o: kernel/kernel.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/cli.o: kernel/cli.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/stdio.o: lib/stdio.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
$(BUILD_DIR)/string.o: lib/string.c | $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# === Step 3: Link kernel ELF and convert to raw binary ===
$(BUILD_DIR)/kernel.elf: $(KERNEL_OBJS) | $(BUILD_DIR)
$(LD) -m elf_i386 -T linker.ld -o $@ $^
$(BUILD_DIR)/kernel.bin: $(BUILD_DIR)/kernel.elf
$(OBJCOPY) -O binary $< $@
# === Step 4: Build full floppy image ===
$(BUILD_DIR)/os-image.img: $(BUILD_DIR)/bootloader.bin $(BUILD_DIR)/kernel.bin | $(BUILD_DIR)
dd if=/dev/zero of=$@ bs=512 count=2880
dd if=$(BUILD_DIR)/bootloader.bin of=$@ conv=notrunc
dd if=$(BUILD_DIR)/kernel.bin of=$@ bs=512 seek=1 conv=notrunc
# === Step 5: Run in QEMU ===
run: all
qemu-system-i386 -fda $(BUILD_DIR)/os-image.img
# === Clean build directory ===
clean:
rm -rf $(BUILD_DIR)