use anyhow::{Context, Result};
use std::process::Command;
pub fn rustc_version() -> Result<String> {
let output = Command::new("rustc")
.arg("--version")
.output()
.context("failed to run rustc --version")?;
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
pub fn target_triple() -> Result<String> {
let output = Command::new("rustc")
.args(["-vV"])
.output()
.context("failed to run rustc -vV")?;
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if let Some(rest) = line.strip_prefix("host: ") {
return Ok(rest.trim().to_string());
}
}
for line in stdout.lines() {
if let Some(rest) = line.strip_prefix("host = ") {
return Ok(rest.trim().to_string());
}
}
for line in stdout.lines() {
if let Some(rest) = line.strip_prefix("Target: ") {
return Ok(rest.trim().to_string());
}
}
if let Some(first) = stdout.lines().next() {
if first.contains('-') {
if let Some(triple) = first.split_whitespace().find(|s| s.contains('-')) {
return Ok(triple.to_string());
}
}
}
if let Ok(env_triple) = std::env::var("RUSTC_HOST_TRIPLE") {
return Ok(env_triple);
}
Ok("x86_64-unknown-linux-gnu".to_string())
}