xref: /linux/rust/Makefile (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
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=$(objtree)/$(obj)/test/libzerocopy.rlib --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	-ffixed-r2 -mmultiple -mno-readonly-in-sdata
461
462# Derived from `scripts/Makefile.clang`.
463BINDGEN_TARGET_x86	:= x86_64-linux-gnu
464BINDGEN_TARGET_arm64	:= aarch64-linux-gnu
465BINDGEN_TARGET_arm	:= arm-linux-gnueabi
466BINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
467BINDGEN_TARGET_s390	:= s390x-linux-gnu
468# This is only for i386 UM builds, which need the 32-bit target not -m32
469BINDGEN_TARGET_i386	:= i386-linux-gnu
470
471ifdef CONFIG_PPC64
472BINDGEN_TARGET_powerpc	:= powerpc64le-linux-gnu
473else
474BINDGEN_TARGET_powerpc	:= powerpc-linux-gnu
475endif
476
477BINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
478BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
479
480# All warnings are inhibited since GCC builds are very experimental,
481# many GCC warnings are not supported by Clang, they may only appear in
482# some configurations, with new GCC versions, etc.
483bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
484
485# Auto variable zero-initialization requires an additional special option with
486# clang that is going to be removed sometime in the future (likely in
487# clang-18), so make sure to pass this option only if clang supports it
488# (libclang major version < 16).
489#
490# https://github.com/llvm/llvm-project/issues/44842
491# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
492ifdef CONFIG_INIT_STACK_ALL_ZERO
493libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
494ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
495bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
496endif
497endif
498
499bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
500	$(bindgen_extra_c_flags)
501endif
502
503ifdef CONFIG_LTO
504bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
505else
506bindgen_c_flags_lto = $(bindgen_c_flags)
507endif
508
509# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
510# prototypes for functions like `memcpy` -- if this flag is not passed,
511# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
512# architecture instead of generating `usize`.
513bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
514
515# `--rust-target` points to our minimum supported Rust version.
516quiet_cmd_bindgen = BINDGEN $@
517      cmd_bindgen = \
518	$(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.85 \
519		--use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
520		--no-debug '.*' --enable-function-attribute-detection \
521		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
522		$(bindgen_target_cflags) $(bindgen_target_extra)
523
524$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
525    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
526$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
527    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
528$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
529    $(src)/bindgen_parameters FORCE
530	$(call if_changed_dep,bindgen)
531
532$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
533    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
534$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
535    $(src)/bindgen_parameters FORCE
536	$(call if_changed_dep,bindgen)
537
538# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
539# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
540# given it is `libclang`; but for consistency, future Clang changes and/or
541# a potential future GCC backend for `bindgen`, we disable it too.
542$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
543    --blocklist-type '.*' --allowlist-var '' \
544    --allowlist-function 'rust_helper_.*'
545$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
546    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
547$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
548    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
549$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
550	$(call if_changed_dep,bindgen)
551
552quiet_cmd_rust_helper = HELPER  $@
553      cmd_rust_helper = \
554	$(CC) $(filter-out $(CFLAGS_REMOVE_helpers/helpers.o), $(c_flags)) \
555		-c -g0 $< -emit-llvm -o $@
556
557$(obj)/helpers/helpers.bc: private part-of-builtin := y
558$(obj)/helpers/helpers_module.bc: private part-of-module := y
559$(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpers.c FORCE
560	+$(call if_changed_dep,rust_helper)
561
562rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
563
564quiet_cmd_exports = EXPORTS $@
565      cmd_exports = \
566	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
567
568$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
569	$(call if_changed,exports)
570
571# Even though Rust kernel modules should never use the bindings directly,
572# symbols from the `bindings` crate and the C helpers need to be exported
573# because Rust generics and inlined functions may not get their code generated
574# in the crate where they are defined. Other helpers, called from non-inline
575# functions, may not be exported, in principle. However, in general, the Rust
576# compiler does not guarantee codegen will be performed for a non-inline
577# function either. Therefore, we export all symbols from helpers and bindings.
578# In the future, this may be revisited to reduce the number of exports after
579# the compiler is informed about the places codegen is required.
580$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
581	$(call if_changed,exports)
582
583$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
584	$(call if_changed,exports)
585
586$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
587	$(call if_changed,exports)
588
589quiet_cmd_rustc_procmacrolibrary = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) PL $@
590      cmd_rustc_procmacrolibrary = \
591	$(rustc_target_envs) \
592	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
593		$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
594		--emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \
595		--out-dir $(objtree)/$(obj)/host -L$(objtree)/$(obj)/host \
596		--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<
597
598$(obj)/host/libproc_macro2.rlib: private skip_clippy = 1
599$(obj)/host/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
600$(obj)/host/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
601	+$(call if_changed_dep,rustc_procmacrolibrary)
602
603$(obj)/host/libquote.rlib: private skip_clippy = 1
604$(obj)/host/libquote.rlib: private skip_flags = $(quote-skip_flags)
605$(obj)/host/libquote.rlib: private rustc_target_flags = $(quote-flags)
606$(obj)/host/libquote.rlib: $(src)/quote/lib.rs $(obj)/host/libproc_macro2.rlib FORCE
607	+$(call if_changed_dep,rustc_procmacrolibrary)
608
609$(obj)/host/libsyn.rlib: private skip_clippy = 1
610$(obj)/host/libsyn.rlib: private rustc_target_flags = $(syn-flags)
611$(obj)/host/libsyn.rlib: $(src)/syn/lib.rs $(obj)/host/libquote.rlib FORCE
612	+$(call if_changed_dep,rustc_procmacrolibrary)
613
614quiet_cmd_rustc_procmacro = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) P $@
615      cmd_rustc_procmacro = \
616	$(rustc_target_envs) \
617	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) $(rust_common_flags) $(rustc_target_flags) \
618		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
619		-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
620		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
621		--crate-type proc-macro -L$(objtree)/$(obj)/host \
622		--crate-name $(patsubst lib%.$(procmacro-extension),%,$(notdir $@)) \
623		@$(objtree)/include/generated/rustc_cfg $<
624
625# Procedural macros can only be used with the `rustc` that compiled it.
626$(obj)/$(libzerocopy_derive_name): private skip_clippy = 1
627$(obj)/$(libzerocopy_derive_name): private rustc_target_flags = $(zerocopy_derive-flags)
628$(obj)/$(libzerocopy_derive_name): $(src)/zerocopy-derive/lib.rs $(obj)/host/libproc_macro2.rlib \
629    $(obj)/host/libquote.rlib $(obj)/host/libsyn.rlib FORCE
630	+$(call if_changed_dep,rustc_procmacro)
631
632$(obj)/$(libmacros_name): private rustc_target_flags = \
633    --extern proc_macro2 --extern quote --extern syn
634$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/host/libproc_macro2.rlib \
635    $(obj)/host/libquote.rlib $(obj)/host/libsyn.rlib FORCE
636	+$(call if_changed_dep,rustc_procmacro)
637
638$(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
639$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
640    $(obj)/host/libproc_macro2.rlib $(obj)/host/libquote.rlib $(obj)/host/libsyn.rlib FORCE
641	+$(call if_changed_dep,rustc_procmacro)
642
643# `rustc` requires `-Zunstable-options` to use custom target specifications
644# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
645quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
646      cmd_rustc_library = \
647	$(rustc_target_envs) \
648	OBJTREE=$(abspath $(objtree)) \
649	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
650		$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
651		--emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
652		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
653		--crate-type rlib -L$(objtree)/$(obj) \
654		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
655		--sysroot=/dev/null \
656		-Zunstable-options \
657	$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
658		$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
659		$(CC) $(CLANG_FLAGS) $(KBUILD_CFLAGS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
660		$(cmd_ld_single)) \
661	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
662	$(cmd_objtool)
663
664rust-analyzer:
665	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
666		--cfgs='core=$(core-cfgs)' $(core-edition) \
667		--cfgs='zerocopy=$(zerocopy-cfgs)' \
668		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
669		--cfgs='quote=$(quote-cfgs)' \
670		--cfgs='syn=$(syn-cfgs)' \
671		--cfgs='zerocopy_derive=$(zerocopy_derive-cfgs)' \
672		--cfgs='pin_init_internal=$(pin_init_internal-cfgs)' \
673		--cfgs='pin_init=$(pin_init-cfgs)' \
674		--envs='zerocopy=$(zerocopy-envs)' \
675		$(realpath $(srctree)) $(realpath $(objtree)) \
676		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
677		> rust-project.json
678
679redirect-intrinsics = \
680	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
681	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
682	__muloti4 __multi3 \
683	__udivmodti4 __udivti3 __umodti3
684
685ifdef CONFIG_ARM
686	# Add eabi initrinsics for ARM 32-bit
687	redirect-intrinsics += \
688		__aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
689		__aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
690		__aeabi_uldivmod
691endif
692ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
693	# These intrinsics are defined for ARM64 and RISCV64
694	redirect-intrinsics += \
695		__ashrti3 \
696		__ashlti3 __lshrti3
697endif
698ifdef CONFIG_PPC32
699	redirect-intrinsics += \
700		__udivdi3 __umoddi3
701endif
702
703ifdef CONFIG_MODVERSIONS
704cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
705	$(call rust_exports,$@,"%s\n") | \
706	scripts/gendwarfksyms/gendwarfksyms \
707		$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
708		$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
709		$@ >> $(dot-target).cmd)
710endif
711
712define rule_rustc_library
713	$(call cmd_and_fixdep,rustc_library)
714	$(call cmd,gen_objtooldep)
715	$(call cmd,gendwarfksyms)
716endef
717
718define rule_rust_cc_library
719	$(call if_changed_rule,cc_o_c)
720	$(call cmd,force_checksrc)
721	$(call cmd,gendwarfksyms)
722endef
723
724# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
725# versions are calculated for the helpers too.
726$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
727	+$(call if_changed_rule,rust_cc_library)
728
729# Disable symbol versioning for exports.o to avoid conflicts with the actual
730# symbol versions generated from Rust objects.
731$(obj)/exports.o: private skip_gendwarfksyms = 1
732
733$(obj)/core.o: private skip_clippy = 1
734$(obj)/core.o: private skip_flags = $(core-skip_flags)
735$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
736$(obj)/core.o: private rustc_target_flags = $(core-flags)
737$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
738    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
739	+$(call if_changed_rule,rustc_library)
740ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
741$(obj)/core.o: scripts/target.json
742endif
743KCOV_INSTRUMENT_core.o := n
744
745$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
746$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
747$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
748	+$(call if_changed_rule,rustc_library)
749
750$(obj)/zerocopy.o: private skip_clippy = 1
751$(obj)/zerocopy.o: private skip_gendwarfksyms = 1
752$(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs)
753$(obj)/zerocopy.o: private rustc_target_flags = $(zerocopy-flags)
754$(obj)/zerocopy.o: $(src)/zerocopy/src/lib.rs $(obj)/compiler_builtins.o FORCE
755	+$(call if_changed_rule,rustc_library)
756
757$(obj)/pin_init.o: private skip_gendwarfksyms = 1
758$(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
759$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
760    $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
761	+$(call if_changed_rule,rustc_library)
762
763# Even if normally `build_error` is not a kernel object, it should still be
764# treated as such so that we pass the same flags. Otherwise, for instance,
765# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
766$(obj)/build_error.o: private is-kernel-object := y
767$(obj)/build_error.o: private skip_gendwarfksyms = 1
768$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
769	+$(call if_changed_rule,rustc_library)
770
771$(obj)/ffi.o: private skip_gendwarfksyms = 1
772$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
773	+$(call if_changed_rule,rustc_library)
774
775$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
776$(obj)/bindings.o: $(src)/bindings/lib.rs \
777    $(obj)/ffi.o \
778    $(obj)/pin_init.o \
779    $(obj)/bindings/bindings_generated.rs \
780    $(obj)/bindings/bindings_helpers_generated.rs FORCE
781	+$(call if_changed_rule,rustc_library)
782
783$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
784$(obj)/uapi.o: private skip_gendwarfksyms = 1
785$(obj)/uapi.o: $(src)/uapi/lib.rs \
786    $(obj)/ffi.o \
787    $(obj)/pin_init.o \
788    $(obj)/uapi/uapi_generated.rs FORCE
789	+$(call if_changed_rule,rustc_library)
790
791$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
792    --extern build_error --extern macros --extern bindings --extern uapi \
793    --extern zerocopy --extern zerocopy_derive
794$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
795    $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \
796    $(obj)/zerocopy.o $(obj)/$(libzerocopy_derive_name) FORCE
797	+$(call if_changed_rule,rustc_library)
798
799ifdef CONFIG_JUMP_LABEL
800$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
801endif
802ifndef CONFIG_UML
803ifdef CONFIG_BUG
804$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
805endif
806endif
807
808ifdef CONFIG_RUST_INLINE_HELPERS
809$(obj)/kernel.o: private link_helper = 1
810$(obj)/kernel.o: $(obj)/helpers/helpers.bc
811endif
812
813endif # CONFIG_RUST
814