Add get_targets_from_rustc to detect_targets

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-06 23:00:33 +10:00
parent 8bf4d187ee
commit cce378e2c5
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -1,4 +1,7 @@
use arrayvec::ArrayVec;
use std::io::{BufRead, Cursor};
use std::process::Output;
use tokio::process::Command;
/// Compiled target triple, used as default for binary fetching
pub const TARGET: &str = env!("TARGET");
@ -17,6 +20,10 @@ pub const TARGET: &str = env!("TARGET");
/// Check [this issue](https://github.com/ryankurte/cargo-binstall/issues/155)
/// for more information.
pub async fn detect_targets() -> ArrayVec<Box<str>, 2> {
if let Some(target) = get_targets_from_rustc().await {
return from_array([target]);
}
#[cfg(target_os = "linux")]
{
linux::detect_targets_linux().await
@ -31,6 +38,20 @@ pub async fn detect_targets() -> ArrayVec<Box<str>, 2> {
}
}
// Figure out what the host target is, from rustc or from this program's own build target
async fn get_targets_from_rustc() -> Option<Box<str>> {
match Command::new("rustc").arg("-vV").output().await {
Ok(Output { status, stdout, .. }) if status.success() => Cursor::new(stdout)
.lines()
.filter_map(|line| line.ok())
.find_map(|line| {
line.strip_prefix("host: ")
.map(|host| host.to_owned().into_boxed_str())
}),
_ => None,
}
}
fn from_array<T, const LEN: usize, const CAP: usize>(arr: [T; LEN]) -> ArrayVec<T, CAP> {
let mut v = ArrayVec::new();
@ -43,9 +64,7 @@ fn from_array<T, const LEN: usize, const CAP: usize>(arr: [T; LEN]) -> ArrayVec<
#[cfg(target_os = "linux")]
mod linux {
use super::{from_array, ArrayVec, TARGET};
use std::process::Output;
use tokio::process::Command;
use super::{from_array, ArrayVec, Command, Output, TARGET};
pub(super) async fn detect_targets_linux() -> ArrayVec<Box<str>, 2> {
let abi = parse_abi();