Get header file generated with cbindgen from a dependency

I have a project that depends on a crate where a header file is generated with cbindgen. How can I get the path to this header file? I have external tools that need to include it.

I am using git dependencies in cargo. I can find the header file in two places:

  1. In cargo's global package registry/cache (for me it's ~/.local/share/cargo/git/checkouts/<my_repo>-<some_hash>/<commit_sha>/...)
  2. In the main project's target directory (if I use the OUT_DIR environment variable in the dependency's build script, it puts it in target/debug/build/<the_crate>-<some_hash>/out/include/header.h)

Is there any way I can get programmatic access to the header? I do not know how the hashes are generated, and even if I did I feel it's a hack to try to get the path to the files that way.

Is there another environment variable I can use to tell cbindgen to put the file somewhere else accessible?

Edit: I know I can use the extra_bindings option in cbindgen, but that puts all bindings in the same header, which is not desired.

It looks like --message-format=json will output all generated paths. It's a definitely a firehose of information, but I think it will get me what I need.

The following gives me the OUT_DIR of my dependency:

❯ cargo build --message-format=json | jq -r 'select(.reason | contains("build-script-executed")) | select(.package_id | contains("<my_crate>")) | .out_dir'

Edit: source: How to retrieve build artifact paths? · Issue #3757 · rust-lang/cargo · GitHub

1 Like

If a package defines unmangled symbols, it makes sense to set the package.links key. If a package sets this key, the buildscript can println cargo::metadata={KEY}={VAL} to make the environment variable DEP_{LINKS}_{KEY}={VAL} available to downstream crates.