1 # SPDX-License-Identifier: GPL-2.0
2 # ==========================================================================
4 # ==========================================================================
6 src := $(srcroot)/$(obj)
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
36 # Read auto.conf if it exists, otherwise ignore
37 -include $(objtree)/include/config/auto.conf
39 include $(srctree)/scripts/Kbuild.include
40 include $(srctree)/scripts/Makefile.compiler
41 include $(kbuild-file)
42 include $(srctree)/scripts/Makefile.lib
45 $(warning kbuild: Makefile.build is included improperly)
48 ifeq ($(need-modorder),)
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.)
55 # ===========================================================================
57 # subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
58 subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
59 subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
61 targets-for-builtin := $(extra-y)
63 ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
64 targets-for-builtin += $(obj)/lib.a
68 targets-for-builtin += $(obj)/built-in.a
71 targets-for-modules := $(foreach x, o mod, \
72 $(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
75 targets-for-modules += $(obj)/modules.order
78 targets += $(targets-for-builtin) $(targets-for-modules)
80 # Linus' kernel sanity checking tool
81 ifeq ($(KBUILD_CHECKSRC),1)
82 quiet_cmd_checksrc = CHECK $<
83 cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
84 else ifeq ($(KBUILD_CHECKSRC),2)
85 quiet_cmd_force_checksrc = CHECK $<
86 cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
89 ifneq ($(KBUILD_EXTRA_WARN),)
90 cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $(KDOCFLAGS) \
91 $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \
95 # Compile C sources (.c)
96 # ---------------------------------------------------------------------------
98 quiet_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 $@ $<
101 $(obj)/%.s: $(obj)/%.c FORCE
102 $(call if_changed_dep,cc_s_c)
104 quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
105 cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
107 $(obj)/%.i: $(obj)/%.c FORCE
108 $(call if_changed_dep,cpp_i_c)
110 getexportsymbols = $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/$(1)/p'
112 gendwarfksyms = $(objtree)/scripts/gendwarfksyms/gendwarfksyms \
113 $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes)) \
114 $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable)
116 genksyms = $(objtree)/scripts/genksyms/genksyms \
117 $(if $(KBUILD_SYMTYPES), -T $(@:.o=.symtypes)) \
118 $(if $(KBUILD_PRESERVE), -p) \
119 $(addprefix -r , $(wildcard $(@:.o=.symref)))
121 # These mirror gensymtypes_S and co below, keep them in synch.
122 ifdef CONFIG_GENDWARFKSYMS
123 cmd_gensymtypes_c = $(if $(skip_gendwarfksyms),, \
124 $(call getexportsymbols,\1) | $(gendwarfksyms) $@)
126 cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
127 endif # CONFIG_GENDWARFKSYMS
130 # Generate .ll files from .c
131 quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
132 cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $<
134 $(obj)/%.ll: $(obj)/%.c FORCE
135 $(call if_changed_dep,cc_ll_c)
138 # The C file is compiled and updated dependency information is generated.
139 # (See cmd_cc_o_c + relevant part of rule_cc_o_c)
141 is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
143 ifdef CONFIG_MODVERSIONS
144 # When module versioning is enabled the following steps are executed:
145 # o compile a <file>.o from <file>.c
146 # o if <file>.o doesn't contain a __export_symbol_*, i.e. does
147 # not export symbols, it's done.
148 # o otherwise, we calculate symbol versions using the good old
149 # genksyms on the preprocessed source and dump them into the .cmd file.
150 # o modpost will extract versions from that file and create *.c files that will
151 # be compiled and linked to the kernel and/or modules.
154 if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \
155 $(cmd_gensymtypes_$1) >> $(dot-target).cmd; \
158 cmd_gen_symversions_c = $(call gen_symversions,c)
162 ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
163 # compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
164 ifdef BUILD_C_RECORDMCOUNT
165 ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
166 RECORDMCOUNT_FLAGS = -w
168 # Due to recursion, we must skip empty.o.
169 # The empty.o file is created in the make process in order to determine
170 # the target endianness and word size. It is made before all other C
171 # files, including recordmcount.
172 sub_cmd_record_mcount = \
173 if [ $(@) != "scripts/mod/empty.o" ]; then \
174 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
176 recordmcount_source := $(srctree)/scripts/recordmcount.c \
177 $(srctree)/scripts/recordmcount.h
179 sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
180 "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
181 "$(if $(CONFIG_64BIT),64,32)" \
182 "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
183 "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
184 "$(if $(part-of-module),1,0)" "$(@)";
185 recordmcount_source := $(srctree)/scripts/recordmcount.pl
186 endif # BUILD_C_RECORDMCOUNT
187 cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
188 $(sub_cmd_record_mcount))
189 endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
191 # 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
192 # 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
193 # 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
195 is-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(target-stem).o)$(OBJECT_FILES_NON_STANDARD)n),$(is-kernel-object))
198 $(obj)/%.o: private objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
201 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
202 cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
205 # Built-in and composite module parts
206 $(obj)/%.o: $(obj)/%.c $(recordmcount_source) FORCE
207 $(call if_changed_rule,cc_o_c)
208 $(call cmd,force_checksrc)
210 # To make this rule robust against "Argument list too long" error,
211 # ensure to add $(obj)/ prefix by a shell command.
212 cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
213 $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
216 $(call if_changed,mod)
218 quiet_cmd_cc_lst_c = MKLST $@
219 cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
220 $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
221 System.map $(OBJDUMP) > $@
223 $(obj)/%.lst: $(obj)/%.c FORCE
224 $(call if_changed_dep,cc_lst_c)
226 # Compile Rust sources (.rs)
227 # ---------------------------------------------------------------------------
229 rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons
231 # `--out-dir` is required to avoid temporaries being created by `rustc` in the
232 # current working directory, which may be not accessible in the out-of-tree
235 OBJTREE=$(abspath $(objtree)) \
236 RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
237 -Zallow-features=$(rust_allowed_features) \
238 -Zcrate-attr=no_std \
239 -Zcrate-attr='feature($(rust_allowed_features))' \
240 -Zunstable-options --extern kernel \
241 --crate-type rlib -L $(objtree)/rust/ \
242 --crate-name $(basename $(notdir $@)) \
243 --sysroot=/dev/null \
244 --out-dir $(dir $@) --emit=dep-info=$(depfile)
246 # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
247 # will be used. We explicitly request `-Ccodegen-units=1` in any case, and
248 # the compiler shows a warning if it is not 1. However, if we ever stop
249 # requesting it explicitly and we start using some other `--emit` that does not
250 # imply it (and for which codegen is performed), then we would be out of sync,
251 # i.e. the outputs we would get for the different single targets (e.g. `.ll`)
252 # would not match each other.
254 quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
255 cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< $(cmd_objtool)
257 define rule_rustc_o_rs
258 $(call cmd_and_fixdep,rustc_o_rs)
259 $(call cmd,gen_objtooldep)
262 $(obj)/%.o: $(obj)/%.rs FORCE
263 +$(call if_changed_rule,rustc_o_rs)
265 quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
267 $(rust_common_cmd) -Zunpretty=expanded $< >$@; \
268 command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@
270 $(obj)/%.rsi: $(obj)/%.rs FORCE
271 +$(call if_changed_dep,rustc_rsi_rs)
273 quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
274 cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
276 $(obj)/%.s: $(obj)/%.rs FORCE
277 +$(call if_changed_dep,rustc_s_rs)
279 quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
280 cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
282 $(obj)/%.ll: $(obj)/%.rs FORCE
283 +$(call if_changed_dep,rustc_ll_rs)
285 quiet_cmd_rustc_rs_rs_S = RSCPP $(quiet_modtag) $@
286 cmd_rustc_rs_rs_S = $(CPP) $(c_flags) -xc -C -P $< | sed '1,/^\/\/ Cut here.$$/d' >$@
288 $(obj)/%.rs: $(obj)/%.rs.S FORCE
289 +$(call if_changed_dep,rustc_rs_rs_S)
291 # Compile assembler sources (.S)
292 # ---------------------------------------------------------------------------
294 # .S file exports must have their C prototypes defined in asm/asm-prototypes.h
295 # or a file that it includes, in order to get versioned symbols. We build a
296 # dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
297 # the .S file (with trailing ';'), and run genksyms on that, to extract vers.
299 # This is convoluted. The .S file must first be preprocessed to run guards and
300 # expand names, then the resulting exports must be constructed into plain
301 # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
302 # to make the genksyms input or compiled into an object for gendwarfksyms.
304 # These mirror gensymtypes_c and co above, keep them in synch.
306 { echo "\#include <linux/kernel.h>" ; \
307 echo "\#include <linux/string.h>" ; \
308 echo "\#include <asm/asm-prototypes.h>" ; \
309 $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; }
311 ifdef CONFIG_GENDWARFKSYMS
312 cmd_gensymtypes_S = \
314 $(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -; \
315 $(call getexportsymbols,\1) | \
316 $(gendwarfksyms) $(@:.o=.gendwarfksyms.o)
318 cmd_gensymtypes_S = \
320 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
321 endif # CONFIG_GENDWARFKSYMS
323 quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
324 cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
326 $(obj)/%.s: $(obj)/%.S FORCE
327 $(call if_changed_dep,cpp_s_S)
329 ifdef CONFIG_ASM_MODVERSIONS
331 # versioning matches the C process described above, with difference that
332 # we parse asm-prototypes.h C header to get function definitions.
334 cmd_gen_symversions_S = $(call gen_symversions,S)
338 $(obj)/%.o: $(obj)/%.S FORCE
339 $(call if_changed_rule,as_o_S)
341 targets += $(filter-out $(subdir-builtin), $(real-obj-y))
342 targets += $(filter-out $(subdir-modorder), $(real-obj-m))
343 targets += $(lib-y) $(always-y)
345 # Linker scripts preprocessor (.lds.S -> .lds)
346 # ---------------------------------------------------------------------------
347 quiet_cmd_cpp_lds_S = LDS $@
348 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
349 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
351 $(obj)/%.lds: $(src)/%.lds.S FORCE
352 $(call if_changed_dep,cpp_lds_S)
355 # ---------------------------------------------------------------------------
356 quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
357 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
358 $(basename $@).c $(basename $@).h
360 $(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
361 $(call cmd,asn1_compiler)
363 # Build the compiled-in targets
364 # ---------------------------------------------------------------------------
366 # To build objects in subdirs, we need to descend into the directories
367 $(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
368 $(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
371 # Rule to compile a set of .o files into one .a file (without symbol table)
373 # To make this rule robust against "Argument list too long" error,
374 # 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) \
381 $(obj)/built-in.a: $(real-obj-y) FORCE
382 $(call if_changed,ar_builtin)
384 # This is a list of build artifacts from the current Makefile and its
385 # sub-directories. The timestamp should be updated when any of the member files.
387 cmd_gen_order = { $(foreach m, $(real-prereqs), \
388 $(if $(filter %/$(notdir $@), $m), cat $m, echo $m);) :; } \
391 $(obj)/modules.order: $(obj-m) FORCE
392 $(call if_changed,gen_order)
395 # Rule to compile a set of .o files into one .a file (with symbol table)
398 $(obj)/lib.a: $(lib-y) FORCE
399 $(call if_changed,ar)
401 quiet_cmd_ld_multi_m = LD [M] $@
402 cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool)
404 define rule_ld_multi_m
405 $(call cmd_and_savecmd,ld_multi_m)
406 $(call cmd,gen_objtooldep)
409 $(multi-obj-m): private objtool-enabled := $(delay-objtool)
410 $(multi-obj-m): private part-of-module := y
411 $(multi-obj-m): %.o: %.mod FORCE
412 $(call if_changed_rule,ld_multi_m)
413 $(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
415 # Add intermediate targets:
416 # When building objects with specific suffix patterns, add intermediate
417 # targets that the final targets are derived from.
418 intermediate_targets = $(foreach sfx, $(2), \
419 $(patsubst %$(strip $(1)),%$(sfx), \
420 $(filter %$(strip $(1)), $(targets))))
421 # %.asn1.o <- %.asn1.[ch] <- %.asn1
422 targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h)
424 # Include additional build rules when necessary
425 # ---------------------------------------------------------------------------
427 # $(sort ...) is used here to remove duplicated words and excessive spaces.
428 hostprogs := $(sort $(hostprogs))
429 ifneq ($(hostprogs),)
430 include $(srctree)/scripts/Makefile.host
433 # $(sort ...) is used here to remove duplicated words and excessive spaces.
434 userprogs := $(sort $(userprogs))
435 ifneq ($(userprogs),)
436 include $(srctree)/scripts/Makefile.userprogs
439 ifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)),)
440 include $(srctree)/scripts/Makefile.dtbs
444 # ---------------------------------------------------------------------------
446 $(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
447 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
448 $(subdir-ym) $(always-y)
452 # ---------------------------------------------------------------------------
454 single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d))
455 single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS))
457 $(single-subdir-goals): $(single-subdirs)
461 # ---------------------------------------------------------------------------
463 PHONY += $(subdir-ym)
465 $(Q)$(MAKE) $(build)=$@ \
466 need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
467 need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \
468 $(filter $@/%, $(single-subdir-goals))
470 # Add FORCE to the prerequisites of a target to force it to be always rebuilt.
471 # ---------------------------------------------------------------------------
477 targets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS))
478 targets := $(filter-out $(PHONY), $(targets))
480 # Read all saved command lines and dependencies for the $(targets) we
481 # may be building above, using $(if_changed{,_dep}). As an
482 # optimization, we don't need to read them if the target does not
483 # exist, we will rebuild anyway in that case.
485 existing-targets := $(wildcard $(sort $(targets)))
487 -include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
489 # Create directories for object files if they do not exist
490 obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
491 # If targets exist, their directories apparently exist. Skip mkdir.
492 existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
493 obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
495 $(shell mkdir -p $(obj-dirs))