Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/cmake/FindPkgConfig.cmake @ 2673

Last change on this file since 2673 was 1505, checked in by rgrieder, 16 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 15.1 KB
Line 
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_OTHERS ... 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_OTHERS  ... 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}_LIBS)
173  _pkgconfig_unset(${_prefix}_LIBS_L)
174  _pkgconfig_unset(${_prefix}_LIBS_PATHS)
175  _pkgconfig_unset(${_prefix}_LIBS_OTHER)
176  _pkgconfig_unset(${_prefix}_CFLAGS)
177  _pkgconfig_unset(${_prefix}_CFLAGS_I)
178  _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
179  _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
180  _pkgconfig_unset(${_prefix}_STATIC_LIBS)
181  _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
182  _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
183  _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
184  _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
185  _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
186  _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
187
188  # create a better addressable variable of the modules and calculate its size
189  set(_pkg_check_modules_list ${ARGN})
190  list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
191
192  if(PKG_CONFIG_EXECUTABLE)
193    # give out status message telling checked module
194    if (NOT ${_is_silent})
195      if (_pkg_check_modules_cnt EQUAL 1)
196        message(STATUS "checking for module '${_pkg_check_modules_list}'")
197      else(_pkg_check_modules_cnt EQUAL 1)
198        message(STATUS "checking for modules '${_pkg_check_modules_list}'")
199      endif(_pkg_check_modules_cnt EQUAL 1)
200    endif(NOT ${_is_silent})
201   
202    set(_pkg_check_modules_packages)
203    set(_pkg_check_modules_failed)
204
205    # iterate through module list and check whether they exist and match the required version
206    foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
207      set(_pkg_check_modules_exist_query)
208
209      # check whether version is given
210      if (_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
211        string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\1" _pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
212        string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\2" _pkg_check_modules_pkg_op   "${_pkg_check_modules_pkg}")
213        string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\3" _pkg_check_modules_pkg_ver  "${_pkg_check_modules_pkg}")
214      else(_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
215        set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
216        set(_pkg_check_modules_pkg_op)
217        set(_pkg_check_modules_pkg_ver)
218      endif(_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
219
220      # handle the operands
221      if (_pkg_check_modules_pkg_op STREQUAL ">=")
222        list(APPEND _pkg_check_modules_exist_query --atleast-version)
223      endif(_pkg_check_modules_pkg_op STREQUAL ">=")
224
225      if (_pkg_check_modules_pkg_op STREQUAL "=")
226        list(APPEND _pkg_check_modules_exist_query --exact-version)
227      endif(_pkg_check_modules_pkg_op STREQUAL "=")
228     
229      if (_pkg_check_modules_pkg_op STREQUAL "<=")
230        list(APPEND _pkg_check_modules_exist_query --max-version)
231      endif(_pkg_check_modules_pkg_op STREQUAL "<=")
232
233      # create the final query which is of the format:
234      # * --atleast-version <version> <pkg-name>
235      # * --exact-version <version> <pkg-name>     
236      # * --max-version <version> <pkg-name>
237      # * --exists <pkg-name>
238      if (_pkg_check_modules_pkg_op)
239        list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
240      else(_pkg_check_modules_pkg_op)
241        list(APPEND _pkg_check_modules_exist_query --exists)
242      endif(_pkg_check_modules_pkg_op)
243
244      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
245      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
246      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
247      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
248
249      list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
250      list(APPEND _pkg_check_modules_packages    "${_pkg_check_modules_pkg_name}")
251
252      # execute the query
253      execute_process(
254        COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
255        RESULT_VARIABLE _pkgconfig_retval)
256
257      # evaluate result and tell failures
258      if (_pkgconfig_retval)
259        if(NOT ${_is_silent})
260          message(STATUS "  package '${_pkg_check_modules_pkg}' not found")
261        endif(NOT ${_is_silent})
262
263        set(_pkg_check_modules_failed 1)
264      endif(_pkgconfig_retval)
265    endforeach(_pkg_check_modules_pkg)
266
267    if(_pkg_check_modules_failed)
268      # fail when requested
269      if (${_is_required})
270        message(SEND_ERROR "A required package was not found")
271      endif (${_is_required})
272    else(_pkg_check_modules_failed)
273      # when we are here, we checked whether requested modules
274      # exist. Now, go through them and set variables
275     
276      _pkgconfig_set(${_prefix}_FOUND 1)
277      list(LENGTH _pkg_check_modules_packages pkg_count)
278
279      # iterate through all modules again and set individual variables
280      foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
281        # handle case when there is only one package required
282        if (pkg_count EQUAL 1)
283          set(_pkg_check_prefix "${_prefix}")
284        else(pkg_count EQUAL 1)
285          set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
286        endif(pkg_count EQUAL 1)
287       
288        _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION    ""   --modversion )
289        _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" PREFIX     ""   --variable=prefix )
290        _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" INCLUDEDIR ""   --variable=includedir )
291        _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" LIBDIR     ""   --variable=libdir )
292
293        message(STATUS "  found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
294      endforeach(_pkg_check_modules_pkg)
295
296      # set variables which are combined for multiple modules
297      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES           "(^| )-l" --libs-only-l )
298      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS        "(^| )-L" --libs-only-L )
299      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS             ""        --libs )
300      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER       ""        --libs-only-other )
301
302      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS        "(^| )-I" --cflags-only-I )
303      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS              ""        --cflags )
304      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER        ""        --cflags-only-other )
305    endif(_pkg_check_modules_failed)
306  else(PKG_CONFIG_EXECUTABLE)
307    if (${_is_required})
308      message(SEND_ERROR "pkg-config tool not found")
309    endif (${_is_required})
310  endif(PKG_CONFIG_EXECUTABLE)
311endmacro(_pkg_check_modules_internal)
312
313###
314### User visible macros start here
315###
316
317###
318macro(pkg_check_modules _prefix _module0)
319  # check cached value
320  if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
321    _pkgconfig_parse_options   (_pkg_modules _pkg_is_required "${_module0}" ${ARGN})
322    _pkg_check_modules_internal("${_pkg_is_required}" 0 "${_prefix}" ${_pkg_modules})
323
324    _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
325  endif(NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
326endmacro(pkg_check_modules)
327
328###
329macro(pkg_search_module _prefix _module0)
330  # check cached value
331  if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
332    set(_pkg_modules_found 0)
333    _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required "${_module0}" ${ARGN})
334
335    message(STATUS "checking for one of the modules '${_pkg_modules_alt}'")
336
337    # iterate through all modules and stop at the first working one.
338    foreach(_pkg_alt ${_pkg_modules_alt})
339      if(NOT _pkg_modules_found)
340        _pkg_check_modules_internal(0 1 "${_prefix}" "${_pkg_alt}")
341      endif(NOT _pkg_modules_found)
342
343      if (${_prefix}_FOUND)
344        set(_pkg_modules_found 1)
345      endif(${_prefix}_FOUND)
346    endforeach(_pkg_alt)
347
348    if (NOT ${_prefix}_FOUND)
349      if(${_pkg_is_required})
350        message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
351      endif(${_pkg_is_required})
352    endif(NOT ${_prefix}_FOUND)
353   
354    _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
355  endif(NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION}) 
356endmacro(pkg_search_module)
357
358### Local Variables:
359### mode: cmake
360### End:
Note: See TracBrowser for help on using the repository browser.