Makefile.build 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 scripts/Kbuild.include
  10. # The filename Kbuild has precedence over Makefile
  11. kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
  12. include $(if $(wildcard $(kbuild-dir)/Kbuild), $(kbuild-dir)/Kbuild, $(kbuild-dir)/Makefile)
  13. include scripts/Makefile.lib
  14. ifdef host-progs
  15. ifneq ($(hostprogs-y),$(host-progs))
  16. $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
  17. hostprogs-y += $(host-progs)
  18. endif
  19. endif
  20. # Do not include host rules unles needed
  21. ifneq ($(hostprogs-y)$(hostprogs-m),)
  22. include scripts/Makefile.host
  23. endif
  24. ifneq ($(KBUILD_SRC),)
  25. # Create output directory if not already present
  26. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  27. # Create directories for object files if directory does not exist
  28. # Needed when obj-y := dir/file.o syntax is used
  29. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  30. endif
  31. ifdef EXTRA_TARGETS
  32. $(warning kbuild: $(obj)/Makefile - Usage of EXTRA_TARGETS is obsolete in 2.6. Please fix!)
  33. endif
  34. ifdef build-targets
  35. $(warning kbuild: $(obj)/Makefile - Usage of build-targets is obsolete in 2.6. Please fix!)
  36. endif
  37. ifdef export-objs
  38. $(warning kbuild: $(obj)/Makefile - Usage of export-objs is obsolete in 2.6. Please fix!)
  39. endif
  40. ifdef O_TARGET
  41. $(warning kbuild: $(obj)/Makefile - Usage of O_TARGET := $(O_TARGET) is obsolete in 2.6. Please fix!)
  42. endif
  43. ifdef L_TARGET
  44. $(error kbuild: $(obj)/Makefile - Use of L_TARGET is replaced by lib-y in 2.6. Please fix!)
  45. endif
  46. ifdef list-multi
  47. $(warning kbuild: $(obj)/Makefile - list-multi := $(list-multi) is obsolete in 2.6. Please fix!)
  48. endif
  49. ifndef obj
  50. $(warning kbuild: Makefile.build is included improperly)
  51. endif
  52. # ===========================================================================
  53. ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)
  54. lib-target := $(obj)/lib.a
  55. endif
  56. ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(lib-target)),)
  57. builtin-target := $(obj)/built-in.o
  58. endif
  59. # We keep a list of all modules in $(MODVERDIR)
  60. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  61. $(if $(KBUILD_MODULES),$(obj-m)) \
  62. $(subdir-ym) $(always)
  63. @:
  64. # Linus' kernel sanity checking tool
  65. ifneq ($(KBUILD_CHECKSRC),0)
  66. ifeq ($(KBUILD_CHECKSRC),2)
  67. quiet_cmd_force_checksrc = CHECK $<
  68. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  69. else
  70. quiet_cmd_checksrc = CHECK $<
  71. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  72. endif
  73. endif
  74. # Compile C sources (.c)
  75. # ---------------------------------------------------------------------------
  76. # Default is built-in, unless we know otherwise
  77. modkern_cflags := $(CFLAGS_KERNEL)
  78. quiet_modtag := $(empty) $(empty)
  79. $(real-objs-m) : modkern_cflags := $(CFLAGS_MODULE)
  80. $(real-objs-m:.o=.i) : modkern_cflags := $(CFLAGS_MODULE)
  81. $(real-objs-m:.o=.s) : modkern_cflags := $(CFLAGS_MODULE)
  82. $(real-objs-m:.o=.lst): modkern_cflags := $(CFLAGS_MODULE)
  83. $(real-objs-m) : quiet_modtag := [M]
  84. $(real-objs-m:.o=.i) : quiet_modtag := [M]
  85. $(real-objs-m:.o=.s) : quiet_modtag := [M]
  86. $(real-objs-m:.o=.lst): quiet_modtag := [M]
  87. $(obj-m) : quiet_modtag := [M]
  88. # Default for not multi-part modules
  89. modname = $(*F)
  90. $(multi-objs-m) : modname = $(modname-multi)
  91. $(multi-objs-m:.o=.i) : modname = $(modname-multi)
  92. $(multi-objs-m:.o=.s) : modname = $(modname-multi)
  93. $(multi-objs-m:.o=.lst) : modname = $(modname-multi)
  94. $(multi-objs-y) : modname = $(modname-multi)
  95. $(multi-objs-y:.o=.i) : modname = $(modname-multi)
  96. $(multi-objs-y:.o=.s) : modname = $(modname-multi)
  97. $(multi-objs-y:.o=.lst) : modname = $(modname-multi)
  98. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  99. cmd_cc_s_c = $(CC) $(c_flags) -fverbose-asm -S -o $@ $<
  100. %.s: %.c FORCE
  101. $(call if_changed_dep,cc_s_c)
  102. quiet_cmd_cc_i_c = CPP $(quiet_modtag) $@
  103. cmd_cc_i_c = $(CPP) $(c_flags) -o $@ $<
  104. %.i: %.c FORCE
  105. $(call if_changed_dep,cc_i_c)
  106. # C (.c) files
  107. # The C file is compiled and updated dependency information is generated.
  108. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  109. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  110. ifndef CONFIG_MODVERSIONS
  111. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  112. else
  113. # When module versioning is enabled the following steps are executed:
  114. # o compile a .tmp_<file>.o from <file>.c
  115. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  116. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  117. # are done.
  118. # o otherwise, we calculate symbol versions using the good old
  119. # genksyms on the preprocessed source and postprocess them in a way
  120. # that they are usable as a linker script
  121. # o generate <file>.o from .tmp_<file>.o using the linker to
  122. # replace the unresolved symbols __crc_exported_symbol with
  123. # the actual value of the checksum generated by genksyms
  124. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  125. cmd_modversions = \
  126. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  127. $(CPP) -D__GENKSYMS__ $(c_flags) $< \
  128. | $(GENKSYMS) -a $(ARCH) \
  129. > $(@D)/.tmp_$(@F:.o=.ver); \
  130. \
  131. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  132. -T $(@D)/.tmp_$(@F:.o=.ver); \
  133. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  134. else \
  135. mv -f $(@D)/.tmp_$(@F) $@; \
  136. fi;
  137. endif
  138. define rule_cc_o_c
  139. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  140. $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
  141. $(cmd_modversions) \
  142. scripts/basic/fixdep $(depfile) $@ '$(call make-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
  252. # Declare the contents of the .PHONY variable as phony. We keep that
  253. # information in a variable se we can use it in if_changed and friends.
  254. .PHONY: $(PHONY)