google/autocxx

autocxx_build::Builder::new() can only be called once in build.rs

Closed this issue · 2 comments

Describe the bug
To avoid a single bridge file grow too big, I want to create more bridge files separately, each generates some rust bindings with provided c++ header files. Then in my cargo repo build.rs, I will write my code like this:

let mut pb_build = autocxx_build::Builder::new("src/pb.rs", 
        &[&src_path, &log_path, &base_path])
        .extra_clang_args(&["-std=c++17"])
        .build()
        .expect("Failed to create C++ builder for pb.rs");

let mut pc_build = autocxx_build::Builder::new("src/pc.rs", 
        &[&src_path, &log_path, &base_path])
        .extra_clang_args(&["-std=c++17"])
        .build()
        .expect("Failed to create C++ builder for pc.rs");

Then I got LogSetError(), basically it is because in Builder::init it is setting up env_logger, and this cannot be initialized twice.

To Reproduce
Call autocxx_build::Builder::new twice in build.rs under a cargo repo and run cargo build.

Expected behavior
I am not sure whether this is a bug or not. If this is not the recommended way to manage interface, then is there a way to build multiple bridge files in a cargo project? Alternatively, it is also acceptable to only include one bridge file, but provide a way to make the ffi module public, therefore we are able to modularize our project and use the generated bindings in other parts of modules, instead of limiting them in only one file. Currently ffi is a private field and inaccessible.

Additional context
Please share the recommend way to expose generated rust bindings and modularize my project.

It's a bug.

You could try

pub mod ffi_pub {
  use ffi::*;
}

I don't know if that will work?

It's a bug.

You could try

pub mod ffi_pub {
  use ffi::*;
}

I don't know if that will work?

cool, working now!