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