-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
175 lines (147 loc) · 4.22 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
MODE := debug
ARCH := x86_64
CXX := clang++
NASM := nasm
LD := $(ARCH)-elf-ld
QEMU := qemu-system-$(ARCH)
DEPS_DIR := build/deps
LIBS_DIR := lib
SUPPORTED_ARCHS = x86_64
ifeq ($(filter $(ARCH),$(SUPPORTED_ARCHS)),)
$(error ARCH "$(ARCH)" is not supported. Supported architectures: $(SUPPORTED_ARCHS))
endif
CXXFLAGS := \
--target=x86_64-unknown-elf \
-std=c++20 \
-Wall \
-Wfatal-errors \
-Wshadow \
-Wextra \
-Wpedantic \
-pedantic-errors \
-ffreestanding \
-fPIE \
-fno-stack-protector \
-fno-unwind-tables \
-fno-omit-frame-pointer \
-fno-asynchronous-unwind-tables \
-fno-rtti \
-fno-exceptions \
-fno-lto \
-m64 \
-mcmodel=kernel \
-mno-mmx \
-mno-sse \
-mno-sse2 \
-mno-red-zone \
CPPFLAGS := \
-MMD \
-I lib/thirdparty/libc++/include \
-I lib/thirdparty/ \
-I . \
NASMFLAGS := \
-Wall \
-f elf64 \
LDFLAGS := \
-m elf_${ARCH} \
-nostdlib \
-static \
--no-dynamic-linker \
-z max-page-size=0x1000 \
-T kernel/arch/$(ARCH)/linker.ld
QEMUFLAGS := \
-device isa-debug-exit,iobase=0xf4,iosize=0x04 \
-smp cpus=2 \
-no-reboot \
-serial stdio
ifeq ($(MODE), debug)
BUILD_DIR := build/debug
NASMFLAGS += -g -dwarf
CXXFLAGS += \
-g -O0 \
-DDEBUG
MACROS := -DDEBUG
QEMUFLAGS += \
-D qemu-log.txt \
-d int -M smm=off
else ifeq ($(MODE), test)
BUILD_DIR := build/test
NASMFLAGS += -g -dwarf
CXXFLAGS += \
-g -O3 \
-DENABLE_TESTS \
-DDEBUG
MACROS := -DDEBUG -DENABLE_TESTS
QEMUFLAGS += \
-D qemu-log.txt \
-display none \
-d int -M smm=off
else ifeq ($(MODE), release)
BUILD_DIR := build/release
MACROS := -DRELEASE
CXXFLAGS += -O3 -DRELEASE
else
$(error Invalid argument $(mode) for variable mode. Must be either debug, release, or test.)
endif
CXX_SOURCES := $(shell find . -type f -name '*.cc' | grep -v $(DEPS_DIR))
NASM_SOURCES := $(shell find . -type f -name '*.asm' | grep -v $(DEPS_DIR))
OBJECTS := $(patsubst ./%.cc, $(BUILD_DIR)/%.cc.o, $(CXX_SOURCES)) \
$(patsubst ./%.asm, $(BUILD_DIR)/%.asm.o, $(NASM_SOURCES))
# Ignore the other object files for other architectures
OBJECTS := $(filter-out $(foreach arch,$(filter-out $(ARCH),$(SUPPORTED_ARCHS)), $(BUILD_DIR)/arch/$(arch)/%.o), $(OBJECTS))
# Do not build test objects if we're not on `test` mode
ifneq ($(MODE), test)
OBJECTS := $(filter-out $(BUILD_DIR)/tests/%.cc.o, $(OBJECTS))
endif
HEADER_DEPS := $(OBJECTS:%.o=%.d)
.PHONY: clean .clangd build build/symbols.h
all: run
clean:
$(RM) -r build/debug build/release build/test
distclean:
$(RM) -rf build .clangd qemu-log.txt
slocs:
tokei . --exclude=assets/fonts/pixeloperator.cc
debug-address:
objdump -D $(BUILD_DIR)/kernel.elf | c++filt | grep -e $(address) -C 4
run: build
@$(QEMU) $(QEMUFLAGS) -cdrom $(BUILD_DIR)/nimble-os.iso || true
build: .clangd dependencies $(OBJECTS) build/symbols.h
@$(LD) $(LDFLAGS) $(OBJECTS) -o $(BUILD_DIR)/kernel.elf
@mkdir -p $(BUILD_DIR)/isoroot
@cp $(BUILD_DIR)/kernel.elf \
limine.cfg \
$(DEPS_DIR)/limine/limine-bios.sys \
$(DEPS_DIR)/limine/limine-bios-cd.bin \
$(DEPS_DIR)/limine/limine-uefi-cd.bin \
$(BUILD_DIR)/isoroot
@mkdir -p $(BUILD_DIR)/isoroot/EFI/BOOT
@cp $(DEPS_DIR)/limine/BOOT*.EFI $(BUILD_DIR)/isoroot/EFI/BOOT/
@xorriso -as mkisofs -b limine-bios-cd.bin \
-no-emul-boot -boot-load-size 4 -boot-info-table \
--efi-boot limine-uefi-cd.bin \
-efi-boot-part --efi-boot-image --protective-msdos-label \
$(BUILD_DIR)/isoroot -o $(BUILD_DIR)/nimble-os.iso
@$(DEPS_DIR)/limine/limine bios-install $(BUILD_DIR)/nimble-os.iso
@$(RM) -r $(BUILD_DIR)/isoroot
dependencies:
@mkdir -p $(DEPS_DIR)/limine
@echo "Downloading Limine ..."
@-git clone https://github.com/limine-bootloader/limine --depth=1 --branch=v5.x-branch-binary $(DEPS_DIR)/limine
@$(MAKE) -C $(DEPS_DIR)/limine
@cp $(DEPS_DIR)/limine/limine.h lib/thirdparty/limine.h
@echo "Downloading Freestanding C++ Headers ..."
@-git clone https://github.com/ilobilo/libstdcxx-headers --depth=1 lib/thirdparty/libc++
-include $(HEADER_DEPS)
$(BUILD_DIR)/%.cc.o: %.cc
@mkdir -p $(dir $@)
@echo "CXX $<"
@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@
$(BUILD_DIR)/%.asm.o: %.asm
@mkdir -p $(dir $@)
@echo "NASM $<"
@$(NASM) $(NASMFLAGS) $< -o $@
.clangd:
@./scripts/generate_clangd_config.py
build/symbols.h:
@./scripts/generate_kernel_symbols.py