Debian Codegraph @

cmake/modules/FindHEIF.cmake

raw
1 # SPDX-FileCopyrightText: 2018 Boudewijn Rempt <[email protected]>
2 # SPDX-FileCopyrightText: 2023 L. E. Segovia <[email protected]>
3 # SPDX-License-Identifier: BSD-3-Clause
4
5 #[=======================================================================[.rst:
6 FindHEIF
7 --------------
8
9 Find HEIF headers and libraries.
10
11 Imported Targets
12 ^^^^^^^^^^^^^^^^
13
14 ``HEIF::heif``
15 The HEIF library, if found.
16
17 Result Variables
18 ^^^^^^^^^^^^^^^^
19
20 This will define the following variables in your project:
21
22 ``HEIF_FOUND``
23 true if (the requested version of) HEIF is available.
24 ``HEIF_VERSION``
25 the version of HEIF.
26 ``HEIF_LIBRARIES``
27 the libraries to link against to use HEIF.
28 ``HEIF_INCLUDE_DIRS``
29 where to find the HEIF headers.
30 ``HEIF_COMPILE_OPTIONS``
31 this should be passed to target_compile_options(), if the
32 target is not used for linking
33
34 #]=======================================================================]
35
36 include(FindPackageHandleStandardArgs)
37
38 set(HEIF_PKGCONF_CONFIG_DIR CACHE STRING "PkgConfig path for locating the package modules")
39 foreach(_dir ${CMAKE_PREFIX_PATH})
40 list(APPEND HEIF_PKGCONF_CONFIG_DIR ${_dir}/lib/cmake/libheif)
41 endforeach()
42 mark_as_advanced(HEIF_PKGCONF_CONFIG_DIR)
43
44 find_package(libheif QUIET NO_MODULE
45 HINTS ${HEIF_PKGCONF_CONFIG_DIR} /usr/lib/cmake/libheif /usr/local/lib/cmake/libheif
46 )
47 mark_as_advanced(libheif_DIR)
48
49 # if we found the libheif CMake package then we are done, and
50 # can print what we found and return.
51 if(libheif_FOUND)
52 if (TARGET heif AND NOT TARGET HEIF::heif)
53 add_library(HEIF::heif ALIAS heif)
54 endif()
55
56 if (TARGET HEIF::HEIF)
57 set(HEIF_FOUND ON)
58 else ()
59 set(HEIF_FOUND OFF)
60 endif ()
61
62 if (NOT HEIF_LIBRARIES)
63 set(HEIF_LIBRARIES HEIF::heif)
64 endif()
65 unset(libheif_LIBRARIES)
66
67 set(HEIF_VERSION "${libheif_VERSION}")
68 unset(libheif_VERSION)
69
70 get_target_property(HEIF_INCLUDE_DIRS HEIF::heif INTERFACE_INCLUDE_DIRECTORIES)
71
72 find_package_handle_standard_args(HEIF
73 FOUND_VAR HEIF_FOUND
74 REQUIRED_VARS HEIF_INCLUDE_DIRS HEIF_LIBRARIES
75 VERSION_VAR HEIF_VERSION
76 NAME_MISMATCHED
77 )
78 return()
79 endif()
80
81 find_package(PkgConfig QUIET)
82
83 if (PkgConfig_FOUND)
84 pkg_check_modules(HEIF_PKGCONF QUIET libheif)
85 set(HEIF_VERSION ${HEIF_PKGCONF_VERSION})
86 set(HEIF_COMPILE_OPTIONS "${HEIF_PKGCONF_CFLAGS};${HEIF_PKGCONF_CFLAGS_OTHER}")
87 endif ()
88
89 find_path(HEIF_INCLUDE_DIR
90 NAMES libheif/heif.h
91 HINTS ${HEIF_PKGCONF_INCLUDE_DIRS} ${HEIF_PKGCONF_INCLUDEDIR}
92 PATH_SUFFIXES heif
93 )
94
95 find_library(HEIF_LIBRARY
96 NAMES libheif heif
97 HINTS ${HEIF_PKGCONF_LIBRARY_DIRS} ${HEIF_PKGCONF_LIBDIR}
98 )
99
100 if (NOT HEIF_VERSION AND HEIF_FOUND)
101 file(READ ${HEIF_INCLUDE_DIR}/libheif/heif_version.h _version_content)
102
103 string(REGEX MATCH "#define LIBHEIF_VERSION[ \t]+\"(.+)\"" _version_match ${_version_content})
104
105 if (_version_match)
106 set(HEIF_VERSION "${CMAKE_MATCH_1}")
107 else()
108 if(NOT HEIF_FIND_QUIETLY)
109 message(WARNING "Failed to get version information from ${HEIF_INCLUDE_DIR}/libheif/config.h")
110 endif()
111 endif()
112 endif()
113
114 if (HEIF_INCLUDE_DIR AND HEIF_LIBRARY)
115 set(HEIF_FOUND ON)
116 else()
117 set(HEIF_FOUND OFF)
118 endif()
119
120 find_package_handle_standard_args(HEIF
121 FOUND_VAR HEIF_FOUND
122 REQUIRED_VARS HEIF_INCLUDE_DIR HEIF_LIBRARY
123 HANDLE_COMPONENTS
124 VERSION_VAR HEIF_VERSION
125 )
126
127 if (HEIF_FOUND)
128 if (HEIF_LIBRARY AND NOT TARGET HEIF::heif)
129 add_library(HEIF::heif UNKNOWN IMPORTED GLOBAL)
130 set_target_properties(HEIF::heif PROPERTIES
131 IMPORTED_LOCATION "${HEIF_LIBRARY}"
132 INTERFACE_COMPILE_OPTIONS "${HEIF_PKGCONF_CFLAGS_OTHER}"
133 INTERFACE_INCLUDE_DIRECTORIES "${HEIF_INCLUDE_DIR}"
134 INTERFACE_LINK_LIBRARIES "${HEIF_PKGCONF_LIBRARIES}"
135 INTERFACE_LINK_DIRECTORIES "${HEIF_PKGCONF_LIBDIR}"
136 )
137 endif()
138
139 mark_as_advanced(
140 HEIF_INCLUDE_DIR
141 HEIF_LIBRARY
142 )
143
144 set(HEIF_LIBRARIES ${HEIF_LIBRARY})
145 set(HEIF_INCLUDE_DIRS ${HEIF_INCLUDE_DIR})
146 endif()