xref: /linux/rust/Makefile (revision 425e10586cf12b86af076d1746bb87b0bb3f18a1)
12f7ab126SMiguel Ojeda# SPDX-License-Identifier: GPL-2.0
22f7ab126SMiguel Ojeda
348fadf44SCarlos Bilbao# Where to place rustdoc generated documentation
448fadf44SCarlos Bilbaorustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
548fadf44SCarlos Bilbao
6d072acdaSGary Guoobj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o
72f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += exports_core_generated.h
82f7ab126SMiguel Ojeda
956762152SMiguel Ojedaobj-$(CONFIG_RUST) += zerocopy.o
1056762152SMiguel Ojeda
113a2486ccSGary Guoifdef CONFIG_RUST_INLINE_HELPERS
123a2486ccSGary Guoalways-$(CONFIG_RUST) += helpers/helpers.bc helpers/helpers_module.bc
133a2486ccSGary Guoelse
143a2486ccSGary Guoobj-$(CONFIG_RUST) += helpers/helpers.o
153a2486ccSGary Guoalways-$(CONFIG_RUST) += exports_helpers_generated.h
163a2486ccSGary Guoendif
172f7ab126SMiguel Ojeda# Missing prototypes are expected in the helpers since these are exported
182f7ab126SMiguel Ojeda# for Rust only, thus there is no header nor prototypes.
1987634653SAndreas HindborgCFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
202f7ab126SMiguel Ojeda
212f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
22d7659accSMiguel Ojedaobj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o
233a2486ccSGary Guoalways-$(CONFIG_RUST) += exports_bindings_generated.h exports_kernel_generated.h
242f7ab126SMiguel Ojeda
254e174665SAsahi Linaalways-$(CONFIG_RUST) += uapi/uapi_generated.rs
264e174665SAsahi Linaobj-$(CONFIG_RUST) += uapi.o
274e174665SAsahi Lina
28ecaa6ddfSGary Guoifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
29ecaa6ddfSGary Guoobj-$(CONFIG_RUST) += build_error.o
30ecaa6ddfSGary Guoelse
31ecaa6ddfSGary Guoalways-$(CONFIG_RUST) += build_error.o
32ecaa6ddfSGary Guoendif
33ecaa6ddfSGary Guo
342f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += exports.o
352f7ab126SMiguel Ojeda
3673740175SMiguel Ojedaalways-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
37158a3b72SMiguel Ojeda
38a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
39a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
40a66d733dSMiguel Ojeda
41a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
42a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
43a66d733dSMiguel Ojeda
448af7a501SMiguel Ojedaalways-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
45dff64b07SFUJITA Tomonoriifndef CONFIG_UML
46dff64b07SFUJITA Tomonorialways-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
47dff64b07SFUJITA Tomonoriendif
48169484abSAlice Ryhl
490730422bSTamir Duberstein# Avoids running `$(RUSTC)` when it may not be available.
502f7ab126SMiguel Ojedaifdef CONFIG_RUST
512f7ab126SMiguel Ojeda
5250f3637aSMiguel Ojedaprocmacro-name = $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name $(1) --crate-type proc-macro - </dev/null)
532846ebc8SMiguel Ojedaprocmacro-extension := $(patsubst libname.%,%,$(call procmacro-name,name))
5450f3637aSMiguel Ojeda
5550605498SMiguel Ojedalibzerocopy_derive_name := $(call procmacro-name,zerocopy_derive)
5650f3637aSMiguel Ojedalibmacros_name := $(call procmacro-name,macros)
5750f3637aSMiguel Ojedalibpin_init_internal_name := $(call procmacro-name,pin_init_internal)
58d7659accSMiguel Ojeda
5950605498SMiguel Ojedaalways-$(CONFIG_RUST) += $(libzerocopy_derive_name) $(libmacros_name) $(libpin_init_internal_name)
600730422bSTamir Duberstein
612f7ab126SMiguel Ojeda# `$(rust_flags)` is passed in case the user added `--sysroot`.
62ecab4115SMiguel Ojedarustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
632f7ab126SMiguel Ojedarustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
642f7ab126SMiguel OjedaRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
652f7ab126SMiguel Ojeda
669ffc80c8SMiguel Ojedaifneq ($(quiet),)
672f7ab126SMiguel Ojedarust_test_quiet=-q
682f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q
69a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null
702f7ab126SMiguel Ojedaendif
712f7ab126SMiguel Ojeda
721181c974SMiguel Ojedacfgs-to-flags = $(patsubst %,--cfg='%',$1)
731181c974SMiguel Ojeda
7446e58a96SMiguel Ojedacore-cfgs := \
751181c974SMiguel Ojeda    no_fp_fmt_parse
762f7ab126SMiguel Ojeda
77f4daa80dSGary Guocore-edition := $(if $(call rustc-min-version,108700),2024,2021)
78f4daa80dSGary Guo
7946e58a96SMiguel Ojedacore-skip_flags := \
8046e58a96SMiguel Ojeda    --edition=2021 \
810f6e1e07SMiguel Ojeda    -Wunreachable_pub
8246e58a96SMiguel Ojeda
8346e58a96SMiguel Ojedacore-flags := \
8446e58a96SMiguel Ojeda    --edition=$(core-edition) \
851181c974SMiguel Ojeda    $(call cfgs-to-flags,$(core-cfgs))
8646e58a96SMiguel Ojeda
872808a396SMiguel Ojedazerocopy-cfgs := \
882808a396SMiguel Ojeda    no_fp_fmt_parse
892808a396SMiguel Ojeda
9056762152SMiguel Ojedazerocopy-flags := \
912808a396SMiguel Ojeda    --cap-lints=allow \
922808a396SMiguel Ojeda    $(call cfgs-to-flags,$(zerocopy-cfgs))
9356762152SMiguel Ojeda
9456762152SMiguel Ojedazerocopy-envs := \
95*425e1058SMiguel Ojeda    CARGO_PKG_VERSION=0.8.54
9656762152SMiguel Ojeda
97158a3b72SMiguel Ojedaproc_macro2-cfgs := \
98158a3b72SMiguel Ojeda    feature="proc-macro" \
99158a3b72SMiguel Ojeda    wrap_proc_macro \
100158a3b72SMiguel Ojeda    $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
101158a3b72SMiguel Ojeda
102158a3b72SMiguel Ojedaproc_macro2-flags := \
103158a3b72SMiguel Ojeda    --cap-lints=allow \
104158a3b72SMiguel Ojeda    $(call cfgs-to-flags,$(proc_macro2-cfgs))
105158a3b72SMiguel Ojeda
10688de91ccSMiguel Ojedaquote-cfgs := \
10788de91ccSMiguel Ojeda    feature="proc-macro"
10888de91ccSMiguel Ojeda
10988de91ccSMiguel Ojedaquote-skip_flags := \
11088de91ccSMiguel Ojeda    --edition=2021
11188de91ccSMiguel Ojeda
11288de91ccSMiguel Ojedaquote-flags := \
11388de91ccSMiguel Ojeda    --edition=2018 \
11488de91ccSMiguel Ojeda    --cap-lints=allow \
11588de91ccSMiguel Ojeda    --extern proc_macro2 \
11688de91ccSMiguel Ojeda    $(call cfgs-to-flags,$(quote-cfgs))
11788de91ccSMiguel Ojeda
11873740175SMiguel Ojeda# `extra-traits`, `fold` and `visit` may be enabled if needed.
11973740175SMiguel Ojedasyn-cfgs := \
12073740175SMiguel Ojeda    feature="clone-impls" \
12173740175SMiguel Ojeda    feature="derive" \
12273740175SMiguel Ojeda    feature="full" \
12373740175SMiguel Ojeda    feature="parsing" \
12473740175SMiguel Ojeda    feature="printing" \
12573740175SMiguel Ojeda    feature="proc-macro" \
126e189bdb6SGary Guo    feature="visit" \
12773740175SMiguel Ojeda    feature="visit-mut"
12873740175SMiguel Ojeda
12973740175SMiguel Ojedasyn-flags := \
13073740175SMiguel Ojeda    --cap-lints=allow \
13173740175SMiguel Ojeda    --extern proc_macro2 \
13273740175SMiguel Ojeda    --extern quote \
13373740175SMiguel Ojeda    $(call cfgs-to-flags,$(syn-cfgs))
13473740175SMiguel Ojeda
1352808a396SMiguel Ojedazerocopy_derive-cfgs := \
1362808a396SMiguel Ojeda    zerocopy_unstable_linux
1372808a396SMiguel Ojeda
13850605498SMiguel Ojedazerocopy_derive-flags := \
13950605498SMiguel Ojeda    --cap-lints=allow \
14050605498SMiguel Ojeda    --extern proc_macro2 \
14150605498SMiguel Ojeda    --extern quote \
1422808a396SMiguel Ojeda    --extern syn \
1432808a396SMiguel Ojeda    $(call cfgs-to-flags,$(zerocopy_derive-cfgs))
14450605498SMiguel Ojeda
145abbe9459STamir Dubersteinpin_init_internal-cfgs := \
146002a121bSGary Guo    kernel USE_RUSTC_FEATURES
147abbe9459STamir Duberstein
148abbe9459STamir Dubersteinpin_init_internal-flags := \
149abbe9459STamir Duberstein    --extern proc_macro2 \
150abbe9459STamir Duberstein    --extern quote \
151abbe9459STamir Duberstein    --extern syn \
152abbe9459STamir Duberstein    $(call cfgs-to-flags,$(pin_init_internal-cfgs))
153abbe9459STamir Duberstein
154abbe9459STamir Dubersteinpin_init-cfgs := \
155002a121bSGary Guo    kernel USE_RUSTC_FEATURES
156abbe9459STamir Duberstein
157abbe9459STamir Dubersteinpin_init-flags := \
158abbe9459STamir Duberstein    --extern pin_init_internal \
159abbe9459STamir Duberstein    --extern macros \
160abbe9459STamir Duberstein    $(call cfgs-to-flags,$(pin_init-cfgs))
161abbe9459STamir Duberstein
162abbf9a44SMiguel Ojeda# `rustdoc` did not save the target modifiers, thus workaround for
163abbf9a44SMiguel Ojeda# the time being (https://github.com/rust-lang/rust/issues/144521).
164abbf9a44SMiguel Ojedarustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
165abbf9a44SMiguel Ojeda
166fad472efSMiguel Ojeda# Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465).
167fad472efSMiguel Ojedadoctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer)
168fad472efSMiguel Ojeda
1692f7ab126SMiguel Ojedaquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
1702f7ab126SMiguel Ojeda      cmd_rustdoc = \
1710ba60f71SMiguel Ojeda	$(rustc_target_envs) \
1722f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
173b06b348eSMiguel Ojeda	$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-scope=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
1742f7ab126SMiguel Ojeda		$(rustc_target_flags) -L$(objtree)/$(obj) \
1756e6efc5fSMiguel Ojeda		-Zunstable-options --generate-link-to-definition \
17648fadf44SCarlos Bilbao		--output $(rustdoc_output) \
1772f7ab126SMiguel Ojeda		--crate-name $(subst rustdoc-,,$@) \
178abbf9a44SMiguel Ojeda		$(rustdoc_modifiers_workaround) \
17971479eeeSMatthew Maurer		$(if $(rustdoc_host),,--sysroot=/dev/null) \
1802f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
1812f7ab126SMiguel Ojeda
1822f7ab126SMiguel Ojeda# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
1832f7ab126SMiguel Ojeda# can be used to specify a custom logo. However:
1842f7ab126SMiguel Ojeda#   - The given value is used as-is, thus it cannot be relative or a local file
1852f7ab126SMiguel Ojeda#     (unlike the non-custom case) since the generated docs have subfolders.
1862f7ab126SMiguel Ojeda#   - It requires adding it to every crate.
1872f7ab126SMiguel Ojeda#   - It requires changing `core` which comes from the sysroot.
1882f7ab126SMiguel Ojeda#
1892f7ab126SMiguel Ojeda# Using `-Zcrate-attr` would solve the last two points, but not the first.
1902f7ab126SMiguel Ojeda# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
1912f7ab126SMiguel Ojeda# command-like flags to solve the issue. Meanwhile, we use the non-custom case
1922f7ab126SMiguel Ojeda# and then retouch the generated files.
1932f7ab126SMiguel Ojedarustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
19450605498SMiguel Ojeda    rustdoc-kernel rustdoc-pin_init rustdoc-zerocopy rustdoc-zerocopy_derive
1959e3bbbf5SMiguel Ojeda	$(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \
1969e3bbbf5SMiguel Ojeda		cut -d'"' -f2 | cut -d/ -f2- | while read f; do \
1979e3bbbf5SMiguel Ojeda			if [ ! -e "$(srctree)/$$f" ]; then \
1989e3bbbf5SMiguel Ojeda				echo "warning: srctree/ link to $$f does not exist"; \
1999e3bbbf5SMiguel Ojeda			fi \
2009e3bbbf5SMiguel Ojeda		done
201cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
202cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
20348fadf44SCarlos Bilbao	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
204cfd96726SMiguel Ojeda		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
205cfd96726SMiguel Ojeda		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
206bc2e7d5cSMiguel Ojeda		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
207e2bad142SMasahiro Yamada		-e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
208cfd96726SMiguel Ojeda	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
209cfd96726SMiguel Ojeda		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
2102f7ab126SMiguel Ojeda
211158a3b72SMiguel Ojedarustdoc-proc_macro2: private rustdoc_host = yes
212158a3b72SMiguel Ojedarustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
213158a3b72SMiguel Ojedarustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
214158a3b72SMiguel Ojeda	+$(call if_changed,rustdoc)
215158a3b72SMiguel Ojeda
21688de91ccSMiguel Ojedarustdoc-quote: private rustdoc_host = yes
21788de91ccSMiguel Ojedarustdoc-quote: private rustc_target_flags = $(quote-flags)
21888de91ccSMiguel Ojedarustdoc-quote: private skip_flags = $(quote-skip_flags)
21988de91ccSMiguel Ojedarustdoc-quote: $(src)/quote/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
22088de91ccSMiguel Ojeda	+$(call if_changed,rustdoc)
22188de91ccSMiguel Ojeda
22273740175SMiguel Ojedarustdoc-syn: private rustdoc_host = yes
22373740175SMiguel Ojedarustdoc-syn: private rustc_target_flags = $(syn-flags)
22473740175SMiguel Ojedarustdoc-syn: $(src)/syn/lib.rs rustdoc-clean rustdoc-quote FORCE
22573740175SMiguel Ojeda	+$(call if_changed,rustdoc)
22673740175SMiguel Ojeda
22750605498SMiguel Ojedarustdoc-zerocopy_derive: private rustdoc_host = yes
22850605498SMiguel Ojedarustdoc-zerocopy_derive: private rustc_target_flags = $(zerocopy_derive-flags) \
22950605498SMiguel Ojeda    --extern proc_macro --crate-type proc-macro
23050605498SMiguel Ojedarustdoc-zerocopy_derive: $(src)/zerocopy-derive/lib.rs rustdoc-clean rustdoc-syn FORCE
23150605498SMiguel Ojeda	+$(call if_changed,rustdoc)
23250605498SMiguel Ojeda
2332f7ab126SMiguel Ojedarustdoc-macros: private rustdoc_host = yes
2342f7ab126SMiguel Ojedarustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
23552ba807fSMiguel Ojeda    --extern proc_macro --extern proc_macro2 --extern quote --extern syn
23688de91ccSMiguel Ojedarustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 \
23773740175SMiguel Ojeda    rustdoc-quote rustdoc-syn FORCE
238ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2392f7ab126SMiguel Ojeda
24046e58a96SMiguel Ojedarustdoc-core: private skip_flags = $(core-skip_flags)
24146e58a96SMiguel Ojedarustdoc-core: private rustc_target_flags = $(core-flags)
242252fea13SMiguel Ojedarustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
243ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2442f7ab126SMiguel Ojeda
24516c43a56SMiguel Ojeda# Even if `rustdoc` targets are not kernel objects, they should still be
24616c43a56SMiguel Ojeda# treated as such so that we pass the same flags. Otherwise, for instance,
24716c43a56SMiguel Ojeda# `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch.
24816c43a56SMiguel Ojedarustdoc-compiler_builtins: private is-kernel-object := y
2492f7ab126SMiguel Ojedarustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
250ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2512f7ab126SMiguel Ojeda
25256762152SMiguel Ojedarustdoc-zerocopy: private rustc_target_envs := $(zerocopy-envs)
25356762152SMiguel Ojedarustdoc-zerocopy: private is-kernel-object := y
25456762152SMiguel Ojedarustdoc-zerocopy: private rustc_target_flags = $(zerocopy-flags) \
25556762152SMiguel Ojeda    --extend-css $(src)/zerocopy/rustdoc/style.css
25656762152SMiguel Ojedarustdoc-zerocopy: $(src)/zerocopy/src/lib.rs rustdoc-clean rustdoc-core FORCE
25756762152SMiguel Ojeda	+$(call if_changed,rustdoc)
25856762152SMiguel Ojeda
25916c43a56SMiguel Ojedarustdoc-ffi: private is-kernel-object := y
260d072acdaSGary Guorustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
261ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2622f7ab126SMiguel Ojeda
263d7659accSMiguel Ojedarustdoc-pin_init_internal: private rustdoc_host = yes
264abbe9459STamir Dubersteinrustdoc-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
265d7659accSMiguel Ojeda    --extern proc_macro --crate-type proc-macro
266252fea13SMiguel Ojedarustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
267514e4ed2SBenno Lossin    rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
268d7659accSMiguel Ojeda	+$(call if_changed,rustdoc)
269d7659accSMiguel Ojeda
270d7659accSMiguel Ojedarustdoc-pin_init: private rustdoc_host = yes
271abbe9459STamir Dubersteinrustdoc-pin_init: private rustc_target_flags = $(pin_init-flags) \
272abbe9459STamir Duberstein    --extern alloc --cfg feature=\"alloc\"
273dbd5058bSBenno Lossinrustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
274d7659accSMiguel Ojeda    rustdoc-macros FORCE
275d7659accSMiguel Ojeda	+$(call if_changed,rustdoc)
276d7659accSMiguel Ojeda
27716c43a56SMiguel Ojedarustdoc-kernel: private is-kernel-object := y
278d7659accSMiguel Ojedarustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
2790730422bSTamir Duberstein    --extern build_error --extern macros \
28056762152SMiguel Ojeda    --extern bindings --extern uapi \
28150605498SMiguel Ojeda    --extern zerocopy --extern zerocopy_derive
282d072acdaSGary Guorustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
283d7659accSMiguel Ojeda    rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \
2842f7ab126SMiguel Ojeda    $(obj)/bindings.o FORCE
285ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2862f7ab126SMiguel Ojeda
287252fea13SMiguel Ojedarustdoc-clean: FORCE
288252fea13SMiguel Ojeda	$(Q)rm -rf $(rustdoc_output)
289252fea13SMiguel Ojeda
2902a87f8b0SMiguel Ojedaquiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $<
2912f7ab126SMiguel Ojeda      cmd_rustc_test_library = \
2920ba60f71SMiguel Ojeda	$(rustc_target_envs) \
2932f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
294d4e7307bSMiguel Ojeda	$(RUSTC_OR_CLIPPY) $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
295d4e7307bSMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
2962f7ab126SMiguel Ojeda		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
2972f7ab126SMiguel Ojeda		--out-dir $(objtree)/$(obj)/test --cfg testlib \
2982f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
2992f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
3002f7ab126SMiguel Ojeda
30156762152SMiguel Ojedarusttestlib-zerocopy: private rustc_target_envs := $(zerocopy-envs)
30256762152SMiguel Ojedarusttestlib-zerocopy: private rustc_target_flags = $(zerocopy-flags)
30356762152SMiguel Ojedarusttestlib-zerocopy: $(src)/zerocopy/src/lib.rs FORCE
30456762152SMiguel Ojeda	+$(call if_changed,rustc_test_library)
30556762152SMiguel Ojeda
3069ffc80c8SMiguel Ojedarusttestlib-build_error: $(src)/build_error.rs FORCE
307ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
308ecaa6ddfSGary Guo
309d072acdaSGary Guorusttestlib-ffi: $(src)/ffi.rs FORCE
310d072acdaSGary Guo	+$(call if_changed,rustc_test_library)
311d072acdaSGary Guo
312158a3b72SMiguel Ojedarusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
313158a3b72SMiguel Ojedarusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
314158a3b72SMiguel Ojeda	+$(call if_changed,rustc_test_library)
315158a3b72SMiguel Ojeda
31688de91ccSMiguel Ojedarusttestlib-quote: private skip_flags = $(quote-skip_flags)
31788de91ccSMiguel Ojedarusttestlib-quote: private rustc_target_flags = $(quote-flags)
31888de91ccSMiguel Ojedarusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
31988de91ccSMiguel Ojeda	+$(call if_changed,rustc_test_library)
32088de91ccSMiguel Ojeda
32173740175SMiguel Ojedarusttestlib-syn: private rustc_target_flags = $(syn-flags)
32273740175SMiguel Ojedarusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
32373740175SMiguel Ojeda	+$(call if_changed,rustc_test_library)
32473740175SMiguel Ojeda
32550605498SMiguel Ojedarusttestlib-zerocopy_derive: private rustc_target_flags = $(zerocopy_derive-flags) \
32650605498SMiguel Ojeda    --extern proc_macro
32750605498SMiguel Ojedarusttestlib-zerocopy_derive: private rustc_test_library_proc = yes
32850605498SMiguel Ojedarusttestlib-zerocopy_derive: $(src)/zerocopy-derive/lib.rs rusttestlib-syn FORCE
32950605498SMiguel Ojeda	+$(call if_changed,rustc_test_library)
33050605498SMiguel Ojeda
33152ba807fSMiguel Ojedarusttestlib-macros: private rustc_target_flags = --extern proc_macro \
33252ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
3332f7ab126SMiguel Ojedarusttestlib-macros: private rustc_test_library_proc = yes
33452ba807fSMiguel Ojedarusttestlib-macros: $(src)/macros/lib.rs \
33552ba807fSMiguel Ojeda    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
336ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3372f7ab126SMiguel Ojeda
338abbe9459STamir Dubersteinrusttestlib-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
339d7659accSMiguel Ojeda    --extern proc_macro
340d7659accSMiguel Ojedarusttestlib-pin_init_internal: private rustc_test_library_proc = yes
341514e4ed2SBenno Lossinrusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
342514e4ed2SBenno Lossin    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
343d7659accSMiguel Ojeda	+$(call if_changed,rustc_test_library)
344d7659accSMiguel Ojeda
345abbe9459STamir Dubersteinrusttestlib-pin_init: private rustc_target_flags = $(pin_init-flags)
346dbd5058bSBenno Lossinrusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
347d7659accSMiguel Ojeda    rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
348d7659accSMiguel Ojeda	+$(call if_changed,rustc_test_library)
349d7659accSMiguel Ojeda
350d072acdaSGary Guorusttestlib-kernel: private rustc_target_flags = --extern ffi \
351d7659accSMiguel Ojeda    --extern build_error --extern macros --extern pin_init \
35256762152SMiguel Ojeda    --extern bindings --extern uapi \
35350605498SMiguel Ojeda    --extern zerocopy --extern zerocopy_derive
354d7659accSMiguel Ojedarusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
355d7659accSMiguel Ojeda    rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
35650605498SMiguel Ojeda    $(obj)/bindings.o rusttestlib-zerocopy rusttestlib-zerocopy_derive FORCE
357ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3582f7ab126SMiguel Ojeda
3593c847e17SBenno Lossinrusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
3603c847e17SBenno Lossinrusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
3612f7ab126SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3622f7ab126SMiguel Ojeda
3633c847e17SBenno Lossinrusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
3643c847e17SBenno Lossinrusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
365ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3664e174665SAsahi Lina
3672f7ab126SMiguel Ojedaquiet_cmd_rustdoc_test = RUSTDOC T $<
3682f7ab126SMiguel Ojeda      cmd_rustdoc_test = \
3690ba60f71SMiguel Ojeda	$(rustc_target_envs) \
3708d3f5079SEthan D. Twardy	RUST_MODFILE=test.rs \
3712f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3722f7ab126SMiguel Ojeda	$(RUSTDOC) --test $(rust_common_flags) \
37374981592SMiguel Ojeda		-Zcrate-attr='feature(used_with_arg)' \
3742f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
3752f7ab126SMiguel Ojeda		$(rustc_target_flags) $(rustdoc_test_target_flags) \
3769ffc80c8SMiguel Ojeda		$(rustdoc_test_quiet) \
37748fadf44SCarlos Bilbao		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
3782f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<
3792f7ab126SMiguel Ojeda
380a66d733dSMiguel Ojedaquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
381a66d733dSMiguel Ojeda      cmd_rustdoc_test_kernel = \
382a66d733dSMiguel Ojeda	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
383a66d733dSMiguel Ojeda	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
3840ba60f71SMiguel Ojeda	$(rustc_target_envs) \
385a66d733dSMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
386b06b348eSMiguel Ojeda	$(RUSTDOC) --test $(filter-out --remap-path-scope=%,$(rust_flags)) \
387d7659accSMiguel Ojeda		-L$(objtree)/$(obj) --extern ffi --extern pin_init \
388d7659accSMiguel Ojeda		--extern kernel --extern build_error --extern macros \
389a66d733dSMiguel Ojeda		--extern bindings --extern uapi \
39050605498SMiguel Ojeda		--extern zerocopy --extern zerocopy_derive \
391a66d733dSMiguel Ojeda		--no-run --crate-name kernel -Zunstable-options \
39271479eeeSMatthew Maurer		--sysroot=/dev/null \
393fad472efSMiguel Ojeda		$(doctests_modifiers_workaround) \
394a66d733dSMiguel Ojeda		--test-builder $(objtree)/scripts/rustdoc_test_builder \
395a66d733dSMiguel Ojeda		$< $(rustdoc_test_kernel_quiet); \
396a66d733dSMiguel Ojeda	$(objtree)/scripts/rustdoc_test_gen
397a66d733dSMiguel Ojeda
398a66d733dSMiguel Ojeda%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
399a66d733dSMiguel Ojeda    $(src)/kernel/lib.rs $(obj)/kernel.o \
400a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_builder \
401a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_gen FORCE
402ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test_kernel)
403a66d733dSMiguel Ojeda
4042f7ab126SMiguel Ojeda# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
4052f7ab126SMiguel Ojeda# so for the moment we skip `-Cpanic=abort`.
4062a87f8b0SMiguel Ojedaquiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
4072f7ab126SMiguel Ojeda      cmd_rustc_test = \
4080ba60f71SMiguel Ojeda	$(rustc_target_envs) \
4092f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
4102a87f8b0SMiguel Ojeda	$(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
4112f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
4122f7ab126SMiguel Ojeda		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
4132f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
4142f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<; \
4152f7ab126SMiguel Ojeda	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
4162f7ab126SMiguel Ojeda		$(rustc_test_run_flags)
4172f7ab126SMiguel Ojeda
41817d5efcbSMiguel Ojedarusttest: rusttest-macros
4192f7ab126SMiguel Ojeda
420b2c261faSEthan D. Twardyrusttest-macros: private rustc_target_flags = --extern proc_macro \
42152ba807fSMiguel Ojeda    --extern macros --extern kernel --extern pin_init \
42252ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
4232f7ab126SMiguel Ojedarusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
424b2c261faSEthan D. Twardyrusttest-macros: $(src)/macros/lib.rs \
425d7659accSMiguel Ojeda    rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
426ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test)
427ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test)
4282f7ab126SMiguel Ojeda
4292f7ab126SMiguel Ojedaifdef CONFIG_CC_IS_CLANG
4302f7ab126SMiguel Ojedabindgen_c_flags = $(c_flags)
4312f7ab126SMiguel Ojedaelse
4322f7ab126SMiguel Ojeda# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
4332f7ab126SMiguel Ojeda# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
4342f7ab126SMiguel Ojeda#
4352f7ab126SMiguel Ojeda# For the moment, here we are tweaking the flags on the fly. This is a hack,
4362f7ab126SMiguel Ojeda# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
4372f7ab126SMiguel Ojeda# if we end up using one of those structs).
4382f7ab126SMiguel Ojedabindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
4392f7ab126SMiguel Ojeda	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
4402f7ab126SMiguel Ojeda	-mindirect-branch=thunk-extern -mindirect-branch-register \
4412f7ab126SMiguel Ojeda	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
4422f7ab126SMiguel Ojeda	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
4432f7ab126SMiguel Ojeda	-mno-pointers-to-nested-functions -mno-string \
44413c23cb4SWANG Rui	-mno-strict-align -mstrict-align -mdirect-extern-access \
44513c23cb4SWANG Rui	-mexplicit-relocs -mno-check-zero-division \
4462f7ab126SMiguel Ojeda	-fconserve-stack -falign-jumps=% -falign-loops=% \
4472f7ab126SMiguel Ojeda	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
4482f7ab126SMiguel Ojeda	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
4492f7ab126SMiguel Ojeda	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
4502f7ab126SMiguel Ojeda	-fzero-call-used-regs=% -fno-stack-clash-protection \
451b0554488SAndrea Righi	-fno-inline-functions-called-once -fsanitize=bounds-strict \
452869b5016SZehui Xu	-fstrict-flex-arrays=% -fmin-function-alignment=% \
453977c4308SRudraksha Gupta	-fzero-init-padding-bits=% -mno-fdpic \
454609db7e7SSiddhesh Poyarekar	-fdiagnostics-show-context -fdiagnostics-show-context=% \
455fe4b3a34SXi Ruoyao	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference
4562f7ab126SMiguel Ojeda
4572f7ab126SMiguel Ojeda# Derived from `scripts/Makefile.clang`.
4582f7ab126SMiguel OjedaBINDGEN_TARGET_x86	:= x86_64-linux-gnu
459724a75acSJamie CunliffeBINDGEN_TARGET_arm64	:= aarch64-linux-gnu
460ccb8ce52SChristian SchrreflBINDGEN_TARGET_arm	:= arm-linux-gnueabi
46113c23cb4SWANG RuiBINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
4623f70ebe6SJan PolenskyBINDGEN_TARGET_s390	:= s390x-linux-gnu
463ba6b3285SDavid Gow# This is only for i386 UM builds, which need the 32-bit target not -m32
464ba6b3285SDavid GowBINDGEN_TARGET_i386	:= i386-linux-gnu
4656b2dab17SThomas WeißschuhBINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
4662f7ab126SMiguel OjedaBINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
4672f7ab126SMiguel Ojeda
4682f7ab126SMiguel Ojeda# All warnings are inhibited since GCC builds are very experimental,
4692f7ab126SMiguel Ojeda# many GCC warnings are not supported by Clang, they may only appear in
4702f7ab126SMiguel Ojeda# some configurations, with new GCC versions, etc.
4712f7ab126SMiguel Ojedabindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
4722f7ab126SMiguel Ojeda
473d966c3caSAndrea Righi# Auto variable zero-initialization requires an additional special option with
474d966c3caSAndrea Righi# clang that is going to be removed sometime in the future (likely in
475d966c3caSAndrea Righi# clang-18), so make sure to pass this option only if clang supports it
476d966c3caSAndrea Righi# (libclang major version < 16).
477d966c3caSAndrea Righi#
478d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/issues/44842
479d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
480d966c3caSAndrea Righiifdef CONFIG_INIT_STACK_ALL_ZERO
481d966c3caSAndrea Righilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
482d966c3caSAndrea Righiifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
483d966c3caSAndrea Righibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
484d966c3caSAndrea Righiendif
485d966c3caSAndrea Righiendif
486d966c3caSAndrea Righi
4872f7ab126SMiguel Ojedabindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
4882f7ab126SMiguel Ojeda	$(bindgen_extra_c_flags)
4892f7ab126SMiguel Ojedaendif
4902f7ab126SMiguel Ojeda
4912f7ab126SMiguel Ojedaifdef CONFIG_LTO
4922f7ab126SMiguel Ojedabindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
4932f7ab126SMiguel Ojedaelse
4942f7ab126SMiguel Ojedabindgen_c_flags_lto = $(bindgen_c_flags)
4952f7ab126SMiguel Ojedaendif
4962f7ab126SMiguel Ojeda
49775c1fd41SGary Guo# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
49875c1fd41SGary Guo# prototypes for functions like `memcpy` -- if this flag is not passed,
49975c1fd41SGary Guo# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
50075c1fd41SGary Guo# architecture instead of generating `usize`.
50175c1fd41SGary Guobindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
5022f7ab126SMiguel Ojeda
503276ed30cSMiguel Ojeda# `--rust-target` points to our minimum supported Rust version.
5042f7ab126SMiguel Ojedaquiet_cmd_bindgen = BINDGEN $@
5052f7ab126SMiguel Ojeda      cmd_bindgen = \
506276ed30cSMiguel Ojeda	$(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.85 \
507d072acdaSGary Guo		--use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
50876501d19SMiguel Ojeda		--no-debug '.*' --enable-function-attribute-detection \
50908ab7865SAakash Sen Sharma		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
5102f7ab126SMiguel Ojeda		$(bindgen_target_cflags) $(bindgen_target_extra)
5112f7ab126SMiguel Ojeda
5122f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
513b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
51474376656SGary Guo$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
51574376656SGary Guo    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
5162f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
5172f7ab126SMiguel Ojeda    $(src)/bindgen_parameters FORCE
5182f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
5192f7ab126SMiguel Ojeda
5204e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
521b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
5224e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
5234e174665SAsahi Lina    $(src)/bindgen_parameters FORCE
5244e174665SAsahi Lina	$(call if_changed_dep,bindgen)
5254e174665SAsahi Lina
5262f7ab126SMiguel Ojeda# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
5272f7ab126SMiguel Ojeda# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
5282f7ab126SMiguel Ojeda# given it is `libclang`; but for consistency, future Clang changes and/or
5292f7ab126SMiguel Ojeda# a potential future GCC backend for `bindgen`, we disable it too.
5302f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
53108ab7865SAakash Sen Sharma    --blocklist-type '.*' --allowlist-var '' \
53208ab7865SAakash Sen Sharma    --allowlist-function 'rust_helper_.*'
5332f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
5342f7ab126SMiguel Ojeda    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
5352f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
5362f7ab126SMiguel Ojeda    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
53787634653SAndreas Hindborg$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
5382f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
5392f7ab126SMiguel Ojeda
5403a2486ccSGary Guoquiet_cmd_rust_helper = HELPER  $@
5413a2486ccSGary Guo      cmd_rust_helper = \
5423a2486ccSGary Guo	$(CC) $(filter-out $(CFLAGS_REMOVE_helpers/helpers.o), $(c_flags)) \
5433a2486ccSGary Guo		-c -g0 $< -emit-llvm -o $@
5443a2486ccSGary Guo
5453a2486ccSGary Guo$(obj)/helpers/helpers.bc: private part-of-builtin := y
5463a2486ccSGary Guo$(obj)/helpers/helpers_module.bc: private part-of-module := y
5473a2486ccSGary Guo$(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpers.c FORCE
5483a2486ccSGary Guo	+$(call if_changed_dep,rust_helper)
5493a2486ccSGary Guo
550c59026c0SSami Tolvanenrust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
551ac61506bSSami Tolvanen
5522f7ab126SMiguel Ojedaquiet_cmd_exports = EXPORTS $@
5532f7ab126SMiguel Ojeda      cmd_exports = \
554ac61506bSSami Tolvanen	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
5552f7ab126SMiguel Ojeda
5562f7ab126SMiguel Ojeda$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
5572f7ab126SMiguel Ojeda	$(call if_changed,exports)
5582f7ab126SMiguel Ojeda
559e26fa546SGary Guo# Even though Rust kernel modules should never use the bindings directly,
560e26fa546SGary Guo# symbols from the `bindings` crate and the C helpers need to be exported
561e26fa546SGary Guo# because Rust generics and inlined functions may not get their code generated
562e26fa546SGary Guo# in the crate where they are defined. Other helpers, called from non-inline
563e26fa546SGary Guo# functions, may not be exported, in principle. However, in general, the Rust
564e26fa546SGary Guo# compiler does not guarantee codegen will be performed for a non-inline
565e26fa546SGary Guo# function either. Therefore, we export all symbols from helpers and bindings.
566e26fa546SGary Guo# In the future, this may be revisited to reduce the number of exports after
567e26fa546SGary Guo# the compiler is informed about the places codegen is required.
568e26fa546SGary Guo$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
569e26fa546SGary Guo	$(call if_changed,exports)
570e26fa546SGary Guo
5712f7ab126SMiguel Ojeda$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
5722f7ab126SMiguel Ojeda	$(call if_changed,exports)
5732f7ab126SMiguel Ojeda
5742f7ab126SMiguel Ojeda$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
5752f7ab126SMiguel Ojeda	$(call if_changed,exports)
5762f7ab126SMiguel Ojeda
57770a8d5adSMiguel Ojedaquiet_cmd_rustc_procmacrolibrary = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) PL $@
5787dbe46c0SMiguel Ojeda      cmd_rustc_procmacrolibrary = \
5790ba60f71SMiguel Ojeda	$(rustc_target_envs) \
5807dbe46c0SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
5817dbe46c0SMiguel Ojeda		$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
582e174dd14SGary Guo		--emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \
5837dbe46c0SMiguel Ojeda		--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
584e174dd14SGary Guo		--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<
5857dbe46c0SMiguel Ojeda
586158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: private skip_clippy = 1
587158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
588158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
589158a3b72SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
590158a3b72SMiguel Ojeda
59188de91ccSMiguel Ojeda$(obj)/libquote.rlib: private skip_clippy = 1
59288de91ccSMiguel Ojeda$(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
59388de91ccSMiguel Ojeda$(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
59488de91ccSMiguel Ojeda$(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
59588de91ccSMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
59688de91ccSMiguel Ojeda
59773740175SMiguel Ojeda$(obj)/libsyn.rlib: private skip_clippy = 1
59873740175SMiguel Ojeda$(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
59973740175SMiguel Ojeda$(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
60073740175SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
60173740175SMiguel Ojeda
602be43b5d9SMiguel Ojedaquiet_cmd_rustc_procmacro = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) P $@
6032f7ab126SMiguel Ojeda      cmd_rustc_procmacro = \
6040ba60f71SMiguel Ojeda	$(rustc_target_envs) \
605be43b5d9SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) $(rust_common_flags) $(rustc_target_flags) \
60680bac83aSMatthew Maurer		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
607ceff0757SHONG Yifan		-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
608295d8398SMasahiro Yamada		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
609c46b34f1SMiguel Ojeda		--crate-type proc-macro -L$(objtree)/$(obj) \
6102846ebc8SMiguel Ojeda		--crate-name $(patsubst lib%.$(procmacro-extension),%,$(notdir $@)) \
61136174d16SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
6122f7ab126SMiguel Ojeda
6132f7ab126SMiguel Ojeda# Procedural macros can only be used with the `rustc` that compiled it.
61450605498SMiguel Ojeda$(obj)/$(libzerocopy_derive_name): private skip_clippy = 1
61550605498SMiguel Ojeda$(obj)/$(libzerocopy_derive_name): private rustc_target_flags = $(zerocopy_derive-flags)
61650605498SMiguel Ojeda$(obj)/$(libzerocopy_derive_name): $(src)/zerocopy-derive/lib.rs $(obj)/libproc_macro2.rlib \
61750605498SMiguel Ojeda    $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
61850605498SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
61950605498SMiguel Ojeda
62052ba807fSMiguel Ojeda$(obj)/$(libmacros_name): private rustc_target_flags = \
62152ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
62288de91ccSMiguel Ojeda$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
62373740175SMiguel Ojeda    $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
624ecab4115SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
6252f7ab126SMiguel Ojeda
626abbe9459STamir Duberstein$(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
627514e4ed2SBenno Lossin$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
628514e4ed2SBenno Lossin    $(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
629d7659accSMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
630d7659accSMiguel Ojeda
6310a9be83eSMiguel Ojeda# `rustc` requires `-Zunstable-options` to use custom target specifications
6320a9be83eSMiguel Ojeda# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
6332f7ab126SMiguel Ojedaquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
6342f7ab126SMiguel Ojeda      cmd_rustc_library = \
6350ba60f71SMiguel Ojeda	$(rustc_target_envs) \
6362f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
6372f7ab126SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
638f4daa80dSGary Guo		$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
6393a2486ccSGary Guo		--emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
640295d8398SMasahiro Yamada		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
641295d8398SMasahiro Yamada		--crate-type rlib -L$(objtree)/$(obj) \
6422185242fSMasahiro Yamada		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
64371479eeeSMatthew Maurer		--sysroot=/dev/null \
6440a9be83eSMiguel Ojeda		-Zunstable-options \
6453a2486ccSGary Guo	$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
6463a2486ccSGary Guo		$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
6473a2486ccSGary Guo		$(CC) $(CLANG_FLAGS) $(KBUILD_CFLAGS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
6483a2486ccSGary Guo		$(cmd_ld_single)) \
649c4d7f546SMiguel Ojeda	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
650c4d7f546SMiguel Ojeda	$(cmd_objtool)
6512f7ab126SMiguel Ojeda
6522f7ab126SMiguel Ojedarust-analyzer:
6530730422bSTamir Duberstein	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
654f4daa80dSGary Guo		--cfgs='core=$(core-cfgs)' $(core-edition) \
6552808a396SMiguel Ojeda		--cfgs='zerocopy=$(zerocopy-cfgs)' \
656158a3b72SMiguel Ojeda		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
65788de91ccSMiguel Ojeda		--cfgs='quote=$(quote-cfgs)' \
65873740175SMiguel Ojeda		--cfgs='syn=$(syn-cfgs)' \
6592808a396SMiguel Ojeda		--cfgs='zerocopy_derive=$(zerocopy_derive-cfgs)' \
6605c8d16acSTamir Duberstein		--cfgs='pin_init_internal=$(pin_init_internal-cfgs)' \
6615c8d16acSTamir Duberstein		--cfgs='pin_init=$(pin_init-cfgs)' \
66256762152SMiguel Ojeda		--envs='zerocopy=$(zerocopy-envs)' \
663e2bad142SMasahiro Yamada		$(realpath $(srctree)) $(realpath $(objtree)) \
66413b25489SMasahiro Yamada		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
66513b25489SMasahiro Yamada		> rust-project.json
6662f7ab126SMiguel Ojeda
667cb7d9defSGary Guoredirect-intrinsics = \
66802dfd63aSMiguel Ojeda	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
66902dfd63aSMiguel Ojeda	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
670cb7d9defSGary Guo	__muloti4 __multi3 \
671cb7d9defSGary Guo	__udivmodti4 __udivti3 __umodti3
672cb7d9defSGary Guo
673ccb8ce52SChristian Schrreflifdef CONFIG_ARM
674ccb8ce52SChristian Schrrefl	# Add eabi initrinsics for ARM 32-bit
675ccb8ce52SChristian Schrrefl	redirect-intrinsics += \
676ccb8ce52SChristian Schrrefl		__aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
677ccb8ce52SChristian Schrrefl		__aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
678ccb8ce52SChristian Schrrefl		__aeabi_uldivmod
679ccb8ce52SChristian Schrreflendif
680cb7d9defSGary Guoifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
681cb7d9defSGary Guo	# These intrinsics are defined for ARM64 and RISCV64
682cb7d9defSGary Guo	redirect-intrinsics += \
683cb7d9defSGary Guo		__ashrti3 \
684cb7d9defSGary Guo		__ashlti3 __lshrti3
685cb7d9defSGary Guoendif
686cb7d9defSGary Guo
687ac61506bSSami Tolvanenifdef CONFIG_MODVERSIONS
688ac61506bSSami Tolvanencmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
689ac61506bSSami Tolvanen	$(call rust_exports,$@,"%s\n") | \
690ac61506bSSami Tolvanen	scripts/gendwarfksyms/gendwarfksyms \
691ac61506bSSami Tolvanen		$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
692ac61506bSSami Tolvanen		$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
693ac61506bSSami Tolvanen		$@ >> $(dot-target).cmd)
694ac61506bSSami Tolvanenendif
695ac61506bSSami Tolvanen
696c4d7f546SMiguel Ojedadefine rule_rustc_library
697c4d7f546SMiguel Ojeda	$(call cmd_and_fixdep,rustc_library)
698c4d7f546SMiguel Ojeda	$(call cmd,gen_objtooldep)
699ac61506bSSami Tolvanen	$(call cmd,gendwarfksyms)
700c4d7f546SMiguel Ojedaendef
701c4d7f546SMiguel Ojeda
702ac61506bSSami Tolvanendefine rule_rust_cc_library
703ac61506bSSami Tolvanen	$(call if_changed_rule,cc_o_c)
704ac61506bSSami Tolvanen	$(call cmd,force_checksrc)
705ac61506bSSami Tolvanen	$(call cmd,gendwarfksyms)
706ac61506bSSami Tolvanenendef
707ac61506bSSami Tolvanen
708ac61506bSSami Tolvanen# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
709ac61506bSSami Tolvanen# versions are calculated for the helpers too.
710ac61506bSSami Tolvanen$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
711ac61506bSSami Tolvanen	+$(call if_changed_rule,rust_cc_library)
712ac61506bSSami Tolvanen
713ac61506bSSami Tolvanen# Disable symbol versioning for exports.o to avoid conflicts with the actual
714ac61506bSSami Tolvanen# symbol versions generated from Rust objects.
715ac61506bSSami Tolvanen$(obj)/exports.o: private skip_gendwarfksyms = 1
716ac61506bSSami Tolvanen
7172f7ab126SMiguel Ojeda$(obj)/core.o: private skip_clippy = 1
71846e58a96SMiguel Ojeda$(obj)/core.o: private skip_flags = $(core-skip_flags)
719cb7d9defSGary Guo$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
72046e58a96SMiguel Ojeda$(obj)/core.o: private rustc_target_flags = $(core-flags)
721ac3e9726SMiguel Ojeda$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
722ac3e9726SMiguel Ojeda    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
723c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
724ab0f4cedSDavid Gowifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
725f82811e2SJamie Cunliffe$(obj)/core.o: scripts/target.json
726f82811e2SJamie Cunliffeendif
7273bf67171SAlice RyhlKCOV_INSTRUMENT_core.o := n
7282f7ab126SMiguel Ojeda
729ac61506bSSami Tolvanen$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
7302f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
7312f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
732c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7332f7ab126SMiguel Ojeda
73456762152SMiguel Ojeda$(obj)/zerocopy.o: private skip_clippy = 1
73556762152SMiguel Ojeda$(obj)/zerocopy.o: private skip_gendwarfksyms = 1
73656762152SMiguel Ojeda$(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs)
73756762152SMiguel Ojeda$(obj)/zerocopy.o: private rustc_target_flags = $(zerocopy-flags)
73856762152SMiguel Ojeda$(obj)/zerocopy.o: $(src)/zerocopy/src/lib.rs $(obj)/compiler_builtins.o FORCE
73956762152SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
74056762152SMiguel Ojeda
741d7659accSMiguel Ojeda$(obj)/pin_init.o: private skip_gendwarfksyms = 1
742abbe9459STamir Duberstein$(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
743dbd5058bSBenno Lossin$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
744d7659accSMiguel Ojeda    $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
745d7659accSMiguel Ojeda	+$(call if_changed_rule,rustc_library)
746d7659accSMiguel Ojeda
74716c43a56SMiguel Ojeda# Even if normally `build_error` is not a kernel object, it should still be
74816c43a56SMiguel Ojeda# treated as such so that we pass the same flags. Otherwise, for instance,
74916c43a56SMiguel Ojeda# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
75016c43a56SMiguel Ojeda$(obj)/build_error.o: private is-kernel-object := y
751ac61506bSSami Tolvanen$(obj)/build_error.o: private skip_gendwarfksyms = 1
752ecaa6ddfSGary Guo$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
753c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
754ecaa6ddfSGary Guo
755ac61506bSSami Tolvanen$(obj)/ffi.o: private skip_gendwarfksyms = 1
756d072acdaSGary Guo$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
757d072acdaSGary Guo	+$(call if_changed_rule,rustc_library)
758d072acdaSGary Guo
7593c847e17SBenno Lossin$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
7602f7ab126SMiguel Ojeda$(obj)/bindings.o: $(src)/bindings/lib.rs \
761d072acdaSGary Guo    $(obj)/ffi.o \
7623c847e17SBenno Lossin    $(obj)/pin_init.o \
7632f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_generated.rs \
7642f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_helpers_generated.rs FORCE
765c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7662f7ab126SMiguel Ojeda
7673c847e17SBenno Lossin$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
768ac61506bSSami Tolvanen$(obj)/uapi.o: private skip_gendwarfksyms = 1
7694e174665SAsahi Lina$(obj)/uapi.o: $(src)/uapi/lib.rs \
770d072acdaSGary Guo    $(obj)/ffi.o \
7713c847e17SBenno Lossin    $(obj)/pin_init.o \
7724e174665SAsahi Lina    $(obj)/uapi/uapi_generated.rs FORCE
773c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7744e174665SAsahi Lina
775d7659accSMiguel Ojeda$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
77656762152SMiguel Ojeda    --extern build_error --extern macros --extern bindings --extern uapi \
77750605498SMiguel Ojeda    --extern zerocopy --extern zerocopy_derive
778d7659accSMiguel Ojeda$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
77956762152SMiguel Ojeda    $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \
78050605498SMiguel Ojeda    $(obj)/zerocopy.o $(obj)/$(libzerocopy_derive_name) FORCE
781c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7822f7ab126SMiguel Ojeda
783169484abSAlice Ryhlifdef CONFIG_JUMP_LABEL
7848af7a501SMiguel Ojeda$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
785169484abSAlice Ryhlendif
786dff64b07SFUJITA Tomonoriifndef CONFIG_UML
787dff64b07SFUJITA Tomonoriifdef CONFIG_BUG
788dff64b07SFUJITA Tomonori$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
789dff64b07SFUJITA Tomonoriendif
790dff64b07SFUJITA Tomonoriendif
791169484abSAlice Ryhl
7923a2486ccSGary Guoifdef CONFIG_RUST_INLINE_HELPERS
7933a2486ccSGary Guo$(obj)/kernel.o: private link_helper = 1
7943a2486ccSGary Guo$(obj)/kernel.o: $(obj)/helpers/helpers.bc
7953a2486ccSGary Guoendif
7963a2486ccSGary Guo
7972f7ab126SMiguel Ojedaendif # CONFIG_RUST
798