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