Skip to content

Commit

Permalink
Resolve WinMain linking issues with CMake
Browse files Browse the repository at this point in the history
  • Loading branch information
ccawley2011 committed Jul 5, 2021
1 parent 3711431 commit aaf2383
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
25 changes: 13 additions & 12 deletions cmake/FindSDL2.cmake
Original file line number Diff line number Diff line change
@@ -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
)

Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion skeleton/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,6 +37,20 @@ target_include_directories(librw_skeleton
$<INSTALL_INTERFACE:${LIBRW_INSTALL_INCLUDEDIR}/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
Expand Down
3 changes: 3 additions & 0 deletions skeleton/sdl2.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifdef LIBRW_SDL2

#ifdef _WIN32
#define SDL_MAIN_HANDLED
#endif
#include <rw.h>
#include "skeleton.h"

Expand Down
3 changes: 2 additions & 1 deletion skeleton/win.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#ifdef _WIN32
#define SDL_MAIN_HANDLED
#include <windows.h>
#include <rw.h>
#include "skeleton.h"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tools/dumprwtree/dumprwtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstring>
#include <cassert>

#define SDL_MAIN_HANDLED
#include <rw.h>

using namespace std;
Expand Down
1 change: 1 addition & 0 deletions tools/ska2anm/ska2anm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include <assert.h>

#define SDL_MAIN_HANDLED
#include <rw.h>
#include <args.h>

Expand Down

0 comments on commit aaf2383

Please sign in to comment.