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