Kbuild.include 10 KB

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