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