From 7997c73cb228716866ac2774b5945d74b69f57ea Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 4 Aug 2022 17:35:24 +1000 Subject: [PATCH 1/3] Optimize: Avoid spawning `ldd` if built with Glibc Signed-off-by: Jiahao XU --- src/target.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/target.rs b/src/target.rs index 8dfb75b6..b1cd8f48 100644 --- a/src/target.rs +++ b/src/target.rs @@ -133,7 +133,20 @@ mod linux { use super::{Command, Output, TARGET}; pub(super) async fn detect_targets_linux() -> Vec { - 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 vec![ + create_target_str("gnu", abi), + create_target_str("musl", abi), + ] + } + _ => (), + } if let Ok(Output { status: _, @@ -173,13 +186,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; 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") { - libc_version + (libc_version, Libc::Glibc) } else { panic!("Unrecognized libc") } From a686aca08c93fb8ff33cb9a40658e9b2cae83136 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 4 Aug 2022 17:38:27 +1000 Subject: [PATCH 2/3] Refactor: Extract `target::linux::create_targets_str` Signed-off-by: Jiahao XU --- src/target.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/target.rs b/src/target.rs index b1cd8f48..dd83aa5d 100644 --- a/src/target.rs +++ b/src/target.rs @@ -139,12 +139,7 @@ mod linux { // 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 vec![ - create_target_str("gnu", abi), - create_target_str("musl", abi), - ] - } + Libc::Glibc => return create_targets_str(&["gnu", "musl"], abi), _ => (), } @@ -164,10 +159,7 @@ mod linux { }; if libc_version == "gnu" { - return vec![ - create_target_str("gnu", abi), - create_target_str("musl", abi), - ]; + return create_targets_str(&["gnu", "musl"], abi); } } @@ -211,6 +203,13 @@ mod linux { format!("{prefix}-{libc_version}{abi}") } + + fn create_targets_str(libc_versions: &[&str], abi: &str) -> Vec { + libc_version + .iter() + .map(|libc_version| create_target_str(libc_version, abi)) + .collect() + } } #[cfg(target_os = "macos")] From d472e8054b8141cdec4ac6bb8183e35a42e7f13d Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 4 Aug 2022 17:46:26 +1000 Subject: [PATCH 3/3] Fix typo in `target::linux::create_targets_str` Signed-off-by: Jiahao XU --- src/target.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/target.rs b/src/target.rs index dd83aa5d..c6392c23 100644 --- a/src/target.rs +++ b/src/target.rs @@ -205,7 +205,7 @@ mod linux { } fn create_targets_str(libc_versions: &[&str], abi: &str) -> Vec { - libc_version + libc_versions .iter() .map(|libc_version| create_target_str(libc_version, abi)) .collect()