cxx icon indicating copy to clipboard operation
cxx copied to clipboard

`cxx_build::bridges()` generates incorrect file tree when using custom builder crate

Open Enyium opened this issue 2 years ago • 0 comments

I wrote the crates foo and foo-builder (not really "foo"). A third-party crate will have foo as a dependency and foo-builder as a build dependency. In its build script, it calls into foo-builder.

  • foo contains Rust and C++ code as well as CXX bridges.
  • foo-builder contains a build script that shouldn't be unnecessarily duplicated in third-party crates.

In foo-builder's lib.rs, I tried this:

let build = cxx_build::bridges(bridge_file_iter);

But it leads to an incorrect directory structure in the target folder with following build errors. It's not usable.

As a workaround, I currently trick cxx_build::bridges() in the following way:

// Save.
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
let dir = env::current_dir().unwrap();

// Fake.
env::set_var("CARGO_PKG_NAME", "foo");
env::set_current_dir(&foo_pkg_dir).unwrap();

// Work.
let mut build = cxx_build::bridges(bridge_file_iter);
build.include(format!("{foo_pkg_dir}/.."));

// Restore.
env::set_var("CARGO_PKG_NAME", pkg_name);
env::set_current_dir(dir).unwrap();

// [Work with `build`...]

How could my use case be improved and supported so that third-party crates using both of my crates have no problems being compiled?


EDIT: Forgot build.include().

Enyium avatar Jan 24 '23 21:01 Enyium