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