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