Kbuild.include 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. define newline
  13. endef
  14. ###
  15. # Comparison macros.
  16. # Usage: $(call test-lt, $(CONFIG_LLD_VERSION), 150000)
  17. #
  18. # Use $(intcmp ...) if supported. (Make >= 4.4)
  19. # Otherwise, fall back to the 'test' shell command.
  20. ifeq ($(intcmp 1,0,,,y),y)
  21. test-ge = $(intcmp $(strip $1)0, $(strip $2)0,,y,y)
  22. test-gt = $(intcmp $(strip $1)0, $(strip $2)0,,,y)
  23. else
  24. test-ge = $(shell test $(strip $1)0 -ge $(strip $2)0 && echo y)
  25. test-gt = $(shell test $(strip $1)0 -gt $(strip $2)0 && echo y)
  26. endif
  27. test-le = $(call test-ge, $2, $1)
  28. test-lt = $(call test-gt, $2, $1)
  29. ###
  30. # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  31. dot-target = $(dir $@).$(notdir $@)
  32. ###
  33. # Name of target with a '.tmp_' as filename prefix. foo/bar.o => foo/.tmp_bar.o
  34. tmp-target = $(dir $@).tmp_$(notdir $@)
  35. ###
  36. # The temporary file to save gcc -MMD generated dependencies must not
  37. # contain a comma
  38. depfile = $(subst $(comma),_,$(dot-target).d)
  39. ###
  40. # filename of target with directory and extension stripped
  41. basetarget = $(basename $(notdir $@))
  42. ###
  43. # real prerequisites without phony targets
  44. real-prereqs = $(filter-out $(PHONY), $^)
  45. ###
  46. # Escape single quote for use in echo statements
  47. escsq = $(subst $(squote),'\$(squote)',$1)
  48. ###
  49. # Quote a string to pass it to C files. foo => '"foo"'
  50. stringify = $(squote)$(quote)$1$(quote)$(squote)
  51. ###
  52. # The path to Kbuild or Makefile. Kbuild has precedence over Makefile.
  53. kbuild-file = $(or $(wildcard $(src)/Kbuild),$(src)/Makefile)
  54. ###
  55. # Read a file, replacing newlines with spaces
  56. #
  57. # Make 4.2 or later can read a file by using its builtin function.
  58. ifneq ($(filter-out 4.0 4.1, $(MAKE_VERSION)),)
  59. read-file = $(subst $(newline),$(space),$(file < $1))
  60. else
  61. read-file = $(shell cat $1 2>/dev/null)
  62. endif
  63. ###
  64. # Easy method for doing a status message
  65. kecho := :
  66. quiet_kecho := echo
  67. silent_kecho := :
  68. kecho := $($(quiet)kecho)
  69. ###
  70. # filechk is used to check if the content of a generated file is updated.
  71. # Sample usage:
  72. #
  73. # filechk_sample = echo $(KERNELRELEASE)
  74. # version.h: FORCE
  75. # $(call filechk,sample)
  76. #
  77. # The rule defined shall write to stdout the content of the new file.
  78. # The existing file will be compared with the new one.
  79. # - If no file exist it is created
  80. # - If the content differ the new file is used
  81. # - If they are equal no change, and no timestamp update
  82. define filechk
  83. $(check-FORCE)
  84. $(Q)set -e; \
  85. mkdir -p $(dir $@); \
  86. trap "rm -f $(tmp-target)" EXIT; \
  87. { $(filechk_$(1)); } > $(tmp-target); \
  88. if [ ! -r $@ ] || ! cmp -s $@ $(tmp-target); then \
  89. $(kecho) ' UPD $@'; \
  90. mv -f $(tmp-target) $@; \
  91. fi
  92. endef
  93. ###
  94. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  95. # Usage:
  96. # $(Q)$(MAKE) $(build)=dir
  97. build := -f $(srctree)/scripts/Makefile.build obj
  98. ###
  99. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
  100. # Usage:
  101. # $(Q)$(MAKE) $(clean)=dir
  102. clean := -f $(srctree)/scripts/Makefile.clean obj
  103. # pring log
  104. #
  105. # If quiet is "silent_", print nothing and sink stdout
  106. # If quiet is "quiet_", print short log
  107. # If quiet is empty, print short log and whole command
  108. silent_log_print = exec >/dev/null;
  109. quiet_log_print = $(if $(quiet_cmd_$1), echo ' $(call escsq,$(quiet_cmd_$1)$(why))';)
  110. log_print = echo '$(pound) $(call escsq,$(or $(quiet_cmd_$1),cmd_$1 $@)$(why))'; \
  111. echo ' $(call escsq,$(cmd_$1))';
  112. # Delete the target on interruption
  113. #
  114. # GNU Make automatically deletes the target if it has already been changed by
  115. # the interrupted recipe. So, you can safely stop the build by Ctrl-C (Make
  116. # will delete incomplete targets), and resume it later.
  117. #
  118. # However, this does not work when the stderr is piped to another program, like
  119. # $ make >&2 | tee log
  120. # Make dies with SIGPIPE before cleaning the targets.
  121. #
  122. # To address it, we clean the target in signal traps.
  123. #
  124. # Make deletes the target when it catches SIGHUP, SIGINT, SIGQUIT, SIGTERM.
  125. # So, we cover them, and also SIGPIPE just in case.
  126. #
  127. # Of course, this is unneeded for phony targets.
  128. delete-on-interrupt = \
  129. $(if $(filter-out $(PHONY), $@), \
  130. $(foreach sig, HUP INT QUIT TERM PIPE, \
  131. trap 'rm -f $@; trap - $(sig); kill -s $(sig) $$$$' $(sig);))
  132. # print and execute commands
  133. cmd = @$(if $(cmd_$(1)),set -e; $($(quiet)log_print) $(delete-on-interrupt) $(cmd_$(1)),:)
  134. ###
  135. # if_changed - execute command if any prerequisite is newer than
  136. # target, or command line has changed
  137. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  138. # including used config symbols
  139. # if_changed_rule - as if_changed but execute rule instead
  140. # See Documentation/kbuild/makefiles.rst for more info
  141. ifneq ($(KBUILD_NOCMDDEP),1)
  142. # Check if both commands are the same including their order. Result is empty
  143. # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
  144. # If the target does not exist, the *.cmd file should not be included so
  145. # $(savedcmd_$@) gets empty. Then, target will be built even if $(newer-prereqs)
  146. # happens to become empty.
  147. cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(savedcmd_$@))), \
  148. $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
  149. else
  150. # We still need to detect missing targets.
  151. cmd-check = $(if $(strip $(savedcmd_$@)),,1)
  152. endif
  153. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  154. # (needed for make)
  155. # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
  156. # (needed for make)
  157. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  158. # (needed for the shell)
  159. make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
  160. # Find any prerequisites that are newer than target or that do not exist.
  161. # PHONY targets skipped in both cases.
  162. # If there is no prerequisite other than phony targets, $(newer-prereqs) becomes
  163. # empty even if the target does not exist. cmd-check saves this corner case.
  164. newer-prereqs = $(filter-out $(PHONY),$?)
  165. # It is a typical mistake to forget the FORCE prerequisite. Check it here so
  166. # no more breakage will slip in.
  167. check-FORCE = $(if $(filter FORCE, $^),,$(warning FORCE prerequisite is missing))
  168. if-changed-cond = $(newer-prereqs)$(cmd-check)$(check-FORCE)
  169. # Execute command if command has changed or prerequisite(s) are updated.
  170. if_changed = $(if $(if-changed-cond),$(cmd_and_savecmd),@:)
  171. cmd_and_savecmd = \
  172. $(cmd); \
  173. printf '%s\n' 'savedcmd_$@ := $(make-cmd)' > $(dot-target).cmd
  174. # Execute the command and also postprocess generated .d dependencies file.
  175. if_changed_dep = $(if $(if-changed-cond),$(cmd_and_fixdep),@:)
  176. cmd_and_fixdep = \
  177. $(cmd); \
  178. $(objtree)/scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
  179. rm -f $(depfile)
  180. # Usage: $(call if_changed_rule,foo)
  181. # Will check if $(cmd_foo) or any of the prerequisites changed,
  182. # and if so will execute $(rule_foo).
  183. if_changed_rule = $(if $(if-changed-cond),$(rule_$(1)),@:)
  184. ###
  185. # why - tell why a target got built
  186. # enabled by make V=2
  187. # Output (listed in the order they are checked):
  188. # (1) - due to target is PHONY
  189. # (2) - due to target missing
  190. # (3) - due to: file1.h file2.h
  191. # (4) - due to command line change
  192. # (5) - due to missing .cmd file
  193. # (6) - due to target not in $(targets)
  194. # (1) PHONY targets are always build
  195. # (2) No target, so we better build it
  196. # (3) Prerequisite is newer than target
  197. # (4) The command line stored in the file named dir/.target.cmd
  198. # differed from actual command line. This happens when compiler
  199. # options changes
  200. # (5) No dir/.target.cmd file (used to store command line)
  201. # (6) No dir/.target.cmd file and target not listed in $(targets)
  202. # This is a good hint that there is a bug in the kbuild file
  203. ifneq ($(findstring 2, $(KBUILD_VERBOSE)),)
  204. _why = \
  205. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  206. $(if $(wildcard $@), \
  207. $(if $(newer-prereqs),- due to: $(newer-prereqs), \
  208. $(if $(cmd-check), \
  209. $(if $(savedcmd_$@),- due to command line change, \
  210. $(if $(filter $@, $(targets)), \
  211. - due to missing .cmd file, \
  212. - due to $(notdir $@) not in $$(targets) \
  213. ) \
  214. ) \
  215. ) \
  216. ), \
  217. - due to target missing \
  218. ) \
  219. )
  220. why = $(space)$(strip $(_why))
  221. endif
  222. ###############################################################################
  223. # delete partially updated (i.e. corrupted) files on error
  224. .DELETE_ON_ERROR:
  225. # do not delete intermediate files automatically
  226. #
  227. # .NOTINTERMEDIATE is more correct, but only available on newer Make versions.
  228. # Make 4.4 introduced .NOTINTERMEDIATE, and it appears in .FEATURES, but the
  229. # global .NOTINTERMEDIATE does not work. We can use it on Make > 4.4.
  230. # Use .SECONDARY for older Make versions, but "newer-prereq" cannot detect
  231. # deleted files.
  232. ifneq ($(and $(filter notintermediate, $(.FEATURES)),$(filter-out 4.4,$(MAKE_VERSION))),)
  233. .NOTINTERMEDIATE:
  234. else
  235. .SECONDARY:
  236. endif