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