Kbuild.include 5.4 KB

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