Kbuild.include 11 KB

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