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 454# This is only for i386 UM builds, which need the 32-bit target not -m32 455BINDGEN_TARGET_i386 := i386-linux-gnu 456BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH)) 457BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) 458 459# All warnings are inhibited since GCC builds are very experimental, 460# many GCC warnings are not supported by Clang, they may only appear in 461# some configurations, with new GCC versions, etc. 462bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) 463 464# Auto variable zero-initialization requires an additional special option with 465# clang that is going to be removed sometime in the future (likely in 466# clang-18), so make sure to pass this option only if clang supports it 467# (libclang major version < 16). 468# 469# https://github.com/llvm/llvm-project/issues/44842 470# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags 471ifdef CONFIG_INIT_STACK_ALL_ZERO 472libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p') 473ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1) 474bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 475endif 476endif 477 478bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \ 479 $(bindgen_extra_c_flags) 480endif 481 482ifdef CONFIG_LTO 483bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags)) 484else 485bindgen_c_flags_lto = $(bindgen_c_flags) 486endif 487 488# `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin 489# prototypes for functions like `memcpy` -- if this flag is not passed, 490# `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on 491# architecture instead of generating `usize`. 492bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__ 493 494# `--rust-target` points to our minimum supported Rust version. 495quiet_cmd_bindgen = BINDGEN $@ 496 cmd_bindgen = \ 497 $(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.85 \ 498 --use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \ 499 --no-debug '.*' --enable-function-attribute-detection \ 500 -o $@ -- $(bindgen_c_flags_final) -DMODULE \ 501 $(bindgen_target_cflags) $(bindgen_target_extra) 502 503$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ 504 $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters) 505$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \ 506 sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@ 507$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ 508 $(src)/bindgen_parameters FORCE 509 $(call if_changed_dep,bindgen) 510 511$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \ 512 $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters) 513$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \ 514 $(src)/bindgen_parameters FORCE 515 $(call if_changed_dep,bindgen) 516 517# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn 518# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here 519# given it is `libclang`; but for consistency, future Clang changes and/or 520# a potential future GCC backend for `bindgen`, we disable it too. 521$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \ 522 --blocklist-type '.*' --allowlist-var '' \ 523 --allowlist-function 'rust_helper_.*' 524$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \ 525 -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations 526$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \ 527 sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@ 528$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE 529 $(call if_changed_dep,bindgen) 530 531quiet_cmd_rust_helper = HELPER $@ 532 cmd_rust_helper = \ 533 $(CC) $(filter-out $(CFLAGS_REMOVE_helpers/helpers.o), $(c_flags)) \ 534 -c -g0 $< -emit-llvm -o $@ 535 536$(obj)/helpers/helpers.bc: private part-of-builtin := y 537$(obj)/helpers/helpers_module.bc: private part-of-module := y 538$(obj)/helpers/helpers.bc $(obj)/helpers/helpers_module.bc: $(src)/helpers/helpers.c FORCE 539 +$(call if_changed_dep,rust_helper) 540 541rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }' 542 543quiet_cmd_exports = EXPORTS $@ 544 cmd_exports = \ 545 $(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@ 546 547$(obj)/exports_core_generated.h: $(obj)/core.o FORCE 548 $(call if_changed,exports) 549 550# Even though Rust kernel modules should never use the bindings directly, 551# symbols from the `bindings` crate and the C helpers need to be exported 552# because Rust generics and inlined functions may not get their code generated 553# in the crate where they are defined. Other helpers, called from non-inline 554# functions, may not be exported, in principle. However, in general, the Rust 555# compiler does not guarantee codegen will be performed for a non-inline 556# function either. Therefore, we export all symbols from helpers and bindings. 557# In the future, this may be revisited to reduce the number of exports after 558# the compiler is informed about the places codegen is required. 559$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE 560 $(call if_changed,exports) 561 562$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE 563 $(call if_changed,exports) 564 565$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE 566 $(call if_changed,exports) 567 568quiet_cmd_rustc_procmacrolibrary = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) PL $@ 569 cmd_rustc_procmacrolibrary = \ 570 $(rustc_target_envs) \ 571 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ 572 $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \ 573 --emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \ 574 --out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \ 575 --crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $< 576 577$(obj)/libproc_macro2.rlib: private skip_clippy = 1 578$(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags) 579$(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE 580 +$(call if_changed_dep,rustc_procmacrolibrary) 581 582$(obj)/libquote.rlib: private skip_clippy = 1 583$(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags) 584$(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags) 585$(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE 586 +$(call if_changed_dep,rustc_procmacrolibrary) 587 588$(obj)/libsyn.rlib: private skip_clippy = 1 589$(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags) 590$(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE 591 +$(call if_changed_dep,rustc_procmacrolibrary) 592 593quiet_cmd_rustc_procmacro = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) P $@ 594 cmd_rustc_procmacro = \ 595 $(rustc_target_envs) \ 596 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) $(rust_common_flags) $(rustc_target_flags) \ 597 -Clinker-flavor=gcc -Clinker=$(HOSTCC) \ 598 -Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \ 599 --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \ 600 --crate-type proc-macro -L$(objtree)/$(obj) \ 601 --crate-name $(patsubst lib%.$(procmacro-extension),%,$(notdir $@)) \ 602 @$(objtree)/include/generated/rustc_cfg $< 603 604# Procedural macros can only be used with the `rustc` that compiled it. 605$(obj)/$(libzerocopy_derive_name): private skip_clippy = 1 606$(obj)/$(libzerocopy_derive_name): private rustc_target_flags = $(zerocopy_derive-flags) 607$(obj)/$(libzerocopy_derive_name): $(src)/zerocopy-derive/lib.rs $(obj)/libproc_macro2.rlib \ 608 $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE 609 +$(call if_changed_dep,rustc_procmacro) 610 611$(obj)/$(libmacros_name): private rustc_target_flags = \ 612 --extern proc_macro2 --extern quote --extern syn 613$(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \ 614 $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE 615 +$(call if_changed_dep,rustc_procmacro) 616 617$(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags) 618$(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \ 619 $(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE 620 +$(call if_changed_dep,rustc_procmacro) 621 622# `rustc` requires `-Zunstable-options` to use custom target specifications 623# since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534). 624quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@ 625 cmd_rustc_library = \ 626 $(rustc_target_envs) \ 627 OBJTREE=$(abspath $(objtree)) \ 628 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ 629 $(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \ 630 --emit=dep-info=$(depfile) --emit=$(if $(link_helper),llvm-bc=$(patsubst %.o,%.bc,$@),obj=$@) \ 631 --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \ 632 --crate-type rlib -L$(objtree)/$(obj) \ 633 --crate-name $(patsubst %.o,%,$(notdir $@)) $< \ 634 --sysroot=/dev/null \ 635 -Zunstable-options \ 636 $(if $(link_helper),;$(LLVM_LINK) --internalize --suppress-warnings $(patsubst %.o,%.bc,$@) \ 637 $(obj)/helpers/helpers$(if $(part-of-module),_module).bc -o $(patsubst %.o,%.m.bc,$@); \ 638 $(CC) $(CLANG_FLAGS) $(KBUILD_CFLAGS) -Wno-override-module -c $(patsubst %.o,%.m.bc,$@) -o $@ \ 639 $(cmd_ld_single)) \ 640 $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \ 641 $(cmd_objtool) 642 643rust-analyzer: 644 $(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \ 645 --cfgs='core=$(core-cfgs)' $(core-edition) \ 646 --cfgs='proc_macro2=$(proc_macro2-cfgs)' \ 647 --cfgs='quote=$(quote-cfgs)' \ 648 --cfgs='syn=$(syn-cfgs)' \ 649 --cfgs='pin_init_internal=$(pin_init_internal-cfgs)' \ 650 --cfgs='pin_init=$(pin_init-cfgs)' \ 651 --envs='zerocopy=$(zerocopy-envs)' \ 652 $(realpath $(srctree)) $(realpath $(objtree)) \ 653 $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \ 654 > rust-project.json 655 656redirect-intrinsics = \ 657 __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \ 658 __adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \ 659 __muloti4 __multi3 \ 660 __udivmodti4 __udivti3 __umodti3 661 662ifdef CONFIG_ARM 663 # Add eabi initrinsics for ARM 32-bit 664 redirect-intrinsics += \ 665 __aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \ 666 __aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \ 667 __aeabi_uldivmod 668endif 669ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),) 670 # These intrinsics are defined for ARM64 and RISCV64 671 redirect-intrinsics += \ 672 __ashrti3 \ 673 __ashlti3 __lshrti3 674endif 675 676ifdef CONFIG_MODVERSIONS 677cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \ 678 $(call rust_exports,$@,"%s\n") | \ 679 scripts/gendwarfksyms/gendwarfksyms \ 680 $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \ 681 $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \ 682 $@ >> $(dot-target).cmd) 683endif 684 685define rule_rustc_library 686 $(call cmd_and_fixdep,rustc_library) 687 $(call cmd,gen_objtooldep) 688 $(call cmd,gendwarfksyms) 689endef 690 691define rule_rust_cc_library 692 $(call if_changed_rule,cc_o_c) 693 $(call cmd,force_checksrc) 694 $(call cmd,gendwarfksyms) 695endef 696 697# helpers.o uses the same export mechanism as Rust libraries, so ensure symbol 698# versions are calculated for the helpers too. 699$(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE 700 +$(call if_changed_rule,rust_cc_library) 701 702# Disable symbol versioning for exports.o to avoid conflicts with the actual 703# symbol versions generated from Rust objects. 704$(obj)/exports.o: private skip_gendwarfksyms = 1 705 706$(obj)/core.o: private skip_clippy = 1 707$(obj)/core.o: private skip_flags = $(core-skip_flags) 708$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym)) 709$(obj)/core.o: private rustc_target_flags = $(core-flags) 710$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \ 711 $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE 712 +$(call if_changed_rule,rustc_library) 713ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),) 714$(obj)/core.o: scripts/target.json 715endif 716KCOV_INSTRUMENT_core.o := n 717 718$(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 719$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' 720$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE 721 +$(call if_changed_rule,rustc_library) 722 723$(obj)/zerocopy.o: private skip_clippy = 1 724$(obj)/zerocopy.o: private skip_gendwarfksyms = 1 725$(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs) 726$(obj)/zerocopy.o: private rustc_target_flags = $(zerocopy-flags) 727$(obj)/zerocopy.o: $(src)/zerocopy/src/lib.rs $(obj)/compiler_builtins.o FORCE 728 +$(call if_changed_rule,rustc_library) 729 730$(obj)/pin_init.o: private skip_gendwarfksyms = 1 731$(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags) 732$(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \ 733 $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE 734 +$(call if_changed_rule,rustc_library) 735 736# Even if normally `build_error` is not a kernel object, it should still be 737# treated as such so that we pass the same flags. Otherwise, for instance, 738# `rustc` will complain about missing sanitizer flags causing an ABI mismatch. 739$(obj)/build_error.o: private is-kernel-object := y 740$(obj)/build_error.o: private skip_gendwarfksyms = 1 741$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE 742 +$(call if_changed_rule,rustc_library) 743 744$(obj)/ffi.o: private skip_gendwarfksyms = 1 745$(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE 746 +$(call if_changed_rule,rustc_library) 747 748$(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init 749$(obj)/bindings.o: $(src)/bindings/lib.rs \ 750 $(obj)/ffi.o \ 751 $(obj)/pin_init.o \ 752 $(obj)/bindings/bindings_generated.rs \ 753 $(obj)/bindings/bindings_helpers_generated.rs FORCE 754 +$(call if_changed_rule,rustc_library) 755 756$(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init 757$(obj)/uapi.o: private skip_gendwarfksyms = 1 758$(obj)/uapi.o: $(src)/uapi/lib.rs \ 759 $(obj)/ffi.o \ 760 $(obj)/pin_init.o \ 761 $(obj)/uapi/uapi_generated.rs FORCE 762 +$(call if_changed_rule,rustc_library) 763 764$(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \ 765 --extern build_error --extern macros --extern bindings --extern uapi \ 766 --extern zerocopy --extern zerocopy_derive 767$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \ 768 $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o \ 769 $(obj)/zerocopy.o $(obj)/$(libzerocopy_derive_name) FORCE 770 +$(call if_changed_rule,rustc_library) 771 772ifdef CONFIG_JUMP_LABEL 773$(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs 774endif 775ifndef CONFIG_UML 776ifdef CONFIG_BUG 777$(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs 778endif 779endif 780 781ifdef CONFIG_RUST_INLINE_HELPERS 782$(obj)/kernel.o: private link_helper = 1 783$(obj)/kernel.o: $(obj)/helpers/helpers.bc 784endif 785 786endif # CONFIG_RUST 787