FindSDL_image.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindSDL_image
  5. -------------
  6. Finds the SDL_image library that loads images of various formats as SDL (Simple
  7. DirectMedia Layer) surfaces.
  8. .. note::
  9. This module is specifically intended for SDL_image version 1. Starting with
  10. version 2.6, SDL_image provides a CMake package configuration file when built
  11. with CMake and should be found using ``find_package(SDL2_image)``. Similarly,
  12. SDL_image version 3 can be found using ``find_package(SDL3_image)``. These
  13. newer versions provide :ref:`Imported Targets` that encapsulate usage
  14. requirements. Refer to the official SDL documentation for more information.
  15. Result Variables
  16. ^^^^^^^^^^^^^^^^
  17. This module defines the following variables:
  18. ``SDL_image_FOUND``
  19. Boolean indicating whether the (requested version of) SDL_image library is
  20. found. For backward compatibility, the ``SDL_IMAGE_FOUND`` variable is also
  21. set to the same value.
  22. ``SDL_IMAGE_VERSION_STRING``
  23. The human-readable string containing the version of SDL_image found.
  24. ``SDL_IMAGE_INCLUDE_DIRS``
  25. Include directories containing headers needed to use the SDL_image library.
  26. ``SDL_IMAGE_LIBRARIES``
  27. Libraries needed to link against to use the SDL_image library.
  28. Hints
  29. ^^^^^
  30. This module accepts the following variables:
  31. ``SDLDIR``
  32. Environment variable that can be set to help locate an SDL library installed
  33. in a custom location. It should point to the installation destination that
  34. was used when configuring, building, and installing SDL library:
  35. ``./configure --prefix=$SDLDIR``.
  36. Deprecated Variables
  37. ^^^^^^^^^^^^^^^^^^^^
  38. For backward compatibility the following variables are also set:
  39. ``SDLIMAGE_FOUND``
  40. .. deprecated:: 2.8.10
  41. Use the ``SDL_image_FOUND``, which has the same value.
  42. ``SDLIMAGE_INCLUDE_DIR``
  43. .. deprecated:: 2.8.10
  44. Use the ``SDL_IMAGE_INCLUDE_DIRS``, which has the same value.
  45. ``SDLIMAGE_LIBRARY``
  46. .. deprecated:: 2.8.10
  47. Use the ``SDL_IMAGE_LIBRARIES``, which has the same value.
  48. Examples
  49. ^^^^^^^^
  50. Finding SDL_image library and creating an imported interface target for linking
  51. it to a project target:
  52. .. code-block:: cmake
  53. find_package(SDL_image)
  54. if(SDL_image_FOUND AND NOT TARGET SDL::SDL_image)
  55. add_library(SDL::SDL_image INTERFACE IMPORTED)
  56. set_target_properties(
  57. SDL::SDL_image
  58. PROPERTIES
  59. INTERFACE_INCLUDE_DIRECTORIES "${SDL_IMAGE_INCLUDE_DIRS}"
  60. INTERFACE_LINK_LIBRARIES "${SDL_IMAGE_LIBRARIES}"
  61. )
  62. endif()
  63. target_link_libraries(project_target PRIVATE SDL::SDL_image)
  64. When working with SDL_image version 2, the upstream package provides the
  65. ``SDL2_image::SDL2_image`` imported target directly. It can be used in a
  66. project without using this module:
  67. .. code-block:: cmake
  68. find_package(SDL2_image)
  69. target_link_libraries(project_target PRIVATE SDL2_image::SDL2_image)
  70. Similarly, for SDL_image version 3:
  71. .. code-block:: cmake
  72. find_package(SDL3_image)
  73. target_link_libraries(project_target PRIVATE SDL3_image::SDL3_image)
  74. See Also
  75. ^^^^^^^^
  76. * The :module:`FindSDL` module to find the main SDL library.
  77. #]=======================================================================]
  78. cmake_policy(PUSH)
  79. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  80. if(NOT SDL_IMAGE_INCLUDE_DIR AND SDLIMAGE_INCLUDE_DIR)
  81. set(SDL_IMAGE_INCLUDE_DIR ${SDLIMAGE_INCLUDE_DIR} CACHE PATH "directory cache
  82. entry initialized from old variable name")
  83. endif()
  84. find_path(SDL_IMAGE_INCLUDE_DIR SDL_image.h
  85. HINTS
  86. ENV SDLIMAGEDIR
  87. ENV SDLDIR
  88. PATH_SUFFIXES SDL
  89. # path suffixes to search inside ENV{SDLDIR}
  90. include/SDL include/SDL12 include/SDL11 include
  91. )
  92. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  93. set(VC_LIB_PATH_SUFFIX lib/x64)
  94. else()
  95. set(VC_LIB_PATH_SUFFIX lib/x86)
  96. endif()
  97. if(NOT SDL_IMAGE_LIBRARY AND SDLIMAGE_LIBRARY)
  98. set(SDL_IMAGE_LIBRARY ${SDLIMAGE_LIBRARY} CACHE FILEPATH "file cache entry
  99. initialized from old variable name")
  100. endif()
  101. find_library(SDL_IMAGE_LIBRARY
  102. NAMES SDL_image
  103. HINTS
  104. ENV SDLIMAGEDIR
  105. ENV SDLDIR
  106. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  107. )
  108. if(SDL_IMAGE_INCLUDE_DIR AND EXISTS "${SDL_IMAGE_INCLUDE_DIR}/SDL_image.h")
  109. file(STRINGS "${SDL_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$")
  110. file(STRINGS "${SDL_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+[0-9]+$")
  111. file(STRINGS "${SDL_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+[0-9]+$")
  112. string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_IMAGE_VERSION_MAJOR "${SDL_IMAGE_VERSION_MAJOR_LINE}")
  113. string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_IMAGE_VERSION_MINOR "${SDL_IMAGE_VERSION_MINOR_LINE}")
  114. string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_IMAGE_VERSION_PATCH "${SDL_IMAGE_VERSION_PATCH_LINE}")
  115. set(SDL_IMAGE_VERSION_STRING ${SDL_IMAGE_VERSION_MAJOR}.${SDL_IMAGE_VERSION_MINOR}.${SDL_IMAGE_VERSION_PATCH})
  116. unset(SDL_IMAGE_VERSION_MAJOR_LINE)
  117. unset(SDL_IMAGE_VERSION_MINOR_LINE)
  118. unset(SDL_IMAGE_VERSION_PATCH_LINE)
  119. unset(SDL_IMAGE_VERSION_MAJOR)
  120. unset(SDL_IMAGE_VERSION_MINOR)
  121. unset(SDL_IMAGE_VERSION_PATCH)
  122. endif()
  123. set(SDL_IMAGE_LIBRARIES ${SDL_IMAGE_LIBRARY})
  124. set(SDL_IMAGE_INCLUDE_DIRS ${SDL_IMAGE_INCLUDE_DIR})
  125. include(FindPackageHandleStandardArgs)
  126. find_package_handle_standard_args(SDL_image
  127. REQUIRED_VARS SDL_IMAGE_LIBRARIES SDL_IMAGE_INCLUDE_DIRS
  128. VERSION_VAR SDL_IMAGE_VERSION_STRING)
  129. # for backward compatibility
  130. set(SDLIMAGE_LIBRARY ${SDL_IMAGE_LIBRARIES})
  131. set(SDLIMAGE_INCLUDE_DIR ${SDL_IMAGE_INCLUDE_DIRS})
  132. set(SDLIMAGE_FOUND ${SDL_image_FOUND})
  133. mark_as_advanced(SDL_IMAGE_LIBRARY SDL_IMAGE_INCLUDE_DIR)
  134. cmake_policy(POP)