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