《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.9 KiB

  1. OBJPREFIX := __objs_
  2. .SECONDEXPANSION:
  3. # -------------------- function begin --------------------
  4. # list all files in some directories: (#directories, #types)
  5. listf = $(filter $(if $(2),$(addprefix %.,$(2)),%),\
  6. $(wildcard $(addsuffix $(SLASH)*,$(1))))
  7. # get .o obj files: (#files[, packet])
  8. toobj = $(addprefix $(OBJDIR)$(SLASH)$(if $(2),$(2)$(SLASH)),\
  9. $(addsuffix .o,$(basename $(1))))
  10. # get .d dependency files: (#files[, packet])
  11. todep = $(patsubst %.o,%.d,$(call toobj,$(1),$(2)))
  12. totarget = $(addprefix $(BINDIR)$(SLASH),$(1))
  13. # change $(name) to $(OBJPREFIX)$(name): (#names)
  14. packetname = $(if $(1),$(addprefix $(OBJPREFIX),$(1)),$(OBJPREFIX))
  15. # cc compile template, generate rule for dep, obj: (file, cc[, flags, dir])
  16. define cc_template
  17. $$(call todep,$(1),$(4)): $(1) | $$$$(dir $$$$@)
  18. @$(2) -I$$(dir $(1)) $(3) -MM $$< -MT "$$(patsubst %.d,%.o,$$@) $$@"> $$@
  19. $$(call toobj,$(1),$(4)): $(1) | $$$$(dir $$$$@)
  20. @echo + cc $$<
  21. $(V)$(2) -I$$(dir $(1)) $(3) -c $$< -o $$@
  22. ALLOBJS += $$(call toobj,$(1),$(4))
  23. endef
  24. # compile file: (#files, cc[, flags, dir])
  25. define do_cc_compile
  26. $$(foreach f,$(1),$$(eval $$(call cc_template,$$(f),$(2),$(3),$(4))))
  27. endef
  28. # add files to packet: (#files, cc[, flags, packet, dir])
  29. define do_add_files_to_packet
  30. __temp_packet__ := $(call packetname,$(4))
  31. ifeq ($$(origin $$(__temp_packet__)),undefined)
  32. $$(__temp_packet__) :=
  33. endif
  34. __temp_objs__ := $(call toobj,$(1),$(5))
  35. $$(foreach f,$(1),$$(eval $$(call cc_template,$$(f),$(2),$(3),$(5))))
  36. $$(__temp_packet__) += $$(__temp_objs__)
  37. endef
  38. # add objs to packet: (#objs, packet)
  39. define do_add_objs_to_packet
  40. __temp_packet__ := $(call packetname,$(2))
  41. ifeq ($$(origin $$(__temp_packet__)),undefined)
  42. $$(__temp_packet__) :=
  43. endif
  44. $$(__temp_packet__) += $(1)
  45. endef
  46. # add packets and objs to target (target, #packes, #objs[, cc, flags])
  47. define do_create_target
  48. __temp_target__ = $(call totarget,$(1))
  49. __temp_objs__ = $$(foreach p,$(call packetname,$(2)),$$($$(p))) $(3)
  50. TARGETS += $$(__temp_target__)
  51. ifneq ($(4),)
  52. $$(__temp_target__): $$(__temp_objs__) | $$$$(dir $$$$@)
  53. $(V)$(4) $(5) $$^ -o $$@
  54. else
  55. $$(__temp_target__): $$(__temp_objs__) | $$$$(dir $$$$@)
  56. endif
  57. endef
  58. # finish all
  59. define do_finish_all
  60. ALLDEPS = $$(ALLOBJS:.o=.d)
  61. $$(sort $$(dir $$(ALLOBJS)) $(BINDIR)$(SLASH) $(OBJDIR)$(SLASH)):
  62. @$(MKDIR) $$@
  63. endef
  64. # -------------------- function end --------------------
  65. # compile file: (#files, cc[, flags, dir])
  66. cc_compile = $(eval $(call do_cc_compile,$(1),$(2),$(3),$(4)))
  67. # add files to packet: (#files, cc[, flags, packet, dir])
  68. add_files = $(eval $(call do_add_files_to_packet,$(1),$(2),$(3),$(4),$(5)))
  69. # add objs to packet: (#objs, packet)
  70. add_objs = $(eval $(call do_add_objs_to_packet,$(1),$(2)))
  71. # add packets and objs to target (target, #packes, #objs, cc, [, flags])
  72. create_target = $(eval $(call do_create_target,$(1),$(2),$(3),$(4),$(5)))
  73. read_packet = $(foreach p,$(call packetname,$(1)),$($(p)))
  74. add_dependency = $(eval $(1): $(2))
  75. finish_all = $(eval $(call do_finish_all))