1# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#[=======================================================================[.rst:
5FindFontconfig
6--------------
7
8.. versionadded:: 3.14
9
10Find Fontconfig headers and library.
11
12Imported Targets
13^^^^^^^^^^^^^^^^
14
15``Fontconfig::Fontconfig``
16  The Fontconfig library, if found.
17
18Result Variables
19^^^^^^^^^^^^^^^^
20
21This will define the following variables in your project:
22
23``Fontconfig_FOUND``
24  true if (the requested version of) Fontconfig is available.
25``Fontconfig_VERSION``
26  the version of Fontconfig.
27``Fontconfig_LIBRARIES``
28  the libraries to link against to use Fontconfig.
29``Fontconfig_INCLUDE_DIRS``
30  where to find the Fontconfig headers.
31``Fontconfig_COMPILE_OPTIONS``
32  this should be passed to target_compile_options(), if the
33  target is not used for linking
34
35#]=======================================================================]
36
37
38# use pkg-config to get the directories and then use these values
39# in the FIND_PATH() and FIND_LIBRARY() calls
40find_package(PkgConfig QUIET)
41pkg_check_modules(PKG_FONTCONFIG QUIET fontconfig)
42set(Fontconfig_COMPILE_OPTIONS ${PKG_FONTCONFIG_CFLAGS_OTHER})
43set(Fontconfig_VERSION ${PKG_FONTCONFIG_VERSION})
44
45find_path( Fontconfig_INCLUDE_DIR
46  NAMES
47    fontconfig/fontconfig.h
48  HINTS
49    ${PKG_FONTCONFIG_INCLUDE_DIRS}
50    /usr/X11/include
51)
52
53find_library( Fontconfig_LIBRARY
54  NAMES
55    fontconfig
56  PATHS
57    ${PKG_FONTCONFIG_LIBRARY_DIRS}
58)
59
60if (Fontconfig_INCLUDE_DIR AND NOT Fontconfig_VERSION)
61  file(STRINGS ${Fontconfig_INCLUDE_DIR}/fontconfig/fontconfig.h _contents REGEX "^#define[ \t]+FC_[A-Z]+[ \t]+[0-9]+$")
62  unset(Fontconfig_VERSION)
63  foreach(VPART MAJOR MINOR REVISION)
64    foreach(VLINE ${_contents})
65      if(VLINE MATCHES "^#define[\t ]+FC_${VPART}[\t ]+([0-9]+)$")
66        set(Fontconfig_VERSION_PART "${CMAKE_MATCH_1}")
67        if(Fontconfig_VERSION)
68          string(APPEND Fontconfig_VERSION ".${Fontconfig_VERSION_PART}")
69        else()
70          set(Fontconfig_VERSION "${Fontconfig_VERSION_PART}")
71        endif()
72      endif()
73    endforeach()
74  endforeach()
75endif ()
76
77include(FindPackageHandleStandardArgs)
78find_package_handle_standard_args(Fontconfig
79  FOUND_VAR
80    Fontconfig_FOUND
81  REQUIRED_VARS
82    Fontconfig_LIBRARY
83    Fontconfig_INCLUDE_DIR
84  VERSION_VAR
85    Fontconfig_VERSION
86)
87
88
89if(Fontconfig_FOUND AND NOT TARGET Fontconfig::Fontconfig)
90  add_library(Fontconfig::Fontconfig UNKNOWN IMPORTED)
91  set_target_properties(Fontconfig::Fontconfig PROPERTIES
92    IMPORTED_LOCATION "${Fontconfig_LIBRARY}"
93    INTERFACE_COMPILE_OPTIONS "${Fontconfig_COMPILE_OPTIONS}"
94    INTERFACE_INCLUDE_DIRECTORIES "${Fontconfig_INCLUDE_DIR}"
95  )
96endif()
97
98mark_as_advanced(Fontconfig_LIBRARY Fontconfig_INCLUDE_DIR)
99
100if(Fontconfig_FOUND)
101  set(Fontconfig_LIBRARIES ${Fontconfig_LIBRARY})
102  set(Fontconfig_INCLUDE_DIRS ${Fontconfig_INCLUDE_DIR})
103endif()
104