Kbuild.include 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. # The temporary file to save gcc -MMD generated dependencies must not
  17. # contain a comma
  18. depfile = $(subst $(comma),_,$(dot-target).d)
  19. ###
  20. # filename of target with directory and extension stripped
  21. basetarget = $(basename $(notdir $@))
  22. ###
  23. # real prerequisites without phony targets
  24. real-prereqs = $(filter-out $(PHONY), $^)
  25. ###
  26. # Escape single quote for use in echo statements
  27. escsq = $(subst $(squote),'\$(squote)',$1)
  28. ###
  29. # Quote a string to pass it to C files. foo => '"foo"'
  30. stringify = $(squote)$(quote)$1$(quote)$(squote)
  31. ###
  32. # Easy method for doing a status message
  33. kecho := :
  34. quiet_kecho := echo
  35. silent_kecho := :
  36. kecho := $($(quiet)kecho)
  37. ###
  38. # filechk is used to check if the content of a generated file is updated.
  39. # Sample usage:
  40. #
  41. # filechk_sample = echo $(KERNELRELEASE)
  42. # version.h: FORCE
  43. # $(call filechk,sample)
  44. #
  45. # The rule defined shall write to stdout the content of the new file.
  46. # The existing file will be compared with the new one.
  47. # - If no file exist it is created
  48. # - If the content differ the new file is used
  49. # - If they are equal no change, and no timestamp update
  50. define filechk
  51. $(Q)set -e; \
  52. mkdir -p $(dir $@); \
  53. trap "rm -f $(dot-target).tmp" EXIT; \
  54. { $(filechk_$(1)); } > $(dot-target).tmp; \
  55. if [ ! -r $@ ] || ! cmp -s $@ $(dot-target).tmp; then \
  56. $(kecho) ' UPD $@'; \
  57. mv -f $(dot-target).tmp $@; \
  58. fi
  59. endef
  60. ######
  61. # gcc support functions
  62. # See documentation in Documentation/kbuild/makefiles.rst
  63. # cc-cross-prefix
  64. # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
  65. # Return first <prefix> where a <prefix>gcc is found in PATH.
  66. # If no gcc found in PATH with listed prefixes return nothing
  67. #
  68. # Note: '2>/dev/null' is here to force Make to invoke a shell. Otherwise, it
  69. # would try to directly execute the shell builtin 'command'. This workaround
  70. # should be kept for a long time since this issue was fixed only after the
  71. # GNU Make 4.2.1 release.
  72. cc-cross-prefix = $(firstword $(foreach c, $(1), \
  73. $(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c))))
  74. # output directory for tests below
  75. TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
  76. # try-run
  77. # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
  78. # Exit code chooses option. "$$TMP" serves as a temporary file and is
  79. # automatically cleaned up.
  80. try-run = $(shell set -e; \
  81. TMP=$(TMPOUT)/tmp; \
  82. TMPO=$(TMPOUT)/tmp.o; \
  83. mkdir -p $(TMPOUT); \
  84. trap "rm -rf $(TMPOUT)" EXIT; \
  85. if ($(1)) >/dev/null 2>&1; \
  86. then echo "$(2)"; \
  87. else echo "$(3)"; \
  88. fi)
  89. # as-option
  90. # Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
  91. as-option = $(call try-run,\
  92. $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
  93. # as-instr
  94. # Usage: cflags-y += $(call as-instr,instr,option1,option2)
  95. as-instr = $(call try-run,\
  96. printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
  97. # __cc-option
  98. # Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
  99. __cc-option = $(call try-run,\
  100. $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
  101. # cc-option
  102. # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
  103. cc-option = $(call __cc-option, $(CC),\
  104. $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
  105. # cc-option-yn
  106. # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
  107. cc-option-yn = $(call try-run,\
  108. $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
  109. # cc-disable-warning
  110. # Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
  111. cc-disable-warning = $(call try-run,\
  112. $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
  113. # cc-ifversion
  114. # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
  115. cc-ifversion = $(shell [ $(CONFIG_GCC_VERSION)0 $(1) $(2)000 ] && echo $(3) || echo $(4))
  116. # ld-option
  117. # Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
  118. ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
  119. # ld-version
  120. # Note this is mainly for HJ Lu's 3 number binutil versions
  121. ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh)
  122. # ld-ifversion
  123. # Usage: $(call ld-ifversion, -ge, 22252, y)
  124. ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
  125. ######
  126. ###
  127. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  128. # Usage:
  129. # $(Q)$(MAKE) $(build)=dir
  130. build := -f $(srctree)/scripts/Makefile.build obj
  131. ###
  132. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
  133. # Usage:
  134. # $(Q)$(MAKE) $(dtbinst)=dir
  135. dtbinst := -f $(srctree)/scripts/Makefile.dtbinst obj
  136. ###
  137. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
  138. # Usage:
  139. # $(Q)$(MAKE) $(clean)=dir
  140. clean := -f $(srctree)/scripts/Makefile.clean obj
  141. # echo command.
  142. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  143. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  144. echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
  145. # sink stdout for 'make -s'
  146. redirect :=
  147. quiet_redirect :=
  148. silent_redirect := exec >/dev/null;
  149. # printing commands
  150. cmd = @set -e; $(echo-cmd) $($(quiet)redirect) $(cmd_$(1))
  151. ###
  152. # if_changed - execute command if any prerequisite is newer than
  153. # target, or command line has changed
  154. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  155. # including used config symbols
  156. # if_changed_rule - as if_changed but execute rule instead
  157. # See Documentation/kbuild/makefiles.rst for more info
  158. ifneq ($(KBUILD_NOCMDDEP),1)
  159. # Check if both commands are the same including their order. Result is empty
  160. # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
  161. cmd-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
  162. $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
  163. else
  164. cmd-check = $(if $(strip $(cmd_$@)),,1)
  165. endif
  166. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  167. # (needed for make)
  168. # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
  169. # (needed for make)
  170. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  171. # (needed for the shell)
  172. make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
  173. # Find any prerequisites that are newer than target or that do not exist.
  174. # (This is not true for now; $? should contain any non-existent prerequisites,
  175. # but it does not work as expected when .SECONDARY is present. This seems a bug
  176. # of GNU Make.)
  177. # PHONY targets skipped in both cases.
  178. newer-prereqs = $(filter-out $(PHONY),$?)
  179. # Execute command if command has changed or prerequisite(s) are updated.
  180. if_changed = $(if $(newer-prereqs)$(cmd-check), \
  181. $(cmd); \
  182. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
  183. # Execute the command and also postprocess generated .d dependencies file.
  184. if_changed_dep = $(if $(newer-prereqs)$(cmd-check),$(cmd_and_fixdep),@:)
  185. cmd_and_fixdep = \
  186. $(cmd); \
  187. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).cmd;\
  188. rm -f $(depfile)
  189. # Usage: $(call if_changed_rule,foo)
  190. # Will check if $(cmd_foo) or any of the prerequisites changed,
  191. # and if so will execute $(rule_foo).
  192. if_changed_rule = $(if $(newer-prereqs)$(cmd-check),$(rule_$(1)),@:)
  193. ###
  194. # why - tell why a target got built
  195. # enabled by make V=2
  196. # Output (listed in the order they are checked):
  197. # (1) - due to target is PHONY
  198. # (2) - due to target missing
  199. # (3) - due to: file1.h file2.h
  200. # (4) - due to command line change
  201. # (5) - due to missing .cmd file
  202. # (6) - due to target not in $(targets)
  203. # (1) PHONY targets are always build
  204. # (2) No target, so we better build it
  205. # (3) Prerequisite is newer than target
  206. # (4) The command line stored in the file named dir/.target.cmd
  207. # differed from actual command line. This happens when compiler
  208. # options changes
  209. # (5) No dir/.target.cmd file (used to store command line)
  210. # (6) No dir/.target.cmd file and target not listed in $(targets)
  211. # This is a good hint that there is a bug in the kbuild file
  212. ifeq ($(KBUILD_VERBOSE),2)
  213. why = \
  214. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  215. $(if $(wildcard $@), \
  216. $(if $(newer-prereqs),- due to: $(newer-prereqs), \
  217. $(if $(cmd-check), \
  218. $(if $(cmd_$@),- due to command line change, \
  219. $(if $(filter $@, $(targets)), \
  220. - due to missing .cmd file, \
  221. - due to $(notdir $@) not in $$(targets) \
  222. ) \
  223. ) \
  224. ) \
  225. ), \
  226. - due to target missing \
  227. ) \
  228. )
  229. echo-why = $(call escsq, $(strip $(why)))
  230. endif
  231. ###############################################################################
  232. #
  233. # When a Kconfig string contains a filename, it is suitable for
  234. # passing to shell commands. It is surrounded by double-quotes, and
  235. # any double-quotes or backslashes within it are escaped by
  236. # backslashes.
  237. #
  238. # This is no use for dependencies or $(wildcard). We need to strip the
  239. # surrounding quotes and the escaping from quotes and backslashes, and
  240. # we *do* need to escape any spaces in the string. So, for example:
  241. #
  242. # Usage: $(eval $(call config_filename,FOO))
  243. #
  244. # Defines FOO_FILENAME based on the contents of the CONFIG_FOO option,
  245. # transformed as described above to be suitable for use within the
  246. # makefile.
  247. #
  248. # Also, if the filename is a relative filename and exists in the source
  249. # tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to
  250. # be prefixed to *both* command invocation and dependencies.
  251. #
  252. # Note: We also print the filenames in the quiet_cmd_foo text, and
  253. # perhaps ought to have a version specially escaped for that purpose.
  254. # But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good
  255. # enough. It'll strip the quotes in the common case where there's no
  256. # space and it's a simple filename, and it'll retain the quotes when
  257. # there's a space. There are some esoteric cases in which it'll print
  258. # the wrong thing, but we don't really care. The actual dependencies
  259. # and commands *do* get it right, with various combinations of single
  260. # and double quotes, backslashes and spaces in the filenames.
  261. #
  262. ###############################################################################
  263. #
  264. define config_filename
  265. ifneq ($$(CONFIG_$(1)),"")
  266. $(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1)))))))
  267. ifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME)))
  268. else
  269. ifeq ($$(wildcard $$($(1)_FILENAME)),)
  270. ifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),)
  271. $(1)_SRCPREFIX := $(srctree)/
  272. endif
  273. endif
  274. endif
  275. endif
  276. endef
  277. #
  278. ###############################################################################
  279. # delete partially updated (i.e. corrupted) files on error
  280. .DELETE_ON_ERROR:
  281. # do not delete intermediate files automatically
  282. .SECONDARY: