rules.mak 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Don't use implicit rules or variables
  2. # we have explicit rules for everything
  3. MAKEFLAGS += -rR
  4. # Files with this suffixes are final, don't try to generate them
  5. # using implicit rules
  6. %.d:
  7. %.h:
  8. %.c:
  9. %.m:
  10. %.mak:
  11. # Flags for dependency generation
  12. QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(*D)/$(*F).d
  13. # Same as -I$(SRC_PATH) -I., but for the nested source/object directories
  14. QEMU_CFLAGS += -I$(<D) -I$(@D)
  15. %.o: %.c
  16. $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," CC $(TARGET_DIR)$@")
  17. ifeq ($(LIBTOOL),)
  18. LIBTOOL = /bin/false
  19. LINK = $(call quiet-command,$(CC) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
  20. $(sort $(filter %.o, $1)) $(filter-out %.o, $1) $(version-obj-y) \
  21. $(LIBS)," LINK $(TARGET_DIR)$@")
  22. else
  23. LIBTOOL += $(if $(V),,--quiet)
  24. %.lo: %.c
  25. $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," lt CC $@")
  26. %.lo: %.dtrace
  27. $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@")
  28. LINK = $(call quiet-command,\
  29. $(if $(filter %.lo %.la,$^),$(LIBTOOL) --mode=link --tag=CC \
  30. )$(CC) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
  31. $(sort $(filter %.o, $1)) $(filter-out %.o, $1) $(version-obj-y) \
  32. $(LIBS),$(if $(filter %.lo %.la,$^),"lt LINK ", " LINK ")"$(TARGET_DIR)$@")
  33. endif
  34. %.asm: %.S
  35. $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@")
  36. %.o: %.asm
  37. $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@")
  38. %.o: %.m
  39. $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
  40. %.o: %.dtrace
  41. $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
  42. %$(EXESUF): %.o
  43. $(call LINK,$^)
  44. %.a:
  45. $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
  46. quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
  47. # cc-option
  48. # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
  49. cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
  50. >/dev/null 2>&1 && echo OK), $2, $3)
  51. VPATH_SUFFIXES = %.c %.h %.S %.m %.mak %.texi %.sh
  52. set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
  53. # find-in-path
  54. # Usage: $(call find-in-path, prog)
  55. # Looks in the PATH if the argument contains no slash, else only considers one
  56. # specific directory. Returns an # empty string if the program doesn't exist
  57. # there.
  58. find-in-path = $(if $(find-string /, $1), \
  59. $(wildcard $1), \
  60. $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
  61. # Generate files with tracetool
  62. TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
  63. # Generate timestamp files for .h include files
  64. config-%.h: config-%.h-timestamp
  65. @cmp $< $@ >/dev/null 2>&1 || cp $< $@
  66. config-%.h-timestamp: config-%.mak
  67. $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
  68. .PHONY: clean-timestamp
  69. clean-timestamp:
  70. rm -f *.timestamp
  71. clean: clean-timestamp
  72. # will delete the target of a rule if commands exit with a nonzero exit status
  73. .DELETE_ON_ERROR:
  74. # magic to descend into other directories
  75. obj := .
  76. old-nested-dirs :=
  77. define push-var
  78. $(eval save-$2-$1 = $(value $1))
  79. $(eval $1 :=)
  80. endef
  81. define pop-var
  82. $(eval subdir-$2-$1 := $(if $(filter $2,$(save-$2-$1)),$(addprefix $2,$($1))))
  83. $(eval $1 = $(value save-$2-$1) $$(subdir-$2-$1))
  84. $(eval save-$2-$1 :=)
  85. endef
  86. define unnest-dir
  87. $(foreach var,$(nested-vars),$(call push-var,$(var),$1/))
  88. $(eval obj := $(obj)/$1)
  89. $(eval include $(SRC_PATH)/$1/Makefile.objs)
  90. $(eval obj := $(patsubst %/$1,%,$(obj)))
  91. $(foreach var,$(nested-vars),$(call pop-var,$(var),$1/))
  92. endef
  93. define unnest-vars-1
  94. $(eval nested-dirs := $(filter-out \
  95. $(old-nested-dirs), \
  96. $(sort $(foreach var,$(nested-vars), $(filter %/, $($(var)))))))
  97. $(if $(nested-dirs),
  98. $(foreach dir,$(nested-dirs),$(call unnest-dir,$(patsubst %/,%,$(dir))))
  99. $(eval old-nested-dirs := $(old-nested-dirs) $(nested-dirs))
  100. $(call unnest-vars-1))
  101. endef
  102. define unnest-vars
  103. $(call unnest-vars-1)
  104. $(foreach var,$(nested-vars),$(eval $(var) := $(filter-out %/, $($(var)))))
  105. $(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
  106. $(foreach var,$(nested-vars), $(eval \
  107. -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
  108. endef