feat detect-targets: Improve support of non-std glibc/musl (#1343)

* feat `detect-targets`: Improve support of non-std glibc/musl

Fixed #1329

 - Refactor: Create `linux::detect_alternative_targets` to reuse code
   from other targets
 - Run `/lib/ld-linux-{cpu_arch}.so.1 --version` for checking glibc
   support instead of running `ldd --version` since it could be non-std
   glibc installation and does not provide
   `/lib/ld-linux-{cpu_arch}.so.1`
 - Check for non-std glibc and add fallback target
   `{cpu_arch}-{distro_name}-linux-gnu{abi}`
 - Add `{cpu_arch}-{distro_name}-linux-musl{abi}` fallback for musl
   libc, specially for Alpine since it has a
   `/lib/ld-musl-{cpu_arch}.so.1`
 - For unknown libc flavor, check for the target provided before
   fallback to musl

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* feat `detect-targets`: Support glibc on musl target

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

* feat `detect-targets`: Unify `Libc::{Gnu, Musl}` checks

since we can't really tell if we are on gnu or musl

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>

---------

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-09-03 09:19:34 +10:00 committed by GitHub
parent 3e67e3624a
commit 0fa315758b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 124 additions and 69 deletions

View file

@ -32,43 +32,25 @@ cfg_if! {
/// Check [this issue](https://github.com/ryankurte/cargo-binstall/issues/155)
/// for more information.
pub async fn detect_targets() -> Vec<String> {
#[cfg(target_os = "linux")]
{
if let Some(target) = get_target_from_rustc().await {
let mut targets = vec![target];
let target = get_target_from_rustc().await.unwrap_or_else(|| {
guess_host_triple::guess_host_triple()
.unwrap_or(crate::TARGET)
.to_string()
});
if targets[0].contains("gnu") {
targets.push(targets[0].replace("gnu", "musl"));
} else if targets[0].contains("android") {
targets.push(targets[0].replace("android", "musl"));
}
let mut targets = vec![target];
targets
} else {
linux::detect_targets_linux().await
cfg_if! {
if #[cfg(target_os = "macos")] {
targets.extend(macos::detect_alternative_targets(&targets[0]).await);
} else if #[cfg(target_os = "windows")] {
targets.extend(windows::detect_alternative_targets(&targets[0]));
} else if #[cfg(target_os = "linux")] {
targets.extend(linux::detect_alternative_targets(&targets[0]).await);
}
}
#[cfg(not(target_os = "linux"))]
{
let target = get_target_from_rustc().await.unwrap_or_else(|| {
guess_host_triple::guess_host_triple()
.unwrap_or(crate::TARGET)
.to_string()
});
let mut targets = vec![target];
cfg_if! {
if #[cfg(target_os = "macos")] {
targets.extend(macos::detect_alternative_targets(&targets[0]).await);
} else if #[cfg(target_os = "windows")] {
targets.extend(windows::detect_alternative_targets(&targets[0]));
}
}
targets
}
targets
}
/// Figure out what the host target is using `rustc`.