sculblog 0.1.9

project xanadu revivalism
Documentation
use std::env;
use std::path::PathBuf;

fn main() {
    println!("cargo:rerun-if-changed=src/");
    println!("cargo:rerun-if-changed=vendor/");
    // dtob-sys emits cargo:rustc-link-lib=static=dtob, but Cargo doesn't
    // propagate it to the final link unless Rust symbols from dtob_sys are
    // directly referenced. Re-emit it here so the linker always sees -ldtob.
    println!("cargo:rustc-link-lib=static=dtob");

    let xdiff_dir = PathBuf::from("vendor/xdiff");
    if !xdiff_dir.join("xtypes.h").exists() {
        std::fs::create_dir_all(&xdiff_dir).unwrap();
        let files = [
            "xdiffi.c", "xprepare.c", "xutils.c", "xpatience.c", "xhistogram.c", "xemit.c", "xmerge.c",
            "xdiff.h", "xdiffi.h", "xemit.h", "xinclude.h", "xmacros.h", "xprepare.h", "xutils.h", "xtypes.h"
        ];
        for f in &files {
            let url = format!("https://raw.githubusercontent.com/git/git/master/xdiff/{}", f);
            let status = std::process::Command::new("curl")
                .args(["-fsSL", &url, "-o"])
                .arg(xdiff_dir.join(f))
                .status()
                .unwrap();
            if !status.success() {
                panic!("Failed to download {}", url);
            }
        }
    }

    // libdtob.a is compiled and linked by the dtob-sys crate (path = ../dtob,
    // `links = "dtob"`). Its build.rs emits `cargo:include=...`, so Cargo
    // hands us the header dir via DEP_DTOB_INCLUDE.
    let dtob_include = env::var("DEP_DTOB_INCLUDE")
        .expect("DEP_DTOB_INCLUDE not set — is dtob-sys in [dependencies]?");

    // Make sure to compile the remaining native deps (potc, xdiff, our FFI).
    // dtob itself is compiled and linked by dtob-sys.
    cc::Build::new()
        .include(&dtob_include)
        .include("vendor/dif")
        .include("vendor/potc/src")
        .include("vendor/xanadoc")
        .include("src/xdiff_compat")
        .include("vendor")
        .include("vendor/xdiff")
        // POTC (Bromberg)
        .file("vendor/potc/src/hash_matrix.c")
        .file("vendor/potc/src/lookup_table.c")
        .file("vendor/potc/src/bromberg_sl2.c")
        // Git libxdiff
        .file("vendor/xdiff/xdiffi.c")
        .file("vendor/xdiff/xprepare.c")
        .file("vendor/xdiff/xutils.c")
        .file("vendor/xdiff/xpatience.c")
        .file("vendor/xdiff/xhistogram.c")
        .file("vendor/xdiff/xemit.c")
        // Our FFI
        .file("src/sculblog_ffi.c")
        .compile("native_deps");

    let bindings = bindgen::Builder::default()
        .header("src/sculblog_ffi.h")
        .clang_arg(format!("-I{}", dtob_include))
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .generate()
        .expect("Unable to generate bindings");

    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}