From 87e44b700b6c80eaf7757e3a2747560ce5b6f7dc Mon Sep 17 00:00:00 2001 From: Nixola Date: Thu, 7 Oct 2021 11:53:56 +0200 Subject: [PATCH] Mingw cross compilation (#24) * Fixed icon filename capitalization * Created mingw cmake toolchain * Adjusted CMakeLists.txt for compatibility * Small mingwcc.cmake cleanup * Added cross-compilation instructions to readme * Update README.md Fixed typo Co-authored-by: Nicola Orlando Co-authored-by: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> --- CMakeLists.txt | 14 ++++++++++---- README.md | 3 ++- SpaceCadetPinball/SpaceCadetPinball.rc | 2 +- mingwcc.cmake | 25 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 mingwcc.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 89da6bd..db5a714 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,8 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMakeModules") # On Windows, set paths to SDL-devel packages here if(WIN32) - set(SDL2_PATH "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2") - set(SDL2_MIXER_PATH "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2_mixer") + list(APPEND SDL2_PATH "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2") + list(APPEND SDL2_MIXER_PATH "${CMAKE_CURRENT_LIST_DIR}/Libs/SDL2_mixer") endif() # SDL2main is not needed @@ -195,8 +195,14 @@ target_link_libraries(SpaceCadetPinball ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY}) # On Windows, copy DLL to output if(WIN32) - get_filename_component(SDL2_DLL_PATH ${SDL2_LIBRARY} DIRECTORY) - get_filename_component(SDL2_MIXER_DLL_PATH ${SDL2_MIXER_LIBRARY} DIRECTORY) + list(POP_BACK SDL2_LIBRARY SDL2_DLL_PATH) + list(POP_BACK SDL2_MIXER_LIBRARY SDL2_MIXER_DLL_PATH) + get_filename_component(SDL2_DLL_PATH ${SDL2_DLL_PATH} DIRECTORY) + get_filename_component(SDL2_MIXER_DLL_PATH ${SDL2_MIXER_DLL_PATH} DIRECTORY) + if(MINGW) + string(REGEX REPLACE "lib$" "bin" SDL2_DLL_PATH ${SDL2_DLL_PATH}) + string(REGEX REPLACE "lib$" "bin" SDL2_MIXER_DLL_PATH ${SDL2_MIXER_DLL_PATH}) + endif() message(STATUS "copy paths='${SDL2_DLL_PATH}' '${SDL2_MIXER_DLL_PATH}'") add_custom_command(TARGET SpaceCadetPinball POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different "${SDL2_DLL_PATH}/SDL2.dll" $ diff --git a/README.md b/README.md index d963574..3b730d6 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ Compile with Visual Studio; tested with 2019. On Linux:\ Install devel packages for `SDL2` and `SDL2_mixer`.\ -Compile with CMake; tested with GCC 10, Clang 11. +Compile with CMake; tested with GCC 10, Clang 11.\ +To cross-compile for Windows, install a 64-bit version of mingw and its `SDL2` and `SDL2_mixer` distributions, then use the `mingwcc.cmake` toolchain. On macOS:\ **Homebrew**: Install the `SDL2`, `SDL2_mixer` homebrew packages.\ diff --git a/SpaceCadetPinball/SpaceCadetPinball.rc b/SpaceCadetPinball/SpaceCadetPinball.rc index c372b1e..d7b380b 100644 --- a/SpaceCadetPinball/SpaceCadetPinball.rc +++ b/SpaceCadetPinball/SpaceCadetPinball.rc @@ -1 +1 @@ -ICON_1 ICON "icon_1.ico" \ No newline at end of file +ICON_1 ICON "Icon_1.ico" diff --git a/mingwcc.cmake b/mingwcc.cmake new file mode 100644 index 0000000..5c26b26 --- /dev/null +++ b/mingwcc.cmake @@ -0,0 +1,25 @@ +set(TOOLCHAIN_PREFIX "x86_64-w64-mingw32") + +set(CMAKE_SYSTEM_NAME Windows) + +set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}-g++") +set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}-gcc") +set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}-objcopy") +set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}-strip") +set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}-size") +set(CMAKE_AR "${TOOLCHAIN_PREFIX}-ar") +set(ASSEMBLER "${TOOLCHAIN_PREFIX}-as") + + +set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX}) + +# adjust the default behavior of the find commands: +# search headers and libraries in the target environment +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) + +# search programs in the host environment +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + +# Add the path to your toolchain version of SDL2 +set(SDL2_PATH /usr/${TOOLCHAIN_PREFIX}) \ No newline at end of file