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