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