use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

fn resolve_mimalloc_root() -> PathBuf {
    if let Ok(root) = env::var("MIMALLOC_SRC") {
        return PathBuf::from(root);
    }

    let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set");
    let manifest_dir = Path::new(&cargo_manifest_dir);

    let flat_root = manifest_dir.join("c_src/mimalloc");
    if flat_root.join("include").is_dir() {
        return flat_root;
    }

    panic!(
        "mimalloc source not found. Set MIMALLOC_SRC or run `git submodule update --init --recursive` to populate c_src/mimalloc (https://github.com/acking-you/mimalloc)."
    );
}

fn emit_rerun_if_changed_tree(path: &Path) {
    if path.is_file() {
        println!("cargo:rerun-if-changed={}", path.display());
        return;
    }

    let entries = fs::read_dir(path)
        .unwrap_or_else(|error| panic!("failed to read {}: {error}", path.display()));
    for entry in entries {
        let entry = entry.unwrap_or_else(|error| {
            panic!(
                "failed to read directory entry under {}: {error}",
                path.display()
            )
        });
        emit_rerun_if_changed_tree(&entry.path());
    }
}

fn main() {
    let mut build = cc::Build::new();

    println!("cargo:rerun-if-env-changed=MIMALLOC_SRC");

    let root = resolve_mimalloc_root();
    let include_dir = root
        .join("include")
        .to_str()
        .expect("include path is not valid UTF-8")
        .to_string();
    let src_dir = root.join("src");
    let static_file = src_dir.join("static.c");
    if !src_dir.is_dir() || !static_file.is_file() {
        panic!(
            "mimalloc source is missing src/static.c in {}",
            root.display()
        );
    }
    emit_rerun_if_changed_tree(&root.join("include"));
    emit_rerun_if_changed_tree(&src_dir);
    // Make the include directory available to consumers via the `DEP_MIMALLOC_INCLUDE_DIR`
    // environment variable.
    println!("cargo:INCLUDE_DIR={include_dir}");

    build.include(&include_dir);
    build.include(&src_dir);
    build.file(&static_file);

    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_vendor = env::var("CARGO_CFG_TARGET_VENDOR").expect("target_vendor 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 env::var_os("CARGO_FEATURE_OVERRIDE").is_some() {
        // Overriding malloc is only available on windows in shared mode, but we
        // only ever build a static lib.
        if target_family != "windows" {
            build.define("MI_MALLOC_OVERRIDE", None);
        }
        if target_vendor == "apple" {
            build.define("MI_OSX_ZONE", Some("1"));
            build.define("MI_OSX_INTERPOSE", Some("1"));
        }
    }

    if env::var_os("CARGO_FEATURE_SECURE").is_some() {
        build.define("MI_SECURE", "4");
    }

    let dynamic_tls = env::var("CARGO_FEATURE_LOCAL_DYNAMIC_TLS").is_ok();

    if target_family == "unix" && target_os != "haiku" {
        if dynamic_tls {
            build.flag_if_supported("-ftls-model=local-dynamic");
        } else {
            build.flag_if_supported("-ftls-model=initial-exec");
        }
    }

    if (target_os == "linux" || target_os == "android")
        && env::var_os("CARGO_FEATURE_NO_THP").is_some()
    {
        build.define("MI_NO_THP", "1");
    }

    if env::var_os("CARGO_FEATURE_DEBUG").is_some()
        || (env::var_os("CARGO_FEATURE_DEBUG_IN_DEBUG").is_some() && cfg!(debug_assertions))
    {
        build.define("MI_DEBUG", "2");
        build.define("MI_SHOW_ERRORS", "1");
    } else {
        // Remove heavy debug assertions etc
        build.define("MI_DEBUG", "0");
    }

    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);
        }
    }
}
