From ac00127984106ed42bb282941ca0749b24e7b892 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 22 Aug 2021 12:11:23 +0200 Subject: [PATCH] Use #[cfg(any())] in lib.rs --- gen/src/main.rs | 41 +++++++++++++++++++++++------------------ src/lib.rs | 20 ++++---------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/gen/src/main.rs b/gen/src/main.rs index aee97a6..0f07074 100644 --- a/gen/src/main.rs +++ b/gen/src/main.rs @@ -122,32 +122,29 @@ fn main() { // Define the module. If this isn't the default version, make it // conditional. - if DEFAULT_LINUX_VERSIONS + let default_arch_versions = DEFAULT_LINUX_VERSIONS .iter() - .any(|default| &default.1 == linux_version) - { - for default in &DEFAULT_LINUX_VERSIONS { - if &default.1 == linux_version { - let cfg_version = format!("#[cfg(target_arch = \"{}\")]", default.0); - writeln!(src_lib_rs, "{}", cfg_version).unwrap(); - writeln!(src_lib_rs, "pub mod {};", linux_version_mod).unwrap(); - } + .filter(|default| &default.1 == linux_version) + .map(|default| default.0) + .collect::>(); + if !default_arch_versions.is_empty() { + let mut cfg_versions = vec![]; + for arch in default_arch_versions { + cfg_versions.push(format!("target_arch = \"{}\"", arch)); } + writeln!(src_lib_rs, "{}", gen_cfg_any(&cfg_versions)).unwrap(); + writeln!(src_lib_rs, "pub mod {};", linux_version_mod).unwrap(); + + // If this is the default version for an architecture, make the + // contents available in the top-level namespace. + writeln!(src_lib_rs, "{}", gen_cfg_any(&cfg_versions)).unwrap(); + writeln!(src_lib_rs, "pub use {}::*;", linux_version_mod).unwrap(); } else { let cfg_version = format!("#[cfg(feature = \"{}\")]", linux_version_mod); writeln!(src_lib_rs, "{}", cfg_version).unwrap(); writeln!(src_lib_rs, "pub mod {};", linux_version_mod).unwrap(); } - // If this is the default version for an architecture, make the - // contents available in the top-level namespace. - for default in &DEFAULT_LINUX_VERSIONS { - if linux_version == &default.1 { - writeln!(src_lib_rs, "#[cfg(target_arch = \"{}\")]", default.0).unwrap(); - writeln!(src_lib_rs, "pub use {}::*;", linux_version_mod).unwrap(); - } - } - let src_vers = format!("../src/{}", linux_version_mod); fs::create_dir_all(&src_vers).unwrap(); let mut src_vers_mod_rs = File::create(&format!("{}/mod.rs", src_vers)).unwrap(); @@ -410,3 +407,11 @@ fn compute_clang_arch(rust_arch: &str) -> &str { rust_arch } } + +fn gen_cfg_any(cfgs: &[String]) -> String { + match &cfgs[..] { + [] => String::new(), + [cfg] => format!("#[cfg({})]", cfg), + cfgs => format!("#[cfg(any({}))]", cfgs.join(", ")), + } +} diff --git a/src/lib.rs b/src/lib.rs index 93ecbd4..4035638 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,17 +1,9 @@ #![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)] // The rest of this file is auto-generated! -#[cfg(target_arch = "x86")] +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "powerpc"))] pub mod v2_6_32; -#[cfg(target_arch = "x86_64")] -pub mod v2_6_32; -#[cfg(target_arch = "powerpc")] -pub mod v2_6_32; -#[cfg(target_arch = "x86")] -pub use v2_6_32::*; -#[cfg(target_arch = "x86_64")] -pub use v2_6_32::*; -#[cfg(target_arch = "powerpc")] +#[cfg(any(target_arch = "x86", target_arch = "x86_64", target_arch = "powerpc"))] pub use v2_6_32::*; #[cfg(target_arch = "arm")] pub mod v3_2; @@ -25,13 +17,9 @@ pub use v3_10::*; pub mod v4_2; #[cfg(target_arch = "aarch64")] pub use v4_2::*; -#[cfg(target_arch = "mips")] +#[cfg(any(target_arch = "mips", target_arch = "mips64"))] pub mod v4_4; -#[cfg(target_arch = "mips64")] -pub mod v4_4; -#[cfg(target_arch = "mips")] -pub use v4_4::*; -#[cfg(target_arch = "mips64")] +#[cfg(any(target_arch = "mips", target_arch = "mips64"))] pub use v4_4::*; #[cfg(target_arch = "riscv64")] pub mod v4_20;