xdelta3 0.1.5

Rust binding for xdelta3
extern crate bindgen;
extern crate cc;

use std::env;
use std::process::Command;

use rand::Rng;
use std::fs::{remove_file, File};
use std::io::Write;
use std::path::PathBuf;

fn add_def(v: &mut Vec<(String, String)>, key: &str, val: &str) {
    v.push((key.to_owned(), val.to_owned()));

fn main() {
    let mut defines = Vec::new();
    for i in &[
        "unsigned int",
        "unsigned long",
        "unsigned long long",
    ] {
        let def_name = format!("SIZEOF_{}", i.to_uppercase().replace(" ", "_"));
        defines.push((def_name, check_native_size(i)));
    add_def(&mut defines, "SECONDARY_DJW", "1");
    add_def(&mut defines, "SECONDARY_FGK", "1");
    add_def(&mut defines, "EXTERNAL_COMPRESSION", "0");
    add_def(&mut defines, "XD3_USE_LARGEFILE64", "1");

    add_def(&mut defines, "XD3_WIN32", "1");
    add_def(&mut defines, "SHELL_TESTS", "0");

        let mut builder = cc::Build::new();
        for (key, val) in &defines {
            builder.define(&key, Some(val.as_str()));


        let mut builder = bindgen::Builder::default();

        for (key, val) in &defines {
            builder = builder.clang_arg(format!("-D{}={}", key, val));
        let bindings = builder
            .expect("Unable to generate bindings");

        // Write the bindings to the $OUT_DIR/bindings.rs file.
        let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
            .expect("Couldn't write bindings!");

fn check_native_size(name: &str) -> String {
    let builder = cc::Build::new();
    let out_dir = env::var("OUT_DIR").unwrap();
    let compiler = builder.get_compiler();
    let mut compile = Command::new(compiler.path().as_os_str());
    let test_code = format!("#include <stdint.h>\n#include <stdio.h>\nint main() {{printf(\"%lu\", sizeof({})); return 0;}}\n", name);
    // didn't use tempfile since tempfile was having issues on Windows
    let mut rng = rand::thread_rng();
    let test_binary_fn = format!("{}/test-{}", out_dir, rng.gen::<i32>());

    let test_binary_fn = format!("{}.exe", test_binary_fn);

    let test_source_fn = format!("{}/test-{:x}.c", out_dir, rng.gen::<i32>());
    let mut test_source = File::create(&test_source_fn).expect("Error creating test compile files");

    if compiler.is_like_msvc() {
        compile.args(&[&test_source_fn, &format!("/Fe{}", test_binary_fn)]);
    } else {
        compile.args(&[&test_source_fn, "-o", &test_binary_fn]);
        .expect("Error writing test compile files");
    drop(test_source); // close the source file, otherwise there will be problems on Windows
    for &(ref a, ref b) in compiler.env().iter() {
        compile.env(a, b);
    compile.output().expect("Error compiling test source");

    compile = Command::new(&test_binary_fn);
    let output = compile
        .expect("Error executing test binary")
    let output = String::from_utf8(output).expect("Error converting Unicode sequence");
    return output;