sdl2-config.cmake.in 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # sdl2 cmake project-config input for ./configure script
  2. include(FeatureSummary)
  3. set_package_properties(SDL2 PROPERTIES
  4. URL "https://www.libsdl.org/"
  5. DESCRIPTION "low level access to audio, keyboard, mouse, joystick, and graphics hardware"
  6. )
  7. # Copied from `configure_package_config_file`
  8. macro(set_and_check _var _file)
  9. set(${_var} "${_file}")
  10. if(NOT EXISTS "${_file}")
  11. message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  12. endif()
  13. endmacro()
  14. get_filename_component(CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR} REALPATH)
  15. get_filename_component(prefix "${CMAKE_CURRENT_LIST_DIR}/@cmake_prefix_relpath@" ABSOLUTE)
  16. set(exec_prefix "@exec_prefix@")
  17. set(bindir "@bindir@")
  18. set(libdir "@libdir@")
  19. set(includedir "@includedir@")
  20. set_and_check(SDL2_PREFIX "${prefix}")
  21. set_and_check(SDL2_EXEC_PREFIX "${exec_prefix}")
  22. set_and_check(SDL2_BINDIR "${bindir}")
  23. set_and_check(SDL2_INCLUDE_DIR "${includedir}/SDL2")
  24. set_and_check(SDL2_LIBDIR "${libdir}")
  25. set(SDL2_INCLUDE_DIRS "${includedir};${SDL2_INCLUDE_DIR}")
  26. set(SDL2_LIBRARIES SDL2::SDL2)
  27. set(SDL2_STATIC_LIBRARIES SDL2::SDL2-static)
  28. set(SDL2MAIN_LIBRARY)
  29. set(SDL2TEST_LIBRARY SDL2::SDL2test)
  30. unset(prefix)
  31. unset(exec_prefix)
  32. unset(bindir)
  33. unset(libdir)
  34. unset(includedir)
  35. set(_sdl2_libraries_in "@SDL_LIBS@")
  36. set(_sdl2_static_private_libs_in "@SDL_STATIC_LIBS@")
  37. # Convert _sdl2_libraries to list and keep only libraries + library directories
  38. string(REGEX MATCHALL "-[lm]([-a-zA-Z0-9._]+)" _sdl2_libraries "${_sdl2_libraries_in}")
  39. string(REGEX REPLACE "^-l" "" _sdl2_libraries "${_sdl2_libraries}")
  40. string(REGEX REPLACE ";-l" ";" _sdl2_libraries "${_sdl2_libraries}")
  41. string(REGEX MATCHALL "-L([-a-zA-Z0-9._/]+)" _sdl2_libdirs "${_sdl2_libraries_in}")
  42. string(REGEX REPLACE "^-L" "" _sdl2_libdirs "${_sdl2_libdirs}")
  43. string(REGEX REPLACE ";-L" ";" _sdl2_libdirs "${_sdl2_libdirs}")
  44. list(APPEND _sdl2_libdirs "${SDL2_LIBDIR}")
  45. # Convert _sdl2_static_private_libs to list and keep only libraries + library directories
  46. string(REGEX MATCHALL "(-[lm]([-a-zA-Z0-9._]+))|(-Wl,[^ ]*framework[^ ]*)|(-pthread)" _sdl2_static_private_libs "${_sdl2_static_private_libs_in}")
  47. string(REGEX REPLACE "^-l" "" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
  48. string(REGEX REPLACE ";-l" ";" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
  49. string(REGEX REPLACE "-Wl,-framework,([a-zA-Z0-9_]+)" "$<LINK_LIBRARY:FRAMEWORK,\\1>" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
  50. string(REGEX REPLACE "-Wl,-weak_framework,([a-zA-Z0-9_]+)" "$<LINK_LIBRARY:WEAK_FRAMEWORK,\\1>" _sdl2_static_private_libs "${_sdl2_static_private_libs}")
  51. string(REGEX MATCHALL "-L([-a-zA-Z0-9._/]+)" _sdl2_static_private_libdirs "${_sdl2_static_private_libs_in}")
  52. string(REGEX REPLACE "^-L" "" _sdl2_static_private_libdirs "${_sdl2_static_private_libdirs}")
  53. string(REGEX REPLACE ";-L" ";" _sdl2_static_private_libdirs "${_sdl2_static_private_libdirs}")
  54. # Set SDL2_NO_MWINDOWS to a true-ish value to not add the -mwindows link option
  55. if(SDL2_NO_MWINDOWS)
  56. list(REMOVE_ITEM _sdl2_libraries "-mwindows")
  57. endif()
  58. if(_sdl2_libraries MATCHES ".*SDL2main.*")
  59. list(INSERT SDL2_LIBRARIES 0 SDL2::SDL2main)
  60. list(INSERT SDL2_STATIC_LIBRARIES 0 SDL2::SDL2main)
  61. endif()
  62. set(_sdl2main_library ${SDL2_LIBDIR}/libSDL2main.a)
  63. if(EXISTS "${_sdl2main_library}")
  64. set(SDL2MAIN_LIBRARY SDL2::SDL2main)
  65. if(NOT TARGET SDL2::SDL2main)
  66. add_library(SDL2::SDL2main STATIC IMPORTED)
  67. set_target_properties(SDL2::SDL2main
  68. PROPERTIES
  69. IMPORTED_LOCATION "${_sdl2main_library}"
  70. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  71. INTERFACE_SDL_VERSION "SDL2"
  72. )
  73. if(WIN32)
  74. # INTERFACE_LINK_OPTIONS needs CMake 3.13
  75. cmake_minimum_required(VERSION 3.13)
  76. # Mark WinMain/WinMain@16 as undefined, such that it will be withheld by the linker.
  77. if(CMAKE_SIZEOF_VOID_P EQUAL 4)
  78. set_target_properties(SDL2::SDL2main
  79. PROPERTIES
  80. INTERFACE_LINK_OPTIONS "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-Wl,--undefined=_WinMain@16>"
  81. )
  82. else()
  83. set_target_properties(SDL2::SDL2main
  84. PROPERTIES
  85. INTERFACE_LINK_OPTIONS "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:-Wl,--undefined=WinMain>"
  86. )
  87. endif()
  88. endif()
  89. endif()
  90. set(SDL2_SDL2main_FOUND TRUE)
  91. else()
  92. set(SDL2_SDL2main_FOUND FALSE)
  93. endif()
  94. unset(_sdl2main_library)
  95. # Remove SDL2 since this is the "central" library
  96. # Remove SDL2main since this will be provided by SDL2::SDL2main (if available)
  97. # Remove mingw32 and cygwin since these are not needed when using `-Wl,--undefined,WinMain`
  98. set(_sdl2_link_libraries ${_sdl2_libraries})
  99. list(REMOVE_ITEM _sdl2_link_libraries SDL2 SDL2main mingw32 cygwin)
  100. if(WIN32)
  101. set(_sdl2_implib "${SDL2_LIBDIR}/libSDL2.dll.a")
  102. set(_sdl2_dll "${SDL2_BINDIR}/SDL2.dll")
  103. if(EXISTS "${_sdl2_implib}" AND EXISTS "${_sdl2_dll}")
  104. if(NOT TARGET SDL2::SDL2)
  105. add_library(SDL2::SDL2 SHARED IMPORTED)
  106. set_target_properties(SDL2::SDL2 PROPERTIES
  107. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  108. INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries}"
  109. INTERFACE_LINK_DIRECTORIES "${_sdl2_libdirs}"
  110. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  111. IMPORTED_IMPLIB "${_sdl2_implib}"
  112. IMPORTED_LOCATION "${_sdl2_dll}"
  113. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  114. INTERFACE_SDL_VERSION "SDL2"
  115. )
  116. endif()
  117. set(SDL2_SDL2_FOUND TRUE)
  118. else()
  119. set(SDL2_SDL2_FOUND FALSE)
  120. endif()
  121. unset(_sdl2_implib)
  122. unset(_sdl2_dll)
  123. else()
  124. set(_sdl2_shared "${SDL2_LIBDIR}/libSDL2${CMAKE_SHARED_LIBRARY_SUFFIX}")
  125. if(EXISTS "${_sdl2_shared}")
  126. if(NOT TARGET SDL2::SDL2)
  127. add_library(SDL2::SDL2 SHARED IMPORTED)
  128. set_target_properties(SDL2::SDL2 PROPERTIES
  129. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  130. INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries}"
  131. INTERFACE_LINK_DIRECTORIES "${_sdl2_libdirs}"
  132. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  133. IMPORTED_LOCATION "${_sdl2_shared}"
  134. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  135. INTERFACE_SDL_VERSION "SDL2"
  136. )
  137. endif()
  138. set(SDL2_SDL2_FOUND TRUE)
  139. else()
  140. set(SDL2_SDL2_FOUND FALSE)
  141. endif()
  142. unset(_sdl2_shared)
  143. endif()
  144. set(_sdl2_static "${SDL2_LIBDIR}/libSDL2.a")
  145. if(EXISTS "${_sdl2_static}")
  146. if(NOT TARGET SDL2::SDL2-static)
  147. add_library(SDL2::SDL2-static STATIC IMPORTED)
  148. set_target_properties(SDL2::SDL2-static
  149. PROPERTIES
  150. IMPORTED_LOCATION "${_sdl2_static}"
  151. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  152. INTERFACE_LINK_LIBRARIES "${_sdl2_link_libraries};${_sdl2_static_private_libs}"
  153. INTERFACE_LINK_DIRECTORIES "${_sdl2_libdirs};${_sdl2_static_private_libdirs}"
  154. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  155. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  156. INTERFACE_SDL_VERSION "SDL2"
  157. )
  158. endif()
  159. set(SDL2_SDL2-static_FOUND TRUE)
  160. else()
  161. set(SDL2_SDL2-static_FOUND FALSE)
  162. endif()
  163. unset(_sdl2_static)
  164. unset(_sdl2_link_libraries)
  165. set(_sdl2test_library "${SDL2_LIBDIR}/libSDL2_test.a")
  166. if(EXISTS "${_sdl2test_library}")
  167. if(NOT TARGET SDL2::SDL2test)
  168. add_library(SDL2::SDL2test STATIC IMPORTED)
  169. set_target_properties(SDL2::SDL2test
  170. PROPERTIES
  171. IMPORTED_LOCATION "${_sdl2test_library}"
  172. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
  173. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  174. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  175. INTERFACE_SDL_VERSION "SDL2"
  176. )
  177. endif()
  178. set(SDL2_SDL2test_FOUND TRUE)
  179. else()
  180. set(SDL2_SDL2test_FOUND FALSE)
  181. endif()
  182. unset(_sdl2test_library)
  183. # Copied from `configure_package_config_file`
  184. macro(check_required_components _NAME)
  185. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  186. if(NOT ${_NAME}_${comp}_FOUND)
  187. if(${_NAME}_FIND_REQUIRED_${comp})
  188. set(${_NAME}_FOUND FALSE)
  189. endif()
  190. endif()
  191. endforeach()
  192. endmacro()
  193. check_required_components(SDL2)
  194. # Create SDL2::SDL2 alias for static-only builds
  195. if(TARGET SDL2::SDL2-static AND NOT TARGET SDL2::SDL2)
  196. if(CMAKE_VERSION VERSION_LESS "3.18")
  197. # FIXME: Aliasing local targets is not supported on CMake < 3.18, so make it global.
  198. add_library(SDL2::SDL2 INTERFACE IMPORTED)
  199. set_target_properties(SDL2::SDL2 PROPERTIES INTERFACE_LINK_LIBRARIES "SDL2::SDL2-static")
  200. else()
  201. add_library(SDL2::SDL2 ALIAS SDL2::SDL2-static)
  202. endif()
  203. endif()