Makefile.build 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. asflags-y :=
  23. ccflags-y :=
  24. cppflags-y :=
  25. ldflags-y :=
  26. # Read auto.conf if it exists, otherwise ignore
  27. -include include/config/auto.conf
  28. include scripts/Kbuild.include
  29. # For backward compatibility check that these variables do not change
  30. save-cflags := $(CFLAGS)
  31. # The filename Kbuild has precedence over Makefile
  32. kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
  33. kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile)
  34. include $(kbuild-file)
  35. # If the save-* variables changed error out
  36. ifeq ($(KBUILD_NOPEDANTIC),)
  37. ifneq ("$(save-cflags)","$(CFLAGS)")
  38. $(error CFLAGS was changed in "$(kbuild-file)". Fix it to use EXTRA_CFLAGS)
  39. endif
  40. endif
  41. include scripts/Makefile.lib
  42. ifdef host-progs
  43. ifneq ($(hostprogs-y),$(host-progs))
  44. $(warning kbuild: $(obj)/Makefile - Usage of host-progs is deprecated. Please replace with hostprogs-y!)
  45. hostprogs-y += $(host-progs)
  46. endif
  47. endif
  48. # Do not include host rules unless needed
  49. ifneq ($(hostprogs-y)$(hostprogs-m),)
  50. include scripts/Makefile.host
  51. endif
  52. ifneq ($(KBUILD_SRC),)
  53. # Create output directory if not already present
  54. _dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
  55. # Create directories for object files if directory does not exist
  56. # Needed when obj-y := dir/file.o syntax is used
  57. _dummy := $(foreach d,$(obj-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
  58. endif
  59. ifndef obj
  60. $(warning kbuild: Makefile.build is included improperly)
  61. endif
  62. # ===========================================================================
  63. ifneq ($(strip $(lib-y) $(lib-m) $(lib-n) $(lib-)),)
  64. lib-target := $(obj)/lib.a
  65. endif
  66. ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(lib-target)),)
  67. builtin-target := $(obj)/built-in.o
  68. endif
  69. modorder-target := $(obj)/modules.order
  70. # We keep a list of all modules in $(MODVERDIR)
  71. __build: $(if $(KBUILD_BUILTIN),$(builtin-target) $(lib-target) $(extra-y)) \
  72. $(if $(KBUILD_MODULES),$(obj-m) $(modorder-target)) \
  73. $(subdir-ym) $(always)
  74. @:
  75. # Linus' kernel sanity checking tool
  76. ifneq ($(KBUILD_CHECKSRC),0)
  77. ifeq ($(KBUILD_CHECKSRC),2)
  78. quiet_cmd_force_checksrc = CHECK $<
  79. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  80. else
  81. quiet_cmd_checksrc = CHECK $<
  82. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $< ;
  83. endif
  84. endif
  85. # Do section mismatch analysis for each module/built-in.o
  86. ifdef CONFIG_DEBUG_SECTION_MISMATCH
  87. cmd_secanalysis = ; scripts/mod/modpost $@
  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. cmd_genksyms = \
  122. $(CPP) -D__GENKSYMS__ $(c_flags) $< | \
  123. $(GENKSYMS) -T $@ -A -a $(ARCH) \
  124. $(if $(KBUILD_PRESERVE),-p) \
  125. $(if $(1),-r $(firstword $(wildcard $(@:.symtypes=.symref) /dev/null)))
  126. quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
  127. cmd_cc_symtypes_c = \
  128. set -e; \
  129. $(call cmd_genksyms, true) >/dev/null; \
  130. test -s $@ || rm -f $@
  131. $(obj)/%.symtypes : $(src)/%.c FORCE
  132. $(call cmd,cc_symtypes_c)
  133. # C (.c) files
  134. # The C file is compiled and updated dependency information is generated.
  135. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  136. quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
  137. ifndef CONFIG_MODVERSIONS
  138. cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
  139. else
  140. # When module versioning is enabled the following steps are executed:
  141. # o compile a .tmp_<file>.s from <file>.c
  142. # o if .tmp_<file>.s doesn't contain a __ksymtab version, i.e. does
  143. # not export symbols, we just assemble .tmp_<file>.s to <file>.o and
  144. # are done.
  145. # o otherwise, we calculate symbol versions using the good old
  146. # genksyms on the preprocessed source and postprocess them in a way
  147. # that they are usable as assembly source
  148. # o assemble <file>.o from .tmp_<file>.s forcing inclusion of directives
  149. # defining the actual values of __crc_*, followed by objcopy-ing them
  150. # to force these symbols to be local to permit stripping them later.
  151. s_file = $(@D)/.tmp_$(@F:.o=.s)
  152. v_file = $(@D)/.tmp_$(@F:.o=.v)
  153. tmp_o_file = $(@D)/.tmp_$(@F)
  154. no_g_c_flags = $(filter-out -g%,$(c_flags))
  155. cmd_cc_o_c = $(CC) $(c_flags) -S -o $(s_file) $<
  156. cmd_modversions = \
  157. if grep -q __ksymtab $(s_file); then \
  158. if $(call cmd_genksyms, $(KBUILD_SYMTYPES)) > $(v_file) \
  159. && $(CC) $(no_g_c_flags) -c -Wa,$(v_file) \
  160. -o $(tmp_o_file) $(s_file) \
  161. && $(OBJCOPY) -L '__crc_*' -L '___crc_*' -w \
  162. $(tmp_o_file) $@; \
  163. then \
  164. : ; \
  165. else \
  166. rm -f $@; exit 1; \
  167. fi; \
  168. else \
  169. rm -f $(v_file); \
  170. $(CC) $(no_g_c_flags) -c -o $@ $(s_file); \
  171. fi;
  172. endif
  173. ifdef CONFIG_64BIT
  174. arch_bits = 64
  175. else
  176. arch_bits = 32
  177. endif
  178. ifdef CONFIG_FTRACE_MCOUNT_RECORD
  179. cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl \
  180. "$(ARCH)" "$(arch_bits)" "$(OBJDUMP)" "$(OBJCOPY)" "$(CC)" "$(LD)" \
  181. "$(NM)" "$(RM)" "$(MV)" "$(@)";
  182. endif
  183. define rule_cc_o_c
  184. $(call echo-cmd,checksrc) $(cmd_checksrc) \
  185. $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
  186. $(cmd_modversions) \
  187. $(cmd_record_mcount) \
  188. scripts/basic/fixdep $(depfile) $@ '$(call make-cmd,cc_o_c)' > \
  189. $(dot-target).tmp; \
  190. if [ -r $(@D)/.tmp_$(@F:.o=.v) ]; then \
  191. echo >> $(dot-target).tmp; \
  192. echo '$@: $(GENKSYMS)' >> $(dot-target).tmp; \
  193. echo '$(GENKSYMS):: ;' >> $(dot-target).tmp; \
  194. fi; \
  195. rm -f $(depfile) $(@D)/.tmp_$(@F:.o=.?); \
  196. mv -f $(dot-target).tmp $(dot-target).cmd
  197. endef
  198. # Built-in and composite module parts
  199. $(obj)/%.o: $(src)/%.c FORCE
  200. $(call cmd,force_checksrc)
  201. $(call if_changed_rule,cc_o_c)
  202. # Single-part modules are special since we need to mark them in $(MODVERDIR)
  203. $(single-used-m): $(obj)/%.o: $(src)/%.c FORCE
  204. $(call cmd,force_checksrc)
  205. $(call if_changed_rule,cc_o_c)
  206. @{ echo $(@:.o=.ko); echo $@; } > $(MODVERDIR)/$(@F:.o=.mod)
  207. quiet_cmd_cc_lst_c = MKLST $@
  208. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  209. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  210. System.map $(OBJDUMP) > $@
  211. $(obj)/%.lst: $(src)/%.c FORCE
  212. $(call if_changed_dep,cc_lst_c)
  213. # Compile assembler sources (.S)
  214. # ---------------------------------------------------------------------------
  215. modkern_aflags := $(AFLAGS_KERNEL)
  216. $(real-objs-m) : modkern_aflags := $(AFLAGS_MODULE)
  217. $(real-objs-m:.o=.s): modkern_aflags := $(AFLAGS_MODULE)
  218. quiet_cmd_as_s_S = CPP $(quiet_modtag) $@
  219. cmd_as_s_S = $(CPP) $(a_flags) -o $@ $<
  220. $(obj)/%.s: $(src)/%.S FORCE
  221. $(call if_changed_dep,as_s_S)
  222. quiet_cmd_as_o_S = AS $(quiet_modtag) $@
  223. cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
  224. $(obj)/%.o: $(src)/%.S FORCE
  225. $(call if_changed_dep,as_o_S)
  226. targets += $(real-objs-y) $(real-objs-m) $(lib-y)
  227. targets += $(extra-y) $(MAKECMDGOALS) $(always)
  228. # Linker scripts preprocessor (.lds.S -> .lds)
  229. # ---------------------------------------------------------------------------
  230. quiet_cmd_cpp_lds_S = LDS $@
  231. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -D__ASSEMBLY__ -o $@ $<
  232. $(obj)/%.lds: $(src)/%.lds.S FORCE
  233. $(call if_changed_dep,cpp_lds_S)
  234. # Build the compiled-in targets
  235. # ---------------------------------------------------------------------------
  236. # To build objects in subdirs, we need to descend into the directories
  237. $(sort $(subdir-obj-y)): $(subdir-ym) ;
  238. #
  239. # Rule to compile a set of .o files into one .o file
  240. #
  241. ifdef builtin-target
  242. quiet_cmd_link_o_target = LD $@
  243. # If the list of objects to link is empty, just create an empty built-in.o
  244. cmd_link_o_target = $(if $(strip $(obj-y)),\
  245. $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \
  246. $(cmd_secanalysis),\
  247. rm -f $@; $(AR) rcs $@)
  248. $(builtin-target): $(obj-y) FORCE
  249. $(call if_changed,link_o_target)
  250. targets += $(builtin-target)
  251. endif # builtin-target
  252. #
  253. # Rule to create modules.order file
  254. #
  255. # Create commands to either record .ko file or cat modules.order from
  256. # a subdirectory
  257. modorder-cmds = \
  258. $(foreach m, $(modorder), \
  259. $(if $(filter %/modules.order, $m), \
  260. cat $m;, echo kernel/$m;))
  261. $(modorder-target): $(subdir-ym) FORCE
  262. $(Q)(cat /dev/null; $(modorder-cmds)) > $@
  263. #
  264. # Rule to compile a set of .o files into one .a file
  265. #
  266. ifdef lib-target
  267. quiet_cmd_link_l_target = AR $@
  268. cmd_link_l_target = rm -f $@; $(AR) rcs $@ $(lib-y)
  269. $(lib-target): $(lib-y) FORCE
  270. $(call if_changed,link_l_target)
  271. targets += $(lib-target)
  272. endif
  273. #
  274. # Rule to link composite objects
  275. #
  276. # Composite objects are specified in kbuild makefile as follows:
  277. # <composite-object>-objs := <list of .o files>
  278. # or
  279. # <composite-object>-y := <list of .o files>
  280. link_multi_deps = \
  281. $(filter $(addprefix $(obj)/, \
  282. $($(subst $(obj)/,,$(@:.o=-objs))) \
  283. $($(subst $(obj)/,,$(@:.o=-y)))), $^)
  284. quiet_cmd_link_multi-y = LD $@
  285. cmd_link_multi-y = $(LD) $(ld_flags) -r -o $@ $(link_multi_deps) $(cmd_secanalysis)
  286. quiet_cmd_link_multi-m = LD [M] $@
  287. cmd_link_multi-m = $(cmd_link_multi-y)
  288. # We would rather have a list of rules like
  289. # foo.o: $(foo-objs)
  290. # but that's not so easy, so we rather make all composite objects depend
  291. # on the set of all their parts
  292. $(multi-used-y) : %.o: $(multi-objs-y) FORCE
  293. $(call if_changed,link_multi-y)
  294. $(multi-used-m) : %.o: $(multi-objs-m) FORCE
  295. $(call if_changed,link_multi-m)
  296. @{ echo $(@:.o=.ko); echo $(link_multi_deps); } > $(MODVERDIR)/$(@F:.o=.mod)
  297. targets += $(multi-used-y) $(multi-used-m)
  298. # Descending
  299. # ---------------------------------------------------------------------------
  300. PHONY += $(subdir-ym)
  301. $(subdir-ym):
  302. $(Q)$(MAKE) $(build)=$@
  303. # Add FORCE to the prequisites of a target to force it to be always rebuilt.
  304. # ---------------------------------------------------------------------------
  305. PHONY += FORCE
  306. FORCE:
  307. # Read all saved command lines and dependencies for the $(targets) we
  308. # may be building above, using $(if_changed{,_dep}). As an
  309. # optimization, we don't need to read them if the target does not
  310. # exist, we will rebuild anyway in that case.
  311. targets := $(wildcard $(sort $(targets)))
  312. cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
  313. ifneq ($(cmd_files),)
  314. include $(cmd_files)
  315. endif
  316. # Declare the contents of the .PHONY variable as phony. We keep that
  317. # information in a variable se we can use it in if_changed and friends.
  318. .PHONY: $(PHONY)