From 7997c73cb228716866ac2774b5945d74b69f57ea Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 4 Aug 2022 17:35:24 +1000 Subject: [PATCH] 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") }