Makefile.lib 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # ===========================================================================
  2. # kbuild: Generic definitions
  3. # ===========================================================================
  4. # Standard vars
  5. comma := ,
  6. empty :=
  7. space := $(empty) $(empty)
  8. # Backward compatibility - to be removed...
  9. extra-y += $(EXTRA_TARGETS)
  10. # Figure out what we need to build from the various variables
  11. # ===========================================================================
  12. # When an object is listed to be built compiled-in and modular,
  13. # only build the compiled-in version
  14. obj-m := $(filter-out $(obj-y),$(obj-m))
  15. # Libraries are always collected in one lib file.
  16. # Filter out objects already built-in
  17. lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
  18. # Handle objects in subdirs
  19. # ---------------------------------------------------------------------------
  20. # o if we encounter foo/ in $(obj-y), replace it by foo/built-in.o
  21. # and add the directory to the list of dirs to descend into: $(subdir-y)
  22. # o if we encounter foo/ in $(obj-m), remove it from $(obj-m)
  23. # and add the directory to the list of dirs to descend into: $(subdir-m)
  24. __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y)))
  25. subdir-y += $(__subdir-y)
  26. __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m)))
  27. subdir-m += $(__subdir-m)
  28. obj-y := $(patsubst %/, %/built-in.o, $(obj-y))
  29. obj-m := $(filter-out %/, $(obj-m))
  30. # Subdirectories we need to descend into
  31. subdir-ym := $(sort $(subdir-y) $(subdir-m))
  32. # if $(foo-objs) exists, foo.o is a composite object
  33. multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
  34. multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))), $(m))))
  35. multi-used := $(multi-used-y) $(multi-used-m)
  36. single-used-m := $(sort $(filter-out $(multi-used-m),$(obj-m)))
  37. # Build list of the parts of our composite objects, our composite
  38. # objects depend on those (obviously)
  39. multi-objs-y := $(foreach m, $(multi-used-y), $($(m:.o=-objs)) $($(m:.o=-y)))
  40. multi-objs-m := $(foreach m, $(multi-used-m), $($(m:.o=-objs)) $($(m:.o=-y)))
  41. multi-objs := $(multi-objs-y) $(multi-objs-m)
  42. # $(subdir-obj-y) is the list of objects in $(obj-y) which do not live
  43. # in the local directory
  44. subdir-obj-y := $(foreach o,$(obj-y),$(if $(filter-out $(o),$(notdir $(o))),$(o)))
  45. # $(obj-dirs) is a list of directories that contain object files
  46. obj-dirs := $(dir $(multi-objs) $(subdir-obj-y))
  47. # Replace multi-part objects by their individual parts, look at local dir only
  48. real-objs-y := $(foreach m, $(filter-out $(subdir-obj-y), $(obj-y)), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) $(extra-y)
  49. real-objs-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
  50. # Add subdir path
  51. extra-y := $(addprefix $(obj)/,$(extra-y))
  52. always := $(addprefix $(obj)/,$(always))
  53. targets := $(addprefix $(obj)/,$(targets))
  54. obj-y := $(addprefix $(obj)/,$(obj-y))
  55. obj-m := $(addprefix $(obj)/,$(obj-m))
  56. lib-y := $(addprefix $(obj)/,$(lib-y))
  57. subdir-obj-y := $(addprefix $(obj)/,$(subdir-obj-y))
  58. real-objs-y := $(addprefix $(obj)/,$(real-objs-y))
  59. real-objs-m := $(addprefix $(obj)/,$(real-objs-m))
  60. single-used-m := $(addprefix $(obj)/,$(single-used-m))
  61. multi-used-y := $(addprefix $(obj)/,$(multi-used-y))
  62. multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
  63. multi-objs-y := $(addprefix $(obj)/,$(multi-objs-y))
  64. multi-objs-m := $(addprefix $(obj)/,$(multi-objs-m))
  65. subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
  66. obj-dirs := $(addprefix $(obj)/,$(obj-dirs))
  67. # The temporary file to save gcc -MD generated dependencies must not
  68. # contain a comma
  69. depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
  70. # These flags are needed for modversions and compiling, so we define them here
  71. # already
  72. # $(modname_flags) #defines KBUILD_MODNAME as the name of the module it will
  73. # end up in (or would, if it gets compiled in)
  74. # Note: It's possible that one object gets potentially linked into more
  75. # than one module. In that case KBUILD_MODNAME will be set to foo_bar,
  76. # where foo and bar are the name of the modules.
  77. basename_flags = -DKBUILD_BASENAME=$(subst $(comma),_,$(subst -,_,$(*F)))
  78. modname_flags = $(if $(filter 1,$(words $(modname))),-DKBUILD_MODNAME=$(subst $(comma),_,$(subst -,_,$(modname))))
  79. _c_flags = $(CFLAGS) $(EXTRA_CFLAGS) $(CFLAGS_$(*F).o)
  80. _a_flags = $(AFLAGS) $(EXTRA_AFLAGS) $(AFLAGS_$(*F).o)
  81. _cpp_flags = $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CPPFLAGS_$(@F))
  82. # If building the kernel in a separate objtree expand all occurrences
  83. # of -Idir to -I$(srctree)/dir except for absolute paths (starting with '/').
  84. ifeq ($(KBUILD_SRC),)
  85. __c_flags = $(_c_flags)
  86. __a_flags = $(_a_flags)
  87. __cpp_flags = $(_cpp_flags)
  88. else
  89. # Prefix -I with $(srctree) if it is not an absolute path
  90. addtree = $(if $(filter-out -I/%,$(1)),$(patsubst -I%,-I$(srctree)/%,$(1))) $(1)
  91. # Find all -I options and call addtree
  92. flags = $(foreach o,$($(1)),$(if $(filter -I%,$(o)),$(call addtree,$(o)),$(o)))
  93. # -I$(obj) locates generated .h files
  94. # $(call addtree,-I$(obj)) locates .h files in srctree, from generated .c files
  95. # and locates generated .h files
  96. # FIXME: Replace both with specific CFLAGS* statements in the makefiles
  97. __c_flags = $(call addtree,-I$(obj)) $(call flags,_c_flags)
  98. __a_flags = $(call flags,_a_flags)
  99. __cpp_flags = $(call flags,_cpp_flags)
  100. endif
  101. c_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
  102. $(__c_flags) $(modkern_cflags) \
  103. $(basename_flags) $(modname_flags)
  104. a_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(CPPFLAGS) \
  105. $(__a_flags) $(modkern_aflags)
  106. cpp_flags = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(__cpp_flags)
  107. ld_flags = $(LDFLAGS) $(EXTRA_LDFLAGS)
  108. # Finds the multi-part object the current object will be linked into
  109. modname-multi = $(sort $(foreach m,$(multi-used),\
  110. $(if $(filter $(subst $(obj)/,,$*.o), $($(m:.o=-objs)) $($(m:.o=-y))),$(m:.o=))))
  111. # Shipped files
  112. # ===========================================================================
  113. quiet_cmd_shipped = SHIPPED $@
  114. cmd_shipped = cat $< > $@
  115. $(obj)/%:: $(src)/%_shipped
  116. $(call cmd,shipped)
  117. # Commands useful for building a boot image
  118. # ===========================================================================
  119. #
  120. # Use as following:
  121. #
  122. # target: source(s) FORCE
  123. # $(if_changed,ld/objcopy/gzip)
  124. #
  125. # and add target to EXTRA_TARGETS so that we know we have to
  126. # read in the saved command line
  127. # Linking
  128. # ---------------------------------------------------------------------------
  129. quiet_cmd_ld = LD $@
  130. cmd_ld = $(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) $(LDFLAGS_$(@F)) \
  131. $(filter-out FORCE,$^) -o $@
  132. # Objcopy
  133. # ---------------------------------------------------------------------------
  134. quiet_cmd_objcopy = OBJCOPY $@
  135. cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
  136. # Gzip
  137. # ---------------------------------------------------------------------------
  138. quiet_cmd_gzip = GZIP $@
  139. cmd_gzip = gzip -f -9 < $< > $@
  140. # ===========================================================================
  141. # Generic stuff
  142. # ===========================================================================
  143. ifneq ($(KBUILD_NOCMDDEP),1)
  144. # Check if both arguments has same arguments. Result in empty string if equal
  145. # User may override this check using make KBUILD_NOCMDDEP=1
  146. arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
  147. endif
  148. # echo command. Short version is $(quiet) equals quiet, otherwise full command
  149. echo-cmd = $(if $($(quiet)cmd_$(1)), \
  150. echo ' $(subst ','\'',$($(quiet)cmd_$(1)))';)
  151. # function to only execute the passed command if necessary
  152. # >'< substitution is for echo to work, >$< substitution to preserve $ when reloading .cmd file
  153. # note: when using inline perl scripts [perl -e '...$$t=1;...'] in $(cmd_xxx) double $$ your perl vars
  154. #
  155. if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  156. @set -e; \
  157. $(echo-cmd) \
  158. $(cmd_$(1)); \
  159. echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).cmd)
  160. # execute the command and also postprocess generated .d dependencies
  161. # file
  162. if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^)\
  163. $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
  164. @set -e; \
  165. $(echo-cmd) \
  166. $(cmd_$(1)); \
  167. scripts/basic/fixdep $(depfile) $@ '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
  168. rm -f $(depfile); \
  169. mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
  170. # Usage: $(call if_changed_rule,foo)
  171. # will check if $(cmd_foo) changed, or any of the prequisites changed,
  172. # and if so will execute $(rule_foo)
  173. if_changed_rule = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),\
  174. @set -e; \
  175. $(rule_$(1)))
  176. # If quiet is set, only print short version of command
  177. cmd = @$(if $($(quiet)cmd_$(1)),echo ' $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
  178. # $(call descend,<dir>,<target>)
  179. # Recursively call a sub-make in <dir> with target <target>
  180. # Usage is deprecated, because make do not see this as an invocation of make.
  181. descend =$(Q)$(MAKE) -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj=$(1) $(2)
  182. # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.build obj=
  183. # Usage:
  184. # $(Q)$(MAKE) $(build)=dir
  185. build := -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Makefile.build obj
  186. # filechk is used to check if the content of a generated file is updated.
  187. # Sample usage:
  188. # define filechk_sample
  189. # echo $KERNELRELEASE
  190. # endef
  191. # version.h : Makefile
  192. # $(call filechk,sample)
  193. # The rule defined shall write to stdout the content of the new file.
  194. # The existing file will be compared with the new one.
  195. # - If no file exist it is created
  196. # - If the content differ the new file is used
  197. # - If they are equal no change, and no timestamp update
  198. define filechk
  199. $(Q)set -e; \
  200. echo ' CHK $@'; \
  201. mkdir -p $(dir $@); \
  202. $(filechk_$(1)) $(2) > $@.tmp; \
  203. if [ -r $@ ] && cmp -s $@ $@.tmp; then \
  204. rm -f $@.tmp; \
  205. else \
  206. echo ' UPD $@'; \
  207. mv -f $@.tmp $@; \
  208. fi
  209. endef