sdl3-config.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # SDL CMake configuration file:
  2. # This file is meant to be placed in a cmake subfolder of SDL3-devel-3.x.y-VC
  3. cmake_minimum_required(VERSION 3.0...3.5)
  4. include(FeatureSummary)
  5. set_package_properties(SDL3 PROPERTIES
  6. URL "https://www.libsdl.org/"
  7. DESCRIPTION "low level access to audio, keyboard, mouse, joystick, and graphics hardware"
  8. )
  9. # Copied from `configure_package_config_file`
  10. macro(set_and_check _var _file)
  11. set(${_var} "${_file}")
  12. if(NOT EXISTS "${_file}")
  13. message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  14. endif()
  15. endmacro()
  16. # Copied from `configure_package_config_file`
  17. macro(check_required_components _NAME)
  18. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  19. if(NOT ${_NAME}_${comp}_FOUND)
  20. if(${_NAME}_FIND_REQUIRED_${comp})
  21. set(${_NAME}_FOUND FALSE)
  22. endif()
  23. endif()
  24. endforeach()
  25. endmacro()
  26. set(SDL3_FOUND TRUE)
  27. if(CMAKE_SIZEOF_VOID_P STREQUAL "4")
  28. set(_sdl_arch_subdir "x86")
  29. elseif(CMAKE_SIZEOF_VOID_P STREQUAL "8")
  30. set(_sdl_arch_subdir "x64")
  31. else()
  32. set(SDL3_FOUND FALSE)
  33. return()
  34. endif()
  35. get_filename_component(_sdl3_prefix "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
  36. set_and_check(_sdl3_prefix "${_sdl3_prefix}")
  37. set(_sdl3_include_dirs "${_sdl3_prefix}/include;${_sdl3_prefix}/include/SDL3")
  38. set(_sdl3_library "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/SDL3.lib")
  39. set(_sdl3_dll_library "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/SDL3.dll")
  40. set(_sdl3test_library "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/SDL3_test.lib")
  41. unset(_sdl_arch_subdir)
  42. unset(_sdl3_prefix)
  43. # All targets are created, even when some might not be requested though COMPONENTS.
  44. # This is done for compatibility with CMake generated SDL3-target.cmake files.
  45. if(NOT TARGET SDL3::Headers)
  46. add_library(SDL3::Headers INTERFACE IMPORTED)
  47. set_target_properties(SDL3::Headers
  48. PROPERTIES
  49. INTERFACE_INCLUDE_DIRECTORIES "${_sdl3_include_dirs}"
  50. )
  51. endif()
  52. set(SDL3_Headers_FOUND TRUE)
  53. unset(_sdl3_include_dirs)
  54. if(EXISTS "${_sdl3_library}" AND EXISTS "${_sdl3_dll_library}")
  55. if(NOT TARGET SDL3::SDL3-shared)
  56. add_library(SDL3::SDL3-shared SHARED IMPORTED)
  57. set_target_properties(SDL3::SDL3-shared
  58. PROPERTIES
  59. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  60. IMPORTED_IMPLIB "${_sdl3_library}"
  61. IMPORTED_LOCATION "${_sdl3_dll_library}"
  62. COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
  63. INTERFACE_SDL3_SHARED "ON"
  64. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  65. INTERFACE_SDL_VERSION "SDL3"
  66. )
  67. endif()
  68. set(SDL3_SDL3-shared_FOUND TRUE)
  69. else()
  70. set(SDL3_SDL3-shared_FOUND FALSE)
  71. endif()
  72. unset(_sdl3_library)
  73. unset(_sdl3_dll_library)
  74. set(SDL3_SDL3-static_FOUND FALSE)
  75. if(EXISTS "${_sdl3test_library}")
  76. if(NOT TARGET SDL3::SDL3_test)
  77. add_library(SDL3::SDL3_test STATIC IMPORTED)
  78. set_target_properties(SDL3::SDL3_test
  79. PROPERTIES
  80. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  81. IMPORTED_LOCATION "${_sdl3test_library}"
  82. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  83. INTERFACE_SDL_VERSION "SDL3"
  84. )
  85. endif()
  86. set(SDL3_SDL3_test_FOUND TRUE)
  87. else()
  88. set(SDL3_SDL3_test_FOUND FALSE)
  89. endif()
  90. unset(_sdl3test_library)
  91. if(SDL3_SDL3-shared_FOUND)
  92. set(SDL3_SDL3_FOUND TRUE)
  93. endif()
  94. function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
  95. if(CMAKE_VERSION VERSION_LESS "3.18")
  96. # Aliasing local targets is not supported on CMake < 3.18, so make it global.
  97. add_library(${NEW_TARGET} INTERFACE IMPORTED)
  98. set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
  99. else()
  100. add_library(${NEW_TARGET} ALIAS ${TARGET})
  101. endif()
  102. endfunction()
  103. # Make sure SDL3::SDL3 always exists
  104. if(NOT TARGET SDL3::SDL3)
  105. if(TARGET SDL3::SDL3-shared)
  106. _sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-shared)
  107. endif()
  108. endif()
  109. check_required_components(SDL3)
  110. set(SDL3_LIBRARIES SDL3::SDL3)
  111. set(SDL3_STATIC_LIBRARIES SDL3::SDL3-static)
  112. set(SDL3_STATIC_PRIVATE_LIBS)
  113. set(SDL3TEST_LIBRARY SDL3::SDL3_test)