xref: /linux/rust/Makefile (revision f59c074e76a6a9ea55818373decdf56c6fddca30)
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 \
358    --extern zerocopy_derive=$(objtree)/$(obj)/test/$(libzerocopy_derive_name)
359rusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
360    rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
361    $(obj)/bindings.o rusttestlib-zerocopy rusttestlib-zerocopy_derive FORCE
362	+$(call if_changed,rustc_test_library)
363
364rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
365rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
366	+$(call if_changed,rustc_test_library)
367
368rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
369rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
370	+$(call if_changed,rustc_test_library)
371
372quiet_cmd_rustdoc_test = RUSTDOC T $<
373      cmd_rustdoc_test = \
374	$(rustc_target_envs) \
375	RUST_MODFILE=test.rs \
376	OBJTREE=$(abspath $(objtree)) \
377	$(RUSTDOC) --test $(rust_common_flags) \
378		-Zcrate-attr='feature(used_with_arg)' \
379		@$(objtree)/include/generated/rustc_cfg \
380		$(rustc_target_flags) $(rustdoc_test_target_flags) \
381		$(rustdoc_test_quiet) \
382		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
383		--crate-name $(subst rusttest-,,$@) $<
384
385quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
386      cmd_rustdoc_test_kernel = \
387	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
388	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
389	$(rustc_target_envs) \
390	OBJTREE=$(abspath $(objtree)) \
391	$(RUSTDOC) --test $(filter-out --remap-path-scope=%,$(rust_flags)) \
392		-L$(objtree)/$(obj) --extern ffi --extern pin_init \
393		--extern kernel --extern build_error --extern macros \
394		--extern bindings --extern uapi \
395		--extern zerocopy --extern zerocopy_derive \
396		--no-run --crate-name kernel -Zunstable-options \
397		--sysroot=/dev/null \
398		$(doctests_modifiers_workaround) \
399		--test-builder $(objtree)/scripts/rustdoc_test_builder \
400		$< $(rustdoc_test_kernel_quiet); \
401	$(objtree)/scripts/rustdoc_test_gen
402
403%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
404    $(src)/kernel/lib.rs $(obj)/kernel.o \
405    $(objtree)/scripts/rustdoc_test_builder \
406    $(objtree)/scripts/rustdoc_test_gen FORCE
407	+$(call if_changed,rustdoc_test_kernel)
408
409# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
410# so for the moment we skip `-Cpanic=abort`.
411quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
412      cmd_rustc_test = \
413	$(rustc_target_envs) \
414	OBJTREE=$(abspath $(objtree)) \
415	$(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
416		@$(objtree)/include/generated/rustc_cfg \
417		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
418		-L$(objtree)/$(obj)/test \
419		--crate-name $(subst rusttest-,,$@) $<; \
420	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
421		$(rustc_test_run_flags)
422
423rusttest: rusttest-macros
424
425rusttest-macros: private rustc_target_flags = --extern proc_macro \
426    --extern macros --extern kernel --extern pin_init \
427    --extern proc_macro2 --extern quote --extern syn
428rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
429rusttest-macros: $(src)/macros/lib.rs \
430    rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
431	+$(call if_changed,rustc_test)
432	+$(call if_changed,rustdoc_test)
433
434ifdef CONFIG_CC_IS_CLANG
435bindgen_c_flags = $(c_flags)
436else
437# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
438# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
439#
440# For the moment, here we are tweaking the flags on the fly. This is a hack,
441# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
442# if we end up using one of those structs).
443bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
444	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
445	-mindirect-branch=thunk-extern -mindirect-branch-register \
446	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
447	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
448	-mno-pointers-to-nested-functions -mno-string \
449	-mno-strict-align -mstrict-align -mdirect-extern-access \
450	-mexplicit-relocs -mno-check-zero-division \
451	-fconserve-stack -falign-jumps=% -falign-loops=% \
452	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
453	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
454	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
455	-fzero-call-used-regs=% -fno-stack-clash-protection \
456	-fno-inline-functions-called-once -fsanitize=bounds-strict \
457	-fstrict-flex-arrays=% -fmin-function-alignment=% \
458	-fzero-init-padding-bits=% -mno-fdpic \
459	-fdiagnostics-show-context -fdiagnostics-show-context=% \
460	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference \
461	-ffixed-r2 -mmultiple -mno-readonly-in-sdata
462
463# Derived from `scripts/Makefile.clang`.
464BINDGEN_TARGET_x86	:= x86_64-linux-gnu
465BINDGEN_TARGET_arm64	:= aarch64-linux-gnu
466BINDGEN_TARGET_arm	:= arm-linux-gnueabi
467BINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
468BINDGEN_TARGET_s390	:= s390x-linux-gnu
469# This is only for i386 UM builds, which need the 32-bit target not -m32
470BINDGEN_TARGET_i386	:= i386-linux-gnu
471
472ifdef CONFIG_PPC64
473BINDGEN_TARGET_powerpc	:= powerpc64le-linux-gnu
474else
475BINDGEN_TARGET_powerpc	:= powerpc-linux-gnu
476endif
477
478BINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
479BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
480
481# All warnings are inhibited since GCC builds are very experimental,
482# many GCC warnings are not supported by Clang, they may only appear in
483# some configurations, with new GCC versions, etc.
484bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
485
486# Auto variable zero-initialization requires an additional special option with
487# clang that is going to be removed sometime in the future (likely in
488# clang-18), so make sure to pass this option only if clang supports it
489# (libclang major version < 16).
490#
491# https://github.com/llvm/llvm-project/issues/44842
492# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
493ifdef CONFIG_INIT_STACK_ALL_ZERO
494libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
495ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
496bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
497endif
498endif
499
500bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
501	$(bindgen_extra_c_flags)
502endif
503
504ifdef CONFIG_LTO
505bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
506else
507bindgen_c_flags_lto = $(bindgen_c_flags)
508endif
509
510# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
511# prototypes for functions like `memcpy` -- if this flag is not passed,
512# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
513# architecture instead of generating `usize`.
514bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
515
516# `--rust-target` points to our minimum supported Rust version.
517quiet_cmd_bindgen = BINDGEN $@
518      cmd_bindgen = \
519	$(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.85 \
520		--use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
521		--no-debug '.*' --enable-function-attribute-detection \
522		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
523		$(bindgen_target_cflags) $(bindgen_target_extra)
524
525$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
526    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
527$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
528    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
529$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
530    $(src)/bindgen_parameters FORCE
531	$(call if_changed_dep,bindgen)
532
533$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
534    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
535$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
536    $(src)/bindgen_parameters FORCE
537	$(call if_changed_dep,bindgen)
538
539# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
540# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
541# given it is `libclang`; but for consistency, future Clang changes and/or
542# a potential future GCC backend for `bindgen`, we disable it too.
543$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
544    --blocklist-type '.*' --allowlist-var '' \
545    --allowlist-function 'rust_helper_.*'
546$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
547    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
548$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
549    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
550$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
551	$(call if_changed_dep,bindgen)
552
553quiet_cmd_rust_helper = HELPER  $@
554      cmd_rust_helper = \
555	$(CC) $(filter-out $(CFLAGS_REMOVE_helpers/helpers.o), $(c_flags)) \
556		-c -g0 $< -emit-llvm -o $@
557
558$(obj)/helpers/helpers.bc: private part-of-builtin := y
559$(obj)/helpers/helpers_module.bc: private part-of-module := y
560$(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpers.c FORCE
561	+$(call if_changed_dep,rust_helper)
562
563rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
564
565quiet_cmd_exports = EXPORTS $@
566      cmd_exports = \
567	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
568
569$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
570	$(call if_changed,exports)
571
572# Even though Rust kernel modules should never use the bindings directly,
573# symbols from the `bindings` crate and the C helpers need to be exported
574# because Rust generics and inlined functions may not get their code generated
575# in the crate where they are defined. Other helpers, called from non-inline
576# functions, may not be exported, in principle. However, in general, the Rust
577# compiler does not guarantee codegen will be performed for a non-inline
578# function either. Therefore, we export all symbols from helpers and bindings.
579# In the future, this may be revisited to reduce the number of exports after
580# the compiler is informed about the places codegen is required.
581$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
582	$(call if_changed,exports)
583
584$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
585	$(call if_changed,exports)
586
587$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
588	$(call if_changed,exports)
589
590quiet_cmd_rustc_procmacrolibrary = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) PL $@
591      cmd_rustc_procmacrolibrary = \
592	$(rustc_target_envs) \
593	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
594		$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
595		--emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \
596		--out-dir $(objtree)/$(obj)/host -L$(objtree)/$(obj)/host \
597		--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<
598
599$(obj)/host/libproc_macro2.rlib: private skip_clippy = 1
600$(obj)/host/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
601$(obj)/host/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
602	+$(call if_changed_dep,rustc_procmacrolibrary)
603
604$(obj)/host/libquote.rlib: private skip_clippy = 1
605$(obj)/host/libquote.rlib: private skip_flags = $(quote-skip_flags)
606$(obj)/host/libquote.rlib: private rustc_target_flags = $(quote-flags)
607$(obj)/host/libquote.rlib: $(src)/quote/lib.rs $(obj)/host/libproc_macro2.rlib FORCE
608	+$(call if_changed_dep,rustc_procmacrolibrary)
609
610$(obj)/host/libsyn.rlib: private skip_clippy = 1
611$(obj)/host/libsyn.rlib: private rustc_target_flags = $(syn-flags)
612$(obj)/host/libsyn.rlib: $(src)/syn/lib.rs $(obj)/host/libquote.rlib FORCE
613	+$(call if_changed_dep,rustc_procmacrolibrary)
614
615quiet_cmd_rustc_procmacro = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) P $@
616      cmd_rustc_procmacro = \
617	$(rustc_target_envs) \
618	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) $(rust_common_flags) $(rustc_target_flags) \
619		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
620		-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
621		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
622		--crate-type proc-macro -L$(objtree)/$(obj)/host \
623		--crate-name $(patsubst lib%.$(procmacro-extension),%,$(notdir $@)) \
624		@$(objtree)/include/generated/rustc_cfg $<
625
626# Procedural macros can only be used with the `rustc` that compiled it.
627$(obj)/$(libzerocopy_derive_name): private skip_clippy = 1
628$(obj)/$(libzerocopy_derive_name): private rustc_target_flags = $(zerocopy_derive-flags)
629$(obj)/$(libzerocopy_derive_name): $(src)/zerocopy-derive/lib.rs $(obj)/host/libproc_macro2.rlib \
630    $(obj)/host/libquote.rlib $(obj)/host/libsyn.rlib FORCE
631	+$(call if_changed_dep,rustc_procmacro)
632
633$(obj)/$(libmacros_name): private rustc_target_flags = \
634    --extern proc_macro2 --extern quote --extern syn
635$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/host/libproc_macro2.rlib \
636    $(obj)/host/libquote.rlib $(obj)/host/libsyn.rlib FORCE
637	+$(call if_changed_dep,rustc_procmacro)
638
639$(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
640$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
641    $(obj)/host/libproc_macro2.rlib $(obj)/host/libquote.rlib $(obj)/host/libsyn.rlib FORCE
642	+$(call if_changed_dep,rustc_procmacro)
643
644# `rustc` requires `-Zunstable-options` to use custom target specifications
645# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
646quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
647      cmd_rustc_library = \
648	$(rustc_target_envs) \
649	OBJTREE=$(abspath $(objtree)) \
650	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
651		$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
652		--emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
653		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
654		--crate-type rlib -L$(objtree)/$(obj) \
655		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
656		--sysroot=/dev/null \
657		-Zunstable-options \
658	$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
659		$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
660		$(CC) $(CLANG_FLAGS) $(filter-out $(CC_FLAGS_LTO),$(KBUILD_CFLAGS)) \
661		$(CC_FLAGS_RUST_INLINE_HELPERS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
662		$(cmd_ld_single)) \
663	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
664	$(cmd_objtool)
665
666rust-analyzer:
667	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
668		--cfgs='core=$(core-cfgs)' $(core-edition) \
669		--cfgs='zerocopy=$(zerocopy-cfgs)' \
670		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
671		--cfgs='quote=$(quote-cfgs)' \
672		--cfgs='syn=$(syn-cfgs)' \
673		--cfgs='zerocopy_derive=$(zerocopy_derive-cfgs)' \
674		--cfgs='pin_init_internal=$(pin_init_internal-cfgs)' \
675		--cfgs='pin_init=$(pin_init-cfgs)' \
676		--envs='zerocopy=$(zerocopy-envs)' \
677		$(realpath $(srctree)) $(realpath $(objtree)) \
678		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
679		> rust-project.json
680
681redirect-intrinsics = \
682	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
683	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
684	__muloti4 __multi3 \
685	__udivmodti4 __udivti3 __umodti3
686
687ifdef CONFIG_ARM
688	# Add eabi initrinsics for ARM 32-bit
689	redirect-intrinsics += \
690		__aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
691		__aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
692		__aeabi_uldivmod
693endif
694ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
695	# These intrinsics are defined for ARM64 and RISCV64
696	redirect-intrinsics += \
697		__ashrti3 \
698		__ashlti3 __lshrti3
699endif
700ifdef CONFIG_PPC32
701	redirect-intrinsics += \
702		__udivdi3 __umoddi3
703endif
704
705ifdef CONFIG_MODVERSIONS
706cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
707	$(call rust_exports,$@,"%s\n") | \
708	scripts/gendwarfksyms/gendwarfksyms \
709		$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
710		$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
711		$@ >> $(dot-target).cmd)
712endif
713
714define rule_rustc_library
715	$(call cmd_and_fixdep,rustc_library)
716	$(call cmd,gen_objtooldep)
717	$(call cmd,gendwarfksyms)
718endef
719
720define rule_rust_cc_library
721	$(call if_changed_rule,cc_o_c)
722	$(call cmd,force_checksrc)
723	$(call cmd,gendwarfksyms)
724endef
725
726# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
727# versions are calculated for the helpers too.
728$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
729	+$(call if_changed_rule,rust_cc_library)
730
731# Disable symbol versioning for exports.o to avoid conflicts with the actual
732# symbol versions generated from Rust objects.
733$(obj)/exports.o: private skip_gendwarfksyms = 1
734
735$(obj)/core.o: private skip_clippy = 1
736$(obj)/core.o: private skip_flags = $(core-skip_flags)
737$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
738$(obj)/core.o: private rustc_target_flags = $(core-flags)
739$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
740    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
741	+$(call if_changed_rule,rustc_library)
742ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
743$(obj)/core.o: scripts/target.json
744endif
745KCOV_INSTRUMENT_core.o := n
746
747$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
748$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
749$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
750	+$(call if_changed_rule,rustc_library)
751
752$(obj)/zerocopy.o: private skip_clippy = 1
753$(obj)/zerocopy.o: private skip_gendwarfksyms = 1
754$(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs)
755$(obj)/zerocopy.o: private rustc_target_flags = $(zerocopy-flags)
756$(obj)/zerocopy.o: $(src)/zerocopy/src/lib.rs $(obj)/compiler_builtins.o FORCE
757	+$(call if_changed_rule,rustc_library)
758
759$(obj)/pin_init.o: private skip_gendwarfksyms = 1
760$(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
761$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
762    $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
763	+$(call if_changed_rule,rustc_library)
764
765# Even if normally `build_error` is not a kernel object, it should still be
766# treated as such so that we pass the same flags. Otherwise, for instance,
767# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
768$(obj)/build_error.o: private is-kernel-object := y
769$(obj)/build_error.o: private skip_gendwarfksyms = 1
770$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
771	+$(call if_changed_rule,rustc_library)
772
773$(obj)/ffi.o: private skip_gendwarfksyms = 1
774$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
775	+$(call if_changed_rule,rustc_library)
776
777$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
778$(obj)/bindings.o: $(src)/bindings/lib.rs \
779    $(obj)/ffi.o \
780    $(obj)/pin_init.o \
781    $(obj)/bindings/bindings_generated.rs \
782    $(obj)/bindings/bindings_helpers_generated.rs FORCE
783	+$(call if_changed_rule,rustc_library)
784
785$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
786$(obj)/uapi.o: private skip_gendwarfksyms = 1
787$(obj)/uapi.o: $(src)/uapi/lib.rs \
788    $(obj)/ffi.o \
789    $(obj)/pin_init.o \
790    $(obj)/uapi/uapi_generated.rs FORCE
791	+$(call if_changed_rule,rustc_library)
792
793$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
794    --extern build_error --extern macros --extern bindings --extern uapi \
795    --extern zerocopy --extern zerocopy_derive
796$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
797    $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \
798    $(obj)/zerocopy.o $(obj)/$(libzerocopy_derive_name) FORCE
799	+$(call if_changed_rule,rustc_library)
800
801ifdef CONFIG_JUMP_LABEL
802$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
803endif
804ifndef CONFIG_UML
805ifdef CONFIG_BUG
806$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
807endif
808endif
809
810ifdef CONFIG_RUST_INLINE_HELPERS
811$(obj)/kernel.o: private link_helper = 1
812$(obj)/kernel.o: $(obj)/helpers/helpers.bc
813endif
814
815endif # CONFIG_RUST
816