Kbuild.include 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ####
  2. # kbuild: Generic definitions
  3. # Convinient variables
  4. comma := ,
  5. squote := '
  6. empty :=
  7. space := $(empty) $(empty)
  8. ###
  9. # The temporary file to save gcc -MD generated dependencies must not
  10. # contain a comma
  11. depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
  12. ###
  13. # Escape single quote for use in echo statements
  14. escsq = $(subst $(squote),'\$(squote)',$1)
  15. ###
  16. # filechk is used to check if the content of a generated file is updated.
  17. # Sample usage:
  18. # define filechk_sample
  19. # echo $KERNELRELEASE
  20. # endef
  21. # version.h : Makefile
  22. # $(call filechk,sample)
  23. # The rule defined shall write to stdout the content of the new file.
  24. # The existing file will be compared with the new one.
  25. # - If no file exist it is created
  26. # - If the content differ the new file is used
  27. # - If they are equal no change, and no timestamp update
  28. # - stdin is piped in from the first prerequisite ($<) so one has
  29. # to specify a valid file as first prerequisite (often the kbuild file)
  30. define filechk
  31. $(Q)set -e; \
  32. echo ' CHK $@'; \
  33. mkdir -p $(dir $@); \
  34. $(filechk_$(1)) < $< > $@.tmp; \
  35. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  36. rm -f $@.tmp; \
  37. else \
  38. echo ' UPD $@'; \
  39. mv -f $@.tmp $@; \
  40. fi
  41. endef
  42. ######
  43. # gcc support functions
  44. # See documentation in Documentation/kbuild/makefiles.txt
  45. # as-option
  46. # Usage: cflags-y += $(call as-option, -Wa$(comma)-isa=foo,)
  47. as-option = $(shell if $(CC) $(CFLAGS) $(1) -Wa,-Z -c -o /dev/null \
  48. -xassembler /dev/null > /dev/null 2>&1; then echo "$(1)"; \
  49. else echo "$(2)"; fi ;)
  50. # cc-option
  51. # Usage: cflags-y += $(call cc-option, -march=winchip-c6, -march=i586)
  52. cc-option = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
  53. > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
  54. # hostcc-option
  55. # Usage: hostcflags-y += $(call hostcc-option, -march=winchip-c6, -march=i586)
  56. hostcc-option = $(shell if $(HOSTCC) $(HOSTCFLAGS) $(1) -S -o /dev/null -xc /dev/null \
  57. > /dev/null 2>&1; then echo "$(1)"; else echo "$(2)"; fi ;)
  58. # cc-option-yn
  59. # Usage: flag := $(call cc-option-yn, -march=winchip-c6)
  60. cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
  61. > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
  62. # cc-option-align
  63. # Prefix align with either -falign or -malign
  64. cc-option-align = $(subst -functions=0,,\
  65. $(call cc-option,-falign-functions=0,-malign-functions=0))
  66. # cc-version
  67. # Usage gcc-ver := $(call cc-version, $(CC))
  68. cc-version = $(shell PATH="$(PATH)" $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \
  69. $(if $(1), $(1), $(CC)))
  70. # cc-ifversion
  71. # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
  72. cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
  73. echo $(3); fi;)
  74. ###
  75. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  76. # Usage:
  77. # $(Q)$(MAKE) $(build)=dir
  78. build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
  79. # Prefix -I with $(srctree) if it is not an absolute path
  80. addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
  81. # Find all -I options and call addtree
  82. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  83. # If quiet is set, only print short version of command
  84. cmd = @$(echo-cmd) $(cmd_$(1))
  85. # Add $(obj)/ for paths that is not absolute
  86. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  87. ###
  88. # if_changed - execute command if any prerequisite is newer than
  89. # target, or command line has changed
  90. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  91. # including used config symbols
  92. # if_changed_rule - as if_changed but execute rule instead
  93. # See Documentation/kbuild/makefiles.txt for more info
  94. ifneq ($(KBUILD_NOCMDDEP),1)
  95. # Check if both arguments has same arguments. Result in empty string if equal
  96. # User may override this check using make KBUILD_NOCMDDEP=1
  97. arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
  98. endif
  99. # echo command. Short version is $(quiet) equals quiet, otherwise full command
  100. echo-cmd = $(if $($(quiet)cmd_$(1)), \
  101. echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
  102. make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
  103. # function to only execute the passed command if necessary
  104. # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
  105. # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
  106. #
  107. if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
  108. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  109. @set -e; \
  110. $(echo-cmd) $(cmd_$(1)); \
  111. echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
  112. # execute the command and also postprocess generated .d dependencies
  113. # file
  114. if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
  115. $(filter-out FORCE $(wildcard $^),$^) \
  116. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  117. @set -e; \
  118. $(echo-cmd) $(cmd_$(1)); \
  119. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
  120. rm -f $(depfile); \
  121. mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
  122. # Usage: $(call if_changed_rule,foo)
  123. # will check if $(cmd_foo) changed, or any of the prequisites changed,
  124. # and if so will execute $(rule_foo)
  125. if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
  126. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
  127. @set -e; \
  128. $(rule_$(1)))