-
Notifications
You must be signed in to change notification settings - Fork 0
/
GNUmakefile
273 lines (234 loc) · 7.56 KB
/
GNUmakefile
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# Nuke built-in rules and variables.
MAKEFLAGS += -rR
.SUFFIXES:
# This is the name that our final executable will have.
# Change as needed.
override OUTPUT := kernel
# Convenience macro to reliably declare user overridable variables.
override USER_VARIABLE = $(if $(filter $(origin $(1)),default undefined),$(eval override $(1) := $(2)))
# Target architecture to build for. Default to x86_64.
$(call USER_VARIABLE,KARCH,x86_64)
# Destination directory on install (should always be empty by default).
$(call USER_VARIABLE,DESTDIR,)
# Install prefix; /usr/local is a good, standard default pick.
$(call USER_VARIABLE,PREFIX,/usr/local)
# Check if the architecture is supported.
ifeq ($(filter $(KARCH),aarch64 loongarch64 riscv64 x86_64),)
$(error Architecture $(KARCH) not supported)
endif
# User controllable C compiler command.
$(call USER_VARIABLE,KCC,clang)
# User controllable C++ compiler command.
$(call USER_VARIABLE,KCXX,clang++)
# User controllable linker command.
$(call USER_VARIABLE,KLD,ld.lld)
# User controllable V command.
$(call USER_VARIABLE,KV,v)
# User controllable C flags.
$(call USER_VARIABLE,KCFLAGS,-g -O2 -pipe)
# User controllable C++ flags. We default to same as C flags.
$(call USER_VARIABLE,KCXXFLAGS,$(KCFLAGS))
# User controllable C/C++ preprocessor flags. We set none by default.
$(call USER_VARIABLE,KCPPFLAGS,)
# User controllable V flags. We set none by default.
$(call USER_VARIABLE,KVFLAGS,)
# User controllable linker flags. We set none by default.
$(call USER_VARIABLE,KLDFLAGS,)
# Internal flags shared by both C and C++ compilers.
override SHARED_FLAGS := \
-Wall \
-Wextra \
-nostdinc \
-ffreestanding \
-fno-stack-protector \
-fno-stack-check \
-fno-lto \
-fno-PIC \
-ffunction-sections \
-fdata-sections
# Internal C/C++ preprocessor flags that should not be changed by the user.
override KCPPFLAGS := \
-I c \
$(KCPPFLAGS) \
-isystem mlibc/build-$(KARCH)/prefix/include \
-isystem freestanding-headers \
-MMD \
-MP
# Architecture specific internal flags.
ifeq ($(KARCH),x86_64)
ifeq ($(KCC),clang)
override KCC += \
-target x86_64-unknown-none
endif
ifeq ($(KCXX),clang++)
override KCXX += \
-target x86_64-unknown-none
endif
override SHARED_FLAGS += \
-m64 \
-march=x86-64 \
-mno-red-zone \
-mcmodel=kernel
override KLDFLAGS += \
-m elf_x86_64
override KNASMFLAGS += \
-f elf64
endif
ifeq ($(KARCH),aarch64)
ifeq ($(KCC),clang)
override KCC += \
-target aarch64-unknown-none
endif
ifeq ($(KCXX),clang++)
override KCXX += \
-target aarch64-unknown-none
endif
override KLDFLAGS += \
-m aarch64elf
endif
ifeq ($(KARCH),riscv64)
ifeq ($(KCC),clang)
override KCC += \
-target riscv64-unknown-none
endif
ifeq ($(shell $(KCC) --version | grep -i 'clang'),)
override KCFLAGS += \
-march=rv64imac_zicsr_zifencei
else
override KCFLAGS += \
-march=rv64imac
endif
ifeq ($(KCXX),clang++)
override KCXX += \
-target riscv64-unknown-none
endif
ifeq ($(shell $(KCXX) --version | grep -i 'clang'),)
override KCXXFLAGS += \
-march=rv64imac_zicsr_zifencei
else
override KCXXFLAGS += \
-march=rv64imac
endif
override SHARED_FLAGS += \
-mabi=lp64 \
-mno-relax
override KLDFLAGS += \
-m elf64lriscv \
--no-relax
endif
ifeq ($(KARCH),loongarch64)
ifeq ($(KCC),clang)
override KCC += \
-target loongarch64-unknown-none
endif
ifeq ($(KCXX),clang++)
override KCXX += \
-target loongarch64-unknown-none
endif
override SHARED_FLAGS += \
-march=loongarch64 \
-mabi=lp64s
override KLDFLAGS += \
-m elf64loongarch \
--no-relax
endif
override MLIBC_SHARED_FLAGS := \
-D__thread='' \
-D_Thread_local='' \
-D_GNU_SOURCE
override MLIBC_CFLAGS := \
$(KCFLAGS) \
$(SHARED_FLAGS) \
$(MLIBC_SHARED_FLAGS)
override MLIBC_CXXFLAGS := \
$(KCXXFLAGS) \
$(SHARED_FLAGS) \
-fno-rtti \
-fno-exceptions \
$(MLIBC_SHARED_FLAGS)
# Internal C flags that should not be changed by the user.
override KCFLAGS += \
-std=gnu99 \
$(SHARED_FLAGS)
obj-$(KARCH)/flanterm/backends/fb.c.o: override KCPPFLAGS += \
-DFLANTERM_FB_DISABLE_BUMP_ALLOC
# Internal linker flags that should not be changed by the user.
override KLDFLAGS += \
-nostdlib \
-static \
-z max-page-size=0x1000 \
-gc-sections \
-T linker-$(KARCH).ld
override KVFLAGS += \
-os vinix \
-enable-globals \
-nofloat \
-manualfree \
-experimental \
-message-limit 10000 \
-gc none \
-d no_backtrace
# Use "find" to glob all *.v, *.c, and *.S files in the tree and obtain the
# object and header dependency file names.
override VFILES := $(shell find -L * -type f -name '*.v' | LC_ALL=C sort)
override CFILES := $(shell cd c && find -L * -type f -name '*.c' | LC_ALL=C sort)
override ASFILES := $(shell cd asm && find -L * -type f -name '*.S' | LC_ALL=C sort)
override OBJ := $(addprefix obj-$(KARCH)/,$(CFILES:.c=.c.o) $(ASFILES:.S=.S.o))
override HEADER_DEPS := $(addprefix obj-$(KARCH)/,$(CFILES:.c=.c.d) $(ASFILES:.S=.S.d))
# Ensure the dependencies have been obtained.
override MISSING_DEPS := $(shell if ! test -d freestanding-headers || ! test -f c/cc-runtime.c || ! test -d c/flanterm || ! test -d mlibc; then echo 1; fi)
ifeq ($(MISSING_DEPS),1)
$(error Please run the ./get-deps script first)
endif
# Default target.
.PHONY: all
all: bin-$(KARCH)/$(OUTPUT)
# Link rules for the final executable.
bin-$(KARCH)/$(OUTPUT): GNUmakefile linker-$(KARCH).ld obj-$(KARCH)/blob.c.o mlibc/build-$(KARCH)/libc.a $(OBJ)
mkdir -p "$$(dirname $@)"
$(KLD) obj-$(KARCH)/blob.c.o mlibc/build-$(KARCH)/libc.a $(OBJ) $(KLDFLAGS) -o $@
obj-$(KARCH)/blob.c.o: mlibc/build-$(KARCH)/libc.a $(VFILES)
mkdir -p "$$(dirname $@)"
$(KV) $(KVFLAGS) -o obj-$(KARCH)/blob.c .
sed 's/call 0(/call *(/g' < obj-$(KARCH)/blob.c > obj-$(KARCH)/blob.c.tmp
mv obj-$(KARCH)/blob.c.tmp obj-$(KARCH)/blob.c
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -w -c obj-$(KARCH)/blob.c -o $@
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
obj-$(KARCH)/%.c.o: c/%.c mlibc/build-$(KARCH)/libc.a GNUmakefile
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
# Compilation rules for *.S files.
obj-$(KARCH)/%.S.o: asm/%.S mlibc/build-$(KARCH)/libc.a GNUmakefile
mkdir -p "$$(dirname $@)"
$(KCC) $(KCFLAGS) $(KCPPFLAGS) -c $< -o $@
.PHONY: mlibc
mlibc:
rm -f mlibc/build-$(KARCH)/libc.a
$(MAKE) mlibc/build-$(KARCH)/libc.a
mlibc/build-$(KARCH)/libc.a:
ARCH="$(KARCH)" \
CFLAGS="$(MLIBC_CFLAGS)" \
CXXFLAGS="$(MLIBC_CXXFLAGS)" \
CC="$(KCC)" \
CXX="$(KCXX)" \
./build-mlibc
# Remove object files and the final executable.
.PHONY: clean
clean:
rm -rf bin-$(KARCH) obj-$(KARCH)
# Remove everything built and generated including downloaded dependencies.
.PHONY: distclean
distclean:
rm -rf bin-* obj-* freestanding-headers c/cc-runtime.c c/flanterm mlibc
# Install the final built executable to its final on-root location.
.PHONY: install
install: all
install -d "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)"
install -m 644 bin-$(KARCH)/$(OUTPUT) "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)/$(OUTPUT)-$(KARCH)"
# Try to undo whatever the "install" target did.
.PHONY: uninstall
uninstall:
rm -f "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)/$(OUTPUT)-$(KARCH)"
-rmdir "$(DESTDIR)$(PREFIX)/share/$(OUTPUT)"