forked from skift-org/skift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
327 lines (239 loc) · 8.12 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
.SUFFIXES:
.DEFAULT_GOAL := all
export PATH := $(shell toolchain/use-it.sh):$(PATH)
export PATH := $(shell toolbox/use-it.sh):$(PATH)
ifeq (, $(shell which inkscape))
$(error "No inkscape in PATH, consider installing it")
endif
DIRECTORY_GUARD=@mkdir -p $(@D)
BUILD_ARCH?=i686
BUILD_CONFIG?=debug
BUILD_SYSTEM?=skift
BUILD_TARGET=$(BUILD_CONFIG)-$(BUILD_ARCH)-$(BUILD_SYSTEM)
BUILD_GITREF=$(shell git rev-parse --abbrev-ref HEAD || echo unknown)@$(shell git rev-parse --short HEAD || echo unknown)
BUILD_UNAME=$(shell uname -s -o -m -r)
BUILD_DIRECTORY=$(shell pwd)/build
SYSROOT=$(BUILD_DIRECTORY)/sysroot
BOOTROOT=$(BUILD_DIRECTORY)/bootroot
BUILD_DIRECTORY_LIBS=$(SYSROOT)/lib
BUILD_DIRECTORY_APPS=$(SYSROOT)/bin
BUILD_DIRECTORY_UTILS=$(SYSROOT)/bin
# --- Configs -------------------------------------------- #
QEMU=qemu-system-x86_64
QEMUFLAGS=-m 256M -serial mon:stdio -rtc base=localtime -nic user,model=virtio-net-pci
CC:=i686-pc-skift-gcc
CFLAGS:= \
-O2 \
-std=gnu11 \
-MD \
\
-Wall \
-Wextra \
-Wc++-compat \
-Werror \
\
-I. \
-Iapplications \
-Ilibraries \
-Ilibraries/libcompat \
\
-D__BUILD_ARCH__=\""$(BUILD_ARCH)"\" \
-D__BUILD_CONFIG__=\""$(BUILD_CONFIG)"\" \
-D__BUILD_SYSTEM__=\""$(BUILD_SYSTEM)"\" \
-D__BUILD_TARGET__=\""$(BUILD_TARGET)"\" \
-D__BUILD_GITREF__=\""$(BUILD_GITREF)"\" \
-D__BUILD_UNAME__=\""$(BUILD_UNAME)"\"
LD:=i686-pc-skift-ld
LDFLAGS:=
AR:=i686-pc-skift-ar
ARFLAGS:=rcs
AS=nasm
ASFLAGS=-f elf32
# --- Kernel --------------------------------------------- #
KERNEL_SOURCES = \
$(wildcard kernel/*.c) \
$(wildcard kernel/*/*.c)
KERNEL_ASSEMBLY_SOURCES = \
$(wildcard kernel/*.s) \
$(wildcard kernel/*/*.s)
KERNEL_LIBRARIES_SOURCES = \
$(wildcard libraries/libfile/*.c) \
$(wildcard libraries/libjson/*.c) \
$(wildcard libraries/libmath/*.c) \
$(wildcard libraries/libmath/*/*.c) \
$(wildcard libraries/libsystem/*.c) \
$(wildcard libraries/libsystem/io/*.c) \
$(wildcard libraries/libsystem/unicode/*.c) \
$(wildcard libraries/libsystem/process/*.c) \
$(wildcard libraries/libsystem/utils/*.c)
KERNEL_BINARY = $(BOOTROOT)/boot/kernel.bin
KERNEL_OBJECTS = \
$(patsubst %.c, $(BUILD_DIRECTORY)/%.o, $(KERNEL_SOURCES)) \
$(patsubst %.s, $(BUILD_DIRECTORY)/%.s.o, $(KERNEL_ASSEMBLY_SOURCES)) \
$(patsubst libraries/%.c, $(BUILD_DIRECTORY)/kernel/%.o, $(KERNEL_LIBRARIES_SOURCES))
OBJECTS += $(KERNEL_OBJECTS)
$(BUILD_DIRECTORY)/kernel/%.o: libraries/%.c
$(DIRECTORY_GUARD)
@echo [KERNEL] [CC] $<
@$(CC) $(CFLAGS) -ffreestanding -nostdlib -c -o $@ $<
$(BUILD_DIRECTORY)/kernel/%.o: kernel/%.c
$(DIRECTORY_GUARD)
@echo [KERNEL] [CC] $<
@$(CC) $(CFLAGS) -ffreestanding -nostdlib -c -o $@ $<
$(BUILD_DIRECTORY)/kernel/%.s.o: kernel/%.s
$(DIRECTORY_GUARD)
@echo [KERNEL] [AS] $<
@$(AS) $(ASFLAGS) $^ -o $@
$(KERNEL_BINARY): $(KERNEL_OBJECTS)
$(DIRECTORY_GUARD)
@echo [KERNEL] [LD] $(KERNEL_BINARY)
@$(LD) $(LDFLAGS) -T kernel/link.ld -o $@ $^
# --- CRTs ----------------------------------------------- #
CRTS= \
$(BUILD_DIRECTORY_LIBS)/crt0.o \
$(BUILD_DIRECTORY_LIBS)/crti.o \
$(BUILD_DIRECTORY_LIBS)/crtn.o
$(BUILD_DIRECTORY_LIBS)/crt0.o: libraries/crt0.s
$(DIRECTORY_GUARD)
@echo [AS] $^
@$(AS) $(ASFLAGS) -o $@ $^
$(BUILD_DIRECTORY_LIBS)/crti.o: libraries/crti.s
$(DIRECTORY_GUARD)
@echo [AS] $^
@$(AS) $(ASFLAGS) -o $@ $^
$(BUILD_DIRECTORY_LIBS)/crtn.o: libraries/crtn.s
$(DIRECTORY_GUARD)
@echo [AS] $^
@$(AS) $(ASFLAGS) -o $@ $^
# --- Libraries ------------------------------------------ #
define LIB_TEMPLATE =
$(1)_ARCHIVE = $(BUILD_DIRECTORY_LIBS)/lib$($(1)_NAME).a
$(1)_SOURCES = \
$$(wildcard libraries/lib$($(1)_NAME)/*.c) \
$$(wildcard libraries/lib$($(1)_NAME)/*/*.c)
$(1)_OBJECTS = $$(patsubst libraries/%.c, $(BUILD_DIRECTORY)/%.o, $$($(1)_SOURCES))
OBJECTS += $$($(1)_OBJECTS)
ICONS += $$($(1)_ICONS)
$$($(1)_ARCHIVE): $$($(1)_OBJECTS)
$$(DIRECTORY_GUARD)
@echo [LIB$(1)] [AR] $$@
@$(AR) $(ARFLAGS) $$@ $$^
LIBS_OBJECTS += $$($(1)_OBJECTS)
LIBS_ARCHIVES += $$($(1)_ARCHIVE)
$(BUILD_DIRECTORY)/lib$($(1)_NAME)/%.o: libraries/lib$($(1)_NAME)/%.c
$$(DIRECTORY_GUARD)
@echo [LIB$(1)] [CC] $$<
@$(CC) $(CFLAGS) $($(1)_CFLAGS) -c -o $$@ $$<
endef
-include libraries/*/.build.mk
$(foreach lib, $(LIBS), $(eval $(call LIB_TEMPLATE,$(lib))))
# --- Coreutils ------------------------------------------ #
define UTIL_TEMPLATE =
$(1)_BINARY = $(BUILD_DIRECTORY_UTILS)/$($(1)_NAME)
$(1)_SOURCE = coreutils/$($(1)_NAME).c
$(1)_OBJECT = $$(patsubst coreutils/%.c, $$(BUILD_DIRECTORY)/%.o, $$($(1)_SOURCE))
UTILS_BINARIES += $$($(1)_BINARY)
OBJECTS += $$($(1)_OBJECT)
$$($(1)_BINARY): $$($(1)_OBJECT) $$(patsubst %, $$(BUILD_DIRECTORY_LIBS)/lib%.a, $$($(1)_LIBS) system) $(CRTS)
$$(DIRECTORY_GUARD)
@echo [$(1)] [LD] $($(1)_NAME)
@$(CC) $(LDFLAGS) -o $$@ $$($(1)_OBJECT) $$(patsubst %, -l%, $$($(1)_LIBS))
$$($(1)_OBJECT): $$($(1)_SOURCE)
$$(DIRECTORY_GUARD)
@echo [$(1)] [CC] $$<
@$(CC) $(CFLAGS) -c -o $$@ $$<
endef
-include coreutils/.build.mk
$(foreach util, $(UTILS), $(eval $(call UTIL_TEMPLATE,$(util))))
# --- Applications --------------------------------------- #
define APP_TEMPLATE =
$(1)_BINARY = $(BUILD_DIRECTORY_APPS)/$($(1)_NAME)
$(1)_SOURCES = $$(wildcard applications/$($(1)_NAME)/*.c) \
$$(wildcard applications/$($(1)_NAME)/*/*.c)
$(1)_OBJECTS = $$(patsubst applications/%.c, $$(BUILD_DIRECTORY)/%.o, $$($(1)_SOURCES))
OBJECTS += $$($(1)_OBJECTS)
ICONS += $$($(1)_ICONS)
$$($(1)_BINARY): $$($(1)_OBJECTS) $$(patsubst %, $$(BUILD_DIRECTORY_LIBS)/lib%.a, $$($(1)_LIBS) system) $(CRTS)
$$(DIRECTORY_GUARD)
@echo [$(1)] [LD] $($(1)_NAME)
@$(CC) $(LDFLAGS) -o $$@ $$($(1)_OBJECTS) $$(patsubst %, -l%, $$($(1)_LIBS))
APPS_OBJECTS += $$($(1)_OBJECTS)
APPS_BINARIES += $$($(1)_BINARY)
$$(BUILD_DIRECTORY)/$$($(1)_NAME)/%.o: applications/$$($(1)_NAME)/%.c
$$(DIRECTORY_GUARD)
@echo [$(1)] [CC] $$<
@$(CC) $(CFLAGS) -c -o $$@ $$<
endef
-include applications/*/.build.mk
$(foreach app, $(APPS), $(eval $(call APP_TEMPLATE,$(app))))
# --- Icons ---------------------------------------------- #
ICONS_SVGs = $(patsubst %, thirdparty/icons/svg/%.svg, $(ICONS))
ICONS_PNGs = $(patsubst thirdparty/icons/svg/%.svg, $(SYSROOT)/res/icons/%.png, $(ICONS_SVGs))
list_icon:
@echo $(ICONS_PNGs)
$(SYSROOT)/res/icons/%.png: thirdparty/icons/svg/%.svg
$(DIRECTORY_GUARD)
@echo [ICON] $(notdir $@)
@inkscape --export-filename=$@ -w 18 -h 18 $< || \
inkscape --export-png $@ -w 18 -h 18 $< 1>/dev/null
# --- Ramdisk -------------------------------------------- #
RAMDISK=$(BOOTROOT)/boot/ramdisk.tar
SYSROOT_CONTENT=$(wildcard sysroot/*) $(wildcard sysroot/*/*) $(wildcard sysroot/*/*/*)
$(RAMDISK): $(CRTS) $(LIBS_ARCHIVES) $(UTILS_BINARIES) $(APPS_BINARIES) $(SYSROOT_CONTENT) $(ICONS_PNGs)
$(DIRECTORY_GUARD)
@echo [TAR] $@
@mkdir -p \
$(SYSROOT)/dev \
$(SYSROOT)/res \
$(SYSROOT)/srv \
$(SYSROOT)/sys
@cp -r sysroot/* $(SYSROOT)/
@cd $(SYSROOT); tar -cf $@ *
# --- Bootdisk ------------------------------------------- #
BOOTDISK=$(BUILD_DIRECTORY)/bootdisk.iso
$(BOOTDISK): $(RAMDISK) $(KERNEL_BINARY) grub.cfg
$(DIRECTORY_GUARD)
@echo [GRUB-MKRESCUE] $@
@mkdir -p $(BOOTROOT)/boot/grub
@cp grub.cfg $(BOOTROOT)/boot/grub/
@grub-mkrescue -o $@ $(BOOTROOT) || grub2-mkrescue -o $@ $(BOOTROOT)
# --- Phony ---------------------------------------------- #
.PHONY: all
all: $(BOOTDISK)
.PHONY: run
run: run-qemu
.PHONY: run-qemu
run-qemu: $(BOOTDISK)
@echo [QEMU] $^
$(QEMU) -cdrom $^ $(QEMUFLAGS) $(QEMUEXTRA) -enable-kvm || \
$(QEMU) -cdrom $^ $(QEMUFLAGS) $(QEMUEXTRA)
.PHONY: run-vbox
run-vbox: $(BOOTDISK)
VBoxManage unregistervm --delete "skiftOS-dev" || echo "Look like it's the fist time you are running this command, this is fine"
VBoxManage createvm \
--name skiftOS-dev \
--ostype Other \
--register \
--basefolder $(shell pwd)/vm
VBoxManage modifyvm \
skiftOS-dev \
--memory 512
VBoxManage storagectl \
skiftOS-dev \
--name IDE \
--add ide \
VBoxManage storageattach \
skiftOS-dev \
--storagectl IDE \
--port 0 \
--device 0 \
--type dvddrive \
--medium $(BOOTDISK)
VBoxManage startvm skiftOS-dev --type gui
sync:
rm $(BOOTDISK) $(RAMDISK)
make $(BOOTDISK)
.PHONY: clean
clean:
rm -rf $(BUILD_DIRECTORY)
-include $(OBJECTS:.o=.d)