ruslo/polly

mingw cross compilation from Linux to Windows

NeroBurner opened this issue · 9 comments

as mentioned over at hunter ruslo/hunter#846

I don't know where to put this file so I created this issue

contents of Toolchain-Ubuntu-mingw64.cmake

# Sample toolchain file for building for Windows from an Ubuntu Linux system.
#
# Typical usage:
#    *) install cross compiler: `sudo apt-get install mingw-w64 g++-mingw-w64`
#    *) cd build
#    *) cmake -DCMAKE_TOOLCHAIN_FILE=~/Toolchain-Ubuntu-mingw64.cmake ..

set(CMAKE_SYSTEM_NAME Windows)
set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_Fortran_COMPILER ${TOOLCHAIN_PREFIX}-gfortran)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

# target environment on the build host system
#   set 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX})

# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
ruslo commented
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

I think we don't need this if you're using Hunter. find_package should never return anything from system if Hunter used, see:

where should I set the windres variable needed for cross-compiling to windows

set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

Edit:
and more importantly, where is the following variable set?

set(CMAKE_SYSTEM_NAME Windows)
ruslo commented

set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

I think it will not hurt if we will add it to the compiler/gcc-cross-compile.cmake.

set(CMAKE_SYSTEM_NAME Windows)

In top linux-mingw64.

should we add emulator support?
https://cmake.org/cmake/help/v3.3/variable/CMAKE_CROSSCOMPILING_EMULATOR.html

with this we could run mingw-w64 cross compiled tests with wine64 or armhf targets with qemu

ruslo commented

should we add emulator support?

It will not hurt I guess. Is emulator a part of mingw-w64 package?

no, it would need an extra emulator installed. And for mingw that would be wine (to run windows binaries)

ruslo commented

no, it would need an extra emulator installed

In this case we can add CMAKE_CROSSCOMPILING_EMULATOR variable but only if it will not return error if emulator not found.

ruslo commented

Closing inactive discussion.