Skip to content

Commit

Permalink
feat(xtask-surun): add check for CARGO_BUILD_TARGET env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
banditopazzo authored and vadorovsky committed Sep 26, 2024
1 parent bf75973 commit df0deed
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions xtask/src/surun.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use clap::Parser;
use xshell::{cmd, Shell};

Expand All @@ -13,15 +13,35 @@ impl SuRunCommand {
pub fn run(&self) -> Result<()> {
let cargo = std::env::var("CARGO").unwrap();

// To determine the target triple it checks in order:
// - `--target` command line option
// - `CARGO_BUILD_TARGET` environment variable
// - default host target
//
// TODO: it should check also hierarchical `config.toml` files as described
// in the following page:
// https://doc.rust-lang.org/cargo/reference/config.html#hierarchical-structure
let target_triple = match self
.run_args
.iter()
.skip_while(|arg| *arg != "--target")
.nth(1) // skip the `--target` identifier
{
Some(target_triple) => target_triple.to_owned(),
None => get_default_target(&cargo)
.context(format!("failed to get target triple with {cargo}"))?,
None => {
const TARGET_TRIPLE_ENV: &str = "CARGO_BUILD_TARGET";

match std::env::var(TARGET_TRIPLE_ENV) {
Ok(target_triple) => target_triple,
Err(std::env::VarError::NotPresent) => {
get_default_target(&cargo)
.context(format!("failed to get target triple with {cargo}"))?
}
Err(std::env::VarError::NotUnicode(var)) => {
bail!("env variable `{TARGET_TRIPLE_ENV}` doesn't contain a valid unicode: {var:?}")
}
}
}
};

log::debug!("Detected host triple: {target_triple}");
Expand Down

0 comments on commit df0deed

Please sign in to comment.