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