Skip to content

Commit

Permalink
Don't call cbindgen recursively
Browse files Browse the repository at this point in the history
To expand a crate, cbindgen calls cargo on that crate. cbindgen can be called from a build.rs
file. But if cargo is called again, it will also call cbindgen again and hence end up in an
endless recursion.

This commit makes sure that cbindgen isn't called again if it is already running.

You can verify this fix with this minimal example https://github.com/vmx/cbindgen-expand-bug

Fixes #347.
  • Loading branch information
vmx authored and emilio committed Aug 6, 2019
1 parent 3e82084 commit 65958a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bindgen/cargo/cargo_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ pub fn expand(
cmd.env("CARGO_TARGET_DIR", PathBuf::from(path).join("expanded"));
}

// Set this variable so that we don't call it recursively if we expand a crate that is using
// cbindgen
cmd.env("_CBINDGEN_IS_RUNNING", "1");

cmd.arg("rustc");
cmd.arg("--lib");
cmd.arg("--manifest-path");
Expand Down
7 changes: 7 additions & 0 deletions src/bindgen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ impl<'a> Parser<'a> {
fn parse_expand_crate(&mut self, pkg: &PackageRef) -> Result<(), Error> {
assert!(self.lib.is_some());

// If you want to expand the crate you run cbindgen on you might end up in an endless
// recursion if the cbindgen generation is triggered from build.rs. Hence don't run the
// expansion if the build was already triggered by cbindgen.
if std::env::var("_CBINDGEN_IS_RUNNING").is_ok() {
return Ok(());
}

let mod_parsed = {
if !self.cache_expanded_crate.contains_key(&pkg.name) {
let s = self
Expand Down

0 comments on commit 65958a5

Please sign in to comment.