xref: /OK3568_Linux_fs/kernel/scripts/Makefile.lib (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun# Backward compatibility
3*4882a593Smuzhiyunasflags-y  += $(EXTRA_AFLAGS)
4*4882a593Smuzhiyunccflags-y  += $(EXTRA_CFLAGS)
5*4882a593Smuzhiyuncppflags-y += $(EXTRA_CPPFLAGS)
6*4882a593Smuzhiyunldflags-y  += $(EXTRA_LDFLAGS)
7*4882a593Smuzhiyunifneq ($(always),)
8*4882a593Smuzhiyun$(warning 'always' is deprecated. Please use 'always-y' instead)
9*4882a593Smuzhiyunalways-y   += $(always)
10*4882a593Smuzhiyunendif
11*4882a593Smuzhiyunifneq ($(hostprogs-y),)
12*4882a593Smuzhiyun$(warning 'hostprogs-y' is deprecated. Please use 'hostprogs' instead)
13*4882a593Smuzhiyunhostprogs  += $(hostprogs-y)
14*4882a593Smuzhiyunendif
15*4882a593Smuzhiyunifneq ($(hostprogs-m),)
16*4882a593Smuzhiyun$(warning 'hostprogs-m' is deprecated. Please use 'hostprogs' instead)
17*4882a593Smuzhiyunhostprogs  += $(hostprogs-m)
18*4882a593Smuzhiyunendif
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun# flags that take effect in current and sub directories
21*4882a593SmuzhiyunKBUILD_AFLAGS += $(subdir-asflags-y)
22*4882a593SmuzhiyunKBUILD_CFLAGS += $(subdir-ccflags-y)
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun# Figure out what we need to build from the various variables
25*4882a593Smuzhiyun# ===========================================================================
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun# When an object is listed to be built compiled-in and modular,
28*4882a593Smuzhiyun# only build the compiled-in version
29*4882a593Smuzhiyunobj-m := $(filter-out $(obj-y),$(obj-m))
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun# Libraries are always collected in one lib file.
32*4882a593Smuzhiyun# Filter out objects already built-in
33*4882a593Smuzhiyunlib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun# Subdirectories we need to descend into
36*4882a593Smuzhiyunsubdir-ym := $(sort $(subdir-y) $(subdir-m) \
37*4882a593Smuzhiyun			$(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun# Handle objects in subdirs:
40*4882a593Smuzhiyun# - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
41*4882a593Smuzhiyun#   foo/modules.order
42*4882a593Smuzhiyun# - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
43*4882a593Smuzhiyun#
44*4882a593Smuzhiyun# Generate modules.order to determine modorder. Unfortunately, we don't have
45*4882a593Smuzhiyun# information about ordering between -y and -m subdirs. Just put -y's first.
46*4882a593Smuzhiyun
47*4882a593Smuzhiyunifdef need-modorder
48*4882a593Smuzhiyunobj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
49*4882a593Smuzhiyunelse
50*4882a593Smuzhiyunobj-m := $(filter-out %/, $(obj-m))
51*4882a593Smuzhiyunendif
52*4882a593Smuzhiyun
53*4882a593Smuzhiyunifdef need-builtin
54*4882a593Smuzhiyunobj-y		:= $(patsubst %/, %/built-in.a, $(obj-y))
55*4882a593Smuzhiyunelse
56*4882a593Smuzhiyunobj-y		:= $(filter-out %/, $(obj-y))
57*4882a593Smuzhiyunendif
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun# Expand $(foo-objs) $(foo-y) by calling $(call suffix-search,foo.o,-objs -y)
60*4882a593Smuzhiyunsuffix-search = $(foreach s,$(2),$($(1:.o=$s)))
61*4882a593Smuzhiyun# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
62*4882a593Smuzhiyun# Do this recursively to find nested composite objects.
63*4882a593Smuzhiyun# foo-y may contain foo.o bar.o . For backwards compatibility, don't treat this
64*4882a593Smuzhiyun# foo.o as a nested object
65*4882a593Smuzhiyunmulti-search = $(sort $(foreach m,$(1),$(if $(strip $(call suffix-search,$(m),$(2) -)),\
66*4882a593Smuzhiyun	$(if $(filter $(m),$(strip $(call suffix-search,$(m),$(2) -))),,\
67*4882a593Smuzhiyun	$(m) $(call multi-search,$(call suffix-search,$(m),$(2)),$(2))))))
68*4882a593Smuzhiyunmulti-used-y := $(call multi-search,$(obj-y),-objs -y)
69*4882a593Smuzhiyunmulti-used-m := $(call multi-search,$(obj-m),-objs -y -m)
70*4882a593Smuzhiyunmulti-used   := $(multi-used-y) $(multi-used-m)
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun# Replace multi-part objects by their individual parts,
73*4882a593Smuzhiyun# including built-in.a from subdirectories
74*4882a593Smuzhiyun# Recursively search for real files. For backwards compatibility,
75*4882a593Smuzhiyun# foo-y may contain foo.o bar.o . foo.o in this context is a real object, and
76*4882a593Smuzhiyun# shouldn't be recursed into.
77*4882a593Smuzhiyunreal-search = $(foreach m,$(1), $(if $(strip $(call suffix-search,$(m),$(2) -)), \
78*4882a593Smuzhiyun	$(filter $(m),$(call suffix-search,$(m),$(2))) $(call real-search,$(filter-out $(m),$(call suffix-search,$(m),$(2))),$(2)),\
79*4882a593Smuzhiyun	$(m)))
80*4882a593Smuzhiyunreal-obj-y := $(call real-search, $(obj-y),-objs -y)
81*4882a593Smuzhiyunreal-obj-m := $(call real-search, $(obj-m),-objs -y -m)
82*4882a593Smuzhiyun
83*4882a593Smuzhiyunalways-y += $(always-m)
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun# hostprogs-always-y += foo
86*4882a593Smuzhiyun# ... is a shorthand for
87*4882a593Smuzhiyun# hostprogs += foo
88*4882a593Smuzhiyun# always-y  += foo
89*4882a593Smuzhiyunhostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
90*4882a593Smuzhiyunalways-y += $(hostprogs-always-y) $(hostprogs-always-m)
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun# userprogs-always-y is likewise.
93*4882a593Smuzhiyunuserprogs += $(userprogs-always-y) $(userprogs-always-m)
94*4882a593Smuzhiyunalways-y += $(userprogs-always-y) $(userprogs-always-m)
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun# DTB
97*4882a593Smuzhiyun# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
98*4882a593Smuzhiyunextra-y				+= $(dtb-y)
99*4882a593Smuzhiyunextra-$(CONFIG_OF_ALL_DTBS)	+= $(dtb-)
100*4882a593Smuzhiyun
101*4882a593Smuzhiyunifneq ($(CHECK_DTBS),)
102*4882a593Smuzhiyunextra-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y))
103*4882a593Smuzhiyunextra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
104*4882a593Smuzhiyunendif
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun# Add subdir path
107*4882a593Smuzhiyun
108*4882a593Smuzhiyunextra-y		:= $(addprefix $(obj)/,$(extra-y))
109*4882a593Smuzhiyunalways-y	:= $(addprefix $(obj)/,$(always-y))
110*4882a593Smuzhiyuntargets		:= $(addprefix $(obj)/,$(targets))
111*4882a593Smuzhiyunobj-m		:= $(addprefix $(obj)/,$(obj-m))
112*4882a593Smuzhiyunlib-y		:= $(addprefix $(obj)/,$(lib-y))
113*4882a593Smuzhiyunreal-obj-y	:= $(addprefix $(obj)/,$(real-obj-y))
114*4882a593Smuzhiyunreal-obj-m	:= $(addprefix $(obj)/,$(real-obj-m))
115*4882a593Smuzhiyunmulti-used-m	:= $(addprefix $(obj)/,$(multi-used-m))
116*4882a593Smuzhiyunsubdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun# Finds the multi-part object the current object will be linked into.
119*4882a593Smuzhiyun# If the object belongs to two or more multi-part objects, list them all.
120*4882a593Smuzhiyunmodname-multi = $(sort $(foreach m,$(multi-used),\
121*4882a593Smuzhiyun		$(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=))))
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun__modname = $(if $(modname-multi),$(modname-multi),$(basetarget))
124*4882a593Smuzhiyun
125*4882a593Smuzhiyunmodname = $(subst $(space),:,$(__modname))
126*4882a593Smuzhiyunmodfile = $(addprefix $(obj)/,$(__modname))
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun# target with $(obj)/ and its suffix stripped
129*4882a593Smuzhiyuntarget-stem = $(basename $(patsubst $(obj)/%,%,$@))
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun# These flags are needed for modversions and compiling, so we define them here
132*4882a593Smuzhiyun# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
133*4882a593Smuzhiyun# end up in (or would, if it gets compiled in)
134*4882a593Smuzhiyunname-fix-token = $(subst $(comma),_,$(subst -,_,$1))
135*4882a593Smuzhiyunname-fix = $(call stringify,$(call name-fix-token,$1))
136*4882a593Smuzhiyunbasename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
137*4882a593Smuzhiyunmodname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
138*4882a593Smuzhiyun		 -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
139*4882a593Smuzhiyunmodfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
142*4882a593Smuzhiyun                     $(filter-out $(ccflags-remove-y), \
143*4882a593Smuzhiyun                         $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
144*4882a593Smuzhiyun                     $(CFLAGS_$(target-stem).o))
145*4882a593Smuzhiyun_a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
146*4882a593Smuzhiyun                     $(filter-out $(asflags-remove-y), \
147*4882a593Smuzhiyun                         $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
148*4882a593Smuzhiyun                     $(AFLAGS_$(target-stem).o))
149*4882a593Smuzhiyun_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun#
152*4882a593Smuzhiyun# Enable gcov profiling flags for a file, directory or for all files depending
153*4882a593Smuzhiyun# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
154*4882a593Smuzhiyun# (in this order)
155*4882a593Smuzhiyun#
156*4882a593Smuzhiyunifeq ($(CONFIG_GCOV_KERNEL),y)
157*4882a593Smuzhiyun_c_flags += $(if $(patsubst n%,, \
158*4882a593Smuzhiyun		$(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
159*4882a593Smuzhiyun		$(CFLAGS_GCOV))
160*4882a593Smuzhiyunendif
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun#
163*4882a593Smuzhiyun# Enable address sanitizer flags for kernel except some files or directories
164*4882a593Smuzhiyun# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
165*4882a593Smuzhiyun#
166*4882a593Smuzhiyunifeq ($(CONFIG_KASAN),y)
167*4882a593Smuzhiyunifneq ($(CONFIG_KASAN_HW_TAGS),y)
168*4882a593Smuzhiyun_c_flags += $(if $(patsubst n%,, \
169*4882a593Smuzhiyun		$(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
170*4882a593Smuzhiyun		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
171*4882a593Smuzhiyunendif
172*4882a593Smuzhiyunendif
173*4882a593Smuzhiyun
174*4882a593Smuzhiyunifeq ($(CONFIG_UBSAN),y)
175*4882a593Smuzhiyun_c_flags += $(if $(patsubst n%,, \
176*4882a593Smuzhiyun		$(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
177*4882a593Smuzhiyun		$(CFLAGS_UBSAN))
178*4882a593Smuzhiyunendif
179*4882a593Smuzhiyun
180*4882a593Smuzhiyunifeq ($(CONFIG_KCOV),y)
181*4882a593Smuzhiyun_c_flags += $(if $(patsubst n%,, \
182*4882a593Smuzhiyun	$(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
183*4882a593Smuzhiyun	$(CFLAGS_KCOV))
184*4882a593Smuzhiyunendif
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun#
187*4882a593Smuzhiyun# Enable KCSAN flags except some files or directories we don't want to check
188*4882a593Smuzhiyun# (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
189*4882a593Smuzhiyun#
190*4882a593Smuzhiyunifeq ($(CONFIG_KCSAN),y)
191*4882a593Smuzhiyun_c_flags += $(if $(patsubst n%,, \
192*4882a593Smuzhiyun	$(KCSAN_SANITIZE_$(basetarget).o)$(KCSAN_SANITIZE)y), \
193*4882a593Smuzhiyun	$(CFLAGS_KCSAN))
194*4882a593Smuzhiyunendif
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun# $(srctree)/$(src) for including checkin headers from generated source files
197*4882a593Smuzhiyun# $(objtree)/$(obj) for including generated headers from checkin source files
198*4882a593Smuzhiyunifeq ($(KBUILD_EXTMOD),)
199*4882a593Smuzhiyunifdef building_out_of_srctree
200*4882a593Smuzhiyun_c_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
201*4882a593Smuzhiyun_a_flags   += -I $(srctree)/$(src) -I $(objtree)/$(obj)
202*4882a593Smuzhiyun_cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
203*4882a593Smuzhiyunendif
204*4882a593Smuzhiyunendif
205*4882a593Smuzhiyun
206*4882a593Smuzhiyunpart-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
207*4882a593Smuzhiyunquiet_modtag = $(if $(part-of-module),[M],   )
208*4882a593Smuzhiyun
209*4882a593Smuzhiyunmodkern_cflags =                                          \
210*4882a593Smuzhiyun	$(if $(part-of-module),                           \
211*4882a593Smuzhiyun		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
212*4882a593Smuzhiyun		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
213*4882a593Smuzhiyun
214*4882a593Smuzhiyunmodkern_aflags = $(if $(part-of-module),				\
215*4882a593Smuzhiyun			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
216*4882a593Smuzhiyun			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL))
217*4882a593Smuzhiyun
218*4882a593Smuzhiyunc_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
219*4882a593Smuzhiyun		 -include $(srctree)/include/linux/compiler_types.h       \
220*4882a593Smuzhiyun		 $(_c_flags) $(modkern_cflags)                           \
221*4882a593Smuzhiyun		 $(basename_flags) $(modname_flags)
222*4882a593Smuzhiyun
223*4882a593Smuzhiyuna_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
224*4882a593Smuzhiyun		 $(_a_flags) $(modkern_aflags)
225*4882a593Smuzhiyun
226*4882a593Smuzhiyuncpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
227*4882a593Smuzhiyun		 $(_cpp_flags)
228*4882a593Smuzhiyun
229*4882a593Smuzhiyunld_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
230*4882a593Smuzhiyun
231*4882a593SmuzhiyunDTC_INCLUDE    := $(srctree)/scripts/dtc/include-prefixes
232*4882a593Smuzhiyun
233*4882a593Smuzhiyundtc_cpp_flags  = -Wp,-MMD,$(depfile).pre.tmp -nostdinc                    \
234*4882a593Smuzhiyun		 $(addprefix -I,$(DTC_INCLUDE))                          \
235*4882a593Smuzhiyun		 -undef -D__DTS__
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun# Objtool arguments are also needed for modfinal with LTO, so we define
238*4882a593Smuzhiyun# then here to avoid duplication.
239*4882a593Smuzhiyunobjtool_args =								\
240*4882a593Smuzhiyun	$(if $(CONFIG_UNWINDER_ORC),orc generate,check)			\
241*4882a593Smuzhiyun	$(if $(part-of-module), --module,)				\
242*4882a593Smuzhiyun	$(if $(CONFIG_FRAME_POINTER),, --no-fp)				\
243*4882a593Smuzhiyun	$(if $(or $(CONFIG_GCOV_KERNEL),$(CONFIG_LTO_CLANG)), 		\
244*4882a593Smuzhiyun		--no-unreachable,)					\
245*4882a593Smuzhiyun	$(if $(CONFIG_RETPOLINE), --retpoline,)				\
246*4882a593Smuzhiyun	$(if $(CONFIG_RETHUNK), --rethunk,)				\
247*4882a593Smuzhiyun	$(if $(CONFIG_X86_SMAP), --uaccess,)				\
248*4882a593Smuzhiyun	$(if $(CONFIG_SLS), --sls,)					\
249*4882a593Smuzhiyun	$(if $(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL), --mcount,)
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun# Useful for describing the dependency of composite objects
252*4882a593Smuzhiyun# Usage:
253*4882a593Smuzhiyun#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
254*4882a593Smuzhiyundefine multi_depend
255*4882a593Smuzhiyun$(foreach m, $(notdir $1), \
256*4882a593Smuzhiyun	$(eval $(obj)/$m: \
257*4882a593Smuzhiyun	$(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
258*4882a593Smuzhiyunendef
259*4882a593Smuzhiyun
260*4882a593Smuzhiyunquiet_cmd_copy = COPY    $@
261*4882a593Smuzhiyun      cmd_copy = cp $< $@
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun# Shipped files
264*4882a593Smuzhiyun# ===========================================================================
265*4882a593Smuzhiyun
266*4882a593Smuzhiyunquiet_cmd_shipped = SHIPPED $@
267*4882a593Smuzhiyuncmd_shipped = cat $< > $@
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun$(obj)/%: $(src)/%_shipped
270*4882a593Smuzhiyun	$(call cmd,shipped)
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun# Commands useful for building a boot image
273*4882a593Smuzhiyun# ===========================================================================
274*4882a593Smuzhiyun#
275*4882a593Smuzhiyun#	Use as following:
276*4882a593Smuzhiyun#
277*4882a593Smuzhiyun#	target: source(s) FORCE
278*4882a593Smuzhiyun#		$(if_changed,ld/objcopy/gzip)
279*4882a593Smuzhiyun#
280*4882a593Smuzhiyun#	and add target to extra-y so that we know we have to
281*4882a593Smuzhiyun#	read in the saved command line
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun# Linking
284*4882a593Smuzhiyun# ---------------------------------------------------------------------------
285*4882a593Smuzhiyun
286*4882a593Smuzhiyunquiet_cmd_ld = LD      $@
287*4882a593Smuzhiyun      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun# Archive
290*4882a593Smuzhiyun# ---------------------------------------------------------------------------
291*4882a593Smuzhiyun
292*4882a593Smuzhiyunquiet_cmd_ar = AR      $@
293*4882a593Smuzhiyun      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun# Objcopy
296*4882a593Smuzhiyun# ---------------------------------------------------------------------------
297*4882a593Smuzhiyun
298*4882a593Smuzhiyunquiet_cmd_objcopy = OBJCOPY $@
299*4882a593Smuzhiyuncmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun# Gzip
302*4882a593Smuzhiyun# ---------------------------------------------------------------------------
303*4882a593Smuzhiyun
304*4882a593Smuzhiyunquiet_cmd_gzip = GZIP    $@
305*4882a593Smuzhiyun      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun# DTC
308*4882a593Smuzhiyun# ---------------------------------------------------------------------------
309*4882a593Smuzhiyunifeq ("$(origin DTC)", "command line")
310*4882a593SmuzhiyunPHONY += $(DTC)
311*4882a593Smuzhiyundtc-option = $(call try-run, $(DTC) $1 -v,$1)
312*4882a593Smuzhiyunelse
313*4882a593Smuzhiyun# Just add the flag. DTC is compiled later as a prerequisite, so there's no dtc
314*4882a593Smuzhiyun# to test the flag against. This is okay because we're not testing flags which
315*4882a593Smuzhiyun# aren't supported by in-kernel dtc to begin with.
316*4882a593Smuzhiyundtc-option = $1
317*4882a593Smuzhiyunendif
318*4882a593Smuzhiyun
319*4882a593SmuzhiyunDTC ?= $(objtree)/scripts/dtc/dtc
320*4882a593SmuzhiyunDTC_FLAGS += $(call dtc-option,-Wno-interrupt_provider)
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun# Generation of symbols for Android
323*4882a593Smuzhiyunifeq ($(CONFIG_DTC_SYMBOLS),y)
324*4882a593SmuzhiyunDTC_FLAGS += -@
325*4882a593Smuzhiyunendif
326*4882a593Smuzhiyun
327*4882a593Smuzhiyunifeq ($(CONFIG_DTC_OMIT_DISABLED),y)
328*4882a593SmuzhiyunDTC_FLAGS += -Wnode_disabled
329*4882a593Smuzhiyunendif
330*4882a593Smuzhiyun
331*4882a593Smuzhiyunifeq ($(CONFIG_DTC_OMIT_EMPTY),y)
332*4882a593SmuzhiyunDTC_FLAGS += -Wnode_empty
333*4882a593Smuzhiyunendif
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun# Disable noisy checks by default
336*4882a593Smuzhiyunifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
337*4882a593SmuzhiyunDTC_FLAGS += $(call dtc-option,-Wno-unit_address_vs_reg) \
338*4882a593Smuzhiyun	$(call dtc-option,-Wno-unit_address_format) \
339*4882a593Smuzhiyun	$(call dtc-option,-Wno-avoid_unnecessary_addr_size) \
340*4882a593Smuzhiyun	$(call dtc-option,-Wno-alias_paths) \
341*4882a593Smuzhiyun	$(call dtc-option,-Wno-graph_child_address) \
342*4882a593Smuzhiyun	$(call dtc-option,-Wno-simple_bus_reg) \
343*4882a593Smuzhiyun	$(call dtc-option,-Wno-unique_unit_address) \
344*4882a593Smuzhiyun	$(call dtc-option,-Wno-pci_device_reg)
345*4882a593Smuzhiyunendif
346*4882a593Smuzhiyun
347*4882a593Smuzhiyunifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),)
348*4882a593SmuzhiyunDTC_FLAGS += $(call dtc-option,-Wnode_name_chars_strict) \
349*4882a593Smuzhiyun	$(call dtc-option,-Wproperty_name_chars_strict) \
350*4882a593Smuzhiyun	$(call dtc-option,-Winterrupt_provider)
351*4882a593Smuzhiyunendif
352*4882a593Smuzhiyun
353*4882a593SmuzhiyunDTC_FLAGS += $(DTC_FLAGS_$(basetarget))
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun# Generate an assembly file to wrap the output of the device tree compiler
356*4882a593Smuzhiyunquiet_cmd_dt_S_dtb= DTB     $@
357*4882a593Smuzhiyuncmd_dt_S_dtb=						\
358*4882a593Smuzhiyun{							\
359*4882a593Smuzhiyun	echo '\#include <asm-generic/vmlinux.lds.h>'; 	\
360*4882a593Smuzhiyun	echo '.section .dtb.init.rodata,"a"';		\
361*4882a593Smuzhiyun	echo '.balign STRUCT_ALIGNMENT';		\
362*4882a593Smuzhiyun	echo '.global __dtb_$(subst -,_,$(*F))_begin';	\
363*4882a593Smuzhiyun	echo '__dtb_$(subst -,_,$(*F))_begin:';		\
364*4882a593Smuzhiyun	echo '.incbin "$<" ';				\
365*4882a593Smuzhiyun	echo '__dtb_$(subst -,_,$(*F))_end:';		\
366*4882a593Smuzhiyun	echo '.global __dtb_$(subst -,_,$(*F))_end';	\
367*4882a593Smuzhiyun	echo '.balign STRUCT_ALIGNMENT'; 		\
368*4882a593Smuzhiyun} > $@
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun$(obj)/%.dtb.S: $(obj)/%.dtb FORCE
371*4882a593Smuzhiyun	$(call if_changed,dt_S_dtb)
372*4882a593Smuzhiyun
373*4882a593Smuzhiyunquiet_cmd_dtc = DTC     $@
374*4882a593Smuzhiyuncmd_dtc = $(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
375*4882a593Smuzhiyun	$(DTC) -O $(patsubst .%,%,$(suffix $@)) -o $@ -b 0 \
376*4882a593Smuzhiyun		$(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \
377*4882a593Smuzhiyun		-d $(depfile).dtc.tmp $(dtc-tmp) ; \
378*4882a593Smuzhiyun	cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun$(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE
381*4882a593Smuzhiyun	$(call if_changed_dep,dtc)
382*4882a593Smuzhiyun
383*4882a593SmuzhiyunDT_CHECKER ?= dt-validate
384*4882a593SmuzhiyunDT_BINDING_DIR := Documentation/devicetree/bindings
385*4882a593Smuzhiyun# DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile
386*4882a593SmuzhiyunDT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.json
387*4882a593Smuzhiyun
388*4882a593Smuzhiyunquiet_cmd_dtb_check =	CHECK   $@
389*4882a593Smuzhiyun      cmd_dtb_check =	$(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@
390*4882a593Smuzhiyun
391*4882a593Smuzhiyundefine rule_dtc
392*4882a593Smuzhiyun	$(call cmd_and_fixdep,dtc)
393*4882a593Smuzhiyun	$(call cmd,dtb_check)
394*4882a593Smuzhiyunendef
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun$(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
397*4882a593Smuzhiyun	$(call if_changed_rule,dtc,yaml)
398*4882a593Smuzhiyun
399*4882a593Smuzhiyundtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun# Bzip2
402*4882a593Smuzhiyun# ---------------------------------------------------------------------------
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun# Bzip2 and LZMA do not include size in file... so we have to fake that;
405*4882a593Smuzhiyun# append the size as a 32-bit littleendian number as gzip does.
406*4882a593Smuzhiyunsize_append = printf $(shell						\
407*4882a593Smuzhiyundec_size=0;								\
408*4882a593Smuzhiyunfor F in $(real-prereqs); do					\
409*4882a593Smuzhiyun	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
410*4882a593Smuzhiyun	dec_size=$$(expr $$dec_size + $$fsize);				\
411*4882a593Smuzhiyundone;									\
412*4882a593Smuzhiyunprintf "%08x\n" $$dec_size |						\
413*4882a593Smuzhiyun	sed 's/\(..\)/\1 /g' | {					\
414*4882a593Smuzhiyun		read ch0 ch1 ch2 ch3;					\
415*4882a593Smuzhiyun		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
416*4882a593Smuzhiyun			printf '%s%03o' '\\' $$((0x$$ch)); 		\
417*4882a593Smuzhiyun		done;							\
418*4882a593Smuzhiyun	}								\
419*4882a593Smuzhiyun)
420*4882a593Smuzhiyun
421*4882a593Smuzhiyunquiet_cmd_bzip2 = BZIP2   $@
422*4882a593Smuzhiyun      cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun# Lzma
425*4882a593Smuzhiyun# ---------------------------------------------------------------------------
426*4882a593Smuzhiyun
427*4882a593Smuzhiyunquiet_cmd_lzma = LZMA    $@
428*4882a593Smuzhiyun      cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
429*4882a593Smuzhiyun
430*4882a593Smuzhiyunquiet_cmd_lzo = LZO     $@
431*4882a593Smuzhiyun      cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
432*4882a593Smuzhiyun
433*4882a593Smuzhiyunquiet_cmd_lz4 = LZ4     $@
434*4882a593Smuzhiyun      cmd_lz4 = { cat $(real-prereqs) | \
435*4882a593Smuzhiyun                  $(LZ4) -l -12 --favor-decSpeed stdin stdout; \
436*4882a593Smuzhiyun                  $(size_append); } > $@
437*4882a593Smuzhiyun
438*4882a593Smuzhiyunquiet_cmd_lz4c = LZ4C    $@
439*4882a593Smuzhiyun      cmd_lz4c = { cat $(real-prereqs) | \
440*4882a593Smuzhiyun                   $(LZ4) -12 --favor-decSpeed stdin stdout; \
441*4882a593Smuzhiyun                   $(size_append); } > $@
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun# U-Boot mkimage
444*4882a593Smuzhiyun# ---------------------------------------------------------------------------
445*4882a593Smuzhiyun
446*4882a593SmuzhiyunMKIMAGE := $(srctree)/scripts/mkuboot.sh
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
449*4882a593Smuzhiyun# the number of overrides in arch makefiles
450*4882a593SmuzhiyunUIMAGE_ARCH ?= $(SRCARCH)
451*4882a593SmuzhiyunUIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
452*4882a593SmuzhiyunUIMAGE_OPTS-y ?=
453*4882a593SmuzhiyunUIMAGE_TYPE ?= kernel
454*4882a593SmuzhiyunUIMAGE_LOADADDR ?= arch_must_set_this
455*4882a593SmuzhiyunUIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
456*4882a593SmuzhiyunUIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
457*4882a593Smuzhiyun
458*4882a593Smuzhiyunquiet_cmd_uimage = UIMAGE  $@
459*4882a593Smuzhiyun      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
460*4882a593Smuzhiyun			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
461*4882a593Smuzhiyun			-T $(UIMAGE_TYPE) \
462*4882a593Smuzhiyun			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
463*4882a593Smuzhiyun			-n $(UIMAGE_NAME) -d $< $@
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun# XZ
466*4882a593Smuzhiyun# ---------------------------------------------------------------------------
467*4882a593Smuzhiyun# Use xzkern to compress the kernel image and xzmisc to compress other things.
468*4882a593Smuzhiyun#
469*4882a593Smuzhiyun# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
470*4882a593Smuzhiyun# of the kernel decompressor. A BCJ filter is used if it is available for
471*4882a593Smuzhiyun# the target architecture. xzkern also appends uncompressed size of the data
472*4882a593Smuzhiyun# using size_append. The .xz format has the size information available at
473*4882a593Smuzhiyun# the end of the file too, but it's in more complex format and it's good to
474*4882a593Smuzhiyun# avoid changing the part of the boot code that reads the uncompressed size.
475*4882a593Smuzhiyun# Note that the bytes added by size_append will make the xz tool think that
476*4882a593Smuzhiyun# the file is corrupt. This is expected.
477*4882a593Smuzhiyun#
478*4882a593Smuzhiyun# xzmisc doesn't use size_append, so it can be used to create normal .xz
479*4882a593Smuzhiyun# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
480*4882a593Smuzhiyun# big dictionary would increase the memory usage too much in the multi-call
481*4882a593Smuzhiyun# decompression mode. A BCJ filter isn't used either.
482*4882a593Smuzhiyunquiet_cmd_xzkern = XZKERN  $@
483*4882a593Smuzhiyun      cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
484*4882a593Smuzhiyun                     $(size_append); } > $@
485*4882a593Smuzhiyun
486*4882a593Smuzhiyunquiet_cmd_xzmisc = XZMISC  $@
487*4882a593Smuzhiyun      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun# ZSTD
490*4882a593Smuzhiyun# ---------------------------------------------------------------------------
491*4882a593Smuzhiyun# Appends the uncompressed size of the data using size_append. The .zst
492*4882a593Smuzhiyun# format has the size information available at the beginning of the file too,
493*4882a593Smuzhiyun# but it's in a more complex format and it's good to avoid changing the part
494*4882a593Smuzhiyun# of the boot code that reads the uncompressed size.
495*4882a593Smuzhiyun#
496*4882a593Smuzhiyun# Note that the bytes added by size_append will make the zstd tool think that
497*4882a593Smuzhiyun# the file is corrupt. This is expected.
498*4882a593Smuzhiyun#
499*4882a593Smuzhiyun# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
500*4882a593Smuzhiyun# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
501*4882a593Smuzhiyun# single pass, so zstd doesn't need to allocate a window buffer. When streaming
502*4882a593Smuzhiyun# decompression is used, like initramfs decompression, zstd22 should likely not
503*4882a593Smuzhiyun# be used because it would require zstd to allocate a 128 MB buffer.
504*4882a593Smuzhiyun
505*4882a593Smuzhiyunquiet_cmd_zstd = ZSTD    $@
506*4882a593Smuzhiyun      cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@
507*4882a593Smuzhiyun
508*4882a593Smuzhiyunquiet_cmd_zstd22 = ZSTD22  $@
509*4882a593Smuzhiyun      cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun# ASM offsets
512*4882a593Smuzhiyun# ---------------------------------------------------------------------------
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun# Default sed regexp - multiline due to syntax constraints
515*4882a593Smuzhiyun#
516*4882a593Smuzhiyun# Use [:space:] because LLVM's integrated assembler inserts <tab> around
517*4882a593Smuzhiyun# the .ascii directive whereas GCC keeps the <space> as-is.
518*4882a593Smuzhiyundefine sed-offsets
519*4882a593Smuzhiyun	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
520*4882a593Smuzhiyun	/^->/{s:->#\(.*\):/* \1 */:; \
521*4882a593Smuzhiyun	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
522*4882a593Smuzhiyun	s:->::; p;}'
523*4882a593Smuzhiyunendef
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun# Use filechk to avoid rebuilds when a header changes, but the resulting file
526*4882a593Smuzhiyun# does not
527*4882a593Smuzhiyundefine filechk_offsets
528*4882a593Smuzhiyun	 echo "#ifndef $2"; \
529*4882a593Smuzhiyun	 echo "#define $2"; \
530*4882a593Smuzhiyun	 echo "/*"; \
531*4882a593Smuzhiyun	 echo " * DO NOT MODIFY."; \
532*4882a593Smuzhiyun	 echo " *"; \
533*4882a593Smuzhiyun	 echo " * This file was generated by Kbuild"; \
534*4882a593Smuzhiyun	 echo " */"; \
535*4882a593Smuzhiyun	 echo ""; \
536*4882a593Smuzhiyun	 sed -ne $(sed-offsets) < $<; \
537*4882a593Smuzhiyun	 echo ""; \
538*4882a593Smuzhiyun	 echo "#endif"
539*4882a593Smuzhiyunendef
540