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