1# SPDX-License-Identifier: GPL-2.0
2
3# flags that take effect in current and sub directories
4KBUILD_AFLAGS += $(subdir-asflags-y)
5KBUILD_CFLAGS += $(subdir-ccflags-y)
6KBUILD_RUSTFLAGS += $(subdir-rustflags-y)
7
8# Figure out what we need to build from the various variables
9# ===========================================================================
10
11# When an object is listed to be built compiled-in and modular,
12# only build the compiled-in version
13obj-m := $(filter-out $(obj-y),$(obj-m))
14
15# Libraries are always collected in one lib file.
16# Filter out objects already built-in
17lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
18
19# Subdirectories we need to descend into
20subdir-ym := $(sort $(subdir-y) $(subdir-m) \
21			$(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
22
23# Handle objects in subdirs:
24# - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
25#   foo/modules.order
26# - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
27#
28# Generate modules.order to determine modorder. Unfortunately, we don't have
29# information about ordering between -y and -m subdirs. Just put -y's first.
30
31ifdef need-modorder
32obj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
33else
34obj-m := $(filter-out %/, $(obj-m))
35endif
36
37ifdef need-builtin
38obj-y		:= $(patsubst %/, %/built-in.a, $(obj-y))
39else
40obj-y		:= $(filter-out %/, $(obj-y))
41endif
42
43# Expand $(foo-objs) $(foo-y) etc. by replacing their individuals
44suffix-search = $(strip $(foreach s, $3, $($(1:%$(strip $2)=%$s))))
45# List composite targets that are constructed by combining other targets
46multi-search = $(sort $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $m)))
47# List primitive targets that are compiled from source files
48real-search = $(foreach m, $1, $(if $(call suffix-search, $m, $2, $3 -), $(call suffix-search, $m, $2, $3), $m))
49
50# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
51multi-obj-y := $(call multi-search, $(obj-y), .o, -objs -y)
52multi-obj-m := $(call multi-search, $(obj-m), .o, -objs -y -m)
53multi-obj-ym := $(multi-obj-y) $(multi-obj-m)
54
55# Replace multi-part objects by their individual parts,
56# including built-in.a from subdirectories
57real-obj-y := $(call real-search, $(obj-y), .o, -objs -y)
58real-obj-m := $(call real-search, $(obj-m), .o, -objs -y -m)
59
60always-y += $(always-m)
61
62# hostprogs-always-y += foo
63# ... is a shorthand for
64# hostprogs += foo
65# always-y  += foo
66hostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
67always-y += $(hostprogs-always-y) $(hostprogs-always-m)
68
69# userprogs-always-y is likewise.
70userprogs += $(userprogs-always-y) $(userprogs-always-m)
71always-y += $(userprogs-always-y) $(userprogs-always-m)
72
73# Add subdir path
74
75ifneq ($(obj),.)
76extra-y		:= $(addprefix $(obj)/,$(extra-y))
77always-y	:= $(addprefix $(obj)/,$(always-y))
78targets		:= $(addprefix $(obj)/,$(targets))
79obj-m		:= $(addprefix $(obj)/,$(obj-m))
80lib-y		:= $(addprefix $(obj)/,$(lib-y))
81real-obj-y	:= $(addprefix $(obj)/,$(real-obj-y))
82real-obj-m	:= $(addprefix $(obj)/,$(real-obj-m))
83multi-obj-m	:= $(addprefix $(obj)/, $(multi-obj-m))
84subdir-ym	:= $(addprefix $(obj)/,$(subdir-ym))
85endif
86
87# Finds the multi-part object the current object will be linked into.
88# If the object belongs to two or more multi-part objects, list them all.
89modname-multi = $(sort $(foreach m,$(multi-obj-ym),\
90		$(if $(filter $*.o, $(call suffix-search, $m, .o, -objs -y -m)),$(m:.o=))))
91
92__modname = $(or $(modname-multi),$(basetarget))
93
94modname = $(subst $(space),:,$(__modname))
95modfile = $(addprefix $(obj)/,$(__modname))
96
97# target with $(obj)/ and its suffix stripped
98target-stem = $(basename $(patsubst $(obj)/%,%,$@))
99
100# These flags are needed for modversions and compiling, so we define them here
101# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
102# end up in (or would, if it gets compiled in)
103name-fix-token = $(subst $(comma),_,$(subst -,_,$1))
104name-fix = $(call stringify,$(call name-fix-token,$1))
105basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
106modname_flags  = -DKBUILD_MODNAME=$(call name-fix,$(modname)) \
107		 -D__KBUILD_MODNAME=kmod_$(call name-fix-token,$(modname))
108modfile_flags  = -DKBUILD_MODFILE=$(call stringify,$(modfile))
109
110_c_flags       = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
111                     $(filter-out $(ccflags-remove-y), \
112                         $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
113                     $(CFLAGS_$(target-stem).o))
114_rust_flags    = $(filter-out $(RUSTFLAGS_REMOVE_$(target-stem).o), \
115                     $(filter-out $(rustflags-remove-y), \
116                         $(KBUILD_RUSTFLAGS) $(rustflags-y)) \
117                     $(RUSTFLAGS_$(target-stem).o))
118_a_flags       = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
119                     $(filter-out $(asflags-remove-y), \
120                         $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
121                     $(AFLAGS_$(target-stem).o))
122_cpp_flags     = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
123
124#
125# Enable gcov profiling flags for a file, directory or for all files depending
126# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
127# (in this order)
128#
129ifeq ($(CONFIG_GCOV_KERNEL),y)
130_c_flags += $(if $(patsubst n%,, \
131		$(GCOV_PROFILE_$(target-stem).o)$(GCOV_PROFILE)$(if $(is-kernel-object),$(CONFIG_GCOV_PROFILE_ALL))), \
132		$(CFLAGS_GCOV))
133endif
134
135#
136# Enable address sanitizer flags for kernel except some files or directories
137# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
138#
139ifeq ($(CONFIG_KASAN),y)
140ifneq ($(CONFIG_KASAN_HW_TAGS),y)
141_c_flags += $(if $(patsubst n%,, \
142		$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
143		$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
144_rust_flags += $(if $(patsubst n%,, \
145		$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
146		$(RUSTFLAGS_KASAN))
147endif
148endif
149
150ifeq ($(CONFIG_KMSAN),y)
151_c_flags += $(if $(patsubst n%,, \
152		$(KMSAN_SANITIZE_$(target-stem).o)$(KMSAN_SANITIZE)$(is-kernel-object)), \
153		$(CFLAGS_KMSAN))
154_c_flags += $(if $(patsubst n%,, \
155		$(KMSAN_ENABLE_CHECKS_$(target-stem).o)$(KMSAN_ENABLE_CHECKS)$(is-kernel-object)), \
156		, -mllvm -msan-disable-checks=1)
157endif
158
159ifeq ($(CONFIG_UBSAN),y)
160_c_flags += $(if $(patsubst n%,, \
161		$(UBSAN_SANITIZE_$(target-stem).o)$(UBSAN_SANITIZE)$(is-kernel-object)), \
162		$(CFLAGS_UBSAN))
163_c_flags += $(if $(patsubst n%,, \
164		$(UBSAN_INTEGER_WRAP_$(target-stem).o)$(UBSAN_SANITIZE_$(target-stem).o)$(UBSAN_INTEGER_WRAP)$(UBSAN_SANITIZE)$(is-kernel-object)), \
165		$(CFLAGS_UBSAN_INTEGER_WRAP))
166endif
167
168ifeq ($(CONFIG_KCOV),y)
169_c_flags += $(if $(patsubst n%,, \
170	$(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \
171	$(CFLAGS_KCOV))
172endif
173
174#
175# Enable KCSAN flags except some files or directories we don't want to check
176# (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
177#
178ifeq ($(CONFIG_KCSAN),y)
179_c_flags += $(if $(patsubst n%,, \
180	$(KCSAN_SANITIZE_$(target-stem).o)$(KCSAN_SANITIZE)$(is-kernel-object)), \
181	$(CFLAGS_KCSAN))
182# Some uninstrumented files provide implied barriers required to avoid false
183# positives: set KCSAN_INSTRUMENT_BARRIERS for barrier instrumentation only.
184_c_flags += $(if $(patsubst n%,, \
185	$(KCSAN_INSTRUMENT_BARRIERS_$(target-stem).o)$(KCSAN_INSTRUMENT_BARRIERS)n), \
186	-D__KCSAN_INSTRUMENT_BARRIERS__)
187endif
188
189#
190# Enable AutoFDO build flags except some files or directories we don't want to
191# enable (depends on variables AUTOFDO_PROFILE_obj.o and AUTOFDO_PROFILE).
192#
193ifeq ($(CONFIG_AUTOFDO_CLANG),y)
194_c_flags += $(if $(patsubst n%,, \
195	$(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
196	$(CFLAGS_AUTOFDO_CLANG))
197endif
198
199#
200# Enable Propeller build flags except some files or directories we don't want to
201# enable (depends on variables AUTOFDO_PROPELLER_obj.o and PROPELLER_PROFILE).
202#
203ifdef CONFIG_PROPELLER_CLANG
204_c_flags += $(if $(patsubst n%,, \
205	$(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(PROPELLER_PROFILE))$(is-kernel-object), \
206	$(CFLAGS_PROPELLER_CLANG))
207endif
208
209# $(src) for including checkin headers from generated source files
210# $(obj) for including generated headers from checkin source files
211ifdef building_out_of_srctree
212_c_flags   += $(addprefix -I, $(src) $(obj))
213_a_flags   += $(addprefix -I, $(src) $(obj))
214_cpp_flags += $(addprefix -I, $(src) $(obj))
215endif
216
217# If $(is-kernel-object) is 'y', this object will be linked to vmlinux or modules
218is-kernel-object = $(or $(part-of-builtin),$(part-of-module))
219
220part-of-builtin = $(if $(filter $(basename $@).o, $(real-obj-y) $(lib-y)),y)
221part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
222quiet_modtag = $(if $(part-of-module),[M],   )
223
224modkern_cflags =                                          \
225	$(if $(part-of-module),                           \
226		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
227		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
228
229modkern_rustflags =                                              \
230	$(if $(part-of-module),                                   \
231		$(KBUILD_RUSTFLAGS_MODULE) $(RUSTFLAGS_MODULE), \
232		$(KBUILD_RUSTFLAGS_KERNEL) $(RUSTFLAGS_KERNEL))
233
234modkern_aflags = $(if $(part-of-module),				\
235			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
236			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL) $(modfile_flags))
237
238c_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
239		 -include $(srctree)/include/linux/compiler_types.h       \
240		 $(_c_flags) $(modkern_cflags)                           \
241		 $(basename_flags) $(modname_flags)
242
243rust_flags     = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg
244
245a_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
246		 $(_a_flags) $(modkern_aflags) $(modname_flags)
247
248cpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
249		 $(_cpp_flags)
250
251ld_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
252
253ifdef CONFIG_OBJTOOL
254
255objtool := $(objtree)/tools/objtool/objtool
256
257objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK)		+= --hacks=jump_label
258objtool-args-$(CONFIG_HAVE_NOINSTR_HACK)		+= --hacks=noinstr
259objtool-args-$(CONFIG_MITIGATION_CALL_DEPTH_TRACKING)	+= --hacks=skylake
260objtool-args-$(CONFIG_X86_KERNEL_IBT)			+= --ibt
261objtool-args-$(CONFIG_FINEIBT)				+= --cfi
262objtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL)	+= --mcount
263ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
264objtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT)		+= --mnop
265endif
266objtool-args-$(CONFIG_UNWINDER_ORC)			+= --orc
267objtool-args-$(CONFIG_MITIGATION_RETPOLINE)		+= --retpoline
268objtool-args-$(CONFIG_MITIGATION_RETHUNK)		+= --rethunk
269objtool-args-$(CONFIG_MITIGATION_SLS)			+= --sls
270objtool-args-$(CONFIG_STACK_VALIDATION)			+= --stackval
271objtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE)		+= --static-call
272objtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION)		+= --uaccess
273objtool-args-$(or $(CONFIG_GCOV_KERNEL),$(CONFIG_KCOV))	+= --no-unreachable
274objtool-args-$(CONFIG_PREFIX_SYMBOLS)			+= --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
275objtool-args-$(CONFIG_OBJTOOL_WERROR)			+= --Werror
276
277objtool-args = $(objtool-args-y)					\
278	$(if $(delay-objtool), --link)					\
279	$(if $(part-of-module), --module)
280
281delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
282
283cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
284cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
285
286objtool-enabled := y
287
288endif # CONFIG_OBJTOOL
289
290# Useful for describing the dependency of composite objects
291# Usage:
292#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
293define multi_depend
294$(foreach m, $1, \
295	$(eval $m: \
296	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
297endef
298
299# Build commands
300# ===========================================================================
301# These are shared by some Makefile.* files.
302
303ifdef CONFIG_LTO_CLANG
304# Run $(LD) here to convert LLVM IR to ELF in the following cases:
305#  - when this object needs objtool processing, as objtool cannot process LLVM IR
306#  - when this is a single-object module, as modpost cannot process LLVM IR
307cmd_ld_single = $(if $(objtool-enabled)$(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
308endif
309
310quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
311      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
312		$(cmd_ld_single) \
313		$(cmd_objtool)
314
315define rule_cc_o_c
316	$(call cmd_and_fixdep,cc_o_c)
317	$(call cmd,checksrc)
318	$(call cmd,checkdoc)
319	$(call cmd,gen_objtooldep)
320	$(call cmd,gen_symversions_c)
321	$(call cmd,record_mcount)
322	$(call cmd,warn_shared_object)
323endef
324
325quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
326      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
327
328define rule_as_o_S
329	$(call cmd_and_fixdep,as_o_S)
330	$(call cmd,gen_objtooldep)
331	$(call cmd,gen_symversions_S)
332	$(call cmd,warn_shared_object)
333endef
334
335# Copy a file
336# ===========================================================================
337# 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
338# the copy would be read-only as well, leading to an error when executing the
339# rule next time. Use 'cat' instead in order to generate a writable file.
340quiet_cmd_copy = COPY    $@
341      cmd_copy = cat $< > $@
342
343$(obj)/%: $(src)/%_shipped
344	$(call cmd,copy)
345
346# Commands useful for building a boot image
347# ===========================================================================
348#
349#	Use as following:
350#
351#	target: source(s) FORCE
352#		$(if_changed,ld/objcopy/gzip)
353#
354#	and add target to 'targets' so that we know we have to
355#	read in the saved command line
356
357# Linking
358# ---------------------------------------------------------------------------
359
360quiet_cmd_ld = LD      $@
361      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
362
363# Archive
364# ---------------------------------------------------------------------------
365
366quiet_cmd_ar = AR      $@
367      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
368
369# Objcopy
370# ---------------------------------------------------------------------------
371
372quiet_cmd_objcopy = OBJCOPY $@
373cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
374
375# Gzip
376# ---------------------------------------------------------------------------
377
378quiet_cmd_gzip = GZIP    $@
379      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
380
381# Bzip2
382# ---------------------------------------------------------------------------
383
384# Bzip2 and LZMA do not include size in file... so we have to fake that;
385# append the size as a 32-bit littleendian number as gzip does.
386size_append = printf $(shell						\
387dec_size=0;								\
388for F in $(real-prereqs); do					\
389	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
390	dec_size=$$(expr $$dec_size + $$fsize);				\
391done;									\
392printf "%08x\n" $$dec_size |						\
393	sed 's/\(..\)/\1 /g' | {					\
394		read ch0 ch1 ch2 ch3;					\
395		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
396			printf '%s%03o' '\\' $$((0x$$ch)); 		\
397		done;							\
398	}								\
399)
400
401quiet_cmd_file_size = GEN     $@
402      cmd_file_size = $(size_append) > $@
403
404quiet_cmd_bzip2 = BZIP2   $@
405      cmd_bzip2 = cat $(real-prereqs) | $(KBZIP2) -9 > $@
406
407quiet_cmd_bzip2_with_size = BZIP2   $@
408      cmd_bzip2_with_size = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
409
410# Lzma
411# ---------------------------------------------------------------------------
412
413quiet_cmd_lzma = LZMA    $@
414      cmd_lzma = cat $(real-prereqs) | $(LZMA) -9 > $@
415
416quiet_cmd_lzma_with_size = LZMA    $@
417      cmd_lzma_with_size = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
418
419quiet_cmd_lzo = LZO     $@
420      cmd_lzo = cat $(real-prereqs) | $(KLZOP) -9 > $@
421
422quiet_cmd_lzo_with_size = LZO     $@
423      cmd_lzo_with_size = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
424
425quiet_cmd_lz4 = LZ4     $@
426      cmd_lz4 = cat $(real-prereqs) | $(LZ4) -l -9 - - > $@
427
428quiet_cmd_lz4_with_size = LZ4     $@
429      cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -9 - -; \
430                  $(size_append); } > $@
431
432# U-Boot mkimage
433# ---------------------------------------------------------------------------
434
435MKIMAGE := $(srctree)/scripts/mkuboot.sh
436
437# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
438# the number of overrides in arch makefiles
439UIMAGE_ARCH ?= $(SRCARCH)
440UIMAGE_COMPRESSION ?= $(or $(2),none)
441UIMAGE_OPTS-y ?=
442UIMAGE_TYPE ?= kernel
443UIMAGE_LOADADDR ?= arch_must_set_this
444UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
445UIMAGE_NAME ?= Linux-$(KERNELRELEASE)
446
447quiet_cmd_uimage = UIMAGE  $@
448      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
449			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
450			-T $(UIMAGE_TYPE) \
451			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
452			-n '$(UIMAGE_NAME)' -d $< $@
453
454# Flat Image Tree (FIT)
455# This allows for packaging of a kernel and all devicetrees files, using
456# compression.
457# ---------------------------------------------------------------------------
458
459MAKE_FIT := $(srctree)/scripts/make_fit.py
460
461# Use this to override the compression algorithm
462FIT_COMPRESSION ?= gzip
463
464quiet_cmd_fit = FIT     $@
465      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
466		--name '$(UIMAGE_NAME)' \
467		$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
468		$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
469		--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
470
471# XZ
472# ---------------------------------------------------------------------------
473# Use xzkern or xzkern_with_size to compress the kernel image and xzmisc to
474# compress other things.
475#
476# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
477# of the kernel decompressor. A BCJ filter is used if it is available for
478# the target architecture.
479#
480# xzkern_with_size also appends uncompressed size of the data using
481# size_append. The .xz format has the size information available at the end
482# of the file too, but it's in more complex format and it's good to avoid
483# changing the part of the boot code that reads the uncompressed size.
484# Note that the bytes added by size_append will make the xz tool think that
485# the file is corrupt. This is expected.
486#
487# xzmisc doesn't use size_append, so it can be used to create normal .xz
488# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
489# big dictionary would increase the memory usage too much in the multi-call
490# decompression mode. A BCJ filter isn't used either.
491quiet_cmd_xzkern = XZKERN  $@
492      cmd_xzkern = cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh > $@
493
494quiet_cmd_xzkern_with_size = XZKERN  $@
495      cmd_xzkern_with_size = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
496                     $(size_append); } > $@
497
498quiet_cmd_xzmisc = XZMISC  $@
499      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
500
501# ZSTD
502# ---------------------------------------------------------------------------
503# Appends the uncompressed size of the data using size_append. The .zst
504# format has the size information available at the beginning of the file too,
505# but it's in a more complex format and it's good to avoid changing the part
506# of the boot code that reads the uncompressed size.
507#
508# Note that the bytes added by size_append will make the zstd tool think that
509# the file is corrupt. This is expected.
510#
511# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
512# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
513# single pass, so zstd doesn't need to allocate a window buffer. When streaming
514# decompression is used, like initramfs decompression, zstd22 should likely not
515# be used because it would require zstd to allocate a 128 MB buffer.
516
517quiet_cmd_zstd = ZSTD    $@
518      cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
519
520quiet_cmd_zstd22 = ZSTD22  $@
521      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
522
523quiet_cmd_zstd22_with_size = ZSTD22  $@
524      cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
525
526# ASM offsets
527# ---------------------------------------------------------------------------
528
529# Default sed regexp - multiline due to syntax constraints
530#
531# Use [:space:] because LLVM's integrated assembler inserts <tab> around
532# the .ascii directive whereas GCC keeps the <space> as-is.
533define sed-offsets
534	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
535	/^->/{s:->#\(.*\):/* \1 */:; \
536	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
537	s:->::; p;}'
538endef
539
540# Use filechk to avoid rebuilds when a header changes, but the resulting file
541# does not
542define filechk_offsets
543	 echo "#ifndef $2"; \
544	 echo "#define $2"; \
545	 echo "/*"; \
546	 echo " * DO NOT MODIFY."; \
547	 echo " *"; \
548	 echo " * This file was generated by Kbuild"; \
549	 echo " */"; \
550	 echo ""; \
551	 sed -ne $(sed-offsets) < $<; \
552	 echo ""; \
553	 echo "#endif"
554endef
555