xref: /linux/rust/Makefile (revision 1b0bab4a873f1034c27573cfc613394cff7e0a5b)
1# SPDX-License-Identifier: GPL-2.0
2
3# Where to place rustdoc generated documentation
4rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
5
6obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o
7always-$(CONFIG_RUST) += exports_core_generated.h
8
9obj-$(CONFIG_RUST) += zerocopy.o
10
11ifdef CONFIG_RUST_INLINE_HELPERS
12always-$(CONFIG_RUST) += helpers/helpers.bc helpers/helpers_module.bc
13else
14obj-$(CONFIG_RUST) += helpers/helpers.o
15always-$(CONFIG_RUST) += exports_helpers_generated.h
16endif
17# Missing prototypes are expected in the helpers since these are exported
18# for Rust only, thus there is no header nor prototypes.
19CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
20
21always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
22obj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o
23always-$(CONFIG_RUST) += exports_bindings_generated.h exports_kernel_generated.h
24
25always-$(CONFIG_RUST) += uapi/uapi_generated.rs
26obj-$(CONFIG_RUST) += uapi.o
27
28ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
29obj-$(CONFIG_RUST) += build_error.o
30else
31always-$(CONFIG_RUST) += build_error.o
32endif
33
34obj-$(CONFIG_RUST) += exports.o
35
36always-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
37
38always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
39always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
40
41obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
42obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
43
44always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
45ifndef CONFIG_UML
46always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
47endif
48
49# Avoids running `$(RUSTC)` when it may not be available.
50ifdef CONFIG_RUST
51
52procmacro-name = $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name $(1) --crate-type proc-macro - </dev/null)
53procmacro-extension := $(patsubst libname.%,%,$(call procmacro-name,name))
54
55libzerocopy_derive_name := $(call procmacro-name,zerocopy_derive)
56libmacros_name := $(call procmacro-name,macros)
57libpin_init_internal_name := $(call procmacro-name,pin_init_internal)
58
59always-$(CONFIG_RUST) += $(libzerocopy_derive_name) $(libmacros_name) $(libpin_init_internal_name)
60
61# `$(rust_flags)` is passed in case the user added `--sysroot`.
62rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
63rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
64RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
65
66ifneq ($(quiet),)
67rust_test_quiet=-q
68rustdoc_test_quiet=--test-args -q
69rustdoc_test_kernel_quiet=>/dev/null
70endif
71
72cfgs-to-flags = $(patsubst %,--cfg='%',$1)
73
74core-cfgs := \
75    no_fp_fmt_parse
76
77core-edition := $(if $(call rustc-min-version,108700),2024,2021)
78
79core-skip_flags := \
80    --edition=2021 \
81    -Wunreachable_pub
82
83core-flags := \
84    --edition=$(core-edition) \
85    $(call cfgs-to-flags,$(core-cfgs))
86
87zerocopy-cfgs := \
88    no_fp_fmt_parse
89
90zerocopy-flags := \
91    --cap-lints=allow \
92    $(call cfgs-to-flags,$(zerocopy-cfgs))
93
94zerocopy-envs := \
95    CARGO_PKG_VERSION=0.8.54
96
97proc_macro2-cfgs := \
98    feature="proc-macro" \
99    wrap_proc_macro \
100    $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
101
102proc_macro2-flags := \
103    --cap-lints=allow \
104    $(call cfgs-to-flags,$(proc_macro2-cfgs))
105
106quote-cfgs := \
107    feature="proc-macro"
108
109quote-skip_flags := \
110    --edition=2021
111
112quote-flags := \
113    --edition=2018 \
114    --cap-lints=allow \
115    --extern proc_macro2 \
116    $(call cfgs-to-flags,$(quote-cfgs))
117
118# `extra-traits`, `fold` and `visit` may be enabled if needed.
119syn-cfgs := \
120    feature="clone-impls" \
121    feature="derive" \
122    feature="full" \
123    feature="parsing" \
124    feature="printing" \
125    feature="proc-macro" \
126    feature="visit" \
127    feature="visit-mut"
128
129syn-flags := \
130    --cap-lints=allow \
131    --extern proc_macro2 \
132    --extern quote \
133    $(call cfgs-to-flags,$(syn-cfgs))
134
135zerocopy_derive-cfgs := \
136    zerocopy_unstable_linux
137
138zerocopy_derive-flags := \
139    --cap-lints=allow \
140    --extern proc_macro2 \
141    --extern quote \
142    --extern syn \
143    $(call cfgs-to-flags,$(zerocopy_derive-cfgs))
144
145pin_init_internal-cfgs := \
146    kernel USE_RUSTC_FEATURES
147
148pin_init_internal-flags := \
149    --extern proc_macro2 \
150    --extern quote \
151    --extern syn \
152    $(call cfgs-to-flags,$(pin_init_internal-cfgs))
153
154pin_init-cfgs := \
155    kernel USE_RUSTC_FEATURES
156
157pin_init-flags := \
158    --extern pin_init_internal \
159    --extern macros \
160    $(call cfgs-to-flags,$(pin_init-cfgs))
161
162# `rustdoc` did not save the target modifiers, thus workaround for
163# the time being (https://github.com/rust-lang/rust/issues/144521).
164rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
165
166# Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465).
167doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer)
168
169quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
170      cmd_rustdoc = \
171	$(rustc_target_envs) \
172	OBJTREE=$(abspath $(objtree)) \
173	$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-scope=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
174		$(rustc_target_flags) -L$(objtree)/$(obj) \
175		-Zunstable-options --generate-link-to-definition \
176		--output $(rustdoc_output) \
177		--crate-name $(subst rustdoc-,,$@) \
178		$(rustdoc_modifiers_workaround) \
179		$(if $(rustdoc_host),,--sysroot=/dev/null) \
180		@$(objtree)/include/generated/rustc_cfg $<
181
182# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
183# can be used to specify a custom logo. However:
184#   - The given value is used as-is, thus it cannot be relative or a local file
185#     (unlike the non-custom case) since the generated docs have subfolders.
186#   - It requires adding it to every crate.
187#   - It requires changing `core` which comes from the sysroot.
188#
189# Using `-Zcrate-attr` would solve the last two points, but not the first.
190# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
191# command-like flags to solve the issue. Meanwhile, we use the non-custom case
192# and then retouch the generated files.
193rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
194    rustdoc-kernel rustdoc-pin_init rustdoc-zerocopy rustdoc-zerocopy_derive
195	$(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \
196		cut -d'"' -f2 | cut -d/ -f2- | while read f; do \
197			if [ ! -e "$(srctree)/$$f" ]; then \
198				echo "warning: srctree/ link to $$f does not exist"; \
199			fi \
200		done
201	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
202	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
203	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
204		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
205		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
206		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
207		-e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
208	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
209		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
210
211rustdoc-proc_macro2: private rustdoc_host = yes
212rustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
213rustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
214	+$(call if_changed,rustdoc)
215
216rustdoc-quote: private rustdoc_host = yes
217rustdoc-quote: private rustc_target_flags = $(quote-flags)
218rustdoc-quote: private skip_flags = $(quote-skip_flags)
219rustdoc-quote: $(src)/quote/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
220	+$(call if_changed,rustdoc)
221
222rustdoc-syn: private rustdoc_host = yes
223rustdoc-syn: private rustc_target_flags = $(syn-flags)
224rustdoc-syn: $(src)/syn/lib.rs rustdoc-clean rustdoc-quote FORCE
225	+$(call if_changed,rustdoc)
226
227rustdoc-zerocopy_derive: private rustdoc_host = yes
228rustdoc-zerocopy_derive: private rustc_target_flags = $(zerocopy_derive-flags) \
229    --extern proc_macro --crate-type proc-macro
230rustdoc-zerocopy_derive: $(src)/zerocopy-derive/lib.rs rustdoc-clean rustdoc-syn FORCE
231	+$(call if_changed,rustdoc)
232
233rustdoc-macros: private rustdoc_host = yes
234rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
235    --extern proc_macro --extern proc_macro2 --extern quote --extern syn
236rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 \
237    rustdoc-quote rustdoc-syn FORCE
238	+$(call if_changed,rustdoc)
239
240rustdoc-core: private skip_flags = $(core-skip_flags)
241rustdoc-core: private rustc_target_flags = $(core-flags)
242rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
243	+$(call if_changed,rustdoc)
244
245# Even if `rustdoc` targets are not kernel objects, they should still be
246# treated as such so that we pass the same flags. Otherwise, for instance,
247# `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch.
248rustdoc-compiler_builtins: private is-kernel-object := y
249rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
250	+$(call if_changed,rustdoc)
251
252rustdoc-zerocopy: private rustc_target_envs := $(zerocopy-envs)
253rustdoc-zerocopy: private is-kernel-object := y
254rustdoc-zerocopy: private rustc_target_flags = $(zerocopy-flags) \
255    --extend-css $(src)/zerocopy/rustdoc/style.css
256rustdoc-zerocopy: $(src)/zerocopy/src/lib.rs rustdoc-clean rustdoc-core FORCE
257	+$(call if_changed,rustdoc)
258
259rustdoc-ffi: private is-kernel-object := y
260rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
261	+$(call if_changed,rustdoc)
262
263rustdoc-pin_init_internal: private rustdoc_host = yes
264rustdoc-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
265    --extern proc_macro --crate-type proc-macro
266rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
267    rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
268	+$(call if_changed,rustdoc)
269
270rustdoc-pin_init: private rustdoc_host = yes
271rustdoc-pin_init: private rustc_target_flags = $(pin_init-flags) \
272    --extern alloc --cfg feature=\"alloc\"
273rustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
274    rustdoc-macros FORCE
275	+$(call if_changed,rustdoc)
276
277rustdoc-kernel: private is-kernel-object := y
278rustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
279    --extern build_error --extern macros \
280    --extern bindings --extern uapi \
281    --extern zerocopy --extern zerocopy_derive
282rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
283    rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \
284    $(obj)/bindings.o FORCE
285	+$(call if_changed,rustdoc)
286
287rustdoc-clean: FORCE
288	$(Q)rm -rf $(rustdoc_output)
289
290quiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $<
291      cmd_rustc_test_library = \
292	$(rustc_target_envs) \
293	OBJTREE=$(abspath $(objtree)) \
294	$(RUSTC_OR_CLIPPY) $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
295		@$(objtree)/include/generated/rustc_cfg \
296		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
297		--out-dir $(objtree)/$(obj)/test --cfg testlib \
298		-L$(objtree)/$(obj)/test \
299		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
300
301rusttestlib-zerocopy: private rustc_target_envs := $(zerocopy-envs)
302rusttestlib-zerocopy: private rustc_target_flags = $(zerocopy-flags)
303rusttestlib-zerocopy: $(src)/zerocopy/src/lib.rs FORCE
304	+$(call if_changed,rustc_test_library)
305
306rusttestlib-build_error: $(src)/build_error.rs FORCE
307	+$(call if_changed,rustc_test_library)
308
309rusttestlib-ffi: $(src)/ffi.rs FORCE
310	+$(call if_changed,rustc_test_library)
311
312rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
313rusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
314	+$(call if_changed,rustc_test_library)
315
316rusttestlib-quote: private skip_flags = $(quote-skip_flags)
317rusttestlib-quote: private rustc_target_flags = $(quote-flags)
318rusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
319	+$(call if_changed,rustc_test_library)
320
321rusttestlib-syn: private rustc_target_flags = $(syn-flags)
322rusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
323	+$(call if_changed,rustc_test_library)
324
325rusttestlib-zerocopy_derive: private rustc_target_flags = $(zerocopy_derive-flags) \
326    --extern proc_macro
327rusttestlib-zerocopy_derive: private rustc_test_library_proc = yes
328rusttestlib-zerocopy_derive: $(src)/zerocopy-derive/lib.rs rusttestlib-syn FORCE
329	+$(call if_changed,rustc_test_library)
330
331rusttestlib-macros: private rustc_target_flags = --extern proc_macro \
332    --extern proc_macro2 --extern quote --extern syn
333rusttestlib-macros: private rustc_test_library_proc = yes
334rusttestlib-macros: $(src)/macros/lib.rs \
335    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
336	+$(call if_changed,rustc_test_library)
337
338rusttestlib-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
339    --extern proc_macro
340rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
341rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
342    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
343	+$(call if_changed,rustc_test_library)
344
345rusttestlib-pin_init: private rustc_target_flags = $(pin_init-flags)
346rusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
347    rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
348	+$(call if_changed,rustc_test_library)
349
350rusttestlib-kernel: private rustc_target_flags = --extern ffi \
351    --extern build_error --extern macros --extern pin_init \
352    --extern bindings --extern uapi \
353    --extern zerocopy=$(objtree)/$(obj)/test/libzerocopy.rlib \
354    --extern zerocopy_derive=$(objtree)/$(obj)/test/$(libzerocopy_derive_name)
355rusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
356    rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
357    $(obj)/bindings.o rusttestlib-zerocopy rusttestlib-zerocopy_derive FORCE
358	+$(call if_changed,rustc_test_library)
359
360rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
361rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
362	+$(call if_changed,rustc_test_library)
363
364rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
365rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
366	+$(call if_changed,rustc_test_library)
367
368quiet_cmd_rustdoc_test = RUSTDOC T $<
369      cmd_rustdoc_test = \
370	$(rustc_target_envs) \
371	RUST_MODFILE=test.rs \
372	OBJTREE=$(abspath $(objtree)) \
373	$(RUSTDOC) --test $(rust_common_flags) \
374		-Zcrate-attr='feature(used_with_arg)' \
375		@$(objtree)/include/generated/rustc_cfg \
376		$(rustc_target_flags) $(rustdoc_test_target_flags) \
377		$(rustdoc_test_quiet) \
378		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
379		--crate-name $(subst rusttest-,,$@) $<
380
381quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
382      cmd_rustdoc_test_kernel = \
383	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
384	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
385	$(rustc_target_envs) \
386	OBJTREE=$(abspath $(objtree)) \
387	$(RUSTDOC) --test $(filter-out --remap-path-scope=%,$(rust_flags)) \
388		-L$(objtree)/$(obj) --extern ffi --extern pin_init \
389		--extern kernel --extern build_error --extern macros \
390		--extern bindings --extern uapi \
391		--extern zerocopy --extern zerocopy_derive \
392		--no-run --crate-name kernel -Zunstable-options \
393		--sysroot=/dev/null \
394		$(doctests_modifiers_workaround) \
395		--test-builder $(objtree)/scripts/rustdoc_test_builder \
396		$< $(rustdoc_test_kernel_quiet); \
397	$(objtree)/scripts/rustdoc_test_gen
398
399%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
400    $(src)/kernel/lib.rs $(obj)/kernel.o \
401    $(objtree)/scripts/rustdoc_test_builder \
402    $(objtree)/scripts/rustdoc_test_gen FORCE
403	+$(call if_changed,rustdoc_test_kernel)
404
405# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
406# so for the moment we skip `-Cpanic=abort`.
407quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
408      cmd_rustc_test = \
409	$(rustc_target_envs) \
410	OBJTREE=$(abspath $(objtree)) \
411	$(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
412		@$(objtree)/include/generated/rustc_cfg \
413		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
414		-L$(objtree)/$(obj)/test \
415		--crate-name $(subst rusttest-,,$@) $<; \
416	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
417		$(rustc_test_run_flags)
418
419rusttest: rusttest-macros
420
421rusttest-macros: private rustc_target_flags = --extern proc_macro \
422    --extern macros --extern kernel --extern pin_init \
423    --extern proc_macro2 --extern quote --extern syn
424rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
425rusttest-macros: $(src)/macros/lib.rs \
426    rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
427	+$(call if_changed,rustc_test)
428	+$(call if_changed,rustdoc_test)
429
430ifdef CONFIG_CC_IS_CLANG
431bindgen_c_flags = $(c_flags)
432else
433# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
434# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
435#
436# For the moment, here we are tweaking the flags on the fly. This is a hack,
437# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
438# if we end up using one of those structs).
439bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
440	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
441	-mindirect-branch=thunk-extern -mindirect-branch-register \
442	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
443	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
444	-mno-pointers-to-nested-functions -mno-string \
445	-mno-strict-align -mstrict-align -mdirect-extern-access \
446	-mexplicit-relocs -mno-check-zero-division \
447	-fconserve-stack -falign-jumps=% -falign-loops=% \
448	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
449	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
450	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
451	-fzero-call-used-regs=% -fno-stack-clash-protection \
452	-fno-inline-functions-called-once -fsanitize=bounds-strict \
453	-fstrict-flex-arrays=% -fmin-function-alignment=% \
454	-fzero-init-padding-bits=% -mno-fdpic \
455	-fdiagnostics-show-context -fdiagnostics-show-context=% \
456	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference
457
458# Derived from `scripts/Makefile.clang`.
459BINDGEN_TARGET_x86	:= x86_64-linux-gnu
460BINDGEN_TARGET_arm64	:= aarch64-linux-gnu
461BINDGEN_TARGET_arm	:= arm-linux-gnueabi
462BINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
463BINDGEN_TARGET_s390	:= s390x-linux-gnu
464# This is only for i386 UM builds, which need the 32-bit target not -m32
465BINDGEN_TARGET_i386	:= i386-linux-gnu
466BINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
467BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
468
469# All warnings are inhibited since GCC builds are very experimental,
470# many GCC warnings are not supported by Clang, they may only appear in
471# some configurations, with new GCC versions, etc.
472bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
473
474# Auto variable zero-initialization requires an additional special option with
475# clang that is going to be removed sometime in the future (likely in
476# clang-18), so make sure to pass this option only if clang supports it
477# (libclang major version < 16).
478#
479# https://github.com/llvm/llvm-project/issues/44842
480# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
481ifdef CONFIG_INIT_STACK_ALL_ZERO
482libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
483ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
484bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
485endif
486endif
487
488bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
489	$(bindgen_extra_c_flags)
490endif
491
492ifdef CONFIG_LTO
493bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
494else
495bindgen_c_flags_lto = $(bindgen_c_flags)
496endif
497
498# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
499# prototypes for functions like `memcpy` -- if this flag is not passed,
500# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
501# architecture instead of generating `usize`.
502bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
503
504# `--rust-target` points to our minimum supported Rust version.
505quiet_cmd_bindgen = BINDGEN $@
506      cmd_bindgen = \
507	$(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.85 \
508		--use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
509		--no-debug '.*' --enable-function-attribute-detection \
510		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
511		$(bindgen_target_cflags) $(bindgen_target_extra)
512
513$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
514    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
515$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
516    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
517$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
518    $(src)/bindgen_parameters FORCE
519	$(call if_changed_dep,bindgen)
520
521$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
522    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
523$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
524    $(src)/bindgen_parameters FORCE
525	$(call if_changed_dep,bindgen)
526
527# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
528# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
529# given it is `libclang`; but for consistency, future Clang changes and/or
530# a potential future GCC backend for `bindgen`, we disable it too.
531$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
532    --blocklist-type '.*' --allowlist-var '' \
533    --allowlist-function 'rust_helper_.*'
534$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
535    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
536$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
537    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
538$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
539	$(call if_changed_dep,bindgen)
540
541quiet_cmd_rust_helper = HELPER  $@
542      cmd_rust_helper = \
543	$(CC) $(filter-out $(CFLAGS_REMOVE_helpers/helpers.o), $(c_flags)) \
544		-c -g0 $< -emit-llvm -o $@
545
546$(obj)/helpers/helpers.bc: private part-of-builtin := y
547$(obj)/helpers/helpers_module.bc: private part-of-module := y
548$(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpers.c FORCE
549	+$(call if_changed_dep,rust_helper)
550
551rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
552
553quiet_cmd_exports = EXPORTS $@
554      cmd_exports = \
555	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
556
557$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
558	$(call if_changed,exports)
559
560# Even though Rust kernel modules should never use the bindings directly,
561# symbols from the `bindings` crate and the C helpers need to be exported
562# because Rust generics and inlined functions may not get their code generated
563# in the crate where they are defined. Other helpers, called from non-inline
564# functions, may not be exported, in principle. However, in general, the Rust
565# compiler does not guarantee codegen will be performed for a non-inline
566# function either. Therefore, we export all symbols from helpers and bindings.
567# In the future, this may be revisited to reduce the number of exports after
568# the compiler is informed about the places codegen is required.
569$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
570	$(call if_changed,exports)
571
572$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
573	$(call if_changed,exports)
574
575$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
576	$(call if_changed,exports)
577
578quiet_cmd_rustc_procmacrolibrary = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) PL $@
579      cmd_rustc_procmacrolibrary = \
580	$(rustc_target_envs) \
581	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
582		$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
583		--emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \
584		--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
585		--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<
586
587$(obj)/libproc_macro2.rlib: private skip_clippy = 1
588$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
589$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
590	+$(call if_changed_dep,rustc_procmacrolibrary)
591
592$(obj)/libquote.rlib: private skip_clippy = 1
593$(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
594$(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
595$(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
596	+$(call if_changed_dep,rustc_procmacrolibrary)
597
598$(obj)/libsyn.rlib: private skip_clippy = 1
599$(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
600$(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
601	+$(call if_changed_dep,rustc_procmacrolibrary)
602
603quiet_cmd_rustc_procmacro = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) P $@
604      cmd_rustc_procmacro = \
605	$(rustc_target_envs) \
606	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) $(rust_common_flags) $(rustc_target_flags) \
607		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
608		-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
609		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
610		--crate-type proc-macro -L$(objtree)/$(obj) \
611		--crate-name $(patsubst lib%.$(procmacro-extension),%,$(notdir $@)) \
612		@$(objtree)/include/generated/rustc_cfg $<
613
614# Procedural macros can only be used with the `rustc` that compiled it.
615$(obj)/$(libzerocopy_derive_name): private skip_clippy = 1
616$(obj)/$(libzerocopy_derive_name): private rustc_target_flags = $(zerocopy_derive-flags)
617$(obj)/$(libzerocopy_derive_name): $(src)/zerocopy-derive/lib.rs $(obj)/libproc_macro2.rlib \
618    $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
619	+$(call if_changed_dep,rustc_procmacro)
620
621$(obj)/$(libmacros_name): private rustc_target_flags = \
622    --extern proc_macro2 --extern quote --extern syn
623$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
624    $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
625	+$(call if_changed_dep,rustc_procmacro)
626
627$(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
628$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
629    $(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
630	+$(call if_changed_dep,rustc_procmacro)
631
632# `rustc` requires `-Zunstable-options` to use custom target specifications
633# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
634quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
635      cmd_rustc_library = \
636	$(rustc_target_envs) \
637	OBJTREE=$(abspath $(objtree)) \
638	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
639		$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
640		--emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
641		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
642		--crate-type rlib -L$(objtree)/$(obj) \
643		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
644		--sysroot=/dev/null \
645		-Zunstable-options \
646	$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
647		$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
648		$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS)) \
649		$(CC_FLAGS_RUST_INLINE_HELPERS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
650		$(cmd_ld_single)) \
651	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
652	$(cmd_objtool)
653
654rust-analyzer:
655	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
656		--cfgs='core=$(core-cfgs)' $(core-edition) \
657		--cfgs='zerocopy=$(zerocopy-cfgs)' \
658		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
659		--cfgs='quote=$(quote-cfgs)' \
660		--cfgs='syn=$(syn-cfgs)' \
661		--cfgs='zerocopy_derive=$(zerocopy_derive-cfgs)' \
662		--cfgs='pin_init_internal=$(pin_init_internal-cfgs)' \
663		--cfgs='pin_init=$(pin_init-cfgs)' \
664		--envs='zerocopy=$(zerocopy-envs)' \
665		$(realpath $(srctree)) $(realpath $(objtree)) \
666		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
667		> rust-project.json
668
669redirect-intrinsics = \
670	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
671	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
672	__muloti4 __multi3 \
673	__udivmodti4 __udivti3 __umodti3
674
675ifdef CONFIG_ARM
676	# Add eabi initrinsics for ARM 32-bit
677	redirect-intrinsics += \
678		__aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
679		__aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
680		__aeabi_uldivmod
681endif
682ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
683	# These intrinsics are defined for ARM64 and RISCV64
684	redirect-intrinsics += \
685		__ashrti3 \
686		__ashlti3 __lshrti3
687endif
688
689ifdef CONFIG_MODVERSIONS
690cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
691	$(call rust_exports,$@,"%s\n") | \
692	scripts/gendwarfksyms/gendwarfksyms \
693		$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
694		$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
695		$@ >> $(dot-target).cmd)
696endif
697
698define rule_rustc_library
699	$(call cmd_and_fixdep,rustc_library)
700	$(call cmd,gen_objtooldep)
701	$(call cmd,gendwarfksyms)
702endef
703
704define rule_rust_cc_library
705	$(call if_changed_rule,cc_o_c)
706	$(call cmd,force_checksrc)
707	$(call cmd,gendwarfksyms)
708endef
709
710# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
711# versions are calculated for the helpers too.
712$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
713	+$(call if_changed_rule,rust_cc_library)
714
715# Disable symbol versioning for exports.o to avoid conflicts with the actual
716# symbol versions generated from Rust objects.
717$(obj)/exports.o: private skip_gendwarfksyms = 1
718
719$(obj)/core.o: private skip_clippy = 1
720$(obj)/core.o: private skip_flags = $(core-skip_flags)
721$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
722$(obj)/core.o: private rustc_target_flags = $(core-flags)
723$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
724    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
725	+$(call if_changed_rule,rustc_library)
726ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
727$(obj)/core.o: scripts/target.json
728endif
729KCOV_INSTRUMENT_core.o := n
730
731$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
732$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
733$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
734	+$(call if_changed_rule,rustc_library)
735
736$(obj)/zerocopy.o: private skip_clippy = 1
737$(obj)/zerocopy.o: private skip_gendwarfksyms = 1
738$(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs)
739$(obj)/zerocopy.o: private rustc_target_flags = $(zerocopy-flags)
740$(obj)/zerocopy.o: $(src)/zerocopy/src/lib.rs $(obj)/compiler_builtins.o FORCE
741	+$(call if_changed_rule,rustc_library)
742
743$(obj)/pin_init.o: private skip_gendwarfksyms = 1
744$(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
745$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
746    $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
747	+$(call if_changed_rule,rustc_library)
748
749# Even if normally `build_error` is not a kernel object, it should still be
750# treated as such so that we pass the same flags. Otherwise, for instance,
751# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
752$(obj)/build_error.o: private is-kernel-object := y
753$(obj)/build_error.o: private skip_gendwarfksyms = 1
754$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
755	+$(call if_changed_rule,rustc_library)
756
757$(obj)/ffi.o: private skip_gendwarfksyms = 1
758$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
759	+$(call if_changed_rule,rustc_library)
760
761$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
762$(obj)/bindings.o: $(src)/bindings/lib.rs \
763    $(obj)/ffi.o \
764    $(obj)/pin_init.o \
765    $(obj)/bindings/bindings_generated.rs \
766    $(obj)/bindings/bindings_helpers_generated.rs FORCE
767	+$(call if_changed_rule,rustc_library)
768
769$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
770$(obj)/uapi.o: private skip_gendwarfksyms = 1
771$(obj)/uapi.o: $(src)/uapi/lib.rs \
772    $(obj)/ffi.o \
773    $(obj)/pin_init.o \
774    $(obj)/uapi/uapi_generated.rs FORCE
775	+$(call if_changed_rule,rustc_library)
776
777$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
778    --extern build_error --extern macros --extern bindings --extern uapi \
779    --extern zerocopy --extern zerocopy_derive
780$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
781    $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \
782    $(obj)/zerocopy.o $(obj)/$(libzerocopy_derive_name) FORCE
783	+$(call if_changed_rule,rustc_library)
784
785ifdef CONFIG_JUMP_LABEL
786$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
787endif
788ifndef CONFIG_UML
789ifdef CONFIG_BUG
790$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
791endif
792endif
793
794ifdef CONFIG_RUST_INLINE_HELPERS
795$(obj)/kernel.o: private link_helper = 1
796$(obj)/kernel.o: $(obj)/helpers/helpers.bc
797endif
798
799endif # CONFIG_RUST
800