use std::env;
use std::ffi::{OsStr, OsString};
use std::fmt::Display;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

fn fail(s: &str) -> ! {
    println!("\n\n{}\n\n", s);
    std::process::exit(1);
}

#[track_caller]
pub fn output(cmd: &mut Command) -> String {
    let output = match cmd.stderr(Stdio::inherit()).output() {
        Ok(status) => status,
        Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}", cmd, e)),
    };
    if !output.status.success() {
        panic!(
            "command did not execute successfully: {:?}\n\
             expected success, got: {}",
            cmd, output.status
        );
    }
    String::from_utf8(output.stdout).unwrap()
}

pub fn rerun_if_changed_anything_in_dir(dir: &Path) {
    let mut stack = dir
        .read_dir()
        .unwrap()
        .map(|e| e.unwrap())
        .filter(|e| &*e.file_name() != ".git")
        .collect::<Vec<_>>();
    while let Some(entry) = stack.pop() {
        let path = entry.path();
        if entry.file_type().unwrap().is_dir() {
            stack.extend(path.read_dir().unwrap().map(|e| e.unwrap()));
        } else {
            println!("cargo:rerun-if-changed={}", path.display());
        }
    }
}

/// Reads an environment variable and adds it to dependencies.
/// Supposed to be used for all variables except those set for build scripts by cargo
/// <https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts>
fn tracked_env_var_os<K: AsRef<OsStr> + Display>(key: K) -> Option<OsString> {
    println!("cargo:rerun-if-env-changed={}", key);
    env::var_os(key)
}

fn detect_llvm_link() -> (&'static str, &'static str) {
    // Force the link mode we want, preferring static by default, but
    // possibly overridden by `configure --enable-llvm-link-shared`.
    if tracked_env_var_os("LLVM_LINK_SHARED").is_some() {
        ("dylib", "--link-shared")
    } else {
        ("static", "--link-static")
    }
}

fn winepath(path: &str) -> PathBuf {
    let mut cmd = Command::new("winepath");
    cmd.arg(path);
    let path = output(&mut cmd);
    Path::new(path.trim()).to_owned()
}

fn main() {
    if std::env::var("COMPILER_NAME").is_err() {
        println!("cargo:rustc-env=COMPILER_NAME=openvaf");
    }

    if tracked_env_var_os("RUST_CHECK").is_some() {
        // If we're just running `check`, there's no need for LLVM to be built.
        return;
    }

    // build_helper::restore_library_path();

    // let target = env::var("TARGET").expect("TARGET was not set");
    let llvm_config = tracked_env_var_os("LLVM_CONFIG").map(PathBuf::from);
    // TODO provide mechanism to build llvm yourself
    // .unwrap_or_else(|| {
    //     if let Some(dir) = tracked_env_var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
    //         let to_test = dir
    //             .parent()
    //             .unwrap()
    //             .parent()
    //             .unwrap()
    //             .join(&target)
    //             .join("llvm/bin/llvm-config");
    //         if Command::new(&to_test).output().is_ok() {
    //             return Some(to_test);
    //         }
    //     }
    //     None
    // });

    if let Some(llvm_config) = &llvm_config {
        println!("cargo:rerun-if-changed={}", llvm_config.display());
    }
    let llvm_config = llvm_config.unwrap_or_else(|| PathBuf::from("llvm-config"));

    // Test whether we're cross-compiling LLVM. This is a pretty rare case
    // currently where we're producing an LLVM for a different platform than
    // what this build script is currently running on.
    //
    // In that case, there's no guarantee that we can actually run the target,
    // so the build system works around this by giving us the LLVM_CONFIG for
    // the host platform. This only really works if the host LLVM and target
    // LLVM are compiled the same way, but for us that's typically the case.
    //
    // We *want* detect this cross compiling situation by asking llvm-config
    // what its host-target is. If that's not the TARGET, then we're cross
    // compiling. Unfortunately `llvm-config` seems either be buggy, or we're
    // misconfiguring it, because the `i686-pc-windows-gnu` build of LLVM will
    // report itself with a `--host-target` of `x86_64-pc-windows-gnu`. This
    // tricks us into thinking we're doing a cross build when we aren't, so
    // havoc ensues.
    //
    // In any case, if we're cross compiling, this generally just means that we
    // can't trust all the output of llvm-config because it might be targeted
    // for the host rather than the target. As a result a bunch of blocks below
    // are gated on `if !is_crossed`
    let target = env::var("TARGET").expect("TARGET was not set");
    let host = env::var("HOST").expect("HOST was not set");
    let is_crossed = target != host;
    let is_wine = is_crossed && target.contains("windows-msvc") && !host.contains("windows");

    let optional_components = &[
        "x86", "arm",
        "aarch64",
        // "amdgpu",
        // "avr",
        // "m68k",
        // "mips",
        // "powerpc",
        // "systemz",
        // "jsbackend",
        // "webassembly",
        // "msp430",
        // "sparc",
        // "nvptx",
        // "hexagon",
        // "riscv",
        // "bpf",
    ];

    let required_components = &[
        "ipo",
        "bitreader",
        "bitwriter",
        // "linker",
        "option",
        // "asmparser",
        "lto",
        "debuginfopdb",
        "windowsmanifest",
        "libdriver", // "coverage",
                     // "instrumentation",
    ];

    let components = output(Command::new(&llvm_config).arg("--components"));
    let mut components = components.split_whitespace().collect::<Vec<_>>();
    components.retain(|c| optional_components.contains(c) || required_components.contains(c));

    for component in required_components {
        if !components.contains(component) {
            panic!("require llvm component {} but wasn't found", component);
        }
    }

    for component in components.iter() {
        println!("cargo:rustc-cfg=llvm_component=\"{}\"", component);
    }

    // Link in our own LLVM shims, compiled with the same flags as LLVM
    let mut cmd = Command::new(&llvm_config);
    cmd.arg("--cxxflags");
    let cxxflags = output(&mut cmd);

    // Obtain version and pass as env variable
    let mut cmd = Command::new(&llvm_config);
    cmd.arg("--version");
    let version = output(&mut cmd).trim().to_owned();
    let version_components: Vec<_> = version.split('.').collect();
    if let [major, minor, patch] = version_components.as_slice() {
        let major: Result<u32, _> = major.parse();
        let minor: Result<u32, _> = minor.parse();
        let patch: Result<u32, _> = patch.parse();
        if let (Ok(major), Ok(minor), Ok(patch)) = (major, minor, patch) {
            println!("cargo:rustc-env=LLVM_VERSION_MAJOR={}", major);
            println!("cargo:rustc-env=LLVM_VERSION_MINOR={}", minor);
            println!("cargo:rustc-env=LLVM_VERSION_PATCH={}", patch);
        } else {
            fail(&format!(
                "Invalid LLVM version {:?}!\nExpected 3 numbers separated by '.' foound {:?}",
                version, components
            ))
        }
    } else {
        fail(&format!("Invalid LLVM version {:?}!\nExpected 3 components (major, minor, path) but found the following {} components\n{:?}", version, components.len(),components))
    }

    let mut cfg = cc::Build::new();
    cfg.warnings(false);
    for flag in cxxflags.split_whitespace() {
        // Ignore flags like `-m64` when we're doing a cross build
        if is_crossed && flag.starts_with("-m") {
            continue;
        }

        if flag.starts_with("-flto") {
            continue;
        }

        // -Wdate-time is not supported by the netbsd cross compiler
        if is_crossed && target.contains("netbsd") && flag.contains("date-time") {
            continue;
        }

        // Include path contains host directory, replace it with target
        if is_crossed && flag.starts_with("-I") {
            cfg.flag(&flag.replace(&host, &target));
            continue;
        }

        cfg.flag(flag);
    }

    for component in &components {
        let mut flag = String::from("LLVM_COMPONENT_");
        flag.push_str(&component.to_uppercase());
        cfg.define(&flag, None);
    }

    //     if tracked_env_var_os("LLVM_RUSTLLVM").is_some() {
    //         cfg.define("LLVM_RUSTLLVM", None);
    //     }

    // if tracked_env_var_os("LLVM_NDEBUG").is_some() {
    cfg.define("NDEBUG", None);
    cfg.debug(false);
    // }

    rerun_if_changed_anything_in_dir(Path::new("wrapper"));
    cfg.cpp(true)
        .warnings(true)
        .file("wrapper/OpenVafWrapper.cpp")
        .cpp_link_stdlib(None) // we handle this below
        .compile("llvm-wrapper");

    let (llvm_kind, llvm_link_arg) = detect_llvm_link();

    // Link in all LLVM libraries, if we're using the "wrong" llvm-config then
    // we don't pick up system libs because unfortunately they're for the host
    // of llvm-config, not the target that we're attempting to link.
    let mut cmd = Command::new(&llvm_config);
    cmd.arg(llvm_link_arg).arg("--libnames");
    cmd.args(&components);

    for mut lib in output(&mut cmd).split_whitespace() {
        lib = lib
            .trim_end_matches(".lib")
            .trim_end_matches(".so")
            .trim_end_matches(".a")
            .trim_start_matches("lib");

        // Don't need or want this library, but LLVM's CMake build system
        // doesn't provide a way to disable it, so filter it here even though we
        // may or may not have built it. We don't reference anything from this
        // library and it otherwise may just pull in extra dependencies on
        // libedit which we don't want
        if lib.contains("LLVMLineEditor") {
            continue;
        }

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

    // Link in all LLVM libraries, if we're using the "wrong" llvm-config then
    // we don't pick up system libs because unfortunately they're for the host
    // of llvm-config, not the target that we're attempting to link.
    let mut cmd = Command::new(&llvm_config);
    cmd.arg(llvm_link_arg);

    if !is_crossed {
        cmd.arg("--system-libs");
        cmd.args(&components);

        for lib in output(&mut cmd).split_whitespace() {
            let name = if let Some(stripped) = lib.strip_prefix("-l") {
                stripped
            } else if let Some(stripped) = lib.strip_prefix('-') {
                stripped
            } else if Path::new(lib).exists() {
                // On MSVC llvm-config will print the full name to libraries, but
                // we're only interested in the name part
                let name = Path::new(lib).file_name().unwrap().to_str().unwrap();
                name.trim_end_matches(".lib")
            } else if lib.ends_with(".lib") {
                // Some MSVC libraries just come up with `.lib` tacked on, so chop
                // that off
                lib.trim_end_matches(".lib")
            } else {
                continue;
            };

            println!("cargo:rustc-link-lib=dylib={}", name);
        }
    } else if target.contains("windows-gnu") {
        println!("cargo:rustc-link-lib=shell32");
        println!("cargo:rustc-link-lib=uuid");
    } else if target.contains("netbsd") || target.contains("haiku") || target.contains("darwin") {
        println!("cargo:rustc-link-lib=z");
    }

    // LLVM ldflags
    //
    // If we're a cross-compile of LLVM then unfortunately we can't trust these
    // ldflags (largely where all the LLVM libs are located). Currently just
    // hack around this by replacing the host triple with the target and pray
    // that those -L directories are the same!
    let mut cmd = Command::new(&llvm_config);
    let mut libdir = output(cmd.arg(llvm_link_arg).arg("--libdir"));

    if is_wine {
        libdir = winepath(&libdir).to_str().expect("all paths are valid utf-8").to_owned();
    }

    println!("cargo:rustc-link-search=native={}", libdir);

    let llvm_linker_flags = tracked_env_var_os("LLVM_LINKER_FLAGS");
    if let Some(s) = llvm_linker_flags {
        for lib in s.into_string().unwrap().split_whitespace() {
            if let Some(stripped) = lib.strip_prefix("-l") {
                println!("cargo:rustc-link-lib={}", stripped);
            } else if let Some(stripped) = lib.strip_prefix("-L") {
                println!("cargo:rustc-link-search=native={}", stripped);
            }
        }
    }

    let llvm_static_stdcpp = tracked_env_var_os("LLVM_STATIC_STDCPP");
    let llvm_use_libcxx = tracked_env_var_os("LLVM_USE_LIBCXX");

    let stdcppname = if target.contains("openbsd") {
        if target.contains("sparc64") {
            "estdc++"
        } else {
            "c++"
        }
    } else if target.contains("freebsd") || target.contains("darwin") {
        "c++"
    } else if target.contains("netbsd") && llvm_static_stdcpp.is_some() {
        // NetBSD uses a separate library when relocation is required
        "stdc++_pic"
    } else if llvm_use_libcxx.is_some() {
        "c++"
    } else {
        "stdc++"
    };

    // RISC-V GCC erroneously requires libatomic for sub-word
    // atomic operations. FreeBSD uses Clang as its system
    // compiler and provides no libatomic in its base system so
    // does not want this.
    if !target.contains("freebsd") && target.starts_with("riscv") {
        println!("cargo:rustc-link-lib=atomic");
    }

    // C++ runtime library
    if !target.contains("msvc") {
        if let Some(s) = llvm_static_stdcpp {
            assert!(!cxxflags.contains("stdlib=libc++"));
            let path = PathBuf::from(s);
            println!("cargo:rustc-link-search=native={}", path.parent().unwrap().display());
            if target.contains("windows") {
                println!("cargo:rustc-link-lib=static:-bundle={}", stdcppname);
            } else {
                println!("cargo:rustc-link-lib=static={}", stdcppname);
            }
        } else if cxxflags.contains("stdlib=libc++") {
            println!("cargo:rustc-link-lib=c++");
        } else {
            println!("cargo:rustc-link-lib={}", stdcppname);
        }
    }

    // Libstdc++ depends on pthread which Rust doesn't link on MinGW
    // since nothing else requires it.
    if target.contains("windows-gnu") {
        println!("cargo:rustc-link-lib=static:-bundle=pthread");
    }
}
