diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c3999afb0213da..dcbd0c6a9cc2c8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,9 +13,11 @@ jobs: steps: # Setup - uses: actions/checkout@v2 + - run: sudo apt update - run: sudo apt install libelf-dev qemu-system-x86 busybox-static - run: rustup default nightly-2020-08-27 - run: rustup component add rust-src + - run: cargo install bindgen # Build - run: cp .github/workflows/kernel-${{ matrix.mode }}.config .config diff --git a/Cargo.lock b/Cargo.lock index c358a6a4fbdca9..40a62a03811b10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,263 +1,23 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "aho-corasick" -version = "0.7.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86" -dependencies = [ - "memchr", -] - -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi", -] - -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi", -] - -[[package]] -name = "bindgen" -version = "0.54.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66c0bb6167449588ff70803f4127f0684f9063097eca5016f37eb52b92c2cf36" -dependencies = [ - "bitflags", - "cexpr", - "cfg-if", - "clang-sys", - "clap", - "env_logger", - "lazy_static", - "lazycell", - "log", - "peeking_take_while", - "proc-macro2", - "quote", - "regex", - "rustc-hash", - "shlex 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", - "which", -] - [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" -[[package]] -name = "cc" -version = "1.0.59" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381" - -[[package]] -name = "cexpr" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27" -dependencies = [ - "nom", -] - -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - -[[package]] -name = "clang-sys" -version = "0.29.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe6837df1d5cba2397b835c8530f51723267e16abbf83892e9e5af4f0e5dd10a" -dependencies = [ - "glob", - "libc", - "libloading", -] - -[[package]] -name = "clap" -version = "2.33.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" -dependencies = [ - "ansi_term", - "atty", - "bitflags", - "strsim", - "textwrap", - "unicode-width", - "vec_map", -] - -[[package]] -name = "env_logger" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - -[[package]] -name = "glob" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574" - -[[package]] -name = "hermit-abi" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" -dependencies = [ - "libc", -] - -[[package]] -name = "humantime" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" -dependencies = [ - "quick-error", -] - [[package]] name = "kernel" version = "0.1.0" dependencies = [ - "bindgen", "bitflags", "module", - "shlex 0.1.1", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "lazycell" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" - -[[package]] -name = "libc" -version = "0.2.76" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" - -[[package]] -name = "libloading" -version = "0.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753" -dependencies = [ - "cc", - "winapi", -] - -[[package]] -name = "log" -version = "0.4.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "memchr" -version = "2.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" - [[package]] name = "module" version = "0.1.0" -[[package]] -name = "nom" -version = "5.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af" -dependencies = [ - "memchr", - "version_check", -] - -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - -[[package]] -name = "proc-macro2" -version = "1.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" -dependencies = [ - "unicode-xid", -] - -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - -[[package]] -name = "quote" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "regex" -version = "1.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", - "thread_local", -] - -[[package]] -name = "regex-syntax" -version = "0.6.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8" - [[package]] name = "rust_example" version = "0.1.0" @@ -265,115 +25,6 @@ dependencies = [ "kernel", ] -[[package]] -name = "rustc-hash" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" - -[[package]] -name = "shlex" -version = "0.1.1" - [[package]] name = "shlex" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" - -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "termcolor" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", -] - -[[package]] -name = "thread_local" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "unicode-width" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" - -[[package]] -name = "unicode-xid" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" - -[[package]] -name = "vec_map" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" - -[[package]] -name = "version_check" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" - -[[package]] -name = "which" -version = "3.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" -dependencies = [ - "libc", -] - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Makefile b/Makefile index 8307664d39015b..00afa89771a213 100644 --- a/Makefile +++ b/Makefile @@ -450,6 +450,7 @@ STRIP = $(CROSS_COMPILE)strip endif RUSTC = rustc CARGO = cargo +BINDGEN = bindgen PAHOLE = pahole RESOLVE_BTFIDS = $(objtree)/tools/bpf/resolve_btfids/resolve_btfids LEX = flex @@ -510,11 +511,10 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \ KBUILD_CPPFLAGS := -D__KERNEL__ KBUILD_RUSTCFLAGS := # TODO: a simple way to update `Cargo.lock` when we add a new driver -# TODO: another option is using explicit target specs, e.g. -# `--target=$(srctree)/arch/$(SRCARCH)/rust-target-spec.json` KBUILD_CARGOFLAGS := $(CARGO_VERBOSE) --locked \ - -Z build-std=core,alloc -Z unstable-options \ - --out-dir=out --target=x86_64-linux-kernel + -Z build-std=core,alloc \ + -Z unstable-options \ + --target=$(srctree)/arch/$(SRCARCH)/rust/target.json KBUILD_AFLAGS_KERNEL := KBUILD_CFLAGS_KERNEL := KBUILD_RUSTCFLAGS_KERNEL := @@ -528,7 +528,7 @@ export KBUILD_LDS_MODULE := $(srctree)/scripts/module-common.lds KBUILD_LDFLAGS := CLANG_FLAGS := -export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC CARGO +export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC CARGO BINDGEN export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL export PERL PYTHON PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD @@ -728,7 +728,7 @@ $(KCONFIG_CONFIG): # This exploits the 'multi-target pattern rule' trick. # The syncconfig should be executed only once to make all the targets. # (Note: use the grouped target '&:' when we bump to GNU Make 4.3) -%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG) +%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rust_cfg: $(KCONFIG_CONFIG) $(Q)$(MAKE) -f $(srctree)/Makefile syncconfig else # !may-sync-config # External modules and some install targets need include/generated/autoconf.h diff --git a/arch/x86/rust/target.json b/arch/x86/rust/target.json new file mode 100644 index 00000000000000..92ffaebf59c9a5 --- /dev/null +++ b/arch/x86/rust/target.json @@ -0,0 +1,34 @@ +{ + "arch": "x86_64", + "code-model": "kernel", + "cpu": "x86-64", + "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", + "disable-redzone": true, + "eliminate-frame-pointer": false, + "env": "gnu", + "features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float", + "function-sections": false, + "is-builtin": true, + "linker-flavor": "gcc", + "linker-is-gnu": true, + "llvm-target": "x86_64-elf", + "max-atomic-width": 64, + "needs-plt": true, + "os": "none", + "panic-strategy": "abort", + "position-independent-executables": true, + "pre-link-args": { + "gcc": [ + "-Wl,--as-needed", + "-Wl,-z,noexecstack", + "-m64" + ] + }, + "relocation-model": "static", + "relro-level": "full", + "stack-probes": true, + "target-c-int-width": "32", + "target-endian": "little", + "target-pointer-width": "64", + "vendor": "unknown" +} diff --git a/rust/kernel/Cargo.toml b/rust/kernel/Cargo.toml index fca60a4635ca14..bd116a6fc7db48 100644 --- a/rust/kernel/Cargo.toml +++ b/rust/kernel/Cargo.toml @@ -11,7 +11,3 @@ publish = false bitflags = "1" module = { path = "../module" } -[build-dependencies] -bindgen = "0.54" -shlex = { path = "../shlex" } - diff --git a/rust/kernel/build.rs b/rust/kernel/build.rs deleted file mode 100644 index 0897186b3f9241..00000000000000 --- a/rust/kernel/build.rs +++ /dev/null @@ -1,134 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -use std::env; -use std::path::PathBuf; - -const INCLUDED_TYPES: &[&str] = &["file_system_type", "mode_t", "umode_t", "ctl_table"]; -const INCLUDED_FUNCTIONS: &[&str] = &[ - "cdev_add", - "cdev_init", - "cdev_del", - "register_filesystem", - "unregister_filesystem", - "krealloc", - "kfree", - "mount_nodev", - "kill_litter_super", - "register_sysctl", - "unregister_sysctl_table", - "access_ok", - "_copy_to_user", - "_copy_from_user", - "alloc_chrdev_region", - "unregister_chrdev_region", - "wait_for_random_bytes", - "get_random_bytes", - "rng_is_initialized", - "printk", - "add_device_randomness", -]; -const INCLUDED_VARS: &[&str] = &[ - "EINVAL", - "ENOMEM", - "ESPIPE", - "EFAULT", - "EAGAIN", - "__this_module", - "FS_REQUIRES_DEV", - "FS_BINARY_MOUNTDATA", - "FS_HAS_SUBTYPE", - "FS_USERNS_MOUNT", - "FS_RENAME_DOES_D_MOVE", - "BINDINGS_GFP_KERNEL", - "KERN_INFO", - "VERIFY_WRITE", - "LINUX_VERSION_CODE", - "SEEK_SET", - "SEEK_CUR", - "SEEK_END", - "O_NONBLOCK", - "param_ops_bool", - "param_ops_int", -]; -const OPAQUE_TYPES: &[&str] = &[ - // These need to be opaque because they're both packed and aligned, which rustc - // doesn't support yet. See https://github.com/rust-lang/rust/issues/59154 - // and https://github.com/rust-lang/rust-bindgen/issues/1538 - "desc_struct", - "xregs_state", -]; - -// Takes the CFLAGS from the kernel Makefile and changes all the include paths to be absolute -// instead of relative. -fn prepare_cflags(cflags: &str, kernel_dir: &str) -> Vec { - let cflag_parts = shlex::split(&cflags).unwrap(); - let mut cflag_iter = cflag_parts.iter(); - let mut kernel_args = vec![]; - while let Some(arg) = cflag_iter.next() { - // TODO: bindgen complains - if arg.starts_with("-Wp,-MMD") { - continue; - } - - if arg.starts_with("-I") && !arg.starts_with("-I/") { - kernel_args.push(format!("-I{}/{}", kernel_dir, &arg[2..])); - } else if arg == "-include" { - kernel_args.push(arg.to_string()); - let include_path = cflag_iter.next().unwrap(); - if include_path.starts_with('/') { - kernel_args.push(include_path.to_string()); - } else { - kernel_args.push(format!("{}/{}", kernel_dir, include_path)); - } - } else { - kernel_args.push(arg.to_string()); - } - } - kernel_args -} - -fn main() { - println!("cargo:rerun-if-env-changed=CC"); - println!("cargo:rerun-if-env-changed=RUST_BINDGEN_CFLAGS"); - - let kernel_dir = "../../"; - let cflags = env::var("RUST_BINDGEN_CFLAGS").expect("Must be invoked from kernel makefile"); - - let kernel_args = prepare_cflags(&cflags, &kernel_dir); - - let target = env::var("TARGET").unwrap(); - - let mut builder = bindgen::Builder::default() - .use_core() - .ctypes_prefix("c_types") - .derive_default(true) - .size_t_is_usize(true) - .rustfmt_bindings(true); - - builder = builder.clang_arg(format!("--target={}", target)); - for arg in kernel_args.iter() { - builder = builder.clang_arg(arg.clone()); - } - - println!("cargo:rerun-if-changed=src/bindings_helper.h"); - builder = builder.header("src/bindings_helper.h"); - - for t in INCLUDED_TYPES { - builder = builder.whitelist_type(t); - } - for f in INCLUDED_FUNCTIONS { - builder = builder.whitelist_function(f); - } - for v in INCLUDED_VARS { - builder = builder.whitelist_var(v); - } - for t in OPAQUE_TYPES { - builder = builder.opaque_type(t); - } - let bindings = builder.generate().expect("Unable to generate bindings"); - - let out_path = PathBuf::from("src/bindings_gen.rs"); - bindings - .write_to_file(out_path) - .expect("Couldn't write bindings!"); -} diff --git a/rust/kernel/src/bindings.rs b/rust/kernel/src/bindings.rs index cfb004a6d7861c..198f49db07cedd 100644 --- a/rust/kernel/src/bindings.rs +++ b/rust/kernel/src/bindings.rs @@ -9,7 +9,7 @@ )] mod bindings_raw { use crate::c_types; - include!("bindings_gen.rs"); + include!(env!("RUST_BINDGEN_FILE")); } pub use bindings_raw::*; diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 9b50f2e0202608..b32748ff0d8e70 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -307,16 +307,22 @@ $(obj)/%.lst: $(src)/%.c FORCE # --------------------------------------------------------------------------- quiet_cmd_cargo = CARGO $(quiet_modtag) $@ - cmd_cargo = cd $(src) && $(CARGO) build $(cargo_flags) + cmd_cargo = export RUST_BINDGEN_FILE=$(shell readlink -f rust_bindings.rs) && $(CARGO) build -p $(shell basename $(basename $(src))) --out-dir $(src) $(cargo_flags) + +rust_bindings.rs: FORCE + $(Q)$(BINDGEN) $(srctree)/rust/kernel/src/bindings_helper.h --no-rustfmt-bindings --opaque-type xregs_state --opaque-type desc_struct --use-core --ctypes-prefix c_types -o rust_bindings.rs --size_t-is-usize -- $(c_flags) # The .o from the Rust staticlib -$(obj)/%.o: $(src)/out/lib%.a +#$(obj)/%.o: $(src)/out/lib%.a +$(obj)/%.o: $(obj)/lib%.a $(obj)/%.a $(Q)$(LD) -r -o $@ --whole-archive $< # The Rust staticlib from cargo -$(obj)/out/lib%.a: FORCE +$(obj)/%.a: rust_bindings.rs FORCE $(call cmd,cargo) +$(obj)/lib%.a: + # Compile assembler sources (.S) # --------------------------------------------------------------------------- diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib index daf920fa95512f..84ee690948ab32 100644 --- a/scripts/Makefile.lib +++ b/scripts/Makefile.lib @@ -229,9 +229,11 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ RUST_BINDGEN_CFLAGS = $(c_flags) $(KBUILD_CFLAGS_MODULE) export RUST_BINDGEN_CFLAGS -rustc_cfg_flags = $(shell sed -nE 's/^(CONFIG_[^=]+)=(y|m)$$/--cfg \1/p' $(srctree)/include/config/auto.conf | xargs) +KCONFIG_RUSTCONFIG ?= include/generated/rust_cfg -rustc_flags = $(_rustc_flags) $(modkern_rustcflags) $(rustc_cfg_flags) +rustconfig_path = $(shell readlink -f $(KCONFIG_RUSTCONFIG)) + +rustc_flags = $(_rustc_flags) $(modkern_rustcflags) @$(rustconfig_path) # Passed by cargo RUSTFLAGS = $(rustc_flags) diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index a39d93e3c6ae8d..bf17f68b7a697c 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -636,6 +636,55 @@ static struct conf_printer kconfig_printer_cb = .print_comment = kconfig_print_comment, }; +/* + * Rust configuration printer + * + * This printer is used when generating the resulting rustc configuration + * after kconfig invocation and `defconfig` files. + * + */ +static void +rconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg) +{ + const char *str; + + switch (sym->type) { + case S_INT: + case S_HEX: + case S_BOOLEAN: + case S_TRISTATE: + str = sym_escape_string_value(value); + if (*value == 'n') { + /* rust cfg does not support comments + bool skip_unset = (arg != NULL); + + if (!skip_unset) + fprintf(fp, "# %s%s is not set\n", + CONFIG_, sym->name);*/ + return; + } + break; + default: + str = value; + break; + } + + fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, str); +} + +static void +rconfig_print_comment(FILE *fp, const char *value, void *arg) +{ + fprintf(stderr, "could not print commend\n"); + return; +} + +static struct conf_printer rconfig_printer_cb = +{ + .print_symbol = rconfig_print_symbol, + .print_comment = rconfig_print_comment, +}; + /* * Header printer * @@ -1043,7 +1092,7 @@ int conf_write_autoconf(int overwrite) struct symbol *sym; const char *name; const char *autoconf_name = conf_get_autoconfig_name(); - FILE *out, *out_h; + FILE *out, *out_h, *out_r; int i; if (!overwrite && is_present(autoconf_name)) @@ -1064,6 +1113,13 @@ int conf_write_autoconf(int overwrite) return 1; } + out_r = fopen(".tmprconfig", "w"); + if (!out_r) { + fclose(out); + fclose(out_h); + return 1; + } + conf_write_heading(out, &kconfig_printer_cb, NULL); conf_write_heading(out_h, &header_printer_cb, NULL); @@ -1075,9 +1131,11 @@ int conf_write_autoconf(int overwrite) /* write symbols to auto.conf and autoconf.h */ conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1); conf_write_symbol(out_h, sym, &header_printer_cb, NULL); + conf_write_symbol(out_r, sym, &rconfig_printer_cb, NULL); } fclose(out); fclose(out_h); + fclose(out_r); name = getenv("KCONFIG_AUTOHEADER"); if (!name) @@ -1087,6 +1145,14 @@ int conf_write_autoconf(int overwrite) if (rename(".tmpconfig.h", name)) return 1; + name = getenv("KCONFIG_RUSTCONFIG"); + if (!name) + name = "include/generated/rust_cfg"; + if (make_parent_dir(name)) + return 1; + if (rename(".tmprconfig", name)) + return 1; + if (make_parent_dir(autoconf_name)) return 1; /*