From 6be2c5a7e9187fc14d51e1ec32ca235143bb0d8b Mon Sep 17 00:00:00 2001 From: Ben Gamari Date: Fri, 14 Jan 2022 19:56:05 -0500 Subject: Windows/Clang: Build system adaptation * Bump win32-tarballs to 0.7 * Move Windows toolchain autoconf logic into separate file * Use clang and LLVM utilities as described in #21019 * Disable object merging as lld doesn't support -r * Drop --oformat=pe-bigobj-x86-64 arguments from ld flags as LLD detects that the output is large on its own. * Drop gcc wrapper since Clang finds its root fine on its own. --- m4/fp_settings.m4 | 145 +++++++++++++++++++++------------------ m4/fp_setup_windows_toolchain.m4 | 107 +++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 68 deletions(-) create mode 100644 m4/fp_setup_windows_toolchain.m4 (limited to 'm4') diff --git a/m4/fp_settings.m4 b/m4/fp_settings.m4 index 09a24f33e1..4db7cd46af 100644 --- a/m4/fp_settings.m4 +++ b/m4/fp_settings.m4 @@ -1,104 +1,113 @@ # FP_SETTINGS # ---------------------------------- # Set the variables used in the settings file -# See Note [tooldir: How GHC finds mingw on Windows] AC_DEFUN([FP_SETTINGS], [ - if test "$windows" = YES -a "$EnableDistroToolchain" = "NO" - then + SettingsUseDistroMINGW="$EnableDistroToolchain" + + if test "$windows" = YES -a "$EnableDistroToolchain" = "NO"; then + # Handle the Windows toolchain installed in FP_SETUP_WINDOWS_TOOLCHAIN. + # See Note [tooldir: How GHC finds mingw on Windows] mingw_bin_prefix='$$tooldir/mingw/bin/' - SettingsCCompilerCommand="${mingw_bin_prefix}gcc.exe" - SettingsHaskellCPPCommand="${mingw_bin_prefix}gcc.exe" - SettingsHaskellCPPFlags="$HaskellCPPArgs" - SettingsLdCommand="${mingw_bin_prefix}ld.exe" - # Overrides FIND_MERGE_OBJECTS in order to avoid hard-coding linker - # path on Windows (#18550). - SettingsMergeObjectsCommand="${SettingsLdCommand}" - SettingsMergeObjectsFlags="-r --oformat=pe-bigobj-x86-64" - SettingsArCommand="${mingw_bin_prefix}ar.exe" - SettingsRanlibCommand="${mingw_bin_prefix}ranlib.exe" - SettingsDllWrapCommand="${mingw_bin_prefix}dllwrap.exe" - SettingsWindresCommand="${mingw_bin_prefix}windres.exe" - SettingsTouchCommand='$$topdir/bin/touchy.exe' - elif test "$EnableDistroToolchain" = "YES" - then - SettingsCCompilerCommand="$(basename $CC)" + SettingsCCompilerCommand="${mingw_bin_prefix}clang.exe" SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2" SettingsCxxCompilerFlags="$CONF_CXX_OPTS_STAGE2" - SettingsHaskellCPPCommand="$(basename $HaskellCPPCmd)" + SettingsCCompilerLinkFlags="$CONF_GCC_LINKER_OPTS_STAGE2" + SettingsHaskellCPPCommand="${mingw_bin_prefix}clang.exe" SettingsHaskellCPPFlags="$HaskellCPPArgs" - SettingsLdCommand="$(basename $LdCmd)" - SettingsMergeObjectsCommand="$(basename $MergeObjsCmd)" - SettingsMergeObjectsFlags="$MergeObjsArgs" - SettingsArCommand="$(basename $ArCmd)" - SettingsDllWrapCommand="$(basename $DllWrapCmd)" - SettingsWindresCommand="$(basename $WindresCmd)" + SettingsLdCommand="${mingw_bin_prefix}ld.lld.exe" + SettingsLdFlags="" + # LLD does not support object merging (#21068) + SettingsMergeObjectsCommand="" + SettingsMergeObjectsFlags="" + SettingsArCommand="${mingw_bin_prefix}llvm-ar.exe" + SettingsRanlibCommand="${mingw_bin_prefix}llvm-ranlib.exe" + SettingsDllWrapCommand="${mingw_bin_prefix}llvm-dllwrap.exe" + SettingsWindresCommand="${mingw_bin_prefix}llvm-windres.exe" SettingsTouchCommand='$$topdir/bin/touchy.exe' + else + # This case handles the "normal" platforms (e.g. not Windows) where we + # don't provide the toolchain. + SettingsCCompilerCommand="$CC" + SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2" + SettingsCxxCompilerFlags="$CONF_CXX_OPTS_STAGE2" SettingsHaskellCPPCommand="$HaskellCPPCmd" SettingsHaskellCPPFlags="$HaskellCPPArgs" + SettingsCCompilerLinkFlags="$CONF_GCC_LINKER_OPTS_STAGE2" SettingsLdCommand="$LdCmd" - SettingsMergeObjectsCommand="$MergeObjsCmd" - SettingsMergeObjectsFlags="$MergeObjsArgs" + SettingsLdFlags="$CONF_LD_LINKER_OPTS_STAGE2" SettingsArCommand="$ArCmd" SettingsRanlibCommand="$RanlibCmd" - if test -z "$DllWrapCmd" - then + SettingsMergeObjectsCommand="$MergeObjsCmd" + SettingsMergeObjectsFlags="$MergeObjsArgs" + + if test -z "$DllWrapCmd"; then SettingsDllWrapCommand="/bin/false" else SettingsDllWrapCommand="$DllWrapCmd" fi - if test -z "$WindresCmd" - then + if test -z "$WindresCmd"; then SettingsWindresCommand="/bin/false" else SettingsWindresCommand="$WindresCmd" fi - SettingsTouchCommand='touch' + + if test "$HostOS" = "mingw32"; then + SettingsTouchCommand='$$topdir/bin/touchy.exe' + else + SettingsTouchCommand='touch' + fi + + if test "$EnableDistroToolchain" = "YES"; then + # If the user specified --enable-distro-toolchain then we just use the + # executable names, not paths. + SettingsCCompilerCommand="$(basename $SettingsCCompilerCommand)" + SettingsHaskellCPPCommand="$(basename $SettingsHaskellCPPCommand)" + SettingsLdCommand="$(basename $SettingsLdCommand)" + SettingsMergeObjectsCommand="$(basename $SettingsMergeObjectsCommand)" + SettingsArCommand="$(basename $SettingsArCommand)" + SettingsDllWrapCommand="$(basename $SettingsDllWrapCommand)" + SettingsWindresCommand="$(basename $SettingsWindresCommand)" + fi fi - if test -z "$LibtoolCmd" - then - SettingsLibtoolCommand="libtool" - else - SettingsLibtoolCommand="$LibtoolCmd" + + # Platform-agnostic tools + if test -z "$LibtoolCmd"; then + LibtoolCmd="libtool" fi - if test -z "$ClangCmd" - then - SettingsClangCommand="clang" - else - SettingsClangCommand="$ClangCmd" + SettingsLibtoolCommand="$LibtoolCmd" + + if test -z "$ClangCmd"; then + ClangCmd="clang" fi - if test -z "$LlcCmd" - then - SettingsLlcCommand="llc" - else - SettingsLlcCommand="$LlcCmd" + SettingsClangCommand="$ClangCmd" + + # LLVM backend tools + if test -z "$LlcCmd"; then + LlcCmd="llc" fi - if test -z "$OptCmd" - then - SettingsOptCommand="opt" - else - SettingsOptCommand="$OptCmd" + SettingsLlcCommand="$LlcCmd" + + if test -z "$OptCmd"; then + OptCmd="opt" fi - if test -z "$OtoolCmd" - then - SettingsOtoolCommand="otool" - else - SettingsOtoolCommand="$OtoolCmd" + SettingsOptCommand="$OptCmd" + + # Mac-only tools + if test -z "$OtoolCmd"; then + OtoolCmd="otool" fi - if test -z "$InstallNameToolCmd" - then - SettingsInstallNameToolCommand="install_name_tool" - else - SettingsInstallNameToolCommand="$InstallNameToolCmd" + SettingsOtoolCommand="$OtoolCmd" + + if test -z "$InstallNameToolCmd"; then + InstallNameToolCmd="install_name_tool" fi - SettingsCCompilerFlags="$CONF_CC_OPTS_STAGE2" - SettingsCxxCompilerFlags="$CONF_CXX_OPTS_STAGE2" - SettingsCCompilerLinkFlags="$CONF_GCC_LINKER_OPTS_STAGE2" + SettingsInstallNameToolCommand="$InstallNameToolCmd" + SettingsCCompilerSupportsNoPie="$CONF_GCC_SUPPORTS_NO_PIE" - SettingsLdFlags="$CONF_LD_LINKER_OPTS_STAGE2" - SettingsUseDistroMINGW="$EnableDistroToolchain" + AC_SUBST(SettingsCCompilerCommand) AC_SUBST(SettingsHaskellCPPCommand) AC_SUBST(SettingsHaskellCPPFlags) diff --git a/m4/fp_setup_windows_toolchain.m4 b/m4/fp_setup_windows_toolchain.m4 new file mode 100644 index 0000000000..35e322c8a0 --- /dev/null +++ b/m4/fp_setup_windows_toolchain.m4 @@ -0,0 +1,107 @@ +AC_DEFUN([FP_SETUP_WINDOWS_TOOLCHAIN],[ + # Find the mingw-w64 archive file to extract. + if test "$HostArch" = "i386" + then + mingw_arch="i686" + tarball_dest_dir="mingw-w64/i686" + tarball_mingw_dir="clang32" + else + mingw_arch="x86_64" + tarball_dest_dir="mingw-w64/x86_64" + tarball_mingw_dir="clang64" + fi + + set_up_tarballs() { + AC_MSG_NOTICE([Checking for Windows toolchain tarballs...]) + local action + if test "$TarballsAutodownload" = "NO" + then + action="verify" + else + action="download" + fi + $PYTHON mk/get-win32-tarballs.py $action $mingw_arch > missing-win32-tarballs + case $? in + 0) + rm missing-win32-tarballs + ;; + 2) + echo + echo "Error:" + echo "Needed msys2 tarballs are missing. You have a few options to get them," + echo + echo " * run configure with the --enable-tarballs-autodownload option" + echo + echo " * run mk/get-win32-tarballs.py download $mingw_arch" + echo + echo " * manually download the files listed in ./missing-win32-tarballs and place" + echo " them in the ghc-tarballs directory." + echo + exit 1 + ;; + *) + echo + echo "Error fetching msys2 tarballs; see errors above." + exit 1 + ;; + esac + + # Extract all the tarballs in one go + if ! test -d inplace/mingw + then + AC_MSG_NOTICE([Extracting Windows toolchain from archives (may take a while)...]) + rm -rf inplace/mingw + local base_dir="../ghc-tarballs/${tarball_dest_dir}" + ( cd inplace && + find "${base_dir}" -name "*.tar.xz" -exec tar --xz -xf {} \; && + find "${base_dir}" -name "*.tar.zst" -exec tar --zstd -xf {} \; && + rm ".MTREE" && + rm ".PKGINFO" && + cd .. ) || AC_MSG_ERROR([Could not extract Windows toolchains.]) + + mv "inplace/${tarball_mingw_dir}" inplace/mingw && + touch inplace/mingw + AC_MSG_NOTICE([In-tree MingW-w64 tree created]) + fi + } + + # See Note [tooldir: How GHC finds mingw on Windows] + test -d inplace || mkdir inplace + + # NB. Download and extract the MingW-w64 distribution if required + set_up_tarballs + + # N.B. The parameters which get plopped in the `settings` file used by the + # resulting compiler are computed in `FP_SETTINGS`. + + # Our Windows toolchain is based around Clang and LLD. We use compiler-rt + # for the runtime, libc++ and libc++abi for the C++ standard library + # implementation, and libunwind for C++ unwinding. + mingwbin="$hardtop/inplace/mingw/bin/" + + CC="${mingwbin}clang.exe" + cflags="--rtlib=compiler-rt" + CFLAGS="$cflags" + CONF_CC_OPTS_STAGE1="$cflags" + CONF_CC_OPTS_STAGE2="$cflags" + + cxxflags="--rtlib=compiler-rt --unwindlib=libunwind --stdlib=libc++" + CXXFLAGS="$cxxflags" + CONF_CXX_OPTS_STAGE1="$cxxflags" + CONF_CXX_OPTS_STAGE2="$cxxflags" + + CONF_GCC_LINKER_OPTS_STAGE1="-fuse-ld=lld $cflags" + CONF_GCC_LINKER_OPTS_STAGE2="-fuse-ld=lld $cflags" + + LD="${mingwbin}ld.lld.exe" + NM="${mingwbin}llvm-nm.exe" + AR="${mingwbin}llvm-ar.exe" + RANLIB="${mingwbin}llvm-ranlib.exe" + OBJDUMP="${mingwbin}llvm-objdump.exe" + DLLTOOL="${mingwbin}llvm-dlltool.exe" + + # N.B. LLD does not support -r + MergeObjsCmd="" + MergeObjsArgs="" + AC_PATH_PROG([Genlib],[genlib]) +]) -- cgit v1.2.1