#cargo-build #cargo #utility #cargo-subcommand #cargo-toml #manifest-file #plugin

bin+lib cargo-exe

A Cargo plugin to print the path to the output executable from cargo build

2 stable releases

1.0.1 Apr 21, 2022

#531 in Cargo plugins

Apache-2.0

13KB
199 lines

Cargo-EXE

Crates.io docs.rs

This small utility command is an extension for Cargo to print the path to the output of cargo build. It is intended primarily for use in shell scripts, but may also be useful as a general purpose command.

Installation

With Cargo installed, the following command will build and install cargo-exe from crates.io automatically:

$ cargo install cargo-exe

As long as $HOME/.cargo/bin/ is included in $PATH, the subcommand should be available immediately.

Usage

The simplest usage is to run the subcommand with no arguments. This returns the path to the Debug build.

$ cargo exe
./target/debug/binary

With the --release flag, the Release build will be returned instead.

$ cargo exe --release
./target/release/binary

If the --latest flag is enabled, the program will actually search through everything in ./target, and print the path to the most recently modified binary it can find. This may be especially helpful in a script that attempts to perform further operations on an executable after building it:

#!/bin/bash
cargo build "$@" && mv $(cargo exe --latest) ~/.local/bin

If a path is provided as an argument, either to a Cargo.toml file or to a directory containing one, that project will be used instead of the one in the current directory.

$ cargo exe /devel/rust/project
/devel/rust/project/target/debug/project

Finally, the -f option allows manual specification of the file name to search for. This bypasses the need to determine the executable name by reading a manifest file. In addition to searching for the latest version of any arbitrary file involved in the build, this enables generating paths to builds of projects that do not exist.

This may or may not have any practical application.

$ cargo exe /root/aaa -f fake.exe
/root/aaa/target/debug/fake.exe

Dependencies

~3.5MB
~70K SLoC