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