Makefile.build 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. # ==========================================================================
  2. # Building
  3. # ==========================================================================
  4. src := $(obj)
  5. PHONY := __build
  6. __build:
  7. # Read .config if it exist, otherwise ignore
  8. -include include/config/auto.conf
  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 = $(basetarget)
  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. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  107. cmd_cc_symtypes_c = \
  108. $(CPP) -D__GENKSYMS__ $(c_flags) $< \
  109. | $(GENKSYMS) -T $@ >/dev/null; \
  110. test -s $@ || rm -f $@
  111. %.symtypes : %.c FORCE
  112. $(call if_changed_dep,cc_symtypes_c)
  113. # C (.c) files
  114. # The C file is compiled and updated dependency information is generated.
  115. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  116. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  117. ifndef CONFIG_MODVERSIONS
  118. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  119. else
  120. # When module versioning is enabled the following steps are executed:
  121. # o compile a .tmp_<file>.o from <file>.c
  122. # o if .tmp_<file>.o doesn't contain a __ksymtab version, i.e. does
  123. # not export symbols, we just rename .tmp_<file>.o to <file>.o and
  124. # are done.
  125. # o otherwise, we calculate symbol versions using the good old
  126. # genksyms on the preprocessed source and postprocess them in a way
  127. # that they are usable as a linker script
  128. # o generate <file>.o from .tmp_<file>.o using the linker to
  129. # replace the unresolved symbols __crc_exported_symbol with
  130. # the actual value of the checksum generated by genksyms
  131. cmd_cc_o_c = $(CC) $(c_flags) -c -o $(@D)/.tmp_$(@F) $<
  132. cmd_modversions = \
  133. if $(OBJDUMP) -h $(@D)/.tmp_$(@F) | grep -q __ksymtab; then \
  134. $(CPP) -D__GENKSYMS__ $(c_flags) $< \
  135. | $(GENKSYMS) $(if $(KBUILD_SYMTYPES), \
  136. -T $(@D)/$(@F:.o=.symtypes)) -a $(ARCH) \
  137. > $(@D)/.tmp_$(@F:.o=.ver); \
  138. \
  139. $(LD) $(LDFLAGS) -r -o $@ $(@D)/.tmp_$(@F) \
  140. -T $(@D)/.tmp_$(@F:.o=.ver); \
  141. rm -f $(@D)/.tmp_$(@F) $(@D)/.tmp_$(@F:.o=.ver); \
  142. else \
  143. mv -f $(@D)/.tmp_$(@F) $@; \
  144. fi;
  145. endif
  146. define rule_cc_o_c
  147. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  148. $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
  149. $(cmd_modversions) \
  150. scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > $(@D)/.$(@F).tmp; \
  151. rm -f $(depfile); \
  152. mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd
  153. endef
  154. # Built-in and composite module parts
  155. %.o: %.c FORCE
  156. $(call cmd,force_checksrc)
  157. $(call if_changed_rule,cc_o_c)
  158. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  159. $(single-used-m): %.o: %.c FORCE
  160. $(call cmd,force_checksrc)
  161. $(call if_changed_rule,cc_o_c)
  162. @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
  163. quiet_cmd_cc_lst_c = MKLST $@
  164. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  165. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  166. System.map $(OBJDUMP) > $@
  167. %.lst: %.c FORCE
  168. $(call if_changed_dep,cc_lst_c)
  169. # Compile assembler sources (.S)
  170. # ---------------------------------------------------------------------------
  171. modkern_aflags := $(AFLAGS_KERNEL)
  172. $(real-objs-m) : modkern_aflags := $(AFLAGS_MODULE)
  173. $(real-objs-m:.o=.s): modkern_aflags := $(AFLAGS_MODULE)
  174. quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
  175. cmd_as_s_S = $(CPP) $(a_flags) -o $@ $<
  176. %.s: %.S FORCE
  177. $(call if_changed_dep,as_s_S)
  178. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  179. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  180. %.o: %.S FORCE
  181. $(call if_changed_dep,as_o_S)
  182. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  183. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  184. # Linker scripts preprocessor (.lds.S -> .lds)
  185. # ---------------------------------------------------------------------------
  186. quiet_cmd_cpp_lds_S = LDS $@
  187. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -D__ASSEMBLY__ -o $@ $<
  188. %.lds: %.lds.S FORCE
  189. $(call if_changed_dep,cpp_lds_S)
  190. # Build the compiled-in targets
  191. # ---------------------------------------------------------------------------
  192. # To build objects in subdirs, we need to descend into the directories
  193. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  194. #
  195. # Rule to compile a set of .o files into one .o file
  196. #
  197. ifdef builtin-target
  198. quiet_cmd_link_o_target = LD $@
  199. # If the list of objects to link is empty, just create an empty built-in.o
  200. cmd_link_o_target = $(if $(strip $(obj-y)),\
  201. $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^),\
  202. rm -f $@; $(AR) rcs $@)
  203. $(builtin-target): $(obj-y) FORCE
  204. $(call if_changed,link_o_target)
  205. targets += $(builtin-target)
  206. endif # builtin-target
  207. #
  208. # Rule to compile a set of .o files into one .a file
  209. #
  210. ifdef lib-target
  211. quiet_cmd_link_l_target = AR $@
  212. cmd_link_l_target = rm -f $@; $(AR) $(EXTRA_ARFLAGS) rcs $@ $(lib-y)
  213. $(lib-target): $(lib-y) FORCE
  214. $(call if_changed,link_l_target)
  215. targets += $(lib-target)
  216. endif
  217. #
  218. # Rule to link composite objects
  219. #
  220. # Composite objects are specified in kbuild makefile as follows:
  221. # <composite-object>-objs := <list of .o files>
  222. # or
  223. # <composite-object>-y := <list of .o files>
  224. link_multi_deps = \
  225. $(filter $(addprefix $(obj)/, \
  226. $($(subst $(obj)/,,$(@:.o=-objs))) \
  227. $($(subst $(obj)/,,$(@:.o=-y)))), $^)
  228. quiet_cmd_link_multi-y = LD $@
  229. cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps)
  230. quiet_cmd_link_multi-m = LD [M] $@
  231. cmd_link_multi-m = $(LD) $(ld_flags) $(LDFLAGS_MODULE) -o $@ $(link_multi_deps)
  232. # We would rather have a list of rules like
  233. # foo.o: $(foo-objs)
  234. # but that's not so easy, so we rather make all composite objects depend
  235. # on the set of all their parts
  236. $(multi-used-y) : %.o: $(multi-objs-y) FORCE
  237. $(call if_changed,link_multi-y)
  238. $(multi-used-m) : %.o: $(multi-objs-m) FORCE
  239. $(call if_changed,link_multi-m)
  240. @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
  241. targets += $(multi-used-y) $(multi-used-m)
  242. # Descending
  243. # ---------------------------------------------------------------------------
  244. PHONY += $(subdir-ym)
  245. $(subdir-ym):
  246. $(Q)$(MAKE) $(build)=$@
  247. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  248. # ---------------------------------------------------------------------------
  249. PHONY += FORCE
  250. FORCE:
  251. # Read all saved command lines and dependencies for the $(targets) we
  252. # may be building above, using $(if_changed{,_dep}). As an
  253. # optimization, we don't need to read them if the target does not
  254. # exist, we will rebuild anyway in that case.
  255. targets := $(wildcard $(sort $(targets)))
  256. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  257. ifneq ($(cmd_files),)
  258. include $(cmd_files)
  259. endif
  260. # Declare the contents of the .PHONY variable as phony. We keep that
  261. # information in a variable se we can use it in if_changed and friends.
  262. .PHONY: $(PHONY)