saja 0.1.0

Zero-configuration C build system
use std::{env, path::PathBuf, process::Command};

fn run(command: &mut Command, context: &str) {
    let status = command.status().unwrap_or_else(|_| panic!("{context}"));

    if !status.success() {
        panic!("{context}");
    }
}

fn main() {
    let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());

    let plugin_dir = PathBuf::from("plugin");
    let source = plugin_dir.join("saja.cpp");
    let build_dir = out_dir.join("plugin-build");

    println!("cargo:rerun-if-changed={}", source.display());
    println!("cargo:rerun-if-changed=plugin/CMakeLists.txt");

    run(
        Command::new("cmake")
            .arg("-S")
            .arg("plugin")
            .arg("-B")
            .arg(&build_dir)
            .arg("-DCMAKE_BUILD_TYPE=Release")
            .arg(format!(
                "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={}",
                out_dir.display()
            )),
        "failed to configure the Clang plugin",
    );

    run(
        Command::new("cmake")
            .arg("--build")
            .arg(&build_dir)
            .arg("--target")
            .arg("saja"),
        "failed to build the Clang plugin",
    );
}