cmake: rewrite autotools' sdl2-config.cmake.in with IMPORTED targets + no more -lmingw32
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
diff --git a/sdl2-config.cmake.in b/sdl2-config.cmake.in
index c570511..16a2520 100644
--- a/sdl2-config.cmake.in
+++ b/sdl2-config.cmake.in
@@ -1,81 +1,194 @@
-# sdl2 cmake project-config input for ./configure scripts
+# sdl2 cmake project-config input for ./configure script
-set(prefix "@prefix@")
+include(FeatureSummary)
+set_package_properties(SDL2 PROPERTIES
+ URL "https://www.libsdl.org/"
+ DESCRIPTION "low level access to audio, keyboard, mouse, joystick, and graphics hardware"
+)
+
+# Copied from `configure_package_config_file`
+macro(set_and_check _var _file)
+ set(${_var} "${_file}")
+ if(NOT EXISTS "${_file}")
+ message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
+ endif()
+endmacro()
+
+# Copied from `configure_package_config_file`
+macro(check_required_components _NAME)
+ foreach(comp ${${_NAME}_FIND_COMPONENTS})
+ if(NOT ${_NAME}_${comp}_FOUND)
+ if(${_NAME}_FIND_REQUIRED_${comp})
+ set(${_NAME}_FOUND FALSE)
+ endif()
+ endif()
+ endforeach()
+endmacro()
+
+get_filename_component(prefix "${CMAKE_CURRENT_LIST_DIR}@cmake_prefix_relpath@" ABSOLUTE)
set(exec_prefix "@exec_prefix@")
+set(bindir "@bindir@")
set(libdir "@libdir@")
set(includedir "@includedir@")
-set(SDL2_PREFIX "${prefix}")
-set(SDL2_EXEC_PREFIX "${exec_prefix}")
-set(SDL2_LIBDIR "${libdir}")
-set(SDL2_INCLUDE_DIRS "${includedir}/SDL2")
-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 "${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")
+
+set_and_check(SDL2_PREFIX "${prefix}")
+set_and_check(SDL2_EXEC_PREFIX "${exec_prefix}")
+set_and_check(SDL2_BINDIR "${bindir}")
+set_and_check(SDL2_INCLUDE_DIR "${includedir}/SDL2")
+set_and_check(SDL2_LIBDIR "${libdir}")
+set(SDL2_INCLUDE_DIRS "${includedir};${SDL2_INCLUDE_DIR}")
+
+set(SDL2_LIBRARIES SDL2::SDL2)
+set(SDL2_STATIC_LIBRARIES SDL2::SDL2-static)
+set(SDL2MAIN_LIBRARY)
+set(SDL2TEST_LIBRARY SDL2::SDL2test)
+
+unset(prefix)
+unset(exec_prefix)
+unset(bindir)
+unset(libdir)
+unset(includedir)
+
+set(_sdl2_libraries "@SDL_LIBS@")
+set(_sdl2_static_private_libs "@SDL_STATIC_LIBS@")
+
+# Convert _sdl2_libraries to list and keep only libraries
+string(REGEX MATCHALL "-[lm]([a-zA-Z0-9._]+)" _sdl2_libraries "${_sdl2_libraries}")
+string(REGEX REPLACE "^-l" "" _sdl2_libraries "${_sdl2_libraries}")
+string(REGEX REPLACE ";-l" ";" _sdl2_libraries "${_sdl2_libraries}")
+
+# Convert _sdl2_static_private_libs to list and keep only libraries
+string(REGEX MATCHALL "(-[lm]([a-zA-Z0-9._]+))|(-Wl,[^ ]*framework[^ ]*)" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
+string(REGEX REPLACE "^-l" "" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
+string(REGEX REPLACE ";-l" ";" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
+
+if(_sdl2_libraries MATCHES ".*SDL2main.*")
+ list(INSERT SDL2_LIBRARIES 0 SDL2::SDL2main)
+ list(INSERT SDL2_STATIC_LIBRARIES 0 SDL2::SDL2main)
+ set(_sdl2main_library ${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2main${CMAKE_STATIC_LIBRARY_SUFFIX})
+ if(EXISTS "${_sdl2main_library}")
+ set(SDL2MAIN_LIBRARY SDL2::SDL2main)
+ if(NOT TARGET SDL2::SDL2main)
+ add_library(SDL2::SDL2main STATIC IMPORTED)
+ set_target_properties(SDL2::SDL2main
+ PROPERTIES
+ IMPORTED_LOCATION "${_sdl2main_library}"
+ )
+ if(WIN32)
+ # INTERFACE_LINK_OPTIONS needs CMake 3.13
+ cmake_minimum_required(VERSION 3.13)
+ # Mark WinMain/WinMain@16 as undefined, such that it will be withheld by the linker.
+ if(CMAKE_SIZEOF_VOID_P EQUAL 4)
+ set_target_properties(SDL2::SDL2main
+ PROPERTIES
+ INTERFACE_LINK_OPTIONS "-Wl,--undefined=_WinMain@16"
+ )
+ else()
+ set_target_properties(SDL2::SDL2main
+ PROPERTIES
+ INTERFACE_LINK_OPTIONS "-Wl,--undefined=WinMain"
+ )
+ endif()
+ endif()
+ endif()
+ set(SDL2_SDL2main_FOUND TRUE)
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}")
+ set(SDL2_SDL2main_FOUND FALSE)
endif()
+ unset(_sdl2main_library)
+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}")
+# Remove SDL2 since this is the "central" library
+# Remove SDL2main since this will be provided by SDL2::SDL2main (if available)
+# Remove mingw32 and cygwin since these are not needed when using `-Wl,--undefined,WinMain`
+set(_sdl2_link_libraries ${_sdl2_libraries})
+list(REMOVE_ITEM _sdl2_link_libraries SDL2 SDL2main mingw32 cygwin)
-endif() # (not WIN32) or MSVC
+if(WIN32)
+ set(_sdl2_implib "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}")
+ set(_sdl2_dll "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}")
+ if(EXISTS "${_sdl2_implib}" AND EXISTS "${_sdl2_dll}")
+ if(NOT TARGET SDL2::SDL2)
+ add_library(SDL2::SDL2 SHARED IMPORTED)
+ set_target_properties(SDL2::SDL2 PROPERTIES
+ INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
+ INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries}"
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ IMPORTED_IMPLIB "${_sdl2_implib}"
+ IMPORTED_LOCATION "${_sdl2_dll}"
+ )
+ endif()
+ set(SDL2_SDL2_FOUND TRUE)
+ else()
+ set(SDL2_SDL2_FOUND FALSE)
+ endif()
+ unset(_sdl2_implib)
+ unset(_sdl2_dll)
+else()
+ set(_sdl2_shared "${SDL2_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX}")
+ if(EXISTS "${_sdl2_shared}")
+ if(NOT TARGET SDL2::SDL2)
+ add_library(SDL2::SDL2 SHARED IMPORTED)
+ set_target_properties(SDL2::SDL2 PROPERTIES
+ INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
+ INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries}"
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ IMPORTED_LOCATION "${_sdl2_shared}"
+ )
+ endif()
+ set(SDL2_SDL2_FOUND TRUE)
+ else()
+ set(SDL2_SDL2_FOUND FALSE)
+ endif()
+ unset(_sdl2_shared)
+endif()
+
+set(_sdl2_static "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_STATIC_LIBRARY_SUFFIX}")
+if(EXISTS "${_sdl2_static}")
+ if(NOT TARGET SDL2::SDL2-static)
+ add_library(SDL2::SDL2-static STATIC IMPORTED)
+ set_target_properties(SDL2::SDL2-static
+ PROPERTIES
+ IMPORTED_LOCATION "${_sdl2_static}"
+ INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
+ INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries};${_sdl2_static_private_libs}"
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ )
+ endif()
+ set(SDL2_SDL2-static_FOUND TRUE)
+else()
+ set(SDL2_SDL2-static_FOUND FALSE)
+endif()
+unset(_sdl2_static)
+
+unset(_sdl2_link_libraries)
+
+set(_sdl2test_library "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2_test${CMAKE_STATIC_LIBRARY_SUFFIX}")
+if(EXISTS "${_sdl2test_library}")
+ if(NOT TARGET SDL2::SDL2test)
+ add_library(SDL2::SDL2test STATIC IMPORTED)
+ set_target_properties(SDL2::SDL2test
+ PROPERTIES
+ IMPORTED_LOCATION "_sdl2test_library"
+ INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
+ IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+ )
+ endif()
+ set(SDL2_SDL2test_FOUND TRUE)
+else()
+ set(SDL2_SDL2test_FOUND FALSE)
+endif()
+unset(_sdl2test_library)
- add_library(SDL2::SDL2-static STATIC IMPORTED)
- set_target_properties(SDL2::SDL2-static PROPERTIES
- INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIRS}"
- IMPORTED_LINK_INTERFACE_LANGUAGES "C"
- IMPORTED_LOCATION "${SDL2_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_STATIC_LIBRARY_SUFFIX}"
- INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS_STATIC}")
+# Copied from `configure_package_config_file`
+macro(check_required_components _NAME)
+ foreach(comp ${${_NAME}_FIND_COMPONENTS})
+ if(NOT ${_NAME}_${comp}_FOUND)
+ if(${_NAME}_FIND_REQUIRED_${comp})
+ set(${_NAME}_FOUND FALSE)
+ endif()
+ endif()
+ endforeach()
+endmacro()
-endif() # NOT TARGET SDL2::SDL2
+check_required_components(SDL2)