sdl3-config.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # SDL CMake configuration file:
  2. # This file is meant to be placed in Resources/CMake of a SDL3 framework
  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. # Compute the installation prefix relative to this file.
  29. get_filename_component(_sdl3_framework_path "${CMAKE_CURRENT_LIST_FILE}" PATH) # /SDL3.framework/Resources/CMake/
  30. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" PATH) # /SDL3.framework/Resources/
  31. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" PATH) # /SDL3.framework/
  32. get_filename_component(_sdl3_framework_parent_path "${_sdl3_framework_path}" PATH) # /
  33. set_and_check(_sdl3_include_dirs "${_sdl3_framework_path}/Headers")
  34. set(SDL3_LIBRARIES "SDL3::SDL3")
  35. # All targets are created, even when some might not be requested though COMPONENTS.
  36. # This is done for compatibility with CMake generated SDL3-target.cmake files.
  37. if(NOT TARGET SDL3::Headers)
  38. add_library(SDL3::Headers INTERFACE IMPORTED)
  39. set_target_properties(SDL3::Headers
  40. PROPERTIES
  41. INTERFACE_COMPILE_OPTIONS "SHELL:-F \"${_sdl3_framework_parent_path}\""
  42. INTERFACE_INCLUDE_DIRECTORIES "${_sdl3_include_dirs}"
  43. )
  44. endif()
  45. set(SDL3_Headers_FOUND TRUE)
  46. unset(_sdl3_include_dirs)
  47. if(NOT TARGET SDL3::SDL3-shared)
  48. add_library(SDL3::SDL3-shared SHARED IMPORTED)
  49. set_target_properties(SDL3::SDL3-shared
  50. PROPERTIES
  51. FRAMEWORK "TRUE"
  52. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  53. IMPORTED_LOCATION "${_sdl3_framework_path}/SDL3"
  54. IMPORTED_SONAME "${_sdl3_framework_path}/SDL3"
  55. COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
  56. INTERFACE_SDL3_SHARED "ON"
  57. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  58. INTERFACE_SDL_VERSION "SDL3"
  59. )
  60. endif()
  61. set(SDL3_SDL3-shared_FOUND TRUE)
  62. set(SDL3_SDL3-static FALSE)
  63. set(SDL3_SDL3_test FALSE)
  64. unset(_sdl3_framework_parent_path)
  65. unset(_sdl3_framework_path)
  66. if(SDL3_SDL3-shared_FOUND OR SDL3_SDL3-static_FOUND)
  67. set(SDL3_SDL3_FOUND TRUE)
  68. endif()
  69. function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
  70. if(CMAKE_VERSION VERSION_LESS "3.18")
  71. # Aliasing local targets is not supported on CMake < 3.18, so make it global.
  72. add_library(${NEW_TARGET} INTERFACE IMPORTED)
  73. set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
  74. else()
  75. add_library(${NEW_TARGET} ALIAS ${TARGET})
  76. endif()
  77. endfunction()
  78. # Make sure SDL3::SDL3 always exists
  79. if(NOT TARGET SDL3::SDL3)
  80. if(TARGET SDL3::SDL3-shared)
  81. _sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-shared)
  82. else()
  83. _sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-static)
  84. endif()
  85. endif()
  86. check_required_components(SDL3)