]> git.lizzy.rs Git - rust.git/blob - src/librustc_target/spec/windows_base.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / librustc_target / spec / windows_base.rs
1 use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
2 use std::default::Default;
3
4 pub fn opts() -> TargetOptions {
5     let mut pre_link_args = LinkArgs::new();
6     pre_link_args.insert(
7         LinkerFlavor::Gcc,
8         vec![
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(),
14             // Do not use the standard system startup files or libraries when linking
15             "-nostdlib".to_string(),
16         ],
17     );
18
19     let mut late_link_args = LinkArgs::new();
20     let mut late_link_args_dynamic = LinkArgs::new();
21     let mut late_link_args_static = LinkArgs::new();
22     late_link_args.insert(
23         LinkerFlavor::Gcc,
24         vec![
25             "-lmingwex".to_string(),
26             "-lmingw32".to_string(),
27             "-lmsvcrt".to_string(),
28             // mingw's msvcrt is a weird hybrid import library and static library.
29             // And it seems that the linker fails to use import symbols from msvcrt
30             // that are required from functions in msvcrt in certain cases. For example
31             // `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
32             // Listing the library twice seems to fix that, and seems to also be done
33             // by mingw's gcc (Though not sure if it's done on purpose, or by mistake).
34             //
35             // See https://github.com/rust-lang/rust/pull/47483
36             "-lmsvcrt".to_string(),
37             "-luser32".to_string(),
38             "-lkernel32".to_string(),
39         ],
40     );
41     late_link_args_dynamic.insert(
42         LinkerFlavor::Gcc,
43         vec![
44             // If any of our crates are dynamically linked then we need to use
45             // the shared libgcc_s-dw2-1.dll. This is required to support
46             // unwinding across DLL boundaries.
47             "-lgcc_s".to_string(),
48             "-lgcc".to_string(),
49             "-lkernel32".to_string(),
50         ],
51     );
52     late_link_args_static.insert(
53         LinkerFlavor::Gcc,
54         vec![
55             // If all of our crates are statically linked then we can get away
56             // with statically linking the libgcc unwinding code. This allows
57             // binaries to be redistributed without the libgcc_s-dw2-1.dll
58             // dependency, but unfortunately break unwinding across DLL
59             // boundaries when unwinding across FFI boundaries.
60             "-lgcc".to_string(),
61             "-lgcc_eh".to_string(),
62             "-lpthread".to_string(),
63             "-lkernel32".to_string(),
64         ],
65     );
66
67     TargetOptions {
68         // FIXME(#13846) this should be enabled for windows
69         function_sections: false,
70         linker: Some("gcc".to_string()),
71         dynamic_linking: true,
72         executables: true,
73         dll_prefix: String::new(),
74         dll_suffix: ".dll".to_string(),
75         exe_suffix: ".exe".to_string(),
76         staticlib_prefix: String::new(),
77         staticlib_suffix: ".lib".to_string(),
78         no_default_libraries: true,
79         target_family: Some("windows".to_string()),
80         is_like_windows: true,
81         allows_weak_linkage: false,
82         pre_link_args,
83         pre_link_objects_exe: vec![
84             "crt2.o".to_string(),    // mingw C runtime initialization for executables
85             "rsbegin.o".to_string(), // Rust compiler runtime initialization, see rsbegin.rs
86         ],
87         pre_link_objects_dll: vec![
88             "dllcrt2.o".to_string(), // mingw C runtime initialization for dlls
89             "rsbegin.o".to_string(),
90         ],
91         late_link_args,
92         late_link_args_dynamic,
93         late_link_args_static,
94         post_link_objects: vec!["rsend.o".to_string()],
95         abi_return_struct_as_int: true,
96         emit_debug_gdb_scripts: false,
97         requires_uwtable: true,
98
99         ..Default::default()
100     }
101 }