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)
226 if(${_prefix}_INCLUDE_DIRS)
227 set_property(TARGET PkgConfig::${_prefix} PROPERTY
228 INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
231 set_property(TARGET PkgConfig::${_prefix} PROPERTY
232 INTERFACE_LINK_LIBRARIES "${_libs}")
234 if(${_prefix}_CFLAGS_OTHER)
235 set_property(TARGET PkgConfig::${_prefix} PROPERTY
236 INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
242 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _prefix)
243 _pkgconfig_unset(${_prefix}_FOUND)
244 _pkgconfig_unset(${_prefix}_VERSION)
245 _pkgconfig_unset(${_prefix}_PREFIX)
246 _pkgconfig_unset(${_prefix}_INCLUDEDIR)
247 _pkgconfig_unset(${_prefix}_LIBDIR)
248 _pkgconfig_unset(${_prefix}_LIBS)
249 _pkgconfig_unset(${_prefix}_LIBS_L)
250 _pkgconfig_unset(${_prefix}_LIBS_PATHS)
251 _pkgconfig_unset(${_prefix}_LIBS_OTHER)
252 _pkgconfig_unset(${_prefix}_CFLAGS)
253 _pkgconfig_unset(${_prefix}_CFLAGS_I)
254 _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
255 _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
256 _pkgconfig_unset(${_prefix}_STATIC_LIBS)
257 _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
258 _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
259 _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
260 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
261 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
262 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
264 # create a better addressable variable of the modules and calculate its size
265 set(_pkg_check_modules_list ${ARGN})
266 list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
268 if(PKG_CONFIG_EXECUTABLE)
269 # give out status message telling checked module
270 if (NOT ${_is_silent})
271 if (_pkg_check_modules_cnt EQUAL 1)
272 message(STATUS "Checking for module '${_pkg_check_modules_list}'")
274 message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
278 set(_pkg_check_modules_packages)
279 set(_pkg_check_modules_failed)
283 if(NOT _no_cmake_path)
284 _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
285 _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
286 _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
289 if(NOT _no_cmake_environment_path)
290 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
291 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
292 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
295 if(NOT "${_extra_paths}" STREQUAL "")
296 # Save the PKG_CONFIG_PATH environment variable, and add paths
297 # from the CMAKE_PREFIX_PATH variables
298 set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
299 set(_pkgconfig_path "${_pkgconfig_path_old}")
300 if(NOT "${_pkgconfig_path}" STREQUAL "")
301 file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
304 # Create a list of the possible pkgconfig subfolder (depending on
307 if(NOT DEFINED CMAKE_SYSTEM_NAME
308 OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
309 AND NOT CMAKE_CROSSCOMPILING))
310 if(EXISTS "/etc/debian_version") # is this a debian system ?
311 if(CMAKE_LIBRARY_ARCHITECTURE)
312 list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
315 # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
316 get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
317 if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
318 list(APPEND _lib_dirs "lib32/pkgconfig")
320 get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
321 if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
322 list(APPEND _lib_dirs "lib64/pkgconfig")
324 get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
325 if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
326 list(APPEND _lib_dirs "libx32/pkgconfig")
330 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
331 list(APPEND _lib_dirs "libdata/pkgconfig")
333 list(APPEND _lib_dirs "lib/pkgconfig")
334 list(APPEND _lib_dirs "share/pkgconfig")
336 # Check if directories exist and eventually append them to the
337 # pkgconfig path list
338 foreach(_prefix_dir ${_extra_paths})
339 foreach(_lib_dir ${_lib_dirs})
340 if(EXISTS "${_prefix_dir}/${_lib_dir}")
341 list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
342 list(REMOVE_DUPLICATES _pkgconfig_path)
347 # Prepare and set the environment variable
348 if(NOT "${_pkgconfig_path}" STREQUAL "")
349 # remove empty values from the list
350 list(REMOVE_ITEM _pkgconfig_path "")
351 file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
353 string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
354 string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
356 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
361 unset(_pkgconfig_path)
364 # iterate through module list and check whether they exist and match the required version
365 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
366 set(_pkg_check_modules_exist_query)
368 # check whether version is given
369 if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
370 set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
371 set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
372 set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
374 set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
375 set(_pkg_check_modules_pkg_op)
376 set(_pkg_check_modules_pkg_ver)
379 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
380 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
381 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
382 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
384 list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
386 # create the final query which is of the format:
387 # * <pkg-name> >= <version>
388 # * <pkg-name> = <version>
389 # * <pkg-name> <= <version>
390 # * --exists <pkg-name>
391 list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
392 if (_pkg_check_modules_pkg_op)
393 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
395 list(APPEND _pkg_check_modules_exist_query --exists)
396 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
401 COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
402 RESULT_VARIABLE _pkgconfig_retval
403 ERROR_VARIABLE _pkgconfig_error
404 ERROR_STRIP_TRAILING_WHITESPACE)
406 # evaluate result and tell failures
407 if (_pkgconfig_retval)
408 if(NOT ${_is_silent})
409 message(STATUS " ${_pkgconfig_error}")
412 set(_pkg_check_modules_failed 1)
416 if(_pkg_check_modules_failed)
417 # fail when requested
419 message(FATAL_ERROR "A required package was not found")
422 # when we are here, we checked whether requested modules
423 # exist. Now, go through them and set variables
425 _pkgconfig_set(${_prefix}_FOUND 1)
426 list(LENGTH _pkg_check_modules_packages pkg_count)
428 # iterate through all modules again and set individual variables
429 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
430 # handle case when there is only one package required
431 if (pkg_count EQUAL 1)
432 set(_pkg_check_prefix "${_prefix}")
434 set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
437 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
438 pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
439 pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
440 pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
441 foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
442 _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
445 if (NOT ${_is_silent})
446 message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
450 # set variables which are combined for multiple modules
451 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
452 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
453 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
454 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
456 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
457 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
458 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
461 _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
465 if(NOT "${_extra_paths}" STREQUAL "")
466 # Restore the environment variable
467 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
471 unset(_pkgconfig_path_old)
474 message(SEND_ERROR "pkg-config tool not found")
480 #[========================================[.rst:
481 .. command:: pkg_check_modules
483 Checks for all the given modules, setting a variety of result variables in
484 the calling scope. ::
486 pkg_check_modules(<prefix>
489 [NO_CMAKE_ENVIRONMENT_PATH]
491 <moduleSpec> [<moduleSpec>...])
493 When the ``REQUIRED`` argument is given, the command will fail with an error
494 if module(s) could not be found.
496 When the ``QUIET`` argument is given, no status messages will be printed.
498 By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
499 later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set to a
500 boolean ``True`` value, then the :variable:`CMAKE_PREFIX_PATH`,
501 :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
502 and environment variables will be added to the ``pkg-config`` search path.
503 The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
504 disable this behavior for the cache variables and environment variables
507 The ``IMPORTED_TARGET`` argument will create an imported target named
508 ``PkgConfig::<prefix>`` that can be passed directly as an argument to
509 :command:`target_link_libraries`.
511 Each ``<moduleSpec>`` must be in one of the following formats::
513 {moduleName} ... matches any version
514 {moduleName}>={version} ... at least version <version> is required
515 {moduleName}={version} ... exactly version <version> is required
516 {moduleName}<={version} ... modules must not be newer than <version>
518 The following variables may be set upon return. Two sets of values exist,
519 one for the common case (``<XXX> = <prefix>``) and another for the
520 information ``pkg-config`` provides when it is called with the ``--static``
521 option (``<XXX> = <prefix>_STATIC``)::
523 <XXX>_FOUND ... set to 1 if module(s) exist
524 <XXX>_LIBRARIES ... only the libraries (without the '-l')
525 <XXX>_LIBRARY_DIRS ... the paths of the libraries (without the '-L')
526 <XXX>_LDFLAGS ... all required linker flags
527 <XXX>_LDFLAGS_OTHER ... all other linker flags
528 <XXX>_INCLUDE_DIRS ... the '-I' preprocessor flags (without the '-I')
529 <XXX>_CFLAGS ... all required cflags
530 <XXX>_CFLAGS_OTHER ... the other compiler flags
532 All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
533 associated variable returned from ``pkg-config`` has multiple values.
535 There are some special variables whose prefix depends on the number of
536 ``<moduleSpec>`` given. When there is only one ``<moduleSpec>``,
537 ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
538 items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``::
540 <YYY>_VERSION ... version of the module
541 <YYY>_PREFIX ... prefix directory of the module
542 <YYY>_INCLUDEDIR ... include directory of the module
543 <YYY>_LIBDIR ... lib directory of the module
547 .. code-block:: cmake
549 pkg_check_modules (GLIB2 glib-2.0)
551 Looks for any version of glib2. If found, the output variable
552 ``GLIB2_VERSION`` will hold the actual version found.
554 .. code-block:: cmake
556 pkg_check_modules (GLIB2 glib-2.0>=2.10)
558 Looks for at least version 2.10 of glib2. If found, the output variable
559 ``GLIB2_VERSION`` will hold the actual version found.
561 .. code-block:: cmake
563 pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
565 Looks for both glib2-2.0 (at least version 2.10) and any version of
566 gtk2+-2.0. Only if both are found will ``FOO`` be considered found.
567 The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
568 set to their respective found module versions.
570 .. code-block:: cmake
572 pkg_check_modules (XRENDER REQUIRED xrender)
574 Requires any version of ``xrender``. Example output variables set by a
577 XRENDER_LIBRARIES=Xrender;X11
578 XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
579 #]========================================]
580 macro(pkg_check_modules _prefix _module0)
581 _pkgconfig_parse_options(_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
583 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
584 (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
585 ( "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
586 _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" ${_pkg_modules})
588 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
589 if (${_prefix}_FOUND)
590 _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
592 elseif (${_prefix}_FOUND AND ${_imp_target})
593 _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
598 #[========================================[.rst:
599 .. command:: pkg_search_module
601 The behavior of this command is the same as :command:`pkg_check_modules`,
602 except that rather than checking for all the specified modules, it searches
603 for just the first successful match. ::
605 pkg_search_module(<prefix>
608 [NO_CMAKE_ENVIRONMENT_PATH]
610 <moduleSpec> [<moduleSpec>...])
614 .. code-block:: cmake
616 pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
617 #]========================================]
618 macro(pkg_search_module _prefix _module0)
619 _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
621 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
622 set(_pkg_modules_found 0)
624 if (NOT ${_pkg_is_silent})
625 message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
628 # iterate through all modules and stop at the first working one.
629 foreach(_pkg_alt ${_pkg_modules_alt})
630 if(NOT _pkg_modules_found)
631 _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" "${_pkg_alt}")
634 if (${_prefix}_FOUND)
635 set(_pkg_modules_found 1)
639 if (NOT ${_prefix}_FOUND)
640 if(${_pkg_is_required})
641 message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
645 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
646 elseif (${_prefix}_FOUND AND ${_imp_target})
647 _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
652 #[========================================[.rst:
653 Variables Affecting Behavior
654 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
656 .. variable:: PKG_CONFIG_EXECUTABLE
658 This can be set to the path of the pkg-config executable. If not provided,
659 it will be set by the module as a result of calling :command:`find_program`
660 internally. The ``PKG_CONFIG`` environment variable can be used as a hint.
662 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
664 Specifies whether :command:`pkg_check_modules` and
665 :command:`pkg_search_module` should add the paths in the
666 :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
667 :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
668 ``pkg-config`` search path.
670 If this variable is not set, this behavior is enabled by default if
671 :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
673 #]========================================]