Is there a way to cargo clean and keep target binaries?

Target directories are like GBs in size, and some times I want to build the binaries and ignore everything else. I know I can pick the binaries, but for some projects there are many targets and it could be tedious. Is there a way in cargo to clean and keep all the executables that are bound to some --bin in a workspace?

2 Likes

Never tried it myself, but can't you run cargo install --root my_binaries --path . to build all [[bin]] targets in the workspace and copy each binary to the ./my_binaries/bin folder?

5 Likes

I'm not sure I understand how this works. When I try this (whether in the workspace root or some bin crate root in the workspace) I get the error: error: no packages found with binaries or examples.

The command is broken down like so:

  • cargo install compiles one or more binary crates in release mode
  • --root my_binaries means the resulting binary will end up in the ./my_binaries directory
  • --path . is the path to the directory containing a Cargo.toml. If you have just run cargo build and that worked, then this is . (the current dir)

I think the only reason it would say that is if your current package doesn't have a bin crate, so not sure what's up with that.

Also, what does this mean?

Indeed, I researched a bit and cargo install is currently not supported for workspaces: #4101, #7124.

I don't know why the command doesn't work when executed in your member crates with binary targets present as this should definitely work.


As a quick and dirty hack around not being able to run cargo install in a workspace, here a little bash script that installs every binary from your workspace. It requires you to have the toml-cli tool present which can be installed by running cargo install toml-cli. If you have library crates this will print ugly errors to your console but it installs every binary into the workspace/bin directory nonetheless (at least locally when I tested it in a dummy workspace):

#!/bin/bash

# make sure workspace/bin directory is present
mkdir -p bin

# retrieve the member crates from the workspace manifest
IFS="," read -r -a MEMBERS <<< "$(toml get Cargo.toml workspace.members | sed 's/\"//g;s/\[//g;s/\]//g')"

# in case that the workspace is not virtual, try installing it
cargo install --path . --root .

# try installing member crates
for member in ${MEMBERS[@]}; do
  cargo install --path $member --root .
done

I'd recommend looking into tools like cargo dist that create distributable packages from your binaries

Thank you for your great effort, really, though I was hoping I wouldn't have to use custom scripts... otherwise I could do it myself :sweat_smile:... I was hoping cargo can at least clean unused link files and stuff.

Thanks for the hint. Even though cargo dist isn't really what I was looking for, but seems like an awesome little tool.

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.