|
| 1 | +# Cross-compile macOS (Apple Silicon) -> Windows x86_64 using Homebrew mingw-w64 |
| 2 | + |
| 3 | +set(CMAKE_SYSTEM_NAME Windows) |
| 4 | +set(CMAKE_SYSTEM_PROCESSOR x86_64) |
| 5 | + |
| 6 | +# Homebrew prefix |
| 7 | +set(_brew "/opt/homebrew") |
| 8 | +set(_triplet "x86_64-w64-mingw32") |
| 9 | + |
| 10 | +# Compilers / binutils |
| 11 | +find_program(CMAKE_C_COMPILER NAMES ${_triplet}-gcc HINTS "${_brew}/bin" REQUIRED) |
| 12 | +find_program(CMAKE_CXX_COMPILER NAMES ${_triplet}-g++ HINTS "${_brew}/bin" REQUIRED) |
| 13 | +find_program(CMAKE_RC_COMPILER NAMES ${_triplet}-windres HINTS "${_brew}/bin") |
| 14 | +find_program(CMAKE_AR NAMES ${_triplet}-ar HINTS "${_brew}/bin") |
| 15 | +find_program(CMAKE_RANLIB NAMES ${_triplet}-ranlib HINTS "${_brew}/bin") |
| 16 | +find_program(CMAKE_STRIP NAMES ${_triplet}-strip HINTS "${_brew}/bin") |
| 17 | + |
| 18 | +# Helps CMake find the right headers/libs under the MinGW sysroot |
| 19 | +execute_process( |
| 20 | + COMMAND "${CMAKE_C_COMPILER}" -print-sysroot |
| 21 | + OUTPUT_VARIABLE _sysroot |
| 22 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 23 | + ERROR_QUIET |
| 24 | +) |
| 25 | + |
| 26 | +set(CMAKE_FIND_ROOT_PATH "") |
| 27 | +if(_sysroot AND EXISTS "${_sysroot}") |
| 28 | + list(APPEND CMAKE_FIND_ROOT_PATH "${_sysroot}") |
| 29 | +endif() |
| 30 | +if(EXISTS "${_brew}/opt/mingw-w64") |
| 31 | + list(APPEND CMAKE_FIND_ROOT_PATH "${_brew}/opt/mingw-w64") |
| 32 | +endif() |
| 33 | + |
| 34 | +# Prevent accidental pickup of macOS host deps |
| 35 | +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) |
| 36 | +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) |
| 37 | +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) |
| 38 | +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) |
0 commit comments