-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
78 lines (61 loc) · 1.88 KB
/
Copy pathMakefile
File metadata and controls
78 lines (61 loc) · 1.88 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
# Compiler/Assembler/Emulator
CC = gcc
ASM = nasm
LD = ld
QEMU = qemu-system-i386
# Directories
BOOT_DIR = bootloader
KERNEL_DIR = kernel
# Source files
BOOT_SOURCE = $(BOOT_DIR)/boot.asm
KERNEL_ENTRY = $(KERNEL_DIR)/kernel_entry.asm
KERNEL_ASM = $(KERNEL_DIR)/keyboard_handler.asm
KERNEL_SOURCE = $(KERNEL_DIR)/kernel_shell.c $(KERNEL_DIR)/interrupts.c
# Object files
BOOT_BIN = boot.bin
KERNEL_ENTRY_OBJ = kernel_entry.o
KERNEL_ASM_OBJ = $(KERNEL_ASM:.asm=.o)
KERNEL_OBJS = $(addprefix $(KERNEL_DIR)/, $(notdir $(KERNEL_SOURCE:.c=.o)))
KERNEL_BIN = kernel.bin
# Output
OS_IMAGE = os-image
DISK_IMG = disk.img
# Flags
CFLAGS = -m32 -c -ffreestanding -fno-pie -fno-stack-protector -nostdlib -Wall
ASMFLAGS = -f elf32
LDFLAGS = -m elf_i386 -T $(KERNEL_DIR)/linker.ld --oformat binary -nostdlib
# Default target
all: $(OS_IMAGE)
# Compile each kernel C source file individually to its corresponding .o file
$(KERNEL_DIR)/%.o: $(KERNEL_DIR)/%.c
$(CC) $(CFLAGS) $< -o $@
# Assemble boot sector
$(BOOT_BIN): $(BOOT_SOURCE)
$(ASM) -f bin $< -o $@
# Assemble kernel entry
$(KERNEL_ENTRY_OBJ): $(KERNEL_ENTRY)
$(ASM) $(ASMFLAGS) $< -o $@
# Assemble kernel assembly files
$(KERNEL_ASM_OBJ): $(KERNEL_ASM)
$(ASM) $(ASMFLAGS) $< -o $@
# Link kernel
$(KERNEL_BIN): $(KERNEL_ENTRY_OBJ) $(KERNEL_ASM_OBJ) $(KERNEL_OBJS)
$(LD) $(LDFLAGS) $^ -o $@
# Create OS image
$(OS_IMAGE): $(BOOT_BIN) $(KERNEL_BIN)
cat $^ > $@
# Create bootable disk image
$(DISK_IMG): $(OS_IMAGE)
dd if=/dev/zero of=$@ bs=512 count=2880
dd if=$(BOOT_BIN) of=$@ bs=512 count=1 conv=notrunc
dd if=$(KERNEL_BIN) of=$@ bs=512 seek=1 conv=notrunc
# Run in QEMU
run: $(DISK_IMG)
$(QEMU) -drive format=raw,file=$<,if=floppy
# Debug with QEMU and GDB
debug: $(DISK_IMG)
$(QEMU) -drive format=raw,file=$<,if=floppy -s -S
# Clean build files
clean:
rm -f *.bin *.o $(OS_IMAGE) $(DISK_IMG) qemu.log $(KERNEL_DIR)/*.o
.PHONY: all run debug clean