I'm trying to access certain vars make distcheck generates, or where my build.rs is currently being called from. I'm trying to access my config.h file from my C project to check some things:
Everything works in my CI apart from this VPATH stage, as I'm obviously using hardcoded paths in my build.rs. I've just tried this:
eprintln!("Where are we? {:?}", env::var("CARGO_MANIFEST_DIR").unwrap());
but get:
--- stderr
Where are we? "/home/ghenry/src/sentrypeer/sentrypeer-4.0.0/sentrypeer_rust"
thread 'main' panicked at build.rs:35:58:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
make[2]: *** [Makefile:2850: /home/ghenry/src/sentrypeer/sentrypeer-4.0.0/_build/sub/sentrypeer_rust/target/release/libsentrypeer_rust.a]
where is not the VPATH dir.
I'm porting/extending my C project to Rust, using Rust as a lib and my Rust lib re-uses some code from my C project by calling it as a lib.
Any ideas how I can find my C source code? Maybe me parsing my autoconf generated config.h file by hand isn't the best way and that's my problem. cbindgen does then go on to complain if I hardcode a path, as it then can't resolve other includes.
Upon inspecting the environment we have we can see CARGO_TARGET_DIR contains /home/ghenry/src/sentrypeer/sentrypeer-4.0.0/_build/sub/sentrypeer_rust/target
so let's go for that:
let target_dir = PathBuf::from(env::var("CARGO_TARGET_DIR").unwrap());
let opendht =
std::fs::read_to_string(target_dir.join("../../")
.join("config.h")).unwrap();
which works! But now we get:
--- stderr
./../src/conf.h:21:10: fatal error: '../config.h' file not found
thread 'main' panicked at build.rs:60:10:
Unable to generate bindings: ClangDiagnostic("./../src/conf.h:21:10:
fatal error: '../config.h' file not found\n")