physfs-sys 0.0.0

PhysicsFS is a library to provide abstract access to various archives.
Documentation
use std::env;
use std::path::{Path, PathBuf};

use bindgen::{Builder, CargoCallbacks};
use cmake::Config;
//use walkdir::WalkDir;

fn build() -> PathBuf {
    Config::new("physfs/")
        .define("PHYSFS_BUILD_STATIC", "ON")
        .define("PHYSFS_BUILD_SHARED", "OFF")
        .define("PHYSFS_BUILD_TEST", "OFF")
        .define(
            "PHYSFS_ARCHIVE_ZIP",
            if cfg!(feature = "PHYSFS_ARCHIVE_ZIP") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_7Z",
            if cfg!(feature = "PHYSFS_ARCHIVE_7Z") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_GRP",
            if cfg!(feature = "PHYSFS_ARCHIVE_GRP") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_WAD",
            if cfg!(feature = "PHYSFS_ARCHIVE_WAD") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_HOG",
            if cfg!(feature = "PHYSFS_ARCHIVE_HOG") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_MVL",
            if cfg!(feature = "PHYSFS_ARCHIVE_MVL") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_QPAK",
            if cfg!(feature = "PHYSFS_ARCHIVE_QPAK") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_SLB",
            if cfg!(feature = "PHYSFS_ARCHIVE_SLB") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_VDF",
            if cfg!(feature = "PHYSFS_ARCHIVE_VDF") {
                "ON"
            } else {
                "OFF"
            },
        )
        .define(
            "PHYSFS_ARCHIVE_ISO9660",
            if cfg!(feature = "PHYSFS_ARCHIVE_ISO9660") {
                "ON"
            } else {
                "OFF"
            },
        )
        .build()
}

fn link(link_path: &Path) {
    println!("cargo:rustc-link-search=native={}", link_path.display());
    if link_path.join("libphysfs.a").exists() {
        println!("cargo:rustc-link-lib=static=physfs");
    } else {
        panic!("Could not find libphysfs.a");
    }
}

fn bind(include_path: &Path, out_path: &Path) {
    Builder::default()
        .header(include_path.join("physfs.h").to_str().unwrap())
        .parse_callbacks(Box::new(CargoCallbacks))
        .generate()
        .expect("Couldn't generate bindings")
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings");
}

fn main() {
    let physfs = build();

    let include_path = physfs.as_path().join("include/");
    let link_path = physfs.as_path().join("lib/");
    let out_path = env::var("OUT_DIR").unwrap();
    let out_path = Path::new(&out_path);

    println!("cargo:include={}", include_path.display());

    link(&link_path);

    bind(&include_path, &out_path);
}