1# SPDX-License-Identifier: GPL-2.0 2# Backward compatibility 3asflags-y += $(EXTRA_AFLAGS) 4ccflags-y += $(EXTRA_CFLAGS) 5cppflags-y += $(EXTRA_CPPFLAGS) 6ldflags-y += $(EXTRA_LDFLAGS) 7ifneq ($(always),) 8$(warning 'always' is deprecated. Please use 'always-y' instead) 9always-y += $(always) 10endif 11ifneq ($(hostprogs-y),) 12$(warning 'hostprogs-y' is deprecated. Please use 'hostprogs' instead) 13hostprogs += $(hostprogs-y) 14endif 15ifneq ($(hostprogs-m),) 16$(warning 'hostprogs-m' is deprecated. Please use 'hostprogs' instead) 17hostprogs += $(hostprogs-m) 18endif 19 20# flags that take effect in current and sub directories 21KBUILD_AFLAGS += $(subdir-asflags-y) 22KBUILD_CFLAGS += $(subdir-ccflags-y) 23 24# Figure out what we need to build from the various variables 25# =========================================================================== 26 27# When an object is listed to be built compiled-in and modular, 28# only build the compiled-in version 29obj-m := $(filter-out $(obj-y),$(obj-m)) 30 31# Libraries are always collected in one lib file. 32# Filter out objects already built-in 33lib-y := $(filter-out $(obj-y), $(sort $(lib-y) $(lib-m))) 34 35# Subdirectories we need to descend into 36subdir-ym := $(sort $(subdir-y) $(subdir-m) \ 37 $(patsubst %/,%, $(filter %/, $(obj-y) $(obj-m)))) 38 39# Handle objects in subdirs: 40# - If we encounter foo/ in $(obj-y), replace it by foo/built-in.a and 41# foo/modules.order 42# - If we encounter foo/ in $(obj-m), replace it by foo/modules.order 43# 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. 46 47ifdef need-modorder 48obj-m := $(patsubst %/,%/modules.order, $(filter %/, $(obj-y)) $(obj-m)) 49else 50obj-m := $(filter-out %/, $(obj-m)) 51endif 52 53ifdef need-builtin 54obj-y := $(patsubst %/, %/built-in.a, $(obj-y)) 55else 56obj-y := $(filter-out %/, $(obj-y)) 57endif 58 59# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object 60multi-used-y := $(sort $(foreach m,$(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))), $(m)))) 61multi-used-m := $(sort $(foreach m,$(obj-m), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m)) $($(m:.o=-))), $(m)))) 62multi-used := $(multi-used-y) $(multi-used-m) 63 64# Replace multi-part objects by their individual parts, 65# including built-in.a from subdirectories 66real-obj-y := $(foreach m, $(obj-y), $(if $(strip $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-))),$($(m:.o=-objs)) $($(m:.o=-y)),$(m))) 67real-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))) 68 69always-y += $(always-m) 70 71# hostprogs-always-y += foo 72# ... is a shorthand for 73# hostprogs += foo 74# always-y += foo 75hostprogs += $(hostprogs-always-y) $(hostprogs-always-m) 76always-y += $(hostprogs-always-y) $(hostprogs-always-m) 77 78# userprogs-always-y is likewise. 79userprogs += $(userprogs-always-y) $(userprogs-always-m) 80always-y += $(userprogs-always-y) $(userprogs-always-m) 81 82# DTB 83# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built 84extra-y += $(dtb-y) 85extra-$(CONFIG_OF_ALL_DTBS) += $(dtb-) 86 87ifneq ($(CHECK_DTBS),) 88extra-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y)) 89extra-y += $(patsubst %.dtbo,%.dt.yaml, $(dtb-y)) 90extra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-)) 91extra-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtbo,%.dt.yaml, $(dtb-)) 92endif 93 94# Add subdir path 95 96extra-y := $(addprefix $(obj)/,$(extra-y)) 97always-y := $(addprefix $(obj)/,$(always-y)) 98targets := $(addprefix $(obj)/,$(targets)) 99obj-m := $(addprefix $(obj)/,$(obj-m)) 100lib-y := $(addprefix $(obj)/,$(lib-y)) 101real-obj-y := $(addprefix $(obj)/,$(real-obj-y)) 102real-obj-m := $(addprefix $(obj)/,$(real-obj-m)) 103multi-used-m := $(addprefix $(obj)/,$(multi-used-m)) 104subdir-ym := $(addprefix $(obj)/,$(subdir-ym)) 105 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. 108modname-multi = $(sort $(foreach m,$(multi-used),\ 109 $(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=)))) 110 111__modname = $(if $(modname-multi),$(modname-multi),$(basetarget)) 112 113modname = $(subst $(space),:,$(__modname)) 114modfile = $(addprefix $(obj)/,$(__modname)) 115 116# target with $(obj)/ and its suffix stripped 117target-stem = $(basename $(patsubst $(obj)/%,%,$@)) 118 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) 122name-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1))) 123basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget)) 124modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) 125modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile)) 126 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) 136 137# 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 140# (in this order) 141# 142ifeq ($(CONFIG_GCOV_KERNEL),y) 143_c_flags += $(if $(patsubst n%,, \ 144 $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \ 145 $(CFLAGS_GCOV)) 146endif 147 148# 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) 151# 152ifeq ($(CONFIG_KASAN),y) 153ifneq ($(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)) 157endif 158endif 159 160ifeq ($(CONFIG_UBSAN),y) 161_c_flags += $(if $(patsubst n%,, \ 162 $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \ 163 $(CFLAGS_UBSAN)) 164endif 165 166ifeq ($(CONFIG_KCOV),y) 167_c_flags += $(if $(patsubst n%,, \ 168 $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(CONFIG_KCOV_INSTRUMENT_ALL)), \ 169 $(CFLAGS_KCOV)) 170endif 171 172# 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) 175# 176ifeq ($(CONFIG_KCSAN),y) 177_c_flags += $(if $(patsubst n%,, \ 178 $(KCSAN_SANITIZE_$(basetarget).o)$(KCSAN_SANITIZE)y), \ 179 $(CFLAGS_KCSAN)) 180endif 181 182# $(srctree)/$(src) for including checkin headers from generated source files 183# $(objtree)/$(obj) for including generated headers from checkin source files 184ifeq ($(KBUILD_EXTMOD),) 185ifdef 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) 189endif 190endif 191 192part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y) 193quiet_modtag = $(if $(part-of-module),[M], ) 194 195modkern_cflags = \ 196 $(if $(part-of-module), \ 197 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \ 198 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags)) 199 200modkern_aflags = $(if $(part-of-module), \ 201 $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE), \ 202 $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)) 203 204c_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) 208 209a_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ 210 $(_a_flags) $(modkern_aflags) 211 212cpp_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ 213 $(_cpp_flags) 214 215ld_flags = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) 216 217DTC_INCLUDE := $(srctree)/scripts/dtc/include-prefixes 218 219dtc_cpp_flags = -Wp,-MMD,$(depfile).pre.tmp -nostdinc \ 220 $(addprefix -I,$(DTC_INCLUDE)) \ 221 -undef -D__DTS__ 222 223# Useful for describing the dependency of composite objects 224# Usage: 225# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add) 226define multi_depend 227$(foreach m, $(notdir $1), \ 228 $(eval $(obj)/$m: \ 229 $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s))))))) 230endef 231 232quiet_cmd_copy = COPY $@ 233 cmd_copy = cp $< $@ 234 235# Shipped files 236# =========================================================================== 237 238quiet_cmd_shipped = SHIPPED $@ 239cmd_shipped = cat $< > $@ 240 241$(obj)/%: $(src)/%_shipped 242 $(call cmd,shipped) 243 244# Commands useful for building a boot image 245# =========================================================================== 246# 247# Use as following: 248# 249# target: source(s) FORCE 250# $(if_changed,ld/objcopy/gzip) 251# 252# and add target to extra-y so that we know we have to 253# read in the saved command line 254 255# Linking 256# --------------------------------------------------------------------------- 257 258quiet_cmd_ld = LD $@ 259 cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@ 260 261# Archive 262# --------------------------------------------------------------------------- 263 264quiet_cmd_ar = AR $@ 265 cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs) 266 267# Objcopy 268# --------------------------------------------------------------------------- 269 270quiet_cmd_objcopy = OBJCOPY $@ 271cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@ 272 273# Gzip 274# --------------------------------------------------------------------------- 275 276quiet_cmd_gzip = GZIP $@ 277 cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@ 278 279# DTC 280# --------------------------------------------------------------------------- 281DTC ?= $(objtree)/scripts/dtc/dtc 282DTC_FLAGS += -Wno-interrupt_provider 283 284# Disable noisy checks by default 285ifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),) 286DTC_FLAGS += -Wno-unit_address_vs_reg \ 287 -Wno-unit_address_format \ 288 -Wno-avoid_unnecessary_addr_size \ 289 -Wno-alias_paths \ 290 -Wno-graph_child_address \ 291 -Wno-simple_bus_reg \ 292 -Wno-unique_unit_address \ 293 -Wno-pci_device_reg 294endif 295 296ifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),) 297DTC_FLAGS += -Wnode_name_chars_strict \ 298 -Wproperty_name_chars_strict \ 299 -Winterrupt_provider 300endif 301 302DTC_FLAGS += $(DTC_FLAGS_$(basetarget)) 303 304# Generate an assembly file to wrap the output of the device tree compiler 305quiet_cmd_dt_S_dtb= DTB $@ 306cmd_dt_S_dtb= \ 307{ \ 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'; \ 317} > $@ 318 319$(obj)/%.dtb.S: $(obj)/%.dtb FORCE 320 $(call if_changed,dt_S_dtb) 321 322quiet_cmd_dtc = DTC $@ 323cmd_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) 328 329$(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE 330 $(call if_changed_dep,dtc) 331 332$(obj)/%.dtbo: $(src)/%.dts $(DTC) FORCE 333 $(call if_changed_dep,dtc) 334 335DT_CHECKER ?= dt-validate 336DT_BINDING_DIR := Documentation/devicetree/bindings 337# DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile 338DT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.json 339 340quiet_cmd_dtb_check = CHECK $@ 341 cmd_dtb_check = $(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@ 342 343define rule_dtc 344 $(call cmd_and_fixdep,dtc) 345 $(call cmd,dtb_check) 346endef 347 348$(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE 349 $(call if_changed_rule,dtc,yaml) 350 351dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp) 352 353# Bzip2 354# --------------------------------------------------------------------------- 355 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. 358size_append = printf $(shell \ 359dec_size=0; \ 360for F in $(real-prereqs); do \ 361 fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \ 362 dec_size=$$(expr $$dec_size + $$fsize); \ 363done; \ 364printf "%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)); \ 369 done; \ 370 } \ 371) 372 373quiet_cmd_bzip2 = BZIP2 $@ 374 cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@ 375 376# Lzma 377# --------------------------------------------------------------------------- 378 379quiet_cmd_lzma = LZMA $@ 380 cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@ 381 382quiet_cmd_lzo = LZO $@ 383 cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@ 384 385quiet_cmd_lz4 = LZ4 $@ 386 cmd_lz4 = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \ 387 $(size_append); } > $@ 388 389# U-Boot mkimage 390# --------------------------------------------------------------------------- 391 392MKIMAGE := $(srctree)/scripts/mkuboot.sh 393 394# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces 395# the number of overrides in arch makefiles 396UIMAGE_ARCH ?= $(SRCARCH) 397UIMAGE_COMPRESSION ?= $(if $(2),$(2),none) 398UIMAGE_OPTS-y ?= 399UIMAGE_TYPE ?= kernel 400UIMAGE_LOADADDR ?= arch_must_set_this 401UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR) 402UIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)' 403 404quiet_cmd_uimage = UIMAGE $@ 405 cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \ 406 -C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \ 407 -T $(UIMAGE_TYPE) \ 408 -a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \ 409 -n $(UIMAGE_NAME) -d $< $@ 410 411# XZ 412# --------------------------------------------------------------------------- 413# Use xzkern to compress the kernel image and xzmisc to compress other things. 414# 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. 423# 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. 428quiet_cmd_xzkern = XZKERN $@ 429 cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \ 430 $(size_append); } > $@ 431 432quiet_cmd_xzmisc = XZMISC $@ 433 cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@ 434 435# ZSTD 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. 441# 442# Note that the bytes added by size_append will make the zstd tool think that 443# the file is corrupt. This is expected. 444# 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. 450 451quiet_cmd_zstd = ZSTD $@ 452 cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@ 453 454quiet_cmd_zstd22 = ZSTD22 $@ 455 cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@ 456 457# ASM offsets 458# --------------------------------------------------------------------------- 459 460# Default sed regexp - multiline due to syntax constraints 461# 462# Use [:space:] because LLVM's integrated assembler inserts <tab> around 463# the .ascii directive whereas GCC keeps the <space> as-is. 464define sed-offsets 465 's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \ 466 /^->/{s:->#\(.*\):/* \1 */:; \ 467 s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \ 468 s:->::; p;}' 469endef 470 471# Use filechk to avoid rebuilds when a header changes, but the resulting file 472# does not 473define filechk_offsets 474 echo "#ifndef $2"; \ 475 echo "#define $2"; \ 476 echo "/*"; \ 477 echo " * DO NOT MODIFY."; \ 478 echo " *"; \ 479 echo " * This file was generated by Kbuild"; \ 480 echo " */"; \ 481 echo ""; \ 482 sed -ne $(sed-offsets) < $<; \ 483 echo ""; \ 484 echo "#endif" 485endef 486