rodrigocfd
rodrigocfd

Reputation: 8052

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.

Upvotes: 4

Views: 5259

Answers (2)

Tim Boudreau
Tim Boudreau

Reputation: 1781

A simple solution to this, if you have a workspace project:

  • Create a new cargo project in your workspace next to the one you want a post-build script for - say thingamabob_post_build
  • Set a dependency in the new project on the one you want a post-build script for
  • Put a build.rs in the new project
  • Have it do whatever you want to be sure is done after the other project is built

Upvotes: 1

Shepmaster
Shepmaster

Reputation: 430961

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:

Upvotes: 7

Related Questions