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