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