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