Kbuild.include 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. # SPDX-License-Identifier: GPL-2.0
  2. ####
  3. # kbuild: Generic definitions
  4. # Convenient variables
  5. comma := ,
  6. quote := "
  7. squote := '
  8. empty :=
  9. space := $(empty) $(empty)
  10. space_escape := _-_SPACE_-_
  11. pound := \#
  12. ###
  13. # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  14. dot-target = $(dir $@).$(notdir $@)
  15. ###
  16. # Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
  17. tmp-target = $(dir $@).tmp_$(notdir $@)
  18. ###
  19. # The temporary file to save gcc -MMD generated dependencies must not
  20. # contain a comma
  21. depfile = $(subst $(comma),_,$(dot-target).d)
  22. ###
  23. # filename of target with directory and extension stripped
  24. basetarget = $(basename $(notdir $@))
  25. ###
  26. # real prerequisites without phony targets
  27. real-prereqs = $(filter-out $(PHONY), $^)
  28. ###
  29. # Escape single quote for use in echo statements
  30. escsq = $(subst $(squote),'\$(squote)',$1)
  31. ###
  32. # Quote a string to pass it to C files. foo => '"foo"'
  33. stringify = $(squote)$(quote)$1$(quote)$(squote)
  34. ###
  35. # Easy method for doing a status message
  36. kecho := :
  37. quiet_kecho := echo
  38. silent_kecho := :
  39. kecho := $($(quiet)kecho)
  40. ###
  41. # filechk is used to check if the content of a generated file is updated.
  42. # Sample usage:
  43. #
  44. # filechk_sample = echo $(KERNELRELEASE)
  45. # version.h: FORCE
  46. # $(call filechk,sample)
  47. #
  48. # The rule defined shall write to stdout the content of the new file.
  49. # The existing file will be compared with the new one.
  50. # - If no file exist it is created
  51. # - If the content differ the new file is used
  52. # - If they are equal no change, and no timestamp update
  53. define filechk
  54. $(check-FORCE)
  55. $(Q)set -e; \
  56. mkdir -p $(dir $@); \
  57. trap "rm -f $(dot-target).tmp" EXIT; \
  58. { $(filechk_$(1)); } > $(dot-target).tmp; \
  59. if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then \
  60. $(kecho) ' UPD $@'; \
  61. mv -f $(dot-target).tmp $@; \
  62. fi
  63. endef
  64. ###
  65. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  66. # Usage:
  67. # $(Q)$(MAKE) $(build)=dir
  68. build := -f $(srctree)/scripts/Makefile.build obj
  69. ###
  70. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
  71. # Usage:
  72. # $(Q)$(MAKE) $(dtbinst)=dir
  73. dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
  74. ###
  75. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
  76. # Usage:
  77. # $(Q)$(MAKE) $(clean)=dir
  78. clean := -f $(srctree)/scripts/Makefile.clean obj
  79. # echo command.
  80. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  81. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  82. echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
  83. # sink stdout for 'make -s'
  84. redirect :=
  85. quiet_redirect :=
  86. silent_redirect := exec >/dev/null;
  87. # Delete the target on interruption
  88. #
  89. # GNU Make automatically deletes the target if it has already been changed by
  90. # the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
  91. # will delete incomplete targets), and resume it later.
  92. #
  93. # However, this does not work when the stderr is piped to another program, like
  94. # $ make >&2 | tee log
  95. # Make dies with SIGPIPE before cleaning the targets.
  96. #
  97. # To address it, we clean the target in signal traps.
  98. #
  99. # Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
  100. # So, we cover them, and also SIGPIPE just in case.
  101. #
  102. # Of course, this is unneeded for phony targets.
  103. delete-on-interrupt = \
  104. $(if $(filter-out $(PHONY), $@), \
  105. $(foreach sig, HUP INT QUIT TERM PIPE, \
  106. trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
  107. # printing commands
  108. cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(delete-on-interrupt) $(cmd_$(1))
  109. ###
  110. # if_changed - execute command if any prerequisite is newer than
  111. # target, or command line has changed
  112. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  113. # including used config symbols
  114. # if_changed_rule - as if_changed but execute rule instead
  115. # See Documentation/kbuild/makefiles.rst for more info
  116. ifneq ($(KBUILD_NOCMDDEP),1)
  117. # Check if both commands are the same including their order. Result is empty
  118. # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
  119. cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
  120. $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
  121. else
  122. cmd-check = $(if $(strip $(cmd_$@)),,1)
  123. endif
  124. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  125. # (needed for make)
  126. # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
  127. # (needed for make)
  128. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  129. # (needed for the shell)
  130. make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
  131. # Find any prerequisites that are newer than target or that do not exist.
  132. # (This is not true for now; $? should contain any non-existent prerequisites,
  133. # but it does not work as expected when .SECONDARY is present. This seems a bug
  134. # of GNU Make.)
  135. # PHONY targets skipped in both cases.
  136. newer-prereqs = $(filter-out $(PHONY),$?)
  137. # It is a typical mistake to forget the FORCE prerequisite. Check it here so
  138. # no more breakage will slip in.
  139. check-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
  140. if-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
  141. # Execute command if command has changed or prerequisite(s) are updated.
  142. if_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
  143. cmd_and_savecmd = \
  144. $(cmd); \
  145. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd
  146. # Execute the command and also postprocess generated .d dependencies file.
  147. if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
  148. cmd_and_fixdep = \
  149. $(cmd); \
  150. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
  151. rm -f $(depfile)
  152. # Usage: $(call if_changed_rule,foo)
  153. # Will check if $(cmd_foo) or any of the prerequisites changed,
  154. # and if so will execute $(rule_foo).
  155. if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
  156. ###
  157. # why - tell why a target got built
  158. # enabled by make V=2
  159. # Output (listed in the order they are checked):
  160. # (1) - due to target is PHONY
  161. # (2) - due to target missing
  162. # (3) - due to: file1.h file2.h
  163. # (4) - due to command line change
  164. # (5) - due to missing .cmd file
  165. # (6) - due to target not in $(targets)
  166. # (1) PHONY targets are always build
  167. # (2) No target, so we better build it
  168. # (3) Prerequisite is newer than target
  169. # (4) The command line stored in the file named dir/.target.cmd
  170. # differed from actual command line. This happens when compiler
  171. # options changes
  172. # (5) No dir/.target.cmd file (used to store command line)
  173. # (6) No dir/.target.cmd file and target not listed in $(targets)
  174. # This is a good hint that there is a bug in the kbuild file
  175. ifeq ($(KBUILD_VERBOSE),2)
  176. why = \
  177. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  178. $(if $(wildcard $@), \
  179. $(if $(newer-prereqs),- due to: $(newer-prereqs), \
  180. $(if $(cmd-check), \
  181. $(if $(cmd_$@),- due to command line change, \
  182. $(if $(filter $@, $(targets)), \
  183. - due to missing .cmd file, \
  184. - due to $(notdir $@) not in $$(targets) \
  185. ) \
  186. ) \
  187. ) \
  188. ), \
  189. - due to target missing \
  190. ) \
  191. )
  192. echo-why = $(call escsq, $(strip $(why)))
  193. endif
  194. ###############################################################################
  195. # delete partially updated (i.e. corrupted) files on error
  196. .DELETE_ON_ERROR:
  197. # do not delete intermediate files automatically
  198. .SECONDARY: