rust-randomwow 0.1.1

Rust interface to the RandomWOW hash function (Wownero)
Documentation
use cmake::Config;
use std::{env, fs, path::Path};

fn main() {
    println!("cargo:rerun-if-changed=config/configuration.h");
    println!("cargo:rerun-if-changed=RandomX");

    let out_dir = env::var("OUT_DIR").unwrap();
    let source_copy = Path::new(&out_dir).join("randomwow-src");

    if source_copy.exists() {
        fs::remove_dir_all(&source_copy).unwrap();
    }
    copy_dir("RandomX", &source_copy);

    fs::copy(
        "config/configuration.h",
        source_copy.join("src/configuration.h"),
    )
    .unwrap();

    let dst = Config::new(&source_copy).define("DARCH", "native").build();

    println!("cargo:rustc-link-search=native={}/lib", dst.display());
    println!("cargo:rustc-link-search=native={}/build", dst.display());
    println!("cargo:rustc-link-lib=static=randomx");

    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
    let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();

    match target_os.as_str() {
        "openbsd" | "bitrig" | "netbsd" | "macos" | "ios" => {
            println!("cargo:rustc-link-lib=dylib=c++");
        }
        "windows" => {
            println!("cargo:rustc-link-lib=advapi32");
            if target_env == "gnu" {
                println!("cargo:rustc-link-lib=dylib=stdc++");
            }
        }
        _ => {
            println!("cargo:rustc-link-lib=dylib=stdc++");
        }
    }
}

fn copy_dir(source: impl AsRef<Path>, destination: impl AsRef<Path>) {
    let source = source.as_ref();
    let destination = destination.as_ref();
    fs::create_dir_all(destination).unwrap();
    for entry in fs::read_dir(source).unwrap() {
        let entry = entry.unwrap();
        let target = destination.join(entry.file_name());
        if entry.file_type().unwrap().is_dir() {
            copy_dir(entry.path(), target);
        } else {
            fs::copy(entry.path(), target).unwrap();
        }
    }
}