1 use crate::spec::crt_objects::{self, CrtObjectsFallback};
2 use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, TargetOptions};
4 pub fn opts() -> TargetOptions {
5 let mut pre_link_args = LinkArgs::new();
9 // Tell GCC to avoid linker plugins, because we are not bundling
10 // them with Windows installer, and Rust does its own LTO anyways.
11 "-fno-use-linker-plugin".to_string(),
12 // Always enable DEP (NX bit) when it is available
13 "-Wl,--nxcompat".to_string(),
17 let mut late_link_args = LinkArgs::new();
18 let mut late_link_args_dynamic = LinkArgs::new();
19 let mut late_link_args_static = LinkArgs::new();
20 // Order of `late_link_args*` was found through trial and error to work with various
21 // mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
22 let mingw_libs = vec![
23 "-lmsvcrt".to_string(),
24 "-lmingwex".to_string(),
25 "-lmingw32".to_string(),
26 // mingw's msvcrt is a weird hybrid import library and static library.
27 // And it seems that the linker fails to use import symbols from msvcrt
28 // that are required from functions in msvcrt in certain cases. For example
29 // `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
30 // The library is purposely listed twice to fix that.
32 // See https://github.com/rust-lang/rust/pull/47483 for some more details.
33 "-lmsvcrt".to_string(),
34 "-luser32".to_string(),
35 "-lkernel32".to_string(),
37 late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone());
38 late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs);
39 let dynamic_unwind_libs = vec![
40 // If any of our crates are dynamically linked then we need to use
41 // the shared libgcc_s-dw2-1.dll. This is required to support
42 // unwinding across DLL boundaries.
43 "-lgcc_s".to_string(),
45 "-lkernel32".to_string(),
47 late_link_args_dynamic.insert(LinkerFlavor::Gcc, dynamic_unwind_libs.clone());
48 late_link_args_dynamic.insert(LinkerFlavor::Lld(LldFlavor::Ld), dynamic_unwind_libs);
49 let static_unwind_libs = vec![
50 // If all of our crates are statically linked then we can get away
51 // with statically linking the libgcc unwinding code. This allows
52 // binaries to be redistributed without the libgcc_s-dw2-1.dll
53 // dependency, but unfortunately break unwinding across DLL
54 // boundaries when unwinding across FFI boundaries.
55 "-lgcc_eh".to_string(),
56 "-l:libpthread.a".to_string(),
58 // libpthread depends on libmsvcrt, so we need to link it *again*.
59 "-lmsvcrt".to_string(),
60 "-lkernel32".to_string(),
62 late_link_args_static.insert(LinkerFlavor::Gcc, static_unwind_libs.clone());
63 late_link_args_static.insert(LinkerFlavor::Lld(LldFlavor::Ld), static_unwind_libs);
66 // FIXME(#13846) this should be enabled for windows
67 function_sections: false,
68 linker: Some("gcc".to_string()),
69 dynamic_linking: true,
71 dll_prefix: String::new(),
72 dll_suffix: ".dll".to_string(),
73 exe_suffix: ".exe".to_string(),
74 staticlib_prefix: "lib".to_string(),
75 staticlib_suffix: ".a".to_string(),
76 target_family: Some("windows".to_string()),
77 is_like_windows: true,
78 allows_weak_linkage: false,
80 pre_link_objects: crt_objects::pre_mingw(),
81 post_link_objects: crt_objects::post_mingw(),
82 pre_link_objects_fallback: crt_objects::pre_mingw_fallback(),
83 post_link_objects_fallback: crt_objects::post_mingw_fallback(),
84 crt_objects_fallback: Some(CrtObjectsFallback::Mingw),
86 late_link_args_dynamic,
87 late_link_args_static,
88 abi_return_struct_as_int: true,
89 emit_debug_gdb_scripts: false,
90 requires_uwtable: true,
91 eh_frame_header: false,