Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve WinMain linking issues with CMake #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

@madebr madebr Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use the SDL2main library, so perhaps all logic related to SDL2main can be removed from this cmake script?


if(SDL2main_LIBRARY AND NOT TARGET SDL2::SDL2main)
add_library(SDL2::SDL2main UNKNOWN IMPORTED)
set_target_properties(SDL2::SDL2main PROPERTIES
Expand Down
14 changes: 13 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,19 @@ 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)
target_link_libraries(librw_skeleton_main INTERFACE -lmingw32 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add a call to SDL_SetMainReady in main/WinMain.
See https://wiki.libsdl.org/SDL_SetMainReady

Copy link
Contributor

@madebr madebr Jul 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked the implementation of SDL_SetMainReady.
It looks like SDL2main is needed (for some platforms).
On e.g. Windows, it does something: https://github.com/libsdl-org/SDL/blob/main/src/main/windows/SDL_windows_main.c

Other platforms also use it: https://github.com/libsdl-org/SDL/tree/main/src/main

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only seems to be needed on platforms that define SDL_MAIN_NEEDED, while Windows defines SDL_MAIN_AVAILABLE instead. For Windows, what SDL2main provides is equivalent to what both the skeleton and MinGW already provide, so calling SDL_SetMainReady shouldn't be necessary on Windows.

#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