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