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