sdl3-config.cmake 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_SYSTEM_PROCESSOR MATCHES "([aA][mM][dD]64|[xX]86(_|-64)?|[iI][34567]86)")
  28. if(CMAKE_SIZEOF_VOID_P EQUAL "4")
  29. set(_sdl_arch_subdir "x86")
  30. elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
  31. set(_sdl_arch_subdir "x64")
  32. else()
  33. set(SDL3_FOUND FALSE)
  34. return()
  35. endif()
  36. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "([aA][aA][rR][cC][hH]64|[aA][rR][mM]64)")
  37. if(CMAKE_SIZEOF_VOID_P EQUAL "8")
  38. set(_sdl_arch_subdir "arm64")
  39. else()
  40. set(SDL3_FOUND FALSE)
  41. return()
  42. endif()
  43. else()
  44. set(SDL3_FOUND FALSE)
  45. return()
  46. endif()
  47. get_filename_component(_sdl3_prefix "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
  48. set_and_check(_sdl3_prefix "${_sdl3_prefix}")
  49. set(_sdl3_include_dirs "${_sdl3_prefix}/include;${_sdl3_prefix}/include/SDL3")
  50. set(_sdl3_library "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/SDL3.lib")
  51. set(_sdl3_dll_library "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/SDL3.dll")
  52. set(_sdl3test_library "${_sdl3_prefix}/lib/${_sdl_arch_subdir}/SDL3_test.lib")
  53. unset(_sdl_arch_subdir)
  54. unset(_sdl3_prefix)
  55. # All targets are created, even when some might not be requested though COMPONENTS.
  56. # This is done for compatibility with CMake generated SDL3-target.cmake files.
  57. if(NOT TARGET SDL3::Headers)
  58. add_library(SDL3::Headers INTERFACE IMPORTED)
  59. set_target_properties(SDL3::Headers
  60. PROPERTIES
  61. INTERFACE_INCLUDE_DIRECTORIES "${_sdl3_include_dirs}"
  62. )
  63. endif()
  64. set(SDL3_Headers_FOUND TRUE)
  65. unset(_sdl3_include_dirs)
  66. if(EXISTS "${_sdl3_library}" AND EXISTS "${_sdl3_dll_library}")
  67. if(NOT TARGET SDL3::SDL3-shared)
  68. add_library(SDL3::SDL3-shared SHARED IMPORTED)
  69. set_target_properties(SDL3::SDL3-shared
  70. PROPERTIES
  71. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  72. IMPORTED_IMPLIB "${_sdl3_library}"
  73. IMPORTED_LOCATION "${_sdl3_dll_library}"
  74. COMPATIBLE_INTERFACE_BOOL "SDL3_SHARED"
  75. INTERFACE_SDL3_SHARED "ON"
  76. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  77. INTERFACE_SDL_VERSION "SDL3"
  78. )
  79. endif()
  80. set(SDL3_SDL3-shared_FOUND TRUE)
  81. else()
  82. set(SDL3_SDL3-shared_FOUND FALSE)
  83. endif()
  84. unset(_sdl3_library)
  85. unset(_sdl3_dll_library)
  86. set(SDL3_SDL3-static_FOUND FALSE)
  87. if(EXISTS "${_sdl3test_library}")
  88. if(NOT TARGET SDL3::SDL3_test)
  89. add_library(SDL3::SDL3_test STATIC IMPORTED)
  90. set_target_properties(SDL3::SDL3_test
  91. PROPERTIES
  92. INTERFACE_LINK_LIBRARIES "SDL3::Headers"
  93. IMPORTED_LOCATION "${_sdl3test_library}"
  94. COMPATIBLE_INTERFACE_STRING "SDL_VERSION"
  95. INTERFACE_SDL_VERSION "SDL3"
  96. )
  97. endif()
  98. set(SDL3_SDL3_test_FOUND TRUE)
  99. else()
  100. set(SDL3_SDL3_test_FOUND FALSE)
  101. endif()
  102. unset(_sdl3test_library)
  103. if(SDL3_SDL3-shared_FOUND)
  104. set(SDL3_SDL3_FOUND TRUE)
  105. endif()
  106. function(_sdl_create_target_alias_compat NEW_TARGET TARGET)
  107. if(CMAKE_VERSION VERSION_LESS "3.18")
  108. # Aliasing local targets is not supported on CMake < 3.18, so make it global.
  109. add_library(${NEW_TARGET} INTERFACE IMPORTED)
  110. set_target_properties(${NEW_TARGET} PROPERTIES INTERFACE_LINK_LIBRARIES "${TARGET}")
  111. else()
  112. add_library(${NEW_TARGET} ALIAS ${TARGET})
  113. endif()
  114. endfunction()
  115. # Make sure SDL3::SDL3 always exists
  116. if(NOT TARGET SDL3::SDL3)
  117. if(TARGET SDL3::SDL3-shared)
  118. _sdl_create_target_alias_compat(SDL3::SDL3 SDL3::SDL3-shared)
  119. endif()
  120. endif()
  121. check_required_components(SDL3)
  122. set(SDL3_LIBRARIES SDL3::SDL3)
  123. set(SDL3_STATIC_LIBRARIES SDL3::SDL3-static)
  124. set(SDL3_STATIC_PRIVATE_LIBS)
  125. set(SDL3TEST_LIBRARY SDL3::SDL3_test)