Kbuild.include 9.9 KB

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