-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
234 lines (186 loc) · 8.67 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
# -ffreestanding needed for kernel
CFLAGS=-ffreestanding -Wall -Wextra -Iinclude -std=gnu11 -Wshadow -Wstrict-overflow -fno-strict-aliasing -fno-omit-frame-pointer -g -include "include/_default.h"
ASFLAGS=
BUILDDIR=build
CONFIGFILE=config.cfg
LDFLAGS=
SED=sed
BUILDSYS:=$(shell uname)
ifeq ($(BUILDSYS),Darwin)
SED=gsed
endif
.DEFAULT_GOAL=all
# these are the required libraries that the kernel needs.
LIBRARIES=ds string
MAKEFILES=Makefile $(CONFIGFILE)
# read in configuration, and add to CFLAGS
include $(CONFIGFILE)
SYSROOT=$(CONFIG_BUILD_SYSROOT)
export SYSROOT
CFLAGS+=$(addprefix -D,$(shell cat $(CONFIGFILE) | $(SED) -e 's/=y/=1/g' -e 's/=n/=0/g' -e 's/\#.*$$//' -e '/^$$/d'))
ARCH=$(CONFIG_ARCH)
MACHINE=$(CONFIG_MACHINE)
C_SOURCES=
ASM_SOURCES=
ifeq ($(CONFIG_UBSAN),y)
CFLAGS+=-fsanitize=undefined -fstack-protector-all -fstack-check
endif
# these warnings pop up if we define asserts to nothing, so remove them
# # TODO
ifeq ($(CONFIG_DEBUG),n)
CFLAGS+=-Wno-unknown-warning-option -Wno-unused-variable -Wno-unused-parameter -Wno-unused-but-set-variable
endif
ifeq ($(CONFIG_BUILD_WERROR),y)
CFLAGS+=-Werror
endif
ifeq ($(CONFIG_PERF_FUNCTIONS),y)
ifeq ($(CONFIG_UBSAN),y)
$(error Cannot do both UBSAN and PERF at the same time)
endif
CFLAGS+=-finstrument-functions -finstrument-functions-exclude-file-list=kernel/debug,serial,printk,lib/string,panic,arch
endif
ifeq ($(CONFIG_BUILD_LTO),y)
ifeq ($(CONFIG_BUILD_CLANG),y)
$(error Cannot do LTO with clang)
endif
$(warning Link Time Optimization (LTO) is untested and unsupported)
CFLAGS+=-flto
LDFLAGS+=-flto
endif
CFLAGS+=-O$(CONFIG_BUILD_OPTIMIZATION)
include $(SYSROOT)/usr/src/include.mk
all: $(BUILDDIR)/kernel.elf $(BUILDDIR)/initrd.tar $(USRPROGS)
# get all normal kernel sources
include kernel/include.mk
ARCHDIR=arch/$(ARCH)
MACHINEDIR=machine/$(MACHINE)
# include the machine sources and the arch sources
# MACHINE must come first!
include $(MACHINEDIR)/include.mk
include $(ARCHDIR)/include.mk
ifeq ($(CONFIG_BUILD_SYSTEM_COMPILER),y)
TOOLCHAIN_PREFIX=
else
TOOLCHAIN_PREFIX:=$(TOOLCHAIN_PREFIX)-
endif
ifeq ($(CONFIG_BUILD_CLANG),y)
CC=clang -target $(TOOLCHAIN_PREFIX) -i$(SYSROOT) /home/dbittman/toolchain/install
else
CC=$(TOOLCHAIN_PREFIX)gcc
endif
AS=$(TOOLCHAIN_PREFIX)as
LD=$(TOOLCHAIN_PREFIX)gcc
MAKEFILES+=$(MACHINEDIR)/include.mk $(ARCHDIR)/include.mk $(SYSROOT)/usr/src/include.mk
# for each library that we're using, include their sources
$(foreach lib,$(LIBRARIES),$(eval include lib/$(lib)/include.mk))
MAKEFILES+=$(addsuffix /include.mk,$(addprefix lib/,$(LIBRARIES)))
#drivers
include drivers/include.mk
STARTFILE=$(shell $(LD) -print-file-name=crtbegin.o)
ENDFILE=$(shell $(LD) -print-file-name=crtend.o)
CFLAGS+=-I$(ARCHDIR)/include -I$(MACHINEDIR)/include
C_OBJECTS=$(addprefix $(BUILDDIR)/,$(C_SOURCES:.c=.o))
ASM_OBJECTS=$(addprefix $(BUILDDIR)/,$(ASM_SOURCES:.S=.o))
SOURCES=$(C_SOURCES) $(ASM_SOURCES)
OBJECTS=
ifneq ($(CRTIOBJ),)
OBJECTS+=$(BUILDDIR)/$(CRTIOBJ)
endif
OBJECTS+=$(STARTFILE) $(sort $(C_OBJECTS) $(ASM_OBJECTS)) $(ENDFILE)
ifneq ($(CRTNOBJ),)
OBJECTS+=$(BUILDDIR)/$(CRTNOBJ)
endif
listobj:
echo $(OBJECTS)
echo $(USRPROGS)
# this is the final kernel binary, including the symbol table.
$(BUILDDIR)/kernel.elf: $(OBJECTS) $(BUILDDIR)/link.ld $(MAKEFILES) $(BUILDDIR)/symbols.o
@echo -e "[LD]\t$(BUILDDIR)/kernel.elf"
@$(LD) $(LDFLAGS) -Wl,-dT,$(BUILDDIR)/link.ld,--export-dynamic -o $(BUILDDIR)/kernel.elf -nostdlib $(OBJECTS) -lgcc $(BUILDDIR)/symbols.o
# this is the generation of symbols.c, from its parts.
$(BUILDDIR)/symbols.c: kernel/syms.c.begin kernel/syms.c.end $(BUILDDIR)/kernel.sym.c
@echo -e "[GEN]\t$(BUILDDIR)/symbols.c"
@cat kernel/syms.c.begin $(BUILDDIR)/kernel.sym.c kernel/syms.c.end > $(BUILDDIR)/symbols.c
@echo >> $(BUILDDIR)/symbols.c
@echo 'size_t kernel_symbol_table_length = ' $$(wc -l < $(BUILDDIR)/kernel.sym.c) ';' >> $(BUILDDIR)/symbols.c
# ...and compile symbols.c
$(BUILDDIR)/symbols.o: $(BUILDDIR)/symbols.c
@echo -e "[CC]\t$(BUILDDIR)/symbols.o"
@$(CC) $(CFLAGS) -c -o $(BUILDDIR)/symbols.o $(BUILDDIR)/symbols.c
# here we dump the symbol table from the stage1 link, and process it into array initializers
# for C using sed and friends.
# this is one line because it's a memorial to the pain and suffering caused by how stupid
# the developers of binutils are.
$(BUILDDIR)/kernel.sym.c: $(BUILDDIR)/kernel.elf.stage1
@echo -e "[GEN]\t$(BUILDDIR)/kernel.sym.c"
@$(TOOLCHAIN_PREFIX)objdump -t $(BUILDDIR)/kernel.elf.stage1 | grep "^.* [lg]" | awk '{print $$1 " " $$(NF-1) " " $$NF}' | grep -v ".hidden" | $(SED) -rn 's|([0-9a-f]+) ([0-9a-f]+) ([a-zA-Z0-9_/\.]+)|{.value=0x\1, .size=0x\2, .name="\3"},|p' > $(BUILDDIR)/kernel.sym.c
# link the stage1 kernel binary, not including the symbol table
$(BUILDDIR)/kernel.elf.stage1: $(OBJECTS) $(BUILDDIR)/link.ld $(MAKEFILES)
@echo -e "[LD]\t$(BUILDDIR)/kernel.elf.stage1"
@$(LD) $(LDFLAGS) -Wl,-dT,$(BUILDDIR)/link.ld,--export-dynamic -o $(BUILDDIR)/kernel.elf.stage1 -nostdlib $(OBJECTS) -lgcc
.PHONY: lint clean test all flags
lint:
cppcheck --enable=style,warning,performance,portability,information,missingInclude -I include -I $(MACHINEDIR)/include -I $(ARCHDIR)/include . -I lib/fdt/include -i lib/fdt
autotest:
@./tests/build-opts-test.sh "" "-enable-kvm" "-smp 2" "-smp 4 -enable-kvm"
-include $(OBJECTS:.o=.d)
$(BUILDDIR)/initrd.tar: $(USRPROGS)
@echo -e "[TAR]\t$(BUILDDIR)/initrd.tar"
@-rm $(BUILDDIR)/initrd.tar 2>/dev/null
@tar --format=ustar -C initrd -c -f $(BUILDDIR)/initrd.tar .
$(BUILDDIR)/hd.img: $(USRPROGS)
truncate -s 1GB $(BUILDDIR)/hd.img
mke2fs -F $(BUILDDIR)/hd.img
mkdir -p $(BUILDDIR)/mnt
sudo mount $(BUILDDIR)/hd.img $(BUILDDIR)/mnt
sudo cp -r $(SYSROOT)/* $(BUILDDIR)/mnt/
sudo mkdir -p $(BUILDDIR)/mnt/bin
sudo mkdir -p $(BUILDDIR)/mnt/dev
sudo mkdir -p $(BUILDDIR)/mnt/tmp
sudo mkdir -p $(BUILDDIR)/mnt/etc
sudo mkdir -p $(BUILDDIR)/mnt/sys
sudo cp $(BUILDDIR)/kernel.elf $(BUILDDIR)/initrd.tar $(BUILDDIR)/mnt/sys/
sudo cp $(USRPROGS) $(BUILDDIR)/mnt/bin
sudo mkdir -p $(BUILDDIR)/mnt/usr/src/seakernel
sudo cp -r arch drivers include kernel lib machine tests tools config.cfg Makefile README.md $(BUILDDIR)/mnt/usr/src/seakernel
sudo $(SED) -s -i -e 's|CONFIG_BUILD_SYSROOT=.*|CONFIG_BUILD_SYSROOT=/|g' $(BUILDDIR)/mnt/usr/src/seakernel/config.cfg
sudo $(SED) -s -i -e 's|CONFIG_BUILD_SYSTEM_COMPILER=.*|CONFIG_BUILD_SYSTEM_COMPILER=y|g' $(BUILDDIR)/mnt/usr/src/seakernel/config.cfg
sudo $(SED) -s -i -e 's|CONFIG_BUILD_CLANG=.*|CONFIG_BUILD_CLANG=n|g' $(BUILDDIR)/mnt/usr/src/seakernel/config.cfg
echo "/lib:/usr/lib:/usr/lib64" | sudo tee $(BUILDDIR)/mnt/etc/ld-musl-$(ARCH).path > /dev/null
echo "nameserver 2001:4860:4860::8888" | sudo tee $(BUILDDIR)/mnt/etc/resolv.conf > /dev/null
sudo umount $(BUILDDIR)/mnt
newhd:
-rm $(BUILDDIR)/hd.img
$(MAKE) $(BUILDDIR)/hd.img
QEMU_AHCI=-device ahci,id=ahci0 -drive if=none,file=$(BUILDDIR)/hd.img,format=raw,id=drive-sata0-0-0 -device ide-drive,bus=ahci0.0,drive=drive-sata0-0-0,id=sata0-0-0
QEMU_NET=-netdev tap,id=n0,downscript=no,script=tools/qemu-ifup.sh -device e1000,netdev=n0 -net dump,file=qemu.pcap
QEMU_NETUSER=-net user,vlan=0,ipv6=on -device e1000,vlan=0 -net dump,file=qemu.pcap
test: $(BUILDDIR)/kernel.elf $(USRPROGS) $(BUILDDIR)/initrd.tar $(BUILDDIR)/hd.img
qemu-system-$(ARCH) -m 1024 -machine $(MACHINE) $(QEMU_FLAGS) -kernel $(BUILDDIR)/kernel.elf -serial stdio -initrd $(BUILDDIR)/initrd.tar $(QEMU_AHCI)
nettest: $(BUILDDIR)/kernel.elf $(USRPROGS) $(BUILDDIR)/initrd.tar $(BUILDDIR)/hd.img
sudo qemu-system-$(ARCH) -m 1024 -machine $(MACHINE) $(QEMU_FLAGS) -kernel $(BUILDDIR)/kernel.elf -serial stdio -initrd $(BUILDDIR)/initrd.tar $(QEMU_AHCI) $(QEMU_NET)
usernettest: $(BUILDDIR)/kernel.elf $(USRPROGS) $(BUILDDIR)/initrd.tar $(BUILDDIR)/hd.img
sudo qemu-system-$(ARCH) -m 1024 -machine $(MACHINE) $(QEMU_FLAGS) -kernel $(BUILDDIR)/kernel.elf -serial stdio -initrd $(BUILDDIR)/initrd.tar $(QEMU_AHCI) $(QEMU_NETUSER)
clean: clean_progs
-rm -r $(BUILDDIR)
flags:
echo $(CFLAGS)
echo $(LDFLAGS)
$(BUILDDIR)/%.o: %.c $(MAKEFILES)
@mkdir -p $(@D)
@echo -e "[CC]\t$@"
@$(CC) $(CFLAGS) -c -MD -MF $(BUILDDIR)/$*.d -o $@ $<
@mv -f $(BUILDDIR)/$*.d $(BUILDDIR)/$*.d.tmp
@$(SED) -e 's|.*:|$@:|' < $(BUILDDIR)/$*.d.tmp > $(BUILDDIR)/$*.d
@$(SED) -e 's/.*://' -e 's/\\$$//' < $(BUILDDIR)/$*.d.tmp | fmt -1 | \
$(SED) -e 's/^ *//' -e 's/$$/:/' >> $(BUILDDIR)/$*.d
@rm -f $(BUILDDIR)/$*.d.tmp
$(BUILDDIR)/%.o: %.S $(MAKEFILES)
@mkdir -p $(@D)
@echo -e "[AS]\t$@"
@$(CC) $(CFLAGS) -c -MD -MF $(BUILDDIR)/$*.d -o $@ $<
@mv -f $(BUILDDIR)/$*.d $(BUILDDIR)/$*.d.tmp
@$(SED) -e 's|.*:|$@:|' < $(BUILDDIR)/$*.d.tmp > $(BUILDDIR)/$*.d
@$(SED) -e 's/.*://' -e 's/\\$$//' < $(BUILDDIR)/$*.d.tmp | fmt -1 | \
$(SED) -e 's/^ *//' -e 's/$$/:/' >> $(BUILDDIR)/$*.d
@rm -f $(BUILDDIR)/$*.d.tmp