sdl2-config.cmake.in: Make SDL2::SDL2 work with MinGW, fixes #3665 It didn't work at all because shared libs defined in CMake with add_library() need something like IMPORTED_IMPLIB (pointing to a .dll.a or .lib for th DLLs) set to link on Windows. But even with that it didn't work because the order of the libs is very important: it must be -lmingw32 -lSDL2main -lSDL -mwindows - but with normal add_library(SDL2::SDL2 SHARED IMPORTED) libs, SDL2 itself is always linked first. So I use an "INTERFACE" library (usually used for header-only libs), which doesn't implicitly/automatically link anything so I can specify the whole order of the linked libs. (SDL2::SDL2-static is completely untested)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
diff --git a/sdl2-config.cmake.in b/sdl2-config.cmake.in
index 77c4e1a..c570511 100644
--- a/sdl2-config.cmake.in
+++ b/sdl2-config.cmake.in
@@ -12,20 +12,64 @@ set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} @SDL_RLD_FLAGS@ @SDL_LIBS@")
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
if(NOT TARGET SDL2::SDL2)
+ # provide SDL2::SDL2, SDL2::SDL2main and SDL2::SDL2-static targets, like SDL2Config.cmake does, for compatibility
+
# Remove -lSDL2 as that is handled by CMake, note the space at the end so it does not replace e.g. -lSDL2main
# This may require "libdir" beeing set (from above)
string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS "@SDL_RLD_FLAGS@ @SDL_LIBS@ ")
+ # also get rid of -lSDL2main, if you want to link against that use both SDL2::SDL2main and SDL2::SDL2 (in that order)
+ # (SDL2Config.cmake has the same behavior)
+ string(REPLACE "-lSDL2main" "" SDL2_EXTRA_LINK_FLAGS ${SDL2_EXTRA_LINK_FLAGS})
string(STRIP "${SDL2_EXTRA_LINK_FLAGS}" SDL2_EXTRA_LINK_FLAGS)
string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS_STATIC "@SDL_STATIC_LIBS@ ")
string(STRIP "${SDL2_EXTRA_LINK_FLAGS_STATIC}" SDL2_EXTRA_LINK_FLAGS_STATIC)
+if(WIN32 AND NOT MSVC)
+ # MINGW needs very special handling, because the link order must be exactly -lmingw32 -lSDL2main -lSDL2
+ # for it to work at all (and -mwindows somewhere); a normal SHARED IMPORTED or STATIC IMPORTED library always puts itself first
+ # so handle this like a header-only lib and put everything in INTERFACE_LINK_LIBRARIES
+
+ add_library(SDL2::SDL2 INTERFACE IMPORTED)
+ set_target_properties(SDL2::SDL2 PROPERTIES
+ INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIRS}"
+ INTERFACE_LINK_LIBRARIES "-L${SDL2_LIBDIR} -lSDL2")
+
+ add_library(SDL2::SDL2main INTERFACE IMPORTED)
+ set_target_properties(SDL2::SDL2main PROPERTIES
+ INTERFACE_LINK_LIBRARIES "-L${SDL2_LIBDIR} -lmingw32 -lSDL2main -mwindows")
+
+else() # (not WIN32) or MSVC
+
add_library(SDL2::SDL2 SHARED IMPORTED)
set_target_properties(SDL2::SDL2 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIRS}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
- IMPORTED_LOCATION "@libdir@/${CMAKE_SHARED_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX}"
- # TODO: on windows set IMPORTED_IMPLIB to SDL2.lib
- INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS}") # TODO: really? SDL2Config.cmake doesn't do this
+ IMPORTED_LOCATION "${SDL2_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX}")
+
+ if(MSVC)
+ # This file is generated when building SDL2 with autotools and MinGW, and MinGW/dlltool
+ # isn't able to generate .lib files that are usable by recent MSVC versions
+ # (something about "module unsafe for SAFESEH"; SAFESEH is enabled by default in MSVC).
+ # The .lib file for SDL2.dll *could* be generated with `gendef SDL2.dll` and then
+ # `lib.exe /machine:x86 /def:SDL2.def /out:SDL2.lib` (or /machine:amd64)
+ # but that requires lib.exe from a Visual Studio installation - and that still doesn't
+ # give you a static SDL2main.lib with SAFESEH support that you'll need (unless you don't use SDL2main)
+ # Note that when building SDL2 with CMake and MSVC, the result works with both MinGW and MSVC.
+
+ message(FATAL_ERROR, "This build of libSDL2 only supports MinGW, not MSVC (Visual C++), because it lacks .lib files!")
+ # MSVC needs SDL2.lib set as IMPORTED_IMPLIB to link against (comment out message() call above if you added SDL2.lib yourself)
+ set_target_properties(SDL2::SDL2 PROPERTIES IMPORTED_IMPLIB "${SDL2_LIBDIR}/SDL2.lib")
+ else()
+ # this mustn't be set for MSVC, so do it here in an extra call here
+ set_target_properties(SDL2::SDL2 PROPERTIES INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS}")
+ endif()
+
+ add_library(SDL2::SDL2main STATIC IMPORTED)
+ set_target_properties(SDL2::SDL2main PROPERTIES
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ IMPORTED_LOCATION "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2main${CMAKE_STATIC_LIBRARY_SUFFIX}")
+
+endif() # (not WIN32) or MSVC
add_library(SDL2::SDL2-static STATIC IMPORTED)
set_target_properties(SDL2::SDL2-static PROPERTIES
@@ -34,8 +78,4 @@ if(NOT TARGET SDL2::SDL2)
IMPORTED_LOCATION "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_STATIC_LIBRARY_SUFFIX}"
INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS_STATIC}")
- add_library(SDL2::SDL2main STATIC IMPORTED)
- set_target_properties(SDL2::SDL2main PROPERTIES
- IMPORTED_LINK_INTERFACE_LANGUAGES "C"
- IMPORTED_LOCATION "@libdir@/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2main${CMAKE_STATIC_LIBRARY_SUFFIX}")
-endif()
+endif() # NOT TARGET SDL2::SDL2