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# Expand $(foo-objs) $(foo-y) by calling $(call suffix-search,foo.o,-objs -y) 60suffix-search = $(foreach s,$(2),$($(1:.o=$s))) 61# If $(foo-objs), $(foo-y), $(foo-m), or $(foo-) exists, foo.o is a composite object 62multi-search = $(sort $(foreach m,$(1), $(if $(strip $(call suffix-search,$(m),$(2) -)), $(m)))) 63multi-used-y := $(call multi-search,$(obj-y),-objs -y) 64multi-used-m := $(call multi-search,$(obj-m),-objs -y -m) 65multi-used := $(multi-used-y) $(multi-used-m) 66 67# Replace multi-part objects by their individual parts, 68# including built-in.a from subdirectories 69real-search = $(foreach m,$(1), $(if $(strip $(call suffix-search,$(m),$(2) -)),$(call suffix-search,$(m),$(2)),$(m))) 70real-obj-y := $(call real-search, $(obj-y),-objs -y) 71real-obj-m := $(call real-search, $(obj-m),-objs -y -m) 72 73always-y += $(always-m) 74 75# hostprogs-always-y += foo 76# ... is a shorthand for 77# hostprogs += foo 78# always-y += foo 79hostprogs += $(hostprogs-always-y) $(hostprogs-always-m) 80always-y += $(hostprogs-always-y) $(hostprogs-always-m) 81 82# userprogs-always-y is likewise. 83userprogs += $(userprogs-always-y) $(userprogs-always-m) 84always-y += $(userprogs-always-y) $(userprogs-always-m) 85 86# DTB 87# If CONFIG_OF_ALL_DTBS is enabled, all DT blobs are built 88always-y += $(dtb-y) 89always-$(CONFIG_OF_ALL_DTBS) += $(dtb-) 90 91ifneq ($(CHECK_DTBS),) 92always-y += $(patsubst %.dtb,%.dt.yaml, $(dtb-y)) 93always-$(CONFIG_OF_ALL_DTBS) += $(patsubst %.dtb,%.dt.yaml, $(dtb-)) 94endif 95 96# Add subdir path 97 98extra-y := $(addprefix $(obj)/,$(extra-y)) 99always-y := $(addprefix $(obj)/,$(always-y)) 100targets := $(addprefix $(obj)/,$(targets)) 101obj-m := $(addprefix $(obj)/,$(obj-m)) 102lib-y := $(addprefix $(obj)/,$(lib-y)) 103real-obj-y := $(addprefix $(obj)/,$(real-obj-y)) 104real-obj-m := $(addprefix $(obj)/,$(real-obj-m)) 105multi-used-m := $(addprefix $(obj)/,$(multi-used-m)) 106subdir-ym := $(addprefix $(obj)/,$(subdir-ym)) 107 108# Finds the multi-part object the current object will be linked into. 109# If the object belongs to two or more multi-part objects, list them all. 110modname-multi = $(sort $(foreach m,$(multi-used),\ 111 $(if $(filter $*.o, $($(m:.o=-objs)) $($(m:.o=-y)) $($(m:.o=-m))),$(m:.o=)))) 112 113__modname = $(if $(modname-multi),$(modname-multi),$(basetarget)) 114 115modname = $(subst $(space),:,$(__modname)) 116modfile = $(addprefix $(obj)/,$(__modname)) 117 118# target with $(obj)/ and its suffix stripped 119target-stem = $(basename $(patsubst $(obj)/%,%,$@)) 120 121# These flags are needed for modversions and compiling, so we define them here 122# $(modname_flags) defines KBUILD_MODNAME as the name of the module it will 123# end up in (or would, if it gets compiled in) 124name-fix = $(call stringify,$(subst $(comma),_,$(subst -,_,$1))) 125basename_flags = -DKBUILD_BASENAME=$(call name-fix,$(basetarget)) 126modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname)) 127modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile)) 128 129_c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \ 130 $(filter-out $(ccflags-remove-y), \ 131 $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \ 132 $(CFLAGS_$(target-stem).o)) 133_a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \ 134 $(filter-out $(asflags-remove-y), \ 135 $(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \ 136 $(AFLAGS_$(target-stem).o)) 137_cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds) 138 139# 140# Enable gcov profiling flags for a file, directory or for all files depending 141# on variables GCOV_PROFILE_obj.o, GCOV_PROFILE and CONFIG_GCOV_PROFILE_ALL 142# (in this order) 143# 144ifeq ($(CONFIG_GCOV_KERNEL),y) 145_c_flags += $(if $(patsubst n%,, \ 146 $(GCOV_PROFILE_$(basetarget).o)$(GCOV_PROFILE)$(CONFIG_GCOV_PROFILE_ALL)), \ 147 $(CFLAGS_GCOV)) 148endif 149 150# 151# Enable address sanitizer flags for kernel except some files or directories 152# we don't want to check (depends on variables KASAN_SANITIZE_obj.o, KASAN_SANITIZE) 153# 154ifeq ($(CONFIG_KASAN),y) 155ifneq ($(CONFIG_KASAN_HW_TAGS),y) 156_c_flags += $(if $(patsubst n%,, \ 157 $(KASAN_SANITIZE_$(basetarget).o)$(KASAN_SANITIZE)y), \ 158 $(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE)) 159endif 160endif 161 162ifeq ($(CONFIG_UBSAN),y) 163_c_flags += $(if $(patsubst n%,, \ 164 $(UBSAN_SANITIZE_$(basetarget).o)$(UBSAN_SANITIZE)$(CONFIG_UBSAN_SANITIZE_ALL)), \ 165 $(CFLAGS_UBSAN)) 166endif 167 168ifeq ($(CONFIG_KCOV),y) 169_c_flags += $(if $(patsubst n%,, \ 170 $(KCOV_INSTRUMENT_$(basetarget).o)$(KCOV_INSTRUMENT)$(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_$(basetarget).o)$(KCSAN_SANITIZE)y), \ 181 $(CFLAGS_KCSAN)) 182endif 183 184# $(srctree)/$(src) for including checkin headers from generated source files 185# $(objtree)/$(obj) for including generated headers from checkin source files 186ifeq ($(KBUILD_EXTMOD),) 187ifdef building_out_of_srctree 188_c_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj) 189_a_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj) 190_cpp_flags += -I $(srctree)/$(src) -I $(objtree)/$(obj) 191endif 192endif 193 194part-of-module = $(if $(filter $(basename $@).o, $(real-obj-m)),y) 195quiet_modtag = $(if $(part-of-module),[M], ) 196 197modkern_cflags = \ 198 $(if $(part-of-module), \ 199 $(KBUILD_CFLAGS_MODULE) $(CFLAGS_MODULE), \ 200 $(KBUILD_CFLAGS_KERNEL) $(CFLAGS_KERNEL) $(modfile_flags)) 201 202modkern_aflags = $(if $(part-of-module), \ 203 $(KBUILD_AFLAGS_MODULE) $(AFLAGS_MODULE), \ 204 $(KBUILD_AFLAGS_KERNEL) $(AFLAGS_KERNEL)) 205 206c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ 207 -include $(srctree)/include/linux/compiler_types.h \ 208 $(_c_flags) $(modkern_cflags) \ 209 $(basename_flags) $(modname_flags) 210 211a_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ 212 $(_a_flags) $(modkern_aflags) 213 214cpp_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ 215 $(_cpp_flags) 216 217ld_flags = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F)) 218 219DTC_INCLUDE := $(srctree)/scripts/dtc/include-prefixes 220 221dtc_cpp_flags = -Wp,-MMD,$(depfile).pre.tmp -nostdinc \ 222 $(addprefix -I,$(DTC_INCLUDE)) \ 223 -undef -D__DTS__ 224 225# Useful for describing the dependency of composite objects 226# Usage: 227# $(call multi_depend, multi_used_targets, suffix_to_remove, suffix_to_add) 228define multi_depend 229$(foreach m, $(notdir $1), \ 230 $(eval $(obj)/$m: \ 231 $(addprefix $(obj)/, $(foreach s, $3, $($(m:%$(strip $2)=%$(s))))))) 232endef 233 234quiet_cmd_copy = COPY $@ 235 cmd_copy = cp $< $@ 236 237# Shipped files 238# =========================================================================== 239 240quiet_cmd_shipped = SHIPPED $@ 241cmd_shipped = cat $< > $@ 242 243$(obj)/%: $(src)/%_shipped 244 $(call cmd,shipped) 245 246# Commands useful for building a boot image 247# =========================================================================== 248# 249# Use as following: 250# 251# target: source(s) FORCE 252# $(if_changed,ld/objcopy/gzip) 253# 254# and add target to 'targets' so that we know we have to 255# read in the saved command line 256 257# Linking 258# --------------------------------------------------------------------------- 259 260quiet_cmd_ld = LD $@ 261 cmd_ld = $(LD) $(ld_flags) $(real-prereqs) -o $@ 262 263# Archive 264# --------------------------------------------------------------------------- 265 266quiet_cmd_ar = AR $@ 267 cmd_ar = rm -f $@; $(AR) cDPrsT $@ $(real-prereqs) 268 269# Objcopy 270# --------------------------------------------------------------------------- 271 272quiet_cmd_objcopy = OBJCOPY $@ 273cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS) $(OBJCOPYFLAGS_$(@F)) $< $@ 274 275# Gzip 276# --------------------------------------------------------------------------- 277 278quiet_cmd_gzip = GZIP $@ 279 cmd_gzip = cat $(real-prereqs) | $(KGZIP) -n -f -9 > $@ 280 281# DTC 282# --------------------------------------------------------------------------- 283DTC ?= $(objtree)/scripts/dtc/dtc 284DTC_FLAGS += -Wno-interrupt_provider 285 286# Disable noisy checks by default 287ifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),) 288DTC_FLAGS += -Wno-unit_address_vs_reg \ 289 -Wno-unit_address_format \ 290 -Wno-avoid_unnecessary_addr_size \ 291 -Wno-alias_paths \ 292 -Wno-graph_child_address \ 293 -Wno-simple_bus_reg \ 294 -Wno-unique_unit_address \ 295 -Wno-pci_device_reg 296endif 297 298ifneq ($(findstring 2,$(KBUILD_EXTRA_WARN)),) 299DTC_FLAGS += -Wnode_name_chars_strict \ 300 -Wproperty_name_chars_strict \ 301 -Winterrupt_provider 302endif 303 304DTC_FLAGS += $(DTC_FLAGS_$(basetarget)) 305 306# Generate an assembly file to wrap the output of the device tree compiler 307quiet_cmd_dt_S_dtb= DTB $@ 308cmd_dt_S_dtb= \ 309{ \ 310 echo '\#include <asm-generic/vmlinux.lds.h>'; \ 311 echo '.section .dtb.init.rodata,"a"'; \ 312 echo '.balign STRUCT_ALIGNMENT'; \ 313 echo '.global __dtb_$(subst -,_,$(*F))_begin'; \ 314 echo '__dtb_$(subst -,_,$(*F))_begin:'; \ 315 echo '.incbin "$<" '; \ 316 echo '__dtb_$(subst -,_,$(*F))_end:'; \ 317 echo '.global __dtb_$(subst -,_,$(*F))_end'; \ 318 echo '.balign STRUCT_ALIGNMENT'; \ 319} > $@ 320 321$(obj)/%.dtb.S: $(obj)/%.dtb FORCE 322 $(call if_changed,dt_S_dtb) 323 324quiet_cmd_dtc = DTC $@ 325cmd_dtc = $(HOSTCC) -E $(dtc_cpp_flags) -x assembler-with-cpp -o $(dtc-tmp) $< ; \ 326 $(DTC) -O $(patsubst .%,%,$(suffix $@)) -o $@ -b 0 \ 327 $(addprefix -i,$(dir $<) $(DTC_INCLUDE)) $(DTC_FLAGS) \ 328 -d $(depfile).dtc.tmp $(dtc-tmp) ; \ 329 cat $(depfile).pre.tmp $(depfile).dtc.tmp > $(depfile) 330 331$(obj)/%.dtb: $(src)/%.dts $(DTC) FORCE 332 $(call if_changed_dep,dtc) 333 334DT_CHECKER ?= dt-validate 335DT_BINDING_DIR := Documentation/devicetree/bindings 336# DT_TMP_SCHEMA may be overridden from Documentation/devicetree/bindings/Makefile 337DT_TMP_SCHEMA ?= $(objtree)/$(DT_BINDING_DIR)/processed-schema.json 338 339quiet_cmd_dtb_check = CHECK $@ 340 cmd_dtb_check = $(DT_CHECKER) -u $(srctree)/$(DT_BINDING_DIR) -p $(DT_TMP_SCHEMA) $@ 341 342define rule_dtc 343 $(call cmd_and_fixdep,dtc) 344 $(call cmd,dtb_check) 345endef 346 347$(obj)/%.dt.yaml: $(src)/%.dts $(DTC) $(DT_TMP_SCHEMA) FORCE 348 $(call if_changed_rule,dtc,yaml) 349 350dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp) 351 352# Bzip2 353# --------------------------------------------------------------------------- 354 355# Bzip2 and LZMA do not include size in file... so we have to fake that; 356# append the size as a 32-bit littleendian number as gzip does. 357size_append = printf $(shell \ 358dec_size=0; \ 359for F in $(real-prereqs); do \ 360 fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \ 361 dec_size=$$(expr $$dec_size + $$fsize); \ 362done; \ 363printf "%08x\n" $$dec_size | \ 364 sed 's/\(..\)/\1 /g' | { \ 365 read ch0 ch1 ch2 ch3; \ 366 for ch in $$ch3 $$ch2 $$ch1 $$ch0; do \ 367 printf '%s%03o' '\\' $$((0x$$ch)); \ 368 done; \ 369 } \ 370) 371 372quiet_cmd_bzip2 = BZIP2 $@ 373 cmd_bzip2 = { cat $(real-prereqs) | $(KBZIP2) -9; $(size_append); } > $@ 374 375# Lzma 376# --------------------------------------------------------------------------- 377 378quiet_cmd_lzma = LZMA $@ 379 cmd_lzma = { cat $(real-prereqs) | $(LZMA) -9; $(size_append); } > $@ 380 381quiet_cmd_lzo = LZO $@ 382 cmd_lzo = { cat $(real-prereqs) | $(KLZOP) -9; $(size_append); } > $@ 383 384quiet_cmd_lz4 = LZ4 $@ 385 cmd_lz4 = { cat $(real-prereqs) | $(LZ4) -l -c1 stdin stdout; \ 386 $(size_append); } > $@ 387 388# U-Boot mkimage 389# --------------------------------------------------------------------------- 390 391MKIMAGE := $(srctree)/scripts/mkuboot.sh 392 393# SRCARCH just happens to match slightly more than ARCH (on sparc), so reduces 394# the number of overrides in arch makefiles 395UIMAGE_ARCH ?= $(SRCARCH) 396UIMAGE_COMPRESSION ?= $(if $(2),$(2),none) 397UIMAGE_OPTS-y ?= 398UIMAGE_TYPE ?= kernel 399UIMAGE_LOADADDR ?= arch_must_set_this 400UIMAGE_ENTRYADDR ?= $(UIMAGE_LOADADDR) 401UIMAGE_NAME ?= 'Linux-$(KERNELRELEASE)' 402 403quiet_cmd_uimage = UIMAGE $@ 404 cmd_uimage = $(BASH) $(MKIMAGE) -A $(UIMAGE_ARCH) -O linux \ 405 -C $(UIMAGE_COMPRESSION) $(UIMAGE_OPTS-y) \ 406 -T $(UIMAGE_TYPE) \ 407 -a $(UIMAGE_LOADADDR) -e $(UIMAGE_ENTRYADDR) \ 408 -n $(UIMAGE_NAME) -d $< $@ 409 410# XZ 411# --------------------------------------------------------------------------- 412# Use xzkern to compress the kernel image and xzmisc to compress other things. 413# 414# xzkern uses a big LZMA2 dictionary since it doesn't increase memory usage 415# of the kernel decompressor. A BCJ filter is used if it is available for 416# the target architecture. xzkern also appends uncompressed size of the data 417# using size_append. The .xz format has the size information available at 418# the end of the file too, but it's in more complex format and it's good to 419# avoid changing the part of the boot code that reads the uncompressed size. 420# Note that the bytes added by size_append will make the xz tool think that 421# the file is corrupt. This is expected. 422# 423# xzmisc doesn't use size_append, so it can be used to create normal .xz 424# files. xzmisc uses smaller LZMA2 dictionary than xzkern, because a very 425# big dictionary would increase the memory usage too much in the multi-call 426# decompression mode. A BCJ filter isn't used either. 427quiet_cmd_xzkern = XZKERN $@ 428 cmd_xzkern = { cat $(real-prereqs) | sh $(srctree)/scripts/xz_wrap.sh; \ 429 $(size_append); } > $@ 430 431quiet_cmd_xzmisc = XZMISC $@ 432 cmd_xzmisc = cat $(real-prereqs) | $(XZ) --check=crc32 --lzma2=dict=1MiB > $@ 433 434# ZSTD 435# --------------------------------------------------------------------------- 436# Appends the uncompressed size of the data using size_append. The .zst 437# format has the size information available at the beginning of the file too, 438# but it's in a more complex format and it's good to avoid changing the part 439# of the boot code that reads the uncompressed size. 440# 441# Note that the bytes added by size_append will make the zstd tool think that 442# the file is corrupt. This is expected. 443# 444# zstd uses a maximum window size of 8 MB. zstd22 uses a maximum window size of 445# 128 MB. zstd22 is used for kernel compression because it is decompressed in a 446# single pass, so zstd doesn't need to allocate a window buffer. When streaming 447# decompression is used, like initramfs decompression, zstd22 should likely not 448# be used because it would require zstd to allocate a 128 MB buffer. 449 450quiet_cmd_zstd = ZSTD $@ 451 cmd_zstd = { cat $(real-prereqs) | $(ZSTD) -19; $(size_append); } > $@ 452 453quiet_cmd_zstd22 = ZSTD22 $@ 454 cmd_zstd22 = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@ 455 456# ASM offsets 457# --------------------------------------------------------------------------- 458 459# Default sed regexp - multiline due to syntax constraints 460# 461# Use [:space:] because LLVM's integrated assembler inserts <tab> around 462# the .ascii directive whereas GCC keeps the <space> as-is. 463define sed-offsets 464 's:^[[:space:]]*\.ascii[[:space:]]*"\(.*\)".*:\1:; \ 465 /^->/{s:->#\(.*\):/* \1 */:; \ 466 s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \ 467 s:->::; p;}' 468endef 469 470# Use filechk to avoid rebuilds when a header changes, but the resulting file 471# does not 472define filechk_offsets 473 echo "#ifndef $2"; \ 474 echo "#define $2"; \ 475 echo "/*"; \ 476 echo " * DO NOT MODIFY."; \ 477 echo " *"; \ 478 echo " * This file was generated by Kbuild"; \ 479 echo " */"; \ 480 echo ""; \ 481 sed -ne $(sed-offsets) < $<; \ 482 echo ""; \ 483 echo "#endif" 484endef 485