《操作系统》的实验代码。
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

288 行
7.5 KiB

  1. PROJ := 8
  2. EMPTY :=
  3. SPACE := $(EMPTY) $(EMPTY)
  4. SLASH := /
  5. V := @
  6. #need llvm/cang-3.5+
  7. #USELLVM := 1
  8. # try to infer the correct GCCPREFX
  9. ifndef GCCPREFIX
  10. GCCPREFIX := $(shell if i386-ucore-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
  11. then echo 'i386-ucore-elf-'; \
  12. elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
  13. then echo ''; \
  14. else echo "***" 1>&2; \
  15. echo "*** Error: Couldn't find an i386-ucore-elf version of GCC/binutils." 1>&2; \
  16. echo "*** Is the directory with i386-ucore-elf-gcc in your PATH?" 1>&2; \
  17. echo "*** If your i386-ucore-elf toolchain is installed with a command" 1>&2; \
  18. echo "*** prefix other than 'i386-ucore-elf-', set your GCCPREFIX" 1>&2; \
  19. echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
  20. echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
  21. echo "***" 1>&2; exit 1; fi)
  22. endif
  23. # try to infer the correct QEMU
  24. ifndef QEMU
  25. QEMU := $(shell if which qemu-system-i386 > /dev/null; \
  26. then echo 'qemu-system-i386'; exit; \
  27. elif which i386-elf-qemu > /dev/null; \
  28. then echo 'i386-elf-qemu'; exit; \
  29. elif which qemu > /dev/null; \
  30. then echo 'qemu'; exit; \
  31. else \
  32. echo "***" 1>&2; \
  33. echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
  34. echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
  35. echo "***" 1>&2; exit 1; fi)
  36. endif
  37. # eliminate default suffix rules
  38. .SUFFIXES: .c .S .h
  39. # delete target files if there is an error (or make is interrupted)
  40. .DELETE_ON_ERROR:
  41. # define compiler and flags
  42. ifndef USELLVM
  43. HOSTCC := gcc
  44. HOSTCFLAGS := -g -Wall -O2
  45. CC := $(GCCPREFIX)gcc
  46. CFLAGS := -fno-builtin -Wall -ggdb -m32 -gstabs -nostdinc $(DEFS)
  47. CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
  48. else
  49. HOSTCC := clang
  50. HOSTCFLAGS := -g -Wall -O2
  51. CC := clang
  52. CFLAGS := -fno-builtin -Wall -g -m32 -mno-sse -nostdinc $(DEFS)
  53. CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
  54. endif
  55. GDB := $(GCCPREFIX)gdb
  56. CTYPE := c S
  57. LD := $(GCCPREFIX)ld
  58. LDFLAGS := -m $(shell $(LD) -V | grep elf_i386 2>/dev/null)
  59. LDFLAGS += -nostdlib
  60. OBJCOPY := $(GCCPREFIX)objcopy
  61. OBJDUMP := $(GCCPREFIX)objdump
  62. COPY := cp
  63. MKDIR := mkdir -p
  64. MV := mv
  65. RM := rm -f
  66. AWK := awk
  67. SED := sed
  68. SH := sh
  69. TR := tr
  70. TOUCH := touch -c
  71. OBJDIR := obj
  72. BINDIR := bin
  73. ALLOBJS :=
  74. ALLDEPS :=
  75. TARGETS :=
  76. include tools/function.mk
  77. listf_cc = $(call listf,$(1),$(CTYPE))
  78. # for cc
  79. add_files_cc = $(call add_files,$(1),$(CC),$(CFLAGS) $(3),$(2),$(4))
  80. create_target_cc = $(call create_target,$(1),$(2),$(3),$(CC),$(CFLAGS))
  81. # for hostcc
  82. add_files_host = $(call add_files,$(1),$(HOSTCC),$(HOSTCFLAGS),$(2),$(3))
  83. create_target_host = $(call create_target,$(1),$(2),$(3),$(HOSTCC),$(HOSTCFLAGS))
  84. cgtype = $(patsubst %.$(2),%.$(3),$(1))
  85. objfile = $(call toobj,$(1))
  86. asmfile = $(call cgtype,$(call toobj,$(1)),o,asm)
  87. outfile = $(call cgtype,$(call toobj,$(1)),o,out)
  88. symfile = $(call cgtype,$(call toobj,$(1)),o,sym)
  89. # for match pattern
  90. match = $(shell echo $(2) | $(AWK) '{for(i=1;i<=NF;i++){if(match("$(1)","^"$$(i)"$$")){exit 1;}}}'; echo $$?)
  91. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  92. # include kernel/user
  93. INCLUDE += libs/
  94. CFLAGS += $(addprefix -I,$(INCLUDE))
  95. LIBDIR += libs
  96. $(call add_files_cc,$(call listf_cc,$(LIBDIR)),libs,)
  97. # -------------------------------------------------------------------
  98. # kernel
  99. KINCLUDE += kern/debug/ \
  100. kern/driver/ \
  101. kern/trap/ \
  102. kern/mm/ \
  103. kern/libs/ \
  104. kern/sync/ \
  105. kern/fs/
  106. KSRCDIR += kern/init \
  107. kern/libs \
  108. kern/debug \
  109. kern/driver \
  110. kern/trap \
  111. kern/mm \
  112. kern/sync \
  113. kern/fs
  114. KCFLAGS += $(addprefix -I,$(KINCLUDE))
  115. $(call add_files_cc,$(call listf_cc,$(KSRCDIR)),kernel,$(KCFLAGS))
  116. KOBJS = $(call read_packet,kernel libs)
  117. # create kernel target
  118. kernel = $(call totarget,kernel)
  119. $(kernel): tools/kernel.ld
  120. $(kernel): $(KOBJS)
  121. @echo + ld $@
  122. $(V)$(LD) $(LDFLAGS) -T tools/kernel.ld -o $@ $(KOBJS)
  123. @$(OBJDUMP) -S $@ > $(call asmfile,kernel)
  124. @$(OBJDUMP) -t $@ | $(SED) '1,/SYMBOL TABLE/d; s/ .* / /; /^$$/d' > $(call symfile,kernel)
  125. $(call create_target,kernel)
  126. # -------------------------------------------------------------------
  127. # create bootblock
  128. bootfiles = $(call listf_cc,boot)
  129. $(foreach f,$(bootfiles),$(call cc_compile,$(f),$(CC),$(CFLAGS) -Os -nostdinc))
  130. bootblock = $(call totarget,bootblock)
  131. $(bootblock): $(call toobj,boot/bootasm.S) $(call toobj,$(bootfiles)) | $(call totarget,sign)
  132. @echo + ld $@
  133. $(V)$(LD) $(LDFLAGS) -N -T tools/boot.ld $^ -o $(call toobj,bootblock)
  134. @$(OBJDUMP) -S $(call objfile,bootblock) > $(call asmfile,bootblock)
  135. @$(OBJCOPY) -S -O binary $(call objfile,bootblock) $(call outfile,bootblock)
  136. @$(call totarget,sign) $(call outfile,bootblock) $(bootblock)
  137. $(call create_target,bootblock)
  138. # -------------------------------------------------------------------
  139. # create 'sign' tools
  140. $(call add_files_host,tools/sign.c,sign,sign)
  141. $(call create_target_host,sign,sign)
  142. # -------------------------------------------------------------------
  143. # create ucore.img
  144. UCOREIMG := $(call totarget,ucore.img)
  145. $(UCOREIMG): $(kernel) $(bootblock)
  146. $(V)dd if=/dev/zero of=$@ count=10000
  147. $(V)dd if=$(bootblock) of=$@ conv=notrunc
  148. $(V)dd if=$(kernel) of=$@ seek=1 conv=notrunc
  149. $(call create_target,ucore.img)
  150. # -------------------------------------------------------------------
  151. # create swap.img
  152. SWAPIMG := $(call totarget,swap.img)
  153. $(SWAPIMG):
  154. $(V)dd if=/dev/zero of=$@ bs=1M count=128
  155. $(call create_target,swap.img)
  156. # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  157. $(call finish_all)
  158. IGNORE_ALLDEPS = clean \
  159. dist-clean \
  160. grade \
  161. touch \
  162. print-.+ \
  163. handin
  164. ifeq ($(call match,$(MAKECMDGOALS),$(IGNORE_ALLDEPS)),0)
  165. -include $(ALLDEPS)
  166. endif
  167. # files for grade script
  168. TARGETS: $(TARGETS)
  169. .DEFAULT_GOAL := TARGETS
  170. QEMUOPTS = -hda $(UCOREIMG) -drive file=$(SWAPIMG),media=disk,cache=writeback
  171. .PHONY: qemu qemu-nox debug debug-nox
  172. qemu-mon: $(UCOREIMG) $(SWAPIMG)
  173. $(V)$(QEMU) -monitor stdio $(QEMUOPTS) -serial null
  174. qemu: $(UCOREIMG) $(SWAPIMG)
  175. $(V)$(QEMU) -parallel stdio $(QEMUOPTS) -serial null
  176. qemu-nox: $(UCOREIMG) $(SWAPIMG)
  177. $(V)$(QEMU) -serial mon:stdio $(QEMUOPTS) -nographic
  178. TERMINAL := gnome-terminal
  179. debug: $(UCOREIMG) $(SWAPIMG)
  180. $(V)$(QEMU) -S -s -parallel stdio $(QEMUOPTS) -serial null &
  181. $(V)sleep 2
  182. $(V)$(TERMINAL) -e "$(GDB) -q -x tools/gdbinit"
  183. debug-nox: $(UCOREIMG) $(SWAPIMG)
  184. $(V)$(QEMU) -S -s -serial mon:stdio $(QEMUOPTS) -nographic &
  185. $(V)sleep 2
  186. $(V)$(TERMINAL) -e "$(GDB) -q -x tools/gdbinit"
  187. .PHONY: grade touch
  188. GRADE_GDB_IN := .gdb.in
  189. GRADE_QEMU_OUT := .qemu.out
  190. HANDIN := proj$(PROJ)-handin.tar.gz
  191. TOUCH_FILES := kern/trap/trap.c
  192. MAKEOPTS := --quiet --no-print-directory
  193. grade:
  194. $(V)$(MAKE) $(MAKEOPTS) clean
  195. $(V)$(SH) tools/grade.sh
  196. touch:
  197. $(V)$(foreach f,$(TOUCH_FILES),$(TOUCH) $(f))
  198. print-%:
  199. @echo $($(shell echo $(patsubst print-%,%,$@) | $(TR) [a-z] [A-Z]))
  200. .PHONY: clean dist-clean handin packall tags
  201. clean:
  202. $(V)$(RM) $(GRADE_GDB_IN) $(GRADE_QEMU_OUT) cscope* tags
  203. -$(RM) -r $(OBJDIR) $(BINDIR)
  204. dist-clean: clean
  205. -$(RM) $(HANDIN)
  206. handin: packall
  207. @echo Please visit http://learn.tsinghua.edu.cn and upload $(HANDIN). Thanks!
  208. packall: clean
  209. @$(RM) -f $(HANDIN)
  210. @tar -czf $(HANDIN) `find . -type f -o -type d | grep -v '^\.*$$' | grep -vF '$(HANDIN)'`
  211. tags:
  212. @echo TAGS ALL
  213. $(V)rm -f cscope.files cscope.in.out cscope.out cscope.po.out tags
  214. $(V)find . -type f -name "*.[chS]" >cscope.files
  215. $(V)cscope -bq
  216. $(V)ctags -L cscope.files