Merge pull request #278 from cargo-bins/optimize

Optimize `target::linux::detect_target_linux`
This commit is contained in:
Jiahao XU 2022-08-04 18:23:04 +10:00 committed by GitHub
commit 64e948f941
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -133,7 +133,15 @@ mod linux {
use super::{Command, Output, TARGET}; use super::{Command, Output, TARGET};
pub(super) async fn detect_targets_linux() -> Vec<String> { pub(super) async fn detect_targets_linux() -> Vec<String> {
let abi = parse_abi(); let (abi, libc) = parse_abi_and_libc();
match libc {
// Glibc can only be dynamically linked.
// If we can run this binary, then it means that the target
// supports both glibc and musl.
Libc::Glibc => return create_targets_str(&["gnu", "musl"], abi),
_ => (),
}
if let Ok(Output { if let Ok(Output {
status: _, status: _,
@ -151,10 +159,7 @@ mod linux {
}; };
if libc_version == "gnu" { if libc_version == "gnu" {
return vec![ return create_targets_str(&["gnu", "musl"], abi);
create_target_str("gnu", abi),
create_target_str("musl", abi),
];
} }
} }
@ -173,13 +178,18 @@ mod linux {
} }
} }
fn parse_abi() -> &'static str { enum Libc {
Glibc,
Musl,
}
fn parse_abi_and_libc() -> (&'static str, Libc) {
let last = TARGET.rsplit_once('-').unwrap().1; let last = TARGET.rsplit_once('-').unwrap().1;
if let Some(libc_version) = last.strip_prefix("musl") { if let Some(libc_version) = last.strip_prefix("musl") {
libc_version (libc_version, Libc::Musl)
} else if let Some(libc_version) = last.strip_prefix("gnu") { } else if let Some(libc_version) = last.strip_prefix("gnu") {
libc_version (libc_version, Libc::Glibc)
} else { } else {
panic!("Unrecognized libc") panic!("Unrecognized libc")
} }
@ -193,6 +203,13 @@ mod linux {
format!("{prefix}-{libc_version}{abi}") format!("{prefix}-{libc_version}{abi}")
} }
fn create_targets_str(libc_versions: &[&str], abi: &str) -> Vec<String> {
libc_versions
.iter()
.map(|libc_version| create_target_str(libc_version, abi))
.collect()
}
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]