Search code examples
rustrust-cargo

Can Cargo run external tools before compilation?


Is it possible to have cargo run external tools before compiling the code?

The exact problem that I am having:

I have a parser generator which is created using re2rust. This parser generator takes a file as an input and generates rust code (a source file). I need to run the parser generator before compiling the core. Ideally only if the original file was modified.

I can't find cargo docs on how to do this.


Solution

  • You could use a build.rs script. Docs are here but the gist for your use case would be something like

    fn main() {
        println!("cargo:rerun-if-changed=path/to/template");
        Command::new("re2rust")
            .arg(/*whatever*/)
            .output()
            .expect("failed to execute process")
    }