From 4d074644dcfe3060eee176753f08e82da178fff7 Mon Sep 17 00:00:00 2001 From: Fabrice de Gans Date: Thu, 14 Mar 2024 12:34:46 -0700 Subject: [PATCH] Configure MinGW separately from the toolchain * Set up the mingw environment in addition to the toolchain rather than separately. * Revert the submodule check to always be done for Windows and MinGW. * Revert the special link step for MinGW/GCC. --- CMakeLists.txt | 24 +++++++++++++++++++++++- cmake/Toolchain-mingw.cmake | 37 ++++++++++++++++++++++--------------- cmake/Toolchain.cmake | 6 ++++-- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ddad39c..10d8e1ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,29 @@ if(NOT CMAKE_CXX_COMPILER_LAUNCHER) endif() endif() +find_package(Git) + +# Make sure we pull in the submodules on windows and mingw. +if(GIT_FOUND AND (WIN32 OR MINGW)) + # Win32 deps submodule + set(SUBMODULE_MANUAL_UPDATE FALSE) + + if(EXISTS "${CMAKE_SOURCE_DIR}/.git" AND NOT EXISTS "${CMAKE_SOURCE_DIR}/dependencies/mingw-xaudio/include") + set(SUBMODULE_MANUAL_UPDATE TRUE) + execute_process( + COMMAND "${GIT_EXECUTABLE}" submodule update --init --remote --recursive + RESULT_VARIABLE SUBMODULE_UPDATE_STATUS + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + ) + endif() + + if(NOT EXISTS "${CMAKE_SOURCE_DIR}/dependencies/mingw-xaudio/include") + if(NOT (SUBMODULE_MANUAL_UPDATE AND SUBMODULE_UPDATE_STATUS EQUAL 0)) + message(FATAL_ERROR "Please pull in git submodules, e.g.\nrun: git submodule update --init --remote --recursive") + endif() + endif() +endif() + set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_C_STANDARD 11) @@ -53,7 +76,6 @@ set(CMAKE_C_STANDARD_REQUIRED True) project(VBA-M C CXX) -find_package(Git) find_package(PkgConfig) if(NOT CMAKE_PREFIX_PATH AND (NOT ("$ENV{CMAKE_PREFIX_PATH}" STREQUAL ""))) diff --git a/cmake/Toolchain-mingw.cmake b/cmake/Toolchain-mingw.cmake index f5b6e260..98e038b9 100644 --- a/cmake/Toolchain-mingw.cmake +++ b/cmake/Toolchain-mingw.cmake @@ -1,21 +1,28 @@ +if (NOT MINGW) + return() +endif() + # this has to run after the toolchain is initialized. include_directories("${CMAKE_SOURCE_DIR}/dependencies/mingw-include") include_directories("${CMAKE_SOURCE_DIR}/dependencies/mingw-xaudio/include") -# Win32 deps submodule -set(git_checkout FALSE) -if(EXISTS "${CMAKE_SOURCE_DIR}/.git") - set(git_checkout TRUE) - execute_process( - COMMAND ${GIT_EXECUTABLE} submodule update --init --remote --recursive - RESULT_VARIABLE git_status - WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" - ) -endif() +# link libgcc/libstdc++ statically on GCC/mingw +# and adjust link command when making a static binary +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND VBAM_STATIC) + # some dists don't have a static libpthread + set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_CXX_LINK_EXECUTABLE} -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread ") -if(NOT EXISTS "${CMAKE_SOURCE_DIR}/dependencies/mingw-xaudio/include") - if(NOT (git_checkout AND git_status EQUAL 0)) - message(FATAL_ERROR "Please pull in git submodules, e.g.\nrun: git submodule update --init --remote --recursive") + if(WIN32) + add_custom_command( + TARGET visualboyadvance-m PRE_LINK + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/msys-link-static.cmake + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) + else() + add_custom_command( + TARGET visualboyadvance-m PRE_LINK + COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/link-static.cmake + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + ) endif() -endif() - +endif() \ No newline at end of file diff --git a/cmake/Toolchain.cmake b/cmake/Toolchain.cmake index 999d3674..cc7b69c3 100644 --- a/cmake/Toolchain.cmake +++ b/cmake/Toolchain.cmake @@ -32,14 +32,16 @@ if(X86_64) add_compile_definitions(__AMD64__ __X86_64__) endif() +# Enable ASAN if requested and supported. include(Toolchain-asan) +# MINGW/MSYS-specific settings. +include(Toolchain-mingw) + # Toolchain-specific settings. if(MSVC) # This also includes clang-cl. include(Toolchain-msvc) -elseif(MINGW) - include(Toolchain-mingw) elseif(CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) include(Toolchain-gcc-clang) else()