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