162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci# ==========================================================================
362306a36Sopenharmony_ci# Building
462306a36Sopenharmony_ci# ==========================================================================
562306a36Sopenharmony_ci
662306a36Sopenharmony_cisrc := $(obj)
762306a36Sopenharmony_ci
862306a36Sopenharmony_ciPHONY := $(obj)/
962306a36Sopenharmony_ci$(obj)/:
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci# Init all relevant variables used in kbuild files so
1262306a36Sopenharmony_ci# 1) they have correct type
1362306a36Sopenharmony_ci# 2) they do not inherit any value from the environment
1462306a36Sopenharmony_ciobj-y :=
1562306a36Sopenharmony_ciobj-m :=
1662306a36Sopenharmony_cilib-y :=
1762306a36Sopenharmony_cilib-m :=
1862306a36Sopenharmony_cialways-y :=
1962306a36Sopenharmony_cialways-m :=
2062306a36Sopenharmony_citargets :=
2162306a36Sopenharmony_cisubdir-y :=
2262306a36Sopenharmony_cisubdir-m :=
2362306a36Sopenharmony_ciEXTRA_AFLAGS   :=
2462306a36Sopenharmony_ciEXTRA_CFLAGS   :=
2562306a36Sopenharmony_ciEXTRA_CPPFLAGS :=
2662306a36Sopenharmony_ciEXTRA_LDFLAGS  :=
2762306a36Sopenharmony_ciasflags-y  :=
2862306a36Sopenharmony_ciccflags-y  :=
2962306a36Sopenharmony_cirustflags-y :=
3062306a36Sopenharmony_cicppflags-y :=
3162306a36Sopenharmony_cildflags-y  :=
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cisubdir-asflags-y :=
3462306a36Sopenharmony_cisubdir-ccflags-y :=
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci# Read auto.conf if it exists, otherwise ignore
3762306a36Sopenharmony_ci-include include/config/auto.conf
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciinclude $(srctree)/scripts/Kbuild.include
4062306a36Sopenharmony_ciinclude $(srctree)/scripts/Makefile.compiler
4162306a36Sopenharmony_ciinclude $(kbuild-file)
4262306a36Sopenharmony_ciinclude $(srctree)/scripts/Makefile.lib
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci# Do not include hostprogs rules unless needed.
4562306a36Sopenharmony_ci# $(sort ...) is used here to remove duplicated words and excessive spaces.
4662306a36Sopenharmony_cihostprogs := $(sort $(hostprogs))
4762306a36Sopenharmony_ciifneq ($(hostprogs),)
4862306a36Sopenharmony_ciinclude $(srctree)/scripts/Makefile.host
4962306a36Sopenharmony_ciendif
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci# Do not include userprogs rules unless needed.
5262306a36Sopenharmony_ci# $(sort ...) is used here to remove duplicated words and excessive spaces.
5362306a36Sopenharmony_ciuserprogs := $(sort $(userprogs))
5462306a36Sopenharmony_ciifneq ($(userprogs),)
5562306a36Sopenharmony_ciinclude $(srctree)/scripts/Makefile.userprogs
5662306a36Sopenharmony_ciendif
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ciifndef obj
5962306a36Sopenharmony_ci$(warning kbuild: Makefile.build is included improperly)
6062306a36Sopenharmony_ciendif
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciifeq ($(need-modorder),)
6362306a36Sopenharmony_ciifneq ($(obj-m),)
6462306a36Sopenharmony_ci$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
6562306a36Sopenharmony_ci$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
6662306a36Sopenharmony_ciendif
6762306a36Sopenharmony_ciendif
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci# ===========================================================================
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
7262306a36Sopenharmony_cisubdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
7362306a36Sopenharmony_cisubdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_citargets-for-builtin := $(extra-y)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
7862306a36Sopenharmony_citargets-for-builtin += $(obj)/lib.a
7962306a36Sopenharmony_ciendif
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ciifdef need-builtin
8262306a36Sopenharmony_citargets-for-builtin += $(obj)/built-in.a
8362306a36Sopenharmony_ciendif
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_citargets-for-modules := $(foreach x, o mod, \
8662306a36Sopenharmony_ci				$(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciifdef need-modorder
8962306a36Sopenharmony_citargets-for-modules += $(obj)/modules.order
9062306a36Sopenharmony_ciendif
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_citargets += $(targets-for-builtin) $(targets-for-modules)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci# Linus' kernel sanity checking tool
9562306a36Sopenharmony_ciifeq ($(KBUILD_CHECKSRC),1)
9662306a36Sopenharmony_ci  quiet_cmd_checksrc       = CHECK   $<
9762306a36Sopenharmony_ci        cmd_checksrc       = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
9862306a36Sopenharmony_cielse ifeq ($(KBUILD_CHECKSRC),2)
9962306a36Sopenharmony_ci  quiet_cmd_force_checksrc = CHECK   $<
10062306a36Sopenharmony_ci        cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
10162306a36Sopenharmony_ciendif
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciifneq ($(KBUILD_EXTRA_WARN),)
10462306a36Sopenharmony_ci  cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $(KDOCFLAGS) \
10562306a36Sopenharmony_ci        $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \
10662306a36Sopenharmony_ci        $<
10762306a36Sopenharmony_ciendif
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci# Compile C sources (.c)
11062306a36Sopenharmony_ci# ---------------------------------------------------------------------------
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ciquiet_cmd_cc_s_c = CC $(quiet_modtag)  $@
11362306a36Sopenharmony_ci      cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci$(obj)/%.s: $(src)/%.c FORCE
11662306a36Sopenharmony_ci	$(call if_changed_dep,cc_s_c)
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ciquiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
11962306a36Sopenharmony_cicmd_cpp_i_c       = $(CPP) $(c_flags) -o $@ $<
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci$(obj)/%.i: $(src)/%.c FORCE
12262306a36Sopenharmony_ci	$(call if_changed_dep,cpp_i_c)
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cigenksyms = scripts/genksyms/genksyms		\
12562306a36Sopenharmony_ci	$(if $(1), -T $(2))			\
12662306a36Sopenharmony_ci	$(if $(KBUILD_PRESERVE), -p)		\
12762306a36Sopenharmony_ci	-r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci# These mirror gensymtypes_S and co below, keep them in synch.
13062306a36Sopenharmony_cicmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciquiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
13362306a36Sopenharmony_ci      cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci$(obj)/%.symtypes : $(src)/%.c FORCE
13662306a36Sopenharmony_ci	$(call cmd,cc_symtypes_c)
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci# LLVM assembly
13962306a36Sopenharmony_ci# Generate .ll files from .c
14062306a36Sopenharmony_ciquiet_cmd_cc_ll_c = CC $(quiet_modtag)  $@
14162306a36Sopenharmony_ci      cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $<
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci$(obj)/%.ll: $(src)/%.c FORCE
14462306a36Sopenharmony_ci	$(call if_changed_dep,cc_ll_c)
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci# C (.c) files
14762306a36Sopenharmony_ci# The C file is compiled and updated dependency information is generated.
14862306a36Sopenharmony_ci# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ciis-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci# When a module consists of a single object, there is no reason to keep LLVM IR.
15362306a36Sopenharmony_ci# Make $(LD) covert LLVM IR to ELF here.
15462306a36Sopenharmony_ciifdef CONFIG_LTO_CLANG
15562306a36Sopenharmony_cicmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
15662306a36Sopenharmony_ciendif
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciquiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
15962306a36Sopenharmony_ci      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
16062306a36Sopenharmony_ci		$(cmd_ld_single_m) \
16162306a36Sopenharmony_ci		$(cmd_objtool)
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ciifdef CONFIG_MODVERSIONS
16462306a36Sopenharmony_ci# When module versioning is enabled the following steps are executed:
16562306a36Sopenharmony_ci# o compile a <file>.o from <file>.c
16662306a36Sopenharmony_ci# o if <file>.o doesn't contain a __export_symbol_*, i.e. does
16762306a36Sopenharmony_ci#   not export symbols, it's done.
16862306a36Sopenharmony_ci# o otherwise, we calculate symbol versions using the good old
16962306a36Sopenharmony_ci#   genksyms on the preprocessed source and dump them into the .cmd file.
17062306a36Sopenharmony_ci# o modpost will extract versions from that file and create *.c files that will
17162306a36Sopenharmony_ci#   be compiled and linked to the kernel and/or modules.
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_cigen_symversions =								\
17462306a36Sopenharmony_ci	if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then		\
17562306a36Sopenharmony_ci		$(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
17662306a36Sopenharmony_ci			>> $(dot-target).cmd;					\
17762306a36Sopenharmony_ci	fi
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cicmd_gen_symversions_c =	$(call gen_symversions,c)
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ciendif
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ciifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
18462306a36Sopenharmony_ci# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
18562306a36Sopenharmony_ciifdef BUILD_C_RECORDMCOUNT
18662306a36Sopenharmony_ciifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
18762306a36Sopenharmony_ci  RECORDMCOUNT_FLAGS = -w
18862306a36Sopenharmony_ciendif
18962306a36Sopenharmony_ci# Due to recursion, we must skip empty.o.
19062306a36Sopenharmony_ci# The empty.o file is created in the make process in order to determine
19162306a36Sopenharmony_ci# the target endianness and word size. It is made before all other C
19262306a36Sopenharmony_ci# files, including recordmcount.
19362306a36Sopenharmony_cisub_cmd_record_mcount =					\
19462306a36Sopenharmony_ci	if [ $(@) != "scripts/mod/empty.o" ]; then	\
19562306a36Sopenharmony_ci		$(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)";	\
19662306a36Sopenharmony_ci	fi;
19762306a36Sopenharmony_cirecordmcount_source := $(srctree)/scripts/recordmcount.c \
19862306a36Sopenharmony_ci		    $(srctree)/scripts/recordmcount.h
19962306a36Sopenharmony_cielse
20062306a36Sopenharmony_cisub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
20162306a36Sopenharmony_ci	"$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
20262306a36Sopenharmony_ci	"$(if $(CONFIG_64BIT),64,32)" \
20362306a36Sopenharmony_ci	"$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
20462306a36Sopenharmony_ci	"$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
20562306a36Sopenharmony_ci	"$(if $(part-of-module),1,0)" "$(@)";
20662306a36Sopenharmony_cirecordmcount_source := $(srctree)/scripts/recordmcount.pl
20762306a36Sopenharmony_ciendif # BUILD_C_RECORDMCOUNT
20862306a36Sopenharmony_cicmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)),	\
20962306a36Sopenharmony_ci	$(sub_cmd_record_mcount))
21062306a36Sopenharmony_ciendif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
21362306a36Sopenharmony_ci# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
21462306a36Sopenharmony_ci# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciis-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y)
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci$(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ciifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
22162306a36Sopenharmony_cicmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
22262306a36Sopenharmony_ciendif
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_cidefine rule_cc_o_c
22562306a36Sopenharmony_ci	$(call cmd_and_fixdep,cc_o_c)
22662306a36Sopenharmony_ci	$(call cmd,checksrc)
22762306a36Sopenharmony_ci	$(call cmd,checkdoc)
22862306a36Sopenharmony_ci	$(call cmd,gen_objtooldep)
22962306a36Sopenharmony_ci	$(call cmd,gen_symversions_c)
23062306a36Sopenharmony_ci	$(call cmd,record_mcount)
23162306a36Sopenharmony_ci	$(call cmd,warn_shared_object)
23262306a36Sopenharmony_ciendef
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cidefine rule_as_o_S
23562306a36Sopenharmony_ci	$(call cmd_and_fixdep,as_o_S)
23662306a36Sopenharmony_ci	$(call cmd,gen_objtooldep)
23762306a36Sopenharmony_ci	$(call cmd,gen_symversions_S)
23862306a36Sopenharmony_ci	$(call cmd,warn_shared_object)
23962306a36Sopenharmony_ciendef
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci# Built-in and composite module parts
24262306a36Sopenharmony_ci$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
24362306a36Sopenharmony_ci	$(call if_changed_rule,cc_o_c)
24462306a36Sopenharmony_ci	$(call cmd,force_checksrc)
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ci# To make this rule robust against "Argument list too long" error,
24762306a36Sopenharmony_ci# ensure to add $(obj)/ prefix by a shell command.
24862306a36Sopenharmony_cicmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
24962306a36Sopenharmony_ci	$(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci$(obj)/%.mod: FORCE
25262306a36Sopenharmony_ci	$(call if_changed,mod)
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ciquiet_cmd_cc_lst_c = MKLST   $@
25562306a36Sopenharmony_ci      cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
25662306a36Sopenharmony_ci		     $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
25762306a36Sopenharmony_ci				     System.map $(OBJDUMP) > $@
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci$(obj)/%.lst: $(src)/%.c FORCE
26062306a36Sopenharmony_ci	$(call if_changed_dep,cc_lst_c)
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci# Compile Rust sources (.rs)
26362306a36Sopenharmony_ci# ---------------------------------------------------------------------------
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_cirust_allowed_features := new_uninit
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci# `--out-dir` is required to avoid temporaries being created by `rustc` in the
26862306a36Sopenharmony_ci# current working directory, which may be not accessible in the out-of-tree
26962306a36Sopenharmony_ci# modules case.
27062306a36Sopenharmony_cirust_common_cmd = \
27162306a36Sopenharmony_ci	RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
27262306a36Sopenharmony_ci	-Zallow-features=$(rust_allowed_features) \
27362306a36Sopenharmony_ci	-Zcrate-attr=no_std \
27462306a36Sopenharmony_ci	-Zcrate-attr='feature($(rust_allowed_features))' \
27562306a36Sopenharmony_ci	--extern alloc --extern kernel \
27662306a36Sopenharmony_ci	--crate-type rlib -L $(objtree)/rust/ \
27762306a36Sopenharmony_ci	--crate-name $(basename $(notdir $@)) \
27862306a36Sopenharmony_ci	--out-dir $(dir $@) --emit=dep-info=$(depfile)
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
28162306a36Sopenharmony_ci# will be used. We explicitly request `-Ccodegen-units=1` in any case, and
28262306a36Sopenharmony_ci# the compiler shows a warning if it is not 1. However, if we ever stop
28362306a36Sopenharmony_ci# requesting it explicitly and we start using some other `--emit` that does not
28462306a36Sopenharmony_ci# imply it (and for which codegen is performed), then we would be out of sync,
28562306a36Sopenharmony_ci# i.e. the outputs we would get for the different single targets (e.g. `.ll`)
28662306a36Sopenharmony_ci# would not match each other.
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ciquiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
28962306a36Sopenharmony_ci      cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $<
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci$(obj)/%.o: $(src)/%.rs FORCE
29262306a36Sopenharmony_ci	$(call if_changed_dep,rustc_o_rs)
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ciquiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
29562306a36Sopenharmony_ci      cmd_rustc_rsi_rs = \
29662306a36Sopenharmony_ci	$(rust_common_cmd) -Zunpretty=expanded $< >$@; \
29762306a36Sopenharmony_ci	command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci$(obj)/%.rsi: $(src)/%.rs FORCE
30062306a36Sopenharmony_ci	$(call if_changed_dep,rustc_rsi_rs)
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ciquiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
30362306a36Sopenharmony_ci      cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci$(obj)/%.s: $(src)/%.rs FORCE
30662306a36Sopenharmony_ci	$(call if_changed_dep,rustc_s_rs)
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ciquiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
30962306a36Sopenharmony_ci      cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci$(obj)/%.ll: $(src)/%.rs FORCE
31262306a36Sopenharmony_ci	$(call if_changed_dep,rustc_ll_rs)
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci# Compile assembler sources (.S)
31562306a36Sopenharmony_ci# ---------------------------------------------------------------------------
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci# .S file exports must have their C prototypes defined in asm/asm-prototypes.h
31862306a36Sopenharmony_ci# or a file that it includes, in order to get versioned symbols. We build a
31962306a36Sopenharmony_ci# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
32062306a36Sopenharmony_ci# the .S file (with trailing ';'), and run genksyms on that, to extract vers.
32162306a36Sopenharmony_ci#
32262306a36Sopenharmony_ci# This is convoluted. The .S file must first be preprocessed to run guards and
32362306a36Sopenharmony_ci# expand names, then the resulting exports must be constructed into plain
32462306a36Sopenharmony_ci# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
32562306a36Sopenharmony_ci# to make the genksyms input.
32662306a36Sopenharmony_ci#
32762306a36Sopenharmony_ci# These mirror gensymtypes_c and co above, keep them in synch.
32862306a36Sopenharmony_cicmd_gensymtypes_S =                                                         \
32962306a36Sopenharmony_ci   { echo "\#include <linux/kernel.h>" ;                                    \
33062306a36Sopenharmony_ci     echo "\#include <asm/asm-prototypes.h>" ;                              \
33162306a36Sopenharmony_ci     $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \
33262306a36Sopenharmony_ci    $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ciquiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
33562306a36Sopenharmony_ci      cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci$(obj)/%.symtypes : $(src)/%.S FORCE
33862306a36Sopenharmony_ci	$(call cmd,cc_symtypes_S)
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ciquiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
34262306a36Sopenharmony_cicmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci$(obj)/%.s: $(src)/%.S FORCE
34562306a36Sopenharmony_ci	$(call if_changed_dep,cpp_s_S)
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ciquiet_cmd_as_o_S = AS $(quiet_modtag)  $@
34862306a36Sopenharmony_ci      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ciifdef CONFIG_ASM_MODVERSIONS
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci# versioning matches the C process described above, with difference that
35362306a36Sopenharmony_ci# we parse asm-prototypes.h C header to get function definitions.
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_cicmd_gen_symversions_S = $(call gen_symversions,S)
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ciendif
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci$(obj)/%.o: $(src)/%.S FORCE
36062306a36Sopenharmony_ci	$(call if_changed_rule,as_o_S)
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_citargets += $(filter-out $(subdir-builtin), $(real-obj-y))
36362306a36Sopenharmony_citargets += $(filter-out $(subdir-modorder), $(real-obj-m))
36462306a36Sopenharmony_citargets += $(real-dtb-y) $(lib-y) $(always-y)
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci# Linker scripts preprocessor (.lds.S -> .lds)
36762306a36Sopenharmony_ci# ---------------------------------------------------------------------------
36862306a36Sopenharmony_ciquiet_cmd_cpp_lds_S = LDS     $@
36962306a36Sopenharmony_ci      cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
37062306a36Sopenharmony_ci	                     -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci$(obj)/%.lds: $(src)/%.lds.S FORCE
37362306a36Sopenharmony_ci	$(call if_changed_dep,cpp_lds_S)
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci# ASN.1 grammar
37662306a36Sopenharmony_ci# ---------------------------------------------------------------------------
37762306a36Sopenharmony_ciquiet_cmd_asn1_compiler = ASN.1   $(basename $@).[ch]
37862306a36Sopenharmony_ci      cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
37962306a36Sopenharmony_ci				$(basename $@).c $(basename $@).h
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
38262306a36Sopenharmony_ci	$(call cmd,asn1_compiler)
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci# Build the compiled-in targets
38562306a36Sopenharmony_ci# ---------------------------------------------------------------------------
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci# To build objects in subdirs, we need to descend into the directories
38862306a36Sopenharmony_ci$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
38962306a36Sopenharmony_ci$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci#
39262306a36Sopenharmony_ci# Rule to compile a set of .o files into one .a file (without symbol table)
39362306a36Sopenharmony_ci#
39462306a36Sopenharmony_ci# To make this rule robust against "Argument list too long" error,
39562306a36Sopenharmony_ci# remove $(obj)/ prefix, and restore it by a shell command.
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ciquiet_cmd_ar_builtin = AR      $@
39862306a36Sopenharmony_ci      cmd_ar_builtin = rm -f $@; \
39962306a36Sopenharmony_ci	$(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
40062306a36Sopenharmony_ci	$(AR) cDPrST $@
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci$(obj)/built-in.a: $(real-obj-y) FORCE
40362306a36Sopenharmony_ci	$(call if_changed,ar_builtin)
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci#
40662306a36Sopenharmony_ci# Rule to create modules.order file
40762306a36Sopenharmony_ci#
40862306a36Sopenharmony_ci# Create commands to either record .ko file or cat modules.order from
40962306a36Sopenharmony_ci# a subdirectory
41062306a36Sopenharmony_ci# Add $(obj-m) as the prerequisite to avoid updating the timestamp of
41162306a36Sopenharmony_ci# modules.order unless contained modules are updated.
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_cicmd_modules_order = { $(foreach m, $(real-prereqs), \
41462306a36Sopenharmony_ci	$(if $(filter %/modules.order, $m), cat $m, echo $m);) :; } \
41562306a36Sopenharmony_ci	> $@
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci$(obj)/modules.order: $(obj-m) FORCE
41862306a36Sopenharmony_ci	$(call if_changed,modules_order)
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci#
42162306a36Sopenharmony_ci# Rule to compile a set of .o files into one .a file (with symbol table)
42262306a36Sopenharmony_ci#
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci$(obj)/lib.a: $(lib-y) FORCE
42562306a36Sopenharmony_ci	$(call if_changed,ar)
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ciquiet_cmd_ld_multi_m = LD [M]  $@
42862306a36Sopenharmony_ci      cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$(patsubst %.o,%.mod,$@) $(cmd_objtool)
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_cidefine rule_ld_multi_m
43162306a36Sopenharmony_ci	$(call cmd_and_savecmd,ld_multi_m)
43262306a36Sopenharmony_ci	$(call cmd,gen_objtooldep)
43362306a36Sopenharmony_ciendef
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci$(multi-obj-m): objtool-enabled := $(delay-objtool)
43662306a36Sopenharmony_ci$(multi-obj-m): part-of-module := y
43762306a36Sopenharmony_ci$(multi-obj-m): %.o: %.mod FORCE
43862306a36Sopenharmony_ci	$(call if_changed_rule,ld_multi_m)
43962306a36Sopenharmony_ci$(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci# Add intermediate targets:
44262306a36Sopenharmony_ci# When building objects with specific suffix patterns, add intermediate
44362306a36Sopenharmony_ci# targets that the final targets are derived from.
44462306a36Sopenharmony_ciintermediate_targets = $(foreach sfx, $(2), \
44562306a36Sopenharmony_ci				$(patsubst %$(strip $(1)),%$(sfx), \
44662306a36Sopenharmony_ci					$(filter %$(strip $(1)), $(targets))))
44762306a36Sopenharmony_ci# %.asn1.o <- %.asn1.[ch] <- %.asn1
44862306a36Sopenharmony_ci# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
44962306a36Sopenharmony_ci# %.dtbo.o <- %.dtbo.S <- %.dtbo <- %.dtso
45062306a36Sopenharmony_ci# %.lex.o <- %.lex.c <- %.l
45162306a36Sopenharmony_ci# %.tab.o <- %.tab.[ch] <- %.y
45262306a36Sopenharmony_citargets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
45362306a36Sopenharmony_ci	   $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
45462306a36Sopenharmony_ci	   $(call intermediate_targets, .dtbo.o, .dtbo.S .dtbo) \
45562306a36Sopenharmony_ci	   $(call intermediate_targets, .lex.o, .lex.c) \
45662306a36Sopenharmony_ci	   $(call intermediate_targets, .tab.o, .tab.c .tab.h)
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci# Build
45962306a36Sopenharmony_ci# ---------------------------------------------------------------------------
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci$(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
46262306a36Sopenharmony_ci	 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
46362306a36Sopenharmony_ci	 $(subdir-ym) $(always-y)
46462306a36Sopenharmony_ci	@:
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci# Single targets
46762306a36Sopenharmony_ci# ---------------------------------------------------------------------------
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_cisingle-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d))
47062306a36Sopenharmony_cisingle-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS))
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_ci$(single-subdir-goals): $(single-subdirs)
47362306a36Sopenharmony_ci	@:
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci# Descending
47662306a36Sopenharmony_ci# ---------------------------------------------------------------------------
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ciPHONY += $(subdir-ym)
47962306a36Sopenharmony_ci$(subdir-ym):
48062306a36Sopenharmony_ci	$(Q)$(MAKE) $(build)=$@ \
48162306a36Sopenharmony_ci	need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
48262306a36Sopenharmony_ci	need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \
48362306a36Sopenharmony_ci	$(filter $@/%, $(single-subdir-goals))
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci# Add FORCE to the prequisites of a target to force it to be always rebuilt.
48662306a36Sopenharmony_ci# ---------------------------------------------------------------------------
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ciPHONY += FORCE
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ciFORCE:
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_citargets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS))
49362306a36Sopenharmony_citargets := $(filter-out $(PHONY), $(targets))
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci# Read all saved command lines and dependencies for the $(targets) we
49662306a36Sopenharmony_ci# may be building above, using $(if_changed{,_dep}). As an
49762306a36Sopenharmony_ci# optimization, we don't need to read them if the target does not
49862306a36Sopenharmony_ci# exist, we will rebuild anyway in that case.
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ciexisting-targets := $(wildcard $(sort $(targets)))
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ci-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci# Create directories for object files if they do not exist
50562306a36Sopenharmony_ciobj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
50662306a36Sopenharmony_ci# If targets exist, their directories apparently exist. Skip mkdir.
50762306a36Sopenharmony_ciexisting-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
50862306a36Sopenharmony_ciobj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
50962306a36Sopenharmony_ciifneq ($(obj-dirs),)
51062306a36Sopenharmony_ci$(shell mkdir -p $(obj-dirs))
51162306a36Sopenharmony_ciendif
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci.PHONY: $(PHONY)
514