Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there an scriptable way to get the final binary which cargo run uses? #7895

Closed
fogti opened this issue Feb 17, 2020 · 7 comments
Closed
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@fogti
Copy link

fogti commented Feb 17, 2020

I want to run the program built by cargo build [--release] in a loop, but I haven't found a way to get the path the produced binary "programmatically" (e.g. automatable).
I have multiple developing machines with differing target-dir's, thus, on some machines, the target dir would be ./target/, but on others it would be /mnt/path/to/target.x64/ or something like that, which makes the easier approach (just using target/release/$binname) unusable. Running cargo run [--release] in a loop is currently not a good solution because each call introduces an overhead and additional output which is (at least) annoying.

  • Is there already a way to get the produced binary path "programmatically"?
  • Or a way to prevent cargo run from trying to rebuild the program every time + suppress the Finished message?
@fogti fogti added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Feb 17, 2020
@fogti
Copy link
Author

fogti commented Feb 17, 2020

@ehuss
Copy link
Contributor

ehuss commented Feb 17, 2020

cargo run --message-format=json will emit "compiler-artifact" messages with the path to the binary (docs).

Alternatively, you can run cargo metadata and look at the "target_directory" field to know which target directory is used. You can also scan the packages binary targets if you don't know the names. docs

#3670 tracks some ideas for future enhancements.

@fogti
Copy link
Author

fogti commented Feb 17, 2020

Ok. Is this (cargo metadata) documented somewhere? I think it should be mentioned in the .cargo/config page linked above.

@fogti fogti closed this as completed Feb 17, 2020
@fogti
Copy link
Author

fogti commented Feb 17, 2020

@ehuss Thanks.

@fogti
Copy link
Author

fogti commented Feb 17, 2020

for future reference, working version using cargo metadata: https://github.com/fogti/crulz-rs/blob/a8b9c124e1ac5d878f563a17b416fa326167eae0/run_examples.sh

@hoijui
Copy link

hoijui commented Dec 22, 2023

The previous link gives us the output ("target") directory. If you know the binary name, you can manually append it to that path like it is done there. if not, you can get the binary name from cargo metadata too, as explained here:
https://users.rust-lang.org/t/what-is-the-best-way-to-get-the-name-of-the-binary-that-cargo-will-produce/17907/4

combined:

TARGET_DIR="$(cargo metadata --format-version 1 --no-deps | jq -r '.target_directory')"
BIN_NAME="$(cargo metadata --format-version 1 --no-deps | jq -r '.packages[].targets[] | select( .kind | map(. == "bin") | any ) | .name')"
BINARY_PATH="$TARGET_DIR/release/$BIN_NAME"

@wr7
Copy link

wr7 commented Aug 31, 2024

For anyone else that finds this on google, here's a script that directly uses the output from cargo build:

#!/bin/sh

json=$(cargo build --message-format=json-render-diagnostics "$@") && \
printf "%s" "$json" | jq -js '[.[] | select(.reason == "compiler-artifact") | select(.executable != null)] | last | .executable'

Additional arguments for cargo build are forwarded from the script's arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

4 participants