From 77753c85395861be639ff872f69b5d54d9de5c54 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Mon, 5 Jul 2021 16:35:43 +0100 Subject: [PATCH] Resolve WinMain linking issues with CMake --- cmake/FindSDL2.cmake | 25 +++++++++++++------------ skeleton/CMakeLists.txt | 15 ++++++++++++++- skeleton/sdl2.cpp | 3 +++ skeleton/win.cpp | 3 ++- tools/dumprwtree/dumprwtree.cpp | 1 + tools/ska2anm/ska2anm.cpp | 1 + 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/cmake/FindSDL2.cmake b/cmake/FindSDL2.cmake index 24288b4..38e420c 100644 --- a/cmake/FindSDL2.cmake +++ b/cmake/FindSDL2.cmake @@ -1,22 +1,21 @@ -find_package(PkgConfig QUIET) -if(PKG_CONFIG_FOUND) - pkg_check_modules(SDL2 IMPORTED_TARGET "sdl2") - if(TARGET PkgConfig::SDL2 AND NOT TARGET SDL2::SDL2) - add_library(SDL2::SDL2 INTERFACE IMPORTED) - set_property(TARGET SDL2::SDL2 PROPERTY INTERFACE_LINK_LIBRARIES PkgConfig::SDL2) +# On Windows, SDL2main is included as part of the pkg-config output, which isn't desirable when linking. +if(NOT WIN32) + find_package(PkgConfig QUIET) + if(PKG_CONFIG_FOUND) + pkg_check_modules(SDL2 IMPORTED_TARGET "sdl2") + if(TARGET PkgConfig::SDL2 AND NOT TARGET SDL2::SDL2) + add_library(SDL2::SDL2 INTERFACE IMPORTED) + set_property(TARGET SDL2::SDL2 PROPERTY INTERFACE_LINK_LIBRARIES PkgConfig::SDL2) + endif() endif() endif() -find_library(SDL2main_LIBRARY SDL2main) - if(NOT SDL2_FOUND) - find_path(SDL2_INCLUDE_DIR sdl2.h) + find_path(SDL2_INCLUDE_DIR SDL.h PATH_SUFFIXES SDL2) find_library(SDL2_LIBRARY SDL2 SDL2d) - find_library(SDL2main_LIBRARY SDL2main) - include(FindPackageHandleStandardArgs) - find_package_handle_standard_args(libuv + find_package_handle_standard_args(SDL2 REQUIRED_VARS SDL2_INCLUDE_DIR SDL2_LIBRARY ) @@ -29,6 +28,8 @@ if(NOT SDL2_FOUND) endif() endif() +find_library(SDL2main_LIBRARY SDL2main) + if(SDL2main_LIBRARY AND NOT TARGET SDL2::SDL2main) add_library(SDL2::SDL2main UNKNOWN IMPORTED) set_target_properties(SDL2::SDL2main PROPERTIES diff --git a/skeleton/CMakeLists.txt b/skeleton/CMakeLists.txt index 1c7c031..0492f05 100644 --- a/skeleton/CMakeLists.txt +++ b/skeleton/CMakeLists.txt @@ -19,7 +19,6 @@ add_library(librw_skeleton imgui/stb_textedit.h imgui/stb_truetype.h ) -add_library(librw::skeleton ALIAS librw_skeleton) set_target_properties(librw_skeleton PROPERTIES @@ -38,6 +37,20 @@ target_include_directories(librw_skeleton $ ) +# HACK: When building with MinGW, it's necessary to link to libmingw32.a *before* the library +# that provides WinMain. To work around this, an intermediate target is created when using MinGW +# that specifies the correct linking order. +# +# TODO: Use SDL_main on platforms that require it +if(MINGW AND LIBRW_PLATFORM_D3D9) + add_library(librw_skeleton_main INTERFACE) + find_library(MINGW32_LIBRARY NAMES mingw32) + target_link_libraries(librw_skeleton_main INTERFACE ${MINGW32_LIBRARY} librw_skeleton) + add_library(librw::skeleton ALIAS librw_skeleton_main) +else() + add_library(librw::skeleton ALIAS librw_skeleton) +endif() + if(LIBRW_INSTALL) install( FILES diff --git a/skeleton/sdl2.cpp b/skeleton/sdl2.cpp index 4cb7c0f..e65bf4f 100644 --- a/skeleton/sdl2.cpp +++ b/skeleton/sdl2.cpp @@ -1,5 +1,8 @@ #ifdef LIBRW_SDL2 +#ifdef _WIN32 +#define SDL_MAIN_HANDLED +#endif #include #include "skeleton.h" diff --git a/skeleton/win.cpp b/skeleton/win.cpp index 9f1ee5a..53c6807 100644 --- a/skeleton/win.cpp +++ b/skeleton/win.cpp @@ -1,4 +1,5 @@ #ifdef _WIN32 +#define SDL_MAIN_HANDLED #include #include #include "skeleton.h" @@ -291,7 +292,7 @@ SetMousePosition(int x, int y) #endif -#ifdef RW_OPENGL +#if defined(RW_OPENGL) && !defined(__MINGW32__) int main(int argc, char *argv[]); int WINAPI diff --git a/tools/dumprwtree/dumprwtree.cpp b/tools/dumprwtree/dumprwtree.cpp index ca86d3c..595a57a 100644 --- a/tools/dumprwtree/dumprwtree.cpp +++ b/tools/dumprwtree/dumprwtree.cpp @@ -3,6 +3,7 @@ #include #include +#define SDL_MAIN_HANDLED #include using namespace std; diff --git a/tools/ska2anm/ska2anm.cpp b/tools/ska2anm/ska2anm.cpp index 900ba82..71b2572 100644 --- a/tools/ska2anm/ska2anm.cpp +++ b/tools/ska2anm/ska2anm.cpp @@ -3,6 +3,7 @@ #include #include +#define SDL_MAIN_HANDLED #include #include