Skip to content

Commit

Permalink
Allow CARGO_TARGET_DIR override in cargo_expand
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Falcon authored and eqrion committed Nov 9, 2017
1 parent a0068c6 commit b6b7351
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/bindgen/cargo/cargo_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,39 @@ pub fn expand(manifest_path: &Path,
crate_name: &str,
version: &str) -> Result<String, String> {
let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
let run = |target_dir: &Path| {
let mut cmd = Command::new(cargo);
cmd.env("CARGO_TARGET_DIR", target_dir);
cmd.arg("rustc");
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
cmd.arg("--all-features");
cmd.arg("-p");
cmd.arg(&format!("{}:{}", crate_name, version));
cmd.arg("--");
cmd.arg("-Z");
cmd.arg("unstable-options");
cmd.arg("--pretty=expanded");
let output = cmd.output().unwrap();

// Create a temp directory to use as a target dir for cargo expand, for
// hygenic purposes.
let target_dir = TempDir::new("cbindgen-expand")
.map_err(|_| format!("couldn't create a temp target directory"))?;
let src = from_utf8(&output.stdout).unwrap().to_owned();
let error = from_utf8(&output.stderr).unwrap().to_owned();

let mut cmd = Command::new(cargo);
cmd.env("CARGO_TARGET_DIR", target_dir.path());
cmd.arg("rustc");
cmd.arg("--manifest-path");
cmd.arg(manifest_path);
cmd.arg("--all-features");
cmd.arg("-p");
cmd.arg(&format!("{}:{}", crate_name, version));
cmd.arg("--");
cmd.arg("-Z");
cmd.arg("unstable-options");
cmd.arg("--pretty=expanded");
let output = cmd.output().unwrap();
if src.len() == 0 {
Err(error)
} else {
Ok(src)
}
};

let src = from_utf8(&output.stdout).unwrap().to_owned();
let error = from_utf8(&output.stderr).unwrap().to_owned();

if src.len() == 0 {
Err(error)
if let Ok(ref path) = env::var("CARGO_EXPAND_TARGET_DIR") {
run(&Path::new(path))
} else {
Ok(src)
// Create a temp directory to use as a target dir for cargo expand, for
// hygenic purposes.
let target_dir = TempDir::new("cbindgen-expand")
.map_err(|_| format!("couldn't create a temp target directory"))?;

run(target_dir.path())
}
}

0 comments on commit b6b7351

Please sign in to comment.