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