casc 0.1.2

A cross-platform CLI tool for Blizzard CASC archives.
use std::env;
use std::path::PathBuf;

fn main() {
    // 1. Compile CascLib using CMake
    let mut config = cmake::Config::new("ext/CascLib");

    config
        .define("CASC_BUILD_STATIC_LIB", "ON")
        .define("CASC_BUILD_SHARED_LIB", "OFF")
        .define("CMAKE_POLICY_VERSION_MINIMUM", "3.5");

    // On Windows, we prefer to use the bundled zlib to avoid external dependencies.
    // On Linux and macOS, we prefer the system zlib for better compatibility and to avoid
    // build errors with the bundled (outdated) zlib code on newer OS versions.
    if cfg!(target_os = "windows") {
        config.define("CMAKE_DISABLE_FIND_PACKAGE_ZLIB", "TRUE");
    }

    let dst = config.build();

    // 2. Link the compiled library
    println!("cargo:rustc-link-search=native={}/lib", dst.display());
    println!("cargo:rustc-link-lib=static=casc");

    // Link C++ standard library and system zlib if used
    if cfg!(target_os = "linux") {
        println!("cargo:rustc-link-lib=dylib=stdc++");
        println!("cargo:rustc-link-lib=dylib=z");
    } else if cfg!(target_os = "macos") {
        println!("cargo:rustc-link-lib=dylib=c++");
        println!("cargo:rustc-link-lib=dylib=z");
    }

    // 3. Generate bindings using bindgen
    let mut builder = bindgen::Builder::default()
        .header("ext/CascLib/src/CascLib.h")
        // Force bindgen to treat the headers as C++ and use C++11 standard
        .clang_args(&["-x", "c++", "-std=c++11"])
        // CascLib uses some Windows types even on Linux (via CascPort.h)
        .clang_arg("-Iext/CascLib/src")
        // Blocklist IPPORT_RESERVED because it's redefined in multiple headers
        .blocklist_item("IPPORT_RESERVED")
        // Disable layout tests (size assertions) because they can fail on some
        // systems with complex glibc/Clang interactions.
        .layout_tests(false)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));

    // On macOS, we need to provide the SDK path to bindgen so it can find standard headers
    // when using a non-system libclang (e.g. from Homebrew).
    let target = env::var("TARGET").unwrap_or_default();
    if target.contains("apple-darwin") {
        let output = std::process::Command::new("xcrun")
            .args(["--show-sdk-path"])
            .output();

        match output {
            Ok(output) if output.status.success() => {
                let sdk_path = String::from_utf8_lossy(&output.stdout).trim().to_string();
                builder = builder.clang_arg("-isysroot").clang_arg(sdk_path);
            }
            _ => {}
        }
    }

    let bindings = builder.generate().expect("Unable to generate bindings");

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