18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci# Backward compatibility
38c2ecf20Sopenharmony_ciasflags-y  += $(EXTRA_AFLAGS)
48c2ecf20Sopenharmony_ciccflags-y  += $(EXTRA_CFLAGS)
58c2ecf20Sopenharmony_cicppflags-y += $(EXTRA_CPPFLAGS)
68c2ecf20Sopenharmony_cildflags-y  += $(EXTRA_LDFLAGS)
78c2ecf20Sopenharmony_ciifneq ($(always),)
88c2ecf20Sopenharmony_ci$(warning 'always' is deprecated. Please use 'always-y' instead)
98c2ecf20Sopenharmony_cialways-y   += $(always)
108c2ecf20Sopenharmony_ciendif
118c2ecf20Sopenharmony_ciifneq ($(hostprogs-y),)
128c2ecf20Sopenharmony_ci$(warning 'hostprogs-y' is deprecated. Please use 'hostprogs' instead)
138c2ecf20Sopenharmony_cihostprogs  += $(hostprogs-y)
148c2ecf20Sopenharmony_ciendif
158c2ecf20Sopenharmony_ciifneq ($(hostprogs-m),)
168c2ecf20Sopenharmony_ci$(warning 'hostprogs-m' is deprecated. Please use 'hostprogs' instead)
178c2ecf20Sopenharmony_cihostprogs  += $(hostprogs-m)
188c2ecf20Sopenharmony_ciendif
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci# flags that take effect in current and sub directories
218c2ecf20Sopenharmony_ciKBUILD_AFLAGS += $(subdir-asflags-y)
228c2ecf20Sopenharmony_ciKBUILD_CFLAGS += $(subdir-ccflags-y)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci# Figure out what we need to build from the various variables
258c2ecf20Sopenharmony_ci# ===========================================================================
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci# When an object is listed to be built compiled-in and modular,
288c2ecf20Sopenharmony_ci# only build the compiled-in version
298c2ecf20Sopenharmony_ciobj-m := $(filter-out $(obj-y),$(obj-m))
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci# Libraries are always collected in one lib file.
328c2ecf20Sopenharmony_ci# Filter out objects already built-in
338c2ecf20Sopenharmony_cilib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci# Subdirectories we need to descend into
368c2ecf20Sopenharmony_cisubdir-ym := $(sort $(subdir-y) $(subdir-m) \
378c2ecf20Sopenharmony_ci			$(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci# Handle objects in subdirs:
408c2ecf20Sopenharmony_ci# - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
418c2ecf20Sopenharmony_ci#   foo/modules.order
428c2ecf20Sopenharmony_ci# - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
438c2ecf20Sopenharmony_ci#
448c2ecf20Sopenharmony_ci# Generate modules.order to determine modorder. Unfortunately, we don't have
458c2ecf20Sopenharmony_ci# information about ordering between -y and -m subdirs. Just put -y's first.
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciifdef need-modorder
488c2ecf20Sopenharmony_ciobj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
498c2ecf20Sopenharmony_cielse
508c2ecf20Sopenharmony_ciobj-m := $(filter-out %/, $(obj-m))
518c2ecf20Sopenharmony_ciendif
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciifdef need-builtin
548c2ecf20Sopenharmony_ciobj-y		:= $(patsubst %/, %/built-in.a, $(obj-y))
558c2ecf20Sopenharmony_cielse
568c2ecf20Sopenharmony_ciobj-y		:= $(filter-out %/, $(obj-y))
578c2ecf20Sopenharmony_ciendif
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
608c2ecf20Sopenharmony_cimulti-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))), $(m))))
618c2ecf20Sopenharmony_cimulti-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))), $(m))))
628c2ecf20Sopenharmony_cimulti-used   := $(multi-used-y) $(multi-used-m)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci# Replace multi-part objects by their individual parts,
658c2ecf20Sopenharmony_ci# including built-in.a from subdirectories
668c2ecf20Sopenharmony_cireal-obj-y := $(foreach m, $(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
678c2ecf20Sopenharmony_cireal-obj-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)),$(m)))
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cialways-y += $(always-m)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci# hostprogs-always-y += foo
728c2ecf20Sopenharmony_ci# ... is a shorthand for
738c2ecf20Sopenharmony_ci# hostprogs += foo
748c2ecf20Sopenharmony_ci# always-y  += foo
758c2ecf20Sopenharmony_cihostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
768c2ecf20Sopenharmony_cialways-y += $(hostprogs-always-y) $(hostprogs-always-m)
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci# userprogs-always-y is likewise.
798c2ecf20Sopenharmony_ciuserprogs += $(userprogs-always-y) $(userprogs-always-m)
808c2ecf20Sopenharmony_cialways-y += $(userprogs-always-y) $(userprogs-always-m)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci# DTB
838c2ecf20Sopenharmony_ci# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
848c2ecf20Sopenharmony_ciextra-y				+= $(dtb-y)
858c2ecf20Sopenharmony_ciextra-$(CONFIG_OF_ALL_DTBS)	+= $(dtb-)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciifneq ($(CHECK_DTBS),)
888c2ecf20Sopenharmony_ciextra-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y))
898c2ecf20Sopenharmony_ciextra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
908c2ecf20Sopenharmony_ciendif
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci# Add subdir path
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciextra-y		:= $(addprefix $(obj)/,$(extra-y))
958c2ecf20Sopenharmony_cialways-y	:= $(addprefix $(obj)/,$(always-y))
968c2ecf20Sopenharmony_citargets		:= $(addprefix $(obj)/,$(targets))
978c2ecf20Sopenharmony_ciobj-m		:= $(addprefix $(obj)/,$(obj-m))
988c2ecf20Sopenharmony_cilib-y		:= $(addprefix $(obj)/,$(lib-y))
998c2ecf20Sopenharmony_cireal-obj-y	:= $(addprefix $(obj)/,$(real-obj-y))
1008c2ecf20Sopenharmony_cireal-obj-m	:= $(addprefix $(obj)/,$(real-obj-m))
1018c2ecf20Sopenharmony_cimulti-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
1028c2ecf20Sopenharmony_cisubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci# Finds the multi-part object the current object will be linked into.
1058c2ecf20Sopenharmony_ci# If the object belongs to two or more multi-part objects, list them all.
1068c2ecf20Sopenharmony_cimodname-multi = $(sort $(foreach m,$(multi-used),\
1078c2ecf20Sopenharmony_ci		$(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=))))
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci__modname = $(if $(modname-multi),$(modname-multi),$(basetarget))
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cimodname = $(subst $(space),:,$(__modname))
1128c2ecf20Sopenharmony_cimodfile = $(addprefix $(obj)/,$(__modname))
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci# target with $(obj)/ and its suffix stripped
1158c2ecf20Sopenharmony_citarget-stem = $(basename $(patsubst $(obj)/%,%,$@))
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci# These flags are needed for modversions and compiling, so we define them here
1188c2ecf20Sopenharmony_ci# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
1198c2ecf20Sopenharmony_ci# end up in (or would, if it gets compiled in)
1208c2ecf20Sopenharmony_ciname-fix-token = $(subst $(comma),_,$(subst -,_,$1))
1218c2ecf20Sopenharmony_ciname-fix = $(call stringify,$(call name-fix-token,$1))
1228c2ecf20Sopenharmony_cibasename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
1238c2ecf20Sopenharmony_cimodname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
1248c2ecf20Sopenharmony_ci		 -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
1258c2ecf20Sopenharmony_cimodfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
1288c2ecf20Sopenharmony_ci                     $(filter-out $(ccflags-remove-y), \
1298c2ecf20Sopenharmony_ci                         $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
1308c2ecf20Sopenharmony_ci                     $(CFLAGS_$(target-stem).o))
1318c2ecf20Sopenharmony_ci_a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
1328c2ecf20Sopenharmony_ci                     $(filter-out $(asflags-remove-y), \
1338c2ecf20Sopenharmony_ci                         $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
1348c2ecf20Sopenharmony_ci                     $(AFLAGS_$(target-stem).o))
1358c2ecf20Sopenharmony_ci_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#
1388c2ecf20Sopenharmony_ci# Enable gcov profiling flags for a file, directory or for all files depending
1398c2ecf20Sopenharmony_ci# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
1408c2ecf20Sopenharmony_ci# (in this order)
1418c2ecf20Sopenharmony_ci#
1428c2ecf20Sopenharmony_ciifeq ($(CONFIG_GCOV_KERNEL),y)
1438c2ecf20Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
1448c2ecf20Sopenharmony_ci		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
1458c2ecf20Sopenharmony_ci		$(CFLAGS_GCOV))
1468c2ecf20Sopenharmony_ciendif
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#
1498c2ecf20Sopenharmony_ci# Enable address sanitizer flags for kernel except some files or directories
1508c2ecf20Sopenharmony_ci# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
1518c2ecf20Sopenharmony_ci#
1528c2ecf20Sopenharmony_ciifeq ($(CONFIG_KASAN),y)
1538c2ecf20Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
1548c2ecf20Sopenharmony_ci		$(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
1558c2ecf20Sopenharmony_ci		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
1568c2ecf20Sopenharmony_ciendif
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciifeq ($(CONFIG_UBSAN),y)
1598c2ecf20Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
1608c2ecf20Sopenharmony_ci		$(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
1618c2ecf20Sopenharmony_ci		$(CFLAGS_UBSAN))
1628c2ecf20Sopenharmony_ciendif
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciifeq ($(CONFIG_KCOV),y)
1658c2ecf20Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
1668c2ecf20Sopenharmony_ci	$(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
1678c2ecf20Sopenharmony_ci	$(CFLAGS_KCOV))
1688c2ecf20Sopenharmony_ciendif
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#
1718c2ecf20Sopenharmony_ci# Enable KCSAN flags except some files or directories we don't want to check
1728c2ecf20Sopenharmony_ci# (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
1738c2ecf20Sopenharmony_ci#
1748c2ecf20Sopenharmony_ciifeq ($(CONFIG_KCSAN),y)
1758c2ecf20Sopenharmony_ci_c_flags += $(if $(patsubst n%,, \
1768c2ecf20Sopenharmony_ci	$(KCSAN_SANITIZE_$(basetarget).o)$(KCSAN_SANITIZE)y), \
1778c2ecf20Sopenharmony_ci	$(CFLAGS_KCSAN))
1788c2ecf20Sopenharmony_ciendif
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci# $(srctree)/$(src) for including checkin headers from generated source files
1818c2ecf20Sopenharmony_ci# $(objtree)/$(obj) for including generated headers from checkin source files
1828c2ecf20Sopenharmony_ciifeq ($(KBUILD_EXTMOD),)
1838c2ecf20Sopenharmony_ciifdef building_out_of_srctree
1848c2ecf20Sopenharmony_ci_c_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
1858c2ecf20Sopenharmony_ci_a_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
1868c2ecf20Sopenharmony_ci_cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
1878c2ecf20Sopenharmony_ciendif
1888c2ecf20Sopenharmony_ciendif
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cipart-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
1918c2ecf20Sopenharmony_ciquiet_modtag = $(if $(part-of-module),[M],   )
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cimodkern_cflags =                                          \
1948c2ecf20Sopenharmony_ci	$(if $(part-of-module),                           \
1958c2ecf20Sopenharmony_ci		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
1968c2ecf20Sopenharmony_ci		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cimodkern_aflags = $(if $(part-of-module),				\
1998c2ecf20Sopenharmony_ci			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
2008c2ecf20Sopenharmony_ci			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL))
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cic_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
2038c2ecf20Sopenharmony_ci		 -include $(srctree)/include/linux/compiler_types.h       \
2048c2ecf20Sopenharmony_ci		 $(_c_flags) $(modkern_cflags)                           \
2058c2ecf20Sopenharmony_ci		 $(basename_flags) $(modname_flags)
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cia_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
2088c2ecf20Sopenharmony_ci		 $(_a_flags) $(modkern_aflags)
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cicpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
2118c2ecf20Sopenharmony_ci		 $(_cpp_flags)
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cild_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ciDTC_INCLUDE    := $(srctree)/scripts/dtc/include-prefixes
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cidtc_cpp_flags  = -Wp,-MMD,$(depfile).pre.tmp -nostdinc                    \
2188c2ecf20Sopenharmony_ci		 $(addprefix -I,$(DTC_INCLUDE))                          \
2198c2ecf20Sopenharmony_ci		 -undef -D__DTS__
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci# Useful for describing the dependency of composite objects
2228c2ecf20Sopenharmony_ci# Usage:
2238c2ecf20Sopenharmony_ci#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
2248c2ecf20Sopenharmony_cidefine multi_depend
2258c2ecf20Sopenharmony_ci$(foreach m, $(notdir $1), \
2268c2ecf20Sopenharmony_ci	$(eval $(obj)/$m: \
2278c2ecf20Sopenharmony_ci	$(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
2288c2ecf20Sopenharmony_ciendef
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ciquiet_cmd_copy = COPY    $@
2318c2ecf20Sopenharmony_ci      cmd_copy = cp $< $@
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci# Shipped files
2348c2ecf20Sopenharmony_ci# ===========================================================================
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ciquiet_cmd_shipped = SHIPPED $@
2378c2ecf20Sopenharmony_cicmd_shipped = cat $< > $@
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci$(obj)/%: $(src)/%_shipped
2408c2ecf20Sopenharmony_ci	$(call cmd,shipped)
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci# Commands useful for building a boot image
2438c2ecf20Sopenharmony_ci# ===========================================================================
2448c2ecf20Sopenharmony_ci#
2458c2ecf20Sopenharmony_ci#	Use as following:
2468c2ecf20Sopenharmony_ci#
2478c2ecf20Sopenharmony_ci#	target: source(s) FORCE
2488c2ecf20Sopenharmony_ci#		$(if_changed,ld/objcopy/gzip)
2498c2ecf20Sopenharmony_ci#
2508c2ecf20Sopenharmony_ci#	and add target to extra-y so that we know we have to
2518c2ecf20Sopenharmony_ci#	read in the saved command line
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci# Linking
2548c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ciquiet_cmd_ld = LD      $@
2578c2ecf20Sopenharmony_ci      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci# Archive
2608c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ciquiet_cmd_ar = AR      $@
2638c2ecf20Sopenharmony_ci      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci# Objcopy
2668c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ciquiet_cmd_objcopy = OBJCOPY $@
2698c2ecf20Sopenharmony_cicmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci# Gzip
2728c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ciquiet_cmd_gzip = GZIP    $@
2758c2ecf20Sopenharmony_ci      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci# DTC
2788c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
2798c2ecf20Sopenharmony_ciDTC ?= $(objtree)/scripts/dtc/dtc
2808c2ecf20Sopenharmony_ciDTC_FLAGS += -Wno-interrupt_provider
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci# Disable noisy checks by default
2838c2ecf20Sopenharmony_ciifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
2848c2ecf20Sopenharmony_ciDTC_FLAGS += -Wno-unit_address_vs_reg \
2858c2ecf20Sopenharmony_ci	-Wno-unit_address_format \
2868c2ecf20Sopenharmony_ci	-Wno-avoid_unnecessary_addr_size \
2878c2ecf20Sopenharmony_ci	-Wno-alias_paths \
2888c2ecf20Sopenharmony_ci	-Wno-graph_child_address \
2898c2ecf20Sopenharmony_ci	-Wno-simple_bus_reg \
2908c2ecf20Sopenharmony_ci	-Wno-unique_unit_address \
2918c2ecf20Sopenharmony_ci	-Wno-pci_device_reg
2928c2ecf20Sopenharmony_ciendif
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),)
2958c2ecf20Sopenharmony_ciDTC_FLAGS += -Wnode_name_chars_strict \
2968c2ecf20Sopenharmony_ci	-Wproperty_name_chars_strict \
2978c2ecf20Sopenharmony_ci	-Winterrupt_provider
2988c2ecf20Sopenharmony_ciendif
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ciDTC_FLAGS += $(DTC_FLAGS_$(basetarget))
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci# Generate an assembly file to wrap the output of the device tree compiler
3038c2ecf20Sopenharmony_ciquiet_cmd_dt_S_dtb= DTB     $@
3048c2ecf20Sopenharmony_cicmd_dt_S_dtb=						\
3058c2ecf20Sopenharmony_ci{							\
3068c2ecf20Sopenharmony_ci	echo '\#include <asm-generic/vmlinux.lds.h>'; 	\
3078c2ecf20Sopenharmony_ci	echo '.section .dtb.init.rodata,"a"';		\
3088c2ecf20Sopenharmony_ci	echo '.balign STRUCT_ALIGNMENT';		\
3098c2ecf20Sopenharmony_ci	echo '.global __dtb_$(subst -,_,$(*F))_begin';	\
3108c2ecf20Sopenharmony_ci	echo '__dtb_$(subst -,_,$(*F))_begin:';		\
3118c2ecf20Sopenharmony_ci	echo '.incbin "$<" ';				\
3128c2ecf20Sopenharmony_ci	echo '__dtb_$(subst -,_,$(*F))_end:';		\
3138c2ecf20Sopenharmony_ci	echo '.global __dtb_$(subst -,_,$(*F))_end';	\
3148c2ecf20Sopenharmony_ci	echo '.balign STRUCT_ALIGNMENT'; 		\
3158c2ecf20Sopenharmony_ci} > $@
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci$(obj)/%.dtb.S: $(obj)/%.dtb FORCE
3188c2ecf20Sopenharmony_ci	$(call if_changed,dt_S_dtb)
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ciquiet_cmd_dtc = DTC     $@
3218c2ecf20Sopenharmony_cicmd_dtc = $(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
3228c2ecf20Sopenharmony_ci	$(DTC) -O $(patsubst .%,%,$(suffix $@)) -o $@ -b 0 \
3238c2ecf20Sopenharmony_ci		$(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \
3248c2ecf20Sopenharmony_ci		-d $(depfile).dtc.tmp $(dtc-tmp) ; \
3258c2ecf20Sopenharmony_ci	cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci$(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE
3288c2ecf20Sopenharmony_ci	$(call if_changed_dep,dtc)
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ciDT_CHECKER ?= dt-validate
3318c2ecf20Sopenharmony_ciDT_BINDING_DIR := Documentation/devicetree/bindings
3328c2ecf20Sopenharmony_ci# DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile
3338c2ecf20Sopenharmony_ciDT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.json
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ciquiet_cmd_dtb_check =	CHECK   $@
3368c2ecf20Sopenharmony_ci      cmd_dtb_check =	$(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cidefine rule_dtc
3398c2ecf20Sopenharmony_ci	$(call cmd_and_fixdep,dtc)
3408c2ecf20Sopenharmony_ci	$(call cmd,dtb_check)
3418c2ecf20Sopenharmony_ciendef
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci$(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
3448c2ecf20Sopenharmony_ci	$(call if_changed_rule,dtc,yaml)
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cidtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci# Bzip2
3498c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci# Bzip2 and LZMA do not include size in file... so we have to fake that;
3528c2ecf20Sopenharmony_ci# append the size as a 32-bit littleendian number as gzip does.
3538c2ecf20Sopenharmony_cisize_append = printf $(shell						\
3548c2ecf20Sopenharmony_cidec_size=0;								\
3558c2ecf20Sopenharmony_cifor F in $(real-prereqs); do					\
3568c2ecf20Sopenharmony_ci	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
3578c2ecf20Sopenharmony_ci	dec_size=$$(expr $$dec_size + $$fsize);				\
3588c2ecf20Sopenharmony_cidone;									\
3598c2ecf20Sopenharmony_ciprintf "%08x\n" $$dec_size |						\
3608c2ecf20Sopenharmony_ci	sed 's/\(..\)/\1 /g' | {					\
3618c2ecf20Sopenharmony_ci		read ch0 ch1 ch2 ch3;					\
3628c2ecf20Sopenharmony_ci		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
3638c2ecf20Sopenharmony_ci			printf '%s%03o' '\\' $$((0x$$ch)); 		\
3648c2ecf20Sopenharmony_ci		done;							\
3658c2ecf20Sopenharmony_ci	}								\
3668c2ecf20Sopenharmony_ci)
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ciquiet_cmd_bzip2 = BZIP2   $@
3698c2ecf20Sopenharmony_ci      cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci# Lzma
3728c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ciquiet_cmd_lzma = LZMA    $@
3758c2ecf20Sopenharmony_ci      cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ciquiet_cmd_lzo = LZO     $@
3788c2ecf20Sopenharmony_ci      cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ciquiet_cmd_lz4 = LZ4     $@
3818c2ecf20Sopenharmony_ci      cmd_lz4 = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \
3828c2ecf20Sopenharmony_ci                  $(size_append); } > $@
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci# U-Boot mkimage
3858c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ciMKIMAGE := $(srctree)/scripts/mkuboot.sh
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
3908c2ecf20Sopenharmony_ci# the number of overrides in arch makefiles
3918c2ecf20Sopenharmony_ciUIMAGE_ARCH ?= $(SRCARCH)
3928c2ecf20Sopenharmony_ciUIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
3938c2ecf20Sopenharmony_ciUIMAGE_OPTS-y ?=
3948c2ecf20Sopenharmony_ciUIMAGE_TYPE ?= kernel
3958c2ecf20Sopenharmony_ciUIMAGE_LOADADDR ?= arch_must_set_this
3968c2ecf20Sopenharmony_ciUIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
3978c2ecf20Sopenharmony_ciUIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ciquiet_cmd_uimage = UIMAGE  $@
4008c2ecf20Sopenharmony_ci      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
4018c2ecf20Sopenharmony_ci			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
4028c2ecf20Sopenharmony_ci			-T $(UIMAGE_TYPE) \
4038c2ecf20Sopenharmony_ci			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
4048c2ecf20Sopenharmony_ci			-n $(UIMAGE_NAME) -d $< $@
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci# XZ
4078c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
4088c2ecf20Sopenharmony_ci# Use xzkern to compress the kernel image and xzmisc to compress other things.
4098c2ecf20Sopenharmony_ci#
4108c2ecf20Sopenharmony_ci# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
4118c2ecf20Sopenharmony_ci# of the kernel decompressor. A BCJ filter is used if it is available for
4128c2ecf20Sopenharmony_ci# the target architecture. xzkern also appends uncompressed size of the data
4138c2ecf20Sopenharmony_ci# using size_append. The .xz format has the size information available at
4148c2ecf20Sopenharmony_ci# the end of the file too, but it's in more complex format and it's good to
4158c2ecf20Sopenharmony_ci# avoid changing the part of the boot code that reads the uncompressed size.
4168c2ecf20Sopenharmony_ci# Note that the bytes added by size_append will make the xz tool think that
4178c2ecf20Sopenharmony_ci# the file is corrupt. This is expected.
4188c2ecf20Sopenharmony_ci#
4198c2ecf20Sopenharmony_ci# xzmisc doesn't use size_append, so it can be used to create normal .xz
4208c2ecf20Sopenharmony_ci# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
4218c2ecf20Sopenharmony_ci# big dictionary would increase the memory usage too much in the multi-call
4228c2ecf20Sopenharmony_ci# decompression mode. A BCJ filter isn't used either.
4238c2ecf20Sopenharmony_ciquiet_cmd_xzkern = XZKERN  $@
4248c2ecf20Sopenharmony_ci      cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
4258c2ecf20Sopenharmony_ci                     $(size_append); } > $@
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ciquiet_cmd_xzmisc = XZMISC  $@
4288c2ecf20Sopenharmony_ci      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci# ZSTD
4318c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
4328c2ecf20Sopenharmony_ci# Appends the uncompressed size of the data using size_append. The .zst
4338c2ecf20Sopenharmony_ci# format has the size information available at the beginning of the file too,
4348c2ecf20Sopenharmony_ci# but it's in a more complex format and it's good to avoid changing the part
4358c2ecf20Sopenharmony_ci# of the boot code that reads the uncompressed size.
4368c2ecf20Sopenharmony_ci#
4378c2ecf20Sopenharmony_ci# Note that the bytes added by size_append will make the zstd tool think that
4388c2ecf20Sopenharmony_ci# the file is corrupt. This is expected.
4398c2ecf20Sopenharmony_ci#
4408c2ecf20Sopenharmony_ci# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
4418c2ecf20Sopenharmony_ci# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
4428c2ecf20Sopenharmony_ci# single pass, so zstd doesn't need to allocate a window buffer. When streaming
4438c2ecf20Sopenharmony_ci# decompression is used, like initramfs decompression, zstd22 should likely not
4448c2ecf20Sopenharmony_ci# be used because it would require zstd to allocate a 128 MB buffer.
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ciquiet_cmd_zstd = ZSTD    $@
4478c2ecf20Sopenharmony_ci      cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ciquiet_cmd_zstd22 = ZSTD22  $@
4508c2ecf20Sopenharmony_ci      cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci# ASM offsets
4538c2ecf20Sopenharmony_ci# ---------------------------------------------------------------------------
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci# Default sed regexp - multiline due to syntax constraints
4568c2ecf20Sopenharmony_ci#
4578c2ecf20Sopenharmony_ci# Use [:space:] because LLVM's integrated assembler inserts <tab> around
4588c2ecf20Sopenharmony_ci# the .ascii directive whereas GCC keeps the <space> as-is.
4598c2ecf20Sopenharmony_cidefine sed-offsets
4608c2ecf20Sopenharmony_ci	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
4618c2ecf20Sopenharmony_ci	/^->/{s:->#\(.*\):/* \1 */:; \
4628c2ecf20Sopenharmony_ci	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
4638c2ecf20Sopenharmony_ci	s:->::; p;}'
4648c2ecf20Sopenharmony_ciendef
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci# Use filechk to avoid rebuilds when a header changes, but the resulting file
4678c2ecf20Sopenharmony_ci# does not
4688c2ecf20Sopenharmony_cidefine filechk_offsets
4698c2ecf20Sopenharmony_ci	 echo "#ifndef $2"; \
4708c2ecf20Sopenharmony_ci	 echo "#define $2"; \
4718c2ecf20Sopenharmony_ci	 echo "/*"; \
4728c2ecf20Sopenharmony_ci	 echo " * DO NOT MODIFY."; \
4738c2ecf20Sopenharmony_ci	 echo " *"; \
4748c2ecf20Sopenharmony_ci	 echo " * This file was generated by Kbuild"; \
4758c2ecf20Sopenharmony_ci	 echo " */"; \
4768c2ecf20Sopenharmony_ci	 echo ""; \
4778c2ecf20Sopenharmony_ci	 sed -ne $(sed-offsets) < $<; \
4788c2ecf20Sopenharmony_ci	 echo ""; \
4798c2ecf20Sopenharmony_ci	 echo "#endif"
4808c2ecf20Sopenharmony_ciendef
481