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