Search code examples
rustcompilationrust-cargotoolchain

Can I perform a task after compilation is complete?


I know that build.rs can perform tasks before the program compilation starts, so I can prepare whatever I want.

What if there's a task to be performed after the compilation is complete, as some sort of cleanup? Is there any way to do such a thing?

As a simple example: before compilation I want rename a file from foo.txt to abc.txt for whatever reason. Then after the compilation terminates I want to rename it back to foo.txt.


Solution

  • No, there is nothing as of Rust 1.50. RFC #1777 — Add Cargo post-build scripts proposed this, but it was not accepted.

    In the meantime, some crates make their own local Cargo third-party commands to mimic this. Documentation of one style of this can be found in the cargo-xtask repository. The TL;DR form:

    1. Create a local binary crate that performs a build and whatever else you need.
    2. Add a Cargo alias to invoke that crate.
    3. Call your custom command: cargo xtask build.

    See also: