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) += libmacros.so 152f7ab126SMiguel Ojedano-clean-files += libmacros.so 162f7ab126SMiguel Ojeda 172f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs 18392e34b6SDanilo Krummrichobj-$(CONFIG_RUST) += bindings.o kernel.o 19392e34b6SDanilo 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 398af7a501SMiguel Ojedaalways-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs 40169484abSAlice Ryhl 412f7ab126SMiguel Ojeda# Avoids running `$(RUSTC)` for the sysroot when it may not be available. 422f7ab126SMiguel Ojedaifdef CONFIG_RUST 432f7ab126SMiguel Ojeda 442f7ab126SMiguel Ojeda# `$(rust_flags)` is passed in case the user added `--sysroot`. 45ecab4115SMiguel Ojedarustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot) 462f7ab126SMiguel Ojedarustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2) 472f7ab126SMiguel OjedaRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library 482f7ab126SMiguel Ojeda 499ffc80c8SMiguel Ojedaifneq ($(quiet),) 502f7ab126SMiguel Ojedarust_test_quiet=-q 512f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q 52a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null 532f7ab126SMiguel Ojedaendif 542f7ab126SMiguel Ojeda 552f7ab126SMiguel Ojedacore-cfgs = \ 562f7ab126SMiguel Ojeda --cfg no_fp_fmt_parse 572f7ab126SMiguel Ojeda 582f7ab126SMiguel Ojedaquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $< 592f7ab126SMiguel Ojeda cmd_rustdoc = \ 602f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 61bef83245SMiguel Ojeda $(RUSTDOC) $(filter-out $(skip_flags),$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \ 622f7ab126SMiguel Ojeda $(rustc_target_flags) -L$(objtree)/$(obj) \ 636e6efc5fSMiguel Ojeda -Zunstable-options --generate-link-to-definition \ 6448fadf44SCarlos Bilbao --output $(rustdoc_output) \ 652f7ab126SMiguel Ojeda --crate-name $(subst rustdoc-,,$@) \ 6671479eeeSMatthew Maurer $(if $(rustdoc_host),,--sysroot=/dev/null) \ 672f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg $< 682f7ab126SMiguel Ojeda 692f7ab126SMiguel Ojeda# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute 702f7ab126SMiguel Ojeda# can be used to specify a custom logo. However: 712f7ab126SMiguel Ojeda# - The given value is used as-is, thus it cannot be relative or a local file 722f7ab126SMiguel Ojeda# (unlike the non-custom case) since the generated docs have subfolders. 732f7ab126SMiguel Ojeda# - It requires adding it to every crate. 742f7ab126SMiguel Ojeda# - It requires changing `core` which comes from the sysroot. 752f7ab126SMiguel Ojeda# 762f7ab126SMiguel Ojeda# Using `-Zcrate-attr` would solve the last two points, but not the first. 772f7ab126SMiguel Ojeda# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new 782f7ab126SMiguel Ojeda# command-like flags to solve the issue. Meanwhile, we use the non-custom case 792f7ab126SMiguel Ojeda# and then retouch the generated files. 802f7ab126SMiguel Ojedarustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \ 81392e34b6SDanilo Krummrich rustdoc-kernel 82cfd96726SMiguel Ojeda $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/ 83cfd96726SMiguel Ojeda $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/ 8448fadf44SCarlos Bilbao $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \ 85cfd96726SMiguel Ojeda -e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \ 86cfd96726SMiguel Ojeda -e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \ 87bc2e7d5cSMiguel Ojeda -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \ 88e2bad142SMasahiro Yamada -e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g' 89cfd96726SMiguel Ojeda $(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \ 90cfd96726SMiguel Ojeda echo ".logo-container > img { object-fit: contain; }" >> $$f; done 912f7ab126SMiguel Ojeda 922f7ab126SMiguel Ojedarustdoc-macros: private rustdoc_host = yes 932f7ab126SMiguel Ojedarustdoc-macros: private rustc_target_flags = --crate-type proc-macro \ 942f7ab126SMiguel Ojeda --extern proc_macro 952f7ab126SMiguel Ojedarustdoc-macros: $(src)/macros/lib.rs FORCE 96ecab4115SMiguel Ojeda +$(call if_changed,rustdoc) 972f7ab126SMiguel Ojeda 98bef83245SMiguel Ojeda# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should 99bef83245SMiguel Ojeda# not be needed -- see https://github.com/rust-lang/rust/pull/128307. 100bef83245SMiguel Ojedarustdoc-core: private skip_flags = -Wrustdoc::unescaped_backticks 1012f7ab126SMiguel Ojedarustdoc-core: private rustc_target_flags = $(core-cfgs) 1022f7ab126SMiguel Ojedarustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE 103ecab4115SMiguel Ojeda +$(call if_changed,rustdoc) 1042f7ab126SMiguel Ojeda 1052f7ab126SMiguel Ojedarustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE 106ecab4115SMiguel Ojeda +$(call if_changed,rustdoc) 1072f7ab126SMiguel Ojeda 108d072acdaSGary Guorustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE 109ecab4115SMiguel Ojeda +$(call if_changed,rustdoc) 1102f7ab126SMiguel Ojeda 111d072acdaSGary Guorustdoc-kernel: private rustc_target_flags = --extern ffi \ 112ecaa6ddfSGary Guo --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \ 1134e174665SAsahi Lina --extern bindings --extern uapi 114d072acdaSGary Guorustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \ 115392e34b6SDanilo Krummrich rustdoc-compiler_builtins $(obj)/libmacros.so \ 1162f7ab126SMiguel Ojeda $(obj)/bindings.o FORCE 117ecab4115SMiguel Ojeda +$(call if_changed,rustdoc) 1182f7ab126SMiguel Ojeda 1192f7ab126SMiguel Ojedaquiet_cmd_rustc_test_library = RUSTC TL $< 1202f7ab126SMiguel Ojeda cmd_rustc_test_library = \ 1212f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 1222f7ab126SMiguel Ojeda $(RUSTC) $(rust_common_flags) \ 1232f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \ 1242f7ab126SMiguel Ojeda --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \ 1252f7ab126SMiguel Ojeda --out-dir $(objtree)/$(obj)/test --cfg testlib \ 1262f7ab126SMiguel Ojeda -L$(objtree)/$(obj)/test \ 1272f7ab126SMiguel Ojeda --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $< 1282f7ab126SMiguel Ojeda 1299ffc80c8SMiguel Ojedarusttestlib-build_error: $(src)/build_error.rs FORCE 130ecab4115SMiguel Ojeda +$(call if_changed,rustc_test_library) 131ecaa6ddfSGary Guo 132d072acdaSGary Guorusttestlib-ffi: $(src)/ffi.rs FORCE 133d072acdaSGary Guo +$(call if_changed,rustc_test_library) 134d072acdaSGary Guo 1352f7ab126SMiguel Ojedarusttestlib-macros: private rustc_target_flags = --extern proc_macro 1362f7ab126SMiguel Ojedarusttestlib-macros: private rustc_test_library_proc = yes 1379ffc80c8SMiguel Ojedarusttestlib-macros: $(src)/macros/lib.rs FORCE 138ecab4115SMiguel Ojeda +$(call if_changed,rustc_test_library) 1392f7ab126SMiguel Ojeda 140d072acdaSGary Guorusttestlib-kernel: private rustc_target_flags = --extern ffi \ 141b2c261faSEthan D. Twardy --extern build_error --extern macros \ 142b2c261faSEthan D. Twardy --extern bindings --extern uapi 143b2c261faSEthan D. Twardyrusttestlib-kernel: $(src)/kernel/lib.rs \ 144b2c261faSEthan D. Twardy rusttestlib-bindings rusttestlib-uapi rusttestlib-build_error \ 145b2c261faSEthan D. Twardy $(obj)/libmacros.so $(obj)/bindings.o FORCE 146ecab4115SMiguel Ojeda +$(call if_changed,rustc_test_library) 1472f7ab126SMiguel Ojeda 148d072acdaSGary Guorusttestlib-bindings: private rustc_target_flags = --extern ffi 149d072acdaSGary Guorusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi FORCE 1502f7ab126SMiguel Ojeda +$(call if_changed,rustc_test_library) 1512f7ab126SMiguel Ojeda 152d072acdaSGary Guorusttestlib-uapi: private rustc_target_flags = --extern ffi 153d072acdaSGary Guorusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi FORCE 154ecab4115SMiguel Ojeda +$(call if_changed,rustc_test_library) 1554e174665SAsahi Lina 1562f7ab126SMiguel Ojedaquiet_cmd_rustdoc_test = RUSTDOC T $< 1572f7ab126SMiguel Ojeda cmd_rustdoc_test = \ 1588d3f5079SEthan D. Twardy RUST_MODFILE=test.rs \ 1592f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 1602f7ab126SMiguel Ojeda $(RUSTDOC) --test $(rust_common_flags) \ 1612f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg \ 1622f7ab126SMiguel Ojeda $(rustc_target_flags) $(rustdoc_test_target_flags) \ 1639ffc80c8SMiguel Ojeda $(rustdoc_test_quiet) \ 16448fadf44SCarlos Bilbao -L$(objtree)/$(obj)/test --output $(rustdoc_output) \ 1652f7ab126SMiguel Ojeda --crate-name $(subst rusttest-,,$@) $< 1662f7ab126SMiguel Ojeda 167a66d733dSMiguel Ojedaquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $< 168a66d733dSMiguel Ojeda cmd_rustdoc_test_kernel = \ 169a66d733dSMiguel Ojeda rm -rf $(objtree)/$(obj)/test/doctests/kernel; \ 170a66d733dSMiguel Ojeda mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \ 171a66d733dSMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 172a66d733dSMiguel Ojeda $(RUSTDOC) --test $(rust_flags) \ 173d072acdaSGary Guo -L$(objtree)/$(obj) --extern ffi --extern kernel \ 174a66d733dSMiguel Ojeda --extern build_error --extern macros \ 175a66d733dSMiguel Ojeda --extern bindings --extern uapi \ 176a66d733dSMiguel Ojeda --no-run --crate-name kernel -Zunstable-options \ 17771479eeeSMatthew Maurer --sysroot=/dev/null \ 178a66d733dSMiguel Ojeda --test-builder $(objtree)/scripts/rustdoc_test_builder \ 179a66d733dSMiguel Ojeda $< $(rustdoc_test_kernel_quiet); \ 180a66d733dSMiguel Ojeda $(objtree)/scripts/rustdoc_test_gen 181a66d733dSMiguel Ojeda 182a66d733dSMiguel Ojeda%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \ 183a66d733dSMiguel Ojeda $(src)/kernel/lib.rs $(obj)/kernel.o \ 184a66d733dSMiguel Ojeda $(objtree)/scripts/rustdoc_test_builder \ 185a66d733dSMiguel Ojeda $(objtree)/scripts/rustdoc_test_gen FORCE 186ecab4115SMiguel Ojeda +$(call if_changed,rustdoc_test_kernel) 187a66d733dSMiguel Ojeda 1882f7ab126SMiguel Ojeda# We cannot use `-Zpanic-abort-tests` because some tests are dynamic, 1892f7ab126SMiguel Ojeda# so for the moment we skip `-Cpanic=abort`. 1902f7ab126SMiguel Ojedaquiet_cmd_rustc_test = RUSTC T $< 1912f7ab126SMiguel Ojeda cmd_rustc_test = \ 1922f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 1932f7ab126SMiguel Ojeda $(RUSTC) --test $(rust_common_flags) \ 1942f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg \ 1952f7ab126SMiguel Ojeda $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \ 1962f7ab126SMiguel Ojeda -L$(objtree)/$(obj)/test \ 1972f7ab126SMiguel Ojeda --crate-name $(subst rusttest-,,$@) $<; \ 1982f7ab126SMiguel Ojeda $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \ 1992f7ab126SMiguel Ojeda $(rustc_test_run_flags) 2002f7ab126SMiguel Ojeda 2012f7ab126SMiguel Ojedarusttest: rusttest-macros rusttest-kernel 2022f7ab126SMiguel Ojeda 203b2c261faSEthan D. Twardyrusttest-macros: private rustc_target_flags = --extern proc_macro \ 204b2c261faSEthan D. Twardy --extern macros --extern kernel 2052f7ab126SMiguel Ojedarusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro 206b2c261faSEthan D. Twardyrusttest-macros: $(src)/macros/lib.rs \ 207b2c261faSEthan D. Twardy rusttestlib-macros rusttestlib-kernel FORCE 208ecab4115SMiguel Ojeda +$(call if_changed,rustc_test) 209ecab4115SMiguel Ojeda +$(call if_changed,rustdoc_test) 2102f7ab126SMiguel Ojeda 211d072acdaSGary Guorusttest-kernel: private rustc_target_flags = --extern ffi \ 2124e174665SAsahi Lina --extern build_error --extern macros --extern bindings --extern uapi 213d072acdaSGary Guorusttest-kernel: $(src)/kernel/lib.rs rusttestlib-ffi rusttestlib-kernel \ 2144e174665SAsahi Lina rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \ 2154e174665SAsahi Lina rusttestlib-uapi FORCE 216ecab4115SMiguel Ojeda +$(call if_changed,rustc_test) 2172f7ab126SMiguel Ojeda 2182f7ab126SMiguel Ojedaifdef CONFIG_CC_IS_CLANG 2192f7ab126SMiguel Ojedabindgen_c_flags = $(c_flags) 2202f7ab126SMiguel Ojedaelse 2212f7ab126SMiguel Ojeda# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC 2222f7ab126SMiguel Ojeda# plugin backend and/or the Clang driver would be perfectly compatible with GCC. 2232f7ab126SMiguel Ojeda# 2242f7ab126SMiguel Ojeda# For the moment, here we are tweaking the flags on the fly. This is a hack, 2252f7ab126SMiguel Ojeda# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT` 2262f7ab126SMiguel Ojeda# if we end up using one of those structs). 2272f7ab126SMiguel Ojedabindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ 2282f7ab126SMiguel Ojeda -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \ 2292f7ab126SMiguel Ojeda -mindirect-branch=thunk-extern -mindirect-branch-register \ 2302f7ab126SMiguel Ojeda -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \ 2312f7ab126SMiguel Ojeda -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \ 2322f7ab126SMiguel Ojeda -mno-pointers-to-nested-functions -mno-string \ 2332f7ab126SMiguel Ojeda -mno-strict-align -mstrict-align \ 2342f7ab126SMiguel Ojeda -fconserve-stack -falign-jumps=% -falign-loops=% \ 2352f7ab126SMiguel Ojeda -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \ 2362f7ab126SMiguel Ojeda -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \ 2372f7ab126SMiguel Ojeda -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \ 2382f7ab126SMiguel Ojeda -fzero-call-used-regs=% -fno-stack-clash-protection \ 239b0554488SAndrea Righi -fno-inline-functions-called-once -fsanitize=bounds-strict \ 240869b5016SZehui Xu -fstrict-flex-arrays=% -fmin-function-alignment=% \ 2412f7ab126SMiguel Ojeda --param=% --param asan-% 2422f7ab126SMiguel Ojeda 2432f7ab126SMiguel Ojeda# Derived from `scripts/Makefile.clang`. 2442f7ab126SMiguel OjedaBINDGEN_TARGET_x86 := x86_64-linux-gnu 245724a75acSJamie CunliffeBINDGEN_TARGET_arm64 := aarch64-linux-gnu 2462f7ab126SMiguel OjedaBINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) 2472f7ab126SMiguel Ojeda 2482f7ab126SMiguel Ojeda# All warnings are inhibited since GCC builds are very experimental, 2492f7ab126SMiguel Ojeda# many GCC warnings are not supported by Clang, they may only appear in 2502f7ab126SMiguel Ojeda# some configurations, with new GCC versions, etc. 2512f7ab126SMiguel Ojedabindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) 2522f7ab126SMiguel Ojeda 253d966c3caSAndrea Righi# Auto variable zero-initialization requires an additional special option with 254d966c3caSAndrea Righi# clang that is going to be removed sometime in the future (likely in 255d966c3caSAndrea Righi# clang-18), so make sure to pass this option only if clang supports it 256d966c3caSAndrea Righi# (libclang major version < 16). 257d966c3caSAndrea Righi# 258d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/issues/44842 259d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags 260d966c3caSAndrea Righiifdef CONFIG_INIT_STACK_ALL_ZERO 261d966c3caSAndrea Righilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p') 262d966c3caSAndrea Righiifeq ($(shell expr $(libclang_maj_ver) \< 16), 1) 263d966c3caSAndrea Righibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 264d966c3caSAndrea Righiendif 265d966c3caSAndrea Righiendif 266d966c3caSAndrea Righi 2672f7ab126SMiguel Ojedabindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \ 2682f7ab126SMiguel Ojeda $(bindgen_extra_c_flags) 2692f7ab126SMiguel Ojedaendif 2702f7ab126SMiguel Ojeda 2712f7ab126SMiguel Ojedaifdef CONFIG_LTO 2722f7ab126SMiguel Ojedabindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags)) 2732f7ab126SMiguel Ojedaelse 2742f7ab126SMiguel Ojedabindgen_c_flags_lto = $(bindgen_c_flags) 2752f7ab126SMiguel Ojedaendif 2762f7ab126SMiguel Ojeda 27775c1fd41SGary Guo# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin 27875c1fd41SGary Guo# prototypes for functions like `memcpy` -- if this flag is not passed, 27975c1fd41SGary Guo# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on 28075c1fd41SGary Guo# architecture instead of generating `usize`. 28175c1fd41SGary Guobindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__ 2822f7ab126SMiguel Ojeda 2837a5f93eaSMiguel Ojeda# Each `bindgen` release may upgrade the list of Rust target versions. By 2847a5f93eaSMiguel Ojeda# default, the highest stable release in their list is used. Thus we need to set 2857a5f93eaSMiguel Ojeda# a `--rust-target` to avoid future `bindgen` releases emitting code that 2867a5f93eaSMiguel Ojeda# `rustc` may not understand. On top of that, `bindgen` does not support passing 2877a5f93eaSMiguel Ojeda# an unknown Rust target version. 2887a5f93eaSMiguel Ojeda# 2897a5f93eaSMiguel Ojeda# Therefore, the Rust target for `bindgen` can be only as high as the minimum 2907a5f93eaSMiguel Ojeda# Rust version the kernel supports and only as high as the greatest stable Rust 2917a5f93eaSMiguel Ojeda# target supported by the minimum `bindgen` version the kernel supports (that 2927a5f93eaSMiguel Ojeda# is, if we do not test the actual `rustc`/`bindgen` versions running). 2937a5f93eaSMiguel Ojeda# 2947a5f93eaSMiguel Ojeda# Starting with `bindgen` 0.71.0, we will be able to set any future Rust version 2957a5f93eaSMiguel Ojeda# instead, i.e. we will be able to set here our minimum supported Rust version. 2962f7ab126SMiguel Ojedaquiet_cmd_bindgen = BINDGEN $@ 2972f7ab126SMiguel Ojeda cmd_bindgen = \ 2987a5f93eaSMiguel Ojeda $(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \ 299d072acdaSGary Guo --use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \ 30076501d19SMiguel Ojeda --no-debug '.*' --enable-function-attribute-detection \ 30108ab7865SAakash Sen Sharma -o $@ -- $(bindgen_c_flags_final) -DMODULE \ 3022f7ab126SMiguel Ojeda $(bindgen_target_cflags) $(bindgen_target_extra) 3032f7ab126SMiguel Ojeda 3042f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ 305b1992c37SMasahiro Yamada $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters) 30674376656SGary Guo$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \ 30774376656SGary Guo sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@ 3082f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ 3092f7ab126SMiguel Ojeda $(src)/bindgen_parameters FORCE 3102f7ab126SMiguel Ojeda $(call if_changed_dep,bindgen) 3112f7ab126SMiguel Ojeda 3124e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \ 313b1992c37SMasahiro Yamada $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters) 3144e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \ 3154e174665SAsahi Lina $(src)/bindgen_parameters FORCE 3164e174665SAsahi Lina $(call if_changed_dep,bindgen) 3174e174665SAsahi Lina 3182f7ab126SMiguel Ojeda# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn 3192f7ab126SMiguel Ojeda# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here 3202f7ab126SMiguel Ojeda# given it is `libclang`; but for consistency, future Clang changes and/or 3212f7ab126SMiguel Ojeda# a potential future GCC backend for `bindgen`, we disable it too. 3222f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \ 32308ab7865SAakash Sen Sharma --blocklist-type '.*' --allowlist-var '' \ 32408ab7865SAakash Sen Sharma --allowlist-function 'rust_helper_.*' 3252f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \ 3262f7ab126SMiguel Ojeda -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations 3272f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \ 3282f7ab126SMiguel Ojeda sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@ 32987634653SAndreas Hindborg$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE 3302f7ab126SMiguel Ojeda $(call if_changed_dep,bindgen) 3312f7ab126SMiguel Ojeda 332*ac61506bSSami Tolvanenrust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ { printf $(2),$$3 }' 333*ac61506bSSami Tolvanen 3342f7ab126SMiguel Ojedaquiet_cmd_exports = EXPORTS $@ 3352f7ab126SMiguel Ojeda cmd_exports = \ 336*ac61506bSSami Tolvanen $(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@ 3372f7ab126SMiguel Ojeda 3382f7ab126SMiguel Ojeda$(obj)/exports_core_generated.h: $(obj)/core.o FORCE 3392f7ab126SMiguel Ojeda $(call if_changed,exports) 3402f7ab126SMiguel Ojeda 341e26fa546SGary Guo# Even though Rust kernel modules should never use the bindings directly, 342e26fa546SGary Guo# symbols from the `bindings` crate and the C helpers need to be exported 343e26fa546SGary Guo# because Rust generics and inlined functions may not get their code generated 344e26fa546SGary Guo# in the crate where they are defined. Other helpers, called from non-inline 345e26fa546SGary Guo# functions, may not be exported, in principle. However, in general, the Rust 346e26fa546SGary Guo# compiler does not guarantee codegen will be performed for a non-inline 347e26fa546SGary Guo# function either. Therefore, we export all symbols from helpers and bindings. 348e26fa546SGary Guo# In the future, this may be revisited to reduce the number of exports after 349e26fa546SGary Guo# the compiler is informed about the places codegen is required. 350e26fa546SGary Guo$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE 351e26fa546SGary Guo $(call if_changed,exports) 352e26fa546SGary Guo 3532f7ab126SMiguel Ojeda$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE 3542f7ab126SMiguel Ojeda $(call if_changed,exports) 3552f7ab126SMiguel Ojeda 3562f7ab126SMiguel Ojeda$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE 3572f7ab126SMiguel Ojeda $(call if_changed,exports) 3582f7ab126SMiguel Ojeda 3592f7ab126SMiguel Ojedaquiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@ 3602f7ab126SMiguel Ojeda cmd_rustc_procmacro = \ 3612f7ab126SMiguel Ojeda $(RUSTC_OR_CLIPPY) $(rust_common_flags) \ 36280bac83aSMatthew Maurer -Clinker-flavor=gcc -Clinker=$(HOSTCC) \ 36380bac83aSMatthew Maurer -Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \ 364295d8398SMasahiro Yamada --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \ 365295d8398SMasahiro Yamada --crate-type proc-macro \ 3662185242fSMasahiro Yamada --crate-name $(patsubst lib%.so,%,$(notdir $@)) $< 3672f7ab126SMiguel Ojeda 3682f7ab126SMiguel Ojeda# Procedural macros can only be used with the `rustc` that compiled it. 369aeb0e24aSMiguel Ojeda$(obj)/libmacros.so: $(src)/macros/lib.rs FORCE 370ecab4115SMiguel Ojeda +$(call if_changed_dep,rustc_procmacro) 3712f7ab126SMiguel Ojeda 3722f7ab126SMiguel Ojedaquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@ 3732f7ab126SMiguel Ojeda cmd_rustc_library = \ 3742f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 3752f7ab126SMiguel Ojeda $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ 3762f7ab126SMiguel Ojeda $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \ 377295d8398SMasahiro Yamada --emit=dep-info=$(depfile) --emit=obj=$@ \ 378295d8398SMasahiro Yamada --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \ 379295d8398SMasahiro Yamada --crate-type rlib -L$(objtree)/$(obj) \ 3802185242fSMasahiro Yamada --crate-name $(patsubst %.o,%,$(notdir $@)) $< \ 38171479eeeSMatthew Maurer --sysroot=/dev/null \ 382c4d7f546SMiguel Ojeda $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \ 383c4d7f546SMiguel Ojeda $(cmd_objtool) 3842f7ab126SMiguel Ojeda 3852f7ab126SMiguel Ojedarust-analyzer: 38649a9ef76SVinay Varma $(Q)$(srctree)/scripts/generate_rust_analyzer.py \ 387392e34b6SDanilo Krummrich --cfgs='core=$(core-cfgs)' \ 388e2bad142SMasahiro Yamada $(realpath $(srctree)) $(realpath $(objtree)) \ 38913b25489SMasahiro Yamada $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \ 39013b25489SMasahiro Yamada > rust-project.json 3912f7ab126SMiguel Ojeda 392cb7d9defSGary Guoredirect-intrinsics = \ 39302dfd63aSMiguel Ojeda __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \ 39402dfd63aSMiguel Ojeda __adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \ 395cb7d9defSGary Guo __muloti4 __multi3 \ 396cb7d9defSGary Guo __udivmodti4 __udivti3 __umodti3 397cb7d9defSGary Guo 398cb7d9defSGary Guoifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),) 399cb7d9defSGary Guo # These intrinsics are defined for ARM64 and RISCV64 400cb7d9defSGary Guo redirect-intrinsics += \ 401cb7d9defSGary Guo __ashrti3 \ 402cb7d9defSGary Guo __ashlti3 __lshrti3 403cb7d9defSGary Guoendif 404cb7d9defSGary Guo 405*ac61506bSSami Tolvanenifdef CONFIG_MODVERSIONS 406*ac61506bSSami Tolvanencmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \ 407*ac61506bSSami Tolvanen $(call rust_exports,$@,"%s\n") | \ 408*ac61506bSSami Tolvanen scripts/gendwarfksyms/gendwarfksyms \ 409*ac61506bSSami Tolvanen $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \ 410*ac61506bSSami Tolvanen $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \ 411*ac61506bSSami Tolvanen $@ >> $(dot-target).cmd) 412*ac61506bSSami Tolvanenendif 413*ac61506bSSami Tolvanen 414c4d7f546SMiguel Ojedadefine rule_rustc_library 415c4d7f546SMiguel Ojeda $(call cmd_and_fixdep,rustc_library) 416c4d7f546SMiguel Ojeda $(call cmd,gen_objtooldep) 417*ac61506bSSami Tolvanen $(call cmd,gendwarfksyms) 418c4d7f546SMiguel Ojedaendef 419c4d7f546SMiguel Ojeda 420*ac61506bSSami Tolvanendefine rule_rust_cc_library 421*ac61506bSSami Tolvanen $(call if_changed_rule,cc_o_c) 422*ac61506bSSami Tolvanen $(call cmd,force_checksrc) 423*ac61506bSSami Tolvanen $(call cmd,gendwarfksyms) 424*ac61506bSSami Tolvanenendef 425*ac61506bSSami Tolvanen 426*ac61506bSSami Tolvanen# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol 427*ac61506bSSami Tolvanen# versions are calculated for the helpers too. 428*ac61506bSSami Tolvanen$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE 429*ac61506bSSami Tolvanen +$(call if_changed_rule,rust_cc_library) 430*ac61506bSSami Tolvanen 431*ac61506bSSami Tolvanen# Disable symbol versioning for exports.o to avoid conflicts with the actual 432*ac61506bSSami Tolvanen# symbol versions generated from Rust objects. 433*ac61506bSSami Tolvanen$(obj)/exports.o: private skip_gendwarfksyms = 1 434*ac61506bSSami Tolvanen 4352f7ab126SMiguel Ojeda$(obj)/core.o: private skip_clippy = 1 436f8f88aa2SMiguel Ojeda$(obj)/core.o: private skip_flags = -Wunreachable_pub 437cb7d9defSGary Guo$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym)) 4382f7ab126SMiguel Ojeda$(obj)/core.o: private rustc_target_flags = $(core-cfgs) 439ac3e9726SMiguel Ojeda$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ 440ac3e9726SMiguel Ojeda $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE 441c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_library) 442ab0f4cedSDavid Gowifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) 443f82811e2SJamie Cunliffe$(obj)/core.o: scripts/target.json 444f82811e2SJamie Cunliffeendif 4452f7ab126SMiguel Ojeda 446*ac61506bSSami Tolvanen$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 4472f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' 4482f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE 449c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_library) 4502f7ab126SMiguel Ojeda 451*ac61506bSSami Tolvanen$(obj)/build_error.o: private skip_gendwarfksyms = 1 452ecaa6ddfSGary Guo$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE 453c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_library) 454ecaa6ddfSGary Guo 455*ac61506bSSami Tolvanen$(obj)/ffi.o: private skip_gendwarfksyms = 1 456d072acdaSGary Guo$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE 457d072acdaSGary Guo +$(call if_changed_rule,rustc_library) 458d072acdaSGary Guo 459d072acdaSGary Guo$(obj)/bindings.o: private rustc_target_flags = --extern ffi 4602f7ab126SMiguel Ojeda$(obj)/bindings.o: $(src)/bindings/lib.rs \ 461d072acdaSGary Guo $(obj)/ffi.o \ 4622f7ab126SMiguel Ojeda $(obj)/bindings/bindings_generated.rs \ 4632f7ab126SMiguel Ojeda $(obj)/bindings/bindings_helpers_generated.rs FORCE 464c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_library) 4652f7ab126SMiguel Ojeda 466d072acdaSGary Guo$(obj)/uapi.o: private rustc_target_flags = --extern ffi 467*ac61506bSSami Tolvanen$(obj)/uapi.o: private skip_gendwarfksyms = 1 4684e174665SAsahi Lina$(obj)/uapi.o: $(src)/uapi/lib.rs \ 469d072acdaSGary Guo $(obj)/ffi.o \ 4704e174665SAsahi Lina $(obj)/uapi/uapi_generated.rs FORCE 471c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_library) 4724e174665SAsahi Lina 473d072acdaSGary Guo$(obj)/kernel.o: private rustc_target_flags = --extern ffi \ 4744e174665SAsahi Lina --extern build_error --extern macros --extern bindings --extern uapi 475392e34b6SDanilo Krummrich$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \ 4764e174665SAsahi Lina $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE 477c4d7f546SMiguel Ojeda +$(call if_changed_rule,rustc_library) 4782f7ab126SMiguel Ojeda 479169484abSAlice Ryhlifdef CONFIG_JUMP_LABEL 4808af7a501SMiguel Ojeda$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs 481169484abSAlice Ryhlendif 482169484abSAlice Ryhl 4832f7ab126SMiguel Ojedaendif # CONFIG_RUST 484