-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
123 lines (82 loc) · 2.34 KB
/
Makefile
File metadata and controls
123 lines (82 loc) · 2.34 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
.SECONDEXPANSION:
#################
### VARIABLES ###
#################
ARCH := x86_64
TARGET := $(ARCH)-none-elf
O := build
ASMC := nasm
CC := clang
LD := ld.lld
OBJCOPY := objcopy
AR := llvm-ar
QEMUC := qemu-system-$(ARCH)
RM := rm -vrf
INCLUDE_DIRS := ./include ./stdlib/include/
CFLAGS := -Wall -Wextra -ffreestanding \
-target $(TARGET) -std=gnu23 $(foreach dir,$(INCLUDE_DIRS), -I$(dir) )
LDFLAGS := -nostdlib
QEMUFLAGS := -no-reboot
ifeq ($(SHOW_DD), 1)
SILENCE_DD :=
else
SILENCE_DD := 2> /dev/null
endif
###################
### COMPILATION ###
###################
$(O)/boot.bin: $(O)/first_boot.bin $(O)/second_boot.bin $(O)/kernel.bin
@$(RM) $@
$(foreach file,$^, dd if=$(file) of=$@ bs=512 oflag=append conv=sync,notrunc $(SILENCE_DD);)
truncate -c -s 11776 $@
$(O)/first_boot.bin: first_boot.s | $(O)/
$(ASMC) -f bin -o $@ $<
##############
### STDLIB ###
##############
STDLIB_SRC := stdlib/print.c stdlib/memory.s
STDLIB_OBJS := $(patsubst %,$(O)/%.o,$(STDLIB_SRC))
$(O)/libstdlib.a: $(STDLIB_OBJS)
$(AR) rcs $@ $^
##############
### KERNEL ###
##############
KERNEL_SRC := kernel/interrupts.c kernel/interrupts.s kernel/kernel.c kernel/kernel.s
KERNEL_OBJS := $(patsubst %,$(O)/%.o,$(KERNEL_SRC))
$(O)/kernel.bin: $(O)/kernel.elf
$(OBJCOPY) -O binary $< $@
$(O)/kernel.elf: kernel/kernel.ld $(KERNEL_OBJS) $(O)/libstdlib.a
$(LD) $(LDFLAGS) -o $@ $^
###############################
### SECOND STAGE BOOTLOADER ###
###############################
SECOND_BOOT_SRC := $(wildcard second_boot/*.c) $(wildcard second_boot/*.s)
SECOND_BOOT_OBJS := $(patsubst %,$(O)/%.o32,$(SECOND_BOOT_SRC))
$(O)/second_boot.bin: $(O)/second_boot.elf
$(OBJCOPY) -O binary $< $@
$(O)/second_boot.elf: second_boot/second_boot.ld $(SECOND_BOOT_OBJS)
$(LD) $(LDFLAGS) -o $@ $^
#############
### PHONY ###
#############
run: $(O)/boot.bin
$(QEMUC) $(QEMUFLAGS) -drive format=raw,file=$<,if=ide
clean:
$(RM) $(O)
.PHONY: clean run
######################
### IMPLICIT RULES ###
######################
# rule for making a directory
%/:
mkdir -p $@
# default way to build a c file
$(O)/%.c.o: %.c | $$(dir $$@)
$(CC) $(CFLAGS) -c -o $@ $<
$(O)/%.s.o: %.s | $$(dir $$@)
$(ASMC) -f elf64 -o $@ $<
# 32 bit version for c and asm files
$(O)/%.c.o32: %.c | $$(dir $$@)
$(CC) $(CFLAGS) -m32 -c -o $@ $<
$(O)/%.s.o32: %.s | $$(dir $$@)
$(ASMC) -f elf32 -o $@ $<