Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support (cross-)compiling for mingw64 #1810

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft

Support (cross-)compiling for mingw64 #1810

wants to merge 15 commits into from

Conversation

fbrausse
Copy link
Member

@fbrausse fbrausse commented May 3, 2024

This PR contains the changes necessary for cross-compiling ESBMC from Gentoo to mingw64-11.0.0 using gcc-13.2.1 against Z3-4.13.0 (only, for now) and a mingw-build of Clang provided by @rafaelsamenezes in #1801 (comment). For reference, this is the CMake --toolchain file I used:

set(CMAKE_SYSTEM_NAME               Windows)
set(CMAKE_SYSTEM_PROCESSOR          x86_64)

## Without that flag CMake is not able to pass test compilation check
#set(CMAKE_TRY_COMPILE_TARGET_TYPE   STATIC_LIBRARY)

# aarch64-linux-gnueabihf
set(arch                      "x86_64")
set(os                        "w64")
set(flavor                    "mingw32")

set(triple                    "${arch}-${os}-${flavor}")

set(TOOLCHAIN_PREFIX          "${triple}-")
set(CMAKE_SYSROOT             "/usr/${triple}")

#set(CMAKE_CROSSCOMPILING_EMULATOR qemu-${arch} -L "${CMAKE_SYSROOT}"
#                                               -E LD_LIBRARY_PATH=/usr/lib/gcc/${triple}/11.2.0)

#set(CMAKE_CROSSCOMPILING_EMULATOR sh -c '"$0" "$@" 2>/dev/null' wine64)
set(CMAKE_CROSSCOMPILING_EMULATOR /home/kane/scorch/esbmc/x86_64-w64-mingw32.cross-emu.sh)
#set(CMAKE_CROSSCOMPILING_EMULATOR env WINEDEBUG=fixme-all WINEPATH='/usr/x86_64-w64-mingw32/usr/bin\;/usr/x86_64-w64-mingw32/usr/lib\;/usr/lib/gcc/x86_64-w64-mingw32/13' wine64)

set(CMAKE_ASM_COMPILER_TARGET ${triple})
set(CMAKE_C_COMPILER_TARGET   ${triple})
set(CMAKE_CXX_COMPILER_TARGET ${triple})

set(CMAKE_AR                  ${TOOLCHAIN_PREFIX}ar${CMAKE_EXECUTABLE_SUFFIX})
set(CMAKE_ASM_COMPILER        ${TOOLCHAIN_PREFIX}gcc${CMAKE_EXECUTABLE_SUFFIX})
set(CMAKE_C_COMPILER          ${TOOLCHAIN_PREFIX}gcc${CMAKE_EXECUTABLE_SUFFIX})
set(CMAKE_CXX_COMPILER        ${TOOLCHAIN_PREFIX}g++${CMAKE_EXECUTABLE_SUFFIX})
set(CMAKE_LINKER              ${TOOLCHAIN_PREFIX}ld${CMAKE_EXECUTABLE_SUFFIX})
set(CMAKE_OBJCOPY             ${TOOLCHAIN_PREFIX}objcopy${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_RANLIB              ${TOOLCHAIN_PREFIX}ranlib${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_SIZE                ${TOOLCHAIN_PREFIX}size${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_STRIP               ${TOOLCHAIN_PREFIX}strip${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")

#set(CMAKE_C_FLAGS             "-Wno-psabi --specs=nosys.specs -fdata-sections -ffunction-sections -Wl,--gc-sections" CACHE INTERNAL "")
#set(CMAKE_C_FLAGS             "-Wno-psabi -fdata-sections -ffunction-sections -Wl,--gc-sections" CACHE INTERNAL "")
#set(CMAKE_CXX_FLAGS           "${CMAKE_C_FLAGS} -fno-exceptions" CACHE INTERNAL "")

#set(CMAKE_C_FLAGS_DEBUG       "-Os -g" CACHE INTERNAL "")
#set(CMAKE_C_FLAGS_RELEASE     "-Os -DNDEBUG" CACHE INTERNAL "")
#set(CMAKE_CXX_FLAGS_DEBUG     "${CMAKE_C_FLAGS_DEBUG}" CACHE INTERNAL "")
#set(CMAKE_CXX_FLAGS_RELEASE   "${CMAKE_C_FLAGS_RELEASE}" CACHE INTERNAL "")

# for symbol '_setjmp' in llvm+clang-14-mingw
set(CMAKE_CXX_STANDARD_LIBRARIES "-lmsvcrt-os -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32" CACHE STRING "")
set(CMAKE_C_STANDARD_LIBRARIES "-lmsvcrt-os -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32" CACHE STRING "")

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

and this is the x86_64-w64-mingw32.cross-emu.sh:

#!/bin/sh

ulimit -m $((8<<20))
exec env \
	WINEDEBUG=fixme-all \
	WINEPATH='/usr/x86_64-w64-mingw32/usr/bin;/usr/x86_64-w64-mingw32/usr/lib;/usr/lib/gcc/x86_64-w64-mingw32/13' \
	wine64 "$@"

@rafaelsamenezes
Copy link
Contributor

That's great! Do you have a list of dependencies and a description about how the environment was set? I few like we should add this into the CI somehow.

Revert "[cmake] windows: use non-installed binary for ctest"

This reverts commit 9fc9fbc.
This reverts commit 8dc9ec4.

On Windows we need all the .dlls in the same path as esbmc.exe.
libz3.dll is already configured by our CMake scripts to be installed
there, but the following ones are still missing:

- zlib1.dll -> /usr/x86_64-w64-mingw32/usr/bin/zlib1.dll
- libboost_filesystem.dll -> /usr/x86_64-w64-mingw32/usr/lib/libboost_filesystem.dll
- libboost_program_options.dll -> /usr/x86_64-w64-mingw32/usr/lib/libboost_program_options.dll
- libbtor2parser.dll -> /usr/x86_64-w64-mingw32/usr/lib/libbtor2parser.dll
- libgcc_s_seh-1.dll -> /usr/lib/gcc/x86_64-w64-mingw32/13/libgcc_s_seh-1.dll
- libstdc++-6.dll -> /usr/lib/gcc/x86_64-w64-mingw32/13/libstdc++-6.dll
- libwinpthread-1.dll -> /usr/x86_64-w64-mingw32/usr/bin/libwinpthread-1.dll
@fbrausse
Copy link
Member Author

fbrausse commented May 6, 2024

@rafaelsamenezes I've added a description at https://github.com/esbmc/esbmc/blob/fb/mingw/BUILDING-cross-mingw.md. Because I was using an Ubuntu-22.04 VM, unfortunately, I couldn't finish the last step, because the gcc-mingw-w64-x86-64-posix is version 10, whose libstdc++ is incompatible with the one you used to build Clang-14 with. I've got to repeat those steps on a newer Ubuntu VM to be sure.

In case you want to give it a shot, please do.

@fbrausse
Copy link
Member Author

fbrausse commented May 7, 2024

Well, I tried Ubuntu-24.04, and it is even less compatible than 22.04, because its x86_64-w64-mingw32 toolchain isn't built with UCRT anymore. There is no way to use the standard packages from that latest Ubuntu version. Fedora seems to have packaged a separate x86_64-w64-mingw32ucrt toolchain, but that won't help us since the Github CI doesn't provide Fedora (as far as I know).

We could recompile Clang-14 (or maybe rather v13, see #1003 (comment)) with the toolchain file on Ubuntu-22.04...

@rafaelsamenezes
Copy link
Contributor

We could recompile Clang-14 (or maybe rather v13, see #1003 (comment)) with the toolchain file on Ubuntu-22.04...

Hm, not sure what to do. IMO we should try to move forward into 24.04 since is the next LTS. It has from LLVM 14 up to 18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants