Debian Codegraph @

cmake/modules/FindOpenJPEG.cmake

raw
1 # SPDX-FileCopyrightText: 2023 L. E. Segovia <[email protected]>
2 # SPDX-License-Identifier: BSD-3-Clause
3 #
4 #[=======================================================================[.rst:
5 FindOpenJPEG
6 --------------
7
8 Find OpenJPEG headers and libraries.
9
10 Imported Targets
11 ^^^^^^^^^^^^^^^^
12
13 ``OpenJPEG::openjp2``
14 The OpenJPEG library, if found.
15
16 ``OpenJPEG::opj_decompress``
17 The OpenJPEG decompression executable, if found.
18
19 ``OpenJPEG::opj_compress``
20 The OpenJPEG compression executable, if found.
21
22 ``OpenJPEG::opj_dump``
23 The OpenJPEG dump executable, if found.
24
25 Result Variables
26 ^^^^^^^^^^^^^^^^
27
28 This will define the following variables in your project:
29
30 ``OpenJPEG_FOUND``
31 true if (the requested version of) OpenJPEG is available.
32 ``OpenJPEG_VERSION``
33 the version of OpenJPEG.
34 ``OpenJPEG_LIBRARIES``
35 the libraries to link against to use OpenJPEG.
36 ``OpenJPEG_INCLUDE_DIRS``
37 where to find the OpenJPEG headers.
38 ``OpenJPEG_COMPILE_OPTIONS``
39 this should be passed to target_compile_options(), if the
40 target is not used for linking
41
42 #]=======================================================================]
43
44 include(FindPackageHandleStandardArgs)
45
46 # No hints in this case because the library versions semantically
47 # the module folder, and it's also capitalized, so let CMake handle it.
48 find_package(OPENJPEG QUIET NO_MODULE NAMES OpenJPEG)
49 mark_as_advanced(OPENJPEG_DIR)
50
51 if(OPENJPEG_FOUND)
52 if (TARGET openjp2 AND NOT TARGET OpenJPEG::openjp2)
53 add_library(OpenJPEG::openjp2 ALIAS openjp2)
54 endif()
55
56 if (TARGET OpenJPEG::openjp2)
57 set(OpenJPEG_openjp2_FOUND ON)
58 else ()
59 set(OpenJPEG_openjp2_FOUND OFF)
60 endif ()
61
62 if (TARGET opj_decompress AND NOT TARGET OpenJPEG::opj_decompress)
63 add_executable(OpenJPEG::opj_decompress ALIAS opj_decompress)
64 endif()
65
66 if (TARGET OpenJPEG::opj_decompress)
67 set(OpenJPEG_opj_decompress_FOUND ON)
68 else ()
69 set(OpenJPEG_opj_decompress_FOUND OFF)
70 endif ()
71
72 if (TARGET opj_compress AND NOT TARGET OpenJPEG::opj_compress)
73 add_executable(OpenJPEG::opj_compress ALIAS opj_compress)
74 endif()
75
76 if (TARGET OpenJPEG::opj_compress)
77 set(OpenJPEG_opj_compress_FOUND ON)
78 else ()
79 set(OpenJPEG_opj_compress_FOUND OFF)
80 endif ()
81
82 if (TARGET opj_dump AND NOT TARGET OpenJPEG::opj_dump)
83 add_executable(OpenJPEG::opj_dump ALIAS opj_dump)
84 endif()
85
86 if (TARGET OpenJPEG::opj_dump)
87 set(OpenJPEG_opj_dump_FOUND ON)
88 else ()
89 set(OpenJPEG_opj_dump_FOUND OFF)
90 endif ()
91
92 if (TARGET openjpip AND NOT TARGET OpenJPEG::openjpip)
93 add_library(OpenJPEG::openjpip ALIAS openjpip)
94 endif()
95
96 if (TARGET OpenJPEG::openjpip)
97 set(OpenJPEG_openjpip_FOUND ON)
98 else ()
99 set(OpenJPEG_openjpip_FOUND OFF)
100 endif ()
101
102 if (TARGET openjpip_server AND NOT TARGET OpenJPEG::openjpip_server)
103 add_library(OpenJPEG::openjpip_server ALIAS openjpip_server)
104 endif()
105
106 if (TARGET OpenJPEG::openjpip_server)
107 set(OpenJPEG_openjpip_server_FOUND ON)
108 else ()
109 set(OpenJPEG_openjpip_server_FOUND OFF)
110 endif ()
111
112 # Patch in the version string, if needed.
113 if (NOT OpenJPEG_VERSION)
114 set(OpenJPEG_VERSION "${OPENJPEG_MAJOR_VERSION}.${OPENJPEG_MINOR_VERSION}.${OPENJPEG_BUILD_VERSION}")
115 endif ()
116
117 # Patch in the library and include dirs.
118 if (NOT OpenJPEG_LIBRARIES)
119 set(OpenJPEG_LIBRARIES OpenJPEG::openjp2)
120 endif()
121 set(OpenJPEG_INCLUDE_DIRS "${OPENJPEG_INCLUDE_DIRS}")
122
123 find_package_handle_standard_args(OpenJPEG
124 FOUND_VAR OpenJPEG_FOUND
125 REQUIRED_VARS OpenJPEG_INCLUDE_DIRS OpenJPEG_LIBRARIES
126 HANDLE_COMPONENTS
127 VERSION_VAR OpenJPEG_VERSION
128 NAME_MISMATCHED
129 # Don't, it defines mismatching cased variables.
130 # CONFIG_MODE
131 )
132 return()
133 endif()
134
135 find_package(PkgConfig QUIET)
136
137 if (PkgConfig_FOUND)
138 pkg_check_modules(PC_OPENJPEG QUIET libopenjp2)
139 set(OpenJPEG_VERSION ${PC_OPENJPEG_VERSION})
140 set(OpenJPEG_COMPILE_OPTIONS "${PC_OPENJPEG_CFLAGS};${PC_OPENJPEG_CFLAGS_OTHER}")
141
142 pkg_check_modules(PC_OPENJPIP QUIET openjpip)
143 endif ()
144
145 find_path(OpenJPEG_INCLUDE_DIR
146 NAMES openjpeg.h
147 HINTS ${PC_OPENJPEG_INCLUDEDIR} ${PC_OPENJPEG_INCLUDE_DIRS}
148 )
149
150 find_library(OpenJPEG_LIBRARY
151 NAMES openjp2 libopenjp2
152 HINTS ${PC_OPENJPEG_LIBDIR} ${PC_OPENJPEG_LIBRARY_DIRS}
153 )
154
155 # There's nothing in the OpenJPEG headers that could be used to detect the exact
156 # OpenJPEG version being used so don't attempt to do so. A version can only be found
157 # through pkg-config
158 if (NOT OpenJPEG_VERSION)
159 message(WARNING "Cannot determine OpenJPEG version without pkg-config")
160 endif ()
161
162 if (OpenJPEG_INCLUDE_DIR AND OpenJPEG_LIBRARY)
163 set(OpenJPEG_openjp2_FOUND ON)
164 else()
165 set(OpenJPEG_openjp2_FOUND OFF)
166 endif()
167
168 # Find components
169 if ("openjpip" IN_LIST OpenJPEG_FIND_COMPONENTS)
170 find_library(OpenJPEG_openjpip_LIBRARY
171 NAMES openjpip libopenjpip
172 HINTS ${PC_OPENJPIP_LIBDIR} ${PC_OPENJPIP_LIBRARY_DIRS}
173 )
174
175 if (OpenJPEG_openjpip_LIBRARY)
176 set(OpenJPEG_openjpip_FOUND ON)
177 else ()
178 set(OpenJPEG_openjpip_FOUND OFF)
179 endif ()
180 endif ()
181
182 if ("openjpip_server" IN_LIST OpenJPEG_FIND_COMPONENTS)
183 find_library(OpenJPEG_openjpip_server_LIBRARY
184 NAMES openjpip_server libopenjpip_server
185 HINTS ${PC_OPENJPIP_LIBDIR} ${PC_OPENJPIP_LIBRARY_DIRS}
186 )
187
188 if (OpenJPEG_openjpip_server_LIBRARY)
189 set(OpenJPEG_openjpip_server_FOUND ON)
190 else ()
191 set(OpenJPEG_openjpip_server_FOUND OFF)
192 endif ()
193 endif ()
194
195 find_package_handle_standard_args(OpenJPEG
196 FOUND_VAR OpenJPEG_FOUND
197 REQUIRED_VARS OpenJPEG_INCLUDE_DIR OpenJPEG_LIBRARY
198 HANDLE_COMPONENTS
199 VERSION_VAR OpenJPEG_VERSION
200 )
201
202 if (OpenJPEG_FOUND)
203 if (OpenJPEG_LIBRARY AND NOT TARGET OpenJPEG::openjp2)
204 add_library(OpenJPEG::openjp2 UNKNOWN IMPORTED GLOBAL)
205 set_target_properties(OpenJPEG::openjp2 PROPERTIES
206 IMPORTED_LOCATION "${OpenJPEG_LIBRARY}"
207 INTERFACE_COMPILE_OPTIONS "${PC_OPENJPEG_CFLAGS};${PC_OPENJPEG_CFLAGS_OTHER}"
208 INTERFACE_INCLUDE_DIRECTORIES "${OpenJPEG_INCLUDE_DIR};${PC_OPENJPEG_INCLUDE_DIRS}"
209 INTERFACE_LINK_LIBRARIES "${PC_OPENJPEG_LINK_LIBRARIES}"
210 )
211 endif ()
212
213 if (OpenJPEG_openjpip_LIBRARY AND NOT TARGET OpenJPEG::openjpip)
214 add_library(OpenJPEG::openjpip UNKNOWN IMPORTED GLOBAL)
215 set_target_properties(OpenJPEG::openjpip PROPERTIES
216 IMPORTED_LOCATION "${OpenJPEG_openjpip_LIBRARY}"
217 INTERFACE_COMPILE_OPTIONS "${PC_OPENJPIP_CFLAGS};${PC_OPENJPIP_CFLAGS_OTHER}"
218 INTERFACE_INCLUDE_DIRECTORIES "${OpenJPEG_INCLUDE_DIR};${PC_OPENJPIP_INCLUDE_DIRS}"
219 INTERFACE_LINK_LIBRARIES "${PC_OPENJPIP_LINK_LIBRARIES}"
220 )
221 endif ()
222
223 if (OpenJPEG_openjpip_server_LIBRARY AND NOT TARGET OpenJPEG::openjpip_server)
224 add_library(OpenJPEG::openjpip_server UNKNOWN IMPORTED GLOBAL)
225 set_target_properties(OpenJPEG::openjpip_server PROPERTIES
226 IMPORTED_LOCATION "${OpenJPEG_openjpip_server_LIBRARY}"
227 INTERFACE_COMPILE_OPTIONS "${PC_OPENJPIP_CFLAGS};${PC_OPENJPIP_CFLAGS_OTHER}"
228 INTERFACE_INCLUDE_DIRECTORIES "${OpenJPEG_INCLUDE_DIR};${PC_OPENJPIP_INCLUDE_DIRS}"
229 INTERFACE_LINK_LIBRARIES "${PC_OPENJPIP_LINK_LIBRARIES}"
230 )
231 endif ()
232
233 mark_as_advanced(
234 OpenJPEG_INCLUDE_DIR
235 OpenJPEG_LIBRARY
236 OpenJPEG_openjpip_LIBRARY
237 OpenJPEG_openjpip_server_LIBRARY
238 )
239
240 set(OpenJPEG_LIBRARIES ${OpenJPEG_LIBRARY} ${OpenJPEG_openjpip_LIBRARY} ${OpenJPEG_openjpip_server_LIBRARY})
241 set(OpenJPEG_INCLUDE_DIRS ${OpenJPEG_INCLUDE_DIR})
242
243 endif()