Skip to content

Commit 65958a5

Browse files
vmxemilio
authored andcommitted
Don't call cbindgen recursively
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 mozilla#347.
1 parent 3e82084 commit 65958a5

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/bindgen/cargo/cargo_expand.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ pub fn expand(
8383
cmd.env("CARGO_TARGET_DIR", PathBuf::from(path).join("expanded"));
8484
}
8585

86+
// Set this variable so that we don't call it recursively if we expand a crate that is using
87+
// cbindgen
88+
cmd.env("_CBINDGEN_IS_RUNNING", "1");
89+
8690
cmd.arg("rustc");
8791
cmd.arg("--lib");
8892
cmd.arg("--manifest-path");

src/bindgen/parser.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ impl<'a> Parser<'a> {
179179
fn parse_expand_crate(&mut self, pkg: &PackageRef) -> Result<(), Error> {
180180
assert!(self.lib.is_some());
181181

182+
// If you want to expand the crate you run cbindgen on you might end up in an endless
183+
// recursion if the cbindgen generation is triggered from build.rs. Hence don't run the
184+
// expansion if the build was already triggered by cbindgen.
185+
if std::env::var("_CBINDGEN_IS_RUNNING").is_ok() {
186+
return Ok(());
187+
}
188+
182189
let mod_parsed = {
183190
if !self.cache_expanded_crate.contains_key(&pkg.name) {
184191
let s = self

0 commit comments

Comments
 (0)