-
Notifications
You must be signed in to change notification settings - Fork 6
/
Makefile
277 lines (222 loc) · 8.88 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
# Copyright (c) 2011 by Markus Duft <[email protected]>
# This file is part of the 'tachyon' operating system.
#.----------------------------------.
#| The all target, keep it first! |
#'----------------------------------'
all: all-kernel all-tools all-libs all-apps
.PHONY: all
all-tools:
@for mf in $$(find $(SOURCEDIR)/tools/apps -name Makefile); do $(MAKE) -f $${mf}; done
all-libs: all-tools
@for mf in $$(find $(SOURCEDIR)/libs -name Makefile); do $(MAKE) -f $${mf}; done
all-apps: all-libs
@for mf in $$(find $(SOURCEDIR)/apps -name Makefile); do $(MAKE) -f $${mf}; done
#.----------------------------------.
#| Include the platform config. |
#'----------------------------------'
include config/config.mk
#.----------------------------------.
#| Set kernel specific variables |
#'----------------------------------'
KERNEL_SILENT := 0
KERNEL_CMD := -deflog=trace
ARCH_LINKSCRIPT := $(CONFIGDIR)/$(ARCH).ld
ARCH_PPLSCRIPT := $(BUILDDIR)/$(ARCH).ld
GDB := gdb
BASE_CPPFLAGS := -Wall -Wextra -I$(SOURCEDIR)/src
ifeq ($(KERNEL_SILENT),1)
BASE_CPPFLAGS += -DLOG_SILENT -Wno-error=unused-but-set-variable -Wno-error=unused-variable
endif
BASE_CFLAGS := $(BASE_CPPFLAGS) -Werror -ffreestanding -nostartfiles -nostdlib -Wno-unused-parameter -std=gnu99 -Wno-cast-function-type
BASE_CXXFLAGS := $(BASE_CFLAGS) -fno-rtti -fno-exceptions -Wold-style-cast
BASE_LDFLAGS = -T $(ARCH_PPLSCRIPT) -Map=$(KERNEL).map
BASE_QFLAGS := -smp 2 -serial file:$(BUILDDIR)/serial-qemu.log -serial file:$(BUILDDIR)/com2-serial-qemu.log
BASE_GDBFLAGS :=
GRUB2_CFG := $(BUILDDIR)/boot/grub/grub.cfg
GRUB2_PXE = $(BUILDDIR)/boot/grub/i386-pc/core.0
GRUB2_ISO = $(BUILDDIR)/$(notdir $(KERNEL))-grub2.iso
#.----------------------------------.
#| Build internal/accumulated vars. |
#'----------------------------------'
KCPPFLAGS := $(BASE_CPPFLAGS) $(CPPFLAGS) $(KERNEL_CPPFLAGS)
KCFLAGS := $(BASE_CFLAGS) $(CFLAGS) $(KERNEL_CFLAGS)
KLDFLAGS := $(BASE_LDFLAGS) $(LDFLAGS) $(KERNEL_LDFLAGS)
#.----------------------------------.
#| Specialized Rules |
#'----------------------------------'
$(ARCH_PPLSCRIPT): $(ARCH_LINKSCRIPT) config/common-layout.ld
@-$(MAKE_BDIR)
@if test $(VERBOSE) = 0; then \
echo "[CPP ] $(notdir $@)"; \
else \
echo "$(CPP) $(KCPPFLAGS) -P -D__ASM__ -o \"$@\" \"$<\""; \
fi
@$(CPP) $(KCPPFLAGS) -P -D__ASM__ -o "$@" "$<"
$(KERNEL): $(KERNEL).sym.o
@-$(MAKE_BDIR)
@if test $(VERBOSE) = 0; then \
echo "[LD ] $(notdir $@)"; \
else \
echo "$(LD) $(KLDFLAGS) -o \"$@\" $(OBJECTS)"; \
fi
@$(LD) $(KLDFLAGS) -o "$@" $(filter-out %ksym_dummy.o,$(OBJECTS)) $(KERNEL).sym.o
$(KERNEL).dbg: $(KERNEL)
@if test $(VERBOSE) = 0; then \
echo "[SPLT] $(notdir $@)"; \
else \
echo "$(OBJCOPY) --only-keep-debug \"$<\" \"$@\""; \
echo "$(OBJCOPY) --strip-debug \"$<\""; \
echo "$(OBJCOPY) --add-gnu-debuglink=\"$@\" \"$<\""; \
echo "touch \"$@\""; \
fi
@$(OBJCOPY) --only-keep-debug "$<" "$@"
@$(OBJCOPY) --strip-debug "$<"
@$(OBJCOPY) --add-gnu-debuglink="$@" "$<"
@touch "$@"
$(KERNEL).rd: all-tools all-apps
@$(RD) c $(KERNEL).rd README LICENSE
@printf "ramdisk ready:\n"
@$(RD) t $(KERNEL).rd | while read line; do echo " $$line"; done
all-kernel: $(KERNEL) $(KERNEL).dbg $(KERNEL).rd
@printf "kernel ready: "
@(cd $(dir $(KERNEL)) && ls -hs $(notdir $(KERNEL)))
#.----------------------------------.
#| Kernel Symbol Table dumping |
#'----------------------------------'
$(KERNEL).sym.dmp: $(OBJECTS) $(ARCH_PPLSCRIPT)
@-rm -f "$@"
@-rm -f "[email protected]"
@$(LD) $(KLDFLAGS) -o "[email protected]" $(OBJECTS)
@echo "declare -a names" >> "$@"
@echo "declare -a sizes" >> "$@"
@$(NM) -n -S --defined-only "[email protected]" | sed -e 's,\(^[0-9a-fA-F]*[ \t]*\)\([tT].*$$\),\10a \2,g' | grep -E '^[0-9a-fA-F]*[ \t]+[0-9a-fA-F]*[ \t]+[Tt]' | while read addr size type name; do \
test -z "$${size}" && size=2; \
test -z "$${idx}" && idx=0; \
echo "names[$${idx}]=\"$${name}\"" >> "$@"; \
echo "addrs[$${idx}]=$${addr}" >> "$@"; \
echo "sizes[$${idx}]=$${size}" >> "$@"; \
((idx += 1)); \
done;
@-rm -f "[email protected]"
$(KERNEL).sym.S: $(KERNEL).sym.dmp
@-rm -f "$@"
@echo "# GENERATED BY THE MAKEFILE!" >> "$@"
@echo ".section .rodata" >> "$@"
@source "$<"; \
x=0; while [[ $${x} -lt $${#names[@]} ]]; do \
echo ".sym$${x}: .string \"$${names[$${x}]}\"" >> "$@"; ((x += 1)); \
done;
@echo ".global ksym_table" >> "$@"
@echo "ksym_table:" >> "$@"
@source "$<"; \
case "$(ARCH)" in x86) op=".long" ;; x86_64) op=".quad" ;; esac; \
x=0; while [[ $${x} -lt $${#names[@]} ]]; do \
if [[ $${x} -gt 0 ]]; then \
((y=x-1)); \
echo "ibase=16" > "[email protected]"; \
echo "$${addrs[$${x}]} - ($${addrs[$${y}]}+$${sizes[$${y}]})" | tr '[a-z]' '[A-Z]' >> "[email protected]"; \
echo "quit" >> "[email protected]"; \
cross=`bc -s -q "[email protected]"`; \
addrs[$${x}]=$${addrs[$${x}]}; \
sizes[$${x}]=$${sizes[$${x}]}; \
if [[ $${cross} -lt 0 && $${cross} -gt -11 ]]; then \
sizes[$${y}]=$${sizes[$${y}]}$${cross}; \
else \
sizes[$${y}]="$${sizes[$${y}]}-1"; \
fi; \
rm -f "[email protected]"; \
fi; \
((x += 1)); \
printf "[SYM ] Generated %5d of %5d\r" $${x} $${#names[@]}; \
done; \
printf "\n"; \
x=0; while [[ $${x} -lt $${#names[@]} ]]; do \
echo " $${op} 0x$${addrs[$${x}]}" >> "$@"; \
echo " $${op} 0x$${sizes[$${x}]}" >> "$@"; \
echo " $${op} .sym$${x}" >> "$@"; \
((x += 1)); \
printf "[SYM ] Wrote %5d of %5d\r" $${x} $${#names[@]}; \
done; \
printf "\n";
$(KERNEL).bochs-sym: $(KERNEL).sym.dmp
@-rm -f "$@"
@echo "[BSYM] $(notdir $(KERNEL))";
@source "$<"; \
x=0; while [[ $${x} -lt $${#names[@]} ]]; do \
echo "$${addrs[$${x}]} $${names[$${x}]}" >> "$@"; \
((x += 1)); \
done;
$(KERNEL).sym.o: $(KERNEL).sym.S
@-$(MAKE_BDIR)
@if test $(VERBOSE) = 0; then \
echo "[SYM ] $(notdir $(KERNEL))"; \
else \
echo "$(CC) $(KCFLAGS) -D__ASM__ -c -o \"$@\" \"$<\""; \
fi
@$(CC) $(KCFLAGS) -D__ASM__ -c -o "$@" "$<"
#.----------------------------------.
#| Helper rules |
#'----------------------------------'
#
# qemu direct kernel boot.
#
KQFLAGS := $(BASE_QFLAGS) $(QFLAGS)
qemu: all-kernel
@arch=$(ARCH); arch=$${arch%%-*}; qe=$$(type -p qemu-system-$${arch}); test -z "$${qe}" && qe=qemu; \
echo "using $${qe}..."; $${qe} $(KQFLAGS) -kernel $(KERNEL) -append "$(KERNEL_CMD)" -initrd "$(KERNEL).rd";
qemu-dbg:
@$(MAKE) -f $(SOURCEDIR)/Makefile qemu QFLAGS="$(QFLAGS) -s -S"
gdb:
@gdb=$$(type -p cgdb); test -x "$${gdb}" || gdb=$(GDB); test -x "$${gdb}" || gdb=gdb; \
echo "using $${gdb}..."; \
case "$${gdb}" in *"cgdb") gdb="$${gdb} -d $(GDB)";; esac; \
$${gdb} -x $(SOURCEDIR)/config/misc/gdb-qemu.gdbinit $(KERNEL)
#
# grub2 network boot
#
qemu-netboot-grub2: all-kernel $(GRUB2_CFG) $(GRUB2_PXE)
@arch=$(ARCH); arch=$${arch%%-*}; qe=$$(type -p qemu-system-$${arch}); test -z "$${qe}" && qe=qemu; \
echo "using $${qe}..."; $${qe} $(KQFLAGS) -net nic -net user,tftp=$(BUILDDIR),bootfile=$(subst $(BUILDDIR),,$(GRUB2_PXE)) -boot n;
qemu-netboot-grub2-dbg:
@$(MAKE) -f $(SOURCEDIR)/Makefile qemu-netboot-grub2 QFLAGS="$(QFLAGS) -s -S"
#
# direct network boot - multiboot loaded by gPXE.
#
qemu-netboot: all-kernel
@arch=$(ARCH); arch=$${arch%%-*}; qe=$$(type -p qemu-system-$${arch}); test -z "$${qe}" && qe=qemu; \
echo "using $${qe}..."; $${qe} $(KQFLAGS) -net nic -net user,tftp=$(BUILDDIR),bootfile=/$(notdir $(KERNEL)) -boot n;
qemu-netboot-dbg:
@$(MAKE) -f $(SOURCEDIR)/Makefile qemu-netboot QFLAGS="$(QFLAGS) -s -S"
#
# cdrom iso boot.
#
qemu-cd: $(GRUB2_ISO)
@arch=$(ARCH); arch=$${arch%%-*}; qe=$$(type -p qemu-system-$${arch}); test -z "$${qe}" && qe=qemu; \
echo "using $${qe}..."; $${qe} $(KQFLAGS) -cdrom $(GRUB2_ISO) -boot d;
qemu-cd-dbg:
@$(MAKE) -f $(SOURCEDIR)/Makefile qemu-cd-dbg QFLAGS="$(QFLAGS) -s -S"
#
# BOCHS related stuff
#
BOCHS_RC := $(BUILDDIR)/bochsrc.txt
$(BOCHS_RC): $(SOURCEDIR)/config/misc/bochsrc.in $(GRUB2_ISO) $(KERNEL).bochs-sym
@-$(MAKE_BDIR)
@sed -e "s,@GRUB2_ISO@,$(GRUB2_ISO),g" -e "s,@SERIAL_FILE@,$(BUILDDIR)/serial-bochs.log,g" -e "s,@LOG_FILE@,$(BUILDDIR)/debug-bochs.log,g" -e "s,@SYM_FILE@,$(KERNEL).bochs-sym,g" < "$<" > "$@"
bochs-cd: $(BOCHS_RC) $(GRUB2_ISO)
@bochs="$$(type -p bochs)"; test -x "$${bochs}" || { echo "bochs not found!"; exit 1; }; \
$${bochs} -qf $(BOCHS_RC) || true
#
# GRUB2 related stuff
#
$(GRUB2_CFG): $(SOURCEDIR)/config/misc/grub2.cfg
@-$(MAKE_BDIR)
@sed -e "s,@KERNEL@,$(notdir $(KERNEL)),g" < "$<" > "$@"
$(GRUB2_PXE):
@mknet=$$(type -p grub-mknetdir); test -x "$${mknet}" || { echo "grub-mknetdir not found!"; exit 1; }; \
$${mknet} --net-directory=$(BUILDDIR);
$(GRUB2_ISO): $(GRUB2_CFG) $(KERNEL)
@-rm -f "$@"
@mkresc=$$(type -p grub-mkrescue); test -x "$${mkresc}" || { echo "grub-mkrescue not found!"; exit 1; }; \
$${mkresc} -o "$@" --modules="biosdisk iso9660 multiboot" $(BUILDDIR)
@echo "iso image \"$@\" built..."
iso: $(GRUB2_ISO)