3

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
rodrigocfd
  • 6,450
  • 6
  • 34
  • 68

1 Answers1

5

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:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thank you for your answer. I ended up with my current setup, which is simply a shell script with `cargo build` among the commands. Too bad that RFC was not accepted. – rodrigocfd Feb 19 '21 at 21:05
  • @rodrigocfd note that your shell script is likely less portable than the Rust solution (e.g. what happens on Windows?), which may or may not matter for your case. – Shepmaster Feb 19 '21 at 21:09
  • You're right, it's not *that* portable. In my particular situation, I'm on Windows using MinGW, which does understand shell scripts, so it's fine. Also, I have no plans to distribute this project. – rodrigocfd Feb 19 '21 at 21:16