2 # OpenCV note: the file has been extracted from CMake 2.6.2 distribution.
3 # It is used to build OpenCV with CMake 2.4.x
6 # - a pkg-config module for CMake
9 # pkg_check_modules(<PREFIX> [REQUIRED] <MODULE> [<MODULE>]*)
10 # checks for all the given modules
12 # pkg_search_module(<PREFIX> [REQUIRED] <MODULE> [<MODULE>]*)
13 # checks for given modules and uses the first working one
15 # When the 'REQUIRED' argument was set, macros will fail with an error
16 # when module(s) could not be found
18 # It sets the following variables:
19 # PKG_CONFIG_FOUND ... true if pkg-config works on the system
20 # PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
21 # <PREFIX>_FOUND ... set to 1 if module(s) exist
23 # For the following variables two sets of values exist; first one is the
24 # common one and has the given PREFIX. The second set contains flags
25 # which are given out when pkgconfig was called with the '--static'
27 # <XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
28 # <XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
29 # <XPREFIX>_LDFLAGS ... all required linker flags
30 # <XPREFIX>_LDFLAGS_OTHER ... all other linker flags
31 # <XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
32 # <XPREFIX>_CFLAGS ... all required cflags
33 # <XPREFIX>_CFLAGS_OTHER ... the other compiler flags
35 # <XPREFIX> = <PREFIX> for common case
36 # <XPREFIX> = <PREFIX>_STATIC for static linking
38 # There are some special variables whose prefix depends on the count
39 # of given modules. When there is only one module, <PREFIX> stays
40 # unchanged. When there are multiple modules, the prefix will be
41 # changed to <PREFIX>_<MODNAME>:
42 # <XPREFIX>_VERSION ... version of the module
43 # <XPREFIX>_PREFIX ... prefix-directory of the module
44 # <XPREFIX>_INCLUDEDIR ... include-dir of the module
45 # <XPREFIX>_LIBDIR ... lib-dir of the module
47 # <XPREFIX> = <PREFIX> when |MODULES| == 1, else
48 # <XPREFIX> = <PREFIX>_<MODNAME>
50 # A <MODULE> parameter can have the following formats:
51 # {MODNAME} ... matches any version
52 # {MODNAME}>={VERSION} ... at least version <VERSION> is required
53 # {MODNAME}={VERSION} ... exactly version <VERSION> is required
54 # {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>
57 # pkg_check_modules (GLIB2 glib-2.0)
59 # pkg_check_modules (GLIB2 glib-2.0>=2.10)
60 # requires at least version 2.10 of glib2 and defines e.g.
61 # GLIB2_VERSION=2.10.3
63 # pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
64 # requires both glib2 and gtk2, and defines e.g.
65 # FOO_glib-2.0_VERSION=2.10.3
66 # FOO_gtk+-2.0_VERSION=2.8.20
68 # pkg_check_modules (XRENDER REQUIRED xrender)
70 # XRENDER_LIBRARIES=Xrender;X11
71 # XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
73 # pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
76 # Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
78 # Redistribution and use, with or without modification, are permitted
79 # provided that the following conditions are met:
81 # 1. Redistributions must retain the above copyright notice, this
82 # list of conditions and the following disclaimer.
83 # 2. The name of the author may not be used to endorse or promote
84 # products derived from this software without specific prior
87 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
88 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
89 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
90 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
91 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
93 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
94 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
95 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
96 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
97 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
100 ### Common stuff ####
101 set(PKG_CONFIG_VERSION 1)
102 set(PKG_CONFIG_FOUND 0)
104 find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
105 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
107 if(PKG_CONFIG_EXECUTABLE)
108 set(PKG_CONFIG_FOUND 1)
109 endif(PKG_CONFIG_EXECUTABLE)
112 # Unsets the given variables
113 macro(_pkgconfig_unset var)
114 set(${var} "" CACHE INTERNAL "")
115 endmacro(_pkgconfig_unset)
117 macro(_pkgconfig_set var value)
118 set(${var} ${value} CACHE INTERNAL "")
119 endmacro(_pkgconfig_set)
121 # Invokes pkgconfig, cleans up the result and sets variables
122 macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
123 set(_pkgconfig_invoke_result)
126 COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
127 OUTPUT_VARIABLE _pkgconfig_invoke_result
128 RESULT_VARIABLE _pkgconfig_failed)
130 if (_pkgconfig_failed)
131 set(_pkgconfig_${_varname} "")
132 _pkgconfig_unset(${_prefix}_${_varname})
133 else(_pkgconfig_failed)
134 string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
135 string(REGEX REPLACE " +$" "" _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
137 if (NOT ${_regexp} STREQUAL "")
138 string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
139 endif(NOT ${_regexp} STREQUAL "")
141 separate_arguments(_pkgconfig_invoke_result)
143 #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
144 set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
145 _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
146 endif(_pkgconfig_failed)
147 endmacro(_pkgconfig_invoke)
149 # Invokes pkgconfig two times; once without '--static' and once with
151 macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
152 _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
153 _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
154 endmacro(_pkgconfig_invoke_dyn)
156 # Splits given arguments into options and a package list
157 macro(_pkgconfig_parse_options _result _is_req)
160 foreach(_pkg ${ARGN})
161 if (_pkg STREQUAL "REQUIRED")
163 endif (_pkg STREQUAL "REQUIRED")
164 endforeach(_pkg ${ARGN})
166 set(${_result} ${ARGN})
167 list(REMOVE_ITEM ${_result} "REQUIRED")
168 endmacro(_pkgconfig_parse_options)
171 macro(_pkg_check_modules_internal _is_required _is_silent _prefix)
172 _pkgconfig_unset(${_prefix}_FOUND)
173 _pkgconfig_unset(${_prefix}_VERSION)
174 _pkgconfig_unset(${_prefix}_PREFIX)
175 _pkgconfig_unset(${_prefix}_INCLUDEDIR)
176 _pkgconfig_unset(${_prefix}_LIBDIR)
177 _pkgconfig_unset(${_prefix}_LIBS)
178 _pkgconfig_unset(${_prefix}_LIBS_L)
179 _pkgconfig_unset(${_prefix}_LIBS_PATHS)
180 _pkgconfig_unset(${_prefix}_LIBS_OTHER)
181 _pkgconfig_unset(${_prefix}_CFLAGS)
182 _pkgconfig_unset(${_prefix}_CFLAGS_I)
183 _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
184 _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
185 _pkgconfig_unset(${_prefix}_STATIC_LIBS)
186 _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
187 _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
188 _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
189 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
190 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
191 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
193 # create a better addressable variable of the modules and calculate its size
194 set(_pkg_check_modules_list ${ARGN})
195 list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
197 if(PKG_CONFIG_EXECUTABLE)
198 # give out status message telling checked module
199 if (NOT ${_is_silent})
200 if (_pkg_check_modules_cnt EQUAL 1)
201 message(STATUS "checking for module '${_pkg_check_modules_list}'")
202 else(_pkg_check_modules_cnt EQUAL 1)
203 message(STATUS "checking for modules '${_pkg_check_modules_list}'")
204 endif(_pkg_check_modules_cnt EQUAL 1)
205 endif(NOT ${_is_silent})
207 set(_pkg_check_modules_packages)
208 set(_pkg_check_modules_failed)
210 # iterate through module list and check whether they exist and match the required version
211 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
212 set(_pkg_check_modules_exist_query)
214 # check whether version is given
215 if (_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
216 string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\1" _pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
217 string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\2" _pkg_check_modules_pkg_op "${_pkg_check_modules_pkg}")
218 string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\3" _pkg_check_modules_pkg_ver "${_pkg_check_modules_pkg}")
219 else(_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
220 set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
221 set(_pkg_check_modules_pkg_op)
222 set(_pkg_check_modules_pkg_ver)
223 endif(_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
225 # handle the operands
226 if (_pkg_check_modules_pkg_op STREQUAL ">=")
227 list(APPEND _pkg_check_modules_exist_query --atleast-version)
228 endif(_pkg_check_modules_pkg_op STREQUAL ">=")
230 if (_pkg_check_modules_pkg_op STREQUAL "=")
231 list(APPEND _pkg_check_modules_exist_query --exact-version)
232 endif(_pkg_check_modules_pkg_op STREQUAL "=")
234 if (_pkg_check_modules_pkg_op STREQUAL "<=")
235 list(APPEND _pkg_check_modules_exist_query --max-version)
236 endif(_pkg_check_modules_pkg_op STREQUAL "<=")
238 # create the final query which is of the format:
239 # * --atleast-version <version> <pkg-name>
240 # * --exact-version <version> <pkg-name>
241 # * --max-version <version> <pkg-name>
242 # * --exists <pkg-name>
243 if (_pkg_check_modules_pkg_op)
244 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
245 else(_pkg_check_modules_pkg_op)
246 list(APPEND _pkg_check_modules_exist_query --exists)
247 endif(_pkg_check_modules_pkg_op)
249 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
250 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
251 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
252 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
254 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
255 list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
259 COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
260 RESULT_VARIABLE _pkgconfig_retval)
262 # evaluate result and tell failures
263 if (_pkgconfig_retval)
264 if(NOT ${_is_silent})
265 message(STATUS " package '${_pkg_check_modules_pkg}' not found")
266 endif(NOT ${_is_silent})
268 set(_pkg_check_modules_failed 1)
269 endif(_pkgconfig_retval)
270 endforeach(_pkg_check_modules_pkg)
272 if(_pkg_check_modules_failed)
273 # fail when requested
275 message(SEND_ERROR "A required package was not found")
276 endif (${_is_required})
277 else(_pkg_check_modules_failed)
278 # when we are here, we checked whether requested modules
279 # exist. Now, go through them and set variables
281 _pkgconfig_set(${_prefix}_FOUND 1)
282 list(LENGTH _pkg_check_modules_packages pkg_count)
284 # iterate through all modules again and set individual variables
285 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
286 # handle case when there is only one package required
287 if (pkg_count EQUAL 1)
288 set(_pkg_check_prefix "${_prefix}")
289 else(pkg_count EQUAL 1)
290 set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
291 endif(pkg_count EQUAL 1)
293 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
294 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" PREFIX "" --variable=prefix )
295 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" INCLUDEDIR "" --variable=includedir )
296 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" LIBDIR "" --variable=libdir )
298 message(STATUS " found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
299 endforeach(_pkg_check_modules_pkg)
301 # set variables which are combined for multiple modules
302 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
303 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
304 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
305 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
307 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
308 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
309 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
310 endif(_pkg_check_modules_failed)
311 else(PKG_CONFIG_EXECUTABLE)
313 message(SEND_ERROR "pkg-config tool not found")
314 endif (${_is_required})
315 endif(PKG_CONFIG_EXECUTABLE)
316 endmacro(_pkg_check_modules_internal)
319 ### User visible macros start here
323 macro(pkg_check_modules _prefix _module0)
325 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
326 _pkgconfig_parse_options (_pkg_modules _pkg_is_required "${_module0}" ${ARGN})
327 _pkg_check_modules_internal("${_pkg_is_required}" 0 "${_prefix}" ${_pkg_modules})
329 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
330 endif(NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
331 endmacro(pkg_check_modules)
334 macro(pkg_search_module _prefix _module0)
336 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
337 set(_pkg_modules_found 0)
338 _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required "${_module0}" ${ARGN})
340 message(STATUS "checking for one of the modules '${_pkg_modules_alt}'")
342 # iterate through all modules and stop at the first working one.
343 foreach(_pkg_alt ${_pkg_modules_alt})
344 if(NOT _pkg_modules_found)
345 _pkg_check_modules_internal(0 1 "${_prefix}" "${_pkg_alt}")
346 endif(NOT _pkg_modules_found)
348 if (${_prefix}_FOUND)
349 set(_pkg_modules_found 1)
350 endif(${_prefix}_FOUND)
353 if (NOT ${_prefix}_FOUND)
354 if(${_pkg_is_required})
355 message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
356 endif(${_pkg_is_required})
357 endif(NOT ${_prefix}_FOUND)
359 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
360 endif(NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
361 endmacro(pkg_search_module)