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