Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make running cbindgen with own create expansion from build.rs #371

Merged
merged 2 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/bindgen/cargo/cargo_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::env;
use std::error;
use std::fmt;
use std::io;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::{from_utf8, Utf8Error};

Expand Down Expand Up @@ -75,8 +75,18 @@ pub fn expand(
cmd.env("CARGO_TARGET_DIR", _temp_dir.unwrap().path());
} else if let Ok(ref path) = env::var("CARGO_EXPAND_TARGET_DIR") {
cmd.env("CARGO_TARGET_DIR", path);
} else if let Ok(ref path) = env::var("OUT_DIR") {
// When cbindgen was started programatically from a build.rs file, Cargo is running and
// locking the default target directory. In this case we need to use another directory,
// else we would end up in a deadlock. If Cargo is running `OUT_DIR` will be set, so we
// can use a directory relative to that.
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");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hacky... Though I don't have better ideas.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's super hacky, but it's also the best idea I had.


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