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