Kbuild.include 18 KB

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