Kbuild.include 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. ####
  2. # kbuild: Generic definitions
  3. # Convenient variables
  4. comma := ,
  5. squote := '
  6. empty :=
  7. space := $(empty) $(empty)
  8. ###
  9. # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  10. dot-target = $(dir $@).$(notdir $@)
  11. ###
  12. # The temporary file to save gcc -MD generated dependencies must not
  13. # contain a comma
  14. depfile = $(subst $(comma),_,$(dot-target).d)
  15. ###
  16. # filename of target with directory and extension stripped
  17. basetarget = $(basename $(notdir $@))
  18. ###
  19. # filename of first prerequisite with directory and extension stripped
  20. baseprereq = $(basename $(notdir $<))
  21. ###
  22. # Escape single quote for use in echo statements
  23. escsq = $(subst $(squote),'\$(squote)',$1)
  24. ###
  25. # Easy method for doing a status message
  26. kecho := :
  27. quiet_kecho := echo
  28. silent_kecho := :
  29. kecho := $($(quiet)kecho)
  30. ###
  31. # filechk is used to check if the content of a generated file is updated.
  32. # Sample usage:
  33. # define filechk_sample
  34. # echo $KERNELRELEASE
  35. # endef
  36. # version.h : Makefile
  37. # $(call filechk,sample)
  38. # The rule defined shall write to stdout the content of the new file.
  39. # The existing file will be compared with the new one.
  40. # - If no file exist it is created
  41. # - If the content differ the new file is used
  42. # - If they are equal no change, and no timestamp update
  43. # - stdin is piped in from the first prerequisite ($<) so one has
  44. # to specify a valid file as first prerequisite (often the kbuild file)
  45. define filechk
  46. $(Q)set -e; \
  47. $(kecho) ' CHK $@'; \
  48. mkdir -p $(dir $@); \
  49. $(filechk_$(1)) < $< > $@.tmp; \
  50. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  51. rm -f $@.tmp; \
  52. else \
  53. $(kecho) ' UPD $@'; \
  54. mv -f $@.tmp $@; \
  55. fi
  56. endef
  57. ######
  58. # gcc support functions
  59. # See documentation in Documentation/kbuild/makefiles.txt
  60. # cc-cross-prefix
  61. # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
  62. # Return first prefix where a prefix$(CC) is found in PATH.
  63. # If no $(CC) found in PATH with listed prefixes return nothing
  64. cc-cross-prefix = \
  65. $(word 1, $(foreach c,$(1), \
  66. $(shell set -e; \
  67. if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
  68. echo $(c); \
  69. fi)))
  70. # output directory for tests below
  71. TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
  72. # try-run
  73. # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
  74. # Exit code chooses option. "$$TMP" is can be used as temporary file and
  75. # is automatically cleaned up.
  76. try-run = $(shell set -e; \
  77. TMP="$(TMPOUT).$$$$.tmp"; \
  78. TMPO="$(TMPOUT).$$$$.o"; \
  79. if ($(1)) >/dev/null 2>&1; \
  80. then echo "$(2)"; \
  81. else echo "$(3)"; \
  82. fi; \
  83. rm -f "$$TMP" "$$TMPO")
  84. # as-option
  85. # Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
  86. as-option = $(call try-run,\
  87. $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
  88. # as-instr
  89. # Usage: cflags-y += $(call as-instr,instr,option1,option2)
  90. as-instr = $(call try-run,\
  91. printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
  92. # __cc-option
  93. # Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
  94. __cc-option = $(call try-run,\
  95. $(1) $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
  96. # Do not attempt to build with gcc plugins during cc-option tests.
  97. # (And this uses delayed resolution so the flags will be up to date.)
  98. CC_OPTION_CFLAGS = $(filter-out $(GCC_PLUGINS_CFLAGS),$(KBUILD_CFLAGS))
  99. # cc-option
  100. # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
  101. cc-option = $(call __cc-option, $(CC),\
  102. $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
  103. # hostcc-option
  104. # Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
  105. hostcc-option = $(call __cc-option, $(HOSTCC),\
  106. $(HOSTCFLAGS) $(HOST_EXTRACFLAGS),$(1),$(2))
  107. # cc-option-yn
  108. # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
  109. cc-option-yn = $(call try-run,\
  110. $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
  111. # cc-option-align
  112. # Prefix align with either -falign or -malign
  113. cc-option-align = $(subst -functions=0,,\
  114. $(call cc-option,-falign-functions=0,-malign-functions=0))
  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) $(KBUILD_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. # Usage gcc-ver := $(call cc-version)
  124. cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
  125. # cc-fullversion
  126. # Usage gcc-ver := $(call cc-fullversion)
  127. cc-fullversion = $(shell $(CONFIG_SHELL) \
  128. $(srctree)/scripts/gcc-version.sh -p $(CC))
  129. # cc-ifversion
  130. # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
  131. cc-ifversion = $(shell [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3))
  132. # cc-ldoption
  133. # Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
  134. cc-ldoption = $(call try-run,\
  135. $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
  136. # ld-option
  137. # Usage: LDFLAGS += $(call ld-option, -X)
  138. ld-option = $(call try-run,\
  139. $(CC) /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2))
  140. # ar-option
  141. # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
  142. # Important: no spaces around options
  143. ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
  144. ######
  145. ###
  146. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  147. # Usage:
  148. # $(Q)$(MAKE) $(build)=dir
  149. build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
  150. ###
  151. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
  152. # Usage:
  153. # $(Q)$(MAKE) $(modbuiltin)=dir
  154. modbuiltin := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.modbuiltin obj
  155. # Prefix -I with $(srctree) if it is not an absolute path.
  156. # skip if -I has no parameter
  157. addtree = $(if $(patsubst -I%,%,$(1)), \
  158. $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1))
  159. # Find all -I options and call addtree
  160. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  161. # echo command.
  162. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  163. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  164. echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
  165. # printing commands
  166. cmd = @$(echo-cmd) $(cmd_$(1))
  167. # Add $(obj)/ for paths that are not absolute
  168. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  169. ###
  170. # if_changed - execute command if any prerequisite is newer than
  171. # target, or command line has changed
  172. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  173. # including used config symbols
  174. # if_changed_rule - as if_changed but execute rule instead
  175. # See Documentation/kbuild/makefiles.txt for more info
  176. ifneq ($(KBUILD_NOCMDDEP),1)
  177. # Check if both arguments has same arguments. Result is empty string if equal.
  178. # User may override this check using make KBUILD_NOCMDDEP=1
  179. arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
  180. $(filter-out $(cmd_$@), $(cmd_$(1))) )
  181. else
  182. arg-check = $(if $(strip $(cmd_$@)),,1)
  183. endif
  184. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  185. # (needed for make)
  186. # Replace >#< with >\#< to avoid starting a comment in the .cmd file
  187. # (needed for make)
  188. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  189. # (needed for the shell)
  190. make-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
  191. # Find any prerequisites that is newer than target or that does not exist.
  192. # PHONY targets skipped in both cases.
  193. any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
  194. # Execute command if command has changed or prerequisite(s) are updated.
  195. #
  196. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  197. @set -e; \
  198. $(echo-cmd) $(cmd_$(1)); \
  199. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
  200. # Execute the command and also postprocess generated .d dependencies file.
  201. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
  202. @set -e; \
  203. $(echo-cmd) $(cmd_$(1)); \
  204. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
  205. rm -f $(depfile); \
  206. mv -f $(dot-target).tmp $(dot-target).cmd)
  207. # Usage: $(call if_changed_rule,foo)
  208. # Will check if $(cmd_foo) or any of the prerequisites changed,
  209. # and if so will execute $(rule_foo).
  210. if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
  211. @set -e; \
  212. $(rule_$(1)))
  213. ###
  214. # why - tell why a a target got build
  215. # enabled by make V=2
  216. # Output (listed in the order they are checked):
  217. # (1) - due to target is PHONY
  218. # (2) - due to target missing
  219. # (3) - due to: file1.h file2.h
  220. # (4) - due to command line change
  221. # (5) - due to missing .cmd file
  222. # (6) - due to target not in $(targets)
  223. # (1) PHONY targets are always build
  224. # (2) No target, so we better build it
  225. # (3) Prerequisite is newer than target
  226. # (4) The command line stored in the file named dir/.target.cmd
  227. # differed from actual command line. This happens when compiler
  228. # options changes
  229. # (5) No dir/.target.cmd file (used to store command line)
  230. # (6) No dir/.target.cmd file and target not listed in $(targets)
  231. # This is a good hint that there is a bug in the kbuild file
  232. ifeq ($(KBUILD_VERBOSE),2)
  233. why = \
  234. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  235. $(if $(wildcard $@), \
  236. $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
  237. $(if $(arg-check), \
  238. $(if $(cmd_$@),- due to command line change, \
  239. $(if $(filter $@, $(targets)), \
  240. - due to missing .cmd file, \
  241. - due to $(notdir $@) not in $$(targets) \
  242. ) \
  243. ) \
  244. ) \
  245. ), \
  246. - due to target missing \
  247. ) \
  248. )
  249. echo-why = $(call escsq, $(strip $(why)))
  250. endif