3
0

Kbuild.include 5.3 KB

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