-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
150 lines (123 loc) · 4.33 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#it's called GNUmakefile as only GNU make will process it
# Nuke built-in rules and variables.
override MAKEFLAGS += -rR
# This is the name that our final kernel executable will have.
# Change as needed.
override KERNEL := myos
# Convenience macro to reliably declare user overridable variables.
define DEFAULT_VAR =
ifeq ($(origin $1),default)
override $(1) := $(2)
endif
ifeq ($(origin $1),undefined)
override $(1) := $(2)
endif
endef
# It is suggested to use a custom built cross toolchain to build a kernel.
# We are using the standard "cc" here, it may work by using
# the host system's toolchain, but this is not guaranteed.
override DEFAULT_CC := $(ENV_GCC)
$(eval $(call DEFAULT_VAR,CC,$(DEFAULT_CC)))
# Same thing for "ld" (the linker).
override DEFAULT_LD := $(ENV_LD)
$(eval $(call DEFAULT_VAR,LD,$(DEFAULT_LD)))
# User controllable C flags.
override DEFAULT_CFLAGS := -g -O2 -pipe
$(eval $(call DEFAULT_VAR,CFLAGS,$(DEFAULT_CFLAGS)))
# User controllable C preprocessor flags. We set none by default.
override DEFAULT_CPPFLAGS :=
$(eval $(call DEFAULT_VAR,CPPFLAGS,$(DEFAULT_CPPFLAGS)))
# User controllable nasm flags.
override DEFAULT_NASMFLAGS := -F dwarf -g
$(eval $(call DEFAULT_VAR,NASMFLAGS,$(DEFAULT_NASMFLAGS)))
# User controllable linker flags. We set none by default.
override DEFAULT_LDFLAGS :=
$(eval $(call DEFAULT_VAR,LDFLAGS,$(DEFAULT_LDFLAGS)))
# Internal C flags that should not be changed by the user.
override CFLAGS += \
-Isrc \
-Wall \
-Wextra \
-std=gnu11 \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fPIE \
-m64 \
-march=x86-64 \
-mno-80387 \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone \
# Internal C preprocessor flags that should not be changed by the user.
override CPPFLAGS := \
-I src \
$(CPPFLAGS) \
-MMD \
-MP
# Internal linker flags that should not be changed by the user.
override LDFLAGS += \
-m elf_x86_64 \
-nostdlib \
-static \
-z text \
--no-dynamic-linker \
-z max-page-size=0x1000 \
-T linker.ld
# Internal nasm flags that should not be changed by the user.
override NASMFLAGS += \
-Wall \
-f elf64
# 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')
override ASFILES := $(shell cd src && find -L * -type f -name '*.S')
override NASMFILES := $(shell cd src && find -L * -type f -name '*.asm')
override OBJ := $(addprefix build/obj/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o) $(NASMFILES:.asm=.asm.o))
override HEADER_DEPS := $(addprefix build/obj/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
# Default target.
.PHONY: all
all: clean init build/bin/$(KERNEL) build/image.iso
build/image.iso: build/bin/$(KERNEL)
mkdir -p build/iso_root/boot
cp -v build/bin/myos build/iso_root/boot/
mkdir -p build/iso_root/boot/limine
cp -v limine.cfg limine/limine-bios.sys limine/limine-bios-cd.bin \
limine/limine-uefi-cd.bin build/iso_root/boot/limine/
mkdir -p build/iso_root/EFI/BOOT
cp -v limine/BOOTX64.EFI build/iso_root/EFI/BOOT/
cp -v limine/BOOTIA32.EFI build/iso_root/EFI/BOOT/
xorriso -as mkisofs -b boot/limine/limine-bios-cd.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table \
--efi-boot boot/limine/limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
build/iso_root -o build/image.iso
./limine/limine bios-install build/image.iso
init:
mkdir -p build
# Link rules for the final kernel executable.
build/bin/$(KERNEL): Makefile linker.ld $(OBJ)
mkdir -p "$$(dirname $@)"
$(ENV_LD) $(OBJ) $(LDFLAGS) -o $@
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
build/obj/%.c.o: src/%.c Makefile
mkdir -p "$$(dirname $@)"
$(ENV_CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
# Compilation rules for *.S files.
build/obj/%.S.o: src/%.S Makefile
mkdir -p "$$(dirname $@)"
$(ENV_CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
# Compilation rules for *.asm (nasm) files.
build/obj/%.asm.o: src/%.asm Makefile
mkdir -p "$$(dirname $@)"
nasm $(NASMFLAGS) $< -o $@
# Remove object files and the final executable.
.PHONY: clean
clean:
rm -rf build
#! git clone https://github.com/limine-bootloader/limine.git --branch=v7.x-binary --depth=1
#! make -C limine