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 <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-12-29 12:24:28 +11:00 committed by GitHub
parent 0ecb30dc9d
commit 59d79d1573
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 45 deletions

View file

@ -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]

View file

@ -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<String> {
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
}
}

View file

@ -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<Item = S
.flatten()
.map(ToString::to_string)
}
pub(super) fn detect_targets_macos() -> Vec<String> {
let mut targets = vec![guess_host_triple().unwrap_or(TARGET).to_string()];
targets.extend(detect_alternative_targets(&targets[0]));
targets
}

View file

@ -1,17 +1,6 @@
use crate::TARGET;
use guess_host_triple::guess_host_triple;
pub(super) fn detect_alternative_targets(target: &str) -> Option<String> {
let (prefix, abi) = target.rsplit_once('-')?;
// detect abi in ["gnu", "gnullvm", ...]
(abi != "msvc").then(|| format!("{prefix}-msvc"))
}
pub(super) fn detect_targets_windows() -> Vec<String> {
let mut targets = vec![guess_host_triple().unwrap_or(TARGET).to_string()];
targets.extend(detect_alternative_targets(&targets[0]));
targets
}