Installation

The easiest way to add the ninja files capability to your project is to create a rust project that consumes it.

Creating a configure executable

cargo new tools/configure

This would create a 'configure' binary application in the tools folder in your project. This can actually be anywhere, but assuming you have other folders and languages, we have put it 'out of the way', to not pollute the directory tool much.

Adding the dependencies

In your Cargo.toml add the below

[dependencies]
ninja-files = { version = "0.1.0" , features = ["format"]}

This adds the meta-package, with serialization included. This is required if you wish to write it to a file. However you can also write your own serializer if you require, nothing about the code will stop you, though you shouldn't need too.

Writing your main function

The barebones setup only requires the below In tools/configure/src/main.rs change to the below

fn main() {
    let ninja = FileBuilder::new().build().unwrap();
    let file = std::fs::File::create("build.ninja").unwrap();
    let _ = write_ninja_file(&ninja, file).unwrap();
}

Note there is no elegant error handling, crashing is most likely okay in a configure script, as we exit-out with an error

The above creates a empty ninja file, but it does create it.

This can be ran with cargo run -p configure which will compile the configure program

./configure in repository root

While the above works, it's not that intuitive for new project contributors. A general standard does exist in build systems. That is the ./configure script

Luckily, it's easy to setup a simple script that just calls the rust one.

Make the below file in your repositories root, calling it configure

#!/usr/bin/env -S cargo run --package configure

Make it executable chmod +x ./configure

Now users can run ./configure and have the build.ninja created.

Running ninja

As the above produces a build.ninja file, you can just run ninja in the project root, and it will rebuild anything out of date. Now the above sample doesn't list anything to build, so it won't do much yet