1 # SPDX-License-Identifier: GPL-2.0
2 # Backward compatibility
3 asflags-y += $(EXTRA_AFLAGS)
4 ccflags-y += $(EXTRA_CFLAGS)
5 cppflags-y += $(EXTRA_CPPFLAGS)
6 ldflags-y += $(EXTRA_LDFLAGS)
8 $(warning 'always' is deprecated. Please use 'always-y' instead)
11 ifneq ($(hostprogs-y),)
12 $(warning 'hostprogs-y' is deprecated. Please use 'hostprogs' instead)
13 hostprogs += $(hostprogs-y)
15 ifneq ($(hostprogs-m),)
16 $(warning 'hostprogs-m' is deprecated. Please use 'hostprogs' instead)
17 hostprogs += $(hostprogs-m)
20 # flags that take effect in current and sub directories
21 KBUILD_AFLAGS += $(subdir-asflags-y)
22 KBUILD_CFLAGS += $(subdir-ccflags-y)
24 # Figure out what we need to build from the various variables
25 # ===========================================================================
27 # When an object is listed to be built compiled-in and modular,
28 # only build the compiled-in version
29 obj-m := $(filter-out $(obj-y),$(obj-m))
31 # Libraries are always collected in one lib file.
32 # Filter out objects already built-in
33 lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m)))
35 # Subdirectories we need to descend into
36 subdir-ym := $(sort $(subdir-y) $(subdir-m) \
37 $(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m))))
39 # Handle objects in subdirs:
40 # - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and
42 # - If we encounter foo/ in $(obj-m), replace it by foo/modules.order
44 # Generate modules.order to determine modorder. Unfortunately, we don't have
45 # information about ordering between -y and -m subdirs. Just put -y's first.
48 obj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m))
50 obj-m := $(filter-out %/, $(obj-m))
54 obj-y := $(patsubst %/, %/built-in.a, $(obj-y))
56 obj-y := $(filter-out %/, $(obj-y))
59 # If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object
60 multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))), $(m))))
61 multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))), $(m))))
62 multi-used := $(multi-used-y) $(multi-used-m)
64 # Replace multi-part objects by their individual parts,
65 # including built-in.a from subdirectories
66 real-obj-y := $(foreach m, $(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m)))
67 real-obj-m := $(foreach m, $(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)),$(m)))
69 always-y += $(always-m)
71 # hostprogs-always-y += foo
72 # ... is a shorthand for
75 hostprogs += $(hostprogs-always-y) $(hostprogs-always-m)
76 always-y += $(hostprogs-always-y) $(hostprogs-always-m)
78 # userprogs-always-y is likewise.
79 userprogs += $(userprogs-always-y) $(userprogs-always-m)
80 always-y += $(userprogs-always-y) $(userprogs-always-m)
83 # If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built
85 extra-$(CONFIG_OF_ALL_DTBS) += $(dtb-)
87 ifneq ($(CHECK_DTBS),)
88 extra-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y))
89 extra-y += $(patsubst %.dtbo,%.dt.yaml, $(dtb-y))
90 extra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-))
91 extra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtbo,%.dt.yaml, $(dtb-))
96 extra-y := $(addprefix $(obj)/,$(extra-y))
97 always-y := $(addprefix $(obj)/,$(always-y))
98 targets := $(addprefix $(obj)/,$(targets))
99 obj-m := $(addprefix $(obj)/,$(obj-m))
100 lib-y := $(addprefix $(obj)/,$(lib-y))
101 real-obj-y := $(addprefix $(obj)/,$(real-obj-y))
102 real-obj-m := $(addprefix $(obj)/,$(real-obj-m))
103 multi-used-m := $(addprefix $(obj)/,$(multi-used-m))
104 subdir-ym := $(addprefix $(obj)/,$(subdir-ym))
106 # Finds the multi-part object the current object will be linked into.
107 # If the object belongs to two or more multi-part objects, list them all.
108 modname-multi = $(sort $(foreach m,$(multi-used),\
109 $(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=))))
111 __modname = $(if $(modname-multi),$(modname-multi),$(basetarget))
113 modname = $(subst $(space),:,$(__modname))
114 modfile = $(addprefix $(obj)/,$(__modname))
116 # target with $(obj)/ and its suffix stripped
117 target-stem = $(basename $(patsubst $(obj)/%,%,$@))
119 # These flags are needed for modversions and compiling, so we define them here
120 # $(modname_flags) defines KBUILD_MODNAME as the name of the module it will
121 # end up in (or would, if it gets compiled in)
122 name-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1)))
123 basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget))
124 modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname))
125 modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))
127 _c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
128 $(filter-out $(ccflags-remove-y), \
129 $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
130 $(CFLAGS_$(target-stem).o))
131 _a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
132 $(filter-out $(asflags-remove-y), \
133 $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
134 $(AFLAGS_$(target-stem).o))
135 _cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
138 # Enable gcov profiling flags for a file, directory or for all files depending
139 # on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL
142 ifeq ($(CONFIG_GCOV_KERNEL),y)
143 _c_flags += $(if $(patsubst n%,, \
144 $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \
149 # Enable address sanitizer flags for kernel except some files or directories
150 # we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE)
152 ifeq ($(CONFIG_KASAN),y)
153 ifneq ($(CONFIG_KASAN_HW_TAGS),y)
154 _c_flags += $(if $(patsubst n%,, \
155 $(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \
156 $(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
160 ifeq ($(CONFIG_UBSAN),y)
161 _c_flags += $(if $(patsubst n%,, \
162 $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \
166 ifeq ($(CONFIG_KCOV),y)
167 _c_flags += $(if $(patsubst n%,, \
168 $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \
173 # Enable KCSAN flags except some files or directories we don't want to check
174 # (depends on variables KCSAN_SANITIZE_obj.o, KCSAN_SANITIZE)
176 ifeq ($(CONFIG_KCSAN),y)
177 _c_flags += $(if $(patsubst n%,, \
178 $(KCSAN_SANITIZE_$(basetarget).o)$(KCSAN_SANITIZE)y), \
182 # $(srctree)/$(src) for including checkin headers from generated source files
183 # $(objtree)/$(obj) for including generated headers from checkin source files
184 ifeq ($(KBUILD_EXTMOD),)
185 ifdef building_out_of_srctree
186 _c_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
187 _a_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
188 _cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj)
192 part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y)
193 quiet_modtag = $(if $(part-of-module),[M], )
196 $(if $(part-of-module), \
197 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \
198 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags))
200 modkern_aflags = $(if $(part-of-module), \
201 $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE), \
202 $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL))
204 c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
205 -include $(srctree)/include/linux/compiler_types.h \
206 $(_c_flags) $(modkern_cflags) \
207 $(basename_flags) $(modname_flags)
209 a_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
210 $(_a_flags) $(modkern_aflags)
212 cpp_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
215 ld_flags = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
217 DTC_INCLUDE := $(srctree)/scripts/dtc/include-prefixes
219 dtc_cpp_flags = -Wp,-MMD,$(depfile).pre.tmp -nostdinc \
220 $(addprefix -I,$(DTC_INCLUDE)) \
223 # Useful for describing the dependency of composite objects
225 # $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add)
227 $(foreach m, $(notdir $1), \
229 $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s)))))))
232 quiet_cmd_copy = COPY $@
236 # ===========================================================================
238 quiet_cmd_shipped = SHIPPED $@
239 cmd_shipped = cat $< > $@
241 $(obj)/%: $(src)/%_shipped
244 # Commands useful for building a boot image
245 # ===========================================================================
249 # target: source(s) FORCE
250 # $(if_changed,ld/objcopy/gzip)
252 # and add target to extra-y so that we know we have to
253 # read in the saved command line
256 # ---------------------------------------------------------------------------
259 cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@
262 # ---------------------------------------------------------------------------
265 cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs)
268 # ---------------------------------------------------------------------------
270 quiet_cmd_objcopy = OBJCOPY $@
271 cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@
274 # ---------------------------------------------------------------------------
276 quiet_cmd_gzip = GZIP $@
277 cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@
280 # ---------------------------------------------------------------------------
281 DTC ?= $(objtree)/scripts/dtc/dtc
282 DTC_FLAGS += -Wno-interrupt_provider
284 # Disable noisy checks by default
285 ifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
286 DTC_FLAGS += -Wno-unit_address_vs_reg \
287 -Wno-unit_address_format \
288 -Wno-avoid_unnecessary_addr_size \
290 -Wno-graph_child_address \
291 -Wno-simple_bus_reg \
292 -Wno-unique_unit_address \
296 ifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),)
297 DTC_FLAGS += -Wnode_name_chars_strict \
298 -Wproperty_name_chars_strict \
302 DTC_FLAGS += $(DTC_FLAGS_$(basetarget))
304 # Generate an assembly file to wrap the output of the device tree compiler
305 quiet_cmd_dt_S_dtb= DTB $@
308 echo '\#include <asm-generic/vmlinux.lds.h>'; \
309 echo '.section .dtb.init.rodata,"a"'; \
310 echo '.balign STRUCT_ALIGNMENT'; \
311 echo '.global __dtb_$(subst -,_,$(*F))_begin'; \
312 echo '__dtb_$(subst -,_,$(*F))_begin:'; \
313 echo '.incbin "$<" '; \
314 echo '__dtb_$(subst -,_,$(*F))_end:'; \
315 echo '.global __dtb_$(subst -,_,$(*F))_end'; \
316 echo '.balign STRUCT_ALIGNMENT'; \
319 $(obj)/%.dtb.S: $(obj)/%.dtb FORCE
320 $(call if_changed,dt_S_dtb)
322 quiet_cmd_dtc = DTC $@
323 cmd_dtc = $(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \
324 $(DTC) -O $(patsubst .%,%,$(suffix $@)) -o $@ -b 0 \
325 $(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \
326 -d $(depfile).dtc.tmp $(dtc-tmp) ; \
327 cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile)
329 $(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE
330 $(call if_changed_dep,dtc)
332 $(obj)/%.dtbo: $(src)/%.dts $(DTC) FORCE
333 $(call if_changed_dep,dtc)
335 DT_CHECKER ?= dt-validate
336 DT_BINDING_DIR := Documentation/devicetree/bindings
337 # DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile
338 DT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.json
340 quiet_cmd_dtb_check = CHECK $@
341 cmd_dtb_check = $(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@
344 $(call cmd_and_fixdep,dtc)
345 $(call cmd,dtb_check)
348 $(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE
349 $(call if_changed_rule,dtc,yaml)
351 dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
354 # ---------------------------------------------------------------------------
356 # Bzip2 and LZMA do not include size in file... so we have to fake that;
357 # append the size as a 32-bit littleendian number as gzip does.
358 size_append = printf $(shell \
360 for F in $(real-prereqs); do \
361 fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \
362 dec_size=$$(expr $$dec_size + $$fsize); \
364 printf "%08x\n" $$dec_size | \
365 sed 's/\(..\)/\1 /g' | { \
366 read ch0 ch1 ch2 ch3; \
367 for ch in $$ch3 $$ch2 $$ch1 $$ch0; do \
368 printf '%s%03o' '\\' $$((0x$$ch)); \
373 quiet_cmd_bzip2 = BZIP2 $@
374 cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@
377 # ---------------------------------------------------------------------------
379 quiet_cmd_lzma = LZMA $@
380 cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@
382 quiet_cmd_lzo = LZO $@
383 cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@
385 quiet_cmd_lz4 = LZ4 $@
386 cmd_lz4 = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \
387 $(size_append); } > $@
390 # ---------------------------------------------------------------------------
392 MKIMAGE := $(srctree)/scripts/mkuboot.sh
394 # SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces
395 # the number of overrides in arch makefiles
396 UIMAGE_ARCH ?= $(SRCARCH)
397 UIMAGE_COMPRESSION ?= $(if $(2),$(2),none)
399 UIMAGE_TYPE ?= kernel
400 UIMAGE_LOADADDR ?= arch_must_set_this
401 UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR)
402 UIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)'
404 quiet_cmd_uimage = UIMAGE $@
405 cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \
406 -C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \
408 -a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \
409 -n $(UIMAGE_NAME) -d $< $@
412 # ---------------------------------------------------------------------------
413 # Use xzkern to compress the kernel image and xzmisc to compress other things.
415 # xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage
416 # of the kernel decompressor. A BCJ filter is used if it is available for
417 # the target architecture. xzkern also appends uncompressed size of the data
418 # using size_append. The .xz format has the size information available at
419 # the end of the file too, but it's in more complex format and it's good to
420 # avoid changing the part of the boot code that reads the uncompressed size.
421 # Note that the bytes added by size_append will make the xz tool think that
422 # the file is corrupt. This is expected.
424 # xzmisc doesn't use size_append, so it can be used to create normal .xz
425 # files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very
426 # big dictionary would increase the memory usage too much in the multi-call
427 # decompression mode. A BCJ filter isn't used either.
428 quiet_cmd_xzkern = XZKERN $@
429 cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \
430 $(size_append); } > $@
432 quiet_cmd_xzmisc = XZMISC $@
433 cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@
436 # ---------------------------------------------------------------------------
437 # Appends the uncompressed size of the data using size_append. The .zst
438 # format has the size information available at the beginning of the file too,
439 # but it's in a more complex format and it's good to avoid changing the part
440 # of the boot code that reads the uncompressed size.
442 # Note that the bytes added by size_append will make the zstd tool think that
443 # the file is corrupt. This is expected.
445 # zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of
446 # 128 MB. zstd22 is used for kernel compression because it is decompressed in a
447 # single pass, so zstd doesn't need to allocate a window buffer. When streaming
448 # decompression is used, like initramfs decompression, zstd22 should likely not
449 # be used because it would require zstd to allocate a 128 MB buffer.
451 quiet_cmd_zstd = ZSTD $@
452 cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@
454 quiet_cmd_zstd22 = ZSTD22 $@
455 cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
458 # ---------------------------------------------------------------------------
460 # Default sed regexp - multiline due to syntax constraints
462 # Use [:space:] because LLVM's integrated assembler inserts <tab> around
463 # the .ascii directive whereas GCC keeps the <space> as-is.
465 's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \
466 /^->/{s:->#\(.*\):/* \1 */:; \
467 s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
471 # Use filechk to avoid rebuilds when a header changes, but the resulting file
473 define filechk_offsets
477 echo " * DO NOT MODIFY."; \
479 echo " * This file was generated by Kbuild"; \
482 sed -ne $(sed-offsets) < $<; \