mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +00:00

* Refactor: Extract new crate `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Extract new mod `detect` for `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Extract `desired_targets` in crate `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Extract `detect::linux` in crate `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Extract `detect::macos` in crate `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Extract `detect::windows` in crate `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add new dep cfg-if v1.0.0 for `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Simplify mod declaration in `detect` using `cfg_if!` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Refactor: Simplify `detect_targets` using `cfg_if!` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add crate doc for `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Enable feature "macros" of tokio in `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Enable feature "io-util" of dep tokio Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Rm unused feature "io-util" & "macros" of dep tokio Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Set stdin & stderr to null in `get_target_from_rustc` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Improve doc of `get_desired_targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Improve `detect_targets_linux`: Run `ldd` with stdin set to null Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix potential panic in `windows::detect_alternative_targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * FIx fmt of `detect_targets_linux` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Do not re-export dep `detect-targets` in `crates/lib` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Fix typo in crate doc for `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Enable feature "macros" of tokio in dev mode Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Add example to crate doc of `detect-targets` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> * Improve API `get_desired_targets`: Take `Option<&str>` instead of `&Option<String>` Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com> Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
86 lines
2.2 KiB
Rust
86 lines
2.2 KiB
Rust
use crate::TARGET;
|
|
|
|
use std::process::{Output, Stdio};
|
|
|
|
use tokio::process::Command;
|
|
|
|
pub(super) async fn detect_targets_linux() -> Vec<String> {
|
|
let (abi, libc) = parse_abi_and_libc();
|
|
|
|
if let Libc::Glibc = libc {
|
|
// Glibc can only be dynamically linked.
|
|
// If we can run this binary, then it means that the target
|
|
// supports both glibc and musl.
|
|
return create_targets_str(&["gnu", "musl"], abi);
|
|
}
|
|
|
|
if let Ok(Output {
|
|
status: _,
|
|
stdout,
|
|
stderr,
|
|
}) = Command::new("ldd")
|
|
.arg("--version")
|
|
.stdin(Stdio::null())
|
|
.output()
|
|
.await
|
|
{
|
|
let libc_version = if let Some(libc_version) = parse_libc_version_from_ldd_output(&stdout) {
|
|
libc_version
|
|
} else if let Some(libc_version) = parse_libc_version_from_ldd_output(&stderr) {
|
|
libc_version
|
|
} else {
|
|
return vec![create_target_str("musl", abi)];
|
|
};
|
|
|
|
if libc_version == "gnu" {
|
|
return create_targets_str(&["gnu", "musl"], abi);
|
|
}
|
|
}
|
|
|
|
// Fallback to using musl
|
|
vec![create_target_str("musl", abi)]
|
|
}
|
|
|
|
fn parse_libc_version_from_ldd_output(output: &[u8]) -> Option<&'static str> {
|
|
let s = String::from_utf8_lossy(output);
|
|
if s.contains("musl libc") {
|
|
Some("musl")
|
|
} else if s.contains("GLIBC") {
|
|
Some("gnu")
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
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::Musl)
|
|
} else if let Some(libc_version) = last.strip_prefix("gnu") {
|
|
(libc_version, Libc::Glibc)
|
|
} else {
|
|
panic!("Unrecognized libc")
|
|
}
|
|
}
|
|
|
|
fn create_target_str(libc_version: &str, abi: &str) -> String {
|
|
let prefix = TARGET
|
|
.rsplit_once('-')
|
|
.expect("unwrap: TARGET always has a -")
|
|
.0;
|
|
|
|
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()
|
|
}
|