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