-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMakefile
457 lines (336 loc) · 11.7 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# sudo mount -t vboxsf ucore /home/moocos/share
PROJ := 8
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
SLASH := /
V := @
# try to infer the correct GCCPREFX
ifndef GCCPREFIX
GCCPREFIX := $(shell if i386-ucore-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
then echo 'i386-ucore-elf-'; \
elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
then echo ''; \
else echo "***" 1>&2; \
echo "*** Error: Couldn't find an i386-ucore-elf version of GCC/binutils." 1>&2; \
echo "*** Is the directory with i386-ucore-elf-gcc in your PATH?" 1>&2; \
echo "*** If your i386-ucore-elf toolchain is installed with a command" 1>&2; \
echo "*** prefix other than 'i386-ucore-elf-', set your GCCPREFIX" 1>&2; \
echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
# try to infer the correct QEMU
ifndef QEMU
QEMU := $(shell if which qemu-system-i386 > /dev/null; \
then echo 'qemu-system-i386'; exit; \
elif which i386-ucore-elf-qemu > /dev/null; \
then echo 'i386-ucore-elf-qemu'; exit; \
else \
echo "***" 1>&2; \
echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
echo "***" 1>&2; exit 1; fi)
endif
ifndef BOCHS
BOCHS := bochs
endif
# eliminate default suffix rules
.SUFFIXES: .c .S .h
# delete target files if there is an error (or make is interrupted)
.DELETE_ON_ERROR:
# try to generate a unique GDB port
GDBPORT := $(shell expr `id -u` % 5000 + 25000)
# define compiler and flags
HOSTCC := gcc
HOSTCFLAGS := -g -Wall -O2 -D_FILE_OFFSET_BITS=64
GDB := $(GCCPREFIX)gdb
CC := $(GCCPREFIX)gcc
CFLAGS := -fno-builtin -Wall -ggdb -m32 -gstabs -nostdinc $(DEFS) -Wunused-function -Wunused-variable
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
CTYPE := c S
LD := $(GCCPREFIX)ld
LDFLAGS := -m $(shell $(LD) -V | grep elf_i386 2>/dev/null)
LDFLAGS += -nostdlib
OBJCOPY := $(GCCPREFIX)objcopy
OBJDUMP := $(GCCPREFIX)objdump
COPY := cp
MKDIR := mkdir -p
MV := mv
RM := rm -f
AWK := awk
SED := sed
SH := sh
TR := tr
TOUCH := touch -c
OBJDIR := obj
BINDIR := bin
ALLOBJS :=
ALLDEPS :=
TARGETS :=
USER_PREFIX := __user_
include tools/function.mk
listf_cc = $(call listf,$(1),$(CTYPE))
# for cc
add_files_cc = $(call add_files,$(1),$(CC),$(CFLAGS) $(3),$(2),$(4))
create_target_cc = $(call create_target,$(1),$(2),$(3),$(CC),$(CFLAGS))
# for hostcc
add_files_host = $(call add_files,$(1),$(HOSTCC),$(HOSTCFLAGS),$(2),$(3))
create_target_host = $(call create_target,$(1),$(2),$(3),$(HOSTCC),$(HOSTCFLAGS))
cgtype = $(patsubst %.$(2),%.$(3),$(1))
objfile = $(call toobj,$(1))
asmfile = $(call cgtype,$(call toobj,$(1)),o,asm)
outfile = $(call cgtype,$(call toobj,$(1)),o,out)
symfile = $(call cgtype,$(call toobj,$(1)),o,sym)
filename = $(basename $(notdir $(1)))
ubinfile = $(call outfile,$(addprefix $(USER_PREFIX),$(call filename,$(1))))
# for match pattern
match = $(shell echo $(2) | $(AWK) '{for(i=1;i<=NF;i++){if(match("$(1)","^"$$(i)"$$")){exit 1;}}}'; echo $$?)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# include kernel/user
INCLUDE += libs/
CFLAGS += $(addprefix -I,$(INCLUDE))
LIBDIR += libs
$(call add_files_cc,$(call listf_cc,$(LIBDIR)),libs,)
# -------------------------------------------------------------------
# user programs
UINCLUDE += user/include/ \
user/libs/
USRCDIR += user
ULIBDIR += user/libs
UCFLAGS += $(addprefix -I,$(UINCLUDE))
USER_BINS :=
$(call add_files_cc,$(call listf_cc,$(ULIBDIR)),ulibs,$(UCFLAGS))
$(call add_files_cc,$(call listf_cc,$(USRCDIR)),uprog,$(UCFLAGS))
UOBJS := $(call read_packet,ulibs libs)
define uprog_ld
__user_bin__ := $$(call ubinfile,$(1))
USER_BINS += $$(__user_bin__)
$$(__user_bin__): tools/user.ld
$$(__user_bin__): $$(UOBJS)
$$(__user_bin__): $(1) | $$$$(dir $$$$@)
$(V)$(LD) $(LDFLAGS) -T tools/user.ld -o $$@ $$(UOBJS) $(1)
@$(OBJDUMP) -S $$@ > $$(call cgtype,$$<,o,asm)
@$(OBJDUMP) -t $$@ | sed '1,/SYMBOL TABLE/d; s/ .* / /; /^$$$$/d' > $$(call cgtype,$$<,o,sym)
endef
$(foreach p,$(call read_packet,uprog),$(eval $(call uprog_ld,$(p))))
# -------------------------------------------------------------------
# kernel
KINCLUDE += kern/debug/ \
kern/driver/ \
kern/trap/ \
kern/mm/ \
kern/libs/ \
kern/sync/ \
kern/fs/ \
kern/process/ \
kern/schedule/ \
kern/syscall/ \
kern/fs/swap/ \
kern/fs/vfs/ \
kern/fs/devs/ \
kern/fs/sfs/ \
kern/net/ \
kern/net/e1000
KSRCDIR += kern/init \
kern/libs \
kern/debug \
kern/driver \
kern/trap \
kern/mm \
kern/sync \
kern/fs \
kern/process \
kern/schedule \
kern/syscall \
kern/fs/swap \
kern/fs/vfs \
kern/fs/devs \
kern/fs/sfs \
kern/net \
kern/net/e1000 \
kern/net/tcpip/src \
kern/net/tcpip/src/sys \
kern/net/tcpip/src/sys/devs \
kern/net/tcpip/src/netapp \
kern/net/tcpip/src/dhcp \
kern/net/tcpip/src/tcp \
kern/net/tcpip/src/tcp/tcpsfm \
kern/net/tcpip/src/tcp/tcpostate \
kern/net/tcpip/src/tcp/tcpprocess \
kern/net/tcpip/src/dgram \
kern/net/tcpip/src/tcpd \
kern/net/tcpip/src/igmp \
kern/net/tcpip/src/lib \
kern/net/tcpip/src/net \
kern/net/tcpip/src/arp \
kern/net/tcpip/src/ip \
kern/net/tcpip/src/rip \
kern/net/tcpip/src/snmp \
kern/net/tcpip/src/udp \
kern/net/tcpip/src/icmp \
kern/net/tcpip/src/socket
KCFLAGS += $(addprefix -I,$(KINCLUDE))
$(call add_files_cc,$(call listf_cc,$(KSRCDIR)),kernel,$(KCFLAGS))
KOBJS = $(call read_packet,kernel libs)
# create kernel target
kernel = $(call totarget,kernel)
$(kernel): tools/kernel.ld
$(kernel): $(KOBJS)
@echo + ld $@
$(V)$(LD) $(LDFLAGS) -T tools/kernel.ld -o $@ $(KOBJS)
@$(OBJDUMP) -S $@ > $(call asmfile,kernel)
@$(OBJDUMP) -t $@ | $(SED) '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(call symfile,kernel)
$(call create_target,kernel)
# -------------------------------------------------------------------
# create bootblock
bootfiles = $(call listf_cc,boot)
$(foreach f,$(bootfiles),$(call cc_compile,$(f),$(CC),$(CFLAGS) -Os -nostdinc))
bootblock = $(call totarget,bootblock)
$(bootblock): $(call toobj,boot/bootasm.S) $(call toobj,$(bootfiles)) | $(call totarget,sign)
#$(bootblock): $(call toobj,boot/bootasm.S) $(call toobj,$(bootfiles))
@echo + ld $@
$(V)$(LD) $(LDFLAGS) -N -T tools/boot.ld $^ -o $(call toobj,bootblock)
@$(OBJDUMP) -S $(call objfile,bootblock) > $(call asmfile,bootblock)
@$(OBJCOPY) -S -O binary $(call objfile,bootblock) $(call outfile,bootblock)
@$(call totarget,sign) $(call outfile,bootblock) $(bootblock)
$(call create_target,bootblock)
# -------------------------------------------------------------------
# create 'sign' tools
$(call add_files_host,tools/sign.c,sign,sign)
$(call create_target_host,sign,sign)
# -------------------------------------------------------------------
# create 'mksfs' tools
$(call add_files_host,tools/mksfs.c,mksfs,mksfs)
$(call create_target_host,mksfs,mksfs)
# -------------------------------------------------------------------
# create ucore.img
UCOREIMG := $(call totarget,ucore.img)
$(UCOREIMG): $(kernel) $(bootblock)
$(V)dd if=/dev/zero of=$@ count=10000
$(V)dd if=$(bootblock) of=$@ conv=notrunc
$(V)dd if=$(kernel) of=$@ seek=1 conv=notrunc
$(call create_target,ucore.img)
# -------------------------------------------------------------------
# create swap.img
SWAPIMG := $(call totarget,swap.img)
$(SWAPIMG):
$(V)dd if=/dev/zero of=$@ bs=1M count=128
$(call create_target,swap.img)
# -------------------------------------------------------------------
# create sfs.img
SFSIMG := $(call totarget,sfs.img)
SFSBINS :=
SFSROOT := disk0
define fscopy
__fs_bin__ := $(2)$(SLASH)$(patsubst $(USER_PREFIX)%,%,$(basename $(notdir $(1))))
SFSBINS += $$(__fs_bin__)
$$(__fs_bin__): $(1) | $$$$(dir $@)
@$(COPY) $$< $$@
endef
$(foreach p,$(USER_BINS),$(eval $(call fscopy,$(p),$(SFSROOT)$(SLASH))))
$(SFSROOT):
$(V)$(MKDIR) $@
$(SFSIMG): $(SFSROOT) $(SFSBINS) | $(call totarget,mksfs)
$(V)dd if=/dev/zero of=$@ bs=1M count=128
@$(call totarget,mksfs) $@ $(SFSROOT)
$(call create_target,sfs.img)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
$(call finish_all)
IGNORE_ALLDEPS = clean \
dist-clean \
grade \
touch \
print-.+ \
run-.+ \
build-.+ \
sh-.+ \
script-.+ \
handin
ifeq ($(call match,$(MAKECMDGOALS),$(IGNORE_ALLDEPS)),0)
-include $(ALLDEPS)
endif
# files for grade script
TARGETS: $(TARGETS)
.DEFAULT_GOAL := TARGETS
PORT7 := $(shell expr $(GDBPORT) + 1)
PORT80 := $(shell expr $(GDBPORT) + 2)
QEMUOPTS = -hda $(UCOREIMG) -drive file=$(SWAPIMG),media=disk,cache=writeback -drive file=$(SFSIMG),media=disk,cache=writeback
QEMUOPTS += -net user -net nic,model=e1000 -redir tcp:$(PORT7)::7 \
-redir tcp:$(PORT80)::80 -redir udp:$(PORT7)::7 -net dump,file=qemu.pcap
.PHONY: qemu qemu-nox debug debug-nox monitor
qemu-mon: $(UCOREIMG) $(SWAPIMG)
$(V)$(QEMU) -monitor stdio $(QEMUOPTS) -serial null
qemu: $(UCOREIMG) $(SWAPIMG) $(SFSIMG)
$(V)$(QEMU) -serial stdio $(QEMUOPTS) -parallel null
# $(V)$(QEMU) -parallel stdio $(QEMUOPTS) -serial null
qemu-nox: $(UCOREIMG) $(SWAPIMG) $(SFSIMG)
$(V)$(QEMU) -serial mon:stdio $(QEMUOPTS) -nographic
qemu-nox-run:
$(V)$(QEMU) -serial mon:stdio $(QEMUOPTS) -nographic
bochs: $(UCOREIMG) $(SWAPIMG) $(SFSIMG)
$(V)$(BOCHS) -f sudo bochs -f bochsrc.txt
monitor: $(UCOREIMG) $(SWAPING) $(SFSIMG)
$(V)$(QEMU) -monitor stdio $(QEMUOPTS) -serial null
TERMINAL := gnome-terminal
dbg4ec: $(UCOREIMG) $(SWAPIMG) $(SFSIMG)
$(V)$(QEMU) -S -s -parallel stdio $(QEMUOPTS) -serial null
debug: $(UCOREIMG) $(SWAPIMG) $(SFSIMG)
$(V)$(QEMU) -S -s -parallel stdio $(QEMUOPTS) -serial null &
$(V)sleep 2
$(V)$(GDB) -q -x tools/gdbinit
debug-nox: $(UCOREIMG) $(SWAPIMG) $(SFSIMG)
$(V)$(QEMU) -S -s -serial mon:stdio $(QEMUOPTS) -nographic &
$(V)sleep 2
$(V)$(GDB) -q -x tools/gdbinit
RUN_PREFIX := _binary_$(OBJDIR)_$(USER_PREFIX)
MAKEOPTS := --quiet --no-print-directory
run-%: build-%
$(V)$(QEMU) -parallel stdio $(QEMUOPTS) -serial null
sh-%: script-%
$(V)$(QEMU) -parallel stdio $(QEMUOPTS) -serial null
run-nox-%: build-%
$(V)$(QEMU) -serial mon:stdio $(QEMUOPTS) -nographic
build-%: touch
$(V)$(MAKE) $(MAKEOPTS) "DEFS+=-DTEST=$*"
script-%: touch
$(V)$(MAKE) $(MAKEOPTS) "DEFS+=-DTEST=sh -DTESTSCRIPT=/script/$*"
.PHONY: grade touch
GRADE_GDB_IN := .gdb.in
GRADE_QEMU_OUT := .qemu.out
HANDIN := proj$(PROJ)-handin.tar.gz
TOUCH_FILES := kern/process/proc.c
MAKEOPTS := --quiet --no-print-directory
grade:
$(V)$(MAKE) $(MAKEOPTS) clean
$(V)$(SH) tools/grade.sh
touch:
$(V)$(foreach f,$(TOUCH_FILES),$(TOUCH) $(f))
print-%:
@echo $($(shell echo $(patsubst print-%,%,$@) | $(TR) [a-z] [A-Z]))
.PHONY: clean dist-clean handin packall
clean:
$(V)$(RM) $(GRADE_GDB_IN) $(GRADE_QEMU_OUT) $(SFSBINS)
-$(RM) -r $(OBJDIR) $(BINDIR)
dist-clean: clean
-$(RM) $(HANDIN)
handin: packall
@echo Please visit http://learn.tsinghua.edu.cn and upload $(HANDIN). Thanks!
packall: clean
@$(RM) -f $(HANDIN)
@tar -czf $(HANDIN) `find . -type f -o -type d | grep -v '^\.*$$' | grep -vF '$(HANDIN)'`
# For network connections
which-ports:
@echo "Local port $(PORT7) forwards to UCORE port 7 (echo server)"
@echo "Local port $(PORT80) forwards to UCORE port 80 (web server)"
nc-80:
nc localhost $(PORT80)
nc-udp-80:
nc -u localhost $(PORT80)
nc-7:
nc localhost $(PORT7)
nc-udp-7:
nc -u localhost $(PORT7)
telnet-80:
telnet localhost $(PORT80)
telnet-7:
telnet localhost $(PORT7)