summaryrefslogtreecommitdiff
path: root/Makefile
blob: fee6417f3780d9fe15e74f3d6ddcfda7f09b3065 (plain)
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
KERNEL=kernel.bin
ISO=os_image.iso

ARCH=amd64
CC=cc
LD=ld
AS=nasm

CFLAGS += -std=c2x -ffreestanding -lgcc -isystem include
CFLAGS += -Wall -Wextra -pedantic
CFLAGS += -O3 -pipe -g -fno-stack-protector
CFLAGS += -DPAGE_SIZE=4096

LDFLAGS += -nmagic --no-warn-rwx-segments -nostdlib

H_SRC = $(shell find src include -type f -name "*.h")

C_SRC = $(shell find src -type f -name "*.c")
C_OBJ = $(patsubst %.c,build/%.o,$(C_SRC))

A_SRC  = $(shell find src -type f -name "*.S" -not -path "src/arch/*")
A_SRC += $(shell find src/arch/$(ARCH) -type f -name "*.S")
A_OBJ  = $(patsubst %.S,build/%.S.o,$(A_SRC))

.PHONY: all clean

all: build/$(ISO)

build: build/$(ISO)

clean:
	@printf "\033[31m  RM  \033[0m%s\n" build
	@rm -rf build/*

$(A_OBJ): build/%.S.o : %.S
	@mkdir -p $(@D)
	@printf "\033[33m  AS  \033[0m%s\n" $<
	@nasm $< -f elf64 -o $@

$(C_OBJ): build/%.o : %.c
	@mkdir -p $(@D)
	@printf "\033[34m  CC  \033[0m%s\n" $<
	@$(CC) -c $(CFLAGS) -o $@ $<

build/$(KERNEL): arch/$(ARCH)/linker.ld $(A_OBJ) $(C_OBJ) $(H_SRC)
	@mkdir -p $(@D)
	@printf "\033[32m  LD  \033[0m%s\n" $@
	@$(LD) $(LDFLAGS) -T arch/$(ARCH)/linker.ld -o build/$(KERNEL) $(A_OBJ) $(C_OBJ)

build/$(ISO): build/$(KERNEL) grub.cfg
	@mkdir -p $(@D)
	@mkdir -p build/iso/boot/grub
	@cp grub.cfg build/iso/boot/grub
	@cp build/$(KERNEL) build/iso/boot
	@printf "\033[35m ISO  \033[0m%s\n" build/$(ISO)
	@grub-mkrescue -o build/$(ISO) build/iso 2> /dev/null

run: all
	@qemu-system-x86_64 \
		-cdrom build/$(ISO) \
		-serial stdio \
		-display sdl \
		-m 1G \
		-enable-kvm \
		-name corn