xref: /linux/scripts/Makefile.lib (revision 4fc012daf9c074772421c904357abf586336b1ca)
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))
172_rust_flags += $(if $(patsubst n%,, \
173	$(KCOV_INSTRUMENT_$(target-stem).o)$(KCOV_INSTRUMENT)$(if $(is-kernel-object),$(CONFIG_KCOV_INSTRUMENT_ALL))), \
174	$(RUSTFLAGS_KCOV))
175endif
176
177#
178# Enable KCSAN flags except some files or directories we don't want to check
179# (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
180#
181ifeq ($(CONFIG_KCSAN),y)
182_c_flags += $(if $(patsubst n%,, \
183	$(KCSAN_SANITIZE_$(target-stem).o)$(KCSAN_SANITIZE)$(is-kernel-object)), \
184	$(CFLAGS_KCSAN))
185# Some uninstrumented files provide implied barriers required to avoid false
186# positives: set KCSAN_INSTRUMENT_BARRIERS for barrier instrumentation only.
187_c_flags += $(if $(patsubst n%,, \
188	$(KCSAN_INSTRUMENT_BARRIERS_$(target-stem).o)$(KCSAN_INSTRUMENT_BARRIERS)n), \
189	-D__KCSAN_INSTRUMENT_BARRIERS__)
190endif
191
192#
193# Enable AutoFDO build flags except some files or directories we don't want to
194# enable (depends on variables AUTOFDO_PROFILE_obj.o and AUTOFDO_PROFILE).
195#
196ifeq ($(CONFIG_AUTOFDO_CLANG),y)
197_c_flags += $(if $(patsubst n%,, \
198	$(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(is-kernel-object)), \
199	$(CFLAGS_AUTOFDO_CLANG))
200endif
201
202#
203# Enable Propeller build flags except some files or directories we don't want to
204# enable (depends on variables AUTOFDO_PROPELLER_obj.o and PROPELLER_PROFILE).
205#
206ifdef CONFIG_PROPELLER_CLANG
207_c_flags += $(if $(patsubst n%,, \
208	$(AUTOFDO_PROFILE_$(target-stem).o)$(AUTOFDO_PROFILE)$(PROPELLER_PROFILE))$(is-kernel-object), \
209	$(CFLAGS_PROPELLER_CLANG))
210endif
211
212# $(src) for including checkin headers from generated source files
213# $(obj) for including generated headers from checkin source files
214ifdef building_out_of_srctree
215_c_flags   += $(addprefix -I, $(src) $(obj))
216_a_flags   += $(addprefix -I, $(src) $(obj))
217_cpp_flags += $(addprefix -I, $(src) $(obj))
218endif
219
220# If $(is-kernel-object) is 'y', this object will be linked to vmlinux or modules
221is-kernel-object = $(or $(part-of-builtin),$(part-of-module))
222
223part-of-builtin = $(if $(filter $(basename $@).o, $(real-obj-y) $(lib-y)),y)
224part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
225quiet_modtag = $(if $(part-of-module),[M],   )
226
227modkern_cflags =                                          \
228	$(if $(part-of-module),                           \
229		$(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
230		$(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
231
232modkern_rustflags =                                              \
233	$(if $(part-of-module),                                   \
234		$(KBUILD_RUSTFLAGS_MODULE) $(RUSTFLAGS_MODULE), \
235		$(KBUILD_RUSTFLAGS_KERNEL) $(RUSTFLAGS_KERNEL))
236
237modkern_aflags = $(if $(part-of-module),				\
238			$(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE),	\
239			$(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL) $(modfile_flags))
240
241c_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
242		 -include $(srctree)/include/linux/compiler_types.h       \
243		 $(_c_flags) $(modkern_cflags)                           \
244		 $(basename_flags) $(modname_flags)
245
246rust_flags     = $(_rust_flags) $(modkern_rustflags) @$(objtree)/include/generated/rustc_cfg
247
248a_flags        = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
249		 $(_a_flags) $(modkern_aflags) $(modname_flags)
250
251cpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
252		 $(_cpp_flags)
253
254ld_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
255
256ifdef CONFIG_OBJTOOL
257
258objtool := $(objtree)/tools/objtool/objtool
259
260objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK)		+= --hacks=jump_label
261objtool-args-$(CONFIG_HAVE_NOINSTR_HACK)		+= --hacks=noinstr
262objtool-args-$(CONFIG_MITIGATION_CALL_DEPTH_TRACKING)	+= --hacks=skylake
263objtool-args-$(CONFIG_X86_KERNEL_IBT)			+= --ibt
264objtool-args-$(CONFIG_FINEIBT)				+= --cfi
265objtool-args-$(CONFIG_FTRACE_MCOUNT_USE_OBJTOOL)	+= --mcount
266ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL
267objtool-args-$(CONFIG_HAVE_OBJTOOL_NOP_MCOUNT)		+= --mnop
268endif
269objtool-args-$(CONFIG_UNWINDER_ORC)			+= --orc
270objtool-args-$(CONFIG_MITIGATION_RETPOLINE)		+= --retpoline
271objtool-args-$(CONFIG_MITIGATION_RETHUNK)		+= --rethunk
272objtool-args-$(CONFIG_MITIGATION_SLS)			+= --sls
273objtool-args-$(CONFIG_STACK_VALIDATION)			+= --stackval
274objtool-args-$(CONFIG_HAVE_STATIC_CALL_INLINE)		+= --static-call
275objtool-args-$(CONFIG_HAVE_UACCESS_VALIDATION)		+= --uaccess
276objtool-args-$(or $(CONFIG_GCOV_KERNEL),$(CONFIG_KCOV))	+= --no-unreachable
277objtool-args-$(CONFIG_PREFIX_SYMBOLS)			+= --prefix=$(CONFIG_FUNCTION_PADDING_BYTES)
278objtool-args-$(CONFIG_OBJTOOL_WERROR)			+= --Werror
279
280objtool-args = $(objtool-args-y)					\
281	$(if $(delay-objtool), --link)					\
282	$(if $(part-of-module), --module)
283
284delay-objtool := $(or $(CONFIG_LTO_CLANG),$(CONFIG_X86_KERNEL_IBT))
285
286cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool-args) $@)
287cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd)
288
289objtool-enabled := y
290
291endif # CONFIG_OBJTOOL
292
293# Useful for describing the dependency of composite objects
294# Usage:
295#   $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
296define multi_depend
297$(foreach m, $1, \
298	$(eval $m: \
299	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
300endef
301
302# Remove ".." and "." from a path, without using "realpath"
303# Usage:
304#   $(call normalize_path,path/to/../file)
305define normalize_path
306$(strip $(eval elements :=) \
307$(foreach elem,$(subst /, ,$1), \
308	$(if $(filter-out .,$(elem)), \
309	     $(if $(filter ..,$(elem)), \
310		  $(eval elements := $(wordlist 2,$(words $(elements)),x $(elements))), \
311		  $(eval elements := $(elements) $(elem))))) \
312$(subst $(space),/,$(elements)))
313endef
314
315# Build commands
316# ===========================================================================
317# These are shared by some Makefile.* files.
318
319ifdef CONFIG_LTO_CLANG
320# Run $(LD) here to convert LLVM IR to ELF in the following cases:
321#  - when this object needs objtool processing, as objtool cannot process LLVM IR
322#  - when this is a single-object module, as modpost cannot process LLVM IR
323cmd_ld_single = $(if $(objtool-enabled)$(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
324endif
325
326quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
327      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
328		$(cmd_ld_single) \
329		$(cmd_objtool)
330
331define rule_cc_o_c
332	$(call cmd_and_fixdep,cc_o_c)
333	$(call cmd,checksrc)
334	$(call cmd,checkdoc)
335	$(call cmd,gen_objtooldep)
336	$(call cmd,gen_symversions_c)
337	$(call cmd,record_mcount)
338	$(call cmd,warn_shared_object)
339endef
340
341quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
342      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
343
344define rule_as_o_S
345	$(call cmd_and_fixdep,as_o_S)
346	$(call cmd,gen_objtooldep)
347	$(call cmd,gen_symversions_S)
348	$(call cmd,warn_shared_object)
349endef
350
351# Copy a file
352# ===========================================================================
353# 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
354# the copy would be read-only as well, leading to an error when executing the
355# rule next time. Use 'cat' instead in order to generate a writable file.
356quiet_cmd_copy = COPY    $@
357      cmd_copy = cat $< > $@
358
359$(obj)/%: $(src)/%_shipped
360	$(call cmd,copy)
361
362# Touch a file
363# ===========================================================================
364quiet_cmd_touch = TOUCH   $(call normalize_path,$@)
365      cmd_touch = touch $@
366
367# Commands useful for building a boot image
368# ===========================================================================
369#
370#	Use as following:
371#
372#	target: source(s) FORCE
373#		$(if_changed,ld/objcopy/gzip)
374#
375#	and add target to 'targets' so that we know we have to
376#	read in the saved command line
377
378# Linking
379# ---------------------------------------------------------------------------
380
381quiet_cmd_ld = LD      $@
382      cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
383
384# Archive
385# ---------------------------------------------------------------------------
386
387quiet_cmd_ar = AR      $@
388      cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
389
390# Objcopy
391# ---------------------------------------------------------------------------
392
393quiet_cmd_objcopy = OBJCOPY $@
394cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
395
396# Gzip
397# ---------------------------------------------------------------------------
398
399quiet_cmd_gzip = GZIP    $@
400      cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
401
402# Bzip2
403# ---------------------------------------------------------------------------
404
405# Bzip2 and LZMA do not include size in file... so we have to fake that;
406# append the size as a 32-bit littleendian number as gzip does.
407size_append = printf $(shell						\
408dec_size=0;								\
409for F in $(real-prereqs); do					\
410	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
411	dec_size=$$(expr $$dec_size + $$fsize);				\
412done;									\
413printf "%08x\n" $$dec_size |						\
414	sed 's/\(..\)/\1 /g' | {					\
415		read ch0 ch1 ch2 ch3;					\
416		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
417			printf '%s%03o' '\\' $$((0x$$ch)); 		\
418		done;							\
419	}								\
420)
421
422quiet_cmd_file_size = GEN     $@
423      cmd_file_size = $(size_append) > $@
424
425quiet_cmd_bzip2 = BZIP2   $@
426      cmd_bzip2 = cat $(real-prereqs) | $(KBZIP2) -9 > $@
427
428quiet_cmd_bzip2_with_size = BZIP2   $@
429      cmd_bzip2_with_size = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
430
431# Lzma
432# ---------------------------------------------------------------------------
433
434quiet_cmd_lzma = LZMA    $@
435      cmd_lzma = cat $(real-prereqs) | $(LZMA) -9 > $@
436
437quiet_cmd_lzma_with_size = LZMA    $@
438      cmd_lzma_with_size = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
439
440quiet_cmd_lzo = LZO     $@
441      cmd_lzo = cat $(real-prereqs) | $(KLZOP) -9 > $@
442
443quiet_cmd_lzo_with_size = LZO     $@
444      cmd_lzo_with_size = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
445
446quiet_cmd_lz4 = LZ4     $@
447      cmd_lz4 = cat $(real-prereqs) | $(LZ4) -l -9 - - > $@
448
449quiet_cmd_lz4_with_size = LZ4     $@
450      cmd_lz4_with_size = { cat $(real-prereqs) | $(LZ4) -l -9 - -; \
451                  $(size_append); } > $@
452
453# U-Boot mkimage
454# ---------------------------------------------------------------------------
455
456MKIMAGE := $(srctree)/scripts/mkuboot.sh
457
458# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
459# the number of overrides in arch makefiles
460UIMAGE_ARCH ?= $(SRCARCH)
461UIMAGE_COMPRESSION ?= $(or $(2),none)
462UIMAGE_OPTS-y ?=
463UIMAGE_TYPE ?= kernel
464UIMAGE_LOADADDR ?= arch_must_set_this
465UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
466UIMAGE_NAME ?= Linux-$(KERNELRELEASE)
467
468quiet_cmd_uimage = UIMAGE  $@
469      cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
470			-C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
471			-T $(UIMAGE_TYPE) \
472			-a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
473			-n '$(UIMAGE_NAME)' -d $< $@
474
475# Flat Image Tree (FIT)
476# This allows for packaging of a kernel and all devicetrees files, using
477# compression.
478# ---------------------------------------------------------------------------
479
480MAKE_FIT := $(srctree)/scripts/make_fit.py
481
482# Use this to override the compression algorithm
483FIT_COMPRESSION ?= gzip
484
485quiet_cmd_fit = FIT     $@
486      cmd_fit = $(MAKE_FIT) -o $@ --arch $(UIMAGE_ARCH) --os linux \
487		--name '$(UIMAGE_NAME)' \
488		$(if $(findstring 1,$(KBUILD_VERBOSE)),-v) \
489		$(if $(FIT_DECOMPOSE_DTBS),--decompose-dtbs) \
490		--compress $(FIT_COMPRESSION) -k $< @$(word 2,$^)
491
492# XZ
493# ---------------------------------------------------------------------------
494# Use xzkern or xzkern_with_size to compress the kernel image and xzmisc to
495# compress other things.
496#
497# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
498# of the kernel decompressor. A BCJ filter is used if it is available for
499# the target architecture.
500#
501# xzkern_with_size also appends uncompressed size of the data using
502# size_append. The .xz format has the size information available at the end
503# of the file too, but it's in more complex format and it's good to avoid
504# changing the part of the boot code that reads the uncompressed size.
505# Note that the bytes added by size_append will make the xz tool think that
506# the file is corrupt. This is expected.
507#
508# xzmisc doesn't use size_append, so it can be used to create normal .xz
509# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
510# big dictionary would increase the memory usage too much in the multi-call
511# decompression mode. A BCJ filter isn't used either.
512quiet_cmd_xzkern = XZKERN  $@
513      cmd_xzkern = cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh > $@
514
515quiet_cmd_xzkern_with_size = XZKERN  $@
516      cmd_xzkern_with_size = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
517                     $(size_append); } > $@
518
519quiet_cmd_xzmisc = XZMISC  $@
520      cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
521
522# ZSTD
523# ---------------------------------------------------------------------------
524# Appends the uncompressed size of the data using size_append. The .zst
525# format has the size information available at the beginning of the file too,
526# but it's in a more complex format and it's good to avoid changing the part
527# of the boot code that reads the uncompressed size.
528#
529# Note that the bytes added by size_append will make the zstd tool think that
530# the file is corrupt. This is expected.
531#
532# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
533# 128 MB. zstd22 is used for kernel compression because it is decompressed in a
534# single pass, so zstd doesn't need to allocate a window buffer. When streaming
535# decompression is used, like initramfs decompression, zstd22 should likely not
536# be used because it would require zstd to allocate a 128 MB buffer.
537
538quiet_cmd_zstd = ZSTD    $@
539      cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
540
541quiet_cmd_zstd22 = ZSTD22  $@
542      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
543
544quiet_cmd_zstd22_with_size = ZSTD22  $@
545      cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
546
547# ASM offsets
548# ---------------------------------------------------------------------------
549
550# Default sed regexp - multiline due to syntax constraints
551#
552# Use [:space:] because LLVM's integrated assembler inserts <tab> around
553# the .ascii directive whereas GCC keeps the <space> as-is.
554define sed-offsets
555	's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
556	/^->/{s:->#\(.*\):/* \1 */:; \
557	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
558	s:->::; p;}'
559endef
560
561# Use filechk to avoid rebuilds when a header changes, but the resulting file
562# does not
563define filechk_offsets
564	 echo "#ifndef $2"; \
565	 echo "#define $2"; \
566	 echo "/*"; \
567	 echo " * DO NOT MODIFY."; \
568	 echo " *"; \
569	 echo " * This file was generated by Kbuild"; \
570	 echo " */"; \
571	 echo ""; \
572	 sed -ne $(sed-offsets) < $<; \
573	 echo ""; \
574	 echo "#endif"
575endef
576