FindFreetype.cmake 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. # Created by Eric Wing.
  37. # Modifications by Alexander Neundorf.
  38. # This file has been renamed to "FindFreetype.cmake" instead of the correct
  39. # "FindFreeType.cmake" in order to be compatible with the one from KDE4, Alex.
  40. # Ugh, FreeType seems to use some #include trickery which
  41. # makes this harder than it should be. It looks like they
  42. # put ft2build.h in a common/easier-to-find location which
  43. # then contains a #include to a more specific header in a
  44. # more specific location (#include <freetype/config/ftheader.h>).
  45. # Then from there, they need to set a bunch of #define's
  46. # so you can do something like:
  47. # #include FT_FREETYPE_H
  48. # Unfortunately, using CMake's mechanisms like include_directories()
  49. # wants explicit full paths and this trickery doesn't work too well.
  50. # I'm going to attempt to cut out the middleman and hope
  51. # everything still works.
  52. set(_Freetype_args)
  53. if (Freetype_FIND_QUIETLY)
  54. list(APPEND _Freetype_args
  55. QUIET)
  56. endif ()
  57. if (Freetype_FIND_VERSION)
  58. list(APPEND _Freetype_args
  59. "${Freetype_FIND_VERSION}")
  60. if (Freetype_FIND_VERSION_EXACT)
  61. list(APPEND _Freetype_args
  62. EXACT)
  63. endif ()
  64. endif ()
  65. set(_Freetype_component_req)
  66. set(_Freetype_component_opt)
  67. foreach (_Freetype_component IN LISTS Freetype_FIND_COMPONENTS)
  68. if (Freetype_FIND_REQUIRE_${_Freetype_component})
  69. list(APPEND _Freetype_component_req
  70. "${_Freetype_component}")
  71. else ()
  72. list(APPEND _Freetype_component_opt
  73. "${_Freetype_component}")
  74. endif ()
  75. endforeach ()
  76. unset(_Freetype_component)
  77. if (_Freetype_component_req)
  78. list(APPEND _Freetype_args
  79. COMPONENTS "${_Freetype_component_req}")
  80. endif ()
  81. unset(_Freetype_component_req)
  82. if (_Freetype_component_opt)
  83. list(APPEND _Freetype_args
  84. OPTIONAL_COMPONENTS "${_Freetype_component_opt}")
  85. endif ()
  86. unset(_Freetype_component_opt)
  87. find_package(freetype CONFIG ${_Freetype_args})
  88. unset(_Freetype_args)
  89. if (freetype_FOUND)
  90. if (NOT TARGET Freetype::Freetype)
  91. add_library(Freetype::Freetype IMPORTED INTERFACE)
  92. set_target_properties(Freetype::Freetype PROPERTIES
  93. INTERFACE_LINK_LIBRARIES freetype)
  94. endif ()
  95. get_property(FREETYPE_INCLUDE_DIRS TARGET freetype PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
  96. get_property(FREETYPE_LIBRARIES TARGET freetype PROPERTY INTERFACE_LINK_LIBRARIES)
  97. get_property(_Freetype_location TARGET freetype PROPERTY IMPORTED_IMPLIB)
  98. if (NOT _Freetype_location)
  99. get_property(_Freetype_location_release TARGET freetype PROPERTY IMPORTED_IMPLIB_RELEASE)
  100. get_property(_Freetype_location_debug TARGET freetype PROPERTY IMPORTED_IMPLIB_DEBUG)
  101. if (_Freetype_location_release AND _Freetype_location_debug)
  102. set(_Freetype_location
  103. optimized "${_Freetype_location_release}"
  104. debug "${_Freetype_location_debug}")
  105. elseif (_Freetype_location_release)
  106. set(_Freetype_location "${_Freetype_location_release}")
  107. elseif (_Freetype_location_debug)
  108. set(_Freetype_location "${_Freetype_location_debug}")
  109. else ()
  110. get_property(_Freetype_location_release TARGET freetype PROPERTY LOCATION_RELEASE)
  111. get_property(_Freetype_location_debug TARGET freetype PROPERTY LOCATION_DEBUG)
  112. if (_Freetype_location_release AND _Freetype_location_debug)
  113. set(_Freetype_location
  114. optimized "${_Freetype_location_release}"
  115. debug "${_Freetype_location_debug}")
  116. elseif (_Freetype_location_release)
  117. set(_Freetype_location "${_Freetype_location_release}")
  118. elseif (_Freetype_location_debug)
  119. set(_Freetype_location "${_Freetype_location_debug}")
  120. else ()
  121. get_property(_Freetype_location TARGET freetype PROPERTY LOCATION)
  122. endif ()
  123. endif ()
  124. unset(_Freetype_location_release)
  125. unset(_Freetype_location_debug)
  126. endif ()
  127. list(INSERT FREETYPE_LIBRARIES 0
  128. "${_Freetype_location}")
  129. unset(_Freetype_location)
  130. set(Freetype_FOUND 1)
  131. set(FREETYPE_FOUND 1)
  132. set(FREETYPE_VERSION_STRING "${freetype_VERSION}")
  133. foreach (_Freetype_component IN LISTS Freetype_FIND_COMPONENTS)
  134. set(Freetype_${_Freetype_component}_FOUND "${freetype_${_Freetype_component}_FOUND}")
  135. endforeach ()
  136. unset(_Freetype_component)
  137. return ()
  138. endif ()
  139. set(FREETYPE_FIND_ARGS
  140. HINTS
  141. ENV FREETYPE_DIR
  142. PATHS
  143. ENV GTKMM_BASEPATH
  144. [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]
  145. [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]
  146. )
  147. find_path(
  148. FREETYPE_INCLUDE_DIR_ft2build
  149. ft2build.h
  150. ${FREETYPE_FIND_ARGS}
  151. PATH_SUFFIXES
  152. include/freetype2
  153. include
  154. freetype2
  155. )
  156. find_path(
  157. FREETYPE_INCLUDE_DIR_freetype2
  158. NAMES
  159. freetype/config/ftheader.h
  160. config/ftheader.h
  161. ${FREETYPE_FIND_ARGS}
  162. PATH_SUFFIXES
  163. include/freetype2
  164. include
  165. freetype2
  166. )
  167. if(NOT FREETYPE_LIBRARY)
  168. find_library(FREETYPE_LIBRARY_RELEASE
  169. NAMES
  170. freetype
  171. libfreetype
  172. freetype219
  173. ${FREETYPE_FIND_ARGS}
  174. PATH_SUFFIXES
  175. lib
  176. )
  177. find_library(FREETYPE_LIBRARY_DEBUG
  178. NAMES
  179. freetyped
  180. libfreetyped
  181. freetype219d
  182. ${FREETYPE_FIND_ARGS}
  183. PATH_SUFFIXES
  184. lib
  185. )
  186. include(${CMAKE_CURRENT_LIST_DIR}/SelectLibraryConfigurations.cmake)
  187. select_library_configurations(FREETYPE)
  188. else()
  189. # on Windows, ensure paths are in canonical format (forward slahes):
  190. file(TO_CMAKE_PATH "${FREETYPE_LIBRARY}" FREETYPE_LIBRARY)
  191. endif()
  192. unset(FREETYPE_FIND_ARGS)
  193. # set the user variables
  194. if(FREETYPE_INCLUDE_DIR_ft2build AND FREETYPE_INCLUDE_DIR_freetype2)
  195. set(FREETYPE_INCLUDE_DIRS "${FREETYPE_INCLUDE_DIR_ft2build};${FREETYPE_INCLUDE_DIR_freetype2}")
  196. list(REMOVE_DUPLICATES FREETYPE_INCLUDE_DIRS)
  197. endif()
  198. set(FREETYPE_LIBRARIES "${FREETYPE_LIBRARY}")
  199. if(EXISTS "${FREETYPE_INCLUDE_DIR_freetype2}/freetype/freetype.h")
  200. set(FREETYPE_H "${FREETYPE_INCLUDE_DIR_freetype2}/freetype/freetype.h")
  201. elseif(EXISTS "${FREETYPE_INCLUDE_DIR_freetype2}/freetype.h")
  202. set(FREETYPE_H "${FREETYPE_INCLUDE_DIR_freetype2}/freetype.h")
  203. endif()
  204. if(FREETYPE_INCLUDE_DIR_freetype2 AND FREETYPE_H)
  205. file(STRINGS "${FREETYPE_H}" freetype_version_str
  206. REGEX "^#[\t ]*define[\t ]+FREETYPE_(MAJOR|MINOR|PATCH)[\t ]+[0-9]+$")
  207. unset(FREETYPE_VERSION_STRING)
  208. foreach(VPART MAJOR MINOR PATCH)
  209. foreach(VLINE ${freetype_version_str})
  210. if(VLINE MATCHES "^#[\t ]*define[\t ]+FREETYPE_${VPART}[\t ]+([0-9]+)$")
  211. set(FREETYPE_VERSION_PART "${CMAKE_MATCH_1}")
  212. if(FREETYPE_VERSION_STRING)
  213. string(APPEND FREETYPE_VERSION_STRING ".${FREETYPE_VERSION_PART}")
  214. else()
  215. set(FREETYPE_VERSION_STRING "${FREETYPE_VERSION_PART}")
  216. endif()
  217. unset(FREETYPE_VERSION_PART)
  218. endif()
  219. endforeach()
  220. endforeach()
  221. endif()
  222. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  223. find_package_handle_standard_args(
  224. Freetype
  225. REQUIRED_VARS
  226. FREETYPE_LIBRARY
  227. FREETYPE_INCLUDE_DIRS
  228. VERSION_VAR
  229. FREETYPE_VERSION_STRING
  230. )
  231. mark_as_advanced(
  232. FREETYPE_INCLUDE_DIR_freetype2
  233. FREETYPE_INCLUDE_DIR_ft2build
  234. )
  235. if(Freetype_FOUND)
  236. if(NOT TARGET Freetype::Freetype)
  237. add_library(Freetype::Freetype UNKNOWN IMPORTED)
  238. set_target_properties(Freetype::Freetype PROPERTIES
  239. INTERFACE_INCLUDE_DIRECTORIES "${FREETYPE_INCLUDE_DIRS}")
  240. if(FREETYPE_LIBRARY_RELEASE)
  241. set_property(TARGET Freetype::Freetype APPEND PROPERTY
  242. IMPORTED_CONFIGURATIONS RELEASE)
  243. set_target_properties(Freetype::Freetype PROPERTIES
  244. IMPORTED_LINK_INTERFACE_LANGUAGES_RELEASE "C"
  245. IMPORTED_LOCATION_RELEASE "${FREETYPE_LIBRARY_RELEASE}")
  246. endif()
  247. if(FREETYPE_LIBRARY_DEBUG)
  248. set_property(TARGET Freetype::Freetype APPEND PROPERTY
  249. IMPORTED_CONFIGURATIONS DEBUG)
  250. set_target_properties(Freetype::Freetype PROPERTIES
  251. IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "C"
  252. IMPORTED_LOCATION_DEBUG "${FREETYPE_LIBRARY_DEBUG}")
  253. endif()
  254. if(NOT FREETYPE_LIBRARY_RELEASE AND NOT FREETYPE_LIBRARY_DEBUG)
  255. set_target_properties(Freetype::Freetype PROPERTIES
  256. IMPORTED_LINK_INTERFACE_LANGUAGES "C"
  257. IMPORTED_LOCATION "${FREETYPE_LIBRARY}")
  258. endif()
  259. endif()
  260. endif()