libmimalloc-sys-ms 0.2.0

MiMalloc Rust Bindings
Documentation
use std::env;
use std::path::Path;

fn main() {
    // Based on `libmimalloc-sys`
    let mut build = cc::Build::new();

    let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let include_dir = Path::new(&cargo_manifest_dir)
        .join("upstream/")
        .join("include")
        .to_str()
        .expect("include path is not valid UTF-8")
        .to_string();

    // Make the include directory available to consumers via the `DEP_MIMALLOC_INCLUDE_DIR`
    // environment variable.
    println!("cargo:INCLUDE_DIR={include_dir}");

    build.include(format!("upstream/include"));
    build.include(format!("upstream/src"));
    build.file(format!("upstream/src/static.c"));

    let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
    let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!");
    let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!");

    if target_family != "windows" {
        build.flag("-Wno-error=date-time");
    }

    if build.get_compiler().is_like_msvc() {
        build.cpp(true);
    }

    build.compile("mimalloc");

    // on armv6 we need to link with libatomic
    if target_os == "linux" && target_arch == "arm" {
        // Embrace the atomic capability library across various platforms.
        // For instance, on certain platforms, llvm has relocated the atomic of the arm32 architecture to libclang_rt.builtins.a
        // while some use libatomic.a, and others use libatomic_ops.a.
        let atomic_name = env::var("DEP_ATOMIC").unwrap_or("atomic".to_owned());
        println!("cargo:rustc-link-lib={}", atomic_name);
    }

    // Link with libs needed on Windows
    if target_os == "windows" {
        // https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487
        let libs = ["psapi", "shell32", "user32", "advapi32", "bcrypt"];

        for lib in libs {
            println!("cargo:rustc-link-lib={}", lib);
        }
    }
}