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