Makefile.build 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # ==========================================================================
  2. # Building
  3. # ==========================================================================
  4. src := $(obj)
  5. .PHONY: __build
  6. __build:
  7. # Read .config if it exist, otherwise ignore
  8. -include .config
  9. include $(if $(wildcard $(obj)/Kbuild), $(obj)/Kbuild, $(obj)/Makefile)
  10. include scripts/Makefile.lib
  11. ifdef host-progs
  12. ifneq ($(hostprogs-y),$(host-progs))
  13. $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
  14. hostprogs-y += $(host-progs)
  15. endif
  16. endif
  17. # Do not include host rules unles needed
  18. ifneq ($(hostprogs-y)$(hostprogs-m),)
  19. include scripts/Makefile.host
  20. endif
  21. ifneq ($(KBUILD_SRC),)
  22. # Create output directory if not already present
  23. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  24. # Create directories for object files if directory does not exist
  25. # Needed when obj-y := dir/file.o syntax is used
  26. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  27. endif
  28. ifdef EXTRA_TARGETS
  29. $(warning kbuild: $(obj)/Makefile - Usage of EXTRA_TARGETS is obsolete in 2.6. Please fix!)
  30. endif
  31. ifdef build-targets
  32. $(warning kbuild: $(obj)/Makefile - Usage of build-targets is obsolete in 2.6. Please fix!)
  33. endif
  34. ifdef export-objs
  35. $(warning kbuild: $(obj)/Makefile - Usage of export-objs is obsolete in 2.6. Please fix!)
  36. endif
  37. ifdef O_TARGET
  38. $(warning kbuild: $(obj)/Makefile - Usage of O_TARGET := $(O_TARGET) is obsolete in 2.6. Please fix!)
  39. endif
  40. ifdef L_TARGET
  41. $(error kbuild: $(obj)/Makefile - Use of L_TARGET is replaced by lib-y in 2.6. Please fix!)
  42. endif
  43. ifdef list-multi
  44. $(warning kbuild: $(obj)/Makefile - list-multi := $(list-multi) is obsolete in 2.6. Please fix!)
  45. endif
  46. ifndef obj
  47. $(warning kbuild: Makefile.build is included improperly)
  48. endif
  49. # ===========================================================================
  50. ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)
  51. lib-target := $(obj)/lib.a
  52. endif
  53. ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(lib-target)),)
  54. builtin-target := $(obj)/built-in.o
  55. endif
  56. # We keep a list of all modules in $(MODVERDIR)
  57. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  58. $(if $(KBUILD_MODULES),$(obj-m)) \
  59. $(subdir-ym) $(always)
  60. @:
  61. # Linus' kernel sanity checking tool
  62. ifneq ($(KBUILD_CHECKSRC),0)
  63. ifeq ($(KBUILD_CHECKSRC),2)
  64. quiet_cmd_force_checksrc = CHECK $<
  65. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  66. else
  67. quiet_cmd_checksrc = CHECK $<
  68. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  69. endif
  70. endif
  71. # Compile C sources (.c)
  72. # ---------------------------------------------------------------------------
  73. # Default is built-in, unless we know otherwise
  74. modkern_cflags := $(CFLAGS_KERNEL)
  75. quiet_modtag := $(empty) $(empty)
  76. $(real-objs-m) : modkern_cflags := $(CFLAGS_MODULE)
  77. $(real-objs-m:.o=.i) : modkern_cflags := $(CFLAGS_MODULE)
  78. $(real-objs-m:.o=.s) : modkern_cflags := $(CFLAGS_MODULE)
  79. $(real-objs-m:.o=.lst): modkern_cflags := $(CFLAGS_MODULE)
  80. $(real-objs-m) : quiet_modtag := [M]
  81. $(real-objs-m:.o=.i) : quiet_modtag := [M]
  82. $(real-objs-m:.o=.s) : quiet_modtag := [M]
  83. $(real-objs-m:.o=.lst): quiet_modtag := [M]
  84. $(obj-m) : quiet_modtag := [M]
  85. # Default for not multi-part modules
  86. modname = $(*F)
  87. $(multi-objs-m) : modname = $(modname-multi)
  88. $(multi-objs-m:.o=.i) : modname = $(modname-multi)
  89. $(multi-objs-m:.o=.s) : modname = $(modname-multi)
  90. $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
  91. $(multi-objs-y) : modname = $(modname-multi)
  92. $(multi-objs-y:.o=.i) : modname = $(modname-multi)
  93. $(multi-objs-y:.o=.s) : modname = $(modname-multi)
  94. $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
  95. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  96. cmd_cc_s_c = $(CC) $(c_flags) -S -o $@ $<
  97. %.s: %.c FORCE
  98. $(call if_changed_dep,cc_s_c)
  99. quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
  100. cmd_cc_i_c = $(CPP) $(c_flags) -o $@ $<
  101. %.i: %.c FORCE
  102. $(call if_changed_dep,cc_i_c)
  103. # C (.c) files
  104. # The C file is compiled and updated dependency information is generated.
  105. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  106. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  107. ifndef CONFIG_MODVERSIONS
  108. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  109. else
  110. # When module versioning is enabled the following steps are executed:
  111. # o compile a .tmp_<file>.o from <file>.c
  112. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  113. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  114. # are done.
  115. # o otherwise, we calculate symbol versions using the good old
  116. # genksyms on the preprocessed source and postprocess them in a way
  117. # that they are usable as a linker script
  118. # o generate <file>.o from .tmp_<file>.o using the linker to
  119. # replace the unresolved symbols __crc_exported_symbol with
  120. # the actual value of the checksum generated by genksyms
  121. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  122. cmd_modversions = \
  123. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  124. $(CPP) -D__GENKSYMS__ $(c_flags) $< \
  125. | $(GENKSYMS) \
  126. > $(@D)/.tmp_$(@F:.o=.ver); \
  127. \
  128. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  129. -T $(@D)/.tmp_$(@F:.o=.ver); \
  130. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  131. else \
  132. mv $(@D)/.tmp_$(@F) $@; \
  133. fi;
  134. endif
  135. define rule_cc_o_c
  136. $(if $($(quiet)cmd_checksrc),echo ' $($(quiet)cmd_checksrc)';) \
  137. $(cmd_checksrc) \
  138. $(if $($(quiet)cmd_cc_o_c),echo ' $(subst ','\'',$($(quiet)cmd_cc_o_c))';) \
  139. $(cmd_cc_o_c); \
  140. $(cmd_modversions) \
  141. scripts/basic/fixdep $(depfile) $@ '$(subst ','\'',$(cmd_cc_o_c))' > $(@D)/.$(@F).tmp; \
  142. rm -f $(depfile); \
  143. mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd
  144. endef
  145. # Built-in and composite module parts
  146. %.o: %.c FORCE
  147. $(call cmd,force_checksrc)
  148. $(call if_changed_rule,cc_o_c)
  149. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  150. $(single-used-m): %.o: %.c FORCE
  151. $(call cmd,force_checksrc)
  152. $(call if_changed_rule,cc_o_c)
  153. @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
  154. quiet_cmd_cc_lst_c = MKLST $@
  155. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  156. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  157. System.map $(OBJDUMP) > $@
  158. %.lst: %.c FORCE
  159. $(call if_changed_dep,cc_lst_c)
  160. # Compile assembler sources (.S)
  161. # ---------------------------------------------------------------------------
  162. modkern_aflags := $(AFLAGS_KERNEL)
  163. $(real-objs-m) : modkern_aflags := $(AFLAGS_MODULE)
  164. $(real-objs-m:.o=.s): modkern_aflags := $(AFLAGS_MODULE)
  165. quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
  166. cmd_as_s_S = $(CPP) $(a_flags) -o $@ $<
  167. %.s: %.S FORCE
  168. $(call if_changed_dep,as_s_S)
  169. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  170. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  171. %.o: %.S FORCE
  172. $(call if_changed_dep,as_o_S)
  173. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  174. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  175. # Linker scripts preprocessor (.lds.S -> .lds)
  176. # ---------------------------------------------------------------------------
  177. quiet_cmd_cpp_lds_S = LDS $@
  178. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -D__ASSEMBLY__ -o $@ $<
  179. %.lds: %.lds.S FORCE
  180. $(call if_changed_dep,cpp_lds_S)
  181. # Build the compiled-in targets
  182. # ---------------------------------------------------------------------------
  183. # To build objects in subdirs, we need to descend into the directories
  184. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  185. #
  186. # Rule to compile a set of .o files into one .o file
  187. #
  188. ifdef builtin-target
  189. quiet_cmd_link_o_target = LD $@
  190. # If the list of objects to link is empty, just create an empty built-in.o
  191. cmd_link_o_target = $(if $(strip $(obj-y)),\
  192. $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\
  193. rm -f $@; $(AR) rcs $@)
  194. $(builtin-target): $(obj-y) FORCE
  195. $(call if_changed,link_o_target)
  196. targets += $(builtin-target)
  197. endif # builtin-target
  198. #
  199. # Rule to compile a set of .o files into one .a file
  200. #
  201. ifdef lib-target
  202. quiet_cmd_link_l_target = AR $@
  203. cmd_link_l_target = rm -f $@; $(AR) $(EXTRA_ARFLAGS) rcs $@ $(lib-y)
  204. $(lib-target): $(lib-y) FORCE
  205. $(call if_changed,link_l_target)
  206. targets += $(lib-target)
  207. endif
  208. #
  209. # Rule to link composite objects
  210. #
  211. # Composite objects are specified in kbuild makefile as follows:
  212. # <composite-object>-objs := <list of .o files>
  213. # or
  214. # <composite-object>-y := <list of .o files>
  215. link_multi_deps = \
  216. $(filter $(addprefix $(obj)/, \
  217. $($(subst $(obj)/,,$(@:.o=-objs))) \
  218. $($(subst $(obj)/,,$(@:.o=-y)))), $^)
  219. quiet_cmd_link_multi-y = LD $@
  220. cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps)
  221. quiet_cmd_link_multi-m = LD [M] $@
  222. cmd_link_multi-m = $(LD) $(ld_flags) $(LDFLAGS_MODULE) -o $@ $(link_multi_deps)
  223. # We would rather have a list of rules like
  224. # foo.o: $(foo-objs)
  225. # but that's not so easy, so we rather make all composite objects depend
  226. # on the set of all their parts
  227. $(multi-used-y) : %.o: $(multi-objs-y) FORCE
  228. $(call if_changed,link_multi-y)
  229. $(multi-used-m) : %.o: $(multi-objs-m) FORCE
  230. $(call if_changed,link_multi-m)
  231. @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
  232. targets += $(multi-used-y) $(multi-used-m)
  233. # Descending
  234. # ---------------------------------------------------------------------------
  235. .PHONY: $(subdir-ym)
  236. $(subdir-ym):
  237. $(Q)$(MAKE) $(build)=$@
  238. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  239. # ---------------------------------------------------------------------------
  240. .PHONY: FORCE
  241. FORCE:
  242. # Read all saved command lines and dependencies for the $(targets) we
  243. # may be building above, using $(if_changed{,_dep}). As an
  244. # optimization, we don't need to read them if the target does not
  245. # exist, we will rebuild anyway in that case.
  246. targets := $(wildcard $(sort $(targets)))
  247. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  248. ifneq ($(cmd_files),)
  249. include $(cmd_files)
  250. endif