Makefile.build 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. # SPDX-License-Identifier: GPL-2.0
  2. # ==========================================================================
  3. # Building
  4. # ==========================================================================
  5. src := $(srcroot)/$(obj)
  6. PHONY := $(obj)/
  7. $(obj)/:
  8. # Init all relevant variables used in kbuild files so
  9. # 1) they have correct type
  10. # 2) they do not inherit any value from the environment
  11. obj-y :=
  12. obj-m :=
  13. lib-y :=
  14. lib-m :=
  15. always-y :=
  16. always-m :=
  17. targets :=
  18. subdir-y :=
  19. subdir-m :=
  20. asflags-y :=
  21. ccflags-y :=
  22. rustflags-y :=
  23. cppflags-y :=
  24. ldflags-y :=
  25. subdir-asflags-y :=
  26. subdir-ccflags-y :=
  27. # Read auto.conf if it exists, otherwise ignore
  28. -include $(objtree)/include/config/auto.conf
  29. include $(srctree)/scripts/Kbuild.include
  30. include $(srctree)/scripts/Makefile.compiler
  31. include $(kbuild-file)
  32. include $(srctree)/scripts/Makefile.lib
  33. # flags that take effect in current and sub directories
  34. KBUILD_AFLAGS += $(subdir-asflags-y)
  35. KBUILD_CFLAGS += $(subdir-ccflags-y)
  36. KBUILD_RUSTFLAGS += $(subdir-rustflags-y)
  37. # Figure out what we need to build from the various variables
  38. # ===========================================================================
  39. # When an object is listed to be built compiled-in and modular,
  40. # only build the compiled-in version
  41. obj-m := $(filter-out $(obj-y),$(obj-m))
  42. # Libraries are always collected in one lib file.
  43. # Filter out objects already built-in
  44. lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
  45. # Subdirectories we need to descend into
  46. subdir-ym := $(sort $(subdir-y) $(subdir-m) \
  47. $(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
  48. # Handle objects in subdirs:
  49. # - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
  50. # foo/modules.order
  51. # - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
  52. #
  53. # Generate modules.order to determine modorder. Unfortunately, we don't have
  54. # information about ordering between -y and -m subdirs. Just put -y's first.
  55. ifdef need-modorder
  56. obj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
  57. else
  58. obj-m := $(filter-out %/, $(obj-m))
  59. endif
  60. ifdef need-builtin
  61. obj-y := $(patsubst %/, %/built-in.a, $(obj-y))
  62. else
  63. obj-y := $(filter-out %/, $(obj-y))
  64. endif
  65. # Expand $(foo-objs) $(foo-y) etc. by replacing their individuals
  66. suffix-search = $(strip $(foreach s, $3, $($(1:%$(strip $2)=%$s))))
  67. # List composite targets that are constructed by combining other targets
  68. multi-search = $(sort $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $m)))
  69. # List primitive targets that are compiled from source files
  70. real-search = $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $(call suffix-search, $m, $2, $3), $m))
  71. # If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
  72. multi-obj-y := $(call multi-search, $(obj-y), .o, -objs -y)
  73. multi-obj-m := $(call multi-search, $(obj-m), .o, -objs -y -m)
  74. multi-obj-ym := $(multi-obj-y) $(multi-obj-m)
  75. # Replace multi-part objects by their individual parts,
  76. # including built-in.a from subdirectories
  77. real-obj-y := $(call real-search, $(obj-y), .o, -objs -y)
  78. real-obj-m := $(call real-search, $(obj-m), .o, -objs -y -m)
  79. always-y += $(always-m)
  80. # hostprogs-always-y += foo
  81. # ... is a shorthand for
  82. # hostprogs += foo
  83. # always-y += foo
  84. hostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
  85. always-y += $(hostprogs-always-y) $(hostprogs-always-m)
  86. # userprogs-always-y is likewise.
  87. userprogs += $(userprogs-always-y) $(userprogs-always-m)
  88. always-y += $(userprogs-always-y) $(userprogs-always-m)
  89. # Add subdir path
  90. ifneq ($(obj),.)
  91. extra-y := $(addprefix $(obj)/, $(extra-y))
  92. always-y := $(addprefix $(obj)/, $(always-y))
  93. targets := $(addprefix $(obj)/, $(targets))
  94. obj-m := $(addprefix $(obj)/, $(obj-m))
  95. lib-y := $(addprefix $(obj)/, $(lib-y))
  96. real-obj-y := $(addprefix $(obj)/, $(real-obj-y))
  97. real-obj-m := $(addprefix $(obj)/, $(real-obj-m))
  98. multi-obj-m := $(addprefix $(obj)/, $(multi-obj-m))
  99. subdir-ym := $(addprefix $(obj)/, $(subdir-ym))
  100. endif
  101. ifndef obj
  102. $(warning kbuild: Makefile.build is included improperly)
  103. endif
  104. ifeq ($(need-modorder),)
  105. ifneq ($(obj-m),)
  106. $(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
  107. $(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
  108. endif
  109. endif
  110. # ===========================================================================
  111. # subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
  112. subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
  113. subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
  114. targets-for-builtin := $(extra-y)
  115. ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
  116. targets-for-builtin += $(obj)/lib.a
  117. endif
  118. ifdef need-builtin
  119. targets-for-builtin += $(obj)/built-in.a
  120. endif
  121. targets-for-modules := $(foreach x, o mod, \
  122. $(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
  123. ifdef need-modorder
  124. targets-for-modules += $(obj)/modules.order
  125. endif
  126. targets += $(targets-for-builtin) $(targets-for-modules)
  127. # Linus' kernel sanity checking tool
  128. ifeq ($(KBUILD_CHECKSRC),1)
  129. quiet_cmd_checksrc = CHECK $<
  130. cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
  131. else ifeq ($(KBUILD_CHECKSRC),2)
  132. quiet_cmd_force_checksrc = CHECK $<
  133. cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
  134. endif
  135. ifeq ($(KBUILD_EXTMOD),)
  136. ifneq ($(KBUILD_EXTRA_WARN),)
  137. cmd_checkdoc = PYTHONDONTWRITEBYTECODE=1 $(PYTHON3) $(KERNELDOC) -none $(KDOCFLAGS) \
  138. $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \
  139. $<
  140. endif
  141. endif
  142. # Compile C sources (.c)
  143. # ---------------------------------------------------------------------------
  144. quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
  145. cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
  146. $(obj)/%.s: $(obj)/%.c FORCE
  147. $(call if_changed_dep,cc_s_c)
  148. quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
  149. cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
  150. $(obj)/%.i: $(obj)/%.c FORCE
  151. $(call if_changed_dep,cpp_i_c)
  152. getexportsymbols = $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/$(1)/p'
  153. gendwarfksyms = $(objtree)/scripts/gendwarfksyms/gendwarfksyms \
  154. $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes)) \
  155. $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable)
  156. genksyms = $(objtree)/scripts/genksyms/genksyms \
  157. $(if $(KBUILD_SYMTYPES), -T $(@:.o=.symtypes)) \
  158. $(if $(KBUILD_PRESERVE), -p) \
  159. $(addprefix -r , $(wildcard $(@:.o=.symref)))
  160. # These mirror gensymtypes_S and co below, keep them in synch.
  161. ifdef CONFIG_GENDWARFKSYMS
  162. cmd_gensymtypes_c = $(if $(skip_gendwarfksyms),, \
  163. $(call getexportsymbols,\1) | $(gendwarfksyms) $@)
  164. else
  165. cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
  166. endif # CONFIG_GENDWARFKSYMS
  167. # LLVM assembly
  168. # Generate .ll files from .c
  169. quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
  170. cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $<
  171. $(obj)/%.ll: $(obj)/%.c FORCE
  172. $(call if_changed_dep,cc_ll_c)
  173. # C (.c) files
  174. # The C file is compiled and updated dependency information is generated.
  175. # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
  176. is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
  177. ifdef CONFIG_MODVERSIONS
  178. # When module versioning is enabled the following steps are executed:
  179. # o compile a <file>.o from <file>.c
  180. # o if <file>.o doesn't contain a __export_symbol_*, i.e. does
  181. # not export symbols, it's done.
  182. # o otherwise, we calculate symbol versions using the good old
  183. # genksyms on the preprocessed source and dump them into the .cmd file.
  184. # o modpost will extract versions from that file and create *.c files that will
  185. # be compiled and linked to the kernel and/or modules.
  186. gen_symversions = \
  187. if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \
  188. $(cmd_gensymtypes_$1) >> $(dot-target).cmd; \
  189. fi
  190. cmd_gen_symversions_c = $(call gen_symversions,c)
  191. endif
  192. ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
  193. # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
  194. ifdef BUILD_C_RECORDMCOUNT
  195. ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
  196. RECORDMCOUNT_FLAGS = -w
  197. endif
  198. # Due to recursion, we must skip empty.o.
  199. # The empty.o file is created in the make process in order to determine
  200. # the target endianness and word size. It is made before all other C
  201. # files, including recordmcount.
  202. sub_cmd_record_mcount = \
  203. if [ $(@) != "scripts/mod/empty.o" ]; then \
  204. $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
  205. fi;
  206. recordmcount_source := $(srctree)/scripts/recordmcount.c \
  207. $(srctree)/scripts/recordmcount.h
  208. else
  209. sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
  210. "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
  211. "$(if $(CONFIG_64BIT),64,32)" \
  212. "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
  213. "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
  214. "$(if $(part-of-module),1,0)" "$(@)";
  215. recordmcount_source := $(srctree)/scripts/recordmcount.pl
  216. endif # BUILD_C_RECORDMCOUNT
  217. cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
  218. $(sub_cmd_record_mcount))
  219. endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
  220. # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
  221. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
  222. # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
  223. is-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(target-stem).o)$(OBJECT_FILES_NON_STANDARD)n),$(is-kernel-object))
  224. ifdef CONFIG_OBJTOOL
  225. $(obj)/%.o: private objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
  226. endif
  227. ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
  228. cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
  229. endif
  230. # Built-in and composite module parts
  231. $(obj)/%.o: $(obj)/%.c $(recordmcount_source) FORCE
  232. $(call if_changed_rule,cc_o_c)
  233. $(call cmd,force_checksrc)
  234. # To make this rule robust against "Argument list too long" error,
  235. # ensure to add $(obj)/ prefix by a shell command.
  236. cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
  237. $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
  238. $(obj)/%.mod: FORCE
  239. $(call if_changed,mod)
  240. quiet_cmd_cc_lst_c = MKLST $@
  241. cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
  242. $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
  243. System.map $(OBJDUMP) > $@
  244. $(obj)/%.lst: $(obj)/%.c FORCE
  245. $(call if_changed_dep,cc_lst_c)
  246. # Compile Rust sources (.rs)
  247. # ---------------------------------------------------------------------------
  248. # The features in this list are the ones allowed for non-`rust/` code.
  249. #
  250. # - Stable since Rust 1.79.0: `feature(slice_ptr_len)`.
  251. # - Stable since Rust 1.81.0: `feature(lint_reasons)`.
  252. # - Stable since Rust 1.82.0: `feature(asm_const)`,
  253. # `feature(offset_of_nested)`, `feature(raw_ref_op)`.
  254. # - Stable since Rust 1.84.0: `feature(strict_provenance)`.
  255. # - Stable since Rust 1.87.0: `feature(asm_goto)`.
  256. # - Expected to become stable: `feature(arbitrary_self_types)`.
  257. # - To be determined: `feature(used_with_arg)`.
  258. #
  259. # Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on
  260. # the unstable features in use.
  261. rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op,slice_ptr_len,strict_provenance,used_with_arg
  262. # `--out-dir` is required to avoid temporaries being created by `rustc` in the
  263. # current working directory, which may be not accessible in the out-of-tree
  264. # modules case.
  265. rust_common_cmd = \
  266. OBJTREE=$(abspath $(objtree)) \
  267. RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
  268. -Zallow-features=$(rust_allowed_features) \
  269. -Zcrate-attr=no_std \
  270. -Zcrate-attr='feature($(rust_allowed_features))' \
  271. -Zunstable-options --extern pin_init --extern kernel \
  272. --crate-type rlib -L $(objtree)/rust/ \
  273. --crate-name $(basename $(notdir $@)) \
  274. --sysroot=/dev/null \
  275. --out-dir $(dir $@) --emit=dep-info=$(depfile)
  276. # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
  277. # will be used. We explicitly request `-Ccodegen-units=1` in any case, and
  278. # the compiler shows a warning if it is not 1. However, if we ever stop
  279. # requesting it explicitly and we start using some other `--emit` that does not
  280. # imply it (and for which codegen is performed), then we would be out of sync,
  281. # i.e. the outputs we would get for the different single targets (e.g. `.ll`)
  282. # would not match each other.
  283. quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  284. cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< $(cmd_objtool)
  285. define rule_rustc_o_rs
  286. $(call cmd_and_fixdep,rustc_o_rs)
  287. $(call cmd,gen_objtooldep)
  288. endef
  289. $(obj)/%.o: $(obj)/%.rs FORCE
  290. +$(call if_changed_rule,rustc_o_rs)
  291. quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  292. cmd_rustc_rsi_rs = \
  293. $(rust_common_cmd) -Zunpretty=expanded $< >$@; \
  294. command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) --config-path $(srctree)/.rustfmt.toml $@
  295. $(obj)/%.rsi: $(obj)/%.rs FORCE
  296. +$(call if_changed_dep,rustc_rsi_rs)
  297. quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  298. cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
  299. $(obj)/%.s: $(obj)/%.rs FORCE
  300. +$(call if_changed_dep,rustc_s_rs)
  301. quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
  302. cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
  303. $(obj)/%.ll: $(obj)/%.rs FORCE
  304. +$(call if_changed_dep,rustc_ll_rs)
  305. quiet_cmd_rustc_rs_rs_S = RSCPP $(quiet_modtag) $@
  306. cmd_rustc_rs_rs_S = $(CPP) $(c_flags) -xc -C -P $< | sed '1,/^\/\/ Cut here.$$/d' >$@
  307. $(obj)/%.rs: $(obj)/%.rs.S FORCE
  308. +$(call if_changed_dep,rustc_rs_rs_S)
  309. # Compile assembler sources (.S)
  310. # ---------------------------------------------------------------------------
  311. # .S file exports must have their C prototypes defined in asm/asm-prototypes.h
  312. # or a file that it includes, in order to get versioned symbols. We build a
  313. # dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
  314. # the .S file (with trailing ';'), and run genksyms on that, to extract vers.
  315. #
  316. # This is convoluted. The .S file must first be preprocessed to run guards and
  317. # expand names, then the resulting exports must be constructed into plain
  318. # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
  319. # to make the genksyms input or compiled into an object for gendwarfksyms.
  320. #
  321. # These mirror gensymtypes_c and co above, keep them in synch.
  322. getasmexports = \
  323. { echo "\#include <linux/kernel.h>" ; \
  324. echo "\#include <linux/string.h>" ; \
  325. echo "\#include <asm/asm-prototypes.h>" ; \
  326. $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; }
  327. ifdef CONFIG_GENDWARFKSYMS
  328. cmd_gensymtypes_S = \
  329. $(getasmexports) | \
  330. $(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -; \
  331. $(call getexportsymbols,\1) | \
  332. $(gendwarfksyms) $(@:.o=.gendwarfksyms.o)
  333. else
  334. cmd_gensymtypes_S = \
  335. $(getasmexports) | \
  336. $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
  337. endif # CONFIG_GENDWARFKSYMS
  338. quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
  339. cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
  340. $(obj)/%.s: $(obj)/%.S FORCE
  341. $(call if_changed_dep,cpp_s_S)
  342. ifdef CONFIG_ASM_MODVERSIONS
  343. # versioning matches the C process described above, with difference that
  344. # we parse asm-prototypes.h C header to get function definitions.
  345. cmd_gen_symversions_S = $(call gen_symversions,S)
  346. endif
  347. $(obj)/%.o: $(obj)/%.S FORCE
  348. $(call if_changed_rule,as_o_S)
  349. targets += $(filter-out $(subdir-builtin), $(real-obj-y))
  350. targets += $(filter-out $(subdir-modorder), $(real-obj-m))
  351. targets += $(lib-y) $(always-y)
  352. # Linker scripts preprocessor (.lds.S -> .lds)
  353. # ---------------------------------------------------------------------------
  354. quiet_cmd_cpp_lds_S = LDS $@
  355. cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
  356. -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
  357. $(obj)/%.lds: $(src)/%.lds.S FORCE
  358. $(call if_changed_dep,cpp_lds_S)
  359. # ASN.1 grammar
  360. # ---------------------------------------------------------------------------
  361. quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
  362. cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
  363. $(basename $@).c $(basename $@).h
  364. $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
  365. $(call cmd,asn1_compiler)
  366. # Build the compiled-in targets
  367. # ---------------------------------------------------------------------------
  368. # To build objects in subdirs, we need to descend into the directories
  369. $(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
  370. $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
  371. #
  372. # Rule to compile a set of .o files into one .a file (without symbol table)
  373. #
  374. # To make this rule robust against "Argument list too long" error,
  375. # remove $(obj)/ prefix, and restore it by a shell command.
  376. quiet_cmd_ar_builtin = AR $@
  377. cmd_ar_builtin = rm -f $@; \
  378. $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
  379. $(AR) cDPrST $@
  380. $(obj)/built-in.a: $(real-obj-y) FORCE
  381. $(call if_changed,ar_builtin)
  382. # This is a list of build artifacts from the current Makefile and its
  383. # sub-directories. The timestamp should be updated when any of the member files.
  384. cmd_gen_order = { $(foreach m, $(real-prereqs), \
  385. $(if $(filter %/$(notdir $@), $m), cat $m, echo $m);) :; } \
  386. > $@
  387. $(obj)/modules.order: $(obj-m) FORCE
  388. $(call if_changed,gen_order)
  389. #
  390. # Rule to compile a set of .o files into one .a file (with symbol table)
  391. #
  392. $(obj)/lib.a: $(lib-y) FORCE
  393. $(call if_changed,ar)
  394. quiet_cmd_ld_multi_m = LD [M] $@
  395. cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool)
  396. define rule_ld_multi_m
  397. $(call cmd_and_savecmd,ld_multi_m)
  398. $(call cmd,gen_objtooldep)
  399. endef
  400. $(multi-obj-m): private objtool-enabled := $(delay-objtool)
  401. $(multi-obj-m): private part-of-module := y
  402. $(multi-obj-m): %.o: %.mod FORCE
  403. $(call if_changed_rule,ld_multi_m)
  404. $(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
  405. # Add intermediate targets:
  406. # When building objects with specific suffix patterns, add intermediate
  407. # targets that the final targets are derived from.
  408. intermediate_targets = $(foreach sfx, $(2), \
  409. $(patsubst %$(strip $(1)),%$(sfx), \
  410. $(filter %$(strip $(1)), $(targets))))
  411. # %.asn1.o <- %.asn1.[ch] <- %.asn1
  412. targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h)
  413. # Include additional build rules when necessary
  414. # ---------------------------------------------------------------------------
  415. # $(sort ...) is used here to remove duplicated words and excessive spaces.
  416. hostprogs := $(sort $(hostprogs))
  417. ifneq ($(hostprogs),)
  418. include $(srctree)/scripts/Makefile.host
  419. endif
  420. # $(sort ...) is used here to remove duplicated words and excessive spaces.
  421. userprogs := $(sort $(userprogs))
  422. ifneq ($(userprogs),)
  423. include $(srctree)/scripts/Makefile.userprogs
  424. endif
  425. # Single targets
  426. # ---------------------------------------------------------------------------
  427. single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d))
  428. single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS))
  429. $(single-subdir-goals): $(single-subdirs)
  430. @:
  431. # Descending
  432. # ---------------------------------------------------------------------------
  433. PHONY += $(subdir-ym)
  434. $(subdir-ym):
  435. $(Q)$(MAKE) $(build)=$@ \
  436. need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
  437. need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \
  438. $(filter $@/%, $(single-subdir-goals))
  439. # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
  440. # ---------------------------------------------------------------------------
  441. PHONY += FORCE
  442. FORCE:
  443. targets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS))
  444. targets := $(filter-out $(PHONY), $(targets))
  445. # Now that targets is fully known, include dtb rules if needed
  446. ifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)),)
  447. include $(srctree)/scripts/Makefile.dtbs
  448. endif
  449. # Build
  450. # Needs to be after the include of Makefile.dtbs, which updates always-y
  451. # ---------------------------------------------------------------------------
  452. $(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
  453. $(if $(KBUILD_MODULES), $(targets-for-modules)) \
  454. $(subdir-ym) $(always-y)
  455. @:
  456. # Read all saved command lines and dependencies for the $(targets) we
  457. # may be building above, using $(if_changed{,_dep}). As an
  458. # optimization, we don't need to read them if the target does not
  459. # exist, we will rebuild anyway in that case.
  460. existing-targets := $(wildcard $(sort $(targets)))
  461. -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
  462. # Create directories for object files if they do not exist
  463. obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
  464. # If targets exist, their directories apparently exist. Skip mkdir.
  465. existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
  466. obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
  467. ifneq ($(obj-dirs),)
  468. $(shell mkdir -p $(obj-dirs))
  469. endif
  470. .PHONY: $(PHONY)