xref: /linux/rust/Makefile (revision 784faa8eca8270671e0ed6d9d21f04bbb80fc5f7)
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
92f7ab126SMiguel Ojeda# Missing prototypes are expected in the helpers since these are exported
102f7ab126SMiguel Ojeda# for Rust only, thus there is no header nor prototypes.
1187634653SAndreas Hindborgobj-$(CONFIG_RUST) += helpers/helpers.o
1287634653SAndreas HindborgCFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
132f7ab126SMiguel Ojeda
142f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
15d7659accSMiguel Ojedaobj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o
16392e34b6SDanilo Krummrichalways-$(CONFIG_RUST) += exports_helpers_generated.h \
17e26fa546SGary Guo    exports_bindings_generated.h exports_kernel_generated.h
182f7ab126SMiguel Ojeda
194e174665SAsahi Linaalways-$(CONFIG_RUST) += uapi/uapi_generated.rs
204e174665SAsahi Linaobj-$(CONFIG_RUST) += uapi.o
214e174665SAsahi Lina
22ecaa6ddfSGary Guoifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
23ecaa6ddfSGary Guoobj-$(CONFIG_RUST) += build_error.o
24ecaa6ddfSGary Guoelse
25ecaa6ddfSGary Guoalways-$(CONFIG_RUST) += build_error.o
26ecaa6ddfSGary Guoendif
27ecaa6ddfSGary Guo
282f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += exports.o
292f7ab126SMiguel Ojeda
3073740175SMiguel Ojedaalways-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
31158a3b72SMiguel Ojeda
32a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
33a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
34a66d733dSMiguel Ojeda
35a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
36a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
37a66d733dSMiguel Ojeda
388af7a501SMiguel Ojedaalways-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
39dff64b07SFUJITA Tomonoriifndef CONFIG_UML
40dff64b07SFUJITA Tomonorialways-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
41dff64b07SFUJITA Tomonoriendif
42169484abSAlice Ryhl
430730422bSTamir Duberstein# Avoids running `$(RUSTC)` when it may not be available.
442f7ab126SMiguel Ojedaifdef CONFIG_RUST
452f7ab126SMiguel Ojeda
460730422bSTamir Dubersteinlibmacros_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name macros --crate-type proc-macro - </dev/null)
470730422bSTamir Dubersteinlibmacros_extension := $(patsubst libmacros.%,%,$(libmacros_name))
480730422bSTamir Duberstein
49d7659accSMiguel Ojedalibpin_init_internal_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name pin_init_internal --crate-type proc-macro - </dev/null)
50d7659accSMiguel Ojedalibpin_init_internal_extension := $(patsubst libpin_init_internal.%,%,$(libpin_init_internal_name))
51d7659accSMiguel Ojeda
52d7659accSMiguel Ojedaalways-$(CONFIG_RUST) += $(libmacros_name) $(libpin_init_internal_name)
530730422bSTamir Duberstein
542f7ab126SMiguel Ojeda# `$(rust_flags)` is passed in case the user added `--sysroot`.
55ecab4115SMiguel Ojedarustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
562f7ab126SMiguel Ojedarustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
572f7ab126SMiguel OjedaRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
582f7ab126SMiguel Ojeda
599ffc80c8SMiguel Ojedaifneq ($(quiet),)
602f7ab126SMiguel Ojedarust_test_quiet=-q
612f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q
62a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null
632f7ab126SMiguel Ojedaendif
642f7ab126SMiguel Ojeda
651181c974SMiguel Ojedacfgs-to-flags = $(patsubst %,--cfg='%',$1)
661181c974SMiguel Ojeda
6746e58a96SMiguel Ojedacore-cfgs := \
681181c974SMiguel Ojeda    no_fp_fmt_parse
692f7ab126SMiguel Ojeda
70f4daa80dSGary Guocore-edition := $(if $(call rustc-min-version,108700),2024,2021)
71f4daa80dSGary Guo
7246e58a96SMiguel Ojedacore-skip_flags := \
7346e58a96SMiguel Ojeda    --edition=2021 \
7446e58a96SMiguel Ojeda    -Wunreachable_pub \
7546e58a96SMiguel Ojeda    -Wrustdoc::unescaped_backticks
7646e58a96SMiguel Ojeda
7746e58a96SMiguel Ojedacore-flags := \
7846e58a96SMiguel Ojeda    --edition=$(core-edition) \
791181c974SMiguel Ojeda    $(call cfgs-to-flags,$(core-cfgs))
8046e58a96SMiguel Ojeda
81158a3b72SMiguel Ojedaproc_macro2-cfgs := \
82158a3b72SMiguel Ojeda    feature="proc-macro" \
83158a3b72SMiguel Ojeda    wrap_proc_macro \
84158a3b72SMiguel Ojeda    $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
85158a3b72SMiguel Ojeda
86158a3b72SMiguel Ojeda# Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
87158a3b72SMiguel Ojedaproc_macro2-flags := \
88158a3b72SMiguel Ojeda    --cap-lints=allow \
89158a3b72SMiguel Ojeda    -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
90158a3b72SMiguel Ojeda    $(call cfgs-to-flags,$(proc_macro2-cfgs))
91158a3b72SMiguel Ojeda
9288de91ccSMiguel Ojedaquote-cfgs := \
9388de91ccSMiguel Ojeda    feature="proc-macro"
9488de91ccSMiguel Ojeda
9588de91ccSMiguel Ojedaquote-skip_flags := \
9688de91ccSMiguel Ojeda    --edition=2021
9788de91ccSMiguel Ojeda
9888de91ccSMiguel Ojedaquote-flags := \
9988de91ccSMiguel Ojeda    --edition=2018 \
10088de91ccSMiguel Ojeda    --cap-lints=allow \
10188de91ccSMiguel Ojeda    --extern proc_macro2 \
10288de91ccSMiguel Ojeda    $(call cfgs-to-flags,$(quote-cfgs))
10388de91ccSMiguel Ojeda
10473740175SMiguel Ojeda# `extra-traits`, `fold` and `visit` may be enabled if needed.
10573740175SMiguel Ojedasyn-cfgs := \
10673740175SMiguel Ojeda    feature="clone-impls" \
10773740175SMiguel Ojeda    feature="derive" \
10873740175SMiguel Ojeda    feature="full" \
10973740175SMiguel Ojeda    feature="parsing" \
11073740175SMiguel Ojeda    feature="printing" \
11173740175SMiguel Ojeda    feature="proc-macro" \
11273740175SMiguel Ojeda    feature="visit-mut"
11373740175SMiguel Ojeda
11473740175SMiguel Ojedasyn-flags := \
11573740175SMiguel Ojeda    --cap-lints=allow \
11673740175SMiguel Ojeda    --extern proc_macro2 \
11773740175SMiguel Ojeda    --extern quote \
11873740175SMiguel Ojeda    $(call cfgs-to-flags,$(syn-cfgs))
11973740175SMiguel Ojeda
120abbf9a44SMiguel Ojeda# `rustdoc` did not save the target modifiers, thus workaround for
121abbf9a44SMiguel Ojeda# the time being (https://github.com/rust-lang/rust/issues/144521).
122abbf9a44SMiguel Ojedarustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
123abbf9a44SMiguel Ojeda
124fad472efSMiguel Ojeda# Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465).
125fad472efSMiguel Ojedadoctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer)
126fad472efSMiguel Ojeda
1272c8725c1SMiguel Ojeda# `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only
1282c8725c1SMiguel Ojeda# since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust
1292c8725c1SMiguel Ojeda# 1.82.0 (https://github.com/rust-lang/rust/issues/138520). Thus workaround both
1302c8725c1SMiguel Ojeda# issues skipping the flag. The former also applies to `RUSTDOC TK`.
1312f7ab126SMiguel Ojedaquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
1322f7ab126SMiguel Ojeda      cmd_rustdoc = \
1332f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
1342c8725c1SMiguel Ojeda	$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
1352f7ab126SMiguel Ojeda		$(rustc_target_flags) -L$(objtree)/$(obj) \
1366e6efc5fSMiguel Ojeda		-Zunstable-options --generate-link-to-definition \
13748fadf44SCarlos Bilbao		--output $(rustdoc_output) \
1382f7ab126SMiguel Ojeda		--crate-name $(subst rustdoc-,,$@) \
139abbf9a44SMiguel Ojeda		$(rustdoc_modifiers_workaround) \
14071479eeeSMatthew Maurer		$(if $(rustdoc_host),,--sysroot=/dev/null) \
1412f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
1422f7ab126SMiguel Ojeda
1432f7ab126SMiguel Ojeda# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
1442f7ab126SMiguel Ojeda# can be used to specify a custom logo. However:
1452f7ab126SMiguel Ojeda#   - The given value is used as-is, thus it cannot be relative or a local file
1462f7ab126SMiguel Ojeda#     (unlike the non-custom case) since the generated docs have subfolders.
1472f7ab126SMiguel Ojeda#   - It requires adding it to every crate.
1482f7ab126SMiguel Ojeda#   - It requires changing `core` which comes from the sysroot.
1492f7ab126SMiguel Ojeda#
1502f7ab126SMiguel Ojeda# Using `-Zcrate-attr` would solve the last two points, but not the first.
1512f7ab126SMiguel Ojeda# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
1522f7ab126SMiguel Ojeda# command-like flags to solve the issue. Meanwhile, we use the non-custom case
1532f7ab126SMiguel Ojeda# and then retouch the generated files.
1542f7ab126SMiguel Ojedarustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
155d7659accSMiguel Ojeda    rustdoc-kernel rustdoc-pin_init
1569e3bbbf5SMiguel Ojeda	$(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \
1579e3bbbf5SMiguel Ojeda		cut -d'"' -f2 | cut -d/ -f2- | while read f; do \
1589e3bbbf5SMiguel Ojeda			if [ ! -e "$(srctree)/$$f" ]; then \
1599e3bbbf5SMiguel Ojeda				echo "warning: srctree/ link to $$f does not exist"; \
1609e3bbbf5SMiguel Ojeda			fi \
1619e3bbbf5SMiguel Ojeda		done
162cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
163cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
16448fadf44SCarlos Bilbao	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
165cfd96726SMiguel Ojeda		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
166cfd96726SMiguel Ojeda		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
167bc2e7d5cSMiguel Ojeda		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
168e2bad142SMasahiro Yamada		-e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
169cfd96726SMiguel Ojeda	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
170cfd96726SMiguel Ojeda		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
1712f7ab126SMiguel Ojeda
172158a3b72SMiguel Ojedarustdoc-proc_macro2: private rustdoc_host = yes
173158a3b72SMiguel Ojedarustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
174158a3b72SMiguel Ojedarustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
175158a3b72SMiguel Ojeda	+$(call if_changed,rustdoc)
176158a3b72SMiguel Ojeda
17788de91ccSMiguel Ojedarustdoc-quote: private rustdoc_host = yes
17888de91ccSMiguel Ojedarustdoc-quote: private rustc_target_flags = $(quote-flags)
17988de91ccSMiguel Ojedarustdoc-quote: private skip_flags = $(quote-skip_flags)
18088de91ccSMiguel Ojedarustdoc-quote: $(src)/quote/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
18188de91ccSMiguel Ojeda	+$(call if_changed,rustdoc)
18288de91ccSMiguel Ojeda
18373740175SMiguel Ojedarustdoc-syn: private rustdoc_host = yes
18473740175SMiguel Ojedarustdoc-syn: private rustc_target_flags = $(syn-flags)
18573740175SMiguel Ojedarustdoc-syn: $(src)/syn/lib.rs rustdoc-clean rustdoc-quote FORCE
18673740175SMiguel Ojeda	+$(call if_changed,rustdoc)
18773740175SMiguel Ojeda
1882f7ab126SMiguel Ojedarustdoc-macros: private rustdoc_host = yes
1892f7ab126SMiguel Ojedarustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
190*52ba807fSMiguel Ojeda    --extern proc_macro --extern proc_macro2 --extern quote --extern syn
19188de91ccSMiguel Ojedarustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 \
19273740175SMiguel Ojeda    rustdoc-quote rustdoc-syn FORCE
193ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
1942f7ab126SMiguel Ojeda
195bef83245SMiguel Ojeda# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
196bef83245SMiguel Ojeda# not be needed -- see https://github.com/rust-lang/rust/pull/128307.
19746e58a96SMiguel Ojedarustdoc-core: private skip_flags = $(core-skip_flags)
19846e58a96SMiguel Ojedarustdoc-core: private rustc_target_flags = $(core-flags)
199252fea13SMiguel Ojedarustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
200ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2012f7ab126SMiguel Ojeda
20216c43a56SMiguel Ojeda# Even if `rustdoc` targets are not kernel objects, they should still be
20316c43a56SMiguel Ojeda# treated as such so that we pass the same flags. Otherwise, for instance,
20416c43a56SMiguel Ojeda# `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch.
20516c43a56SMiguel Ojedarustdoc-compiler_builtins: private is-kernel-object := y
2062f7ab126SMiguel Ojedarustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
207ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2082f7ab126SMiguel Ojeda
20916c43a56SMiguel Ojedarustdoc-ffi: private is-kernel-object := y
210d072acdaSGary Guorustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
211ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2122f7ab126SMiguel Ojeda
213d7659accSMiguel Ojedarustdoc-pin_init_internal: private rustdoc_host = yes
214d7659accSMiguel Ojedarustdoc-pin_init_internal: private rustc_target_flags = --cfg kernel \
215d7659accSMiguel Ojeda    --extern proc_macro --crate-type proc-macro
216252fea13SMiguel Ojedarustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
217252fea13SMiguel Ojeda    rustdoc-clean FORCE
218d7659accSMiguel Ojeda	+$(call if_changed,rustdoc)
219d7659accSMiguel Ojeda
220d7659accSMiguel Ojedarustdoc-pin_init: private rustdoc_host = yes
221d7659accSMiguel Ojedarustdoc-pin_init: private rustc_target_flags = --extern pin_init_internal \
2229b2299afSBenno Lossin    --extern macros --extern alloc --cfg kernel --cfg feature=\"alloc\"
223dbd5058bSBenno Lossinrustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
224d7659accSMiguel Ojeda    rustdoc-macros FORCE
225d7659accSMiguel Ojeda	+$(call if_changed,rustdoc)
226d7659accSMiguel Ojeda
22716c43a56SMiguel Ojedarustdoc-kernel: private is-kernel-object := y
228d7659accSMiguel Ojedarustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
2290730422bSTamir Duberstein    --extern build_error --extern macros \
2304e174665SAsahi Lina    --extern bindings --extern uapi
231d072acdaSGary Guorustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
232d7659accSMiguel Ojeda    rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \
2332f7ab126SMiguel Ojeda    $(obj)/bindings.o FORCE
234ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
2352f7ab126SMiguel Ojeda
236252fea13SMiguel Ojedarustdoc-clean: FORCE
237252fea13SMiguel Ojeda	$(Q)rm -rf $(rustdoc_output)
238252fea13SMiguel Ojeda
2392a87f8b0SMiguel Ojedaquiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $<
2402f7ab126SMiguel Ojeda      cmd_rustc_test_library = \
2412f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
242d4e7307bSMiguel Ojeda	$(RUSTC_OR_CLIPPY) $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
243d4e7307bSMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
2442f7ab126SMiguel Ojeda		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
2452f7ab126SMiguel Ojeda		--out-dir $(objtree)/$(obj)/test --cfg testlib \
2462f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
2472f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
2482f7ab126SMiguel Ojeda
2499ffc80c8SMiguel Ojedarusttestlib-build_error: $(src)/build_error.rs FORCE
250ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
251ecaa6ddfSGary Guo
252d072acdaSGary Guorusttestlib-ffi: $(src)/ffi.rs FORCE
253d072acdaSGary Guo	+$(call if_changed,rustc_test_library)
254d072acdaSGary Guo
255158a3b72SMiguel Ojedarusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
256158a3b72SMiguel Ojedarusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
257158a3b72SMiguel Ojeda	+$(call if_changed,rustc_test_library)
258158a3b72SMiguel Ojeda
25988de91ccSMiguel Ojedarusttestlib-quote: private skip_flags = $(quote-skip_flags)
26088de91ccSMiguel Ojedarusttestlib-quote: private rustc_target_flags = $(quote-flags)
26188de91ccSMiguel Ojedarusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
26288de91ccSMiguel Ojeda	+$(call if_changed,rustc_test_library)
26388de91ccSMiguel Ojeda
26473740175SMiguel Ojedarusttestlib-syn: private rustc_target_flags = $(syn-flags)
26573740175SMiguel Ojedarusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
26673740175SMiguel Ojeda	+$(call if_changed,rustc_test_library)
26773740175SMiguel Ojeda
268*52ba807fSMiguel Ojedarusttestlib-macros: private rustc_target_flags = --extern proc_macro \
269*52ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
2702f7ab126SMiguel Ojedarusttestlib-macros: private rustc_test_library_proc = yes
271*52ba807fSMiguel Ojedarusttestlib-macros: $(src)/macros/lib.rs \
272*52ba807fSMiguel Ojeda    rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
273ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
2742f7ab126SMiguel Ojeda
275d7659accSMiguel Ojedarusttestlib-pin_init_internal: private rustc_target_flags = --cfg kernel \
276d7659accSMiguel Ojeda    --extern proc_macro
277d7659accSMiguel Ojedarusttestlib-pin_init_internal: private rustc_test_library_proc = yes
278dbd5058bSBenno Lossinrusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE
279d7659accSMiguel Ojeda	+$(call if_changed,rustc_test_library)
280d7659accSMiguel Ojeda
281d7659accSMiguel Ojedarusttestlib-pin_init: private rustc_target_flags = --extern pin_init_internal \
282d7659accSMiguel Ojeda    --extern macros --cfg kernel
283dbd5058bSBenno Lossinrusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
284d7659accSMiguel Ojeda    rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
285d7659accSMiguel Ojeda	+$(call if_changed,rustc_test_library)
286d7659accSMiguel Ojeda
287d072acdaSGary Guorusttestlib-kernel: private rustc_target_flags = --extern ffi \
288d7659accSMiguel Ojeda    --extern build_error --extern macros --extern pin_init \
289b2c261faSEthan D. Twardy    --extern bindings --extern uapi
290d7659accSMiguel Ojedarusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
291d7659accSMiguel Ojeda    rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
292d7659accSMiguel Ojeda    $(obj)/bindings.o FORCE
293ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
2942f7ab126SMiguel Ojeda
2953c847e17SBenno Lossinrusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
2963c847e17SBenno Lossinrusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
2972f7ab126SMiguel Ojeda	+$(call if_changed,rustc_test_library)
2982f7ab126SMiguel Ojeda
2993c847e17SBenno Lossinrusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
3003c847e17SBenno Lossinrusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
301ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
3024e174665SAsahi Lina
3032f7ab126SMiguel Ojedaquiet_cmd_rustdoc_test = RUSTDOC T $<
3042f7ab126SMiguel Ojeda      cmd_rustdoc_test = \
3058d3f5079SEthan D. Twardy	RUST_MODFILE=test.rs \
3062f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3072f7ab126SMiguel Ojeda	$(RUSTDOC) --test $(rust_common_flags) \
30874981592SMiguel Ojeda		-Zcrate-attr='feature(used_with_arg)' \
3092f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
3102f7ab126SMiguel Ojeda		$(rustc_target_flags) $(rustdoc_test_target_flags) \
3119ffc80c8SMiguel Ojeda		$(rustdoc_test_quiet) \
31248fadf44SCarlos Bilbao		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
3132f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<
3142f7ab126SMiguel Ojeda
315a66d733dSMiguel Ojedaquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
316a66d733dSMiguel Ojeda      cmd_rustdoc_test_kernel = \
317a66d733dSMiguel Ojeda	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
318a66d733dSMiguel Ojeda	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
319a66d733dSMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3202c8725c1SMiguel Ojeda	$(RUSTDOC) --test $(filter-out --remap-path-prefix=%,$(rust_flags)) \
321d7659accSMiguel Ojeda		-L$(objtree)/$(obj) --extern ffi --extern pin_init \
322d7659accSMiguel Ojeda		--extern kernel --extern build_error --extern macros \
323a66d733dSMiguel Ojeda		--extern bindings --extern uapi \
324a66d733dSMiguel Ojeda		--no-run --crate-name kernel -Zunstable-options \
32571479eeeSMatthew Maurer		--sysroot=/dev/null \
326fad472efSMiguel Ojeda		$(doctests_modifiers_workaround) \
327a66d733dSMiguel Ojeda		--test-builder $(objtree)/scripts/rustdoc_test_builder \
328a66d733dSMiguel Ojeda		$< $(rustdoc_test_kernel_quiet); \
329a66d733dSMiguel Ojeda	$(objtree)/scripts/rustdoc_test_gen
330a66d733dSMiguel Ojeda
331a66d733dSMiguel Ojeda%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
332a66d733dSMiguel Ojeda    $(src)/kernel/lib.rs $(obj)/kernel.o \
333a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_builder \
334a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_gen FORCE
335ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test_kernel)
336a66d733dSMiguel Ojeda
3372f7ab126SMiguel Ojeda# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
3382f7ab126SMiguel Ojeda# so for the moment we skip `-Cpanic=abort`.
3392a87f8b0SMiguel Ojedaquiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T  $<
3402f7ab126SMiguel Ojeda      cmd_rustc_test = \
3412f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3422a87f8b0SMiguel Ojeda	$(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
3432f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
3442f7ab126SMiguel Ojeda		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
3452f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
3462f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<; \
3472f7ab126SMiguel Ojeda	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
3482f7ab126SMiguel Ojeda		$(rustc_test_run_flags)
3492f7ab126SMiguel Ojeda
35017d5efcbSMiguel Ojedarusttest: rusttest-macros
3512f7ab126SMiguel Ojeda
352b2c261faSEthan D. Twardyrusttest-macros: private rustc_target_flags = --extern proc_macro \
353*52ba807fSMiguel Ojeda    --extern macros --extern kernel --extern pin_init \
354*52ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
3552f7ab126SMiguel Ojedarusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
356b2c261faSEthan D. Twardyrusttest-macros: $(src)/macros/lib.rs \
357d7659accSMiguel Ojeda    rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
358ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test)
359ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test)
3602f7ab126SMiguel Ojeda
3612f7ab126SMiguel Ojedaifdef CONFIG_CC_IS_CLANG
3622f7ab126SMiguel Ojedabindgen_c_flags = $(c_flags)
3632f7ab126SMiguel Ojedaelse
3642f7ab126SMiguel Ojeda# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
3652f7ab126SMiguel Ojeda# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
3662f7ab126SMiguel Ojeda#
3672f7ab126SMiguel Ojeda# For the moment, here we are tweaking the flags on the fly. This is a hack,
3682f7ab126SMiguel Ojeda# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
3692f7ab126SMiguel Ojeda# if we end up using one of those structs).
3702f7ab126SMiguel Ojedabindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
3712f7ab126SMiguel Ojeda	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
3722f7ab126SMiguel Ojeda	-mindirect-branch=thunk-extern -mindirect-branch-register \
3732f7ab126SMiguel Ojeda	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
3742f7ab126SMiguel Ojeda	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
3752f7ab126SMiguel Ojeda	-mno-pointers-to-nested-functions -mno-string \
37613c23cb4SWANG Rui	-mno-strict-align -mstrict-align -mdirect-extern-access \
37713c23cb4SWANG Rui	-mexplicit-relocs -mno-check-zero-division \
3782f7ab126SMiguel Ojeda	-fconserve-stack -falign-jumps=% -falign-loops=% \
3792f7ab126SMiguel Ojeda	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
3802f7ab126SMiguel Ojeda	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
3812f7ab126SMiguel Ojeda	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
3822f7ab126SMiguel Ojeda	-fzero-call-used-regs=% -fno-stack-clash-protection \
383b0554488SAndrea Righi	-fno-inline-functions-called-once -fsanitize=bounds-strict \
384869b5016SZehui Xu	-fstrict-flex-arrays=% -fmin-function-alignment=% \
385977c4308SRudraksha Gupta	-fzero-init-padding-bits=% -mno-fdpic \
386fe4b3a34SXi Ruoyao	--param=% --param asan-% -fno-isolate-erroneous-paths-dereference
3872f7ab126SMiguel Ojeda
3882f7ab126SMiguel Ojeda# Derived from `scripts/Makefile.clang`.
3892f7ab126SMiguel OjedaBINDGEN_TARGET_x86	:= x86_64-linux-gnu
390724a75acSJamie CunliffeBINDGEN_TARGET_arm64	:= aarch64-linux-gnu
391ccb8ce52SChristian SchrreflBINDGEN_TARGET_arm	:= arm-linux-gnueabi
39213c23cb4SWANG RuiBINDGEN_TARGET_loongarch	:= loongarch64-linux-gnusf
3936b2dab17SThomas WeißschuhBINDGEN_TARGET_um	:= $(BINDGEN_TARGET_$(SUBARCH))
3942f7ab126SMiguel OjedaBINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
3952f7ab126SMiguel Ojeda
3962f7ab126SMiguel Ojeda# All warnings are inhibited since GCC builds are very experimental,
3972f7ab126SMiguel Ojeda# many GCC warnings are not supported by Clang, they may only appear in
3982f7ab126SMiguel Ojeda# some configurations, with new GCC versions, etc.
3992f7ab126SMiguel Ojedabindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
4002f7ab126SMiguel Ojeda
401d966c3caSAndrea Righi# Auto variable zero-initialization requires an additional special option with
402d966c3caSAndrea Righi# clang that is going to be removed sometime in the future (likely in
403d966c3caSAndrea Righi# clang-18), so make sure to pass this option only if clang supports it
404d966c3caSAndrea Righi# (libclang major version < 16).
405d966c3caSAndrea Righi#
406d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/issues/44842
407d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
408d966c3caSAndrea Righiifdef CONFIG_INIT_STACK_ALL_ZERO
409d966c3caSAndrea Righilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
410d966c3caSAndrea Righiifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
411d966c3caSAndrea Righibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
412d966c3caSAndrea Righiendif
413d966c3caSAndrea Righiendif
414d966c3caSAndrea Righi
4152f7ab126SMiguel Ojedabindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
4162f7ab126SMiguel Ojeda	$(bindgen_extra_c_flags)
4172f7ab126SMiguel Ojedaendif
4182f7ab126SMiguel Ojeda
4192f7ab126SMiguel Ojedaifdef CONFIG_LTO
4202f7ab126SMiguel Ojedabindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
4212f7ab126SMiguel Ojedaelse
4222f7ab126SMiguel Ojedabindgen_c_flags_lto = $(bindgen_c_flags)
4232f7ab126SMiguel Ojedaendif
4242f7ab126SMiguel Ojeda
42575c1fd41SGary Guo# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
42675c1fd41SGary Guo# prototypes for functions like `memcpy` -- if this flag is not passed,
42775c1fd41SGary Guo# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
42875c1fd41SGary Guo# architecture instead of generating `usize`.
42975c1fd41SGary Guobindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
4302f7ab126SMiguel Ojeda
4317a5f93eaSMiguel Ojeda# Each `bindgen` release may upgrade the list of Rust target versions. By
4327a5f93eaSMiguel Ojeda# default, the highest stable release in their list is used. Thus we need to set
4337a5f93eaSMiguel Ojeda# a `--rust-target` to avoid future `bindgen` releases emitting code that
4347a5f93eaSMiguel Ojeda# `rustc` may not understand. On top of that, `bindgen` does not support passing
4357a5f93eaSMiguel Ojeda# an unknown Rust target version.
4367a5f93eaSMiguel Ojeda#
4377a5f93eaSMiguel Ojeda# Therefore, the Rust target for `bindgen` can be only as high as the minimum
4387a5f93eaSMiguel Ojeda# Rust version the kernel supports and only as high as the greatest stable Rust
4397a5f93eaSMiguel Ojeda# target supported by the minimum `bindgen` version the kernel supports (that
4407a5f93eaSMiguel Ojeda# is, if we do not test the actual `rustc`/`bindgen` versions running).
4417a5f93eaSMiguel Ojeda#
4427a5f93eaSMiguel Ojeda# Starting with `bindgen` 0.71.0, we will be able to set any future Rust version
4437a5f93eaSMiguel Ojeda# instead, i.e. we will be able to set here our minimum supported Rust version.
4442f7ab126SMiguel Ojedaquiet_cmd_bindgen = BINDGEN $@
4452f7ab126SMiguel Ojeda      cmd_bindgen = \
4467a5f93eaSMiguel Ojeda	$(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \
447d072acdaSGary Guo		--use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
44876501d19SMiguel Ojeda		--no-debug '.*' --enable-function-attribute-detection \
44908ab7865SAakash Sen Sharma		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
4502f7ab126SMiguel Ojeda		$(bindgen_target_cflags) $(bindgen_target_extra)
4512f7ab126SMiguel Ojeda
4522f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
453b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
45474376656SGary Guo$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
45574376656SGary Guo    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
4562f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
4572f7ab126SMiguel Ojeda    $(src)/bindgen_parameters FORCE
4582f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
4592f7ab126SMiguel Ojeda
4604e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
461b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
4624e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
4634e174665SAsahi Lina    $(src)/bindgen_parameters FORCE
4644e174665SAsahi Lina	$(call if_changed_dep,bindgen)
4654e174665SAsahi Lina
4662f7ab126SMiguel Ojeda# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
4672f7ab126SMiguel Ojeda# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
4682f7ab126SMiguel Ojeda# given it is `libclang`; but for consistency, future Clang changes and/or
4692f7ab126SMiguel Ojeda# a potential future GCC backend for `bindgen`, we disable it too.
4702f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
47108ab7865SAakash Sen Sharma    --blocklist-type '.*' --allowlist-var '' \
47208ab7865SAakash Sen Sharma    --allowlist-function 'rust_helper_.*'
4732f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
4742f7ab126SMiguel Ojeda    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
4752f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
4762f7ab126SMiguel Ojeda    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
47787634653SAndreas Hindborg$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
4782f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
4792f7ab126SMiguel Ojeda
480c59026c0SSami Tolvanenrust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
481ac61506bSSami Tolvanen
4822f7ab126SMiguel Ojedaquiet_cmd_exports = EXPORTS $@
4832f7ab126SMiguel Ojeda      cmd_exports = \
484ac61506bSSami Tolvanen	$(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
4852f7ab126SMiguel Ojeda
4862f7ab126SMiguel Ojeda$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
4872f7ab126SMiguel Ojeda	$(call if_changed,exports)
4882f7ab126SMiguel Ojeda
489e26fa546SGary Guo# Even though Rust kernel modules should never use the bindings directly,
490e26fa546SGary Guo# symbols from the `bindings` crate and the C helpers need to be exported
491e26fa546SGary Guo# because Rust generics and inlined functions may not get their code generated
492e26fa546SGary Guo# in the crate where they are defined. Other helpers, called from non-inline
493e26fa546SGary Guo# functions, may not be exported, in principle. However, in general, the Rust
494e26fa546SGary Guo# compiler does not guarantee codegen will be performed for a non-inline
495e26fa546SGary Guo# function either. Therefore, we export all symbols from helpers and bindings.
496e26fa546SGary Guo# In the future, this may be revisited to reduce the number of exports after
497e26fa546SGary Guo# the compiler is informed about the places codegen is required.
498e26fa546SGary Guo$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
499e26fa546SGary Guo	$(call if_changed,exports)
500e26fa546SGary Guo
5012f7ab126SMiguel Ojeda$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
5022f7ab126SMiguel Ojeda	$(call if_changed,exports)
5032f7ab126SMiguel Ojeda
5042f7ab126SMiguel Ojeda$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
5052f7ab126SMiguel Ojeda	$(call if_changed,exports)
5062f7ab126SMiguel Ojeda
5077dbe46c0SMiguel Ojedaquiet_cmd_rustc_procmacrolibrary = $(RUSTC_OR_CLIPPY_QUIET) PL $@
5087dbe46c0SMiguel Ojeda      cmd_rustc_procmacrolibrary = \
5097dbe46c0SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
5107dbe46c0SMiguel Ojeda		$(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
5117dbe46c0SMiguel Ojeda		--emit=dep-info,link --crate-type rlib -O \
5127dbe46c0SMiguel Ojeda		--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
5137dbe46c0SMiguel Ojeda		--crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<; \
5147dbe46c0SMiguel Ojeda	mv $(objtree)/$(obj)/$(patsubst lib%.rlib,%,$(notdir $@)).d $(depfile); \
5157dbe46c0SMiguel Ojeda	sed -i '/^\#/d' $(depfile)
5167dbe46c0SMiguel Ojeda
517158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: private skip_clippy = 1
518158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
519158a3b72SMiguel Ojeda$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
520158a3b72SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
521158a3b72SMiguel Ojeda
52288de91ccSMiguel Ojeda$(obj)/libquote.rlib: private skip_clippy = 1
52388de91ccSMiguel Ojeda$(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
52488de91ccSMiguel Ojeda$(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
52588de91ccSMiguel Ojeda$(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
52688de91ccSMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
52788de91ccSMiguel Ojeda
52873740175SMiguel Ojeda$(obj)/libsyn.rlib: private skip_clippy = 1
52973740175SMiguel Ojeda$(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
53073740175SMiguel Ojeda$(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
53173740175SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacrolibrary)
53273740175SMiguel Ojeda
5332f7ab126SMiguel Ojedaquiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
5342f7ab126SMiguel Ojeda      cmd_rustc_procmacro = \
535d7659accSMiguel Ojeda	$(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \
53680bac83aSMatthew Maurer		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
537ceff0757SHONG Yifan		-Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
538295d8398SMasahiro Yamada		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
539c46b34f1SMiguel Ojeda		--crate-type proc-macro -L$(objtree)/$(obj) \
54036174d16SMiguel Ojeda		--crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \
54136174d16SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
5422f7ab126SMiguel Ojeda
5432f7ab126SMiguel Ojeda# Procedural macros can only be used with the `rustc` that compiled it.
544*52ba807fSMiguel Ojeda$(obj)/$(libmacros_name): private rustc_target_flags = \
545*52ba807fSMiguel Ojeda    --extern proc_macro2 --extern quote --extern syn
54688de91ccSMiguel Ojeda$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
54773740175SMiguel Ojeda    $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
548ecab4115SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
5492f7ab126SMiguel Ojeda
550d7659accSMiguel Ojeda$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel
551dbd5058bSBenno Lossin$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs FORCE
552d7659accSMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
553d7659accSMiguel Ojeda
5542f7ab126SMiguel Ojedaquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
5552f7ab126SMiguel Ojeda      cmd_rustc_library = \
5562f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
5572f7ab126SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
558f4daa80dSGary Guo		$(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
559295d8398SMasahiro Yamada		--emit=dep-info=$(depfile) --emit=obj=$@ \
560295d8398SMasahiro Yamada		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
561295d8398SMasahiro Yamada		--crate-type rlib -L$(objtree)/$(obj) \
5622185242fSMasahiro Yamada		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
56371479eeeSMatthew Maurer		--sysroot=/dev/null \
564c4d7f546SMiguel Ojeda	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
565c4d7f546SMiguel Ojeda	$(cmd_objtool)
5662f7ab126SMiguel Ojeda
5672f7ab126SMiguel Ojedarust-analyzer:
5680730422bSTamir Duberstein	$(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
569f4daa80dSGary Guo		--cfgs='core=$(core-cfgs)' $(core-edition) \
570158a3b72SMiguel Ojeda		--cfgs='proc_macro2=$(proc_macro2-cfgs)' \
57188de91ccSMiguel Ojeda		--cfgs='quote=$(quote-cfgs)' \
57273740175SMiguel Ojeda		--cfgs='syn=$(syn-cfgs)' \
573e2bad142SMasahiro Yamada		$(realpath $(srctree)) $(realpath $(objtree)) \
57413b25489SMasahiro Yamada		$(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
57513b25489SMasahiro Yamada		> rust-project.json
5762f7ab126SMiguel Ojeda
577cb7d9defSGary Guoredirect-intrinsics = \
57802dfd63aSMiguel Ojeda	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
57902dfd63aSMiguel Ojeda	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
580cb7d9defSGary Guo	__muloti4 __multi3 \
581cb7d9defSGary Guo	__udivmodti4 __udivti3 __umodti3
582cb7d9defSGary Guo
583ccb8ce52SChristian Schrreflifdef CONFIG_ARM
584ccb8ce52SChristian Schrrefl	# Add eabi initrinsics for ARM 32-bit
585ccb8ce52SChristian Schrrefl	redirect-intrinsics += \
586ccb8ce52SChristian Schrrefl		__aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
587ccb8ce52SChristian Schrrefl		__aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
588ccb8ce52SChristian Schrrefl		__aeabi_uldivmod
589ccb8ce52SChristian Schrreflendif
590cb7d9defSGary Guoifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
591cb7d9defSGary Guo	# These intrinsics are defined for ARM64 and RISCV64
592cb7d9defSGary Guo	redirect-intrinsics += \
593cb7d9defSGary Guo		__ashrti3 \
594cb7d9defSGary Guo		__ashlti3 __lshrti3
595cb7d9defSGary Guoendif
596cb7d9defSGary Guo
597ac61506bSSami Tolvanenifdef CONFIG_MODVERSIONS
598ac61506bSSami Tolvanencmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
599ac61506bSSami Tolvanen	$(call rust_exports,$@,"%s\n") | \
600ac61506bSSami Tolvanen	scripts/gendwarfksyms/gendwarfksyms \
601ac61506bSSami Tolvanen		$(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
602ac61506bSSami Tolvanen		$(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
603ac61506bSSami Tolvanen		$@ >> $(dot-target).cmd)
604ac61506bSSami Tolvanenendif
605ac61506bSSami Tolvanen
606c4d7f546SMiguel Ojedadefine rule_rustc_library
607c4d7f546SMiguel Ojeda	$(call cmd_and_fixdep,rustc_library)
608c4d7f546SMiguel Ojeda	$(call cmd,gen_objtooldep)
609ac61506bSSami Tolvanen	$(call cmd,gendwarfksyms)
610c4d7f546SMiguel Ojedaendef
611c4d7f546SMiguel Ojeda
612ac61506bSSami Tolvanendefine rule_rust_cc_library
613ac61506bSSami Tolvanen	$(call if_changed_rule,cc_o_c)
614ac61506bSSami Tolvanen	$(call cmd,force_checksrc)
615ac61506bSSami Tolvanen	$(call cmd,gendwarfksyms)
616ac61506bSSami Tolvanenendef
617ac61506bSSami Tolvanen
618ac61506bSSami Tolvanen# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
619ac61506bSSami Tolvanen# versions are calculated for the helpers too.
620ac61506bSSami Tolvanen$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
621ac61506bSSami Tolvanen	+$(call if_changed_rule,rust_cc_library)
622ac61506bSSami Tolvanen
623ac61506bSSami Tolvanen# Disable symbol versioning for exports.o to avoid conflicts with the actual
624ac61506bSSami Tolvanen# symbol versions generated from Rust objects.
625ac61506bSSami Tolvanen$(obj)/exports.o: private skip_gendwarfksyms = 1
626ac61506bSSami Tolvanen
6272f7ab126SMiguel Ojeda$(obj)/core.o: private skip_clippy = 1
62846e58a96SMiguel Ojeda$(obj)/core.o: private skip_flags = $(core-skip_flags)
629cb7d9defSGary Guo$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
63046e58a96SMiguel Ojeda$(obj)/core.o: private rustc_target_flags = $(core-flags)
631ac3e9726SMiguel Ojeda$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
632ac3e9726SMiguel Ojeda    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
633c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
634ab0f4cedSDavid Gowifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
635f82811e2SJamie Cunliffe$(obj)/core.o: scripts/target.json
636f82811e2SJamie Cunliffeendif
6373bf67171SAlice RyhlKCOV_INSTRUMENT_core.o := n
6382f7ab126SMiguel Ojeda
639ac61506bSSami Tolvanen$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
6402f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
6412f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
642c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
6432f7ab126SMiguel Ojeda
644d7659accSMiguel Ojeda$(obj)/pin_init.o: private skip_gendwarfksyms = 1
645d7659accSMiguel Ojeda$(obj)/pin_init.o: private rustc_target_flags = --extern pin_init_internal \
646d7659accSMiguel Ojeda    --extern macros --cfg kernel
647dbd5058bSBenno Lossin$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
648d7659accSMiguel Ojeda    $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
649d7659accSMiguel Ojeda	+$(call if_changed_rule,rustc_library)
650d7659accSMiguel Ojeda
65116c43a56SMiguel Ojeda# Even if normally `build_error` is not a kernel object, it should still be
65216c43a56SMiguel Ojeda# treated as such so that we pass the same flags. Otherwise, for instance,
65316c43a56SMiguel Ojeda# `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
65416c43a56SMiguel Ojeda$(obj)/build_error.o: private is-kernel-object := y
655ac61506bSSami Tolvanen$(obj)/build_error.o: private skip_gendwarfksyms = 1
656ecaa6ddfSGary Guo$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
657c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
658ecaa6ddfSGary Guo
659ac61506bSSami Tolvanen$(obj)/ffi.o: private skip_gendwarfksyms = 1
660d072acdaSGary Guo$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
661d072acdaSGary Guo	+$(call if_changed_rule,rustc_library)
662d072acdaSGary Guo
6633c847e17SBenno Lossin$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
6642f7ab126SMiguel Ojeda$(obj)/bindings.o: $(src)/bindings/lib.rs \
665d072acdaSGary Guo    $(obj)/ffi.o \
6663c847e17SBenno Lossin    $(obj)/pin_init.o \
6672f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_generated.rs \
6682f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_helpers_generated.rs FORCE
669c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
6702f7ab126SMiguel Ojeda
6713c847e17SBenno Lossin$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
672ac61506bSSami Tolvanen$(obj)/uapi.o: private skip_gendwarfksyms = 1
6734e174665SAsahi Lina$(obj)/uapi.o: $(src)/uapi/lib.rs \
674d072acdaSGary Guo    $(obj)/ffi.o \
6753c847e17SBenno Lossin    $(obj)/pin_init.o \
6764e174665SAsahi Lina    $(obj)/uapi/uapi_generated.rs FORCE
677c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
6784e174665SAsahi Lina
679d7659accSMiguel Ojeda$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
6804e174665SAsahi Lina    --extern build_error --extern macros --extern bindings --extern uapi
681d7659accSMiguel Ojeda$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
6820730422bSTamir Duberstein    $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE
683c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
6842f7ab126SMiguel Ojeda
685169484abSAlice Ryhlifdef CONFIG_JUMP_LABEL
6868af7a501SMiguel Ojeda$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
687169484abSAlice Ryhlendif
688dff64b07SFUJITA Tomonoriifndef CONFIG_UML
689dff64b07SFUJITA Tomonoriifdef CONFIG_BUG
690dff64b07SFUJITA Tomonori$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
691dff64b07SFUJITA Tomonoriendif
692dff64b07SFUJITA Tomonoriendif
693169484abSAlice Ryhl
6942f7ab126SMiguel Ojedaendif # CONFIG_RUST
695