1# SPDX-License-Identifier: GPL-2.0 2 3# Where to place rustdoc generated documentation 4rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc 5 6obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o 7always-$(CONFIG_RUST) += exports_core_generated.h 8 9# Missing prototypes are expected in the helpers since these are exported 10# for Rust only, thus there is no header nor prototypes. 11obj-$(CONFIG_RUST) += helpers/helpers.o 12CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations 13 14always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs 15obj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o 16always-$(CONFIG_RUST) += exports_helpers_generated.h \ 17 exports_bindings_generated.h exports_kernel_generated.h 18 19always-$(CONFIG_RUST) += uapi/uapi_generated.rs 20obj-$(CONFIG_RUST) += uapi.o 21 22ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW 23obj-$(CONFIG_RUST) += build_error.o 24else 25always-$(CONFIG_RUST) += build_error.o 26endif 27 28obj-$(CONFIG_RUST) += exports.o 29 30always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs 31always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c 32 33obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o 34obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o 35 36always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs 37ifndef CONFIG_UML 38always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs 39endif 40 41# Avoids running `$(RUSTC)` when it may not be available. 42ifdef CONFIG_RUST 43 44libmacros_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name macros --crate-type proc-macro - </dev/null) 45libmacros_extension := $(patsubst libmacros.%,%,$(libmacros_name)) 46 47libpin_init_internal_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name pin_init_internal --crate-type proc-macro - </dev/null) 48libpin_init_internal_extension := $(patsubst libpin_init_internal.%,%,$(libpin_init_internal_name)) 49 50always-$(CONFIG_RUST) += $(libmacros_name) $(libpin_init_internal_name) 51 52# `$(rust_flags)` is passed in case the user added `--sysroot`. 53rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot) 54rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2) 55RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library 56 57ifneq ($(quiet),) 58rust_test_quiet=-q 59rustdoc_test_quiet=--test-args -q 60rustdoc_test_kernel_quiet=>/dev/null 61endif 62 63core-cfgs = \ 64 --cfg no_fp_fmt_parse 65 66core-edition := $(if $(call rustc-min-version,108700),2024,2021) 67 68# `rustdoc` did not save the target modifiers, thus workaround for 69# the time being (https://github.com/rust-lang/rust/issues/144521). 70rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18) 71 72# Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465). 73doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer) 74 75# `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only 76# since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust 77# 1.82.0 (https://github.com/rust-lang/rust/issues/138520). Thus workaround both 78# issues skipping the flag. The former also applies to `RUSTDOC TK`. 79quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $< 80 cmd_rustdoc = \ 81 OBJTREE=$(abspath $(objtree)) \ 82 $(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \ 83 $(rustc_target_flags) -L$(objtree)/$(obj) \ 84 -Zunstable-options --generate-link-to-definition \ 85 --output $(rustdoc_output) \ 86 --crate-name $(subst rustdoc-,,$@) \ 87 $(rustdoc_modifiers_workaround) \ 88 $(if $(rustdoc_host),,--sysroot=/dev/null) \ 89 @$(objtree)/include/generated/rustc_cfg $< 90 91# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute 92# can be used to specify a custom logo. However: 93# - The given value is used as-is, thus it cannot be relative or a local file 94# (unlike the non-custom case) since the generated docs have subfolders. 95# - It requires adding it to every crate. 96# - It requires changing `core` which comes from the sysroot. 97# 98# Using `-Zcrate-attr` would solve the last two points, but not the first. 99# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new 100# command-like flags to solve the issue. Meanwhile, we use the non-custom case 101# and then retouch the generated files. 102rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \ 103 rustdoc-kernel rustdoc-pin_init 104 $(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \ 105 cut -d'"' -f2 | cut -d/ -f2- | while read f; do \ 106 if [ ! -e "$(srctree)/$$f" ]; then \ 107 echo "warning: srctree/ link to $$f does not exist"; \ 108 fi \ 109 done 110 $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/ 111 $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/ 112 $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \ 113 -e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \ 114 -e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \ 115 -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \ 116 -e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g' 117 $(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \ 118 echo ".logo-container > img { object-fit: contain; }" >> $$f; done 119 120rustdoc-macros: private rustdoc_host = yes 121rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \ 122 --extern proc_macro 123rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean FORCE 124 +$(call if_changed,rustdoc) 125 126# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should 127# not be needed -- see https://github.com/rust-lang/rust/pull/128307. 128rustdoc-core: private skip_flags = --edition=2021 -Wrustdoc::unescaped_backticks 129rustdoc-core: private rustc_target_flags = --edition=$(core-edition) $(core-cfgs) 130rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE 131 +$(call if_changed,rustdoc) 132 133# Even if `rustdoc` targets are not kernel objects, they should still be 134# treated as such so that we pass the same flags. Otherwise, for instance, 135# `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch. 136rustdoc-compiler_builtins: private is-kernel-object := y 137rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE 138 +$(call if_changed,rustdoc) 139 140rustdoc-ffi: private is-kernel-object := y 141rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE 142 +$(call if_changed,rustdoc) 143 144rustdoc-pin_init_internal: private rustdoc_host = yes 145rustdoc-pin_init_internal: private rustc_target_flags = --cfg kernel \ 146 --extern proc_macro --crate-type proc-macro 147rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \ 148 rustdoc-clean FORCE 149 +$(call if_changed,rustdoc) 150 151rustdoc-pin_init: private rustdoc_host = yes 152rustdoc-pin_init: private rustc_target_flags = --extern pin_init_internal \ 153 --extern macros --extern alloc --cfg kernel --cfg feature=\"alloc\" 154rustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \ 155 rustdoc-macros FORCE 156 +$(call if_changed,rustdoc) 157 158rustdoc-kernel: private is-kernel-object := y 159rustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \ 160 --extern build_error --extern macros \ 161 --extern bindings --extern uapi 162rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \ 163 rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \ 164 $(obj)/bindings.o FORCE 165 +$(call if_changed,rustdoc) 166 167rustdoc-clean: FORCE 168 $(Q)rm -rf $(rustdoc_output) 169 170quiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $< 171 cmd_rustc_test_library = \ 172 OBJTREE=$(abspath $(objtree)) \ 173 $(RUSTC_OR_CLIPPY) $(rust_common_flags) \ 174 @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \ 175 --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \ 176 --out-dir $(objtree)/$(obj)/test --cfg testlib \ 177 -L$(objtree)/$(obj)/test \ 178 --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $< 179 180rusttestlib-build_error: $(src)/build_error.rs FORCE 181 +$(call if_changed,rustc_test_library) 182 183rusttestlib-ffi: $(src)/ffi.rs FORCE 184 +$(call if_changed,rustc_test_library) 185 186rusttestlib-macros: private rustc_target_flags = --extern proc_macro 187rusttestlib-macros: private rustc_test_library_proc = yes 188rusttestlib-macros: $(src)/macros/lib.rs FORCE 189 +$(call if_changed,rustc_test_library) 190 191rusttestlib-pin_init_internal: private rustc_target_flags = --cfg kernel \ 192 --extern proc_macro 193rusttestlib-pin_init_internal: private rustc_test_library_proc = yes 194rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs FORCE 195 +$(call if_changed,rustc_test_library) 196 197rusttestlib-pin_init: private rustc_target_flags = --extern pin_init_internal \ 198 --extern macros --cfg kernel 199rusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \ 200 rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE 201 +$(call if_changed,rustc_test_library) 202 203rusttestlib-kernel: private rustc_target_flags = --extern ffi \ 204 --extern build_error --extern macros --extern pin_init \ 205 --extern bindings --extern uapi 206rusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \ 207 rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \ 208 $(obj)/bindings.o FORCE 209 +$(call if_changed,rustc_test_library) 210 211rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init 212rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE 213 +$(call if_changed,rustc_test_library) 214 215rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init 216rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE 217 +$(call if_changed,rustc_test_library) 218 219quiet_cmd_rustdoc_test = RUSTDOC T $< 220 cmd_rustdoc_test = \ 221 RUST_MODFILE=test.rs \ 222 OBJTREE=$(abspath $(objtree)) \ 223 $(RUSTDOC) --test $(rust_common_flags) \ 224 -Zcrate-attr='feature(used_with_arg)' \ 225 @$(objtree)/include/generated/rustc_cfg \ 226 $(rustc_target_flags) $(rustdoc_test_target_flags) \ 227 $(rustdoc_test_quiet) \ 228 -L$(objtree)/$(obj)/test --output $(rustdoc_output) \ 229 --crate-name $(subst rusttest-,,$@) $< 230 231quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $< 232 cmd_rustdoc_test_kernel = \ 233 rm -rf $(objtree)/$(obj)/test/doctests/kernel; \ 234 mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \ 235 OBJTREE=$(abspath $(objtree)) \ 236 $(RUSTDOC) --test $(filter-out --remap-path-prefix=%,$(rust_flags)) \ 237 -L$(objtree)/$(obj) --extern ffi --extern pin_init \ 238 --extern kernel --extern build_error --extern macros \ 239 --extern bindings --extern uapi \ 240 --no-run --crate-name kernel -Zunstable-options \ 241 --sysroot=/dev/null \ 242 $(doctests_modifiers_workaround) \ 243 --test-builder $(objtree)/scripts/rustdoc_test_builder \ 244 $< $(rustdoc_test_kernel_quiet); \ 245 $(objtree)/scripts/rustdoc_test_gen 246 247%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \ 248 $(src)/kernel/lib.rs $(obj)/kernel.o \ 249 $(objtree)/scripts/rustdoc_test_builder \ 250 $(objtree)/scripts/rustdoc_test_gen FORCE 251 +$(call if_changed,rustdoc_test_kernel) 252 253# We cannot use `-Zpanic-abort-tests` because some tests are dynamic, 254# so for the moment we skip `-Cpanic=abort`. 255quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T $< 256 cmd_rustc_test = \ 257 OBJTREE=$(abspath $(objtree)) \ 258 $(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \ 259 @$(objtree)/include/generated/rustc_cfg \ 260 $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \ 261 -L$(objtree)/$(obj)/test \ 262 --crate-name $(subst rusttest-,,$@) $<; \ 263 $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \ 264 $(rustc_test_run_flags) 265 266rusttest: rusttest-macros 267 268rusttest-macros: private rustc_target_flags = --extern proc_macro \ 269 --extern macros --extern kernel --extern pin_init 270rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro 271rusttest-macros: $(src)/macros/lib.rs \ 272 rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE 273 +$(call if_changed,rustc_test) 274 +$(call if_changed,rustdoc_test) 275 276ifdef CONFIG_CC_IS_CLANG 277bindgen_c_flags = $(c_flags) 278else 279# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC 280# plugin backend and/or the Clang driver would be perfectly compatible with GCC. 281# 282# For the moment, here we are tweaking the flags on the fly. This is a hack, 283# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT` 284# if we end up using one of those structs). 285bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ 286 -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \ 287 -mindirect-branch=thunk-extern -mindirect-branch-register \ 288 -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \ 289 -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \ 290 -mno-pointers-to-nested-functions -mno-string \ 291 -mno-strict-align -mstrict-align -mdirect-extern-access \ 292 -mexplicit-relocs -mno-check-zero-division \ 293 -fconserve-stack -falign-jumps=% -falign-loops=% \ 294 -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \ 295 -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \ 296 -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \ 297 -fzero-call-used-regs=% -fno-stack-clash-protection \ 298 -fno-inline-functions-called-once -fsanitize=bounds-strict \ 299 -fstrict-flex-arrays=% -fmin-function-alignment=% \ 300 -fzero-init-padding-bits=% -mno-fdpic \ 301 --param=% --param asan-% 302 303# Derived from `scripts/Makefile.clang`. 304BINDGEN_TARGET_x86 := x86_64-linux-gnu 305BINDGEN_TARGET_arm64 := aarch64-linux-gnu 306BINDGEN_TARGET_arm := arm-linux-gnueabi 307BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf 308BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH)) 309BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) 310 311# All warnings are inhibited since GCC builds are very experimental, 312# many GCC warnings are not supported by Clang, they may only appear in 313# some configurations, with new GCC versions, etc. 314bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) 315 316# Auto variable zero-initialization requires an additional special option with 317# clang that is going to be removed sometime in the future (likely in 318# clang-18), so make sure to pass this option only if clang supports it 319# (libclang major version < 16). 320# 321# https://github.com/llvm/llvm-project/issues/44842 322# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags 323ifdef CONFIG_INIT_STACK_ALL_ZERO 324libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p') 325ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1) 326bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 327endif 328endif 329 330bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \ 331 $(bindgen_extra_c_flags) 332endif 333 334ifdef CONFIG_LTO 335bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags)) 336else 337bindgen_c_flags_lto = $(bindgen_c_flags) 338endif 339 340# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin 341# prototypes for functions like `memcpy` -- if this flag is not passed, 342# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on 343# architecture instead of generating `usize`. 344bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__ 345 346# Each `bindgen` release may upgrade the list of Rust target versions. By 347# default, the highest stable release in their list is used. Thus we need to set 348# a `--rust-target` to avoid future `bindgen` releases emitting code that 349# `rustc` may not understand. On top of that, `bindgen` does not support passing 350# an unknown Rust target version. 351# 352# Therefore, the Rust target for `bindgen` can be only as high as the minimum 353# Rust version the kernel supports and only as high as the greatest stable Rust 354# target supported by the minimum `bindgen` version the kernel supports (that 355# is, if we do not test the actual `rustc`/`bindgen` versions running). 356# 357# Starting with `bindgen` 0.71.0, we will be able to set any future Rust version 358# instead, i.e. we will be able to set here our minimum supported Rust version. 359quiet_cmd_bindgen = BINDGEN $@ 360 cmd_bindgen = \ 361 $(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \ 362 --use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \ 363 --no-debug '.*' --enable-function-attribute-detection \ 364 -o $@ -- $(bindgen_c_flags_final) -DMODULE \ 365 $(bindgen_target_cflags) $(bindgen_target_extra) 366 367$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ 368 $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters) 369$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \ 370 sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@ 371$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ 372 $(src)/bindgen_parameters FORCE 373 $(call if_changed_dep,bindgen) 374 375$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \ 376 $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters) 377$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \ 378 $(src)/bindgen_parameters FORCE 379 $(call if_changed_dep,bindgen) 380 381# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn 382# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here 383# given it is `libclang`; but for consistency, future Clang changes and/or 384# a potential future GCC backend for `bindgen`, we disable it too. 385$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \ 386 --blocklist-type '.*' --allowlist-var '' \ 387 --allowlist-function 'rust_helper_.*' 388$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \ 389 -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations 390$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \ 391 sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@ 392$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE 393 $(call if_changed_dep,bindgen) 394 395rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }' 396 397quiet_cmd_exports = EXPORTS $@ 398 cmd_exports = \ 399 $(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@ 400 401$(obj)/exports_core_generated.h: $(obj)/core.o FORCE 402 $(call if_changed,exports) 403 404# Even though Rust kernel modules should never use the bindings directly, 405# symbols from the `bindings` crate and the C helpers need to be exported 406# because Rust generics and inlined functions may not get their code generated 407# in the crate where they are defined. Other helpers, called from non-inline 408# functions, may not be exported, in principle. However, in general, the Rust 409# compiler does not guarantee codegen will be performed for a non-inline 410# function either. Therefore, we export all symbols from helpers and bindings. 411# In the future, this may be revisited to reduce the number of exports after 412# the compiler is informed about the places codegen is required. 413$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE 414 $(call if_changed,exports) 415 416$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE 417 $(call if_changed,exports) 418 419$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE 420 $(call if_changed,exports) 421 422quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@ 423 cmd_rustc_procmacro = \ 424 $(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \ 425 -Clinker-flavor=gcc -Clinker=$(HOSTCC) \ 426 -Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \ 427 --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \ 428 --crate-type proc-macro \ 429 --crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \ 430 @$(objtree)/include/generated/rustc_cfg $< 431 432# Procedural macros can only be used with the `rustc` that compiled it. 433$(obj)/$(libmacros_name): $(src)/macros/lib.rs FORCE 434 +$(call if_changed_dep,rustc_procmacro) 435 436$(obj)/$(libpin_init_internal_name): private rustc_target_flags = --cfg kernel 437$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs FORCE 438 +$(call if_changed_dep,rustc_procmacro) 439 440quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@ 441 cmd_rustc_library = \ 442 OBJTREE=$(abspath $(objtree)) \ 443 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ 444 $(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \ 445 --emit=dep-info=$(depfile) --emit=obj=$@ \ 446 --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \ 447 --crate-type rlib -L$(objtree)/$(obj) \ 448 --crate-name $(patsubst %.o,%,$(notdir $@)) $< \ 449 --sysroot=/dev/null \ 450 $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \ 451 $(cmd_objtool) 452 453rust-analyzer: 454 $(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \ 455 --cfgs='core=$(core-cfgs)' $(core-edition) \ 456 $(realpath $(srctree)) $(realpath $(objtree)) \ 457 $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \ 458 > rust-project.json 459 460redirect-intrinsics = \ 461 __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \ 462 __adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \ 463 __muloti4 __multi3 \ 464 __udivmodti4 __udivti3 __umodti3 465 466ifdef CONFIG_ARM 467 # Add eabi initrinsics for ARM 32-bit 468 redirect-intrinsics += \ 469 __aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \ 470 __aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \ 471 __aeabi_uldivmod 472endif 473ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),) 474 # These intrinsics are defined for ARM64 and RISCV64 475 redirect-intrinsics += \ 476 __ashrti3 \ 477 __ashlti3 __lshrti3 478endif 479 480ifdef CONFIG_MODVERSIONS 481cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \ 482 $(call rust_exports,$@,"%s\n") | \ 483 scripts/gendwarfksyms/gendwarfksyms \ 484 $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \ 485 $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \ 486 $@ >> $(dot-target).cmd) 487endif 488 489define rule_rustc_library 490 $(call cmd_and_fixdep,rustc_library) 491 $(call cmd,gen_objtooldep) 492 $(call cmd,gendwarfksyms) 493endef 494 495define rule_rust_cc_library 496 $(call if_changed_rule,cc_o_c) 497 $(call cmd,force_checksrc) 498 $(call cmd,gendwarfksyms) 499endef 500 501# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol 502# versions are calculated for the helpers too. 503$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE 504 +$(call if_changed_rule,rust_cc_library) 505 506# Disable symbol versioning for exports.o to avoid conflicts with the actual 507# symbol versions generated from Rust objects. 508$(obj)/exports.o: private skip_gendwarfksyms = 1 509 510$(obj)/core.o: private skip_clippy = 1 511$(obj)/core.o: private skip_flags = --edition=2021 -Wunreachable_pub 512$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym)) 513$(obj)/core.o: private rustc_target_flags = --edition=$(core-edition) $(core-cfgs) 514$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ 515 $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE 516 +$(call if_changed_rule,rustc_library) 517ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) 518$(obj)/core.o: scripts/target.json 519endif 520KCOV_INSTRUMENT_core.o := n 521 522$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 523$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' 524$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE 525 +$(call if_changed_rule,rustc_library) 526 527$(obj)/pin_init.o: private skip_gendwarfksyms = 1 528$(obj)/pin_init.o: private rustc_target_flags = --extern pin_init_internal \ 529 --extern macros --cfg kernel 530$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \ 531 $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE 532 +$(call if_changed_rule,rustc_library) 533 534# Even if normally `build_error` is not a kernel object, it should still be 535# treated as such so that we pass the same flags. Otherwise, for instance, 536# `rustc` will complain about missing sanitizer flags causing an ABI mismatch. 537$(obj)/build_error.o: private is-kernel-object := y 538$(obj)/build_error.o: private skip_gendwarfksyms = 1 539$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE 540 +$(call if_changed_rule,rustc_library) 541 542$(obj)/ffi.o: private skip_gendwarfksyms = 1 543$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE 544 +$(call if_changed_rule,rustc_library) 545 546$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init 547$(obj)/bindings.o: $(src)/bindings/lib.rs \ 548 $(obj)/ffi.o \ 549 $(obj)/pin_init.o \ 550 $(obj)/bindings/bindings_generated.rs \ 551 $(obj)/bindings/bindings_helpers_generated.rs FORCE 552 +$(call if_changed_rule,rustc_library) 553 554$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init 555$(obj)/uapi.o: private skip_gendwarfksyms = 1 556$(obj)/uapi.o: $(src)/uapi/lib.rs \ 557 $(obj)/ffi.o \ 558 $(obj)/pin_init.o \ 559 $(obj)/uapi/uapi_generated.rs FORCE 560 +$(call if_changed_rule,rustc_library) 561 562$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \ 563 --extern build_error --extern macros --extern bindings --extern uapi 564$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \ 565 $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE 566 +$(call if_changed_rule,rustc_library) 567 568ifdef CONFIG_JUMP_LABEL 569$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs 570endif 571ifndef CONFIG_UML 572ifdef CONFIG_BUG 573$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs 574endif 575endif 576 577endif # CONFIG_RUST 578