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 = $(srctree)/scripts/kernel-doc -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 225rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op 226 227# `--out-dir` is required to avoid temporaries being created by `rustc` in the 228# current working directory, which may be not accessible in the out-of-tree 229# modules case. 230rust_common_cmd = \ 231 OBJTREE=$(abspath $(objtree)) \ 232 RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \ 233 -Zallow-features=$(rust_allowed_features) \ 234 -Zcrate-attr=no_std \ 235 -Zcrate-attr='feature($(rust_allowed_features))' \ 236 -Zunstable-options --extern pin_init --extern kernel \ 237 --crate-type rlib -L $(objtree)/rust/ \ 238 --crate-name $(basename $(notdir $@)) \ 239 --sysroot=/dev/null \ 240 --out-dir $(dir $@) --emit=dep-info=$(depfile) 241 242# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit 243# will be used. We explicitly request `-Ccodegen-units=1` in any case, and 244# the compiler shows a warning if it is not 1. However, if we ever stop 245# requesting it explicitly and we start using some other `--emit` that does not 246# imply it (and for which codegen is performed), then we would be out of sync, 247# i.e. the outputs we would get for the different single targets (e.g. `.ll`) 248# would not match each other. 249 250quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 251 cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $< $(cmd_objtool) 252 253define rule_rustc_o_rs 254 $(call cmd_and_fixdep,rustc_o_rs) 255 $(call cmd,gen_objtooldep) 256endef 257 258$(obj)/%.o: $(obj)/%.rs FORCE 259 +$(call if_changed_rule,rustc_o_rs) 260 261quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 262 cmd_rustc_rsi_rs = \ 263 $(rust_common_cmd) -Zunpretty=expanded $< >$@; \ 264 command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@ 265 266$(obj)/%.rsi: $(obj)/%.rs FORCE 267 +$(call if_changed_dep,rustc_rsi_rs) 268 269quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 270 cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $< 271 272$(obj)/%.s: $(obj)/%.rs FORCE 273 +$(call if_changed_dep,rustc_s_rs) 274 275quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ 276 cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $< 277 278$(obj)/%.ll: $(obj)/%.rs FORCE 279 +$(call if_changed_dep,rustc_ll_rs) 280 281quiet_cmd_rustc_rs_rs_S = RSCPP $(quiet_modtag) $@ 282 cmd_rustc_rs_rs_S = $(CPP) $(c_flags) -xc -C -P $< | sed '1,/^\/\/ Cut here.$$/d' >$@ 283 284$(obj)/%.rs: $(obj)/%.rs.S FORCE 285 +$(call if_changed_dep,rustc_rs_rs_S) 286 287# Compile assembler sources (.S) 288# --------------------------------------------------------------------------- 289 290# .S file exports must have their C prototypes defined in asm/asm-prototypes.h 291# or a file that it includes, in order to get versioned symbols. We build a 292# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from 293# the .S file (with trailing ';'), and run genksyms on that, to extract vers. 294# 295# This is convoluted. The .S file must first be preprocessed to run guards and 296# expand names, then the resulting exports must be constructed into plain 297# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed 298# to make the genksyms input or compiled into an object for gendwarfksyms. 299# 300# These mirror gensymtypes_c and co above, keep them in synch. 301getasmexports = \ 302 { echo "\#include <linux/kernel.h>" ; \ 303 echo "\#include <linux/string.h>" ; \ 304 echo "\#include <asm/asm-prototypes.h>" ; \ 305 $(call getexportsymbols,EXPORT_SYMBOL(\1);) ; } 306 307ifdef CONFIG_GENDWARFKSYMS 308cmd_gensymtypes_S = \ 309 $(getasmexports) | \ 310 $(CC) $(c_flags) -c -o $(@:.o=.gendwarfksyms.o) -xc -; \ 311 $(call getexportsymbols,\1) | \ 312 $(gendwarfksyms) $(@:.o=.gendwarfksyms.o) 313else 314cmd_gensymtypes_S = \ 315 $(getasmexports) | \ 316 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) 317endif # CONFIG_GENDWARFKSYMS 318 319quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@ 320cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $< 321 322$(obj)/%.s: $(obj)/%.S FORCE 323 $(call if_changed_dep,cpp_s_S) 324 325ifdef CONFIG_ASM_MODVERSIONS 326 327# versioning matches the C process described above, with difference that 328# we parse asm-prototypes.h C header to get function definitions. 329 330cmd_gen_symversions_S = $(call gen_symversions,S) 331 332endif 333 334$(obj)/%.o: $(obj)/%.S FORCE 335 $(call if_changed_rule,as_o_S) 336 337targets += $(filter-out $(subdir-builtin), $(real-obj-y)) 338targets += $(filter-out $(subdir-modorder), $(real-obj-m)) 339targets += $(lib-y) $(always-y) 340 341# Linker scripts preprocessor (.lds.S -> .lds) 342# --------------------------------------------------------------------------- 343quiet_cmd_cpp_lds_S = LDS $@ 344 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \ 345 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $< 346 347$(obj)/%.lds: $(src)/%.lds.S FORCE 348 $(call if_changed_dep,cpp_lds_S) 349 350# ASN.1 grammar 351# --------------------------------------------------------------------------- 352quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch] 353 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \ 354 $(basename $@).c $(basename $@).h 355 356$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler 357 $(call cmd,asn1_compiler) 358 359# Build the compiled-in targets 360# --------------------------------------------------------------------------- 361 362# To build objects in subdirs, we need to descend into the directories 363$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ; 364$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ; 365 366# 367# Rule to compile a set of .o files into one .a file (without symbol table) 368# 369# To make this rule robust against "Argument list too long" error, 370# remove $(obj)/ prefix, and restore it by a shell command. 371 372quiet_cmd_ar_builtin = AR $@ 373 cmd_ar_builtin = rm -f $@; \ 374 $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \ 375 $(AR) cDPrST $@ 376 377$(obj)/built-in.a: $(real-obj-y) FORCE 378 $(call if_changed,ar_builtin) 379 380# This is a list of build artifacts from the current Makefile and its 381# sub-directories. The timestamp should be updated when any of the member files. 382 383cmd_gen_order = { $(foreach m, $(real-prereqs), \ 384 $(if $(filter %/$(notdir $@), $m), cat $m, echo $m);) :; } \ 385 > $@ 386 387$(obj)/modules.order: $(obj-m) FORCE 388 $(call if_changed,gen_order) 389 390# 391# Rule to compile a set of .o files into one .a file (with symbol table) 392# 393 394$(obj)/lib.a: $(lib-y) FORCE 395 $(call if_changed,ar) 396 397quiet_cmd_ld_multi_m = LD [M] $@ 398 cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool) 399 400define rule_ld_multi_m 401 $(call cmd_and_savecmd,ld_multi_m) 402 $(call cmd,gen_objtooldep) 403endef 404 405$(multi-obj-m): private objtool-enabled := $(delay-objtool) 406$(multi-obj-m): private part-of-module := y 407$(multi-obj-m): %.o: %.mod FORCE 408 $(call if_changed_rule,ld_multi_m) 409$(call multi_depend, $(multi-obj-m), .o, -objs -y -m) 410 411# Add intermediate targets: 412# When building objects with specific suffix patterns, add intermediate 413# targets that the final targets are derived from. 414intermediate_targets = $(foreach sfx, $(2), \ 415 $(patsubst %$(strip $(1)),%$(sfx), \ 416 $(filter %$(strip $(1)), $(targets)))) 417# %.asn1.o <- %.asn1.[ch] <- %.asn1 418targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) 419 420# Include additional build rules when necessary 421# --------------------------------------------------------------------------- 422 423# $(sort ...) is used here to remove duplicated words and excessive spaces. 424hostprogs := $(sort $(hostprogs)) 425ifneq ($(hostprogs),) 426include $(srctree)/scripts/Makefile.host 427endif 428 429# $(sort ...) is used here to remove duplicated words and excessive spaces. 430userprogs := $(sort $(userprogs)) 431ifneq ($(userprogs),) 432include $(srctree)/scripts/Makefile.userprogs 433endif 434 435ifneq ($(need-dtbslist)$(dtb-y)$(dtb-)$(filter %.dtb %.dtb.o %.dtbo.o,$(targets)),) 436include $(srctree)/scripts/Makefile.dtbs 437endif 438 439# Build 440# --------------------------------------------------------------------------- 441 442$(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \ 443 $(if $(KBUILD_MODULES), $(targets-for-modules)) \ 444 $(subdir-ym) $(always-y) 445 @: 446 447# Single targets 448# --------------------------------------------------------------------------- 449 450single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d)) 451single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS)) 452 453$(single-subdir-goals): $(single-subdirs) 454 @: 455 456# Descending 457# --------------------------------------------------------------------------- 458 459PHONY += $(subdir-ym) 460$(subdir-ym): 461 $(Q)$(MAKE) $(build)=$@ \ 462 need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ 463 need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ 464 $(filter $@/%, $(single-subdir-goals)) 465 466# Add FORCE to the prerequisites of a target to force it to be always rebuilt. 467# --------------------------------------------------------------------------- 468 469PHONY += FORCE 470 471FORCE: 472 473targets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS)) 474targets := $(filter-out $(PHONY), $(targets)) 475 476# Read all saved command lines and dependencies for the $(targets) we 477# may be building above, using $(if_changed{,_dep}). As an 478# optimization, we don't need to read them if the target does not 479# exist, we will rebuild anyway in that case. 480 481existing-targets := $(wildcard $(sort $(targets))) 482 483-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd) 484 485# Create directories for object files if they do not exist 486obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets)))) 487# If targets exist, their directories apparently exist. Skip mkdir. 488existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets)))) 489obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs))) 490ifneq ($(obj-dirs),) 491$(shell mkdir -p $(obj-dirs)) 492endif 493 494.PHONY: $(PHONY) 495