162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci# Backward compatibility
362306a36Sopenharmony_ciasflags-y  += $(EXTRA_AFLAGS)
462306a36Sopenharmony_ciccflags-y  += $(EXTRA_CFLAGS)
562306a36Sopenharmony_cicppflags-y += $(EXTRA_CPPFLAGS)
662306a36Sopenharmony_cildflags-y  += $(EXTRA_LDFLAGS)
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci# flags that take effect in current and sub directories
962306a36Sopenharmony_ciKBUILD_AFLAGS += $(subdir-asflags-y)
1062306a36Sopenharmony_ciKBUILD_CFLAGS += $(subdir-ccflags-y)
1162306a36Sopenharmony_ciKBUILD_RUSTFLAGS += $(subdir-rustflags-y)
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci# Figure out what we need to build from the various variables
1462306a36Sopenharmony_ci# ===========================================================================
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci# When an object is listed to be built compiled-in and modular,
1762306a36Sopenharmony_ci# only build the compiled-in version
1862306a36Sopenharmony_ciobj-m := $(filter-out $(obj-y),$(obj-m))
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci# Libraries are always collected in one lib file.
2162306a36Sopenharmony_ci# Filter out objects already built-in
2262306a36Sopenharmony_cilib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci# Subdirectories we need to descend into
2562306a36Sopenharmony_cisubdir-ym := $(sort $(subdir-y) $(subdir-m) \
2662306a36Sopenharmony_ci			$(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci# Handle objects in subdirs:
2962306a36Sopenharmony_ci# - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
3062306a36Sopenharmony_ci#   foo/modules.order
3162306a36Sopenharmony_ci# - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
3262306a36Sopenharmony_ci#
3362306a36Sopenharmony_ci# Generate modules.order to determine modorder. Unfortunately, we don't have
3462306a36Sopenharmony_ci# information about ordering between -y and -m subdirs. Just put -y's first.
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciifdef need-modorder
3762306a36Sopenharmony_ciobj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
3862306a36Sopenharmony_cielse
3962306a36Sopenharmony_ciobj-m := $(filter-out %/, $(obj-m))
4062306a36Sopenharmony_ciendif
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciifdef need-builtin
4362306a36Sopenharmony_ciobj-y		:= $(patsubst %/, %/built-in.a, $(obj-y))
4462306a36Sopenharmony_cielse
4562306a36Sopenharmony_ciobj-y		:= $(filter-out %/, $(obj-y))
4662306a36Sopenharmony_ciendif
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci# Expand $(foo-objs) $(foo-y) etc. by replacing their individuals
4962306a36Sopenharmony_cisuffix-search = $(strip $(foreach s, $3, $($(1:%$(strip $2)=%$s))))
5062306a36Sopenharmony_ci# List composite targets that are constructed by combining other targets
5162306a36Sopenharmony_cimulti-search = $(sort $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $m)))
5262306a36Sopenharmony_ci# List primitive targets that are compiled from source files
5362306a36Sopenharmony_cireal-search = $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $(call suffix-search, $m, $2, $3), $m))
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
5662306a36Sopenharmony_cimulti-obj-y := $(call multi-search, $(obj-y), .o, -objs -y)
5762306a36Sopenharmony_cimulti-obj-m := $(call multi-search, $(obj-m), .o, -objs -y -m)
5862306a36Sopenharmony_cimulti-obj-ym := $(multi-obj-y) $(multi-obj-m)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci# Replace multi-part objects by their individual parts,
6162306a36Sopenharmony_ci# including built-in.a from subdirectories
6262306a36Sopenharmony_cireal-obj-y := $(call real-search, $(obj-y), .o, -objs -y)
6362306a36Sopenharmony_cireal-obj-m := $(call real-search, $(obj-m), .o, -objs -y -m)
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cialways-y += $(always-m)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci# hostprogs-always-y += foo
6862306a36Sopenharmony_ci# ... is a shorthand for
6962306a36Sopenharmony_ci# hostprogs += foo
7062306a36Sopenharmony_ci# always-y  += foo
7162306a36Sopenharmony_cihostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
7262306a36Sopenharmony_cialways-y += $(hostprogs-always-y) $(hostprogs-always-m)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci# userprogs-always-y is likewise.
7562306a36Sopenharmony_ciuserprogs += $(userprogs-always-y) $(userprogs-always-m)
7662306a36Sopenharmony_cialways-y += $(userprogs-always-y) $(userprogs-always-m)
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci# DTB
7962306a36Sopenharmony_ci# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
8062306a36Sopenharmony_cidtb-$(CONFIG_OF_ALL_DTBS)       += $(dtb-)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci# Composite DTB (i.e. DTB constructed by overlay)
8362306a36Sopenharmony_cimulti-dtb-y := $(call multi-search, $(dtb-y), .dtb, -dtbs)
8462306a36Sopenharmony_ci# Primitive DTB compiled from *.dts
8562306a36Sopenharmony_cireal-dtb-y := $(call real-search, $(dtb-y), .dtb, -dtbs)
8662306a36Sopenharmony_ci# Base DTB that overlay is applied onto (each first word of $(*-dtbs) expansion)
8762306a36Sopenharmony_cibase-dtb-y := $(foreach m, $(multi-dtb-y), $(firstword $(call suffix-search, $m, .dtb, -dtbs)))
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cialways-y			+= $(dtb-y)
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci# Add subdir path
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ciifneq ($(obj),.)
9462306a36Sopenharmony_ciextra-y		:= $(addprefix $(obj)/,$(extra-y))
9562306a36Sopenharmony_cialways-y	:= $(addprefix $(obj)/,$(always-y))
9662306a36Sopenharmony_citargets		:= $(addprefix $(obj)/,$(targets))
9762306a36Sopenharmony_ciobj-m		:= $(addprefix $(obj)/,$(obj-m))
9862306a36Sopenharmony_cilib-y		:= $(addprefix $(obj)/,$(lib-y))
9962306a36Sopenharmony_cireal-obj-y	:= $(addprefix $(obj)/,$(real-obj-y))
10062306a36Sopenharmony_cireal-obj-m	:= $(addprefix $(obj)/,$(real-obj-m))
10162306a36Sopenharmony_cimulti-obj-m	:= $(addprefix $(obj)/, $(multi-obj-m))
10262306a36Sopenharmony_cimulti-dtb-y	:= $(addprefix $(obj)/, $(multi-dtb-y))
10362306a36Sopenharmony_cireal-dtb-y	:= $(addprefix $(obj)/, $(real-dtb-y))
10462306a36Sopenharmony_cisubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
10562306a36Sopenharmony_ciendif
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci# Finds the multi-part object the current object will be linked into.
10862306a36Sopenharmony_ci# If the object belongs to two or more multi-part objects, list them all.
10962306a36Sopenharmony_cimodname-multi = $(sort $(foreach m,$(multi-obj-ym),\
11062306a36Sopenharmony_ci		$(if $(filter $*.o, $(call suffix-search, $m, .o, -objs -y -m)),$(m:.o=))))
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci__modname = $(or $(modname-multi),$(basetarget))
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cimodname = $(subst $(space),:,$(__modname))
11562306a36Sopenharmony_cimodfile = $(addprefix $(obj)/,$(__modname))
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci# target with $(obj)/ and its suffix stripped
11862306a36Sopenharmony_citarget-stem = $(basename $(patsubst $(obj)/%,%,$@))
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci# These flags are needed for modversions and compiling, so we define them here
12162306a36Sopenharmony_ci# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
12262306a36Sopenharmony_ci# end up in (or would, if it gets compiled in)
12362306a36Sopenharmony_ciname-fix-token = $(subst $(comma),_,$(subst -,_,$1))
12462306a36Sopenharmony_ciname-fix = $(call stringify,$(call name-fix-token,$1))
12562306a36Sopenharmony_cibasename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
12662306a36Sopenharmony_cimodname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
12762306a36Sopenharmony_ci		 -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
12862306a36Sopenharmony_cimodfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
13162306a36Sopenharmony_ci                     $(filter-out $(ccflags-remove-y), \
13262306a36Sopenharmony_ci                         $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
13362306a36Sopenharmony_ci                     $(CFLAGS_$(target-stem).o))
13462306a36Sopenharmony_ci_rust_flags    = $(filter-out $(RUSTFLAGS_REMOVE_$(target-stem).o), \
13562306a36Sopenharmony_ci                     $(filter-out $(rustflags-remove-y), \
13662306a36Sopenharmony_ci                         $(KBUILD_RUSTFLAGS) $(rustflags-y)) \
13762306a36Sopenharmony_ci                     $(RUSTFLAGS_$(target-stem).o))
13862306a36Sopenharmony_ci_a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
13962306a36Sopenharmony_ci                     $(filter-out $(asflags-remove-y), \
14062306a36Sopenharmony_ci                         $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
14162306a36Sopenharmony_ci                     $(AFLAGS_$(target-stem).o))
14262306a36Sopenharmony_ci_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#
14562306a36Sopenharmony_ci# Enable gcov profiling flags for a file, directory or for all files depending
14662306a36Sopenharmony_ci# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
14762306a36Sopenharmony_ci# (in this order)
14862306a36Sopenharmony_ci#
14962306a36Sopenharmony_ciifeq ($(CONFIG_GCOV_KERNEL),y)
15062306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
15162306a36Sopenharmony_ci		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
15262306a36Sopenharmony_ci		$(CFLAGS_GCOV))
15362306a36Sopenharmony_ciendif
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci#
15662306a36Sopenharmony_ci# Enable address sanitizer flags for kernel except some files or directories
15762306a36Sopenharmony_ci# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
15862306a36Sopenharmony_ci#
15962306a36Sopenharmony_ciifeq ($(CONFIG_KASAN),y)
16062306a36Sopenharmony_ciifneq ($(CONFIG_KASAN_HW_TAGS),y)
16162306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
16262306a36Sopenharmony_ci		$(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
16362306a36Sopenharmony_ci		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
16462306a36Sopenharmony_ciendif
16562306a36Sopenharmony_ciendif
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciifeq ($(CONFIG_KMSAN),y)
16862306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
16962306a36Sopenharmony_ci		$(KMSAN_SANITIZE_$(basetarget).o)$(KMSAN_SANITIZE)y), \
17062306a36Sopenharmony_ci		$(CFLAGS_KMSAN))
17162306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
17262306a36Sopenharmony_ci		$(KMSAN_ENABLE_CHECKS_$(basetarget).o)$(KMSAN_ENABLE_CHECKS)y), \
17362306a36Sopenharmony_ci		, -mllvm -msan-disable-checks=1)
17462306a36Sopenharmony_ciendif
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ciifeq ($(CONFIG_UBSAN),y)
17762306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
17862306a36Sopenharmony_ci		$(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
17962306a36Sopenharmony_ci		$(CFLAGS_UBSAN))
18062306a36Sopenharmony_ciendif
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciifeq ($(CONFIG_KCOV),y)
18362306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
18462306a36Sopenharmony_ci	$(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
18562306a36Sopenharmony_ci	$(CFLAGS_KCOV))
18662306a36Sopenharmony_ciendif
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci#
18962306a36Sopenharmony_ci# Enable KCSAN flags except some files or directories we don't want to check
19062306a36Sopenharmony_ci# (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
19162306a36Sopenharmony_ci#
19262306a36Sopenharmony_ciifeq ($(CONFIG_KCSAN),y)
19362306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
19462306a36Sopenharmony_ci	$(KCSAN_SANITIZE_$(basetarget).o)$(KCSAN_SANITIZE)y), \
19562306a36Sopenharmony_ci	$(CFLAGS_KCSAN))
19662306a36Sopenharmony_ci# Some uninstrumented files provide implied barriers required to avoid false
19762306a36Sopenharmony_ci# positives: set KCSAN_INSTRUMENT_BARRIERS for barrier instrumentation only.
19862306a36Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
19962306a36Sopenharmony_ci	$(KCSAN_INSTRUMENT_BARRIERS_$(basetarget).o)$(KCSAN_INSTRUMENT_BARRIERS)n), \
20062306a36Sopenharmony_ci	-D__KCSAN_INSTRUMENT_BARRIERS__)
20162306a36Sopenharmony_ciendif
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci# $(srctree)/$(src) for including checkin headers from generated source files
20462306a36Sopenharmony_ci# $(objtree)/$(obj) for including generated headers from checkin source files
20562306a36Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
20662306a36Sopenharmony_ciifdef building_out_of_srctree
20762306a36Sopenharmony_ci_c_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
20862306a36Sopenharmony_ci_a_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
20962306a36Sopenharmony_ci_cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
21062306a36Sopenharmony_ciendif
21162306a36Sopenharmony_ciendif
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_cipart-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
21462306a36Sopenharmony_ciquiet_modtag = $(if $(part-of-module),[M],   )
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_cimodkern_cflags =                                          \
21762306a36Sopenharmony_ci	$(if $(part-of-module),                           \
21862306a36Sopenharmony_ci		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
21962306a36Sopenharmony_ci		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cimodkern_rustflags =                                              \
22262306a36Sopenharmony_ci	$(if $(part-of-module),                                   \
22362306a36Sopenharmony_ci		$(KBUILD_RUSTFLAGS_MODULE) $(RUSTFLAGS_MODULE), \
22462306a36Sopenharmony_ci		$(KBUILD_RUSTFLAGS_KERNEL) $(RUSTFLAGS_KERNEL))
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_cimodkern_aflags = $(if $(part-of-module),				\
22762306a36Sopenharmony_ci			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
22862306a36Sopenharmony_ci			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL))
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cic_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
23162306a36Sopenharmony_ci		 -include $(srctree)/include/linux/compiler_types.h       \
23262306a36Sopenharmony_ci		 $(_c_flags) $(modkern_cflags)                           \
23362306a36Sopenharmony_ci		 $(basename_flags) $(modname_flags)
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cirust_flags     = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_cia_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
23862306a36Sopenharmony_ci		 $(_a_flags) $(modkern_aflags)
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cicpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
24162306a36Sopenharmony_ci		 $(_cpp_flags)
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cild_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ciDTC_INCLUDE    := $(srctree)/scripts/dtc/include-prefixes
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_cidtc_cpp_flags  = -Wp,-MMD,$(depfile).pre.tmp -nostdinc                    \
24862306a36Sopenharmony_ci		 $(addprefix -I,$(DTC_INCLUDE))                          \
24962306a36Sopenharmony_ci		 -undef -D__DTS__
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ciifdef CONFIG_OBJTOOL
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciobjtool := $(objtree)/tools/objtool/objtool
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ciobjtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK)		+= --hacks=jump_label
25662306a36Sopenharmony_ciobjtool-args-$(CONFIG_HAVE_NOINSTR_HACK)		+= --hacks=noinstr
25762306a36Sopenharmony_ciobjtool-args-$(CONFIG_CALL_DEPTH_TRACKING)		+= --hacks=skylake
25862306a36Sopenharmony_ciobjtool-args-$(CONFIG_X86_KERNEL_IBT)			+= --ibt
25962306a36Sopenharmony_ciobjtool-args-$(CONFIG_FINEIBT)				+= --cfi
26062306a36Sopenharmony_ciobjtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL)	+= --mcount
26162306a36Sopenharmony_ciifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
26262306a36Sopenharmony_ciobjtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT)		+= --mnop
26362306a36Sopenharmony_ciendif
26462306a36Sopenharmony_ciobjtool-args-$(CONFIG_UNWINDER_ORC)			+= --orc
26562306a36Sopenharmony_ciobjtool-args-$(CONFIG_RETPOLINE)			+= --retpoline
26662306a36Sopenharmony_ciobjtool-args-$(CONFIG_RETHUNK)				+= --rethunk
26762306a36Sopenharmony_ciobjtool-args-$(CONFIG_SLS)				+= --sls
26862306a36Sopenharmony_ciobjtool-args-$(CONFIG_STACK_VALIDATION)			+= --stackval
26962306a36Sopenharmony_ciobjtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE)		+= --static-call
27062306a36Sopenharmony_ciobjtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION)		+= --uaccess
27162306a36Sopenharmony_ciobjtool-args-$(CONFIG_GCOV_KERNEL)			+= --no-unreachable
27262306a36Sopenharmony_ciobjtool-args-$(CONFIG_PREFIX_SYMBOLS)			+= --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ciobjtool-args = $(objtool-args-y)					\
27562306a36Sopenharmony_ci	$(if $(delay-objtool), --link)					\
27662306a36Sopenharmony_ci	$(if $(part-of-module), --module)
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_cidelay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_cicmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
28162306a36Sopenharmony_cicmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ciendif # CONFIG_OBJTOOL
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci# Useful for describing the dependency of composite objects
28662306a36Sopenharmony_ci# Usage:
28762306a36Sopenharmony_ci#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
28862306a36Sopenharmony_cidefine multi_depend
28962306a36Sopenharmony_ci$(foreach m, $1, \
29062306a36Sopenharmony_ci	$(eval $m: \
29162306a36Sopenharmony_ci	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
29262306a36Sopenharmony_ciendef
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci# Copy a file
29562306a36Sopenharmony_ci# ===========================================================================
29662306a36Sopenharmony_ci# 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
29762306a36Sopenharmony_ci# the copy would be read-only as well, leading to an error when executing the
29862306a36Sopenharmony_ci# rule next time. Use 'cat' instead in order to generate a writable file.
29962306a36Sopenharmony_ciquiet_cmd_copy = COPY    $@
30062306a36Sopenharmony_ci      cmd_copy = cat $< > $@
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci$(obj)/%: $(src)/%_shipped
30362306a36Sopenharmony_ci	$(call cmd,copy)
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci# Commands useful for building a boot image
30662306a36Sopenharmony_ci# ===========================================================================
30762306a36Sopenharmony_ci#
30862306a36Sopenharmony_ci#	Use as following:
30962306a36Sopenharmony_ci#
31062306a36Sopenharmony_ci#	target: source(s) FORCE
31162306a36Sopenharmony_ci#		$(if_changed,ld/objcopy/gzip)
31262306a36Sopenharmony_ci#
31362306a36Sopenharmony_ci#	and add target to 'targets' so that we know we have to
31462306a36Sopenharmony_ci#	read in the saved command line
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci# Linking
31762306a36Sopenharmony_ci# ---------------------------------------------------------------------------
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ciquiet_cmd_ld = LD      $@
32062306a36Sopenharmony_ci      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci# Archive
32362306a36Sopenharmony_ci# ---------------------------------------------------------------------------
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ciquiet_cmd_ar = AR      $@
32662306a36Sopenharmony_ci      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ci# Objcopy
32962306a36Sopenharmony_ci# ---------------------------------------------------------------------------
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ciquiet_cmd_objcopy = OBJCOPY $@
33262306a36Sopenharmony_cicmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci# Gzip
33562306a36Sopenharmony_ci# ---------------------------------------------------------------------------
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ciquiet_cmd_gzip = GZIP    $@
33862306a36Sopenharmony_ci      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci# DTC
34162306a36Sopenharmony_ci# ---------------------------------------------------------------------------
34262306a36Sopenharmony_ciDTC ?= $(objtree)/scripts/dtc/dtc
34362306a36Sopenharmony_ciDTC_FLAGS += -Wno-interrupt_provider \
34462306a36Sopenharmony_ci	-Wno-unique_unit_address
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci# Disable noisy checks by default
34762306a36Sopenharmony_ciifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
34862306a36Sopenharmony_ciDTC_FLAGS += -Wno-unit_address_vs_reg \
34962306a36Sopenharmony_ci	-Wno-avoid_unnecessary_addr_size \
35062306a36Sopenharmony_ci	-Wno-alias_paths \
35162306a36Sopenharmony_ci	-Wno-graph_child_address \
35262306a36Sopenharmony_ci	-Wno-simple_bus_reg
35362306a36Sopenharmony_cielse
35462306a36Sopenharmony_ciDTC_FLAGS += \
35562306a36Sopenharmony_ci        -Wunique_unit_address_if_enabled
35662306a36Sopenharmony_ciendif
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ciifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),)
35962306a36Sopenharmony_ciDTC_FLAGS += -Wnode_name_chars_strict \
36062306a36Sopenharmony_ci	-Wproperty_name_chars_strict \
36162306a36Sopenharmony_ci	-Winterrupt_provider \
36262306a36Sopenharmony_ci	-Wunique_unit_address
36362306a36Sopenharmony_ciendif
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ciDTC_FLAGS += $(DTC_FLAGS_$(basetarget))
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci# Set -@ if the target is a base DTB that overlay is applied onto
36862306a36Sopenharmony_ciDTC_FLAGS += $(if $(filter $(patsubst $(obj)/%,%,$@), $(base-dtb-y)), -@)
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci# Generate an assembly file to wrap the output of the device tree compiler
37162306a36Sopenharmony_ciquiet_cmd_wrap_S_dtb = WRAP    $@
37262306a36Sopenharmony_ci      cmd_wrap_S_dtb = {								\
37362306a36Sopenharmony_ci		symbase=__$(patsubst .%,%,$(suffix $<))_$(subst -,_,$(notdir $*));	\
37462306a36Sopenharmony_ci		echo '\#include <asm-generic/vmlinux.lds.h>';				\
37562306a36Sopenharmony_ci		echo '.section .dtb.init.rodata,"a"';					\
37662306a36Sopenharmony_ci		echo '.balign STRUCT_ALIGNMENT';					\
37762306a36Sopenharmony_ci		echo ".global $${symbase}_begin";					\
37862306a36Sopenharmony_ci		echo "$${symbase}_begin:";						\
37962306a36Sopenharmony_ci		echo '.incbin "$<" ';							\
38062306a36Sopenharmony_ci		echo ".global $${symbase}_end";						\
38162306a36Sopenharmony_ci		echo "$${symbase}_end:";						\
38262306a36Sopenharmony_ci		echo '.balign STRUCT_ALIGNMENT';					\
38362306a36Sopenharmony_ci	} > $@
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci$(obj)/%.dtb.S: $(obj)/%.dtb FORCE
38662306a36Sopenharmony_ci	$(call if_changed,wrap_S_dtb)
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci$(obj)/%.dtbo.S: $(obj)/%.dtbo FORCE
38962306a36Sopenharmony_ci	$(call if_changed,wrap_S_dtb)
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ciquiet_cmd_dtc = DTC     $@
39262306a36Sopenharmony_cicmd_dtc = $(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
39362306a36Sopenharmony_ci	$(DTC) -o $@ -b 0 \
39462306a36Sopenharmony_ci		$(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \
39562306a36Sopenharmony_ci		-d $(depfile).dtc.tmp $(dtc-tmp) ; \
39662306a36Sopenharmony_ci	cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ciquiet_cmd_fdtoverlay = DTOVL   $@
39962306a36Sopenharmony_ci      cmd_fdtoverlay = $(objtree)/scripts/dtc/fdtoverlay -o $@ -i $(real-prereqs)
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci$(multi-dtb-y): FORCE
40262306a36Sopenharmony_ci	$(call if_changed,fdtoverlay)
40362306a36Sopenharmony_ci$(call multi_depend, $(multi-dtb-y), .dtb, -dtbs)
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ciifneq ($(CHECK_DTBS)$(CHECK_DT_BINDING),)
40662306a36Sopenharmony_ciDT_CHECKER ?= dt-validate
40762306a36Sopenharmony_ciDT_CHECKER_FLAGS ?= $(if $(DT_SCHEMA_FILES),-l $(DT_SCHEMA_FILES),-m)
40862306a36Sopenharmony_ciDT_BINDING_DIR := Documentation/devicetree/bindings
40962306a36Sopenharmony_ciDT_TMP_SCHEMA := $(objtree)/$(DT_BINDING_DIR)/processed-schema.json
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ciquiet_cmd_dtb =	DTC_CHK $@
41262306a36Sopenharmony_ci      cmd_dtb =	$(cmd_dtc) ; $(DT_CHECKER) $(DT_CHECKER_FLAGS) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@ || true
41362306a36Sopenharmony_cielse
41462306a36Sopenharmony_ciquiet_cmd_dtb = $(quiet_cmd_dtc)
41562306a36Sopenharmony_ci      cmd_dtb = $(cmd_dtc)
41662306a36Sopenharmony_ciendif
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci$(obj)/%.dtb: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
41962306a36Sopenharmony_ci	$(call if_changed_dep,dtb)
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci$(obj)/%.dtbo: $(src)/%.dtso $(DTC) FORCE
42262306a36Sopenharmony_ci	$(call if_changed_dep,dtc)
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_cidtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci# Bzip2
42762306a36Sopenharmony_ci# ---------------------------------------------------------------------------
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci# Bzip2 and LZMA do not include size in file... so we have to fake that;
43062306a36Sopenharmony_ci# append the size as a 32-bit littleendian number as gzip does.
43162306a36Sopenharmony_cisize_append = printf $(shell						\
43262306a36Sopenharmony_cidec_size=0;								\
43362306a36Sopenharmony_cifor F in $(real-prereqs); do					\
43462306a36Sopenharmony_ci	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
43562306a36Sopenharmony_ci	dec_size=$$(expr $$dec_size + $$fsize);				\
43662306a36Sopenharmony_cidone;									\
43762306a36Sopenharmony_ciprintf "%08x\n" $$dec_size |						\
43862306a36Sopenharmony_ci	sed 's/\(..\)/\1 /g' | {					\
43962306a36Sopenharmony_ci		read ch0 ch1 ch2 ch3;					\
44062306a36Sopenharmony_ci		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
44162306a36Sopenharmony_ci			printf '%s%03o' '\\' $$((0x$$ch)); 		\
44262306a36Sopenharmony_ci		done;							\
44362306a36Sopenharmony_ci	}								\
44462306a36Sopenharmony_ci)
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ciquiet_cmd_file_size = GEN     $@
44762306a36Sopenharmony_ci      cmd_file_size = $(size_append) > $@
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ciquiet_cmd_bzip2 = BZIP2   $@
45062306a36Sopenharmony_ci      cmd_bzip2 = cat $(real-prereqs) | $(KBZIP2) -9 > $@
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ciquiet_cmd_bzip2_with_size = BZIP2   $@
45362306a36Sopenharmony_ci      cmd_bzip2_with_size = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci# Lzma
45662306a36Sopenharmony_ci# ---------------------------------------------------------------------------
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ciquiet_cmd_lzma = LZMA    $@
45962306a36Sopenharmony_ci      cmd_lzma = cat $(real-prereqs) | $(LZMA) -9 > $@
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ciquiet_cmd_lzma_with_size = LZMA    $@
46262306a36Sopenharmony_ci      cmd_lzma_with_size = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ciquiet_cmd_lzo = LZO     $@
46562306a36Sopenharmony_ci      cmd_lzo = cat $(real-prereqs) | $(KLZOP) -9 > $@
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ciquiet_cmd_lzo_with_size = LZO     $@
46862306a36Sopenharmony_ci      cmd_lzo_with_size = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ciquiet_cmd_lz4 = LZ4     $@
47162306a36Sopenharmony_ci      cmd_lz4 = cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout > $@
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ciquiet_cmd_lz4_with_size = LZ4     $@
47462306a36Sopenharmony_ci      cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \
47562306a36Sopenharmony_ci                  $(size_append); } > $@
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci# U-Boot mkimage
47862306a36Sopenharmony_ci# ---------------------------------------------------------------------------
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ciMKIMAGE := $(srctree)/scripts/mkuboot.sh
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
48362306a36Sopenharmony_ci# the number of overrides in arch makefiles
48462306a36Sopenharmony_ciUIMAGE_ARCH ?= $(SRCARCH)
48562306a36Sopenharmony_ciUIMAGE_COMPRESSION ?= $(or $(2),none)
48662306a36Sopenharmony_ciUIMAGE_OPTS-y ?=
48762306a36Sopenharmony_ciUIMAGE_TYPE ?= kernel
48862306a36Sopenharmony_ciUIMAGE_LOADADDR ?= arch_must_set_this
48962306a36Sopenharmony_ciUIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
49062306a36Sopenharmony_ciUIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ciquiet_cmd_uimage = UIMAGE  $@
49362306a36Sopenharmony_ci      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
49462306a36Sopenharmony_ci			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
49562306a36Sopenharmony_ci			-T $(UIMAGE_TYPE) \
49662306a36Sopenharmony_ci			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
49762306a36Sopenharmony_ci			-n $(UIMAGE_NAME) -d $< $@
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci# XZ
50062306a36Sopenharmony_ci# ---------------------------------------------------------------------------
50162306a36Sopenharmony_ci# Use xzkern to compress the kernel image and xzmisc to compress other things.
50262306a36Sopenharmony_ci#
50362306a36Sopenharmony_ci# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
50462306a36Sopenharmony_ci# of the kernel decompressor. A BCJ filter is used if it is available for
50562306a36Sopenharmony_ci# the target architecture. xzkern also appends uncompressed size of the data
50662306a36Sopenharmony_ci# using size_append. The .xz format has the size information available at
50762306a36Sopenharmony_ci# the end of the file too, but it's in more complex format and it's good to
50862306a36Sopenharmony_ci# avoid changing the part of the boot code that reads the uncompressed size.
50962306a36Sopenharmony_ci# Note that the bytes added by size_append will make the xz tool think that
51062306a36Sopenharmony_ci# the file is corrupt. This is expected.
51162306a36Sopenharmony_ci#
51262306a36Sopenharmony_ci# xzmisc doesn't use size_append, so it can be used to create normal .xz
51362306a36Sopenharmony_ci# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
51462306a36Sopenharmony_ci# big dictionary would increase the memory usage too much in the multi-call
51562306a36Sopenharmony_ci# decompression mode. A BCJ filter isn't used either.
51662306a36Sopenharmony_ciquiet_cmd_xzkern = XZKERN  $@
51762306a36Sopenharmony_ci      cmd_xzkern = cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh > $@
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ciquiet_cmd_xzkern_with_size = XZKERN  $@
52062306a36Sopenharmony_ci      cmd_xzkern_with_size = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
52162306a36Sopenharmony_ci                     $(size_append); } > $@
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ciquiet_cmd_xzmisc = XZMISC  $@
52462306a36Sopenharmony_ci      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci# ZSTD
52762306a36Sopenharmony_ci# ---------------------------------------------------------------------------
52862306a36Sopenharmony_ci# Appends the uncompressed size of the data using size_append. The .zst
52962306a36Sopenharmony_ci# format has the size information available at the beginning of the file too,
53062306a36Sopenharmony_ci# but it's in a more complex format and it's good to avoid changing the part
53162306a36Sopenharmony_ci# of the boot code that reads the uncompressed size.
53262306a36Sopenharmony_ci#
53362306a36Sopenharmony_ci# Note that the bytes added by size_append will make the zstd tool think that
53462306a36Sopenharmony_ci# the file is corrupt. This is expected.
53562306a36Sopenharmony_ci#
53662306a36Sopenharmony_ci# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
53762306a36Sopenharmony_ci# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
53862306a36Sopenharmony_ci# single pass, so zstd doesn't need to allocate a window buffer. When streaming
53962306a36Sopenharmony_ci# decompression is used, like initramfs decompression, zstd22 should likely not
54062306a36Sopenharmony_ci# be used because it would require zstd to allocate a 128 MB buffer.
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ciquiet_cmd_zstd = ZSTD    $@
54362306a36Sopenharmony_ci      cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ciquiet_cmd_zstd22 = ZSTD22  $@
54662306a36Sopenharmony_ci      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ciquiet_cmd_zstd22_with_size = ZSTD22  $@
54962306a36Sopenharmony_ci      cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
55062306a36Sopenharmony_ci
55162306a36Sopenharmony_ci# ASM offsets
55262306a36Sopenharmony_ci# ---------------------------------------------------------------------------
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci# Default sed regexp - multiline due to syntax constraints
55562306a36Sopenharmony_ci#
55662306a36Sopenharmony_ci# Use [:space:] because LLVM's integrated assembler inserts <tab> around
55762306a36Sopenharmony_ci# the .ascii directive whereas GCC keeps the <space> as-is.
55862306a36Sopenharmony_cidefine sed-offsets
55962306a36Sopenharmony_ci	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
56062306a36Sopenharmony_ci	/^->/{s:->#\(.*\):/* \1 */:; \
56162306a36Sopenharmony_ci	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
56262306a36Sopenharmony_ci	s:->::; p;}'
56362306a36Sopenharmony_ciendef
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci# Use filechk to avoid rebuilds when a header changes, but the resulting file
56662306a36Sopenharmony_ci# does not
56762306a36Sopenharmony_cidefine filechk_offsets
56862306a36Sopenharmony_ci	 echo "#ifndef $2"; \
56962306a36Sopenharmony_ci	 echo "#define $2"; \
57062306a36Sopenharmony_ci	 echo "/*"; \
57162306a36Sopenharmony_ci	 echo " * DO NOT MODIFY."; \
57262306a36Sopenharmony_ci	 echo " *"; \
57362306a36Sopenharmony_ci	 echo " * This file was generated by Kbuild"; \
57462306a36Sopenharmony_ci	 echo " */"; \
57562306a36Sopenharmony_ci	 echo ""; \
57662306a36Sopenharmony_ci	 sed -ne $(sed-offsets) < $<; \
57762306a36Sopenharmony_ci	 echo ""; \
57862306a36Sopenharmony_ci	 echo "#endif"
57962306a36Sopenharmony_ciendef
580