Makefile.build 12 KB

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