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