PROJ := 5 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 > /dev/null; \ then echo 'qemu'; 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 # eliminate default suffix rules .SUFFIXES: .c .S .h # delete target files if there is an error (or make is interrupted) .DELETE_ON_ERROR: # define compiler and flags HOSTCC := gcc HOSTCFLAGS := -g -Wall -O2 GDB := $(GCCPREFIX)gdb CC := $(GCCPREFIX)gcc CFLAGS := -fno-builtin -Wall -ggdb -m32 -gstabs -nostdinc $(DEFS) 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 TAR := tar ZIP := gzip OBJDIR := obj BINDIR := bin ALLOBJS := ALLDEPS := TARGETS := 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) # 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,) # ------------------------------------------------------------------- # kernel KINCLUDE += kern/debug/ \ kern/driver/ \ kern/trap/ \ kern/mm/ \ kern/libs/ \ kern/sync/ KSRCDIR += kern/init \ kern/libs \ kern/debug \ kern/driver \ kern/trap \ kern/mm \ kern/sync 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 kernel_nopage target kernel_nopage = $(call totarget,kernel_nopage) $(kernel_nopage): tools/kernel_nopage.ld $(kernel_nopage): $(KOBJS) @echo + ld $@ $(V)$(LD) $(LDFLAGS) -T tools/kernel_nopage.ld -o $@ $(KOBJS) @$(OBJDUMP) -S $@ > $(call asmfile,kernel_nopage) @$(OBJDUMP) -t $@ | $(SED) '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(call symfile,kernel_nopage) $(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) @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 ucore.img UCOREIMG := $(call totarget,ucore.img) $(UCOREIMG): $(kernel) $(bootblock) $(kernel_nopage) $(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) # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> $(call finish_all) IGNORE_ALLDEPS = gdb \ clean \ distclean \ grade \ touch \ print-.+ \ handin ifeq ($(call match,$(MAKECMDGOALS),$(IGNORE_ALLDEPS)),0) -include $(ALLDEPS) endif # files for grade script targets: $(TARGETS) .DEFAULT_GOAL := targets QEMUOPTS = -hda $(UCOREIMG) .PHONY: qemu qemu-nox gdb debug debug-mon debug-nox qemu: targets $(V)$(QEMU) -parallel stdio $(QEMUOPTS) -serial null qemu-nox: targets $(V)$(QEMU) -serial mon:stdio $(QEMUOPTS) -nographic gdb: $(V)$(GDB) -q -x tools/gdbinit debug: targets $(V)$(QEMU) -S -s -parallel stdio $(QEMUOPTS) -serial null debug-mon: targets $(V)$(QEMU) -S -s -monitor stdio $(QEMUOPTS) -parallel null -serial null debug-nox: targets $(V)$(QEMU) -S -s -serial mon:stdio $(QEMUOPTS) -nographic .PHONY: grade touch GRADE_GDB_IN := .gdb.in GRADE_QEMU_OUT := .qemu.out HANDIN := lab2-handin.tar.gz TOUCH_FILES := kern/trap/trap.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 distclean handin tags clean: $(V)$(RM) $(GRADE_GDB_IN) $(GRADE_QEMU_OUT) cscope* tags $(V)$(RM) -r $(OBJDIR) $(BINDIR) distclean: clean $(V)$(RM) $(HANDIN) handin: distclean $(V)$(TAR) -cf - `find . -type f -o -type d | grep -v '^\.$$' | grep -v '/CVS/' \ | grep -v '/\.git/' | grep -v '/\.svn/' | grep -v "$(HANDIN)"` \ | $(ZIP) > $(HANDIN) tags: @echo TAGS ALL $(V)rm -f cscope.files cscope.in.out cscope.out cscope.po.out tags $(V)find . -type f -name "*.[chS]" >cscope.files $(V)cscope -bq $(V)ctags -L cscope.files