1 # - a pkg-config module for CMake
4 # pkg_check_modules(<PREFIX> [REQUIRED] <MODULE> [<MODULE>]*)
5 # checks for all the given modules
7 # pkg_search_module(<PREFIX> [REQUIRED] <MODULE> [<MODULE>]*)
8 # checks for given modules and uses the first working one
10 # When the 'REQUIRED' argument was set, macros will fail with an error
11 # when module(s) could not be found
13 # It sets the following variables:
14 # PKG_CONFIG_FOUND ... true if pkg-config works on the system
15 # PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
16 # <PREFIX>_FOUND ... set to 1 if module(s) exist
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'
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
30 # <XPREFIX> = <PREFIX> for common case
31 # <XPREFIX> = <PREFIX>_STATIC for static linking
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
42 # <XPREFIX> = <PREFIX> when |MODULES| == 1, else
43 # <XPREFIX> = <PREFIX>_<MODNAME>
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>
52 # pkg_check_modules (GLIB2 glib-2.0)
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
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
63 # pkg_check_modules (XRENDER REQUIRED xrender)
65 # XRENDER_LIBRARIES=Xrender;X11
66 # XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
68 # pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
71 # Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
73 # Redistribution and use, with or without modification, are permitted
74 # provided that the following conditions are met:
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
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.
96 set(PKG_CONFIG_VERSION 1)
97 set(PKG_CONFIG_FOUND 0)
99 find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
100 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
102 if(PKG_CONFIG_EXECUTABLE)
103 set(PKG_CONFIG_FOUND 1)
104 endif(PKG_CONFIG_EXECUTABLE)
107 # Unsets the given variables
108 macro(_pkgconfig_unset var)
109 set(${var} "" CACHE INTERNAL "")
110 endmacro(_pkgconfig_unset)
112 macro(_pkgconfig_set var value)
113 set(${var} ${value} CACHE INTERNAL "")
114 endmacro(_pkgconfig_set)
116 # Invokes pkgconfig, cleans up the result and sets variables
117 macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
118 set(_pkgconfig_invoke_result)
121 COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
122 OUTPUT_VARIABLE _pkgconfig_invoke_result
123 RESULT_VARIABLE _pkgconfig_failed)
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}")
132 if (NOT ${_regexp} STREQUAL "")
133 string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
134 endif(NOT ${_regexp} STREQUAL "")
136 separate_arguments(_pkgconfig_invoke_result)
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)
142 endmacro(_pkgconfig_invoke)
144 # Invokes pkgconfig two times; once without '--static' and once with
146 macro(_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})
149 endmacro(_pkgconfig_invoke_dyn)
151 # Splits given arguments into options and a package list
152 macro(_pkgconfig_parse_options _result _is_req)
155 foreach(_pkg ${ARGN})
156 if (_pkg STREQUAL "REQUIRED")
158 endif (_pkg STREQUAL "REQUIRED")
159 endforeach(_pkg ${ARGN})
161 set(${_result} ${ARGN})
162 list(REMOVE_ITEM ${_result} "REQUIRED")
163 endmacro(_pkgconfig_parse_options)
166 macro(_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)
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)
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})
202 set(_pkg_check_modules_packages)
203 set(_pkg_check_modules_failed)
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)
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 ".*(>=|=|<=).*")
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 ">=")
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 "=")
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 "<=")
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)
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)
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}")
254 COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
255 RESULT_VARIABLE _pkgconfig_retval)
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})
263 set(_pkg_check_modules_failed 1)
264 endif(_pkgconfig_retval)
265 endforeach(_pkg_check_modules_pkg)
267 if(_pkg_check_modules_failed)
268 # fail when requested
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
276 _pkgconfig_set(${_prefix}_FOUND 1)
277 list(LENGTH _pkg_check_modules_packages pkg_count)
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)
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 )
293 message(STATUS " found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
294 endforeach(_pkg_check_modules_pkg)
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 )
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)
308 message(SEND_ERROR "pkg-config tool not found")
309 endif (${_is_required})
310 endif(PKG_CONFIG_EXECUTABLE)
311 endmacro(_pkg_check_modules_internal)
314 ### User visible macros start here
318 macro(pkg_check_modules _prefix _module0)
320 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
321 _pkgconfig_parse_options (_pkg_modules _pkg_is_required "${_module0}" ${ARGN})
322 _pkg_check_modules_internal("${_pkg_is_required}" 0 "${_prefix}" ${_pkg_modules})
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} OR NOT ${_prefix}_FOUND)
326 endmacro(pkg_check_modules)
329 macro(pkg_search_module _prefix _module0)
331 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
332 set(_pkg_modules_found 0)
333 _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required "${_module0}" ${ARGN})
335 message(STATUS "checking for one of the modules '${_pkg_modules_alt}'")
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)
343 if (${_prefix}_FOUND)
344 set(_pkg_modules_found 1)
345 endif(${_prefix}_FOUND)
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)
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} OR NOT ${_prefix}_FOUND)
356 endmacro(pkg_search_module)