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