FindFreetype.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindFreetype
  5. ------------
  6. Find the FreeType font renderer includes and library.
  7. Imported Targets
  8. ^^^^^^^^^^^^^^^^
  9. .. versionadded:: 3.10
  10. This module defines the following :prop_tgt:`IMPORTED` target:
  11. ``Freetype::Freetype``
  12. The Freetype ``freetype`` library, if found
  13. Result Variables
  14. ^^^^^^^^^^^^^^^^
  15. This module will set the following variables in your project:
  16. ``FREETYPE_FOUND``
  17. true if the Freetype headers and libraries were found
  18. ``FREETYPE_INCLUDE_DIRS``
  19. directories containing the Freetype headers. This is the
  20. concatenation of the variables:
  21. ``FREETYPE_INCLUDE_DIR_ft2build``
  22. directory holding the main Freetype API configuration header
  23. ``FREETYPE_INCLUDE_DIR_freetype2``
  24. directory holding Freetype public headers
  25. ``FREETYPE_LIBRARIES``
  26. the library to link against
  27. ``FREETYPE_VERSION_STRING``
  28. the version of freetype found
  29. .. versionadded:: 3.7
  30. Debug and Release variants are found separately.
  31. Hints
  32. ^^^^^
  33. The user may set the environment variable ``FREETYPE_DIR`` to the root
  34. directory of a Freetype installation.
  35. #]=======================================================================]
  36. cmake_policy(PUSH)
  37. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  38. # Created by Eric Wing.
  39. # Modifications by Alexander Neundorf.
  40. # This file has been renamed to "FindFreetype.cmake" instead of the correct
  41. # "FindFreeType.cmake" in order to be compatible with the one from KDE4, Alex.
  42. # Ugh, FreeType seems to use some #include trickery which
  43. # makes this harder than it should be. It looks like they
  44. # put ft2build.h in a common/easier-to-find location which
  45. # then contains a #include to a more specific header in a
  46. # more specific location (#include <freetype/config/ftheader.h>).
  47. # Then from there, they need to set a bunch of #define's
  48. # so you can do something like:
  49. # #include FT_FREETYPE_H
  50. # Unfortunately, using CMake's mechanisms like include_directories()
  51. # wants explicit full paths and this trickery doesn't work too well.
  52. # I'm going to attempt to cut out the middleman and hope
  53. # everything still works.
  54. set(FREETYPE_FIND_ARGS
  55. HINTS
  56. ENV FREETYPE_DIR
  57. PATHS
  58. ENV GTKMM_BASEPATH
  59. [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]
  60. [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]
  61. )
  62. find_path(
  63. FREETYPE_INCLUDE_DIR_ft2build
  64. ft2build.h
  65. ${FREETYPE_FIND_ARGS}
  66. PATH_SUFFIXES
  67. include/freetype2
  68. include
  69. freetype2
  70. )
  71. find_path(
  72. FREETYPE_INCLUDE_DIR_freetype2
  73. NAMES
  74. freetype/config/ftheader.h
  75. config/ftheader.h
  76. ${FREETYPE_FIND_ARGS}
  77. PATH_SUFFIXES
  78. include/freetype2
  79. include
  80. freetype2
  81. )
  82. if(NOT FREETYPE_LIBRARY)
  83. find_library(FREETYPE_LIBRARY_RELEASE
  84. NAMES
  85. freetype
  86. libfreetype
  87. freetype219
  88. ${FREETYPE_FIND_ARGS}
  89. PATH_SUFFIXES
  90. lib
  91. )
  92. find_library(FREETYPE_LIBRARY_DEBUG
  93. NAMES
  94. freetyped
  95. libfreetyped
  96. freetype219d
  97. ${FREETYPE_FIND_ARGS}
  98. PATH_SUFFIXES
  99. lib
  100. )
  101. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  102. select_library_configurations(FREETYPE)
  103. else()
  104. # on Windows, ensure paths are in canonical format (forward slahes):
  105. file(TO_CMAKE_PATH "${FREETYPE_LIBRARY}" FREETYPE_LIBRARY)
  106. endif()
  107. unset(FREETYPE_FIND_ARGS)
  108. # set the user variables
  109. if(FREETYPE_INCLUDE_DIR_ft2build AND FREETYPE_INCLUDE_DIR_freetype2)
  110. set(FREETYPE_INCLUDE_DIRS "${FREETYPE_INCLUDE_DIR_ft2build};${FREETYPE_INCLUDE_DIR_freetype2}")
  111. list(REMOVE_DUPLICATES FREETYPE_INCLUDE_DIRS)
  112. endif()
  113. set(FREETYPE_LIBRARIES "${FREETYPE_LIBRARY}")
  114. if(EXISTS "${FREETYPE_INCLUDE_DIR_freetype2}/freetype/freetype.h")
  115. set(FREETYPE_H "${FREETYPE_INCLUDE_DIR_freetype2}/freetype/freetype.h")
  116. elseif(EXISTS "${FREETYPE_INCLUDE_DIR_freetype2}/freetype.h")
  117. set(FREETYPE_H "${FREETYPE_INCLUDE_DIR_freetype2}/freetype.h")
  118. endif()
  119. if(FREETYPE_INCLUDE_DIR_freetype2 AND FREETYPE_H)
  120. file(STRINGS "${FREETYPE_H}" freetype_version_str
  121. REGEX "^#[\t ]*define[\t ]+FREETYPE_(MAJOR|MINOR|PATCH)[\t ]+[0-9]+$")
  122. unset(FREETYPE_VERSION_STRING)
  123. foreach(VPART MAJOR MINOR PATCH)
  124. foreach(VLINE ${freetype_version_str})
  125. if(VLINE MATCHES "^#[\t ]*define[\t ]+FREETYPE_${VPART}[\t ]+([0-9]+)$")
  126. set(FREETYPE_VERSION_PART "${CMAKE_MATCH_1}")
  127. if(FREETYPE_VERSION_STRING)
  128. string(APPEND FREETYPE_VERSION_STRING ".${FREETYPE_VERSION_PART}")
  129. else()
  130. set(FREETYPE_VERSION_STRING "${FREETYPE_VERSION_PART}")
  131. endif()
  132. unset(FREETYPE_VERSION_PART)
  133. endif()
  134. endforeach()
  135. endforeach()
  136. endif()
  137. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  138. find_package_handle_standard_args(
  139. Freetype
  140. REQUIRED_VARS
  141. FREETYPE_LIBRARY
  142. FREETYPE_INCLUDE_DIRS
  143. VERSION_VAR
  144. FREETYPE_VERSION_STRING
  145. )
  146. mark_as_advanced(
  147. FREETYPE_INCLUDE_DIR_freetype2
  148. FREETYPE_INCLUDE_DIR_ft2build
  149. )
  150. if(Freetype_FOUND)
  151. if(NOT TARGET Freetype::Freetype)
  152. add_library(Freetype::Freetype UNKNOWN IMPORTED)
  153. set_target_properties(Freetype::Freetype PROPERTIES
  154. INTERFACE_INCLUDE_DIRECTORIES "${FREETYPE_INCLUDE_DIRS}")
  155. if(FREETYPE_LIBRARY_RELEASE)
  156. set_property(TARGET Freetype::Freetype APPEND PROPERTY
  157. IMPORTED_CONFIGURATIONS RELEASE)
  158. set_target_properties(Freetype::Freetype PROPERTIES
  159. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  160. IMPORTED_LOCATION_RELEASE "${FREETYPE_LIBRARY_RELEASE}")
  161. endif()
  162. if(FREETYPE_LIBRARY_DEBUG)
  163. set_property(TARGET Freetype::Freetype APPEND PROPERTY
  164. IMPORTED_CONFIGURATIONS DEBUG)
  165. set_target_properties(Freetype::Freetype PROPERTIES
  166. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  167. IMPORTED_LOCATION_DEBUG "${FREETYPE_LIBRARY_DEBUG}")
  168. endif()
  169. if(NOT FREETYPE_LIBRARY_RELEASE AND NOT FREETYPE_LIBRARY_DEBUG)
  170. set_target_properties(Freetype::Freetype PROPERTIES
  171. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  172. IMPORTED_LOCATION "${FREETYPE_LIBRARY}")
  173. endif()
  174. endif()
  175. endif()
  176. cmake_policy(POP)