Makefile.build 11 KB

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