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