How to generate crate before Cargo build?

Viewed 31

I want to use SBE codecs in Rust. I have a polyglot project which uses java and rust. Java is built by gradle, rust is built by cargo. I have a gradle task generateMessagesRust which generates a directory with src/lib.rs and the codec files. Generated crate is imported in Cargo.toml like this.

[dependencies]
ipc_connectivity = { path = "aeron-connectivity/build/generator/ipc_connectivity" }

Import works just fine if I manually call generateMessagesRust. But I want to automate it, and force Cargo call ./gradlew generateMessagesRust before it builds a project. In order to do this I created the following build.rs file:

use std::process::Command;

fn main() {
    let command = Command::new("./gradlew")
        .arg("generateMessagesRust")
        .spawn()
        .expect("Failed to run command");
    let output = command.wait_with_output().unwrap();
    println!("{}", output.status);
    println!("{}", String::from_utf8(output.stdout).unwrap());
}

However, it doesn't work.

Execution failed (exit code 101).
/home/vadim/.cargo/bin/cargo metadata --verbose --format-version 1 --all-features
stdout : error: failed to get `ipc_connectivity` as a dependency of package `dijkstra v0.1.0 (/home/vadim/IdeaProjects/myproj)`

Caused by:
  failed to load source for dependency `ipc_connectivity`

Caused by:
  Unable to update /home/vadim/IdeaProjects/myproj/aeron-connectivity/build/generator/ipc_connectivity

Caused by:
  failed to read `/home/vadim/IdeaProjects/myproj/aeron-connectivity/build/generator/ipc_connectivity/Cargo.toml`

Caused by:
  No such file or directory (os error 2)

stderr : 

I assume that Cargo tries to process Cargo.toml and only then evaluates build.rs. So it fails before generating the codecs.

My question is: how to generate codecs automatically before Cargo build?

0 Answers
Related