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