Kbuild.include 10 KB

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