Skip to content

Commit

Permalink
Hack to make rust buildable for Clang prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mati865 committed Jul 4, 2021
1 parent 3352606 commit bd75a43
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
83 changes: 83 additions & 0 deletions mingw-w64-rust/0007-clang-subsystem.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
diff -urN rustc-1.52.1-src.orig/compiler/rustc_target/src/spec/windows_gnu_base.rs rustc-1.52.1-src/compiler/rustc_target/src/spec/windows_gnu_base.rs
--- rustc-1.52.1-src.orig/compiler/rustc_target/src/spec/windows_gnu_base.rs 2021-05-09 04:52:39.000000000 +0200
+++ rustc-1.52.1-src/compiler/rustc_target/src/spec/windows_gnu_base.rs 2021-06-06 22:12:33.367953300 +0200
@@ -18,34 +18,13 @@
],
);

- 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.
- "-lgcc_s".to_string(),
+ "-l:libunwind.dll.a".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);
@@ -55,8 +34,7 @@
// binaries to be redistributed without the libgcc_s-dw2-1.dll
// dependency, but unfortunately break unwinding across DLL
// boundaries when unwinding across FFI boundaries.
- "-lgcc_eh".to_string(),
- "-l:libpthread.a".to_string(),
+ "-l:libunwind.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);
@@ -66,7 +44,8 @@
env: "gnu".to_string(),
vendor: "pc".to_string(),
// FIXME(#13846) this should be enabled for windows
- function_sections: false,
+ function_sections: true,
+ no_default_libraries: false,
linker: Some("gcc".to_string()),
dynamic_linking: true,
executables: true,
@@ -84,7 +63,6 @@
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,
diff -urN rustc-1.53.0-src/src/bootstrap/bootstrap.py.orig rustc-1.53.0-src/src/bootstrap/bootstrap.py
--- rustc-1.53.0-src/src/bootstrap/bootstrap.py.orig 2021-07-03 13:57:23.756296000 +0200
+++ rustc-1.53.0-src/src/bootstrap/bootstrap.py 2021-07-03 13:58:41.037817900 +0200
@@ -425,6 +425,12 @@
with output(self.rustc_stamp(stage0)) as rust_stamp:
rust_stamp.write(key)

+ gcc_libs_hack = os.environ.get('GCC_LIBS_HACK')
+ 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)
+
if self.rustfmt() and self.rustfmt().startswith(bin_root) and (
not os.path.exists(self.rustfmt())
or self.program_out_of_date(self.rustfmt_stamp(), self.rustfmt_channel)
25 changes: 22 additions & 3 deletions mingw-w64-rust/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ source=("https://static.rust-lang.org/dist/${_realname}c-${pkgver}-src.tar.gz"
"0001-add-missing-libs.patch"
"0004-unbundle-gcc.patch"
"0005-win32-config.patch"
"0006-Update-cc.patch")
"0006-Update-cc.patch"
"0006-Update-cc.patch"
"0007-clang-subsystem.patch")
sha256sums=('5cf7ca39a10f6bf4e0b0bd15e3b9a61ce721f301e12d148262e5ba968ab825b9'
'bd63b990d2c1626acb87d5ea534c3d82fc4daddb5f784a83053be7b162327445'
'c706841ace2ef76fd1004b9c0486ea9ef7d99073c5ce2884e4c97fa4a7beab25'
'6cc3234644768e24824e455d6304585c717ae25aa53ebf9cf1f1dea87bf869b7'
'c4e5ffeef84296d39c3e3e8f807fc8b33ce786b1e4edb21eef26b053586aca27'
'dd8f785138a78398c486fe60237833d1c6a84be5128ad6b3210c135dda258497')
'dd8f785138a78398c486fe60237833d1c6a84be5128ad6b3210c135dda258497'
'dd8f785138a78398c486fe60237833d1c6a84be5128ad6b3210c135dda258497'
'9e2e2eb9c0684f329b1ce6598fcb66727abf84121087b24670a9cfc44a514888')
#validpgpkeys=('108F66205EAEB0AAA8DD5E1C85AB96E6FA1BE5FE') # Rust Language (Tag and Release Signing Key) <[email protected]>
noextract=(${_realname}c-${pkgver}-src.tar.gz)

Expand All @@ -55,12 +59,27 @@ prepare() {
patch -p1 -i "${srcdir}/0004-unbundle-gcc.patch"
patch -p1 -i "${srcdir}/0005-win32-config.patch"
patch -p1 -i "${srcdir}/0006-Update-cc.patch"

if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
patch -p1 -i "${srcdir}/0007-clang-subsystem.patch"
fi
}

build() {
[[ -d "${srcdir}/${CARCH}" ]] && rm -rf "${srcdir}/${CARCH}"
mkdir -p "${srcdir}/${CARCH}" && cd "${srcdir}/${CARCH}"


# The ultimate hack to let the bootstrap compiler use libgcc* libs
if [[ $MINGW_PACKAGE_PREFIX == *-clang-* ]]; then
export GCC_LIBS_HACK="$(cygpath -am build/missing-libs-hack)"
mkdir -p "${GCC_LIBS_HACK}"
cp "$(cygpath -u $(clang -print-libgcc-file-name))" "${GCC_LIBS_HACK}/libgcc.a"
cp "${MINGW_PREFIX}/lib/libunwind.a" "${GCC_LIBS_HACK}/libgcc_eh.a"
cp "${MINGW_PREFIX}/lib/libunwind.dll.a" "${GCC_LIBS_HACK}/libgcc_s.a"
export RUSTFLAGS_BOOTSTRAP="-C link-arg=-L$(cygpath -am build/missing-libs-hack)"
fi

#We have to do the following because rust doesn't count x86_64-w64-mingw32 as a target triple
OSTYPE="$CARCH-pc-windows-gnu"

Expand Down Expand Up @@ -88,7 +107,7 @@ build() {
DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
DEP_OPENSSL_ROOT=${MINGW_PREFIX} \
DEP_Z_ROOT=${MINGW_PREFIX} \
${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py build --verbose --stage 2 -j "$(nproc)"
${MINGW_PREFIX}/bin/python ../${_realname}c-${pkgver}-src/x.py build --verbose --stage 2

#create the install at a temporary directory
DEP_NGHTTP_ROOT=${MINGW_PREFIX} \
Expand Down

0 comments on commit bd75a43

Please sign in to comment.