Makefile.build 9.8 KB

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