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