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