Kbuild.include 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. mkdir -p $(dir $@); \
  51. $(filechk_$(1)) < $< > $@.tmp; \
  52. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  53. rm -f $@.tmp; \
  54. else \
  55. $(kecho) ' UPD $@'; \
  56. mv -f $@.tmp $@; \
  57. fi
  58. endef
  59. ######
  60. # gcc support functions
  61. # See documentation in Documentation/kbuild/makefiles.txt
  62. # cc-cross-prefix
  63. # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
  64. # Return first prefix where a prefix$(CC) is found in PATH.
  65. # If no $(CC) found in PATH with listed prefixes return nothing
  66. cc-cross-prefix = \
  67. $(word 1, $(foreach c,$(1), \
  68. $(shell set -e; \
  69. if (which $(strip $(c))$(CC)) > /dev/null 2>&1 ; then \
  70. echo $(c); \
  71. fi)))
  72. # output directory for tests below
  73. TMPOUT := $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/)
  74. # try-run
  75. # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
  76. # Exit code chooses option. "$$TMP" is can be used as temporary file and
  77. # is automatically cleaned up.
  78. # modifed for U-Boot: prevent cc-option from leaving .*.su files
  79. try-run = $(shell set -e; \
  80. TMP="$(TMPOUT).$$$$.tmp"; \
  81. TMPO="$(TMPOUT).$$$$.o"; \
  82. TMPSU="$(TMPOUT).$$$$.su"; \
  83. if ($(1)) >/dev/null 2>&1; \
  84. then echo "$(2)"; \
  85. else echo "$(3)"; \
  86. fi; \
  87. rm -f "$$TMP" "$$TMPO" "$$TMPSU")
  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. # cc-option
  101. # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
  102. cc-option = $(call __cc-option, $(CC),\
  103. $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
  104. # hostcc-option
  105. # Usage: cflags-y += $(call hostcc-option,-march=winchip-c6,-march=i586)
  106. hostcc-option = $(call __cc-option, $(HOSTCC),\
  107. $(HOSTCFLAGS) $(HOST_EXTRACFLAGS),$(1),$(2))
  108. # cc-option-yn
  109. # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
  110. cc-option-yn = $(call try-run,\
  111. $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
  112. # cc-disable-warning
  113. # Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
  114. cc-disable-warning = $(call try-run,\
  115. $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
  116. # cc-name
  117. # Expands to either gcc or clang
  118. cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
  119. # cc-version
  120. cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
  121. # cc-fullversion
  122. cc-fullversion = $(shell $(CONFIG_SHELL) \
  123. $(srctree)/scripts/gcc-version.sh -p $(CC))
  124. # cc-ifversion
  125. # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
  126. cc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4))
  127. # added for U-Boot
  128. binutils-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/binutils-version.sh $(AS))
  129. # cc-ldoption
  130. # Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
  131. cc-ldoption = $(call try-run,\
  132. $(CC) $(1) $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
  133. # ld-option
  134. # Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
  135. ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
  136. # ar-option
  137. # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
  138. # Important: no spaces around options
  139. ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
  140. # ld-version
  141. # Note this is mainly for HJ Lu's 3 number binutil versions
  142. ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh)
  143. # ld-ifversion
  144. # Usage: $(call ld-ifversion, -ge, 22252, y)
  145. ld-ifversion = $(shell [ $(ld-version) $(1) $(2) ] && echo $(3) || echo $(4))
  146. ######
  147. ###
  148. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  149. # Usage:
  150. # $(Q)$(MAKE) $(build)=dir
  151. build := -f $(srctree)/scripts/Makefile.build obj
  152. ###
  153. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
  154. # Usage:
  155. # $(Q)$(MAKE) $(modbuiltin)=dir
  156. modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj
  157. ###
  158. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj=
  159. # Usage:
  160. # $(Q)$(MAKE) $(dtbinst)=dir
  161. dtbinst := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.dtbinst obj
  162. ###
  163. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.clean obj=
  164. # Usage:
  165. # $(Q)$(MAKE) $(clean)=dir
  166. clean := -f $(srctree)/scripts/Makefile.clean obj
  167. ###
  168. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.headersinst obj=
  169. # Usage:
  170. # $(Q)$(MAKE) $(hdr-inst)=dir
  171. hdr-inst := -f $(srctree)/scripts/Makefile.headersinst obj
  172. # Prefix -I with $(srctree) if it is not an absolute path.
  173. # skip if -I has no parameter
  174. addtree = $(if $(patsubst -I%,%,$(1)), \
  175. $(if $(filter-out -I/% -I./% -I../%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1)),$(1)),$(1))
  176. # Find all -I options and call addtree
  177. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  178. # echo command.
  179. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  180. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  181. echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
  182. # printing commands
  183. cmd = @$(echo-cmd) $(cmd_$(1))
  184. # Add $(obj)/ for paths that are not absolute
  185. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  186. ###
  187. # if_changed - execute command if any prerequisite is newer than
  188. # target, or command line has changed
  189. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  190. # including used config symbols
  191. # if_changed_rule - as if_changed but execute rule instead
  192. # See Documentation/kbuild/makefiles.txt for more info
  193. ifneq ($(KBUILD_NOCMDDEP),1)
  194. # Check if both arguments are the same including their order. Result is empty
  195. # string if equal. User may override this check using make KBUILD_NOCMDDEP=1
  196. arg-check = $(filter-out $(subst $(space),$(space_escape),$(strip $(cmd_$@))), \
  197. $(subst $(space),$(space_escape),$(strip $(cmd_$1))))
  198. else
  199. arg-check = $(if $(strip $(cmd_$@)),,1)
  200. endif
  201. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  202. # (needed for make)
  203. # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
  204. # (needed for make)
  205. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  206. # (needed for the shell)
  207. make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
  208. # Find any prerequisites that is newer than target or that does not exist.
  209. # PHONY targets skipped in both cases.
  210. any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
  211. # Execute command if command has changed or prerequisite(s) are updated.
  212. #
  213. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  214. @set -e; \
  215. $(echo-cmd) $(cmd_$(1)); \
  216. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd, @:)
  217. # Execute the command and also postprocess generated .d dependencies file.
  218. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
  219. @set -e; \
  220. $(echo-cmd) $(cmd_$(1)); \
  221. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
  222. rm -f $(depfile); \
  223. mv -f $(dot-target).tmp $(dot-target).cmd, @:)
  224. # Usage: $(call if_changed_rule,foo)
  225. # Will check if $(cmd_foo) or any of the prerequisites changed,
  226. # and if so will execute $(rule_foo).
  227. if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
  228. @set -e; \
  229. $(rule_$(1)), @:)
  230. ###
  231. # why - tell why a a target got build
  232. # enabled by make V=2
  233. # Output (listed in the order they are checked):
  234. # (1) - due to target is PHONY
  235. # (2) - due to target missing
  236. # (3) - due to: file1.h file2.h
  237. # (4) - due to command line change
  238. # (5) - due to missing .cmd file
  239. # (6) - due to target not in $(targets)
  240. # (1) PHONY targets are always build
  241. # (2) No target, so we better build it
  242. # (3) Prerequisite is newer than target
  243. # (4) The command line stored in the file named dir/.target.cmd
  244. # differed from actual command line. This happens when compiler
  245. # options changes
  246. # (5) No dir/.target.cmd file (used to store command line)
  247. # (6) No dir/.target.cmd file and target not listed in $(targets)
  248. # This is a good hint that there is a bug in the kbuild file
  249. ifeq ($(KBUILD_VERBOSE),2)
  250. why = \
  251. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  252. $(if $(wildcard $@), \
  253. $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
  254. $(if $(arg-check), \
  255. $(if $(cmd_$@),- due to command line change, \
  256. $(if $(filter $@, $(targets)), \
  257. - due to missing .cmd file, \
  258. - due to $(notdir $@) not in $$(targets) \
  259. ) \
  260. ) \
  261. ) \
  262. ), \
  263. - due to target missing \
  264. ) \
  265. )
  266. echo-why = $(call escsq, $(strip $(why)))
  267. endif
  268. # delete partially updated (i.e. corrupted) files on error
  269. .DELETE_ON_ERROR:
  270. # do not delete intermediate files automatically
  271. .SECONDARY:
  272. ifdef CONFIG_SPL_BUILD
  273. SPL_ := SPL_
  274. ifeq ($(CONFIG_TPL_BUILD),y)
  275. SPL_TPL_ := TPL_
  276. else
  277. SPL_TPL_ := SPL_
  278. endif
  279. else
  280. SPL_ :=
  281. SPL_TPL_ :=
  282. endif