From 59d79d15730effc5a655f8ad6e8cc67c12e90800 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 29 Dec 2022 12:24:28 +1100 Subject: [PATCH] Refactor `detect-targets` (#636) * Enable dep guess_host_triple for any non-linux OS * Refactor: Rm `{windows, macos}::detect_target_*` * Refactor `detect_targets` Signed-off-by: Jiahao XU --- crates/detect-targets/Cargo.toml | 2 +- crates/detect-targets/src/detect.rs | 52 ++++++++++++--------- crates/detect-targets/src/detect/macos.rs | 11 ----- crates/detect-targets/src/detect/windows.rs | 11 ----- 4 files changed, 31 insertions(+), 45 deletions(-) diff --git a/crates/detect-targets/Cargo.toml b/crates/detect-targets/Cargo.toml index 6943e400..a3469b29 100644 --- a/crates/detect-targets/Cargo.toml +++ b/crates/detect-targets/Cargo.toml @@ -13,7 +13,7 @@ license = "Apache-2.0 OR MIT" tokio = { version = "1.23.0", features = ["rt", "process", "sync"], default-features = false } cfg-if = "1.0.0" -[target.'cfg(any(target_os = "macos", target_os = "windows"))'.dependencies] +[target.'cfg(not(target_os = "linux"))'.dependencies] guess_host_triple = "0.1.3" [dev-dependencies] diff --git a/crates/detect-targets/src/detect.rs b/crates/detect-targets/src/detect.rs index b2b859d0..4c1877fe 100644 --- a/crates/detect-targets/src/detect.rs +++ b/crates/detect-targets/src/detect.rs @@ -9,6 +9,8 @@ use std::{ use cfg_if::cfg_if; use tokio::process::Command; +use crate::TARGET; + cfg_if! { if #[cfg(target_os = "linux")] { mod linux; @@ -33,34 +35,40 @@ cfg_if! { /// Check [this issue](https://github.com/ryankurte/cargo-binstall/issues/155) /// for more information. pub async fn detect_targets() -> Vec { - if let Some(target) = get_target_from_rustc().await { - let mut v = vec![target]; + #[cfg(target_os = "linux")] + { + if let Some(target) = get_target_from_rustc().await { + let mut targets = vec![target]; + + if targets[0].contains("gnu") { + targets.push(targets[0].replace("gnu", "musl")); + } + + targets + } else { + linux::detect_targets_linux().await + } + } + + #[cfg(not(target_os = "linux"))] + { + let target = get_target_from_rustc().await.unwrap_or_else(|| { + guess_host_triple::guess_host_triple() + .unwrap_or(TARGET) + .to_string() + }); + + let mut targets = vec![target]; cfg_if! { - if #[cfg(target_os = "linux")] { - if v[0].contains("gnu") { - v.push(v[0].replace("gnu", "musl")); - } - } else if #[cfg(target_os = "macos")] { - v.extend(macos::detect_alternative_targets(&v[0])); + if #[cfg(target_os = "macos")] { + targets.extend(macos::detect_alternative_targets(&targets[0])); } else if #[cfg(target_os = "windows")] { - v.extend(windows::detect_alternative_targets(&v[0])); + targets.extend(windows::detect_alternative_targets(&targets[0])); } } - v - } else { - cfg_if! { - if #[cfg(target_os = "linux")] { - linux::detect_targets_linux().await - } else if #[cfg(target_os = "macos")] { - macos::detect_targets_macos() - } else if #[cfg(target_os = "windows")] { - windows::detect_targets_windows() - } else { - vec![TARGET.into()] - } - } + targets } } diff --git a/crates/detect-targets/src/detect/macos.rs b/crates/detect-targets/src/detect/macos.rs index ad5b1f0e..0c216981 100644 --- a/crates/detect-targets/src/detect/macos.rs +++ b/crates/detect-targets/src/detect/macos.rs @@ -1,6 +1,3 @@ -use crate::TARGET; -use guess_host_triple::guess_host_triple; - const AARCH64: &str = "aarch64-apple-darwin"; const X86: &str = "x86_64-apple-darwin"; const UNIVERSAL: &str = "universal-apple-darwin"; @@ -15,11 +12,3 @@ pub(super) fn detect_alternative_targets(target: &str) -> impl Iterator Vec { - let mut targets = vec![guess_host_triple().unwrap_or(TARGET).to_string()]; - - targets.extend(detect_alternative_targets(&targets[0])); - - targets -} diff --git a/crates/detect-targets/src/detect/windows.rs b/crates/detect-targets/src/detect/windows.rs index 4efd5f2c..04e86428 100644 --- a/crates/detect-targets/src/detect/windows.rs +++ b/crates/detect-targets/src/detect/windows.rs @@ -1,17 +1,6 @@ -use crate::TARGET; -use guess_host_triple::guess_host_triple; - pub(super) fn detect_alternative_targets(target: &str) -> Option { let (prefix, abi) = target.rsplit_once('-')?; // detect abi in ["gnu", "gnullvm", ...] (abi != "msvc").then(|| format!("{prefix}-msvc")) } - -pub(super) fn detect_targets_windows() -> Vec { - let mut targets = vec![guess_host_triple().unwrap_or(TARGET).to_string()]; - - targets.extend(detect_alternative_targets(&targets[0])); - - targets -}