Kbuild.include 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
  94. cc-option = $(call try-run,\
  95. $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
  96. # cc-option-yn
  97. # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
  98. cc-option-yn = $(call try-run,\
  99. $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
  100. # cc-option-align
  101. # Prefix align with either -falign or -malign
  102. cc-option-align = $(subst -functions=0,,\
  103. $(call cc-option,-falign-functions=0,-malign-functions=0))
  104. # cc-disable-warning
  105. # Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
  106. cc-disable-warning = $(call try-run,\
  107. $(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
  108. # cc-version
  109. # Usage gcc-ver := $(call cc-version)
  110. cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
  111. # cc-fullversion
  112. # Usage gcc-ver := $(call 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 [ $(call cc-version, $(CC)) $(1) $(2) ] && echo $(3))
  118. # added for U-Boot
  119. binutils-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/binutils-version.sh $(AS))
  120. dtc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/dtc-version.sh $(DTC))
  121. # cc-ldoption
  122. # Usage: ldflags += $(call cc-ldoption, -Wl$(comma)--hash-style=both)
  123. cc-ldoption = $(call try-run,\
  124. $(CC) $(1) -nostdlib -x c /dev/null -o "$$TMP",$(1),$(2))
  125. # ld-option
  126. # Usage: LDFLAGS += $(call ld-option, -X)
  127. ld-option = $(call try-run,\
  128. $(CC) -x c /dev/null -c -o "$$TMPO" ; $(LD) $(1) "$$TMPO" -o "$$TMP",$(1),$(2))
  129. # ar-option
  130. # Usage: KBUILD_ARFLAGS := $(call ar-option,D)
  131. # Important: no spaces around options
  132. ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2))
  133. ######
  134. ###
  135. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  136. # Usage:
  137. # $(Q)$(MAKE) $(build)=dir
  138. build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
  139. ###
  140. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj=
  141. # Usage:
  142. # $(Q)$(MAKE) $(modbuiltin)=dir
  143. modbuiltin := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.modbuiltin obj
  144. # Prefix -I with $(srctree) if it is not an absolute path.
  145. # skip if -I has no parameter
  146. addtree = $(if $(patsubst -I%,%,$(1)), \
  147. $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1))
  148. # Find all -I options and call addtree
  149. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  150. # echo command.
  151. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  152. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  153. echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
  154. # printing commands
  155. cmd = @$(echo-cmd) $(cmd_$(1))
  156. # Add $(obj)/ for paths that are not absolute
  157. objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
  158. ###
  159. # if_changed - execute command if any prerequisite is newer than
  160. # target, or command line has changed
  161. # if_changed_dep - as if_changed, but uses fixdep to reveal dependencies
  162. # including used config symbols
  163. # if_changed_rule - as if_changed but execute rule instead
  164. # See Documentation/kbuild/makefiles.txt for more info
  165. ifneq ($(KBUILD_NOCMDDEP),1)
  166. # Check if both arguments has same arguments. Result is empty string if equal.
  167. # User may override this check using make KBUILD_NOCMDDEP=1
  168. arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
  169. $(filter-out $(cmd_$@), $(cmd_$(1))) )
  170. else
  171. arg-check = $(if $(strip $(cmd_$@)),,1)
  172. endif
  173. # >'< substitution is for echo to work,
  174. # >$< substitution to preserve $ when reloading .cmd file
  175. # note: when using inline perl scripts [perl -e '...$$t=1;...']
  176. # in $(cmd_xxx) double $$ your perl vars
  177. make-cmd = $(subst \\,\\\\,$(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))))
  178. # Find any prerequisites that is newer than target or that does not exist.
  179. # PHONY targets skipped in both cases.
  180. any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
  181. # Execute command if command has changed or prerequisite(s) are updated.
  182. #
  183. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  184. @set -e; \
  185. $(echo-cmd) $(cmd_$(1)); \
  186. echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
  187. # Execute the command and also postprocess generated .d dependencies file.
  188. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check) ), \
  189. @set -e; \
  190. $(echo-cmd) $(cmd_$(1)); \
  191. scripts/basic/fixdep $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;\
  192. rm -f $(depfile); \
  193. mv -f $(dot-target).tmp $(dot-target).cmd)
  194. # Usage: $(call if_changed_rule,foo)
  195. # Will check if $(cmd_foo) or any of the prerequisites changed,
  196. # and if so will execute $(rule_foo).
  197. if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
  198. @set -e; \
  199. $(rule_$(1)))
  200. ###
  201. # why - tell why a a target got build
  202. # enabled by make V=2
  203. # Output (listed in the order they are checked):
  204. # (1) - due to target is PHONY
  205. # (2) - due to target missing
  206. # (3) - due to: file1.h file2.h
  207. # (4) - due to command line change
  208. # (5) - due to missing .cmd file
  209. # (6) - due to target not in $(targets)
  210. # (1) PHONY targets are always build
  211. # (2) No target, so we better build it
  212. # (3) Prerequisite is newer than target
  213. # (4) The command line stored in the file named dir/.target.cmd
  214. # differed from actual command line. This happens when compiler
  215. # options changes
  216. # (5) No dir/.target.cmd file (used to store command line)
  217. # (6) No dir/.target.cmd file and target not listed in $(targets)
  218. # This is a good hint that there is a bug in the kbuild file
  219. ifeq ($(KBUILD_VERBOSE),2)
  220. why = \
  221. $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
  222. $(if $(wildcard $@), \
  223. $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
  224. $(if $(arg-check), \
  225. $(if $(cmd_$@),- due to command line change, \
  226. $(if $(filter $@, $(targets)), \
  227. - due to missing .cmd file, \
  228. - due to $(notdir $@) not in $$(targets) \
  229. ) \
  230. ) \
  231. ) \
  232. ), \
  233. - due to target missing \
  234. ) \
  235. )
  236. echo-why = $(call escsq, $(strip $(why)))
  237. endif