Skip to content

Commit

Permalink
add cargo runner support in order to work with cross
Browse files Browse the repository at this point in the history
  • Loading branch information
glehmann committed Jan 24, 2024
1 parent 19da72b commit 746d489
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 16 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::env;
use std::fs;
use std::io::Write;
use std::path;

fn main() {
println!("cargo:rerun-if-changed=build.rs");

// env::ARCH doesn't include full triplet, and AFAIK there isn't a nicer way of getting the full triplet
// (see lib.rs for the rest of this hack)
let out = path::PathBuf::from(env::var_os("OUT_DIR").expect("run within cargo"))
.join("current_target.txt");
let default_target = env::var("TARGET").expect("run as cargo build script");
let mut file = fs::File::create(out).unwrap();
file.write_all(default_target.as_bytes()).unwrap();
}
20 changes: 19 additions & 1 deletion src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,27 @@ impl CommandCargoExt for process::Command {
pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
let path = cargo_bin(name);
if path.is_file() {
Ok(process::Command::new(path))
if let Some(runner) = cargo_runner() {
let runner_vec: Vec<_> = runner.split(" ").collect();

Check failure

Code scanning / clippy

single-character string constant used as pattern Error

single-character string constant used as pattern
let mut cmd = process::Command::new(runner_vec[0]);
cmd.args(&runner_vec[1..]).arg(path);
Ok(cmd)
} else {
Ok(process::Command::new(path))
}
} else {
Err(CargoError::with_cause(NotFoundError { path }))
}
}

pub(crate) fn cargo_runner() -> Option<String> {
let runner_env = format!(
"CARGO_TARGET_{}_RUNNER",
CURRENT_TARGET.replace("-", "_").to_uppercase()

Check failure

Code scanning / clippy

single-character string constant used as pattern Error

single-character string constant used as pattern
);
std::env::var(runner_env).ok()
}

/// Error when finding crate binary.
#[derive(Debug)]
pub struct CargoError {
Expand Down Expand Up @@ -206,3 +221,6 @@ fn cargo_bin_str(name: &str) -> path::PathBuf {
.map(|p| p.into())
.unwrap_or_else(|| target_dir().join(format!("{}{}", name, env::consts::EXE_SUFFIX)))
}

/// The current process' target triplet.
pub const CURRENT_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/current_target.txt"));

0 comments on commit 746d489

Please sign in to comment.