-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
107 lines (87 loc) · 2.8 KB
/
Makefile
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
# Nuke built-in rules and variables.
MAKEFLAGS += -rR
.SUFFIXES:
# This is the name that our final executable will have.
# Change as needed.
override OUTPUT := potatos
# Convenience macro to reliably declare user overridable variables.
override USER_VARIABLE = $(if $(filter $(origin $(1)),default undefined),$(eval override $(1) := $(2)))
# User controllable C compiler command.
$(call USER_VARIABLE,KCC,cc)
# User controllable linker command.
$(call USER_VARIABLE,KLD,ld)
# User controllable C flags.
$(call USER_VARIABLE,KCFLAGS,-g -O2 -pipe)
# User controllable C preprocessor flags. We set none by default.
$(call USER_VARIABLE,KCPPFLAGS,)
# User controllable nasm flags.
$(call USER_VARIABLE,KNASMFLAGS,-F dwarf -g)
# User controllable linker flags. We set none by default.
$(call USER_VARIABLE,KLDFLAGS,)
# Internal C flags that should not be changed by the user.
override KCFLAGS += \
-Wall \
-Wextra \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fPIE \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-g \
-mno-sse \
-mno-sse2 \
-fno-omit-frame-pointer \
-mno-red-zone
# Internal C preprocessor flags that should not be changed by the user.
override KCPPFLAGS := \
-I src \
$(KCPPFLAGS) \
-MMD \
-MP
# Internal nasm flags that should not be changed by the user.
override KNASMFLAGS += \
-Wall \
-f elf64
# Internal linker flags that should not be changed by the user.
override KLDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-static \
-z max-page-size=0x1000 \
-T linker.ld
# Use "find" to glob all *.c, *.S, and *.asm files in the tree and obtain the
# object and header dependency file names.
override CFILES := $(shell cd src && find -L * -type f -name '*.c' | LC_ALL=C sort)
override ASFILES := $(shell cd src && find -L * -type f -name '*.S' | LC_ALL=C sort)
override NASMFILES := $(shell cd src && find -L * -type f -name '*.asm' | LC_ALL=C sort)
override OBJ := $(addprefix obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
# Default target.
.PHONY: all
all: bin/$(OUTPUT)
# Link rules for the final executable.
bin/$(OUTPUT): linker.ld $(OBJ)
mkdir -p "$$(dirname $@)"
$(KLD) $(OBJ) $(KLDFLAGS) -o $@
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
obj/%.c.o: src/%.c
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
# Compilation rules for *.S files.
obj/%.S.o: src/%.S
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
# Compilation rules for *.asm (nasm) files.
obj/%.asm.o: src/%.asm
mkdir -p "$$(dirname $@)"
nasm $(KNASMFLAGS) $< -o $@
# Remove object files and the final executable.
.PHONY: clean
clean:
rm -rf obj