-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
180 lines (131 loc) · 4.48 KB
/
Makefile
File metadata and controls
180 lines (131 loc) · 4.48 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
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
176
177
178
179
180
# Makefile - build toolchain
# Copyright (C) 2025-2026 Ebrahim Aleem
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>
# Debug options
#export DEBUG = 1
export DEBUG_LOGGING = 1
export SMP_ENABLE = 1
# Global options
# Optional boot modules
export BUILD_BOOT_MULTIBOOT2 = 1
# Optional core modules
export BUILD_KERNEL_GRAPHICSBASE = 1
export BUILD_KERNEL_MEM_TEST = 1
# Optional driver modules
export BUILD_DRIVERS_SERIAL = 1
export BUILD_DRIVERS_HPET = 1
export BUILD_DRIVERS_AHCI = 1
export BUILD_DRIVERS_GPT = 1
export BUILD_DRIVERS_EXT2 = 1
# End of options
ifdef BUILD_DRIVERS_AHCI
export BUILD_DRIVERS_SATA = 1
endif
export SRC_TREE_ROOT = .
export OBJ_DIR = build
export CC := clang
export AR := llvm-ar
export STRIP := llvm-strip
SUBDIRS := kernel boot drivers test userland
KERNEL_TARGETS := \
$(OBJ_DIR)/kernel.a
BOOT_TARGETS := \
$(OBJ_DIR)/boot.a \
$(OBJ_DIR)/esp.img
DRIVERS_TARGETS := \
$(OBJ_DIR)/drivers.a
TEST_TARGETS := \
$(OBJ_DIR)/test_lib.a \
$(OBJ_DIR)/test_testsuite.a
USERLAND_TARGETS := $(OBJ_DIR)/userland_files/
export RUNTIME_DRIVERS_TARGETS := \
$(OBJ_DIR)/drivers/driver_list.txt
TEST_CANIDATES := $(patsubst $(OBJ_DIR)/test_%.a,test-%,$(TEST_TARGETS))
TEST_EXEC := $(basename $(TEST_TARGETS))
COPY_DOC_TO := $(OBJ_DIR)/rootfs/usr/share/doc/ModulOS/
COPY_DRIVERS_TO := $(OBJ_DIR)/rootfs/lib/drivers/
COPY_USERLAND_TO := $(OBJ_DIR)/rootfs/
SRC := $(shell find . -type f \( -name "*.c" -o -name "*.S" -o -name "*.h" \))
include $(SRC_TREE_ROOT)/scripts/Makefile.kcflags
.PHONY: build
build: $(OBJ_DIR)/modulos.img
.PHONY: all
all: index build test-all
.PHONY: index
index: cscope.files
ctags --C-kinds=+pxzL -R $(SRC)
cscope -q -R -b -i cscope.files
.PHONY: clean
clean:
-rm -rd $(OBJ_DIR)/ tags cscope.*
.PHONY: test-all
test-all: $(TEST_CANIDATES)
.PHONY: $(TEST_CANIDATES)
$(TEST_CANIDATES): test-%: $(OBJ_DIR)/test_%
-./$<
.PHONY: $(SUBDIRS)
$(SUBDIRS):
$(MAKE) -C $@ build
cscope.files: $(SRC)
find . -type f \( -name "*.c" -o -name "*.S" -o -name "*.h" \) > $@
%/:
-mkdir -p $@
$(KERNEL_TARGETS): kernel
$(BOOT_TARGETS): boot
$(DRIVERS_TARGETS): drivers
$(RUNTIME_DRIVERS_TARGETS): drivers
$(USERLAND_TARGETS): userland
$(TEST_TARGETS): test
$(TEST_EXEC): %: %.a $(OBJ_DIR)/boot.a $(OBJ_DIR)/kernel.a $(OBJ_DIR)/drivers.a
$(CC) -g -O0 -std=c23 $(CWARN) -fuse-ld=lld -o $@ $^
.PHONY: copy-doc
copy-doc: COPYING LICENSES | $(COPY_DOC_TO)
cp -r $^ $|
.PHONY: copy-runtime-drivers
copy-runtime-drivers: $(RUNTIME_DRIVERS_TARGETS) | $(COPY_DRIVERS_TO)
cp -r $^ $|
.PHONY: copy-userland
copy-userland: $(USERLAND_TARGETS) | $(COPY_USERLAND_TO)
cp -r $^/* $|
$(OBJ_DIR)/stub.img: | $(OBJ_DIR)/
truncate -s 4G $@
parted -s $@ mklabel gpt
parted -s $@ mkpart ESP fat32 4MiB 52MiB
parted -s $@ set 1 esp on
parted -s $@ mkpart rootfs ext2 52MiB 100%
$(OBJ_DIR)/stub-esp.dummy: $(OBJ_DIR)/stub.img $(OBJ_DIR)/modulos $(OBJ_DIR)/esp.img
mcopy -o -i $(OBJ_DIR)/esp.img $(OBJ_DIR)/modulos ::/
dd if=$(OBJ_DIR)/esp.img of=$< seek=1 count=12 bs=4M conv=notrunc
touch $@
# 54525952 is for 52MiB offset (512 * 1024 * 1024)
# 1034240 is for 52MiB initial (4MiB align + 48MiB ESP) and 4MiB tail for gpt
$(OBJ_DIR)/stub-fs.dummy: $(OBJ_DIR)/stub.img copy-runtime-drivers copy-userland copy-doc
yes | mke2fs -L rootfs -E offset=54525952 -d $(OBJ_DIR)/rootfs/ -t ext2 \
-b 4096 $< 1034240
touch $@
$(OBJ_DIR)/modulos.img: $(OBJ_DIR)/stub.img $(OBJ_DIR)/stub-esp.dummy $(OBJ_DIR)/stub-fs.dummy
@echo -n "\033[0;32m"
ln -f -s -r $< $@
@echo -n "\033[0m"
$(OBJ_DIR)/modulos.ld: $(filter-out test,$(SUBDIRS))
cat $$(find $(OBJ_DIR)/lds -type l -name "*.ld" | awk -F/ '{print $$NF, $$0}' | sort \
| cut -d' ' -f2-) > $@
$(OBJ_DIR)/modulos: $(OBJ_DIR)/modulos-dbg
$(STRIP) -s -o $@ $<
$(OBJ_DIR)/modulos-dbg: $(OBJ_DIR)/modulos.ld \
$(OBJ_DIR)/boot.a \
$(OBJ_DIR)/kernel.a \
$(OBJ_DIR)/drivers.a
$(CC) -o $@ $(LD_FLAGS) -fuse-ld=lld -T $^