xref: /linux/rust/Makefile (revision 392e34b6bc22077ef63abf62387ea3e9f39418c1)
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
62f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += core.o compiler_builtins.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) += libmacros.so
152f7ab126SMiguel Ojedano-clean-files += libmacros.so
162f7ab126SMiguel Ojeda
172f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
18*392e34b6SDanilo Krummrichobj-$(CONFIG_RUST) += bindings.o kernel.o
19*392e34b6SDanilo Krummrichalways-$(CONFIG_RUST) += exports_helpers_generated.h \
20e26fa546SGary Guo    exports_bindings_generated.h exports_kernel_generated.h
212f7ab126SMiguel Ojeda
224e174665SAsahi Linaalways-$(CONFIG_RUST) += uapi/uapi_generated.rs
234e174665SAsahi Linaobj-$(CONFIG_RUST) += uapi.o
244e174665SAsahi Lina
25ecaa6ddfSGary Guoifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
26ecaa6ddfSGary Guoobj-$(CONFIG_RUST) += build_error.o
27ecaa6ddfSGary Guoelse
28ecaa6ddfSGary Guoalways-$(CONFIG_RUST) += build_error.o
29ecaa6ddfSGary Guoendif
30ecaa6ddfSGary Guo
312f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += exports.o
322f7ab126SMiguel Ojeda
33a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
34a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
35a66d733dSMiguel Ojeda
36a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
37a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
38a66d733dSMiguel Ojeda
392f7ab126SMiguel Ojeda# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
402f7ab126SMiguel Ojedaifdef CONFIG_RUST
412f7ab126SMiguel Ojeda
422f7ab126SMiguel Ojeda# `$(rust_flags)` is passed in case the user added `--sysroot`.
43ecab4115SMiguel Ojedarustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
442f7ab126SMiguel Ojedarustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
452f7ab126SMiguel OjedaRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
462f7ab126SMiguel Ojeda
479ffc80c8SMiguel Ojedaifneq ($(quiet),)
482f7ab126SMiguel Ojedarust_test_quiet=-q
492f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q
50a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null
512f7ab126SMiguel Ojedaendif
522f7ab126SMiguel Ojeda
532f7ab126SMiguel Ojedacore-cfgs = \
542f7ab126SMiguel Ojeda    --cfg no_fp_fmt_parse
552f7ab126SMiguel Ojeda
562f7ab126SMiguel Ojedaquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
572f7ab126SMiguel Ojeda      cmd_rustdoc = \
582f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
59bef83245SMiguel Ojeda	$(RUSTDOC) $(filter-out $(skip_flags),$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
602f7ab126SMiguel Ojeda		$(rustc_target_flags) -L$(objtree)/$(obj) \
616e6efc5fSMiguel Ojeda		-Zunstable-options --generate-link-to-definition \
6248fadf44SCarlos Bilbao		--output $(rustdoc_output) \
632f7ab126SMiguel Ojeda		--crate-name $(subst rustdoc-,,$@) \
6471479eeeSMatthew Maurer		$(if $(rustdoc_host),,--sysroot=/dev/null) \
652f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $<
662f7ab126SMiguel Ojeda
672f7ab126SMiguel Ojeda# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
682f7ab126SMiguel Ojeda# can be used to specify a custom logo. However:
692f7ab126SMiguel Ojeda#   - The given value is used as-is, thus it cannot be relative or a local file
702f7ab126SMiguel Ojeda#     (unlike the non-custom case) since the generated docs have subfolders.
712f7ab126SMiguel Ojeda#   - It requires adding it to every crate.
722f7ab126SMiguel Ojeda#   - It requires changing `core` which comes from the sysroot.
732f7ab126SMiguel Ojeda#
742f7ab126SMiguel Ojeda# Using `-Zcrate-attr` would solve the last two points, but not the first.
752f7ab126SMiguel Ojeda# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
762f7ab126SMiguel Ojeda# command-like flags to solve the issue. Meanwhile, we use the non-custom case
772f7ab126SMiguel Ojeda# and then retouch the generated files.
782f7ab126SMiguel Ojedarustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
79*392e34b6SDanilo Krummrich    rustdoc-kernel
80cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
81cfd96726SMiguel Ojeda	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
8248fadf44SCarlos Bilbao	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
83cfd96726SMiguel Ojeda		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
84cfd96726SMiguel Ojeda		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
85bc2e7d5cSMiguel Ojeda		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
86e2bad142SMasahiro Yamada		-e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
87cfd96726SMiguel Ojeda	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
88cfd96726SMiguel Ojeda		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
892f7ab126SMiguel Ojeda
902f7ab126SMiguel Ojedarustdoc-macros: private rustdoc_host = yes
912f7ab126SMiguel Ojedarustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
922f7ab126SMiguel Ojeda    --extern proc_macro
932f7ab126SMiguel Ojedarustdoc-macros: $(src)/macros/lib.rs FORCE
94ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
952f7ab126SMiguel Ojeda
96bef83245SMiguel Ojeda# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
97bef83245SMiguel Ojeda# not be needed -- see https://github.com/rust-lang/rust/pull/128307.
98bef83245SMiguel Ojedarustdoc-core: private skip_flags = -Wrustdoc::unescaped_backticks
992f7ab126SMiguel Ojedarustdoc-core: private rustc_target_flags = $(core-cfgs)
1002f7ab126SMiguel Ojedarustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
101ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
1022f7ab126SMiguel Ojeda
1032f7ab126SMiguel Ojedarustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
104ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
1052f7ab126SMiguel Ojeda
106*392e34b6SDanilo Krummrichrustdoc-kernel: private rustc_target_flags = \
107ecaa6ddfSGary Guo    --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
1084e174665SAsahi Lina    --extern bindings --extern uapi
1092f7ab126SMiguel Ojedarustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
110*392e34b6SDanilo Krummrich    rustdoc-compiler_builtins $(obj)/libmacros.so \
1112f7ab126SMiguel Ojeda    $(obj)/bindings.o FORCE
112ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc)
1132f7ab126SMiguel Ojeda
1142f7ab126SMiguel Ojedaquiet_cmd_rustc_test_library = RUSTC TL $<
1152f7ab126SMiguel Ojeda      cmd_rustc_test_library = \
1162f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
1172f7ab126SMiguel Ojeda	$(RUSTC) $(rust_common_flags) \
1182f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
1192f7ab126SMiguel Ojeda		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
1202f7ab126SMiguel Ojeda		--out-dir $(objtree)/$(obj)/test --cfg testlib \
1212f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
1222f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
1232f7ab126SMiguel Ojeda
1249ffc80c8SMiguel Ojedarusttestlib-build_error: $(src)/build_error.rs FORCE
125ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
126ecaa6ddfSGary Guo
1272f7ab126SMiguel Ojedarusttestlib-macros: private rustc_target_flags = --extern proc_macro
1282f7ab126SMiguel Ojedarusttestlib-macros: private rustc_test_library_proc = yes
1299ffc80c8SMiguel Ojedarusttestlib-macros: $(src)/macros/lib.rs FORCE
130ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
1312f7ab126SMiguel Ojeda
1329ffc80c8SMiguel Ojedarusttestlib-bindings: $(src)/bindings/lib.rs FORCE
133ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
1342f7ab126SMiguel Ojeda
1359ffc80c8SMiguel Ojedarusttestlib-uapi: $(src)/uapi/lib.rs FORCE
136ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
1374e174665SAsahi Lina
1382f7ab126SMiguel Ojedaquiet_cmd_rustdoc_test = RUSTDOC T $<
1392f7ab126SMiguel Ojeda      cmd_rustdoc_test = \
1402f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
1412f7ab126SMiguel Ojeda	$(RUSTDOC) --test $(rust_common_flags) \
1422f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
1432f7ab126SMiguel Ojeda		$(rustc_target_flags) $(rustdoc_test_target_flags) \
1449ffc80c8SMiguel Ojeda		$(rustdoc_test_quiet) \
14548fadf44SCarlos Bilbao		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
1462f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<
1472f7ab126SMiguel Ojeda
148a66d733dSMiguel Ojedaquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
149a66d733dSMiguel Ojeda      cmd_rustdoc_test_kernel = \
150a66d733dSMiguel Ojeda	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
151a66d733dSMiguel Ojeda	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
152a66d733dSMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
153a66d733dSMiguel Ojeda	$(RUSTDOC) --test $(rust_flags) \
154*392e34b6SDanilo Krummrich		-L$(objtree)/$(obj) --extern kernel \
155a66d733dSMiguel Ojeda		--extern build_error --extern macros \
156a66d733dSMiguel Ojeda		--extern bindings --extern uapi \
157a66d733dSMiguel Ojeda		--no-run --crate-name kernel -Zunstable-options \
15871479eeeSMatthew Maurer		--sysroot=/dev/null \
159a66d733dSMiguel Ojeda		--test-builder $(objtree)/scripts/rustdoc_test_builder \
160a66d733dSMiguel Ojeda		$< $(rustdoc_test_kernel_quiet); \
161a66d733dSMiguel Ojeda	$(objtree)/scripts/rustdoc_test_gen
162a66d733dSMiguel Ojeda
163a66d733dSMiguel Ojeda%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
164a66d733dSMiguel Ojeda    $(src)/kernel/lib.rs $(obj)/kernel.o \
165a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_builder \
166a66d733dSMiguel Ojeda    $(objtree)/scripts/rustdoc_test_gen FORCE
167ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test_kernel)
168a66d733dSMiguel Ojeda
1692f7ab126SMiguel Ojeda# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
1702f7ab126SMiguel Ojeda# so for the moment we skip `-Cpanic=abort`.
1712f7ab126SMiguel Ojedaquiet_cmd_rustc_test = RUSTC T  $<
1722f7ab126SMiguel Ojeda      cmd_rustc_test = \
1732f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
1742f7ab126SMiguel Ojeda	$(RUSTC) --test $(rust_common_flags) \
1752f7ab126SMiguel Ojeda		@$(objtree)/include/generated/rustc_cfg \
1762f7ab126SMiguel Ojeda		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
1772f7ab126SMiguel Ojeda		-L$(objtree)/$(obj)/test \
1782f7ab126SMiguel Ojeda		--crate-name $(subst rusttest-,,$@) $<; \
1792f7ab126SMiguel Ojeda	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
1802f7ab126SMiguel Ojeda		$(rustc_test_run_flags)
1812f7ab126SMiguel Ojeda
1822f7ab126SMiguel Ojedarusttest: rusttest-macros rusttest-kernel
1832f7ab126SMiguel Ojeda
1842f7ab126SMiguel Ojedarusttest-macros: private rustc_target_flags = --extern proc_macro
1852f7ab126SMiguel Ojedarusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
1869ffc80c8SMiguel Ojedarusttest-macros: $(src)/macros/lib.rs FORCE
187ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test)
188ecab4115SMiguel Ojeda	+$(call if_changed,rustdoc_test)
1892f7ab126SMiguel Ojeda
190*392e34b6SDanilo Krummrichrusttest-kernel: private rustc_target_flags = \
1914e174665SAsahi Lina    --extern build_error --extern macros --extern bindings --extern uapi
1929ffc80c8SMiguel Ojedarusttest-kernel: $(src)/kernel/lib.rs \
1934e174665SAsahi Lina    rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
1944e174665SAsahi Lina    rusttestlib-uapi FORCE
195ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test)
196ecab4115SMiguel Ojeda	+$(call if_changed,rustc_test_library)
1972f7ab126SMiguel Ojeda
1982f7ab126SMiguel Ojedaifdef CONFIG_CC_IS_CLANG
1992f7ab126SMiguel Ojedabindgen_c_flags = $(c_flags)
2002f7ab126SMiguel Ojedaelse
2012f7ab126SMiguel Ojeda# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
2022f7ab126SMiguel Ojeda# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
2032f7ab126SMiguel Ojeda#
2042f7ab126SMiguel Ojeda# For the moment, here we are tweaking the flags on the fly. This is a hack,
2052f7ab126SMiguel Ojeda# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
2062f7ab126SMiguel Ojeda# if we end up using one of those structs).
2072f7ab126SMiguel Ojedabindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
2082f7ab126SMiguel Ojeda	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
2092f7ab126SMiguel Ojeda	-mindirect-branch=thunk-extern -mindirect-branch-register \
2102f7ab126SMiguel Ojeda	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
2112f7ab126SMiguel Ojeda	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
2122f7ab126SMiguel Ojeda	-mno-pointers-to-nested-functions -mno-string \
2132f7ab126SMiguel Ojeda	-mno-strict-align -mstrict-align \
2142f7ab126SMiguel Ojeda	-fconserve-stack -falign-jumps=% -falign-loops=% \
2152f7ab126SMiguel Ojeda	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
2162f7ab126SMiguel Ojeda	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
2172f7ab126SMiguel Ojeda	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
2182f7ab126SMiguel Ojeda	-fzero-call-used-regs=% -fno-stack-clash-protection \
219b0554488SAndrea Righi	-fno-inline-functions-called-once -fsanitize=bounds-strict \
220869b5016SZehui Xu	-fstrict-flex-arrays=% -fmin-function-alignment=% \
2212f7ab126SMiguel Ojeda	--param=% --param asan-%
2222f7ab126SMiguel Ojeda
2232f7ab126SMiguel Ojeda# Derived from `scripts/Makefile.clang`.
2242f7ab126SMiguel OjedaBINDGEN_TARGET_x86	:= x86_64-linux-gnu
225724a75acSJamie CunliffeBINDGEN_TARGET_arm64	:= aarch64-linux-gnu
2262f7ab126SMiguel OjedaBINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
2272f7ab126SMiguel Ojeda
2282f7ab126SMiguel Ojeda# All warnings are inhibited since GCC builds are very experimental,
2292f7ab126SMiguel Ojeda# many GCC warnings are not supported by Clang, they may only appear in
2302f7ab126SMiguel Ojeda# some configurations, with new GCC versions, etc.
2312f7ab126SMiguel Ojedabindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
2322f7ab126SMiguel Ojeda
233d966c3caSAndrea Righi# Auto variable zero-initialization requires an additional special option with
234d966c3caSAndrea Righi# clang that is going to be removed sometime in the future (likely in
235d966c3caSAndrea Righi# clang-18), so make sure to pass this option only if clang supports it
236d966c3caSAndrea Righi# (libclang major version < 16).
237d966c3caSAndrea Righi#
238d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/issues/44842
239d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
240d966c3caSAndrea Righiifdef CONFIG_INIT_STACK_ALL_ZERO
241d966c3caSAndrea Righilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
242d966c3caSAndrea Righiifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
243d966c3caSAndrea Righibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
244d966c3caSAndrea Righiendif
245d966c3caSAndrea Righiendif
246d966c3caSAndrea Righi
2472f7ab126SMiguel Ojedabindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
2482f7ab126SMiguel Ojeda	$(bindgen_extra_c_flags)
2492f7ab126SMiguel Ojedaendif
2502f7ab126SMiguel Ojeda
2512f7ab126SMiguel Ojedaifdef CONFIG_LTO
2522f7ab126SMiguel Ojedabindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
2532f7ab126SMiguel Ojedaelse
2542f7ab126SMiguel Ojedabindgen_c_flags_lto = $(bindgen_c_flags)
2552f7ab126SMiguel Ojedaendif
2562f7ab126SMiguel Ojeda
2572f7ab126SMiguel Ojedabindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
2582f7ab126SMiguel Ojeda
2592f7ab126SMiguel Ojedaquiet_cmd_bindgen = BINDGEN $@
2602f7ab126SMiguel Ojeda      cmd_bindgen = \
2612f7ab126SMiguel Ojeda	$(BINDGEN) $< $(bindgen_target_flags) \
2622f7ab126SMiguel Ojeda		--use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
26376501d19SMiguel Ojeda		--no-debug '.*' --enable-function-attribute-detection \
26408ab7865SAakash Sen Sharma		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
2652f7ab126SMiguel Ojeda		$(bindgen_target_cflags) $(bindgen_target_extra)
2662f7ab126SMiguel Ojeda
2672f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
268b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
26974376656SGary Guo$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
27074376656SGary Guo    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
2712f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
2722f7ab126SMiguel Ojeda    $(src)/bindgen_parameters FORCE
2732f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
2742f7ab126SMiguel Ojeda
2754e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
276b1992c37SMasahiro Yamada    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
2774e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
2784e174665SAsahi Lina    $(src)/bindgen_parameters FORCE
2794e174665SAsahi Lina	$(call if_changed_dep,bindgen)
2804e174665SAsahi Lina
2812f7ab126SMiguel Ojeda# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
2822f7ab126SMiguel Ojeda# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
2832f7ab126SMiguel Ojeda# given it is `libclang`; but for consistency, future Clang changes and/or
2842f7ab126SMiguel Ojeda# a potential future GCC backend for `bindgen`, we disable it too.
2852f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
28608ab7865SAakash Sen Sharma    --blocklist-type '.*' --allowlist-var '' \
28708ab7865SAakash Sen Sharma    --allowlist-function 'rust_helper_.*'
2882f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
2892f7ab126SMiguel Ojeda    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
2902f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
2912f7ab126SMiguel Ojeda    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
29287634653SAndreas Hindborg$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
2932f7ab126SMiguel Ojeda	$(call if_changed_dep,bindgen)
2942f7ab126SMiguel Ojeda
2952f7ab126SMiguel Ojedaquiet_cmd_exports = EXPORTS $@
2962f7ab126SMiguel Ojeda      cmd_exports = \
2972f7ab126SMiguel Ojeda	$(NM) -p --defined-only $< \
29857017256SLinus Torvalds		| awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
2992f7ab126SMiguel Ojeda
3002f7ab126SMiguel Ojeda$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
3012f7ab126SMiguel Ojeda	$(call if_changed,exports)
3022f7ab126SMiguel Ojeda
303e26fa546SGary Guo# Even though Rust kernel modules should never use the bindings directly,
304e26fa546SGary Guo# symbols from the `bindings` crate and the C helpers need to be exported
305e26fa546SGary Guo# because Rust generics and inlined functions may not get their code generated
306e26fa546SGary Guo# in the crate where they are defined. Other helpers, called from non-inline
307e26fa546SGary Guo# functions, may not be exported, in principle. However, in general, the Rust
308e26fa546SGary Guo# compiler does not guarantee codegen will be performed for a non-inline
309e26fa546SGary Guo# function either. Therefore, we export all symbols from helpers and bindings.
310e26fa546SGary Guo# In the future, this may be revisited to reduce the number of exports after
311e26fa546SGary Guo# the compiler is informed about the places codegen is required.
312e26fa546SGary Guo$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
313e26fa546SGary Guo	$(call if_changed,exports)
314e26fa546SGary Guo
3152f7ab126SMiguel Ojeda$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
3162f7ab126SMiguel Ojeda	$(call if_changed,exports)
3172f7ab126SMiguel Ojeda
3182f7ab126SMiguel Ojeda$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
3192f7ab126SMiguel Ojeda	$(call if_changed,exports)
3202f7ab126SMiguel Ojeda
3212f7ab126SMiguel Ojedaquiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
3222f7ab126SMiguel Ojeda      cmd_rustc_procmacro = \
3232f7ab126SMiguel Ojeda	$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
32480bac83aSMatthew Maurer		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
32580bac83aSMatthew Maurer		-Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
326295d8398SMasahiro Yamada		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
327295d8398SMasahiro Yamada		--crate-type proc-macro \
3282185242fSMasahiro Yamada		--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
3292f7ab126SMiguel Ojeda
3302f7ab126SMiguel Ojeda# Procedural macros can only be used with the `rustc` that compiled it.
331aeb0e24aSMiguel Ojeda$(obj)/libmacros.so: $(src)/macros/lib.rs FORCE
332ecab4115SMiguel Ojeda	+$(call if_changed_dep,rustc_procmacro)
3332f7ab126SMiguel Ojeda
3342f7ab126SMiguel Ojedaquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
3352f7ab126SMiguel Ojeda      cmd_rustc_library = \
3362f7ab126SMiguel Ojeda	OBJTREE=$(abspath $(objtree)) \
3372f7ab126SMiguel Ojeda	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
3382f7ab126SMiguel Ojeda		$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
339295d8398SMasahiro Yamada		--emit=dep-info=$(depfile) --emit=obj=$@ \
340295d8398SMasahiro Yamada		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
341295d8398SMasahiro Yamada		--crate-type rlib -L$(objtree)/$(obj) \
3422185242fSMasahiro Yamada		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
34371479eeeSMatthew Maurer		--sysroot=/dev/null \
344c4d7f546SMiguel Ojeda	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
345c4d7f546SMiguel Ojeda	$(cmd_objtool)
3462f7ab126SMiguel Ojeda
3472f7ab126SMiguel Ojedarust-analyzer:
34849a9ef76SVinay Varma	$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
349*392e34b6SDanilo Krummrich		--cfgs='core=$(core-cfgs)' \
350e2bad142SMasahiro Yamada		$(realpath $(srctree)) $(realpath $(objtree)) \
351fe992163SSarthak Singh		$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
35249a9ef76SVinay Varma		$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
3532f7ab126SMiguel Ojeda
354cb7d9defSGary Guoredirect-intrinsics = \
35502dfd63aSMiguel Ojeda	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
35602dfd63aSMiguel Ojeda	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
357cb7d9defSGary Guo	__muloti4 __multi3 \
358cb7d9defSGary Guo	__udivmodti4 __udivti3 __umodti3
359cb7d9defSGary Guo
360cb7d9defSGary Guoifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
361cb7d9defSGary Guo	# These intrinsics are defined for ARM64 and RISCV64
362cb7d9defSGary Guo	redirect-intrinsics += \
363cb7d9defSGary Guo		__ashrti3 \
364cb7d9defSGary Guo		__ashlti3 __lshrti3
365cb7d9defSGary Guoendif
366cb7d9defSGary Guo
367c4d7f546SMiguel Ojedadefine rule_rustc_library
368c4d7f546SMiguel Ojeda	$(call cmd_and_fixdep,rustc_library)
369c4d7f546SMiguel Ojeda	$(call cmd,gen_objtooldep)
370c4d7f546SMiguel Ojedaendef
371c4d7f546SMiguel Ojeda
3722f7ab126SMiguel Ojeda$(obj)/core.o: private skip_clippy = 1
373f8f88aa2SMiguel Ojeda$(obj)/core.o: private skip_flags = -Wunreachable_pub
374cb7d9defSGary Guo$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
3752f7ab126SMiguel Ojeda$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
376ac3e9726SMiguel Ojeda$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
377ac3e9726SMiguel Ojeda    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
378c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
379ab0f4cedSDavid Gowifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
380f82811e2SJamie Cunliffe$(obj)/core.o: scripts/target.json
381f82811e2SJamie Cunliffeendif
3822f7ab126SMiguel Ojeda
3832f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
3842f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
385c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
3862f7ab126SMiguel Ojeda
387ecaa6ddfSGary Guo$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
388c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
389ecaa6ddfSGary Guo
3902f7ab126SMiguel Ojeda$(obj)/bindings.o: $(src)/bindings/lib.rs \
3912f7ab126SMiguel Ojeda    $(obj)/compiler_builtins.o \
3922f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_generated.rs \
3932f7ab126SMiguel Ojeda    $(obj)/bindings/bindings_helpers_generated.rs FORCE
394c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
3952f7ab126SMiguel Ojeda
3964e174665SAsahi Lina$(obj)/uapi.o: $(src)/uapi/lib.rs \
3974e174665SAsahi Lina    $(obj)/compiler_builtins.o \
3984e174665SAsahi Lina    $(obj)/uapi/uapi_generated.rs FORCE
399c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
4004e174665SAsahi Lina
401*392e34b6SDanilo Krummrich$(obj)/kernel.o: private rustc_target_flags = \
4024e174665SAsahi Lina    --extern build_error --extern macros --extern bindings --extern uapi
403*392e34b6SDanilo Krummrich$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \
4044e174665SAsahi Lina    $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
405c4d7f546SMiguel Ojeda	+$(call if_changed_rule,rustc_library)
4062f7ab126SMiguel Ojeda
4072f7ab126SMiguel Ojedaendif # CONFIG_RUST
408