xref: /linux/rust/Makefile (revision 3a2486cc1da5cf637fe5da4540929d67c4540022)
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
9*3a2486ccSGary Guoifdef CONFIG_RUST_INLINE_HELPERS
10*3a2486ccSGary Guoalways-$(CONFIG_RUST) += helpers/helpers.bc helpers/helpers_module.bc
11*3a2486ccSGary Guoelse
12*3a2486ccSGary Guoobj-$(CONFIG_RUST) += helpers/helpers.o
13*3a2486ccSGary Guoalways-$(CONFIG_RUST) += exports_helpers_generated.h
14*3a2486ccSGary Guoendif
152f7ab126SMiguel Ojeda# Missing prototypes are expected in the helpers since these are exported
162f7ab126SMiguel Ojeda# for Rust only, thus there is no header nor prototypes.
1787634653SAndreas HindborgCFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
182f7ab126SMiguel Ojeda
192f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
20d7659accSMiguel Ojedaobj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o
21*3a2486ccSGary Guoalways-$(CONFIG_RUST) += exports_bindings_generated.h exports_kernel_generated.h
222f7ab126SMiguel Ojeda
234e174665SAsahi Linaalways-$(CONFIG_RUST) += uapi/uapi_generated.rs
244e174665SAsahi Linaobj-$(CONFIG_RUST) += uapi.o
254e174665SAsahi Lina
26ecaa6ddfSGary Guoifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
27ecaa6ddfSGary Guoobj-$(CONFIG_RUST) += build_error.o
28ecaa6ddfSGary Guoelse
29ecaa6ddfSGary Guoalways-$(CONFIG_RUST) += build_error.o
30ecaa6ddfSGary Guoendif
31ecaa6ddfSGary Guo
322f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += exports.o
332f7ab126SMiguel Ojeda
3473740175SMiguel Ojedaalways-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
35158a3b72SMiguel Ojeda
36a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
37a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
38a66d733dSMiguel Ojeda
39a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
40a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
41a66d733dSMiguel Ojeda
428af7a501SMiguel Ojedaalways-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
43dff64b07SFUJITA Tomonoriifndef CONFIG_UML
44dff64b07SFUJITA Tomonorialways-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
45dff64b07SFUJITA Tomonoriendif
46169484abSAlice Ryhl
470730422bSTamir Duberstein# Avoids running `$(RUSTC)` when it may not be available.
482f7ab126SMiguel Ojedaifdef CONFIG_RUST
492f7ab126SMiguel Ojeda
500730422bSTamir Dubersteinlibmacros_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name macros --crate-type proc-macro - </dev/null)
510730422bSTamir Dubersteinlibmacros_extension := $(patsubst libmacros.%,%,$(libmacros_name))
520730422bSTamir Duberstein
53d7659accSMiguel Ojedalibpin_init_internal_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name pin_init_internal --crate-type proc-macro - </dev/null)
54d7659accSMiguel Ojedalibpin_init_internal_extension := $(patsubst libpin_init_internal.%,%,$(libpin_init_internal_name))
55d7659accSMiguel Ojeda
56d7659accSMiguel Ojedaalways-$(CONFIG_RUST) += $(libmacros_name) $(libpin_init_internal_name)
570730422bSTamir Duberstein
582f7ab126SMiguel Ojeda# `$(rust_flags)` is passed in case the user added `--sysroot`.
59ecab4115SMiguel Ojedarustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
602f7ab126SMiguel Ojedarustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
612f7ab126SMiguel OjedaRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
622f7ab126SMiguel Ojeda
639ffc80c8SMiguel Ojedaifneq ($(quiet),)
642f7ab126SMiguel Ojedarust_test_quiet=-q
652f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q
66a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null
672f7ab126SMiguel Ojedaendif
682f7ab126SMiguel Ojeda
691181c974SMiguel Ojedacfgs-to-flags = $(patsubst %,--cfg='%',$1)
701181c974SMiguel Ojeda
7146e58a96SMiguel Ojedacore-cfgs := \
721181c974SMiguel Ojeda    no_fp_fmt_parse
732f7ab126SMiguel Ojeda
74f4daa80dSGary Guocore-edition := $(if $(call rustc-min-version,108700),2024,2021)
75f4daa80dSGary Guo
7646e58a96SMiguel Ojedacore-skip_flags := \
7746e58a96SMiguel Ojeda    --edition=2021 \
7846e58a96SMiguel Ojeda    -Wunreachable_pub \
7946e58a96SMiguel Ojeda    -Wrustdoc::unescaped_backticks
8046e58a96SMiguel Ojeda
8146e58a96SMiguel Ojedacore-flags := \
8246e58a96SMiguel Ojeda    --edition=$(core-edition) \
831181c974SMiguel Ojeda    $(call cfgs-to-flags,$(core-cfgs))
8446e58a96SMiguel Ojeda
85158a3b72SMiguel Ojedaproc_macro2-cfgs := \
86158a3b72SMiguel Ojeda    feature="proc-macro" \
87158a3b72SMiguel Ojeda    wrap_proc_macro \
88158a3b72SMiguel Ojeda    $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
89158a3b72SMiguel Ojeda
90158a3b72SMiguel Ojeda# Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
91158a3b72SMiguel Ojedaproc_macro2-flags := \
92158a3b72SMiguel Ojeda    --cap-lints=allow \
93158a3b72SMiguel Ojeda    -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
94158a3b72SMiguel Ojeda    $(call cfgs-to-flags,$(proc_macro2-cfgs))
95158a3b72SMiguel Ojeda
9688de91ccSMiguel Ojedaquote-cfgs := \
9788de91ccSMiguel Ojeda    feature="proc-macro"
9888de91ccSMiguel Ojeda
9988de91ccSMiguel Ojedaquote-skip_flags := \
10088de91ccSMiguel Ojeda    --edition=2021
10188de91ccSMiguel Ojeda
10288de91ccSMiguel Ojedaquote-flags := \
10388de91ccSMiguel Ojeda    --edition=2018 \
10488de91ccSMiguel Ojeda    --cap-lints=allow \
10588de91ccSMiguel Ojeda    --extern proc_macro2 \
10688de91ccSMiguel Ojeda    $(call cfgs-to-flags,$(quote-cfgs))
10788de91ccSMiguel Ojeda
10873740175SMiguel Ojeda# `extra-traits`, `fold` and `visit` may be enabled if needed.
10973740175SMiguel Ojedasyn-cfgs := \
11073740175SMiguel Ojeda    feature="clone-impls" \
11173740175SMiguel Ojeda    feature="derive" \
11273740175SMiguel Ojeda    feature="full" \
11373740175SMiguel Ojeda    feature="parsing" \
11473740175SMiguel Ojeda    feature="printing" \
11573740175SMiguel Ojeda    feature="proc-macro" \
11673740175SMiguel Ojeda    feature="visit-mut"
11773740175SMiguel Ojeda
11873740175SMiguel Ojedasyn-flags := \
11973740175SMiguel Ojeda    --cap-lints=allow \
12073740175SMiguel Ojeda    --extern proc_macro2 \
12173740175SMiguel Ojeda    --extern quote \
12273740175SMiguel Ojeda    $(call cfgs-to-flags,$(syn-cfgs))
12373740175SMiguel Ojeda
124abbe9459STamir Dubersteinpin_init_internal-cfgs := \
125abbe9459STamir Duberstein    kernel
126abbe9459STamir Duberstein
127abbe9459STamir Dubersteinpin_init_internal-flags := \
128abbe9459STamir Duberstein    --extern proc_macro2 \
129abbe9459STamir Duberstein    --extern quote \
130abbe9459STamir Duberstein    --extern syn \
131abbe9459STamir Duberstein    $(call cfgs-to-flags,$(pin_init_internal-cfgs))
132abbe9459STamir Duberstein
133abbe9459STamir Dubersteinpin_init-cfgs := \
134abbe9459STamir Duberstein    kernel
135abbe9459STamir Duberstein
136abbe9459STamir Dubersteinpin_init-flags := \
137abbe9459STamir Duberstein    --extern pin_init_internal \
138abbe9459STamir Duberstein    --extern macros \
139abbe9459STamir Duberstein    $(call cfgs-to-flags,$(pin_init-cfgs))
140abbe9459STamir Duberstein
141abbf9a44SMiguel Ojeda# `rustdoc` did not save the target modifiers, thus workaround for
142abbf9a44SMiguel Ojeda# the time being (https://github.com/rust-lang/rust/issues/144521).
143abbf9a44SMiguel Ojedarustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
144abbf9a44SMiguel Ojeda
145fad472efSMiguel Ojeda# Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465).
146fad472efSMiguel Ojedadoctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer)
147fad472efSMiguel Ojeda
1482c8725c1SMiguel Ojeda# `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only
1492c8725c1SMiguel Ojeda# since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust
1502c8725c1SMiguel Ojeda# 1.82.0 (https://github.com/rust-lang/rust/issues/138520). Thus workaround both
1512c8725c1SMiguel Ojeda# issues skipping the flag. The former also applies to `RUSTDOC TK`.
1522f7ab126SMiguel Ojedaquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
1532f7ab126SMiguel Ojeda      cmd_rustdoc = \
1542f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
1552c8725c1SMiguel Ojeda	$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
1562f7ab126SMiguel Ojeda		$(rustc_target_flags) -L$(objtree)/$(obj) \
1576e6efc5fSMiguel Ojeda		-Zunstable-options --generate-link-to-definition \
15848fadf44SCarlos Bilbao		--output $(rustdoc_output) \
1592f7ab126SMiguel Ojeda		--crate-name $(subst rustdoc-,,$@) \
160abbf9a44SMiguel Ojeda		$(rustdoc_modifiers_workaround) \
16171479eeeSMatthew Maurer		$(if $(rustdoc_host),,--sysroot=/dev/null) \
1622f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
1632f7ab126SMiguel Ojeda
1642f7ab126SMiguel Ojeda# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
1652f7ab126SMiguel Ojeda# can be used to specify a custom logo. However:
1662f7ab126SMiguel Ojeda#   - The given value is used as-is, thus it cannot be relative or a local file
1672f7ab126SMiguel Ojeda#     (unlike the non-custom case) since the generated docs have subfolders.
1682f7ab126SMiguel Ojeda#   - It requires adding it to every crate.
1692f7ab126SMiguel Ojeda#   - It requires changing `core` which comes from the sysroot.
1702f7ab126SMiguel Ojeda#
1712f7ab126SMiguel Ojeda# Using `-Zcrate-attr` would solve the last two points, but not the first.
1722f7ab126SMiguel Ojeda# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
1732f7ab126SMiguel Ojeda# command-like flags to solve the issue. Meanwhile, we use the non-custom case
1742f7ab126SMiguel Ojeda# and then retouch the generated files.
1752f7ab126SMiguel Ojedarustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
176d7659accSMiguel Ojeda    rustdoc-kernel rustdoc-pin_init
1779e3bbbf5SMiguel Ojeda	$(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \
1789e3bbbf5SMiguel Ojeda		cut -d'"' -f2 | cut -d/ -f2- | while read f; do \
1799e3bbbf5SMiguel Ojeda			if [ ! -e "$(srctree)/$$f" ]; then \
1809e3bbbf5SMiguel Ojeda				echo "warning: srctree/ link to $$f does not exist"; \
1819e3bbbf5SMiguel Ojeda			fi \
1829e3bbbf5SMiguel Ojeda		done
183cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
184cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
18548fadf44SCarlos Bilbao	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
186cfd96726SMiguel Ojeda		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
187cfd96726SMiguel Ojeda		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
188bc2e7d5cSMiguel Ojeda		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
189e2bad142SMasahiro Yamada		-e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
190cfd96726SMiguel Ojeda	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
191cfd96726SMiguel Ojeda		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
1922f7ab126SMiguel Ojeda
193158a3b72SMiguel Ojedarustdoc-proc_macro2: private rustdoc_host = yes
194158a3b72SMiguel Ojedarustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
195158a3b72SMiguel Ojedarustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
196158a3b72SMiguel Ojeda	+$(call if_changed,rustdoc)
197158a3b72SMiguel Ojeda
19888de91ccSMiguel Ojedarustdoc-quote: private rustdoc_host = yes
19988de91ccSMiguel Ojedarustdoc-quote: private rustc_target_flags = $(quote-flags)
20088de91ccSMiguel Ojedarustdoc-quote: private skip_flags = $(quote-skip_flags)
20188de91ccSMiguel Ojedarustdoc-quote: $(src)/quote/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
20288de91ccSMiguel Ojeda	+$(call if_changed,rustdoc)
20388de91ccSMiguel Ojeda
20473740175SMiguel Ojedarustdoc-syn: private rustdoc_host = yes
20573740175SMiguel Ojedarustdoc-syn: private rustc_target_flags = $(syn-flags)
20673740175SMiguel Ojedarustdoc-syn: $(src)/syn/lib.rs rustdoc-clean rustdoc-quote FORCE
20773740175SMiguel Ojeda	+$(call if_changed,rustdoc)
20873740175SMiguel Ojeda
2092f7ab126SMiguel Ojedarustdoc-macros: private rustdoc_host = yes
2102f7ab126SMiguel Ojedarustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
21152ba807fSMiguel Ojeda    --extern proc_macro --extern proc_macro2 --extern quote --extern syn
21288de91ccSMiguel Ojedarustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 \
21373740175SMiguel Ojeda    rustdoc-quote rustdoc-syn FORCE
214ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2152f7ab126SMiguel Ojeda
216bef83245SMiguel Ojeda# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
217bef83245SMiguel Ojeda# not be needed -- see https://github.com/rust-lang/rust/pull/128307.
21846e58a96SMiguel Ojedarustdoc-core: private skip_flags = $(core-skip_flags)
21946e58a96SMiguel Ojedarustdoc-core: private rustc_target_flags = $(core-flags)
220252fea13SMiguel Ojedarustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
221ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2222f7ab126SMiguel Ojeda
22316c43a56SMiguel Ojeda# Even if `rustdoc` targets are not kernel objects, they should still be
22416c43a56SMiguel Ojeda# treated as such so that we pass the same flags. Otherwise, for instance,
22516c43a56SMiguel Ojeda# `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch.
22616c43a56SMiguel Ojedarustdoc-compiler_builtins: private is-kernel-object := y
2272f7ab126SMiguel Ojedarustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
228ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2292f7ab126SMiguel Ojeda
23016c43a56SMiguel Ojedarustdoc-ffi: private is-kernel-object := y
231d072acdaSGary Guorustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
232ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2332f7ab126SMiguel Ojeda
234d7659accSMiguel Ojedarustdoc-pin_init_internal: private rustdoc_host = yes
235abbe9459STamir Dubersteinrustdoc-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
236d7659accSMiguel Ojeda    --extern proc_macro --crate-type proc-macro
237252fea13SMiguel Ojedarustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
238514e4ed2SBenno Lossin    rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
239d7659accSMiguel Ojeda	+$(call if_changed,rustdoc)
240d7659accSMiguel Ojeda
241d7659accSMiguel Ojedarustdoc-pin_init: private rustdoc_host = yes
242abbe9459STamir Dubersteinrustdoc-pin_init: private rustc_target_flags = $(pin_init-flags) \
243abbe9459STamir Duberstein    --extern alloc --cfg feature=\"alloc\"
244dbd5058bSBenno Lossinrustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
245d7659accSMiguel Ojeda    rustdoc-macros FORCE
246d7659accSMiguel Ojeda	+$(call if_changed,rustdoc)
247d7659accSMiguel Ojeda
24816c43a56SMiguel Ojedarustdoc-kernel: private is-kernel-object := y
249d7659accSMiguel Ojedarustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
2500730422bSTamir Duberstein    --extern build_error --extern macros \
2514e174665SAsahi Lina    --extern bindings --extern uapi
252d072acdaSGary Guorustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
253d7659accSMiguel Ojeda    rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \
2542f7ab126SMiguel Ojeda    $(obj)/bindings.o FORCE
255ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2562f7ab126SMiguel Ojeda
257252fea13SMiguel Ojedarustdoc-clean: FORCE
258252fea13SMiguel Ojeda	$(Q)rm -rf $(rustdoc_output)
259252fea13SMiguel Ojeda
2602a87f8b0SMiguel Ojedaquiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $<
2612f7ab126SMiguel Ojeda      cmd_rustc_test_library = \
2622f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
263d4e7307bSMiguel Ojeda	$(RUSTC_OR_CLIPPY) $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
264d4e7307bSMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
2652f7ab126SMiguel Ojeda		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
2662f7ab126SMiguel Ojeda		--out-dir $(objtree)/$(obj)/test --cfg testlib \
2672f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
2682f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
2692f7ab126SMiguel Ojeda
2709ffc80c8SMiguel Ojedarusttestlib-build_error: $(src)/build_error.rs FORCE
271ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
272ecaa6ddfSGary Guo
273d072acdaSGary Guorusttestlib-ffi: $(src)/ffi.rs FORCE
274d072acdaSGary Guo	+$(call if_changed,rustc_test_library)
275d072acdaSGary Guo
276158a3b72SMiguel Ojedarusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
277158a3b72SMiguel Ojedarusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
278158a3b72SMiguel Ojeda	+$(call if_changed,rustc_test_library)
279158a3b72SMiguel Ojeda
28088de91ccSMiguel Ojedarusttestlib-quote: private skip_flags = $(quote-skip_flags)
28188de91ccSMiguel Ojedarusttestlib-quote: private rustc_target_flags = $(quote-flags)
28288de91ccSMiguel Ojedarusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
28388de91ccSMiguel Ojeda	+$(call if_changed,rustc_test_library)
28488de91ccSMiguel Ojeda
28573740175SMiguel Ojedarusttestlib-syn: private rustc_target_flags = $(syn-flags)
28673740175SMiguel Ojedarusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
28773740175SMiguel Ojeda	+$(call if_changed,rustc_test_library)
28873740175SMiguel Ojeda
28952ba807fSMiguel Ojedarusttestlib-macros: private rustc_target_flags = --extern proc_macro \
29052ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
2912f7ab126SMiguel Ojedarusttestlib-macros: private rustc_test_library_proc = yes
29252ba807fSMiguel Ojedarusttestlib-macros: $(src)/macros/lib.rs \
29352ba807fSMiguel Ojeda    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
294ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
2952f7ab126SMiguel Ojeda
296abbe9459STamir Dubersteinrusttestlib-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
297d7659accSMiguel Ojeda    --extern proc_macro
298d7659accSMiguel Ojedarusttestlib-pin_init_internal: private rustc_test_library_proc = yes
299514e4ed2SBenno Lossinrusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
300514e4ed2SBenno Lossin    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
301d7659accSMiguel Ojeda	+$(call if_changed,rustc_test_library)
302d7659accSMiguel Ojeda
303abbe9459STamir Dubersteinrusttestlib-pin_init: private rustc_target_flags = $(pin_init-flags)
304dbd5058bSBenno Lossinrusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
305d7659accSMiguel Ojeda    rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
306d7659accSMiguel Ojeda	+$(call if_changed,rustc_test_library)
307d7659accSMiguel Ojeda
308d072acdaSGary Guorusttestlib-kernel: private rustc_target_flags = --extern ffi \
309d7659accSMiguel Ojeda    --extern build_error --extern macros --extern pin_init \
310b2c261faSEthan D. Twardy    --extern bindings --extern uapi
311d7659accSMiguel Ojedarusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
312d7659accSMiguel Ojeda    rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
313d7659accSMiguel Ojeda    $(obj)/bindings.o FORCE
314ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3152f7ab126SMiguel Ojeda
3163c847e17SBenno Lossinrusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
3173c847e17SBenno Lossinrusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
3182f7ab126SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3192f7ab126SMiguel Ojeda
3203c847e17SBenno Lossinrusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
3213c847e17SBenno Lossinrusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
322ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3234e174665SAsahi Lina
3242f7ab126SMiguel Ojedaquiet_cmd_rustdoc_test = RUSTDOC T $<
3252f7ab126SMiguel Ojeda      cmd_rustdoc_test = \
3268d3f5079SEthan D. Twardy	RUST_MODFILE=test.rs \
3272f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3282f7ab126SMiguel Ojeda	$(RUSTDOC) --test $(rust_common_flags) \
32974981592SMiguel Ojeda		-Zcrate-attr='feature(used_with_arg)' \
3302f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
3312f7ab126SMiguel Ojeda		$(rustc_target_flags) $(rustdoc_test_target_flags) \
3329ffc80c8SMiguel Ojeda		$(rustdoc_test_quiet) \
33348fadf44SCarlos Bilbao		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
3342f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<
3352f7ab126SMiguel Ojeda
336a66d733dSMiguel Ojedaquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
337a66d733dSMiguel Ojeda      cmd_rustdoc_test_kernel = \
338a66d733dSMiguel Ojeda	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
339a66d733dSMiguel Ojeda	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
340a66d733dSMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3412c8725c1SMiguel Ojeda	$(RUSTDOC) --test $(filter-out --remap-path-prefix=%,$(rust_flags)) \
342d7659accSMiguel Ojeda		-L$(objtree)/$(obj) --extern ffi --extern pin_init \
343d7659accSMiguel Ojeda		--extern kernel --extern build_error --extern macros \
344a66d733dSMiguel Ojeda		--extern bindings --extern uapi \
345a66d733dSMiguel Ojeda		--no-run --crate-name kernel -Zunstable-options \
34671479eeeSMatthew Maurer		--sysroot=/dev/null \
347fad472efSMiguel Ojeda		$(doctests_modifiers_workaround) \
348a66d733dSMiguel Ojeda		--test-builder $(objtree)/scripts/rustdoc_test_builder \
349a66d733dSMiguel Ojeda		$< $(rustdoc_test_kernel_quiet); \
350a66d733dSMiguel Ojeda	$(objtree)/scripts/rustdoc_test_gen
351a66d733dSMiguel Ojeda
352a66d733dSMiguel Ojeda%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
353a66d733dSMiguel Ojeda    $(src)/kernel/lib.rs $(obj)/kernel.o \
354a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_builder \
355a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_gen FORCE
356ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test_kernel)
357a66d733dSMiguel Ojeda
3582f7ab126SMiguel Ojeda# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
3592f7ab126SMiguel Ojeda# so for the moment we skip `-Cpanic=abort`.
3602a87f8b0SMiguel Ojedaquiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
3612f7ab126SMiguel Ojeda      cmd_rustc_test = \
3622f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3632a87f8b0SMiguel Ojeda	$(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
3642f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
3652f7ab126SMiguel Ojeda		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
3662f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
3672f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<; \
3682f7ab126SMiguel Ojeda	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
3692f7ab126SMiguel Ojeda		$(rustc_test_run_flags)
3702f7ab126SMiguel Ojeda
37117d5efcbSMiguel Ojedarusttest: rusttest-macros
3722f7ab126SMiguel Ojeda
373b2c261faSEthan D. Twardyrusttest-macros: private rustc_target_flags = --extern proc_macro \
37452ba807fSMiguel Ojeda    --extern macros --extern kernel --extern pin_init \
37552ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
3762f7ab126SMiguel Ojedarusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
377b2c261faSEthan D. Twardyrusttest-macros: $(src)/macros/lib.rs \
378d7659accSMiguel Ojeda    rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
379ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test)
380ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test)
3812f7ab126SMiguel Ojeda
3822f7ab126SMiguel Ojedaifdef CONFIG_CC_IS_CLANG
3832f7ab126SMiguel Ojedabindgen_c_flags = $(c_flags)
3842f7ab126SMiguel Ojedaelse
3852f7ab126SMiguel Ojeda# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
3862f7ab126SMiguel Ojeda# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
3872f7ab126SMiguel Ojeda#
3882f7ab126SMiguel Ojeda# For the moment, here we are tweaking the flags on the fly. This is a hack,
3892f7ab126SMiguel Ojeda# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
3902f7ab126SMiguel Ojeda# if we end up using one of those structs).
3912f7ab126SMiguel Ojedabindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
3922f7ab126SMiguel Ojeda	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
3932f7ab126SMiguel Ojeda	-mindirect-branch=thunk-extern -mindirect-branch-register \
3942f7ab126SMiguel Ojeda	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
3952f7ab126SMiguel Ojeda	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
3962f7ab126SMiguel Ojeda	-mno-pointers-to-nested-functions -mno-string \
39713c23cb4SWANG Rui	-mno-strict-align -mstrict-align -mdirect-extern-access \
39813c23cb4SWANG Rui	-mexplicit-relocs -mno-check-zero-division \
3992f7ab126SMiguel Ojeda	-fconserve-stack -falign-jumps=% -falign-loops=% \
4002f7ab126SMiguel Ojeda	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
4012f7ab126SMiguel Ojeda	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
4022f7ab126SMiguel Ojeda	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
4032f7ab126SMiguel Ojeda	-fzero-call-used-regs=% -fno-stack-clash-protection \
404b0554488SAndrea Righi	-fno-inline-functions-called-once -fsanitize=bounds-strict \
405869b5016SZehui Xu	-fstrict-flex-arrays=% -fmin-function-alignment=% \
406977c4308SRudraksha Gupta	-fzero-init-padding-bits=% -mno-fdpic \
407609db7e7SSiddhesh Poyarekar	-fdiagnostics-show-context -fdiagnostics-show-context=% \
408fe4b3a34SXi Ruoyao	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference
4092f7ab126SMiguel Ojeda
4102f7ab126SMiguel Ojeda# Derived from `scripts/Makefile.clang`.
4112f7ab126SMiguel OjedaBINDGEN_TARGET_x86	:= x86_64-linux-gnu
412724a75acSJamie CunliffeBINDGEN_TARGET_arm64	:= aarch64-linux-gnu
413ccb8ce52SChristian SchrreflBINDGEN_TARGET_arm	:= arm-linux-gnueabi
41413c23cb4SWANG RuiBINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
4156b2dab17SThomas WeißschuhBINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
4162f7ab126SMiguel OjedaBINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
4172f7ab126SMiguel Ojeda
4182f7ab126SMiguel Ojeda# All warnings are inhibited since GCC builds are very experimental,
4192f7ab126SMiguel Ojeda# many GCC warnings are not supported by Clang, they may only appear in
4202f7ab126SMiguel Ojeda# some configurations, with new GCC versions, etc.
4212f7ab126SMiguel Ojedabindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
4222f7ab126SMiguel Ojeda
423d966c3caSAndrea Righi# Auto variable zero-initialization requires an additional special option with
424d966c3caSAndrea Righi# clang that is going to be removed sometime in the future (likely in
425d966c3caSAndrea Righi# clang-18), so make sure to pass this option only if clang supports it
426d966c3caSAndrea Righi# (libclang major version < 16).
427d966c3caSAndrea Righi#
428d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/issues/44842
429d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
430d966c3caSAndrea Righiifdef CONFIG_INIT_STACK_ALL_ZERO
431d966c3caSAndrea Righilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
432d966c3caSAndrea Righiifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
433d966c3caSAndrea Righibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
434d966c3caSAndrea Righiendif
435d966c3caSAndrea Righiendif
436d966c3caSAndrea Righi
4372f7ab126SMiguel Ojedabindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
4382f7ab126SMiguel Ojeda	$(bindgen_extra_c_flags)
4392f7ab126SMiguel Ojedaendif
4402f7ab126SMiguel Ojeda
4412f7ab126SMiguel Ojedaifdef CONFIG_LTO
4422f7ab126SMiguel Ojedabindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
4432f7ab126SMiguel Ojedaelse
4442f7ab126SMiguel Ojedabindgen_c_flags_lto = $(bindgen_c_flags)
4452f7ab126SMiguel Ojedaendif
4462f7ab126SMiguel Ojeda
44775c1fd41SGary Guo# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
44875c1fd41SGary Guo# prototypes for functions like `memcpy` -- if this flag is not passed,
44975c1fd41SGary Guo# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
45075c1fd41SGary Guo# architecture instead of generating `usize`.
45175c1fd41SGary Guobindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
4522f7ab126SMiguel Ojeda
4537a5f93eaSMiguel Ojeda# Each `bindgen` release may upgrade the list of Rust target versions. By
4547a5f93eaSMiguel Ojeda# default, the highest stable release in their list is used. Thus we need to set
4557a5f93eaSMiguel Ojeda# a `--rust-target` to avoid future `bindgen` releases emitting code that
4567a5f93eaSMiguel Ojeda# `rustc` may not understand. On top of that, `bindgen` does not support passing
4577a5f93eaSMiguel Ojeda# an unknown Rust target version.
4587a5f93eaSMiguel Ojeda#
4597a5f93eaSMiguel Ojeda# Therefore, the Rust target for `bindgen` can be only as high as the minimum
4607a5f93eaSMiguel Ojeda# Rust version the kernel supports and only as high as the greatest stable Rust
4617a5f93eaSMiguel Ojeda# target supported by the minimum `bindgen` version the kernel supports (that
4627a5f93eaSMiguel Ojeda# is, if we do not test the actual `rustc`/`bindgen` versions running).
4637a5f93eaSMiguel Ojeda#
4647a5f93eaSMiguel Ojeda# Starting with `bindgen` 0.71.0, we will be able to set any future Rust version
4657a5f93eaSMiguel Ojeda# instead, i.e. we will be able to set here our minimum supported Rust version.
4662f7ab126SMiguel Ojedaquiet_cmd_bindgen = BINDGEN $@
4672f7ab126SMiguel Ojeda      cmd_bindgen = \
4687a5f93eaSMiguel Ojeda	$(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \
469d072acdaSGary Guo		--use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
47076501d19SMiguel Ojeda		--no-debug '.*' --enable-function-attribute-detection \
47108ab7865SAakash Sen Sharma		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
4722f7ab126SMiguel Ojeda		$(bindgen_target_cflags) $(bindgen_target_extra)
4732f7ab126SMiguel Ojeda
4742f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
475b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
47674376656SGary Guo$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
47774376656SGary Guo    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
4782f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
4792f7ab126SMiguel Ojeda    $(src)/bindgen_parameters FORCE
4802f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
4812f7ab126SMiguel Ojeda
4824e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
483b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
4844e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
4854e174665SAsahi Lina    $(src)/bindgen_parameters FORCE
4864e174665SAsahi Lina	$(call if_changed_dep,bindgen)
4874e174665SAsahi Lina
4882f7ab126SMiguel Ojeda# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
4892f7ab126SMiguel Ojeda# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
4902f7ab126SMiguel Ojeda# given it is `libclang`; but for consistency, future Clang changes and/or
4912f7ab126SMiguel Ojeda# a potential future GCC backend for `bindgen`, we disable it too.
4922f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
49308ab7865SAakash Sen Sharma    --blocklist-type '.*' --allowlist-var '' \
49408ab7865SAakash Sen Sharma    --allowlist-function 'rust_helper_.*'
4952f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
4962f7ab126SMiguel Ojeda    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
4972f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
4982f7ab126SMiguel Ojeda    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
49987634653SAndreas Hindborg$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
5002f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
5012f7ab126SMiguel Ojeda
502*3a2486ccSGary Guoquiet_cmd_rust_helper = HELPER  $@
503*3a2486ccSGary Guo      cmd_rust_helper = \
504*3a2486ccSGary Guo	$(CC) $(filter-out $(CFLAGS_REMOVE_helpers/helpers.o), $(c_flags)) \
505*3a2486ccSGary Guo		-c -g0 $< -emit-llvm -o $@
506*3a2486ccSGary Guo
507*3a2486ccSGary Guo$(obj)/helpers/helpers.bc: private part-of-builtin := y
508*3a2486ccSGary Guo$(obj)/helpers/helpers_module.bc: private part-of-module := y
509*3a2486ccSGary Guo$(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpers.c FORCE
510*3a2486ccSGary Guo	+$(call if_changed_dep,rust_helper)
511*3a2486ccSGary Guo
512c59026c0SSami Tolvanenrust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
513ac61506bSSami Tolvanen
5142f7ab126SMiguel Ojedaquiet_cmd_exports = EXPORTS $@
5152f7ab126SMiguel Ojeda      cmd_exports = \
516ac61506bSSami Tolvanen	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
5172f7ab126SMiguel Ojeda
5182f7ab126SMiguel Ojeda$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
5192f7ab126SMiguel Ojeda	$(call if_changed,exports)
5202f7ab126SMiguel Ojeda
521e26fa546SGary Guo# Even though Rust kernel modules should never use the bindings directly,
522e26fa546SGary Guo# symbols from the `bindings` crate and the C helpers need to be exported
523e26fa546SGary Guo# because Rust generics and inlined functions may not get their code generated
524e26fa546SGary Guo# in the crate where they are defined. Other helpers, called from non-inline
525e26fa546SGary Guo# functions, may not be exported, in principle. However, in general, the Rust
526e26fa546SGary Guo# compiler does not guarantee codegen will be performed for a non-inline
527e26fa546SGary Guo# function either. Therefore, we export all symbols from helpers and bindings.
528e26fa546SGary Guo# In the future, this may be revisited to reduce the number of exports after
529e26fa546SGary Guo# the compiler is informed about the places codegen is required.
530e26fa546SGary Guo$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
531e26fa546SGary Guo	$(call if_changed,exports)
532e26fa546SGary Guo
5332f7ab126SMiguel Ojeda$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
5342f7ab126SMiguel Ojeda	$(call if_changed,exports)
5352f7ab126SMiguel Ojeda
5362f7ab126SMiguel Ojeda$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
5372f7ab126SMiguel Ojeda	$(call if_changed,exports)
5382f7ab126SMiguel Ojeda
5397dbe46c0SMiguel Ojedaquiet_cmd_rustc_procmacrolibrary = $(RUSTC_OR_CLIPPY_QUIET) PL $@
5407dbe46c0SMiguel Ojeda      cmd_rustc_procmacrolibrary = \
5417dbe46c0SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
5427dbe46c0SMiguel Ojeda		$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
5437dbe46c0SMiguel Ojeda		--emit=dep-info,link --crate-type rlib -O \
5447dbe46c0SMiguel Ojeda		--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
5457dbe46c0SMiguel Ojeda		--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<; \
5467dbe46c0SMiguel Ojeda	mv $(objtree)/$(obj)/$(patsubst lib%.rlib,%,$(notdir $@)).d $(depfile); \
5477dbe46c0SMiguel Ojeda	sed -i '/^\#/d' $(depfile)
5487dbe46c0SMiguel Ojeda
549158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: private skip_clippy = 1
550158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
551158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
552158a3b72SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
553158a3b72SMiguel Ojeda
55488de91ccSMiguel Ojeda$(obj)/libquote.rlib: private skip_clippy = 1
55588de91ccSMiguel Ojeda$(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
55688de91ccSMiguel Ojeda$(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
55788de91ccSMiguel Ojeda$(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
55888de91ccSMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
55988de91ccSMiguel Ojeda
56073740175SMiguel Ojeda$(obj)/libsyn.rlib: private skip_clippy = 1
56173740175SMiguel Ojeda$(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
56273740175SMiguel Ojeda$(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
56373740175SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
56473740175SMiguel Ojeda
5652f7ab126SMiguel Ojedaquiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
5662f7ab126SMiguel Ojeda      cmd_rustc_procmacro = \
567d7659accSMiguel Ojeda	$(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \
56880bac83aSMatthew Maurer		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
569ceff0757SHONG Yifan		-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
570295d8398SMasahiro Yamada		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
571c46b34f1SMiguel Ojeda		--crate-type proc-macro -L$(objtree)/$(obj) \
57236174d16SMiguel Ojeda		--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \
57336174d16SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
5742f7ab126SMiguel Ojeda
5752f7ab126SMiguel Ojeda# Procedural macros can only be used with the `rustc` that compiled it.
57652ba807fSMiguel Ojeda$(obj)/$(libmacros_name): private rustc_target_flags = \
57752ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
57888de91ccSMiguel Ojeda$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
57973740175SMiguel Ojeda    $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
580ecab4115SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
5812f7ab126SMiguel Ojeda
582abbe9459STamir Duberstein$(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
583514e4ed2SBenno Lossin$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
584514e4ed2SBenno Lossin    $(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
585d7659accSMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
586d7659accSMiguel Ojeda
5870a9be83eSMiguel Ojeda# `rustc` requires `-Zunstable-options` to use custom target specifications
5880a9be83eSMiguel Ojeda# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
5892f7ab126SMiguel Ojedaquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
5902f7ab126SMiguel Ojeda      cmd_rustc_library = \
5912f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
5922f7ab126SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
593f4daa80dSGary Guo		$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
594*3a2486ccSGary Guo		--emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \
595295d8398SMasahiro Yamada		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
596295d8398SMasahiro Yamada		--crate-type rlib -L$(objtree)/$(obj) \
5972185242fSMasahiro Yamada		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
59871479eeeSMatthew Maurer		--sysroot=/dev/null \
5990a9be83eSMiguel Ojeda		-Zunstable-options \
600*3a2486ccSGary Guo	$(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \
601*3a2486ccSGary Guo		$(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \
602*3a2486ccSGary Guo		$(CC) $(CLANG_FLAGS) $(KBUILD_CFLAGS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \
603*3a2486ccSGary Guo		$(cmd_ld_single)) \
604c4d7f546SMiguel Ojeda	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
605c4d7f546SMiguel Ojeda	$(cmd_objtool)
6062f7ab126SMiguel Ojeda
6072f7ab126SMiguel Ojedarust-analyzer:
6080730422bSTamir Duberstein	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
609f4daa80dSGary Guo		--cfgs='core=$(core-cfgs)' $(core-edition) \
610158a3b72SMiguel Ojeda		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
61188de91ccSMiguel Ojeda		--cfgs='quote=$(quote-cfgs)' \
61273740175SMiguel Ojeda		--cfgs='syn=$(syn-cfgs)' \
613e2bad142SMasahiro Yamada		$(realpath $(srctree)) $(realpath $(objtree)) \
61413b25489SMasahiro Yamada		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
61513b25489SMasahiro Yamada		> rust-project.json
6162f7ab126SMiguel Ojeda
617cb7d9defSGary Guoredirect-intrinsics = \
61802dfd63aSMiguel Ojeda	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
61902dfd63aSMiguel Ojeda	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
620cb7d9defSGary Guo	__muloti4 __multi3 \
621cb7d9defSGary Guo	__udivmodti4 __udivti3 __umodti3
622cb7d9defSGary Guo
623ccb8ce52SChristian Schrreflifdef CONFIG_ARM
624ccb8ce52SChristian Schrrefl	# Add eabi initrinsics for ARM 32-bit
625ccb8ce52SChristian Schrrefl	redirect-intrinsics += \
626ccb8ce52SChristian Schrrefl		__aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
627ccb8ce52SChristian Schrrefl		__aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
628ccb8ce52SChristian Schrrefl		__aeabi_uldivmod
629ccb8ce52SChristian Schrreflendif
630cb7d9defSGary Guoifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
631cb7d9defSGary Guo	# These intrinsics are defined for ARM64 and RISCV64
632cb7d9defSGary Guo	redirect-intrinsics += \
633cb7d9defSGary Guo		__ashrti3 \
634cb7d9defSGary Guo		__ashlti3 __lshrti3
635cb7d9defSGary Guoendif
636cb7d9defSGary Guo
637ac61506bSSami Tolvanenifdef CONFIG_MODVERSIONS
638ac61506bSSami Tolvanencmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
639ac61506bSSami Tolvanen	$(call rust_exports,$@,"%s\n") | \
640ac61506bSSami Tolvanen	scripts/gendwarfksyms/gendwarfksyms \
641ac61506bSSami Tolvanen		$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
642ac61506bSSami Tolvanen		$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
643ac61506bSSami Tolvanen		$@ >> $(dot-target).cmd)
644ac61506bSSami Tolvanenendif
645ac61506bSSami Tolvanen
646c4d7f546SMiguel Ojedadefine rule_rustc_library
647c4d7f546SMiguel Ojeda	$(call cmd_and_fixdep,rustc_library)
648c4d7f546SMiguel Ojeda	$(call cmd,gen_objtooldep)
649ac61506bSSami Tolvanen	$(call cmd,gendwarfksyms)
650c4d7f546SMiguel Ojedaendef
651c4d7f546SMiguel Ojeda
652ac61506bSSami Tolvanendefine rule_rust_cc_library
653ac61506bSSami Tolvanen	$(call if_changed_rule,cc_o_c)
654ac61506bSSami Tolvanen	$(call cmd,force_checksrc)
655ac61506bSSami Tolvanen	$(call cmd,gendwarfksyms)
656ac61506bSSami Tolvanenendef
657ac61506bSSami Tolvanen
658ac61506bSSami Tolvanen# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
659ac61506bSSami Tolvanen# versions are calculated for the helpers too.
660ac61506bSSami Tolvanen$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
661ac61506bSSami Tolvanen	+$(call if_changed_rule,rust_cc_library)
662ac61506bSSami Tolvanen
663ac61506bSSami Tolvanen# Disable symbol versioning for exports.o to avoid conflicts with the actual
664ac61506bSSami Tolvanen# symbol versions generated from Rust objects.
665ac61506bSSami Tolvanen$(obj)/exports.o: private skip_gendwarfksyms = 1
666ac61506bSSami Tolvanen
6672f7ab126SMiguel Ojeda$(obj)/core.o: private skip_clippy = 1
66846e58a96SMiguel Ojeda$(obj)/core.o: private skip_flags = $(core-skip_flags)
669cb7d9defSGary Guo$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
67046e58a96SMiguel Ojeda$(obj)/core.o: private rustc_target_flags = $(core-flags)
671ac3e9726SMiguel Ojeda$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
672ac3e9726SMiguel Ojeda    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
673c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
674ab0f4cedSDavid Gowifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
675f82811e2SJamie Cunliffe$(obj)/core.o: scripts/target.json
676f82811e2SJamie Cunliffeendif
6773bf67171SAlice RyhlKCOV_INSTRUMENT_core.o := n
6782f7ab126SMiguel Ojeda
679ac61506bSSami Tolvanen$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
6802f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
6812f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
682c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
6832f7ab126SMiguel Ojeda
684d7659accSMiguel Ojeda$(obj)/pin_init.o: private skip_gendwarfksyms = 1
685abbe9459STamir Duberstein$(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
686dbd5058bSBenno Lossin$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
687d7659accSMiguel Ojeda    $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
688d7659accSMiguel Ojeda	+$(call if_changed_rule,rustc_library)
689d7659accSMiguel Ojeda
69016c43a56SMiguel Ojeda# Even if normally `build_error` is not a kernel object, it should still be
69116c43a56SMiguel Ojeda# treated as such so that we pass the same flags. Otherwise, for instance,
69216c43a56SMiguel Ojeda# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
69316c43a56SMiguel Ojeda$(obj)/build_error.o: private is-kernel-object := y
694ac61506bSSami Tolvanen$(obj)/build_error.o: private skip_gendwarfksyms = 1
695ecaa6ddfSGary Guo$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
696c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
697ecaa6ddfSGary Guo
698ac61506bSSami Tolvanen$(obj)/ffi.o: private skip_gendwarfksyms = 1
699d072acdaSGary Guo$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
700d072acdaSGary Guo	+$(call if_changed_rule,rustc_library)
701d072acdaSGary Guo
7023c847e17SBenno Lossin$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
7032f7ab126SMiguel Ojeda$(obj)/bindings.o: $(src)/bindings/lib.rs \
704d072acdaSGary Guo    $(obj)/ffi.o \
7053c847e17SBenno Lossin    $(obj)/pin_init.o \
7062f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_generated.rs \
7072f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_helpers_generated.rs FORCE
708c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7092f7ab126SMiguel Ojeda
7103c847e17SBenno Lossin$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
711ac61506bSSami Tolvanen$(obj)/uapi.o: private skip_gendwarfksyms = 1
7124e174665SAsahi Lina$(obj)/uapi.o: $(src)/uapi/lib.rs \
713d072acdaSGary Guo    $(obj)/ffi.o \
7143c847e17SBenno Lossin    $(obj)/pin_init.o \
7154e174665SAsahi Lina    $(obj)/uapi/uapi_generated.rs FORCE
716c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7174e174665SAsahi Lina
718d7659accSMiguel Ojeda$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
7194e174665SAsahi Lina    --extern build_error --extern macros --extern bindings --extern uapi
720d7659accSMiguel Ojeda$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
7210730422bSTamir Duberstein    $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE
722c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
7232f7ab126SMiguel Ojeda
724169484abSAlice Ryhlifdef CONFIG_JUMP_LABEL
7258af7a501SMiguel Ojeda$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
726169484abSAlice Ryhlendif
727dff64b07SFUJITA Tomonoriifndef CONFIG_UML
728dff64b07SFUJITA Tomonoriifdef CONFIG_BUG
729dff64b07SFUJITA Tomonori$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
730dff64b07SFUJITA Tomonoriendif
731dff64b07SFUJITA Tomonoriendif
732169484abSAlice Ryhl
733*3a2486ccSGary Guoifdef CONFIG_RUST_INLINE_HELPERS
734*3a2486ccSGary Guo$(obj)/kernel.o: private link_helper = 1
735*3a2486ccSGary Guo$(obj)/kernel.o: $(obj)/helpers/helpers.bc
736*3a2486ccSGary Guoendif
737*3a2486ccSGary Guo
7382f7ab126SMiguel Ojedaendif # CONFIG_RUST
739