FindSDL_image.cmake 6.3 KB

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