sdl3-config.cmake 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # SDL CMake configuration file:
  2. # This file is meant to be placed in share/cmake/SDL3, next to SDL3.xcframework
  3. # INTERFACE_LINK_OPTIONS needs CMake 3.12
  4. cmake_minimum_required(VERSION 3.12)
  5. include(FeatureSummary)
  6. set_package_properties(SDL3 PROPERTIES
  7. URL "https://www.libsdl.org/"
  8. DESCRIPTION "low level access to audio, keyboard, mouse, joystick, and graphics hardware"
  9. )
  10. # Copied from `configure_package_config_file`
  11. macro(set_and_check _var _file)
  12. set(${_var} "${_file}")
  13. if(NOT EXISTS "${_file}")
  14. message(FATAL_ERROR "File or directory ${_file} referenced by variable ${_var} does not exist !")
  15. endif()
  16. endmacro()
  17. # Copied from `configure_package_config_file`
  18. macro(check_required_components _NAME)
  19. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  20. if(NOT ${_NAME}_${comp}_FOUND)
  21. if(${_NAME}_FIND_REQUIRED_${comp})
  22. set(${_NAME}_FOUND FALSE)
  23. endif()
  24. endif()
  25. endforeach()
  26. endmacro()
  27. set(SDL3_FOUND TRUE)
  28. macro(_check_target_is_simulator)
  29. include(CheckCSourceCompiles)
  30. check_c_source_compiles([===[
  31. #include <TargetConditionals.h>
  32. #if defined(TARGET_OS_SIMULATOR)
  33. int target_is_simulator;
  34. #endif
  35. int main(int argc, char *argv[]) { return target_is_simulator; }
  36. ]===] SDL_TARGET_IS_SIMULATOR)
  37. endmacro()
  38. if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
  39. _check_target_is_simulator()
  40. if(SDL_TARGET_IS_SIMULATOR)
  41. set(_xcfw_target_subdir "ios-arm64_x86_64-simulator")
  42. else()
  43. set(_xcfw_target_subdir "ios-arm64")
  44. endif()
  45. elseif(CMAKE_SYSTEM_NAME STREQUAL "tvOS")
  46. _check_target_is_simulator()
  47. if(SDL_TARGET_IS_SIMULATOR)
  48. set(_xcfw_target_subdir "tvos-arm64_x86_64-simulator")
  49. else()
  50. set(_xcfw_target_subdir "tvos-arm64")
  51. endif()
  52. elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  53. set(_xcfw_target_subdir "macos-arm64_x86_64")
  54. else()
  55. message(WARNING "Unsupported Apple platform (${CMAKE_SYSTEM_NAME}) and broken sdl3-config-version.cmake")
  56. set(SDL3_FOUND FALSE)
  57. return()
  58. endif()
  59. # Compute the installation prefix relative to this file.
  60. get_filename_component(_sdl3_xcframework_parent_path "${CMAKE_CURRENT_LIST_DIR}" REALPATH) # /share/cmake/SDL3/
  61. get_filename_component(_sdl3_xcframework_parent_path "${_sdl3_xcframework_parent_path}" REALPATH) # /share/cmake/SDL3/
  62. get_filename_component(_sdl3_xcframework_parent_path "${_sdl3_xcframework_parent_path}" PATH) # /share/cmake
  63. get_filename_component(_sdl3_xcframework_parent_path "${_sdl3_xcframework_parent_path}" PATH) # /share
  64. get_filename_component(_sdl3_xcframework_parent_path "${_sdl3_xcframework_parent_path}" PATH) # /
  65. set_and_check(_sdl3_xcframework_path "${_sdl3_xcframework_parent_path}/SDL3.xcframework") # /SDL3.xcframework
  66. set_and_check(_sdl3_framework_parent_path "${_sdl3_xcframework_path}/${_xcfw_target_subdir}") # /SDL3.xcframework/macos-arm64_x86_64
  67. set_and_check(_sdl3_framework_path "${_sdl3_framework_parent_path}/SDL3.framework") # /SDL3.xcframework/macos-arm64_x86_64/SDL3.framework
  68. set_and_check(_sdl3_include_dirs "${_sdl3_framework_path}/Headers")
  69. # All targets are created, even when some might not be requested though COMPONENTS.
  70. # This is done for compatibility with CMake generated SDL3-target.cmake files.
  71. if(NOT TARGET SDL3::Headers)
  72. add_library(SDL3::Headers INTERFACE IMPORTED)
  73. set_target_properties(SDL3::Headers
  74. PROPERTIES
  75. INTERFACE_COMPILE_OPTIONS "SHELL:-F \"${_sdl3_framework_parent_path}\""
  76. INTERFACE_INCLUDE_DIRECTORIES "${_sdl3_include_dirs}"
  77. )
  78. endif()
  79. set(SDL3_Headers_FOUND TRUE)
  80. if(NOT TARGET SDL3::SDL3-shared)
  81. add_library(SDL3::SDL3-shared SHARED IMPORTED)
  82. if(CMAKE_VERSION GREATER_EQUAL "3.28")
  83. set_target_properties(SDL3::SDL3-shared
  84. PROPERTIES
  85. FRAMEWORK "TRUE"
  86. IMPORTED_LOCATION "${_sdl3_xcframework_path}"
  87. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  88. )
  89. else()
  90. set_target_properties(SDL3::SDL3-shared
  91. PROPERTIES
  92. FRAMEWORK "TRUE"
  93. IMPORTED_LOCATION "${_sdl3_framework_path}/SDL3"
  94. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  95. )
  96. endif()
  97. set_target_properties(SDL3::SDL3-shared
  98. PROPERTIES
  99. COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
  100. INTERFACE_SDL3_SHARED "ON"
  101. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  102. INTERFACE_SDL_VERSION "SDL3"
  103. )
  104. endif()
  105. set(SDL3_SDL3-shared_FOUND TRUE)
  106. set(SDL3_SDL3-static FALSE)
  107. set(SDL3_SDL3_test FALSE)
  108. unset(_sdl3_xcframework_parent_path)
  109. unset(_sdl3_xcframework_path)
  110. unset(_sdl3_framework_parent_path)
  111. unset(_sdl3_framework_path)
  112. unset(_sdl3_include_dirs)
  113. if(SDL3_SDL3-shared_FOUND)
  114. set(SDL3_SDL3_FOUND TRUE)
  115. endif()
  116. function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
  117. if(CMAKE_VERSION VERSION_LESS "3.18")
  118. # Aliasing local targets is not supported on CMake < 3.18, so make it global.
  119. add_library(${NEW_TARGET} INTERFACE IMPORTED)
  120. set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
  121. else()
  122. add_library(${NEW_TARGET} ALIAS ${TARGET})
  123. endif()
  124. endfunction()
  125. # Make sure SDL3::SDL3 always exists
  126. if(NOT TARGET SDL3::SDL3)
  127. if(TARGET SDL3::SDL3-shared)
  128. _sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-shared)
  129. endif()
  130. endif()
  131. check_required_components(SDL3)
  132. set(SDL3_LIBRARIES SDL3::SDL3)
  133. set(SDL3_STATIC_LIBRARIES SDL3::SDL3-static)
  134. set(SDL3_STATIC_PRIVATE_LIBS)
  135. set(SDL3TEST_LIBRARY SDL3::SDL3_test)