xref: /linux-6.15/scripts/Makefile.lib (revision ac4f0678)
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_SIGNED_WRAP_$(target-stem).o)$(UBSAN_SANITIZE_$(target-stem).o)$(UBSAN_SIGNED_WRAP)$(UBSAN_SANITIZE)$(is-kernel-object)), \
165		$(CFLAGS_UBSAN_SIGNED_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-$(CONFIG_GCOV_KERNEL)			+= --no-unreachable
274objtool-args-$(CONFIG_PREFIX_SYMBOLS)			+= --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
275
276objtool-args = $(objtool-args-y)					\
277	$(if $(delay-objtool), --link)					\
278	$(if $(part-of-module), --module)
279
280delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
281
282cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
283cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
284
285objtool-enabled := y
286
287endif # CONFIG_OBJTOOL
288
289# Useful for describing the dependency of composite objects
290# Usage:
291#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
292define multi_depend
293$(foreach m, $1, \
294	$(eval $m: \
295	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
296endef
297
298# Build commands
299# ===========================================================================
300# These are shared by some Makefile.* files.
301
302ifdef CONFIG_LTO_CLANG
303# Run $(LD) here to convert LLVM IR to ELF in the following cases:
304#  - when this object needs objtool processing, as objtool cannot process LLVM IR
305#  - when this is a single-object module, as modpost cannot process LLVM IR
306cmd_ld_single = $(if $(objtool-enabled)$(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
307endif
308
309quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
310      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
311		$(cmd_ld_single) \
312		$(cmd_objtool)
313
314define rule_cc_o_c
315	$(call cmd_and_fixdep,cc_o_c)
316	$(call cmd,checksrc)
317	$(call cmd,checkdoc)
318	$(call cmd,gen_objtooldep)
319	$(call cmd,gen_symversions_c)
320	$(call cmd,record_mcount)
321	$(call cmd,warn_shared_object)
322endef
323
324quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
325      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
326
327define rule_as_o_S
328	$(call cmd_and_fixdep,as_o_S)
329	$(call cmd,gen_objtooldep)
330	$(call cmd,gen_symversions_S)
331	$(call cmd,warn_shared_object)
332endef
333
334# Copy a file
335# ===========================================================================
336# 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
337# the copy would be read-only as well, leading to an error when executing the
338# rule next time. Use 'cat' instead in order to generate a writable file.
339quiet_cmd_copy = COPY    $@
340      cmd_copy = cat $< > $@
341
342$(obj)/%: $(src)/%_shipped
343	$(call cmd,copy)
344
345# Commands useful for building a boot image
346# ===========================================================================
347#
348#	Use as following:
349#
350#	target: source(s) FORCE
351#		$(if_changed,ld/objcopy/gzip)
352#
353#	and add target to 'targets' so that we know we have to
354#	read in the saved command line
355
356# Linking
357# ---------------------------------------------------------------------------
358
359quiet_cmd_ld = LD      $@
360      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
361
362# Archive
363# ---------------------------------------------------------------------------
364
365quiet_cmd_ar = AR      $@
366      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
367
368# Objcopy
369# ---------------------------------------------------------------------------
370
371quiet_cmd_objcopy = OBJCOPY $@
372cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
373
374# Gzip
375# ---------------------------------------------------------------------------
376
377quiet_cmd_gzip = GZIP    $@
378      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
379
380# Bzip2
381# ---------------------------------------------------------------------------
382
383# Bzip2 and LZMA do not include size in file... so we have to fake that;
384# append the size as a 32-bit littleendian number as gzip does.
385size_append = printf $(shell						\
386dec_size=0;								\
387for F in $(real-prereqs); do					\
388	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
389	dec_size=$$(expr $$dec_size + $$fsize);				\
390done;									\
391printf "%08x\n" $$dec_size |						\
392	sed 's/\(..\)/\1 /g' | {					\
393		read ch0 ch1 ch2 ch3;					\
394		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
395			printf '%s%03o' '\\' $$((0x$$ch)); 		\
396		done;							\
397	}								\
398)
399
400quiet_cmd_file_size = GEN     $@
401      cmd_file_size = $(size_append) > $@
402
403quiet_cmd_bzip2 = BZIP2   $@
404      cmd_bzip2 = cat $(real-prereqs) | $(KBZIP2) -9 > $@
405
406quiet_cmd_bzip2_with_size = BZIP2   $@
407      cmd_bzip2_with_size = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
408
409# Lzma
410# ---------------------------------------------------------------------------
411
412quiet_cmd_lzma = LZMA    $@
413      cmd_lzma = cat $(real-prereqs) | $(LZMA) -9 > $@
414
415quiet_cmd_lzma_with_size = LZMA    $@
416      cmd_lzma_with_size = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
417
418quiet_cmd_lzo = LZO     $@
419      cmd_lzo = cat $(real-prereqs) | $(KLZOP) -9 > $@
420
421quiet_cmd_lzo_with_size = LZO     $@
422      cmd_lzo_with_size = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
423
424quiet_cmd_lz4 = LZ4     $@
425      cmd_lz4 = cat $(real-prereqs) | $(LZ4) -l -9 - - > $@
426
427quiet_cmd_lz4_with_size = LZ4     $@
428      cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -9 - -; \
429                  $(size_append); } > $@
430
431# U-Boot mkimage
432# ---------------------------------------------------------------------------
433
434MKIMAGE := $(srctree)/scripts/mkuboot.sh
435
436# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
437# the number of overrides in arch makefiles
438UIMAGE_ARCH ?= $(SRCARCH)
439UIMAGE_COMPRESSION ?= $(or $(2),none)
440UIMAGE_OPTS-y ?=
441UIMAGE_TYPE ?= kernel
442UIMAGE_LOADADDR ?= arch_must_set_this
443UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
444UIMAGE_NAME ?= Linux-$(KERNELRELEASE)
445
446quiet_cmd_uimage = UIMAGE  $@
447      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
448			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
449			-T $(UIMAGE_TYPE) \
450			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
451			-n '$(UIMAGE_NAME)' -d $< $@
452
453# Flat Image Tree (FIT)
454# This allows for packaging of a kernel and all devicetrees files, using
455# compression.
456# ---------------------------------------------------------------------------
457
458MAKE_FIT := $(srctree)/scripts/make_fit.py
459
460# Use this to override the compression algorithm
461FIT_COMPRESSION ?= gzip
462
463quiet_cmd_fit = FIT     $@
464      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
465		--name '$(UIMAGE_NAME)' \
466		$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
467		$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
468		--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
469
470# XZ
471# ---------------------------------------------------------------------------
472# Use xzkern or xzkern_with_size to compress the kernel image and xzmisc to
473# compress other things.
474#
475# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
476# of the kernel decompressor. A BCJ filter is used if it is available for
477# the target architecture.
478#
479# xzkern_with_size also appends uncompressed size of the data using
480# size_append. The .xz format has the size information available at the end
481# of the file too, but it's in more complex format and it's good to avoid
482# changing the part of the boot code that reads the uncompressed size.
483# Note that the bytes added by size_append will make the xz tool think that
484# the file is corrupt. This is expected.
485#
486# xzmisc doesn't use size_append, so it can be used to create normal .xz
487# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
488# big dictionary would increase the memory usage too much in the multi-call
489# decompression mode. A BCJ filter isn't used either.
490quiet_cmd_xzkern = XZKERN  $@
491      cmd_xzkern = cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh > $@
492
493quiet_cmd_xzkern_with_size = XZKERN  $@
494      cmd_xzkern_with_size = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
495                     $(size_append); } > $@
496
497quiet_cmd_xzmisc = XZMISC  $@
498      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
499
500# ZSTD
501# ---------------------------------------------------------------------------
502# Appends the uncompressed size of the data using size_append. The .zst
503# format has the size information available at the beginning of the file too,
504# but it's in a more complex format and it's good to avoid changing the part
505# of the boot code that reads the uncompressed size.
506#
507# Note that the bytes added by size_append will make the zstd tool think that
508# the file is corrupt. This is expected.
509#
510# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
511# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
512# single pass, so zstd doesn't need to allocate a window buffer. When streaming
513# decompression is used, like initramfs decompression, zstd22 should likely not
514# be used because it would require zstd to allocate a 128 MB buffer.
515
516quiet_cmd_zstd = ZSTD    $@
517      cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
518
519quiet_cmd_zstd22 = ZSTD22  $@
520      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
521
522quiet_cmd_zstd22_with_size = ZSTD22  $@
523      cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
524
525# ASM offsets
526# ---------------------------------------------------------------------------
527
528# Default sed regexp - multiline due to syntax constraints
529#
530# Use [:space:] because LLVM's integrated assembler inserts <tab> around
531# the .ascii directive whereas GCC keeps the <space> as-is.
532define sed-offsets
533	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
534	/^->/{s:->#\(.*\):/* \1 */:; \
535	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
536	s:->::; p;}'
537endef
538
539# Use filechk to avoid rebuilds when a header changes, but the resulting file
540# does not
541define filechk_offsets
542	 echo "#ifndef $2"; \
543	 echo "#define $2"; \
544	 echo "/*"; \
545	 echo " * DO NOT MODIFY."; \
546	 echo " *"; \
547	 echo " * This file was generated by Kbuild"; \
548	 echo " */"; \
549	 echo ""; \
550	 sed -ne $(sed-offsets) < $<; \
551	 echo ""; \
552	 echo "#endif"
553endef
554