libinjection 0.3.8

Rust bindings for libinjection
use std::env;
use std::fs::remove_dir_all;
use std::path::{Path, PathBuf};
use std::process::Command;

const LIBINJECTION_URL: &str = "https://github.com/libinjection/libinjection";
const LIBINJECTION_COMMIT: &str = "85e252a28981d479822092bad235bb75beb7edfa";
const BUILD_DIR_NAME: &str = "libinjection";

fn clone_libinjection(build_dir: &Path) {
    if !run(
        "git",
        &[
            "clone",
            LIBINJECTION_URL,
            build_dir.as_os_str().to_str().unwrap(),
        ],
        Path::new("/"),
    ) {
        panic!("unable to clone the repo");
    }

    if !run("git", &["reset", "--hard", LIBINJECTION_COMMIT], build_dir) {
        panic!("unable to checkout to the commit");
    }
}

fn run(cmd: &str, args: &[&str], cwd: &Path) -> bool {
    let output = Command::new(cmd)
        .args(args)
        .env("OUT_DIR", env::var("OUT_DIR").unwrap())
        .current_dir(cwd)
        .output()
        .unwrap();
    if output.status.success() {
        true
    } else {
        panic!("make error: {}", String::from_utf8_lossy(&output.stderr));
    }
}

fn main() {
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    let build_parent_dir = out_path.join(BUILD_DIR_NAME);

    let _ = remove_dir_all(build_parent_dir.as_path());

    clone_libinjection(build_parent_dir.as_path());

    if !run("bash", &["autogen.sh"], build_parent_dir.as_path()) {
        panic!("unable to run autogen.sh");
    }

    if !run("bash", &["configure"], build_parent_dir.as_path()) {
        panic!("unable to run configure");
    }

    if !run("make", &["-C", "src"], build_parent_dir.as_path()) {
        panic!("unable to make libinjection");
    }

    if !run(
        "ar",
        &[
            "-crs",
            "libinjection.a",
            "libinjection_sqli.o",
            "libinjection_html5.o",
            "libinjection_xss.o",
        ],
        build_parent_dir.join("src").as_path(),
    ) {
        panic!("unable to build static library");
    }

    println!("cargo:rustc-link-lib=static=injection");
    println!(
        "cargo:rustc-link-search={}",
        build_parent_dir.join("src").display()
    );

    let h_path = build_parent_dir.join("src/libinjection.h");
    let bindings = bindgen::Builder::default()
        .header(h_path.to_str().unwrap())
        .generate()
        .expect("Unable to generate bindings");

    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("unable to write bindings");
}