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