FindSDL_ttf.cmake 6.1 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_ttf
  5. -----------
  6. Finds the SDL_ttf library that provides support for rendering text with TrueType
  7. fonts in SDL (Simple DirectMedia Layer) applications:
  8. .. code-block:: cmake
  9. find_package(SDL_ttf [<version>] [...])
  10. .. note::
  11. This module is specifically intended for SDL_ttf version 1. Starting with
  12. version 2.0.15, SDL_ttf provides a CMake package configuration file when built
  13. with CMake and should be found using ``find_package(SDL2_ttf)``. Similarly,
  14. SDL_ttf version 3 can be found using ``find_package(SDL3_ttf)``. These newer
  15. versions provide :ref:`Imported Targets` that encapsulate usage requirements.
  16. Refer to the official SDL documentation for more information.
  17. Result Variables
  18. ^^^^^^^^^^^^^^^^
  19. This module defines the following variables:
  20. ``SDL_ttf_FOUND``
  21. Boolean indicating whether the (requested version of) SDL_ttf library is
  22. found. For backward compatibility, the ``SDL_TTF_FOUND`` variable is also set
  23. to the same value.
  24. ``SDL_ttf_VERSION``
  25. .. versionadded:: 4.2
  26. The human-readable string containing the version of SDL_ttf found.
  27. ``SDL_TTF_INCLUDE_DIRS``
  28. Include directories containing headers needed to use SDL_ttf library.
  29. ``SDL_TTF_LIBRARIES``
  30. Libraries needed to link against to use SDL_ttf.
  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_TTF_VERSION_STRING``
  43. .. deprecated:: 4.2
  44. Use the ``SDL_ttf_VERSION``.
  45. The human-readable string containing the version of SDL_ttf found.
  46. ``SDLTTF_FOUND``
  47. .. deprecated:: 2.8.10
  48. Replaced with ``SDL_ttf_FOUND``, which has the same value.
  49. ``SDLTTF_INCLUDE_DIR``
  50. .. deprecated:: 2.8.10
  51. Replaced with ``SDL_TTF_INCLUDE_DIRS``, which has the same value.
  52. ``SDLTTF_LIBRARY``
  53. .. deprecated:: 2.8.10
  54. Replaced with ``SDL_TTF_LIBRARIES``, which has the same value.
  55. Examples
  56. ^^^^^^^^
  57. Finding SDL_ttf library and creating an imported interface target for linking
  58. it to a project target:
  59. .. code-block:: cmake
  60. find_package(SDL_ttf)
  61. if(SDL_ttf_FOUND AND NOT TARGET SDL::SDL_ttf)
  62. add_library(SDL::SDL_ttf INTERFACE IMPORTED)
  63. set_target_properties(
  64. SDL::SDL_ttf
  65. PROPERTIES
  66. INTERFACE_INCLUDE_DIRECTORIES "${SDL_TTF_INCLUDE_DIRS}"
  67. INTERFACE_LINK_LIBRARIES "${SDL_TTF_LIBRARIES}"
  68. )
  69. endif()
  70. target_link_libraries(project_target PRIVATE SDL::SDL_ttf)
  71. When working with SDL_ttf version 2, the upstream package provides the
  72. ``SDL2_ttf::SDL2_ttf`` imported target directly. It can be used in a project
  73. without using this module:
  74. .. code-block:: cmake
  75. find_package(SDL2_ttf)
  76. target_link_libraries(project_target PRIVATE SDL2_ttf::SDL2_ttf)
  77. Similarly, for SDL_ttf version 3:
  78. .. code-block:: cmake
  79. find_package(SDL3_ttf)
  80. target_link_libraries(project_target PRIVATE SDL3_ttf::SDL3_ttf)
  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_TTF_INCLUDE_DIR AND SDLTTF_INCLUDE_DIR)
  88. set(SDL_TTF_INCLUDE_DIR ${SDLTTF_INCLUDE_DIR} CACHE PATH "directory cache
  89. entry initialized from old variable name")
  90. endif()
  91. find_path(SDL_TTF_INCLUDE_DIR SDL_ttf.h
  92. HINTS
  93. ENV SDLTTFDIR
  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_TTF_LIBRARY AND SDLTTF_LIBRARY)
  105. set(SDL_TTF_LIBRARY ${SDLTTF_LIBRARY} CACHE FILEPATH "file cache entry
  106. initialized from old variable name")
  107. endif()
  108. find_library(SDL_TTF_LIBRARY
  109. NAMES SDL_ttf
  110. HINTS
  111. ENV SDLTTFDIR
  112. ENV SDLDIR
  113. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  114. )
  115. if(SDL_TTF_INCLUDE_DIR AND EXISTS "${SDL_TTF_INCLUDE_DIR}/SDL_ttf.h")
  116. file(STRINGS "${SDL_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL_TTF_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+[0-9]+$")
  117. file(STRINGS "${SDL_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL_TTF_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_TTF_MINOR_VERSION[ \t]+[0-9]+$")
  118. file(STRINGS "${SDL_TTF_INCLUDE_DIR}/SDL_ttf.h" SDL_TTF_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_TTF_PATCHLEVEL[ \t]+[0-9]+$")
  119. string(REGEX REPLACE "^#define[ \t]+SDL_TTF_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_TTF_VERSION_MAJOR "${SDL_TTF_VERSION_MAJOR_LINE}")
  120. string(REGEX REPLACE "^#define[ \t]+SDL_TTF_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_TTF_VERSION_MINOR "${SDL_TTF_VERSION_MINOR_LINE}")
  121. string(REGEX REPLACE "^#define[ \t]+SDL_TTF_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_TTF_VERSION_PATCH "${SDL_TTF_VERSION_PATCH_LINE}")
  122. set(SDL_ttf_VERSION ${SDL_TTF_VERSION_MAJOR}.${SDL_TTF_VERSION_MINOR}.${SDL_TTF_VERSION_PATCH})
  123. set(SDL_TTF_VERSION_STRING "${SDL_ttf_VERSION}")
  124. unset(SDL_TTF_VERSION_MAJOR_LINE)
  125. unset(SDL_TTF_VERSION_MINOR_LINE)
  126. unset(SDL_TTF_VERSION_PATCH_LINE)
  127. unset(SDL_TTF_VERSION_MAJOR)
  128. unset(SDL_TTF_VERSION_MINOR)
  129. unset(SDL_TTF_VERSION_PATCH)
  130. endif()
  131. set(SDL_TTF_LIBRARIES ${SDL_TTF_LIBRARY})
  132. set(SDL_TTF_INCLUDE_DIRS ${SDL_TTF_INCLUDE_DIR})
  133. include(FindPackageHandleStandardArgs)
  134. find_package_handle_standard_args(SDL_ttf
  135. REQUIRED_VARS SDL_TTF_LIBRARIES SDL_TTF_INCLUDE_DIRS
  136. VERSION_VAR SDL_ttf_VERSION)
  137. # for backward compatibility
  138. set(SDLTTF_LIBRARY ${SDL_TTF_LIBRARIES})
  139. set(SDLTTF_INCLUDE_DIR ${SDL_TTF_INCLUDE_DIRS})
  140. set(SDLTTF_FOUND ${SDL_TTF_FOUND})
  141. mark_as_advanced(SDL_TTF_LIBRARY SDL_TTF_INCLUDE_DIR)
  142. cmake_policy(POP)