1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[========================================[.rst:
8 A ``pkg-config`` module for CMake.
10 Finds the ``pkg-config`` executable and adds the :command:`pkg_get_variable`,
11 :command:`pkg_check_modules` and :command:`pkg_search_module` commands. The
12 following variables will also be set::
14 PKG_CONFIG_FOUND ... if pkg-config executable was found
15 PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
16 PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
19 #]========================================]
22 set(PKG_CONFIG_VERSION 1)
24 # find pkg-config, use PKG_CONFIG if set
25 if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
26 set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable")
28 find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
29 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
31 if (PKG_CONFIG_EXECUTABLE)
32 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version
33 OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING
35 OUTPUT_STRIP_TRAILING_WHITESPACE)
38 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
39 find_package_handle_standard_args(PkgConfig
40 REQUIRED_VARS PKG_CONFIG_EXECUTABLE
41 VERSION_VAR PKG_CONFIG_VERSION_STRING)
43 # This is needed because the module name is "PkgConfig" but the name of
44 # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
46 set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
48 # Unsets the given variables
49 macro(_pkgconfig_unset var)
50 set(${var} "" CACHE INTERNAL "")
53 macro(_pkgconfig_set var value)
54 set(${var} ${value} CACHE INTERNAL "")
57 # Invokes pkgconfig, cleans up the result and sets variables
58 macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
59 set(_pkgconfig_invoke_result)
62 COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
63 OUTPUT_VARIABLE _pkgconfig_invoke_result
64 RESULT_VARIABLE _pkgconfig_failed
65 OUTPUT_STRIP_TRAILING_WHITESPACE)
67 if (_pkgconfig_failed)
68 set(_pkgconfig_${_varname} "")
69 _pkgconfig_unset(${_prefix}_${_varname})
71 string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
73 if (NOT ${_regexp} STREQUAL "")
74 string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
77 separate_arguments(_pkgconfig_invoke_result)
79 #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
80 set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
81 _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
85 #[========================================[.rst:
86 .. command:: pkg_get_variable
88 Retrieves the value of a pkg-config variable ``varName`` and stores it in the
89 result variable ``resultVar`` in the calling scope. ::
91 pkg_get_variable(<resultVar> <moduleName> <varName>)
93 If ``pkg-config`` returns multiple values for the specified variable,
94 ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
100 pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
101 #]========================================]
102 function (pkg_get_variable result pkg variable)
103 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
109 # Invokes pkgconfig two times; once without '--static' and once with
111 macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
112 _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
113 _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
116 # Splits given arguments into options and a package list
117 macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target)
120 set(${_no_cmake_path} 0)
121 set(${_no_cmake_environment_path} 0)
122 set(${_imp_target} 0)
123 if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
124 if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
125 set(${_no_cmake_path} 1)
126 set(${_no_cmake_environment_path} 1)
128 elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
129 set(${_no_cmake_path} 1)
130 set(${_no_cmake_environment_path} 1)
133 foreach(_pkg ${ARGN})
134 if (_pkg STREQUAL "REQUIRED")
137 if (_pkg STREQUAL "QUIET")
140 if (_pkg STREQUAL "NO_CMAKE_PATH")
141 set(${_no_cmake_path} 1)
143 if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
144 set(${_no_cmake_environment_path} 1)
146 if (_pkg STREQUAL "IMPORTED_TARGET")
147 set(${_imp_target} 1)
151 set(${_result} ${ARGN})
152 list(REMOVE_ITEM ${_result} "REQUIRED")
153 list(REMOVE_ITEM ${_result} "QUIET")
154 list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
155 list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
156 list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
159 # Add the content of a variable or an environment variable to a list of
162 # - _pkgconfig_add_extra_path(_extra_paths VAR)
163 # - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
164 function(_pkgconfig_add_extra_path _extra_paths_var _var)
166 if(ARGC GREATER 2 AND _var STREQUAL "ENV")
171 if(NOT "${${_var}}" STREQUAL "")
172 list(APPEND ${_extra_paths_var} ${${_var}})
175 if(NOT "$ENV{${_var}}" STREQUAL "")
176 file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
177 list(APPEND ${_extra_paths_var} ${_path})
181 set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
184 # scan the LDFLAGS returned by pkg-config for library directories and
185 # libraries, figure out the absolute paths of that libraries in the
186 # given directories, and create an imported target from them
187 function(_pkg_create_imp_target _prefix _no_cmake_path _no_cmake_environment_path)
191 # set the options that are used as long as the .pc file does not provide a library
194 list(APPEND _find_opts "NO_CMAKE_PATH")
196 if(_no_cmake_environment_path)
197 list(APPEND _find_opts "NO_CMAKE_ENVIRONMENT_PATH")
201 foreach (flag IN LISTS ${_prefix}_LDFLAGS)
202 if (flag MATCHES "^-L(.*)")
203 # only look into the given paths from now on
204 list(APPEND _search_paths ${CMAKE_MATCH_1})
205 set(_find_opts HINTS ${_search_paths} NO_DEFAULT_PATH)
208 if (flag MATCHES "^-l(.*)")
209 set(_pkg_search "${CMAKE_MATCH_1}")
214 find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
217 list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
220 # only create the target if it is linkable, i.e. no executables
221 if (NOT TARGET PkgConfig::${_prefix}
222 AND ( ${_prefix}_INCLUDE_DIRS OR _libs OR ${_prefix}_CFLAGS_OTHER ))
223 add_library(PkgConfig::${_prefix} INTERFACE IMPORTED)
225 if(${_prefix}_INCLUDE_DIRS)
226 set_property(TARGET PkgConfig::${_prefix} PROPERTY
227 INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
230 set_property(TARGET PkgConfig::${_prefix} PROPERTY
231 INTERFACE_LINK_LIBRARIES "${_libs}")
233 if(${_prefix}_CFLAGS_OTHER)
234 set_property(TARGET PkgConfig::${_prefix} PROPERTY
235 INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
241 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _prefix)
242 _pkgconfig_unset(${_prefix}_FOUND)
243 _pkgconfig_unset(${_prefix}_VERSION)
244 _pkgconfig_unset(${_prefix}_PREFIX)
245 _pkgconfig_unset(${_prefix}_INCLUDEDIR)
246 _pkgconfig_unset(${_prefix}_LIBDIR)
247 _pkgconfig_unset(${_prefix}_LIBS)
248 _pkgconfig_unset(${_prefix}_LIBS_L)
249 _pkgconfig_unset(${_prefix}_LIBS_PATHS)
250 _pkgconfig_unset(${_prefix}_LIBS_OTHER)
251 _pkgconfig_unset(${_prefix}_CFLAGS)
252 _pkgconfig_unset(${_prefix}_CFLAGS_I)
253 _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
254 _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
255 _pkgconfig_unset(${_prefix}_STATIC_LIBS)
256 _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
257 _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
258 _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
259 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
260 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
261 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
263 # create a better addressable variable of the modules and calculate its size
264 set(_pkg_check_modules_list ${ARGN})
265 list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
267 if(PKG_CONFIG_EXECUTABLE)
268 # give out status message telling checked module
269 if (NOT ${_is_silent})
270 if (_pkg_check_modules_cnt EQUAL 1)
271 message(STATUS "Checking for module '${_pkg_check_modules_list}'")
273 message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
277 set(_pkg_check_modules_packages)
278 set(_pkg_check_modules_failed)
282 if(NOT _no_cmake_path)
283 _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
284 _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
285 _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
288 if(NOT _no_cmake_environment_path)
289 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
290 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
291 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
294 if(NOT "${_extra_paths}" STREQUAL "")
295 # Save the PKG_CONFIG_PATH environment variable, and add paths
296 # from the CMAKE_PREFIX_PATH variables
297 set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
298 set(_pkgconfig_path "${_pkgconfig_path_old}")
299 if(NOT "${_pkgconfig_path}" STREQUAL "")
300 file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
303 # Create a list of the possible pkgconfig subfolder (depending on
306 if(NOT DEFINED CMAKE_SYSTEM_NAME
307 OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
308 AND NOT CMAKE_CROSSCOMPILING))
309 if(EXISTS "/etc/debian_version") # is this a debian system ?
310 if(CMAKE_LIBRARY_ARCHITECTURE)
311 list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
314 # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
315 get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
316 if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
317 list(APPEND _lib_dirs "lib32/pkgconfig")
319 get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
320 if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
321 list(APPEND _lib_dirs "lib64/pkgconfig")
323 get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
324 if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
325 list(APPEND _lib_dirs "libx32/pkgconfig")
329 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
330 list(APPEND _lib_dirs "libdata/pkgconfig")
332 list(APPEND _lib_dirs "lib/pkgconfig")
333 list(APPEND _lib_dirs "share/pkgconfig")
335 # Check if directories exist and eventually append them to the
336 # pkgconfig path list
337 foreach(_prefix_dir ${_extra_paths})
338 foreach(_lib_dir ${_lib_dirs})
339 if(EXISTS "${_prefix_dir}/${_lib_dir}")
340 list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
341 list(REMOVE_DUPLICATES _pkgconfig_path)
346 # Prepare and set the environment variable
347 if(NOT "${_pkgconfig_path}" STREQUAL "")
348 # remove empty values from the list
349 list(REMOVE_ITEM _pkgconfig_path "")
350 file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
352 string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
353 string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
355 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
360 unset(_pkgconfig_path)
363 # iterate through module list and check whether they exist and match the required version
364 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
365 set(_pkg_check_modules_exist_query)
367 # check whether version is given
368 if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
369 set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
370 set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
371 set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
373 set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
374 set(_pkg_check_modules_pkg_op)
375 set(_pkg_check_modules_pkg_ver)
378 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
379 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
380 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
381 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
383 list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
385 # create the final query which is of the format:
386 # * <pkg-name> >= <version>
387 # * <pkg-name> = <version>
388 # * <pkg-name> <= <version>
389 # * --exists <pkg-name>
390 list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
391 if (_pkg_check_modules_pkg_op)
392 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
394 list(APPEND _pkg_check_modules_exist_query --exists)
395 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
400 COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
401 RESULT_VARIABLE _pkgconfig_retval
402 ERROR_VARIABLE _pkgconfig_error
403 ERROR_STRIP_TRAILING_WHITESPACE)
405 # evaluate result and tell failures
406 if (_pkgconfig_retval)
407 if(NOT ${_is_silent})
408 message(STATUS " ${_pkgconfig_error}")
411 set(_pkg_check_modules_failed 1)
415 if(_pkg_check_modules_failed)
416 # fail when requested
418 message(FATAL_ERROR "A required package was not found")
421 # when we are here, we checked whether requested modules
422 # exist. Now, go through them and set variables
424 _pkgconfig_set(${_prefix}_FOUND 1)
425 list(LENGTH _pkg_check_modules_packages pkg_count)
427 # iterate through all modules again and set individual variables
428 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
429 # handle case when there is only one package required
430 if (pkg_count EQUAL 1)
431 set(_pkg_check_prefix "${_prefix}")
433 set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
436 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
437 pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
438 pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
439 pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
440 foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
441 _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
444 if (NOT ${_is_silent})
445 message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
449 # set variables which are combined for multiple modules
450 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
451 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
452 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
453 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
455 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
456 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
457 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
460 _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
464 if(NOT "${_extra_paths}" STREQUAL "")
465 # Restore the environment variable
466 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
470 unset(_pkgconfig_path_old)
473 message(SEND_ERROR "pkg-config tool not found")
479 #[========================================[.rst:
480 .. command:: pkg_check_modules
482 Checks for all the given modules, setting a variety of result variables in
483 the calling scope. ::
485 pkg_check_modules(<prefix>
488 [NO_CMAKE_ENVIRONMENT_PATH]
490 <moduleSpec> [<moduleSpec>...])
492 When the ``REQUIRED`` argument is given, the command will fail with an error
493 if module(s) could not be found.
495 When the ``QUIET`` argument is given, no status messages will be printed.
497 By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
498 later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set to a
499 boolean ``True`` value, then the :variable:`CMAKE_PREFIX_PATH`,
500 :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
501 and environment variables will be added to the ``pkg-config`` search path.
502 The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
503 disable this behavior for the cache variables and environment variables
506 The ``IMPORTED_TARGET`` argument will create an imported target named
507 ``PkgConfig::<prefix>`` that can be passed directly as an argument to
508 :command:`target_link_libraries`.
510 Each ``<moduleSpec>`` must be in one of the following formats::
512 {moduleName} ... matches any version
513 {moduleName}>={version} ... at least version <version> is required
514 {moduleName}={version} ... exactly version <version> is required
515 {moduleName}<={version} ... modules must not be newer than <version>
517 The following variables may be set upon return. Two sets of values exist,
518 one for the common case (``<XXX> = <prefix>``) and another for the
519 information ``pkg-config`` provides when it is called with the ``--static``
520 option (``<XXX> = <prefix>_STATIC``)::
522 <XXX>_FOUND ... set to 1 if module(s) exist
523 <XXX>_LIBRARIES ... only the libraries (without the '-l')
524 <XXX>_LIBRARY_DIRS ... the paths of the libraries (without the '-L')
525 <XXX>_LDFLAGS ... all required linker flags
526 <XXX>_LDFLAGS_OTHER ... all other linker flags
527 <XXX>_INCLUDE_DIRS ... the '-I' preprocessor flags (without the '-I')
528 <XXX>_CFLAGS ... all required cflags
529 <XXX>_CFLAGS_OTHER ... the other compiler flags
531 All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
532 associated variable returned from ``pkg-config`` has multiple values.
534 There are some special variables whose prefix depends on the number of
535 ``<moduleSpec>`` given. When there is only one ``<moduleSpec>``,
536 ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
537 items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``::
539 <YYY>_VERSION ... version of the module
540 <YYY>_PREFIX ... prefix directory of the module
541 <YYY>_INCLUDEDIR ... include directory of the module
542 <YYY>_LIBDIR ... lib directory of the module
546 .. code-block:: cmake
548 pkg_check_modules (GLIB2 glib-2.0)
550 Looks for any version of glib2. If found, the output variable
551 ``GLIB2_VERSION`` will hold the actual version found.
553 .. code-block:: cmake
555 pkg_check_modules (GLIB2 glib-2.0>=2.10)
557 Looks for at least version 2.10 of glib2. If found, the output variable
558 ``GLIB2_VERSION`` will hold the actual version found.
560 .. code-block:: cmake
562 pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
564 Looks for both glib2-2.0 (at least version 2.10) and any version of
565 gtk2+-2.0. Only if both are found will ``FOO`` be considered found.
566 The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
567 set to their respective found module versions.
569 .. code-block:: cmake
571 pkg_check_modules (XRENDER REQUIRED xrender)
573 Requires any version of ``xrender``. Example output variables set by a
576 XRENDER_LIBRARIES=Xrender;X11
577 XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
578 #]========================================]
579 macro(pkg_check_modules _prefix _module0)
580 _pkgconfig_parse_options(_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
582 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
583 (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
584 ( "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
585 _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" ${_pkg_modules})
587 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
588 if (${_prefix}_FOUND)
589 _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
591 elseif (${_prefix}_FOUND AND ${_imp_target})
592 _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
597 #[========================================[.rst:
598 .. command:: pkg_search_module
600 The behavior of this command is the same as :command:`pkg_check_modules`,
601 except that rather than checking for all the specified modules, it searches
602 for just the first successful match. ::
604 pkg_search_module(<prefix>
607 [NO_CMAKE_ENVIRONMENT_PATH]
609 <moduleSpec> [<moduleSpec>...])
613 .. code-block:: cmake
615 pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
616 #]========================================]
617 macro(pkg_search_module _prefix _module0)
618 _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
620 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
621 set(_pkg_modules_found 0)
623 if (NOT ${_pkg_is_silent})
624 message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
627 # iterate through all modules and stop at the first working one.
628 foreach(_pkg_alt ${_pkg_modules_alt})
629 if(NOT _pkg_modules_found)
630 _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" "${_pkg_alt}")
633 if (${_prefix}_FOUND)
634 set(_pkg_modules_found 1)
638 if (NOT ${_prefix}_FOUND)
639 if(${_pkg_is_required})
640 message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
644 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
645 elseif (${_prefix}_FOUND AND ${_imp_target})
646 _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
651 #[========================================[.rst:
652 Variables Affecting Behavior
653 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
655 .. variable:: PKG_CONFIG_EXECUTABLE
657 This can be set to the path of the pkg-config executable. If not provided,
658 it will be set by the module as a result of calling :command:`find_program`
659 internally. The ``PKG_CONFIG`` environment variable can be used as a hint.
661 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
663 Specifies whether :command:`pkg_check_modules` and
664 :command:`pkg_search_module` should add the paths in the
665 :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
666 :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
667 ``pkg-config`` search path.
669 If this variable is not set, this behavior is enabled by default if
670 :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
672 #]========================================]