12f7ab126SMiguel Ojeda# SPDX-License-Identifier: GPL-2.0 22f7ab126SMiguel Ojeda 32f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += core.o compiler_builtins.o 42f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += exports_core_generated.h 52f7ab126SMiguel Ojeda 62f7ab126SMiguel Ojeda# Missing prototypes are expected in the helpers since these are exported 72f7ab126SMiguel Ojeda# for Rust only, thus there is no header nor prototypes. 82f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += helpers.o 92f7ab126SMiguel OjedaCFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations 102f7ab126SMiguel Ojeda 112f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += libmacros.so 122f7ab126SMiguel Ojedano-clean-files += libmacros.so 132f7ab126SMiguel Ojeda 142f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs 152f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o 162f7ab126SMiguel Ojedaalways-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \ 172f7ab126SMiguel Ojeda exports_kernel_generated.h 182f7ab126SMiguel Ojeda 194e174665SAsahi Linaalways-$(CONFIG_RUST) += uapi/uapi_generated.rs 204e174665SAsahi Linaobj-$(CONFIG_RUST) += uapi.o 214e174665SAsahi Lina 22ecaa6ddfSGary Guoifdef CONFIG_RUST_BUILD_ASSERT_ALLOW 23ecaa6ddfSGary Guoobj-$(CONFIG_RUST) += build_error.o 24ecaa6ddfSGary Guoelse 25ecaa6ddfSGary Guoalways-$(CONFIG_RUST) += build_error.o 26ecaa6ddfSGary Guoendif 27ecaa6ddfSGary Guo 282f7ab126SMiguel Ojedaobj-$(CONFIG_RUST) += exports.o 292f7ab126SMiguel Ojeda 30*a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs 31*a66d733dSMiguel Ojedaalways-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c 32*a66d733dSMiguel Ojeda 33*a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o 34*a66d733dSMiguel Ojedaobj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o 35*a66d733dSMiguel Ojeda 362f7ab126SMiguel Ojeda# Avoids running `$(RUSTC)` for the sysroot when it may not be available. 372f7ab126SMiguel Ojedaifdef CONFIG_RUST 382f7ab126SMiguel Ojeda 392f7ab126SMiguel Ojeda# `$(rust_flags)` is passed in case the user added `--sysroot`. 402f7ab126SMiguel Ojedarustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot) 412f7ab126SMiguel Ojedarustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2) 422f7ab126SMiguel OjedaRUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library 432f7ab126SMiguel Ojeda 442f7ab126SMiguel Ojedaifeq ($(quiet),silent_) 452f7ab126SMiguel Ojedacargo_quiet=-q 462f7ab126SMiguel Ojedarust_test_quiet=-q 472f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q 48*a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null 492f7ab126SMiguel Ojedaelse ifeq ($(quiet),quiet_) 502f7ab126SMiguel Ojedarust_test_quiet=-q 512f7ab126SMiguel Ojedarustdoc_test_quiet=--test-args -q 52*a66d733dSMiguel Ojedarustdoc_test_kernel_quiet=>/dev/null 532f7ab126SMiguel Ojedaelse 542f7ab126SMiguel Ojedacargo_quiet=--verbose 552f7ab126SMiguel Ojedaendif 562f7ab126SMiguel Ojeda 572f7ab126SMiguel Ojedacore-cfgs = \ 582f7ab126SMiguel Ojeda --cfg no_fp_fmt_parse 592f7ab126SMiguel Ojeda 602f7ab126SMiguel Ojedaalloc-cfgs = \ 618909a80eSMiguel Ojeda --cfg no_borrow \ 622f7ab126SMiguel Ojeda --cfg no_fmt \ 632f7ab126SMiguel Ojeda --cfg no_global_oom_handling \ 642f7ab126SMiguel Ojeda --cfg no_macros \ 652f7ab126SMiguel Ojeda --cfg no_rc \ 662f7ab126SMiguel Ojeda --cfg no_str \ 672f7ab126SMiguel Ojeda --cfg no_string \ 682f7ab126SMiguel Ojeda --cfg no_sync \ 692f7ab126SMiguel Ojeda --cfg no_thin 702f7ab126SMiguel Ojeda 712f7ab126SMiguel Ojedaquiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $< 722f7ab126SMiguel Ojeda cmd_rustdoc = \ 732f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 742f7ab126SMiguel Ojeda $(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \ 752f7ab126SMiguel Ojeda $(rustc_target_flags) -L$(objtree)/$(obj) \ 762f7ab126SMiguel Ojeda --output $(objtree)/$(obj)/doc \ 772f7ab126SMiguel Ojeda --crate-name $(subst rustdoc-,,$@) \ 782f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg $< 792f7ab126SMiguel Ojeda 802f7ab126SMiguel Ojeda# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute 812f7ab126SMiguel Ojeda# can be used to specify a custom logo. However: 822f7ab126SMiguel Ojeda# - The given value is used as-is, thus it cannot be relative or a local file 832f7ab126SMiguel Ojeda# (unlike the non-custom case) since the generated docs have subfolders. 842f7ab126SMiguel Ojeda# - It requires adding it to every crate. 852f7ab126SMiguel Ojeda# - It requires changing `core` which comes from the sysroot. 862f7ab126SMiguel Ojeda# 872f7ab126SMiguel Ojeda# Using `-Zcrate-attr` would solve the last two points, but not the first. 882f7ab126SMiguel Ojeda# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new 892f7ab126SMiguel Ojeda# command-like flags to solve the issue. Meanwhile, we use the non-custom case 902f7ab126SMiguel Ojeda# and then retouch the generated files. 912f7ab126SMiguel Ojedarustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \ 922f7ab126SMiguel Ojeda rustdoc-alloc rustdoc-kernel 932f7ab126SMiguel Ojeda $(Q)cp $(srctree)/Documentation/images/logo.svg $(objtree)/$(obj)/doc 942f7ab126SMiguel Ojeda $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(objtree)/$(obj)/doc 952f7ab126SMiguel Ojeda $(Q)find $(objtree)/$(obj)/doc -name '*.html' -type f -print0 | xargs -0 sed -Ei \ 962f7ab126SMiguel Ojeda -e 's:rust-logo\.svg:logo.svg:g' \ 972f7ab126SMiguel Ojeda -e 's:rust-logo\.png:logo.svg:g' \ 982f7ab126SMiguel Ojeda -e 's:favicon\.svg:logo.svg:g' \ 992f7ab126SMiguel Ojeda -e 's:<link rel="alternate icon" type="image/png" href="[./]*favicon-(16x16|32x32)\.png">::g' 1002f7ab126SMiguel Ojeda $(Q)echo '.logo-container > img { object-fit: contain; }' \ 1012f7ab126SMiguel Ojeda >> $(objtree)/$(obj)/doc/rustdoc.css 1022f7ab126SMiguel Ojeda 1032f7ab126SMiguel Ojedarustdoc-macros: private rustdoc_host = yes 1042f7ab126SMiguel Ojedarustdoc-macros: private rustc_target_flags = --crate-type proc-macro \ 1052f7ab126SMiguel Ojeda --extern proc_macro 1062f7ab126SMiguel Ojedarustdoc-macros: $(src)/macros/lib.rs FORCE 1072f7ab126SMiguel Ojeda $(call if_changed,rustdoc) 1082f7ab126SMiguel Ojeda 1092f7ab126SMiguel Ojedarustdoc-core: private rustc_target_flags = $(core-cfgs) 1102f7ab126SMiguel Ojedarustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE 1112f7ab126SMiguel Ojeda $(call if_changed,rustdoc) 1122f7ab126SMiguel Ojeda 1132f7ab126SMiguel Ojedarustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE 1142f7ab126SMiguel Ojeda $(call if_changed,rustdoc) 1152f7ab126SMiguel Ojeda 1162f7ab126SMiguel Ojeda# We need to allow `rustdoc::broken_intra_doc_links` because some 1172f7ab126SMiguel Ojeda# `no_global_oom_handling` functions refer to non-`no_global_oom_handling` 1182f7ab126SMiguel Ojeda# functions. Ideally `rustdoc` would have a way to distinguish broken links 1192f7ab126SMiguel Ojeda# due to things that are "configured out" vs. entirely non-existing ones. 1202f7ab126SMiguel Ojedarustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \ 1212f7ab126SMiguel Ojeda -Arustdoc::broken_intra_doc_links 1222f7ab126SMiguel Ojedarustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE 1232f7ab126SMiguel Ojeda $(call if_changed,rustdoc) 1242f7ab126SMiguel Ojeda 1252f7ab126SMiguel Ojedarustdoc-kernel: private rustc_target_flags = --extern alloc \ 126ecaa6ddfSGary Guo --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \ 1274e174665SAsahi Lina --extern bindings --extern uapi 1282f7ab126SMiguel Ojedarustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \ 1292f7ab126SMiguel Ojeda rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \ 1302f7ab126SMiguel Ojeda $(obj)/bindings.o FORCE 1312f7ab126SMiguel Ojeda $(call if_changed,rustdoc) 1322f7ab126SMiguel Ojeda 1332f7ab126SMiguel Ojedaquiet_cmd_rustc_test_library = RUSTC TL $< 1342f7ab126SMiguel Ojeda cmd_rustc_test_library = \ 1352f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 1362f7ab126SMiguel Ojeda $(RUSTC) $(rust_common_flags) \ 1372f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \ 1382f7ab126SMiguel Ojeda --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \ 1392f7ab126SMiguel Ojeda --out-dir $(objtree)/$(obj)/test --cfg testlib \ 1402f7ab126SMiguel Ojeda --sysroot $(objtree)/$(obj)/test/sysroot \ 1412f7ab126SMiguel Ojeda -L$(objtree)/$(obj)/test \ 1422f7ab126SMiguel Ojeda --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $< 1432f7ab126SMiguel Ojeda 144ecaa6ddfSGary Guorusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE 145ecaa6ddfSGary Guo $(call if_changed,rustc_test_library) 146ecaa6ddfSGary Guo 1472f7ab126SMiguel Ojedarusttestlib-macros: private rustc_target_flags = --extern proc_macro 1482f7ab126SMiguel Ojedarusttestlib-macros: private rustc_test_library_proc = yes 1492f7ab126SMiguel Ojedarusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE 1502f7ab126SMiguel Ojeda $(call if_changed,rustc_test_library) 1512f7ab126SMiguel Ojeda 1522f7ab126SMiguel Ojedarusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE 1532f7ab126SMiguel Ojeda $(call if_changed,rustc_test_library) 1542f7ab126SMiguel Ojeda 1554e174665SAsahi Linarusttestlib-uapi: $(src)/uapi/lib.rs rusttest-prepare FORCE 1564e174665SAsahi Lina $(call if_changed,rustc_test_library) 1574e174665SAsahi Lina 1582f7ab126SMiguel Ojedaquiet_cmd_rustdoc_test = RUSTDOC T $< 1592f7ab126SMiguel Ojeda cmd_rustdoc_test = \ 1602f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 1612f7ab126SMiguel Ojeda $(RUSTDOC) --test $(rust_common_flags) \ 1622f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg \ 1632f7ab126SMiguel Ojeda $(rustc_target_flags) $(rustdoc_test_target_flags) \ 1642f7ab126SMiguel Ojeda --sysroot $(objtree)/$(obj)/test/sysroot $(rustdoc_test_quiet) \ 1652f7ab126SMiguel Ojeda -L$(objtree)/$(obj)/test --output $(objtree)/$(obj)/doc \ 1662f7ab126SMiguel Ojeda --crate-name $(subst rusttest-,,$@) $< 1672f7ab126SMiguel Ojeda 168*a66d733dSMiguel Ojedaquiet_cmd_rustdoc_test_kernel = RUSTDOC TK $< 169*a66d733dSMiguel Ojeda cmd_rustdoc_test_kernel = \ 170*a66d733dSMiguel Ojeda rm -rf $(objtree)/$(obj)/test/doctests/kernel; \ 171*a66d733dSMiguel Ojeda mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \ 172*a66d733dSMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 173*a66d733dSMiguel Ojeda $(RUSTDOC) --test $(rust_flags) \ 174*a66d733dSMiguel Ojeda @$(objtree)/include/generated/rustc_cfg \ 175*a66d733dSMiguel Ojeda -L$(objtree)/$(obj) --extern alloc --extern kernel \ 176*a66d733dSMiguel Ojeda --extern build_error --extern macros \ 177*a66d733dSMiguel Ojeda --extern bindings --extern uapi \ 178*a66d733dSMiguel Ojeda --no-run --crate-name kernel -Zunstable-options \ 179*a66d733dSMiguel Ojeda --test-builder $(objtree)/scripts/rustdoc_test_builder \ 180*a66d733dSMiguel Ojeda $< $(rustdoc_test_kernel_quiet); \ 181*a66d733dSMiguel Ojeda $(objtree)/scripts/rustdoc_test_gen 182*a66d733dSMiguel Ojeda 183*a66d733dSMiguel Ojeda%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \ 184*a66d733dSMiguel Ojeda $(src)/kernel/lib.rs $(obj)/kernel.o \ 185*a66d733dSMiguel Ojeda $(objtree)/scripts/rustdoc_test_builder \ 186*a66d733dSMiguel Ojeda $(objtree)/scripts/rustdoc_test_gen FORCE 187*a66d733dSMiguel Ojeda $(call if_changed,rustdoc_test_kernel) 188*a66d733dSMiguel Ojeda 1892f7ab126SMiguel Ojeda# We cannot use `-Zpanic-abort-tests` because some tests are dynamic, 1902f7ab126SMiguel Ojeda# so for the moment we skip `-Cpanic=abort`. 1912f7ab126SMiguel Ojedaquiet_cmd_rustc_test = RUSTC T $< 1922f7ab126SMiguel Ojeda cmd_rustc_test = \ 1932f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 1942f7ab126SMiguel Ojeda $(RUSTC) --test $(rust_common_flags) \ 1952f7ab126SMiguel Ojeda @$(objtree)/include/generated/rustc_cfg \ 1962f7ab126SMiguel Ojeda $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \ 1972f7ab126SMiguel Ojeda --sysroot $(objtree)/$(obj)/test/sysroot \ 1982f7ab126SMiguel Ojeda -L$(objtree)/$(obj)/test \ 1992f7ab126SMiguel Ojeda --crate-name $(subst rusttest-,,$@) $<; \ 2002f7ab126SMiguel Ojeda $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \ 2012f7ab126SMiguel Ojeda $(rustc_test_run_flags) 2022f7ab126SMiguel Ojeda 2032f7ab126SMiguel Ojedarusttest: rusttest-macros rusttest-kernel 2042f7ab126SMiguel Ojeda 2052f7ab126SMiguel Ojeda# This prepares a custom sysroot with our custom `alloc` instead of 2062f7ab126SMiguel Ojeda# the standard one. 2072f7ab126SMiguel Ojeda# 2082f7ab126SMiguel Ojeda# This requires several hacks: 2092f7ab126SMiguel Ojeda# - Unlike `core` and `alloc`, `std` depends on more than a dozen crates, 2102f7ab126SMiguel Ojeda# including third-party crates that need to be downloaded, plus custom 2112f7ab126SMiguel Ojeda# `build.rs` steps. Thus hardcoding things here is not maintainable. 2122f7ab126SMiguel Ojeda# - `cargo` knows how to build the standard library, but it is an unstable 2132f7ab126SMiguel Ojeda# feature so far (`-Zbuild-std`). 2142f7ab126SMiguel Ojeda# - `cargo` only considers the use case of building the standard library 2152f7ab126SMiguel Ojeda# to use it in a given package. Thus we need to create a dummy package 2162f7ab126SMiguel Ojeda# and pick the generated libraries from there. 2172f7ab126SMiguel Ojeda# - Since we only keep a subset of upstream `alloc` in-tree, we need 2182f7ab126SMiguel Ojeda# to recreate it on the fly by putting our sources on top. 2192f7ab126SMiguel Ojeda# - The usual ways of modifying the dependency graph in `cargo` do not seem 2202f7ab126SMiguel Ojeda# to apply for the `-Zbuild-std` steps, thus we have to mislead it 2212f7ab126SMiguel Ojeda# by modifying the sources in the sysroot. 2222f7ab126SMiguel Ojeda# - To avoid messing with the user's Rust installation, we create a clone 2232f7ab126SMiguel Ojeda# of the sysroot. However, `cargo` ignores `RUSTFLAGS` in the `-Zbuild-std` 2242f7ab126SMiguel Ojeda# steps, thus we use a wrapper binary passed via `RUSTC` to pass the flag. 2252f7ab126SMiguel Ojeda# 2262f7ab126SMiguel Ojeda# In the future, we hope to avoid the whole ordeal by either: 2272f7ab126SMiguel Ojeda# - Making the `test` crate not depend on `std` (either improving upstream 2282f7ab126SMiguel Ojeda# or having our own custom crate). 2292f7ab126SMiguel Ojeda# - Making the tests run in kernel space (requires the previous point). 2302f7ab126SMiguel Ojeda# - Making `std` and friends be more like a "normal" crate, so that 2312f7ab126SMiguel Ojeda# `-Zbuild-std` and related hacks are not needed. 2322f7ab126SMiguel Ojedaquiet_cmd_rustsysroot = RUSTSYSROOT 2332f7ab126SMiguel Ojeda cmd_rustsysroot = \ 2342f7ab126SMiguel Ojeda rm -rf $(objtree)/$(obj)/test; \ 2352f7ab126SMiguel Ojeda mkdir -p $(objtree)/$(obj)/test; \ 2362f7ab126SMiguel Ojeda cp -a $(rustc_sysroot) $(objtree)/$(obj)/test/sysroot; \ 2372f7ab126SMiguel Ojeda cp -r $(srctree)/$(src)/alloc/* \ 2382f7ab126SMiguel Ojeda $(objtree)/$(obj)/test/sysroot/lib/rustlib/src/rust/library/alloc/src; \ 2392f7ab126SMiguel Ojeda echo '\#!/bin/sh' > $(objtree)/$(obj)/test/rustc_sysroot; \ 2402f7ab126SMiguel Ojeda echo "$(RUSTC) --sysroot=$(abspath $(objtree)/$(obj)/test/sysroot) \"\$$@\"" \ 2412f7ab126SMiguel Ojeda >> $(objtree)/$(obj)/test/rustc_sysroot; \ 2422f7ab126SMiguel Ojeda chmod u+x $(objtree)/$(obj)/test/rustc_sysroot; \ 2432f7ab126SMiguel Ojeda $(CARGO) -q new $(objtree)/$(obj)/test/dummy; \ 2442f7ab126SMiguel Ojeda RUSTC=$(objtree)/$(obj)/test/rustc_sysroot $(CARGO) $(cargo_quiet) \ 2452f7ab126SMiguel Ojeda test -Zbuild-std --target $(rustc_host_target) \ 2462f7ab126SMiguel Ojeda --manifest-path $(objtree)/$(obj)/test/dummy/Cargo.toml; \ 2472f7ab126SMiguel Ojeda rm $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib/*; \ 2482f7ab126SMiguel Ojeda cp $(objtree)/$(obj)/test/dummy/target/$(rustc_host_target)/debug/deps/* \ 2492f7ab126SMiguel Ojeda $(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib 2502f7ab126SMiguel Ojeda 2512f7ab126SMiguel Ojedarusttest-prepare: FORCE 2522f7ab126SMiguel Ojeda $(call if_changed,rustsysroot) 2532f7ab126SMiguel Ojeda 2542f7ab126SMiguel Ojedarusttest-macros: private rustc_target_flags = --extern proc_macro 2552f7ab126SMiguel Ojedarusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro 2562f7ab126SMiguel Ojedarusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE 2572f7ab126SMiguel Ojeda $(call if_changed,rustc_test) 2582f7ab126SMiguel Ojeda $(call if_changed,rustdoc_test) 2592f7ab126SMiguel Ojeda 2602f7ab126SMiguel Ojedarusttest-kernel: private rustc_target_flags = --extern alloc \ 2614e174665SAsahi Lina --extern build_error --extern macros --extern bindings --extern uapi 2622f7ab126SMiguel Ojedarusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \ 2634e174665SAsahi Lina rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \ 2644e174665SAsahi Lina rusttestlib-uapi FORCE 2652f7ab126SMiguel Ojeda $(call if_changed,rustc_test) 2662f7ab126SMiguel Ojeda $(call if_changed,rustc_test_library) 2672f7ab126SMiguel Ojeda 2682f7ab126SMiguel Ojedaifdef CONFIG_CC_IS_CLANG 2692f7ab126SMiguel Ojedabindgen_c_flags = $(c_flags) 2702f7ab126SMiguel Ojedaelse 2712f7ab126SMiguel Ojeda# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC 2722f7ab126SMiguel Ojeda# plugin backend and/or the Clang driver would be perfectly compatible with GCC. 2732f7ab126SMiguel Ojeda# 2742f7ab126SMiguel Ojeda# For the moment, here we are tweaking the flags on the fly. This is a hack, 2752f7ab126SMiguel Ojeda# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT` 2762f7ab126SMiguel Ojeda# if we end up using one of those structs). 2772f7ab126SMiguel Ojedabindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \ 2782f7ab126SMiguel Ojeda -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \ 2792f7ab126SMiguel Ojeda -mindirect-branch=thunk-extern -mindirect-branch-register \ 2802f7ab126SMiguel Ojeda -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \ 2812f7ab126SMiguel Ojeda -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \ 2822f7ab126SMiguel Ojeda -mno-pointers-to-nested-functions -mno-string \ 2832f7ab126SMiguel Ojeda -mno-strict-align -mstrict-align \ 2842f7ab126SMiguel Ojeda -fconserve-stack -falign-jumps=% -falign-loops=% \ 2852f7ab126SMiguel Ojeda -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \ 2862f7ab126SMiguel Ojeda -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \ 2872f7ab126SMiguel Ojeda -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \ 2882f7ab126SMiguel Ojeda -fzero-call-used-regs=% -fno-stack-clash-protection \ 2892f7ab126SMiguel Ojeda -fno-inline-functions-called-once \ 2902f7ab126SMiguel Ojeda --param=% --param asan-% 2912f7ab126SMiguel Ojeda 2922f7ab126SMiguel Ojeda# Derived from `scripts/Makefile.clang`. 2932f7ab126SMiguel OjedaBINDGEN_TARGET_x86 := x86_64-linux-gnu 2942f7ab126SMiguel OjedaBINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH)) 2952f7ab126SMiguel Ojeda 2962f7ab126SMiguel Ojeda# All warnings are inhibited since GCC builds are very experimental, 2972f7ab126SMiguel Ojeda# many GCC warnings are not supported by Clang, they may only appear in 2982f7ab126SMiguel Ojeda# some configurations, with new GCC versions, etc. 2992f7ab126SMiguel Ojedabindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET) 3002f7ab126SMiguel Ojeda 301d966c3caSAndrea Righi# Auto variable zero-initialization requires an additional special option with 302d966c3caSAndrea Righi# clang that is going to be removed sometime in the future (likely in 303d966c3caSAndrea Righi# clang-18), so make sure to pass this option only if clang supports it 304d966c3caSAndrea Righi# (libclang major version < 16). 305d966c3caSAndrea Righi# 306d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/issues/44842 307d966c3caSAndrea Righi# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags 308d966c3caSAndrea Righiifdef CONFIG_INIT_STACK_ALL_ZERO 309d966c3caSAndrea Righilibclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p') 310d966c3caSAndrea Righiifeq ($(shell expr $(libclang_maj_ver) \< 16), 1) 311d966c3caSAndrea Righibindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang 312d966c3caSAndrea Righiendif 313d966c3caSAndrea Righiendif 314d966c3caSAndrea Righi 3152f7ab126SMiguel Ojedabindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \ 3162f7ab126SMiguel Ojeda $(bindgen_extra_c_flags) 3172f7ab126SMiguel Ojedaendif 3182f7ab126SMiguel Ojeda 3192f7ab126SMiguel Ojedaifdef CONFIG_LTO 3202f7ab126SMiguel Ojedabindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags)) 3212f7ab126SMiguel Ojedaelse 3222f7ab126SMiguel Ojedabindgen_c_flags_lto = $(bindgen_c_flags) 3232f7ab126SMiguel Ojedaendif 3242f7ab126SMiguel Ojeda 3252f7ab126SMiguel Ojedabindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__ 3262f7ab126SMiguel Ojeda 3272f7ab126SMiguel Ojedaquiet_cmd_bindgen = BINDGEN $@ 3282f7ab126SMiguel Ojeda cmd_bindgen = \ 3292f7ab126SMiguel Ojeda $(BINDGEN) $< $(bindgen_target_flags) \ 3302f7ab126SMiguel Ojeda --use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \ 3312f7ab126SMiguel Ojeda --no-debug '.*' \ 3322f7ab126SMiguel Ojeda --size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \ 3332f7ab126SMiguel Ojeda $(bindgen_target_cflags) $(bindgen_target_extra) 3342f7ab126SMiguel Ojeda 3352f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ 3361c5f054fSVincenzo Palazzo $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters) 3372f7ab126SMiguel Ojeda$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ 3382f7ab126SMiguel Ojeda $(src)/bindgen_parameters FORCE 3392f7ab126SMiguel Ojeda $(call if_changed_dep,bindgen) 3402f7ab126SMiguel Ojeda 3414e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \ 3424e174665SAsahi Lina $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters) 3434e174665SAsahi Lina$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \ 3444e174665SAsahi Lina $(src)/bindgen_parameters FORCE 3454e174665SAsahi Lina $(call if_changed_dep,bindgen) 3464e174665SAsahi Lina 3472f7ab126SMiguel Ojeda# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn 3482f7ab126SMiguel Ojeda# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here 3492f7ab126SMiguel Ojeda# given it is `libclang`; but for consistency, future Clang changes and/or 3502f7ab126SMiguel Ojeda# a potential future GCC backend for `bindgen`, we disable it too. 3512f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \ 3522f7ab126SMiguel Ojeda --blacklist-type '.*' --whitelist-var '' \ 3532f7ab126SMiguel Ojeda --whitelist-function 'rust_helper_.*' 3542f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \ 3552f7ab126SMiguel Ojeda -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations 3562f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \ 3572f7ab126SMiguel Ojeda sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@ 3582f7ab126SMiguel Ojeda$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE 3592f7ab126SMiguel Ojeda $(call if_changed_dep,bindgen) 3602f7ab126SMiguel Ojeda 3612f7ab126SMiguel Ojedaquiet_cmd_exports = EXPORTS $@ 3622f7ab126SMiguel Ojeda cmd_exports = \ 3632f7ab126SMiguel Ojeda $(NM) -p --defined-only $< \ 3642f7ab126SMiguel Ojeda | grep -E ' (T|R|D) ' | cut -d ' ' -f 3 \ 3652f7ab126SMiguel Ojeda | xargs -Isymbol \ 3662f7ab126SMiguel Ojeda echo 'EXPORT_SYMBOL_RUST_GPL(symbol);' > $@ 3672f7ab126SMiguel Ojeda 3682f7ab126SMiguel Ojeda$(obj)/exports_core_generated.h: $(obj)/core.o FORCE 3692f7ab126SMiguel Ojeda $(call if_changed,exports) 3702f7ab126SMiguel Ojeda 3712f7ab126SMiguel Ojeda$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE 3722f7ab126SMiguel Ojeda $(call if_changed,exports) 3732f7ab126SMiguel Ojeda 3742f7ab126SMiguel Ojeda$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE 3752f7ab126SMiguel Ojeda $(call if_changed,exports) 3762f7ab126SMiguel Ojeda 3772f7ab126SMiguel Ojeda$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE 3782f7ab126SMiguel Ojeda $(call if_changed,exports) 3792f7ab126SMiguel Ojeda 3802f7ab126SMiguel Ojedaquiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@ 3812f7ab126SMiguel Ojeda cmd_rustc_procmacro = \ 3822f7ab126SMiguel Ojeda $(RUSTC_OR_CLIPPY) $(rust_common_flags) \ 383295d8398SMasahiro Yamada --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \ 384295d8398SMasahiro Yamada --crate-type proc-macro \ 3852185242fSMasahiro Yamada --crate-name $(patsubst lib%.so,%,$(notdir $@)) $< 3862f7ab126SMiguel Ojeda 3872f7ab126SMiguel Ojeda# Procedural macros can only be used with the `rustc` that compiled it. 3882f7ab126SMiguel Ojeda# Therefore, to get `libmacros.so` automatically recompiled when the compiler 3892f7ab126SMiguel Ojeda# version changes, we add `core.o` as a dependency (even if it is not needed). 3902f7ab126SMiguel Ojeda$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE 3912f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_procmacro) 3922f7ab126SMiguel Ojeda 3932f7ab126SMiguel Ojedaquiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@ 3942f7ab126SMiguel Ojeda cmd_rustc_library = \ 3952f7ab126SMiguel Ojeda OBJTREE=$(abspath $(objtree)) \ 3962f7ab126SMiguel Ojeda $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ 3972f7ab126SMiguel Ojeda $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \ 398295d8398SMasahiro Yamada --emit=dep-info=$(depfile) --emit=obj=$@ \ 399295d8398SMasahiro Yamada --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \ 400295d8398SMasahiro Yamada --crate-type rlib -L$(objtree)/$(obj) \ 4012185242fSMasahiro Yamada --crate-name $(patsubst %.o,%,$(notdir $@)) $< \ 4022f7ab126SMiguel Ojeda $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) 4032f7ab126SMiguel Ojeda 4042f7ab126SMiguel Ojedarust-analyzer: 4052f7ab126SMiguel Ojeda $(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \ 4062f7ab126SMiguel Ojeda $(RUST_LIB_SRC) > $(objtree)/rust-project.json 4072f7ab126SMiguel Ojeda 408cb7d9defSGary Guoredirect-intrinsics = \ 409cb7d9defSGary Guo __eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \ 410cb7d9defSGary Guo __unorddf2 \ 411cb7d9defSGary Guo __muloti4 __multi3 \ 412cb7d9defSGary Guo __udivmodti4 __udivti3 __umodti3 413cb7d9defSGary Guo 414cb7d9defSGary Guoifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),) 415cb7d9defSGary Guo # These intrinsics are defined for ARM64 and RISCV64 416cb7d9defSGary Guo redirect-intrinsics += \ 417cb7d9defSGary Guo __ashrti3 \ 418cb7d9defSGary Guo __ashlti3 __lshrti3 419cb7d9defSGary Guoendif 420cb7d9defSGary Guo 4212f7ab126SMiguel Ojeda$(obj)/core.o: private skip_clippy = 1 4222f7ab126SMiguel Ojeda$(obj)/core.o: private skip_flags = -Dunreachable_pub 423cb7d9defSGary Guo$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym)) 4242f7ab126SMiguel Ojeda$(obj)/core.o: private rustc_target_flags = $(core-cfgs) 425c83b16ceSMasahiro Yamada$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE 4262f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_library) 4272f7ab126SMiguel Ojeda 4282f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' 4292f7ab126SMiguel Ojeda$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE 4302f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_library) 4312f7ab126SMiguel Ojeda 4322f7ab126SMiguel Ojeda$(obj)/alloc.o: private skip_clippy = 1 4332f7ab126SMiguel Ojeda$(obj)/alloc.o: private skip_flags = -Dunreachable_pub 4342f7ab126SMiguel Ojeda$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs) 4352f7ab126SMiguel Ojeda$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE 4362f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_library) 4372f7ab126SMiguel Ojeda 438ecaa6ddfSGary Guo$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE 439ecaa6ddfSGary Guo $(call if_changed_dep,rustc_library) 440ecaa6ddfSGary Guo 4412f7ab126SMiguel Ojeda$(obj)/bindings.o: $(src)/bindings/lib.rs \ 4422f7ab126SMiguel Ojeda $(obj)/compiler_builtins.o \ 4432f7ab126SMiguel Ojeda $(obj)/bindings/bindings_generated.rs \ 4442f7ab126SMiguel Ojeda $(obj)/bindings/bindings_helpers_generated.rs FORCE 4452f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_library) 4462f7ab126SMiguel Ojeda 4474e174665SAsahi Lina$(obj)/uapi.o: $(src)/uapi/lib.rs \ 4484e174665SAsahi Lina $(obj)/compiler_builtins.o \ 4494e174665SAsahi Lina $(obj)/uapi/uapi_generated.rs FORCE 4504e174665SAsahi Lina $(call if_changed_dep,rustc_library) 4514e174665SAsahi Lina 4522f7ab126SMiguel Ojeda$(obj)/kernel.o: private rustc_target_flags = --extern alloc \ 4534e174665SAsahi Lina --extern build_error --extern macros --extern bindings --extern uapi 454ecaa6ddfSGary Guo$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \ 4554e174665SAsahi Lina $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE 4562f7ab126SMiguel Ojeda $(call if_changed_dep,rustc_library) 4572f7ab126SMiguel Ojeda 4582f7ab126SMiguel Ojedaendif # CONFIG_RUST 459