3
0

Kbuild.include 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. # cc-option-yn
  55. # Usage: flag := $(call cc-option-yn, -march=winchip-c6)
  56. cc-option-yn = $(shell if $(CC) $(CFLAGS) $(1) -S -o /dev/null -xc /dev/null \
  57. > /dev/null 2>&1; then echo "y"; else echo "n"; fi;)
  58. # cc-option-align
  59. # Prefix align with either -falign or -malign
  60. cc-option-align = $(subst -functions=0,,\
  61. $(call cc-option,-falign-functions=0,-malign-functions=0))
  62. # cc-version
  63. # Usage gcc-ver := $(call cc-version, $(CC))
  64. cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh \
  65. $(if $(1), $(1), $(CC)))
  66. # cc-ifversion
  67. # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
  68. cc-ifversion = $(shell if [ $(call cc-version, $(CC)) $(1) $(2) ]; then \
  69. echo $(3); fi;)
  70. ###
  71. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  72. # Usage:
  73. # $(Q)$(MAKE) $(build)=dir
  74. build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
  75. # Prefix -I with $(srctree) if it is not an absolute path
  76. addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
  77. # Find all -I options and call addtree
  78. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  79. # If quiet is set, only print short version of command
  80. cmd = @$(echo-cmd) $(cmd_$(1))
  81. # Add $(obj)/ for paths that is not absolute
  82. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  83. ###
  84. # if_changed - execute command if any prerequisite is newer than
  85. # target, or command line has changed
  86. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  87. # including used config symbols
  88. # if_changed_rule - as if_changed but execute rule instead
  89. # See Documentation/kbuild/makefiles.txt for more info
  90. ifneq ($(KBUILD_NOCMDDEP),1)
  91. # Check if both arguments has same arguments. Result in empty string if equal
  92. # User may override this check using make KBUILD_NOCMDDEP=1
  93. arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
  94. endif
  95. # echo command. Short version is $(quiet) equals quiet, otherwise full command
  96. echo-cmd = $(if $($(quiet)cmd_$(1)), \
  97. echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
  98. make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
  99. # function to only execute the passed command if necessary
  100. # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
  101. # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
  102. #
  103. if_changed = $(if $(strip $(filter-out $(PHONY),$?) \
  104. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  105. @set -e; \
  106. $(echo-cmd) $(cmd_$(1)); \
  107. echo 'cmd_$@ := $(make-cmd)' > $(@D)/.$(@F).cmd)
  108. # execute the command and also postprocess generated .d dependencies
  109. # file
  110. if_changed_dep = $(if $(strip $(filter-out $(PHONY),$?) \
  111. $(filter-out FORCE $(wildcard $^),$^) \
  112. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  113. @set -e; \
  114. $(echo-cmd) $(cmd_$(1)); \
  115. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(@D)/.$(@F).tmp; \
  116. rm -f $(depfile); \
  117. mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
  118. # Usage: $(call if_changed_rule,foo)
  119. # will check if $(cmd_foo) changed, or any of the prequisites changed,
  120. # and if so will execute $(rule_foo)
  121. if_changed_rule = $(if $(strip $(filter-out $(PHONY),$?) \
  122. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
  123. @set -e; \
  124. $(rule_$(1)))