-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
58 lines (48 loc) · 1.62 KB
/
Copy pathMakefile
File metadata and controls
58 lines (48 loc) · 1.62 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
CC=gcc
CFLAGS=-Wall -Wextra -ffreestanding -nostdlib -fno-builtin -fno-stack-protector -O2 -Iinclude -Iinclude/asm-x86_64
SYSCALL_CFLAGS=$(CFLAGS) -mgeneral-regs-only
NASM=nasm
NASMFLAGS_BIN=-f bin -w+all
NASMFLAGS_ELF64 = -f elf64 -w+all
LD = ld
LDFLAGS = -T linker.ld -nostdlib
DIRS = arch/x86_64 kernel mm init drivers fs lib
C_SRCS = $(foreach dir,$(DIRS),$(wildcard $(dir)/*.c))
ASM_SRCS_BIN = boot/boot.S
ASM_SRCS_NASM = arch/x86_64/interrupts.S arch/x86_64/startup.S arch/x86_64/isr.S kernel/context_switch.S
C_OBJS = $(C_SRCS:.c=.o)
ASM_OBJS_BIN = $(ASM_SRCS_BIN:.S=.bin)
ASM_OBJS_NASM = $(ASM_SRCS_NASM:.S=.o)
.PHONY: all clean iso run
all: boot.bin kernel.bin
boot.bin: boot/boot.S
$(NASM) $(NASMFLAGS_BIN) -o $@ $<
%.o: %.S
@if echo $< | grep -q -E "^(boot/boot.S)"; then \
$(NASM) $(NASMFLAGS_BIN) -o $@ $<; \
else \
$(NASM) $(NASMFLAGS_ELF64) -o $@ $<; \
fi
kernel/syscall.o: kernel/syscall.c
$(CC) $(SYSCALL_CFLAGS) -c $< -o $@
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
kernel.bin: $(C_OBJS) $(ASM_OBJS_NASM)
$(LD) $(LDFLAGS) -o $@ $^
iso: all
rm -rf iso
mkdir -p iso/boot/grub
cp kernel.bin iso/boot/kernel.bin
cp boot.bin iso/boot/boot.bin
echo 'set timeout=1' > iso/boot/grub/grub.cfg
echo 'set default=0' >> iso/boot/grub/grub.cfg
echo 'menuentry "Simple Kernel" {' >> iso/boot/grub/grub.cfg
echo ' multiboot2 /boot.bin' >> iso/boot/grub/grub.cfg
echo ' boot' >> iso/boot/grub/grub.cfg
echo '}' >> iso/boot/grub/grub.cfg
grub-mkrescue -o test_os.iso iso
clean:
rm -f $(C_OBJS) $(ASM_OBJS_NASM) boot.bin kernel.bin
rm -rf iso test_os.iso
run: iso
qemu-system-x86_64 -cdrom test_os.iso -m 512M -serial stdio