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