-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
68 lines (55 loc) · 2.01 KB
/
Makefile
File metadata and controls
68 lines (55 loc) · 2.01 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
ARCH ?= x86_64
KERNEL := build/kernel-$(ARCH).bin
ISO := build/os-$(ARCH).iso
AS := nasm
ASFLAGS := -felf64
CC := $(ARCH)-elf-gcc --sysroot=../libc/sysroot -isystem=/usr/include
LD := $(ARCH)-elf-ld
LINKER_SCRIPT := src/arch/$(ARCH)/linker.ld
LDFLAGS := -n -L../libc/sysroot/usr/lib
CSTD := -std=gnu11
WARNINGS := -Wall -Werror -Wextra
INCLUDES := -Iinclude
LIBS := -lk
CFLAGS := $(CSTD) $(INCLUDES) -ffreestanding $(WARNINGS)
GRUB_CFG := src/arch/$(ARCH)/grub/grub.cfg
ASSEMBLY_SOURCES := $(wildcard src/arch/$(ARCH)/*.asm)
ASSEMBLY_OBJECTS := $(patsubst src/arch/$(ARCH)/%.asm, build/arch/$(ARCH)/%.o, $(ASSEMBLY_SOURCES))
C_SOURCES := $(wildcard src/*.c)
C_ARCH_SOURCES := $(wildcard src/arch/$(ARCH)/*.c)
C_ARCH_OBJECTS := $(patsubst src/%.c, build/%.o, $(C_ARCH_SOURCES))
C_OBJECTS := $(patsubst src/%.c, build/%.o, $(C_SOURCES))
CRTBEGIN_OBJECT := $(shell $(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=crtbegin.o)
CRTI_OBJECT := build/arch/x86_64/crti.o
CRTN_OBJECT := build/arch/x86_64/crtn.o
CRT_FILTER := $(CRTI_OBJECT) $(CRTN_OBJECT)
CRTEND_OBJECT := $(shell $(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=crtend.o)
OBJECT_LINK_LIST := \
$(CRTI_OBJECT) \
$(filter-out $(CRTN_OBJECT), $(filter-out $(CRTI_OBJECT), $(ASSEMBLY_OBJECTS))) \
$(C_ARCH_OBJECTS) \
$(C_OBJECTS) \
$(CRTN_OBJECT)
.PHONY: all clean run iso
all: $(KERNEL)
clean:
rm -rfv build
run: $(ISO)
qemu-system-x86_64 -monitor stdio -cdrom $(ISO)
iso: $(ISO)
$(ISO): $(KERNEL) $(GRUB_CFG)
mkdir -p build/isofiles/boot/grub
cp $(KERNEL) build/isofiles/boot/kernel.bin
cp $(GRUB_CFG) build/isofiles/boot/grub
grub-mkrescue -d /usr/lib/grub/i386-pc -o $(ISO) build/isofiles
rm -rfv build/isofiles
$(KERNEL): $(OBJECT_LINK_LIST) $(LINKER_SCRIPT)
$(LD) -T $(LINKER_SCRIPT) $(OBJECT_LINK_LIST) -o $(KERNEL) $(LDFLAGS) $(LIBS)
# compile assembly files
build/arch/$(ARCH)/%.o: src/arch/$(ARCH)/%.asm
mkdir -p $(shell dirname $@)
$(AS) $(ASFLAGS) $< -o $@
build/%.o: src/%.c
mkdir -p $(shell dirname $@)
$(CC) $(CFLAGS) $(LIBS) -c -o $@ $<
print-% : ; @echo $* = $($*)