Cargo: Locating target artifacts with environment variables

I'm using cargo-make to build a project and I need to run a post-build step to copy the final built binary to another directory to be able to test it. Is there some kind of environment variable that will point me to either the absolute path to the build artifact or the absolute path to the directory where cargo placed the artifact (e.g. where the binary is)?

Unfortunately not. There's cargo metadata JSON that you can use to find paths of built executables.

If you can launch your tests via Cargo from tests/ directory, then there's an CARGO_BIN_EXE_<name> env var for executables.

I'm surprised that isn't a feature. I'd think that'd be already available. I can't seem to find it in the cargo crate, either...

You can use cargo install --root foo to build and copy the executable to the foo dir.

My binary isn't one that you can use cargo install on. I don't think, anyway. Or does any binary qualify?

cargo install installs all bin targets from a crate.

However, I think it's meant for installing development tools, not merely another way to build binaries. Currently it's relatively primitive, but there were plans to extend cargo install into a more advanced package manager, which could complicate this workaround.

cargo install --root doesn't work anyway; it says:

error: Using `cargo install` to install the binaries for the package in current working directory is no longer supported, use `cargo install --path .` instead. Use `cargo build` if you want to simply build the package.

Which is not what I want to do. I either want to:

  1. Detect the output directory where binaries are placed; or
  2. Detect the target triplet and build type, and then manually construct the target path that way.

You need to parse paths out of the metadata cargo_metadata — Rust-lang // Lib.rs

I looked at the metadata of my crate and nowhere does it list build type or the actual target directory (it just points me to the directory called "target" and not beyond it).

Sorry, my memory has failed me. Indeed it requires building paths manually based on target_directory and target name.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.