use std::borrow::Cow;

use crate::spec::crt_objects::pre_mingw_self_contained;
use crate::spec::{
    Abi, BinaryFormat, Cc, DebuginfoKind, Env, LinkSelfContainedDefault, LinkerFlavor, Lld, Os,
    SplitDebuginfo, TargetOptions, add_link_args, cvs,
};

pub(crate) fn opts() -> TargetOptions {
    // We cannot use `-nodefaultlibs` because compiler-rt has to be passed
    // as a path since it's not added to linker search path by the default.
    // There were attempts to make it behave like libgcc (so one can just use -l<name>)
    // but LLVM maintainers rejected it: https://reviews.llvm.org/D51440
    let pre_link_args = TargetOptions::link_args(
        LinkerFlavor::Gnu(Cc::Yes, Lld::No),
        &["-nolibc", "--unwindlib=none"],
    );
    // Order of `late_link_args*` does not matter with LLD.
    let mingw_libs = &["-lmingw32", "-lmingwex", "-lmsvcrt", "-lkernel32", "-luser32"];

    let mut late_link_args =
        TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), mingw_libs);
    add_link_args(&mut late_link_args, LinkerFlavor::Gnu(Cc::Yes, Lld::No), mingw_libs);

    TargetOptions {
        os: Os::Windows,
        env: Env::Gnu,
        vendor: "pc".into(),
        abi: Abi::Llvm,
        linker: Some("clang".into()),
        dynamic_linking: true,
        dll_tls_export: false,
        dll_prefix: "".into(),
        dll_suffix: ".dll".into(),
        exe_suffix: ".exe".into(),
        families: cvs!["windows"],
        is_like_windows: true,
        binary_format: BinaryFormat::Coff,
        allows_weak_linkage: false,
        pre_link_args,
        pre_link_objects_self_contained: pre_mingw_self_contained(),
        link_self_contained: LinkSelfContainedDefault::InferredForMingw,
        late_link_args,
        abi_return_struct_as_int: true,
        emit_debug_gdb_scripts: false,
        requires_uwtable: true,
        eh_frame_header: false,
        no_default_libraries: false,
        has_thread_local: true,
        crt_static_allows_dylibs: true,
        crt_static_respected: true,
        debuginfo_kind: DebuginfoKind::Dwarf,
        // FIXME(davidtwco): Support Split DWARF on Windows GNU - may require LLVM changes to
        // output DWO, despite using DWARF, doesn't use ELF..
        supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]),
        ..Default::default()
    }
}
