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