-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
382 lines (304 loc) · 9.39 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Makefile
##############################
# Default config definitions #
##############################
ARCH ?= x86
BOOTLOADER ?= izixboot
#####################
# Utility functions #
#####################
define uniq =
$(eval seen :=)
$(foreach _,$1,$(if $(filter $_,${seen}),,$(eval seen += $_)))
${seen}
endef
#############################
# Variables generate tagets #
#############################
# Earlier boot objects
object_start := kernel/arch/$(ARCH)/boot/$(BOOTLOADER)_start.o
objects_boot :=
objects_boot := $(addprefix kernel/boot/,$(objects_boot))
ifeq (x86,$(ARCH))
objects_boot_x86 := $(BOOTLOADER)_main.o null_handler.o
objects_boot_x86 := $(addprefix kernel/arch/$(ARCH)/boot/,$(objects_boot_x86))
objects_boot := $(objects_boot) $(objects_boot_x86)
endif
# Our custom .init and .fini sections.
objects_source_crt := crti.o crtn.o
ifeq (izixboot,$(BOOTLOADER))
objects_source_crt := $(objects_source_crt) crti_zero_bss.o
endif
objects_source_crt := $(addprefix kernel/arch/$(ARCH)/crt/,$(objects_source_crt))
# GCCs .init and .fini sections.
objects_bin_crt := crtbegin.o crtend.o
objects_bin_crt := $(addprefix kernel/arch/$(ARCH)/crt/,$(objects_bin_crt))
# Our VGA drivers
objects_drivers_video_vga := vga_text.o vga_cursor.o
objects_drivers_video_vga := $(addprefix kernel/drivers/video/vga/,$(objects_drivers_video_vga))
# Our video drivers.
objects_drivers_video := $(objects_drivers_video_vga)
# Our TTY drivers.
objects_drivers_tty := tty_chardev_driver.o tty_vga_text.o
objects_drivers_tty := $(addprefix kernel/drivers/tty/,$(objects_drivers_tty))
# All of our drivers.
objects_drivers := $(objects_drivers_video) $(objects_drivers_tty)
ifeq (x86,$(ARCH))
objects_drivers_x86_pic_8259 := pic_8259.o
objects_drivers_x86_pic_8259 := \
$(addprefix kernel/arch/$(ARCH)/drivers/pic_8259/,$(objects_drivers_x86_pic_8259))
objects_drivers_x86_pit_8253 := pit_8253.o
objects_drivers_x86_pit_8253 := \
$(addprefix kernel/arch/$(ARCH)/drivers/pit_8253/,$(objects_drivers_x86_pit_8253))
objects_drivers_x86_cmos := cmos.o nmi.o rtc.o
objects_drivers_x86_cmos := \
$(addprefix kernel/arch/$(ARCH)/drivers/cmos/,$(objects_drivers_x86_cmos))
objects_drivers := \
$(objects_drivers) \
$(objects_drivers_x86_pic_8259) \
$(objects_drivers_x86_pit_8253) \
$(objects_drivers_x86_cmos)
endif
objects_time := clock.o
objects_time := $(addprefix kernel/time/,$(objects_time))
ifeq (x86,$(ARCH))
objects_time_x86 := clock_tick.o
objects_time_x86 := \
$(addprefix kernel/arch/$(ARCH)/time/,$(objects_time_x86))
objects_time := $(objects_time) $(objects_time_x86)
endif
objects_dev := dev.o
objects_dev := $(addprefix kernel/dev/,$(objects_dev))
# Our kprint subsystem.
objects_kprint := kprint.o
objects_kprint := $(addprefix kernel/kprint/,$(objects_kprint))
objects_kpanic := kpanic.o
objects_kpanic := $(addprefix kernel/kpanic/,$(objects_kpanic))
objects_mm := freemem.o malloc.o
objects_mm := $(addprefix kernel/mm/,$(objects_mm))
ifeq (x86,$(ARCH))
objects_x86_mm := gdt.o e820.o paging.o
objects_x86_mm := $(addprefix kernel/arch/$(ARCH)/mm/,$(objects_x86_mm))
objects_mm := $(objects_mm) $(objects_x86_mm)
endif
objects_sched := spinlock.o mutex.o kthread.o
objects_sched := $(addprefix kernel/sched/,$(objects_sched))
objects_sched_asm :=
objects_sched_asm := $(addprefix kernel/sched/,$(objects_sched_asm))
ifeq (x86,$(ARCH))
objects_x86_sched := tss.o kthread_task.o kthread_preempt.o
objects_x86_sched := $(addprefix kernel/arch/$(ARCH)/sched/,$(objects_x86_sched))
objects_sched := $(objects_sched) $(objects_x86_sched)
objects_x86_sched_asm := kthread_switch.o kthread_bootstrap.o
objects_x86_sched_asm := $(addprefix kernel/arch/$(ARCH)/sched/,$(objects_x86_sched_asm))
objects_sched_asm := $(objects_sched_asm) $(objects_x86_sched_asm)
endif
objects_int :=
objects_int := $(addprefix kernel/int/,$(objects_int))
ifeq (x86,$(ARCH))
objects_x86_int := idt.o int_selector_ec.o
objects_x86_int := $(addprefix kernel/arch/$(ARCH)/int/,$(objects_x86_int))
objects_int := $(objects_int) $(objects_x86_int)
endif
objects_isr :=
objects_isr := $(addprefix kernel/isr/,$(objects_isr))
ifeq (x86,$(ARCH))
objects_x86_isr := df.o np.o gp.o irq.o
objects_x86_isr := $(addprefix kernel/arch/$(ARCH)/isr/,$(objects_x86_isr))
objects_isr := $(objects_isr) $(objects_x86_isr)
endif
objects_irq :=
objects_irq := $(addprefix kernel/irq/,$(objects_irq))
ifeq (x86,$(ARCH))
objects_x86_irq := irq.o
objects_x86_irq := $(addprefix kernel/arch/$(ARCH)/irq/,$(objects_x86_irq))
objects_irq := $(objects_irq) $(objects_x86_irq)
endif
# libk string objects
objects_libk_string := memchr.o memcpy.o memset.o strcat.o strlen.o
objects_libk_string := $(addprefix libk/string/,$(objects_libk_string))
# libk strings objects
objects_libk_strings := ffs.o
objects_libk_strings := $(addprefix libk/strings/,$(objects_libk_strings))
# libk format objects
objects_libk_format := pad.o itoa.o sprintf.o numeric.o
objects_libk_format := $(addprefix libk/format/,$(objects_libk_format))
objects_libk_collections := bintree.o linked_list.o sparse_collection.o
objects_libk_collections := $(addprefix libk/collections/,$(objects_libk_collections))
# All Libk format objects
objects_libk := \
$(objects_libk_string) \
$(objects_libk_strings) \
$(objects_libk_format) \
$(objects_libk_collections)
# Libk library
libk := libk.a
libk := $(addprefix libk/,$(libk))
# Objects based on build tasks.
asm_source_objects := \
$(object_start) \
$(objects_source_crt) \
$(objects_isr) \
$(objects_sched_asm)
c_source_objects := \
$(objects_boot) \
$(objects_drivers) \
$(objects_time) \
$(objects_dev) \
$(objects_kprint) \
$(objects_kpanic) \
$(objects_mm) \
$(objects_sched) \
$(objects_int) \
$(objects_irq) \
$(objects_libk)
# Object dirs
all_objects := $(objects_bin_crt) $(asm_source_objects) $(c_source_objects)
all_object_dirs := $(call uniq,$(realpath $(dir $(all_objects))))
all_object_dirs := $(all_object_dirs:$(CURDIR)/%=%)
# Clean targets
CLEAN_object_dirs := $(addprefix clean_,$(all_object_dirs))
# Our linker script
linker_script := $(BOOTLOADER).ld
linker_script := $(addprefix lds/,$(linker_script))
#########################
# Variables for linking #
#########################
# The very first object.
objects_first := $(object_start)
# .init section
objects_begin_crt := crti.o
ifeq (izixboot,$(BOOTLOADER))
objects_begin_crt := $(objects_begin_crt) crti_zero_bss.o
endif
objects_begin_crt := $(objects_begin_crt) crtbegin.o
objects_begin_crt := $(addprefix kernel/arch/$(ARCH)/crt/,$(objects_begin_crt))
# Kernel objects.
objects_kernel := \
$(objects_boot) \
$(objects_drivers) \
$(objects_time) \
$(objects_dev) \
$(objects_mm) \
$(objects_sched) \
$(objects_sched_asm) \
$(objects_int) \
$(objects_isr) \
$(objects_irq) \
$(objects_kprint) \
$(objects_kpanic)
# Libraries to link to.
libs := \
-nostdlib \
-lk \
-lgcc
# .fini section
objects_end_crt := crtend.o crtn.o
objects_end_crt := $(addprefix kernel/arch/$(ARCH)/crt/,$(objects_end_crt))
# The link order.
link_order := \
$(objects_first) \
$(objects_begin_crt) \
$(objects_kernel) \
$(libs) \
$(objects_end_crt)
#######################
# Toolchain variables #
#######################
# Date format
AMERICAN_DATE ?= true
# Our toolchain binaries.
CC ?= gcc
AR ?= ar
STRIP ?= strip
OBJCOPY ?= objcopy
# We will use $(CC) for linking and assembling.
# LD ?=
# AS ?=
# Our C compiler flags.
CFLAGS ?= \
-O2 -Wall -Wextra
CFLAGS := \
$(CFLAGS) \
-I./kernel/include \
-I./kernel/arch/$(ARCH)/include \
-I./libk/include \
-ffreestanding \
-Werror=format \
-DIZIX \
$(addprefix -DARCH_,$(shell echo $(ARCH) | tr a-z A-Z)) \
-DCOMPILE_YEAR=$(shell date +%y) \
-DCOMPILE_CENTURY=$(shell date +%C)
ifeq (x86,$(ARCH))
ifeq (izixboot,$(BOOTLOADER))
CFLAGS := \
$(CFLAGS) \
-DIZIX_SUPPORT_E820_3X
ifeq (true, $(AMERICAN_DATE))
CFLAGS := \
$(CFLAGS) \
-DAMERICAN_DATE
endif
endif
endif
# Our assembling flags.
ASFLAGS ?= \
-Wall -Wextra
# Our linker flags.
LDFLAGS := \
-L./libk \
-Wl,-T./$(linker_script)
STRIPFLAGS ?= \
--remove-section=.note.gnu.gold-version \
--remove-section=.comment \
--remove-section=.note \
--remove-section=.note.gnu.build-id \
--remove-section=.note.ABI-tag \
--strip-all \
--strip-unneeded
####################
# Start of targets #
####################
# Our default target must go first.
.PHONY: all
all: izix.kernel
# All our included dependancies.
include $(wildcard $(addsuffix /*.d,$(all_object_dirs)))
$(asm_source_objects):%.o:%.s
$(CC) $(ASFLAGS) -Wa,-I./$(dir $<) -c $< -o $@
$(c_source_objects):%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
$(objects_bin_crt):
OBJ=`$(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=$(shell basename $@)` && cp "$$OBJ" $@
$(libk): $(objects_libk)
$(AR) rcs $@ $(objects_libk)
izix.kernel: $(linker_script) $(object_start) $(objects_source_crt) $(objects_bin_crt) $(objects_kernel) $(libk)
$(CC) $(CFLAGS) $(LDFLAGS) \
$(link_order) \
-o $@
.PHONY: debug
debug: izix.debug
izix.debug: izix.kernel
$(OBJCOPY) --only-keep-debug \
izix.kernel izix.debug
.PHONY: clean
clean: clean_object_dirs clean_libk clean_izix.kernel clean_izix.debug
.PHONY: clean_object_dirs
clean_object_dirs: $(CLEAN_object_dirs)
.PHONY: $(CLEAN_object_dirs)
$(CLEAN_object_dirs):clean_%:%
rm -f $(addsuffix /*.o,$<)
.PHONY: clean_libk
clean_libk:
rm -f $(libk)
.PHONY: clean_izix.kernel
clean_izix.kernel:
rm -f izix.kernel
.PHONY: clean_izix.debug
clean_izix.debug:
rm -f izix.debug
.PHONY: strip
strip:
$(STRIP) $(STRIPFLAGS) \
izix.kernel
# vim: set ts=4 sw=4 noet syn=make: