Kbuild.include 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ####
  2. # kbuild: Generic definitions
  3. # Convinient variables
  4. comma := ,
  5. empty :=
  6. space := $(empty) $(empty)
  7. ###
  8. # The temporary file to save gcc -MD generated dependencies must not
  9. # contain a comma
  10. depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
  11. ###
  12. # filechk is used to check if the content of a generated file is updated.
  13. # Sample usage:
  14. # define filechk_sample
  15. # echo $KERNELRELEASE
  16. # endef
  17. # version.h : Makefile
  18. # $(call filechk,sample)
  19. # The rule defined shall write to stdout the content of the new file.
  20. # The existing file will be compared with the new one.
  21. # - If no file exist it is created
  22. # - If the content differ the new file is used
  23. # - If they are equal no change, and no timestamp update
  24. # - stdin is piped in from the first prerequisite ($<) so one has
  25. # to specify a valid file as first prerequisite (often the kbuild file)
  26. define filechk
  27. $(Q)set -e; \
  28. echo ' CHK $@'; \
  29. mkdir -p $(dir $@); \
  30. $(filechk_$(1)) < $< > $@.tmp; \
  31. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  32. rm -f $@.tmp; \
  33. else \
  34. echo ' UPD $@'; \
  35. mv -f $@.tmp $@; \
  36. fi
  37. endef
  38. ###
  39. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  40. # Usage:
  41. # $(Q)$(MAKE) $(build)=dir
  42. build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
  43. # If quiet is set, only print short version of command
  44. cmd = @$(if $($(quiet)cmd_$(1)),\
  45. echo ' $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
  46. # Add $(obj)/ for paths that is not absolute
  47. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  48. ###
  49. # if_changed - execute command if any prerequisite is newer than
  50. # target, or command line has changed
  51. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  52. # including used config symbols
  53. # if_changed_rule - as if_changed but execute rule instead
  54. # See Documentation/kbuild/makefiles.txt for more info
  55. ifneq ($(KBUILD_NOCMDDEP),1)
  56. # Check if both arguments has same arguments. Result in empty string if equal
  57. # User may override this check using make KBUILD_NOCMDDEP=1
  58. arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
  59. endif
  60. # echo command. Short version is $(quiet) equals quiet, otherwise full command
  61. echo-cmd = $(if $($(quiet)cmd_$(1)), \
  62. echo ' $(subst ','\'',$($(quiet)cmd_$(1)))';)
  63. # function to only execute the passed command if necessary
  64. # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
  65. # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
  66. #
  67. if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  68. @set -e; \
  69. $(echo-cmd) \
  70. $(cmd_$(1)); \
  71. echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
  72. # execute the command and also postprocess generated .d dependencies
  73. # file
  74. if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
  75. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  76. @set -e; \
  77. $(echo-cmd) \
  78. $(cmd_$(1)); \
  79. scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
  80. rm -f $(depfile); \
  81. mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
  82. # Usage: $(call if_changed_rule,foo)
  83. # will check if $(cmd_foo) changed, or any of the prequisites changed,
  84. # and if so will execute $(rule_foo)
  85. if_changed_rule = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
  86. @set -e; \
  87. $(rule_$(1)))