Skip to content

Commit

Permalink
Several updates to try cross-compiling from gcc to clang
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisameling committed Jan 2, 2022
1 parent 07e12c9 commit 9fad921
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 10 deletions.
30 changes: 26 additions & 4 deletions compiler/rustc_target/src/spec/windows_gnu_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,34 @@ pub fn opts() -> TargetOptions {
],
);

let mut late_link_args = LinkArgs::new();
let mut late_link_args_dynamic = LinkArgs::new();
let mut late_link_args_static = LinkArgs::new();
// Order of `late_link_args*` was found through trial and error to work with various
// mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
let mingw_libs = vec![
"-lmsvcrt".to_string(),
"-lmingwex".to_string(),
"-lmingw32".to_string(),
"-lgcc".to_string(), // alas, mingw* libraries above depend on libgcc
// mingw's msvcrt is a weird hybrid import library and static library.
// And it seems that the linker fails to use import symbols from msvcrt
// that are required from functions in msvcrt in certain cases. For example
// `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
// The library is purposely listed twice to fix that.
//
// See https://github.com/rust-lang/rust/pull/47483 for some more details.
"-lmsvcrt".to_string(),
"-luser32".to_string(),
"-lkernel32".to_string(),
];
late_link_args.insert(LinkerFlavor::Gcc, mingw_libs.clone());
late_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), mingw_libs);
let dynamic_unwind_libs = vec![
// If any of our crates are dynamically linked then we need to use
// the shared libgcc_s-dw2-1.dll. This is required to support
// unwinding across DLL boundaries.
"-l:libunwind.dll.a".to_string(),
"-lgcc_s".to_string(),
];
late_link_args_dynamic.insert(LinkerFlavor::Gcc, dynamic_unwind_libs.clone());
late_link_args_dynamic.insert(LinkerFlavor::Lld(LldFlavor::Ld), dynamic_unwind_libs);
Expand All @@ -32,7 +53,8 @@ pub fn opts() -> TargetOptions {
// binaries to be redistributed without the libgcc_s-dw2-1.dll
// dependency, but unfortunately break unwinding across DLL
// boundaries when unwinding across FFI boundaries.
"-l:libunwind.a".to_string(),
"-lgcc_eh".to_string(),
"-l:libpthread.a".to_string(),
];
late_link_args_static.insert(LinkerFlavor::Gcc, static_unwind_libs.clone());
late_link_args_static.insert(LinkerFlavor::Lld(LldFlavor::Ld), static_unwind_libs);
Expand All @@ -42,8 +64,7 @@ pub fn opts() -> TargetOptions {
env: "gnu".to_string(),
vendor: "pc".to_string(),
// FIXME(#13846) this should be enabled for windows
function_sections: true,
no_default_libraries: false,
function_sections: false,
linker: Some("gcc".to_string()),
dynamic_linking: true,
executables: true,
Expand All @@ -59,6 +80,7 @@ pub fn opts() -> TargetOptions {
pre_link_objects_fallback: crt_objects::pre_mingw_fallback(),
post_link_objects_fallback: crt_objects::post_mingw_fallback(),
crt_objects_fallback: Some(CrtObjectsFallback::Mingw),
late_link_args,
late_link_args_dynamic,
late_link_args_static,
abi_return_struct_as_int: true,
Expand Down
Binary file removed missing-libs-hack/libgcc.a
Binary file not shown.
Binary file removed missing-libs-hack/libgcc_eh.a
Binary file not shown.
Binary file removed missing-libs-hack/libgcc_s.a
Binary file not shown.
11 changes: 6 additions & 5 deletions src/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,12 @@ def download_toolchain(self, stage0=True, rustc_channel=None):
with output(self.rustc_stamp(stage0)) as rust_stamp:
rust_stamp.write(key)

gcc_libs_hack = '{}/missing-libs-hack'.format(self.rust_root)
gcc_libs_hack_dest = '{}/rustlib/{}/lib'.format(lib_dir, self.build)
shutil.copy(gcc_libs_hack + '/libgcc.a', gcc_libs_hack_dest)
shutil.copy(gcc_libs_hack + '/libgcc_eh.a', gcc_libs_hack_dest)
shutil.copy(gcc_libs_hack + '/libgcc_s.a', gcc_libs_hack_dest)
#gcc_libs_hack = '{}/missing-libs-hack'.format(self.rust_root)
#gcc_libs_hack_dest = '{}/rustlib/{}/lib'.format(lib_dir, self.build)
#shutil.copy(gcc_libs_hack + '/libgcc.a', gcc_libs_hack_dest)
#shutil.copy(gcc_libs_hack + '/libgcc_eh.a', gcc_libs_hack_dest)
#shutil.copy(gcc_libs_hack + '/libgcc_s.a', gcc_libs_hack_dest)
#shutil.copy(gcc_libs_hack + '/libstdc++.a', gcc_libs_hack_dest)

if self.rustfmt() and self.rustfmt().startswith(bin_root) and (
not os.path.exists(self.rustfmt())
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn make_win_dist(
"libm.a",
"libmingw32.a",
"libmingwex.a",
//"libstdc++.a",
"libstdc++.a",
"libiconv.a",
"libmoldname.a",
"libpthread.a",
Expand Down
137 changes: 137 additions & 0 deletions x86_64-pc-windows-gnu.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"abi-return-struct-as-int": true,
"allows-weak-linkage": false,
"arch": "x86_64",
"crt-objects-fallback": "mingw",
"data-layout": "e-m:w-p:64:64-i32:32-i64:64-i128:128-n32:64-S128",
"dll-prefix": "",
"dll-suffix": ".dll",
"dynamic-linking": true,
"eh-frame-header": false,
"emit-debug-gdb-scripts": false,
"env": "gnu",
"exe-suffix": ".exe",
"executables": true,
"is-builtin": false,
"is-like-windows": true,
"late-link-args-dynamic": {
"gcc": [
"-l:libunwind.dll.a"
],
"ld.lld": [
"-l:libunwind.dll.a"
]
},
"late-link-args-static": {
"gcc": [
"-l:libunwind.a"
],
"ld.lld": [
"-l:libunwind.a"
]
},
"linker": "x86_64-w64-mingw32-clang",
"llvm-target": "x86_64-pc-windows-gnu",
"max-atomic-width": 64,
"no-default-libraries": false,
"os": "windows",
"post-link-objects": {
"dynamic-dylib": [
"rsend.o"
],
"dynamic-nopic-exe": [
"rsend.o"
],
"dynamic-pic-exe": [
"rsend.o"
],
"static-dylib": [
"rsend.o"
],
"static-nopic-exe": [
"rsend.o"
],
"static-pic-exe": [
"rsend.o"
]
},
"post-link-objects-fallback": {
"dynamic-dylib": [
"rsend.o"
],
"dynamic-nopic-exe": [
"rsend.o"
],
"dynamic-pic-exe": [
"rsend.o"
],
"static-dylib": [
"rsend.o"
],
"static-nopic-exe": [
"rsend.o"
],
"static-pic-exe": [
"rsend.o"
]
},
"pre-link-args": {
"gcc": [
"-fno-use-linker-plugin",
"-Wl,--dynamicbase",
"-Wl,--disable-auto-image-base"
]
},
"pre-link-objects": {
"dynamic-dylib": [
"rsbegin.o"
],
"dynamic-nopic-exe": [
"rsbegin.o"
],
"dynamic-pic-exe": [
"rsbegin.o"
],
"static-dylib": [
"rsbegin.o"
],
"static-nopic-exe": [
"rsbegin.o"
],
"static-pic-exe": [
"rsbegin.o"
]
},
"pre-link-objects-fallback": {
"dynamic-dylib": [
"dllcrt2.o",
"rsbegin.o"
],
"dynamic-nopic-exe": [
"crt2.o",
"rsbegin.o"
],
"dynamic-pic-exe": [
"crt2.o",
"rsbegin.o"
],
"static-dylib": [
"dllcrt2.o",
"rsbegin.o"
],
"static-nopic-exe": [
"crt2.o",
"rsbegin.o"
],
"static-pic-exe": [
"crt2.o",
"rsbegin.o"
]
},
"requires-uwtable": true,
"target-family": [
"windows"
],
"target-pointer-width": "64",
"vendor": "pc"
}

0 comments on commit 9fad921

Please sign in to comment.