Kbuild.include 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. ####
  2. # kbuild: Generic definitions
  3. # Convenient variables
  4. comma := ,
  5. quote := "
  6. squote := '
  7. empty :=
  8. space := $(empty) $(empty)
  9. space_escape := _-_SPACE_-_
  10. pound := \#
  11. ###
  12. # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  13. dot-target = $(dir $@).$(notdir $@)
  14. ###
  15. # The temporary file to save gcc -MD generated dependencies must not
  16. # contain a comma
  17. depfile = $(subst $(comma),_,$(dot-target).d)
  18. ###
  19. # filename of target with directory and extension stripped
  20. basetarget = $(basename $(notdir $@))
  21. ###
  22. # filename of first prerequisite with directory and extension stripped
  23. baseprereq = $(basename $(notdir $<))
  24. ###
  25. # Escape single quote for use in echo statements
  26. escsq = $(subst $(squote),'\$(squote)',$1)
  27. ###
  28. # Easy method for doing a status message
  29. kecho := :
  30. quiet_kecho := echo
  31. silent_kecho := :
  32. kecho := $($(quiet)kecho)
  33. ###
  34. # filechk is used to check if the content of a generated file is updated.
  35. # Sample usage:
  36. # define filechk_sample
  37. # echo $KERNELRELEASE
  38. # endef
  39. # version.h : Makefile
  40. # $(call filechk,sample)
  41. # The rule defined shall write to stdout the content of the new file.
  42. # The existing file will be compared with the new one.
  43. # - If no file exist it is created
  44. # - If the content differ the new file is used
  45. # - If they are equal no change, and no timestamp update
  46. # - stdin is piped in from the first prerequisite ($<) so one has
  47. # to specify a valid file as first prerequisite (often the kbuild file)
  48. define filechk
  49. $(Q)set -e; \
  50. $(kecho) ' CHK $@'; \
  51. mkdir -p $(dir $@); \
  52. $(filechk_$(1)) < $< > $@.tmp; \
  53. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  54. rm -f $@.tmp; \
  55. else \
  56. $(kecho) ' UPD $@'; \
  57. mv -f $@.tmp $@; \
  58. fi
  59. endef
  60. ######
  61. # gcc support functions
  62. # See documentation in Documentation/kbuild/makefiles.txt
  63. # cc-cross-prefix
  64. # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
  65. # Return first prefix where a prefix$(CC) is found in PATH.
  66. # If no $(CC) found in PATH with listed prefixes return nothing
  67. cc-cross-prefix = \
  68. $(word 1, $(foreach c,$(1), \
  69. $(shell set -e; \
  70. if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
  71. echo $(c); \
  72. fi)))
  73. # output directory for tests below
  74. TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
  75. # try-run
  76. # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
  77. # Exit code chooses option. "$$TMP" serves as a temporary file and is
  78. # automatically cleaned up.
  79. try-run = $(shell set -e; \
  80. TMP=$(TMPOUT)/tmp; \
  81. TMPO=$(TMPOUT)/tmp.o; \
  82. mkdir -p $(TMPOUT); \
  83. trap "rm -rf $(TMPOUT)" EXIT; \
  84. if ($(1)) >/dev/null 2>&1; \
  85. then echo "$(2)"; \
  86. else echo "$(3)"; \
  87. fi)
  88. # as-option
  89. # Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
  90. as-option = $(call try-run,\
  91. $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
  92. # as-instr
  93. # Usage: cflags-y += $(call as-instr,instr,option1,option2)
  94. as-instr = $(call try-run,\
  95. printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
  96. # __cc-option
  97. # Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
  98. __cc-option = $(call try-run,\
  99. $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
  100. # Do not attempt to build with gcc plugins during cc-option tests.
  101. # (And this uses delayed resolution so the flags will be up to date.)
  102. CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
  103. # cc-option
  104. # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
  105. cc-option = $(call __cc-option, $(CC),\
  106. $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS),$(1),$(2))
  107. # hostcc-option
  108. # Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
  109. hostcc-option = $(call __cc-option, $(HOSTCC),\
  110. $(HOSTCFLAGS) $(HOST_EXTRACFLAGS),$(1),$(2))
  111. # cc-option-yn
  112. # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
  113. cc-option-yn = $(call try-run,\
  114. $(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
  115. # cc-disable-warning
  116. # Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
  117. cc-disable-warning = $(call try-run,\
  118. $(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
  119. # cc-name
  120. # Expands to either gcc or clang
  121. cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
  122. # __cc-version
  123. # Returns compiler version
  124. __cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/$(cc-name)-version.sh $(CC))
  125. # __cc-fullversion
  126. # Returns full compiler version
  127. __cc-fullversion = $(shell $(CONFIG_SHELL) \
  128. $(srctree)/scripts/$(cc-name)-version.sh -p $(CC))
  129. # __cc-ifversion
  130. # Matches compiler name and version
  131. # Usage: EXTRA_CFLAGS += $(call cc-if-name-version, gcc, -lt, 0402, -O1)
  132. __cc-ifversion = $(shell [ $(cc-name) = $(1) ] && [ $(__cc-version) $(2) $(3) ] && echo $(4) || echo $(5))
  133. # __cc-if-fullversion
  134. # Matches compiler name and full version
  135. # Usage: EXTRA_CFLAGS += $(call cc-if-name-fullversion, gcc, -lt, 040502, -O1)
  136. __cc-if-fullversion = $(shell [ $(cc-name) = $(1) ] && [ $(__cc-fullversion) $(2) $(3) ] && echo $(4) || echo $(5))
  137. # gcc-ifversion
  138. gcc-ifversion = $(call __cc-ifversion, gcc, $(1), $(2), $(3), $(4))
  139. # gcc-if-fullversion
  140. gcc-if-fullversion = (call __cc-if-fullversion, gcc, $(1), $(2), $(3), $(4))
  141. # clang-ifversion
  142. clang-ifversion = $(call __cc-ifversion, clang, $(1), $(2), $(3), $(4))
  143. # clang-if-fullversion
  144. clang-if-fullversion = (call __cc-if-fullversion, clang, $(1), $(2), $(3), $(4))
  145. # cc-version
  146. cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
  147. # cc-fullversion
  148. cc-fullversion = $(shell $(CONFIG_SHELL) \
  149. $(srctree)/scripts/gcc-version.sh -p $(CC))
  150. # backward compatibility
  151. cc-ifversion = $(gcc-ifversion)
  152. cc-if-fullversion = $(gcc-if-fullversion)
  153. # cc-if-fullversion
  154. # Usage: EXTRA_CFLAGS += $(call cc-if-fullversion, -lt, 040502, -O1)
  155. cc-if-fullversion = $(shell [ $(cc-fullversion) $(1) $(2) ] && echo $(3) || echo $(4))
  156. # cc-ldoption
  157. # Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
  158. cc-ldoption = $(call try-run,\
  159. $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
  160. # ld-option
  161. # Usage: LDFLAGS += $(call ld-option, -X)
  162. ld-option = $(call try-run, $(LD) $(LDFLAGS) $(1) -v,$(1),$(2))
  163. # ar-option
  164. # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
  165. # Important: no spaces around options
  166. ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
  167. # ld-name
  168. # Expands to bfd, gold, or lld
  169. ld-name = $(shell if $(LD) -v 2>&1 | grep -q "GNU gold"; then echo gold; elif $(LD) -v 2>&1 | grep -q "LLD"; then echo lld; else echo bfd; fi)
  170. # ld-version
  171. # Note this is mainly for HJ Lu's 3 number binutil versions
  172. ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh)
  173. # ld-ifversion
  174. # Usage: $(call ld-ifversion, -ge, 22252, y)
  175. ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
  176. # __ld-ifversion
  177. # Usage: $(call __ld-ifversion, gold, -ge, 112000000, y)
  178. __ld-ifversion = $(shell [ $(ld-name) = $(1) ] && [ $(ld-version) $(2) $(3) ] && echo $(4) || echo $(5))
  179. # bfd-ifversion
  180. # Usage: $(call bfd-ifversion, -ge, 227000000, y)
  181. bfd-ifversion = $(call __ld-ifversion, bfd, $(1), $(2), $(3), $(4))
  182. # gold-ifversion
  183. # Usage: $(call gold-ifversion, -ge, 112000000, y)
  184. gold-ifversion = $(call __ld-ifversion, gold, $(1), $(2), $(3), $(4))
  185. ######
  186. ###
  187. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  188. # Usage:
  189. # $(Q)$(MAKE) $(build)=dir
  190. build := -f $(srctree)/scripts/Makefile.build obj
  191. ###
  192. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
  193. # Usage:
  194. # $(Q)$(MAKE) $(modbuiltin)=dir
  195. modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj
  196. ###
  197. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
  198. # Usage:
  199. # $(Q)$(MAKE) $(dtbinst)=dir
  200. dtbinst := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.dtbinst obj
  201. ###
  202. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
  203. # Usage:
  204. # $(Q)$(MAKE) $(clean)=dir
  205. clean := -f $(srctree)/scripts/Makefile.clean obj
  206. ###
  207. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj=
  208. # Usage:
  209. # $(Q)$(MAKE) $(hdr-inst)=dir
  210. hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
  211. # Prefix -I with $(srctree) if it is not an absolute path.
  212. # skip if -I has no parameter
  213. addtree = $(if $(patsubst -I%,%,$(1)), \
  214. $(if $(filter-out -I/% -I./% -I../%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)))
  215. # Find all -I options and call addtree
  216. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  217. # echo command.
  218. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  219. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  220. echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
  221. # printing commands
  222. cmd = @$(echo-cmd) $(cmd_$(1))
  223. # Add $(obj)/ for paths that are not absolute
  224. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  225. ###
  226. # if_changed - execute command if any prerequisite is newer than
  227. # target, or command line has changed
  228. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  229. # including used config symbols
  230. # if_changed_rule - as if_changed but execute rule instead
  231. # See Documentation/kbuild/makefiles.txt for more info
  232. ifneq ($(KBUILD_NOCMDDEP),1)
  233. # Check if both arguments are the same including their order. Result is empty
  234. # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
  235. arg-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
  236. $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
  237. else
  238. arg-check = $(if $(strip $(cmd_$@)),,1)
  239. endif
  240. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  241. # (needed for make)
  242. # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
  243. # (needed for make)
  244. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  245. # (needed for the shell)
  246. make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
  247. # Find any prerequisites that is newer than target or that does not exist.
  248. # PHONY targets skipped in both cases.
  249. any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
  250. # Execute command if command has changed or prerequisite(s) are updated.
  251. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  252. @set -e; \
  253. $(echo-cmd) $(cmd_$(1)); \
  254. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
  255. # Execute the command and also postprocess generated .d dependencies file.
  256. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
  257. @set -e; \
  258. $(cmd_and_fixdep), @:)
  259. ifndef CONFIG_TRIM_UNUSED_KSYMS
  260. cmd_and_fixdep = \
  261. $(echo-cmd) $(cmd_$(1)); \
  262. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
  263. rm -f $(depfile); \
  264. mv -f $(dot-target).tmp $(dot-target).cmd;
  265. else
  266. # Filter out exported kernel symbol names from the preprocessor output.
  267. # See also __KSYM_DEPS__ in include/linux/export.h.
  268. # We disable the depfile generation here, so as not to overwrite the existing
  269. # depfile while fixdep is parsing it.
  270. flags_nodeps = $(filter-out -Wp$(comma)-M%, $($(1)))
  271. ksym_dep_filter = \
  272. case "$(1)" in \
  273. cc_*_c|cpp_i_c) \
  274. $(CPP) $(call flags_nodeps,c_flags) -D__KSYM_DEPS__ $< ;; \
  275. as_*_S|cpp_s_S) \
  276. $(CPP) $(call flags_nodeps,a_flags) -D__KSYM_DEPS__ $< ;; \
  277. boot*|build*|cpp_its_S|*cpp_lds_S|dtc|host*|vdso*) : ;; \
  278. *) echo "Don't know how to preprocess $(1)" >&2; false ;; \
  279. esac | tr ";" "\n" | sed -rn 's/^.*=== __KSYM_(.*) ===.*$$/KSYM_\1/p'
  280. cmd_and_fixdep = \
  281. $(echo-cmd) $(cmd_$(1)); \
  282. $(ksym_dep_filter) | \
  283. scripts/basic/fixdep -e $(depfile) $@ '$(make-cmd)' \
  284. > $(dot-target).tmp; \
  285. rm -f $(depfile); \
  286. mv -f $(dot-target).tmp $(dot-target).cmd;
  287. endif
  288. # Usage: $(call if_changed_rule,foo)
  289. # Will check if $(cmd_foo) or any of the prerequisites changed,
  290. # and if so will execute $(rule_foo).
  291. if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
  292. @set -e; \
  293. $(rule_$(1)), @:)
  294. ###
  295. # why - tell why a target got built
  296. # enabled by make V=2
  297. # Output (listed in the order they are checked):
  298. # (1) - due to target is PHONY
  299. # (2) - due to target missing
  300. # (3) - due to: file1.h file2.h
  301. # (4) - due to command line change
  302. # (5) - due to missing .cmd file
  303. # (6) - due to target not in $(targets)
  304. # (1) PHONY targets are always build
  305. # (2) No target, so we better build it
  306. # (3) Prerequisite is newer than target
  307. # (4) The command line stored in the file named dir/.target.cmd
  308. # differed from actual command line. This happens when compiler
  309. # options changes
  310. # (5) No dir/.target.cmd file (used to store command line)
  311. # (6) No dir/.target.cmd file and target not listed in $(targets)
  312. # This is a good hint that there is a bug in the kbuild file
  313. ifeq ($(KBUILD_VERBOSE),2)
  314. why = \
  315. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  316. $(if $(wildcard $@), \
  317. $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
  318. $(if $(arg-check), \
  319. $(if $(cmd_$@),- due to command line change, \
  320. $(if $(filter $@, $(targets)), \
  321. - due to missing .cmd file, \
  322. - due to $(notdir $@) not in $$(targets) \
  323. ) \
  324. ) \
  325. ) \
  326. ), \
  327. - due to target missing \
  328. ) \
  329. )
  330. echo-why = $(call escsq, $(strip $(why)))
  331. endif
  332. ###############################################################################
  333. #
  334. # When a Kconfig string contains a filename, it is suitable for
  335. # passing to shell commands. It is surrounded by double-quotes, and
  336. # any double-quotes or backslashes within it are escaped by
  337. # backslashes.
  338. #
  339. # This is no use for dependencies or $(wildcard). We need to strip the
  340. # surrounding quotes and the escaping from quotes and backslashes, and
  341. # we *do* need to escape any spaces in the string. So, for example:
  342. #
  343. # Usage: $(eval $(call config_filename,FOO))
  344. #
  345. # Defines FOO_FILENAME based on the contents of the CONFIG_FOO option,
  346. # transformed as described above to be suitable for use within the
  347. # makefile.
  348. #
  349. # Also, if the filename is a relative filename and exists in the source
  350. # tree but not the build tree, define FOO_SRCPREFIX as $(srctree)/ to
  351. # be prefixed to *both* command invocation and dependencies.
  352. #
  353. # Note: We also print the filenames in the quiet_cmd_foo text, and
  354. # perhaps ought to have a version specially escaped for that purpose.
  355. # But it's only cosmetic, and $(patsubst "%",%,$(CONFIG_FOO)) is good
  356. # enough. It'll strip the quotes in the common case where there's no
  357. # space and it's a simple filename, and it'll retain the quotes when
  358. # there's a space. There are some esoteric cases in which it'll print
  359. # the wrong thing, but we don't really care. The actual dependencies
  360. # and commands *do* get it right, with various combinations of single
  361. # and double quotes, backslashes and spaces in the filenames.
  362. #
  363. ###############################################################################
  364. #
  365. define config_filename
  366. ifneq ($$(CONFIG_$(1)),"")
  367. $(1)_FILENAME := $$(subst \\,\,$$(subst \$$(quote),$$(quote),$$(subst $$(space_escape),\$$(space),$$(patsubst "%",%,$$(subst $$(space),$$(space_escape),$$(CONFIG_$(1)))))))
  368. ifneq ($$(patsubst /%,%,$$(firstword $$($(1)_FILENAME))),$$(firstword $$($(1)_FILENAME)))
  369. else
  370. ifeq ($$(wildcard $$($(1)_FILENAME)),)
  371. ifneq ($$(wildcard $$(srctree)/$$($(1)_FILENAME)),)
  372. $(1)_SRCPREFIX := $(srctree)/
  373. endif
  374. endif
  375. endif
  376. endif
  377. endef
  378. #
  379. ###############################################################################
  380. # delete partially updated (i.e. corrupted) files on error
  381. .DELETE_ON_ERROR: