sdl3-config.cmake 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # SDL3 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. set(_sdl3_framework_path "${CMAKE_CURRENT_LIST_DIR}") # > /SDL3.framework/Resources/CMake/
  30. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" REALPATH) # > /SDL3.framework/Versions/Current/Resources/CMake
  31. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" REALPATH) # > /SDL3.framework/Versions/A/Resources/CMake/
  32. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" PATH) # > /SDL3.framework/Versions/A/Resources/
  33. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" PATH) # > /SDL3.framework/Versions/A/
  34. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" PATH) # > /SDL3.framework/Versions/
  35. get_filename_component(_sdl3_framework_path "${_sdl3_framework_path}" PATH) # > /SDL3.framework/
  36. get_filename_component(_sdl3_framework_parent_path "${_sdl3_framework_path}" PATH) # > /
  37. # All targets are created, even when some might not be requested though COMPONENTS.
  38. # This is done for compatibility with CMake generated SDL3-target.cmake files.
  39. if(NOT TARGET SDL3::Headers)
  40. add_library(SDL3::Headers INTERFACE IMPORTED)
  41. set_target_properties(SDL3::Headers
  42. PROPERTIES
  43. INTERFACE_COMPILE_OPTIONS "SHELL:-F \"${_sdl3_framework_parent_path}\""
  44. )
  45. endif()
  46. set(SDL3_Headers_FOUND TRUE)
  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. IMPORTED_LOCATION "${_sdl3_framework_path}/SDL3"
  53. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  54. COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
  55. INTERFACE_SDL3_SHARED "ON"
  56. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  57. INTERFACE_SDL_VERSION "SDL3"
  58. )
  59. endif()
  60. set(SDL3_SDL3-shared_FOUND TRUE)
  61. set(SDL3_SDL3-static FALSE)
  62. set(SDL3_SDL3_test FALSE)
  63. unset(_sdl3_framework_parent_path)
  64. unset(_sdl3_framework_path)
  65. if(SDL3_SDL3-shared_FOUND)
  66. set(SDL3_SDL3_FOUND TRUE)
  67. endif()
  68. function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
  69. if(CMAKE_VERSION VERSION_LESS "3.18")
  70. # Aliasing local targets is not supported on CMake < 3.18, so make it global.
  71. add_library(${NEW_TARGET} INTERFACE IMPORTED)
  72. set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
  73. else()
  74. add_library(${NEW_TARGET} ALIAS ${TARGET})
  75. endif()
  76. endfunction()
  77. # Make sure SDL3::SDL3 always exists
  78. if(NOT TARGET SDL3::SDL3)
  79. if(TARGET SDL3::SDL3-shared)
  80. _sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-shared)
  81. endif()
  82. endif()
  83. check_required_components(SDL3)
  84. set(SDL3_LIBRARIES SDL3::SDL3)
  85. set(SDL3_STATIC_LIBRARIES SDL3::SDL3-static)
  86. set(SDL3_STATIC_PRIVATE_LIBS)
  87. set(SDL3TEST_LIBRARY SDL3::SDL3_test)