Use #[cfg(any())] in lib.rs

This commit is contained in:
bjorn3
2021-08-22 12:11:23 +02:00
committed by Dan Gohman
parent 181585dc3a
commit ac00127984
2 changed files with 27 additions and 34 deletions
+23 -18
View File
@@ -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::<Vec<_>>();
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(", ")),
}
}
+4 -16
View File
@@ -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;