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:
15 True if a pkg-config executable was found.
17 ``PKG_CONFIG_VERSION_STRING``
18 .. versionadded:: 2.8.8
20 The version of pkg-config that was found.
22 ``PKG_CONFIG_EXECUTABLE``
23 The pathname of the pkg-config program.
26 .. versionadded:: 3.22
28 A list of arguments to pass to pkg-config.
30 Both ``PKG_CONFIG_EXECUTABLE`` and ``PKG_CONFIG_ARGN`` are initialized by the
31 module, but may be overridden by the user. See `Variables Affecting Behavior`_
32 for how these variables are initialized.
34 #]========================================]
37 cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
38 cmake_policy(SET CMP0057 NEW) # if IN_LIST
41 set(PKG_CONFIG_VERSION 1)
43 # find pkg-config, use PKG_CONFIG if set
44 if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
45 separate_arguments(PKG_CONFIG_FROM_ENV_SPLIT NATIVE_COMMAND PROGRAM SEPARATE_ARGS "$ENV{PKG_CONFIG}")
46 list(LENGTH PKG_CONFIG_FROM_ENV_SPLIT PKG_CONFIG_FROM_ENV_SPLIT_ARGC)
47 if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 0)
48 list(GET PKG_CONFIG_FROM_ENV_SPLIT 0 PKG_CONFIG_FROM_ENV_ARGV0)
49 if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 1)
50 list(SUBLIST PKG_CONFIG_FROM_ENV_SPLIT 1 -1 PKG_CONFIG_ARGN)
52 set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_FROM_ENV_ARGV0}" CACHE FILEPATH "pkg-config executable")
56 set(PKG_CONFIG_NAMES "pkg-config")
58 list(PREPEND PKG_CONFIG_NAMES "pkg-config.bat")
60 list(APPEND PKG_CONFIG_NAMES "pkgconf")
62 find_program(PKG_CONFIG_EXECUTABLE
63 NAMES ${PKG_CONFIG_NAMES}
65 DOC "pkg-config executable")
66 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
68 set(PKG_CONFIG_ARGN "${PKG_CONFIG_ARGN}" CACHE STRING "Arguments to supply to pkg-config")
69 mark_as_advanced(PKG_CONFIG_ARGN)
71 set(_PKG_CONFIG_FAILURE_MESSAGE "")
72 if (PKG_CONFIG_EXECUTABLE)
73 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} --version
74 OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE
75 ERROR_VARIABLE _PKG_CONFIG_VERSION_ERROR ERROR_STRIP_TRAILING_WHITESPACE
76 RESULT_VARIABLE _PKG_CONFIG_VERSION_RESULT
79 if (NOT _PKG_CONFIG_VERSION_RESULT EQUAL 0)
80 string(REPLACE "\n" "\n " _PKG_CONFIG_VERSION_ERROR " ${_PKG_CONFIG_VERSION_ERROR}")
82 string(REPLACE ";" " " PKG_CONFIG_ARGN " ${PKG_CONFIG_ARGN}")
84 string(APPEND _PKG_CONFIG_FAILURE_MESSAGE
86 " \"${PKG_CONFIG_EXECUTABLE}\"${PKG_CONFIG_ARGN} --version\n"
87 " failed with output:\n${PKG_CONFIG_VERSION_STRING}\n"
88 " stderr: \n${_PKG_CONFIG_VERSION_ERROR}\n"
89 " result: \n${_PKG_CONFIG_VERSION_RESULT}"
91 set(PKG_CONFIG_EXECUTABLE "")
92 set(PKG_CONFIG_ARGN "")
93 unset(PKG_CONFIG_VERSION_STRING)
95 unset(_PKG_CONFIG_VERSION_RESULT)
98 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
99 find_package_handle_standard_args(PkgConfig
100 REQUIRED_VARS PKG_CONFIG_EXECUTABLE
101 REASON_FAILURE_MESSAGE "${_PKG_CONFIG_FAILURE_MESSAGE}"
102 VERSION_VAR PKG_CONFIG_VERSION_STRING)
104 # This is needed because the module name is "PkgConfig" but the name of
105 # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
107 set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
109 # Unsets the given variables
110 macro(_pkgconfig_unset var)
111 # Clear normal variable (possibly set by project code).
113 # Store as cache variable.
114 # FIXME: Add a policy to switch to a normal variable.
115 set(${var} "" CACHE INTERNAL "")
118 macro(_pkgconfig_set var value)
119 # Clear normal variable (possibly set by project code).
121 # Store as cache variable.
122 # FIXME: Add a policy to switch to a normal variable.
123 set(${var} ${value} CACHE INTERNAL "")
126 # Invokes pkgconfig, cleans up the result and sets variables
127 macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
128 set(_pkgconfig_invoke_result)
131 COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${ARGN} ${_pkglist}
132 OUTPUT_VARIABLE _pkgconfig_invoke_result
133 RESULT_VARIABLE _pkgconfig_failed
134 OUTPUT_STRIP_TRAILING_WHITESPACE)
136 if (_pkgconfig_failed)
137 set(_pkgconfig_${_varname} "")
138 _pkgconfig_unset(${_prefix}_${_varname})
140 string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
142 if (NOT ${_regexp} STREQUAL "")
143 string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
146 separate_arguments(_pkgconfig_invoke_result)
148 #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
149 set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
150 _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
154 # Internal version of pkg_get_variable; expects PKG_CONFIG_PATH to already be set
155 function (_pkg_get_variable result pkg variable)
156 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
162 # Invokes pkgconfig two times; once without '--static' and once with
164 macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
165 _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
166 _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
169 # Splits given arguments into options and a package list
170 macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global)
173 set(${_no_cmake_path} 0)
174 set(${_no_cmake_environment_path} 0)
175 set(${_imp_target} 0)
176 set(${_imp_target_global} 0)
177 if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
178 if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
179 set(${_no_cmake_path} 1)
180 set(${_no_cmake_environment_path} 1)
182 elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
183 set(${_no_cmake_path} 1)
184 set(${_no_cmake_environment_path} 1)
187 foreach(_pkg ${ARGN})
188 if (_pkg STREQUAL "REQUIRED")
191 if (_pkg STREQUAL "QUIET")
194 if (_pkg STREQUAL "NO_CMAKE_PATH")
195 set(${_no_cmake_path} 1)
197 if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
198 set(${_no_cmake_environment_path} 1)
200 if (_pkg STREQUAL "IMPORTED_TARGET")
201 set(${_imp_target} 1)
203 if (_pkg STREQUAL "GLOBAL")
204 set(${_imp_target_global} 1)
208 if (${_imp_target_global} AND NOT ${_imp_target})
209 message(SEND_ERROR "the argument GLOBAL may only be used together with IMPORTED_TARGET")
212 set(${_result} ${ARGN})
213 list(REMOVE_ITEM ${_result} "REQUIRED")
214 list(REMOVE_ITEM ${_result} "QUIET")
215 list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
216 list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
217 list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
218 list(REMOVE_ITEM ${_result} "GLOBAL")
221 # Add the content of a variable or an environment variable to a list of
224 # - _pkgconfig_add_extra_path(_extra_paths VAR)
225 # - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
226 function(_pkgconfig_add_extra_path _extra_paths_var _var)
228 if(ARGC GREATER 2 AND _var STREQUAL "ENV")
233 if(NOT "${${_var}}" STREQUAL "")
234 list(APPEND ${_extra_paths_var} ${${_var}})
237 if(NOT "$ENV{${_var}}" STREQUAL "")
238 file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
239 list(APPEND ${_extra_paths_var} ${_path})
243 set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
246 # scan the LDFLAGS returned by pkg-config for library directories and
247 # libraries, figure out the absolute paths of that libraries in the
249 function(_pkg_find_libs _prefix _no_cmake_path _no_cmake_environment_path)
253 # set the options that are used as long as the .pc file does not provide a library
256 list(APPEND _find_opts "NO_CMAKE_PATH")
258 if(_no_cmake_environment_path)
259 list(APPEND _find_opts "NO_CMAKE_ENVIRONMENT_PATH")
263 unset(_next_is_framework)
264 foreach (flag IN LISTS ${_prefix}_LDFLAGS)
265 if (_next_is_framework)
266 list(APPEND _libs "-framework ${flag}")
267 unset(_next_is_framework)
270 if (flag MATCHES "^-L(.*)")
271 list(APPEND _search_paths ${CMAKE_MATCH_1})
274 if (flag MATCHES "^-l(.*)")
275 set(_pkg_search "${CMAKE_MATCH_1}")
277 if (flag STREQUAL "-framework")
278 set(_next_is_framework TRUE)
284 # Firstly search in -L paths
285 find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
287 HINTS ${_search_paths} NO_DEFAULT_PATH)
289 find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
292 mark_as_advanced(pkgcfg_lib_${_prefix}_${_pkg_search})
293 if(pkgcfg_lib_${_prefix}_${_pkg_search})
294 list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
296 list(APPEND _libs ${_pkg_search})
300 set(${_prefix}_LINK_LIBRARIES "${_libs}" PARENT_SCOPE)
303 # create an imported target from all the information returned by pkg-config
304 function(_pkg_create_imp_target _prefix _imp_target_global)
305 if (NOT TARGET PkgConfig::${_prefix})
306 if(${_imp_target_global})
307 set(_global_opt "GLOBAL")
311 add_library(PkgConfig::${_prefix} INTERFACE IMPORTED ${_global_opt})
313 if(${_prefix}_INCLUDE_DIRS)
314 set_property(TARGET PkgConfig::${_prefix} PROPERTY
315 INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
317 if(${_prefix}_LINK_LIBRARIES)
318 set_property(TARGET PkgConfig::${_prefix} PROPERTY
319 INTERFACE_LINK_LIBRARIES "${${_prefix}_LINK_LIBRARIES}")
321 if(${_prefix}_LDFLAGS_OTHER)
322 set_property(TARGET PkgConfig::${_prefix} PROPERTY
323 INTERFACE_LINK_OPTIONS "${${_prefix}_LDFLAGS_OTHER}")
325 if(${_prefix}_CFLAGS_OTHER)
326 set_property(TARGET PkgConfig::${_prefix} PROPERTY
327 INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
332 # recalculate the dynamic output
333 # this is a macro and not a function so the result of _pkg_find_libs is automatically propagated
334 macro(_pkg_recalculate _prefix _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global)
335 _pkg_find_libs(${_prefix} ${_no_cmake_path} ${_no_cmake_environment_path})
337 _pkg_create_imp_target(${_prefix} ${_imp_target_global})
342 macro(_pkg_set_path_internal)
345 if(NOT _no_cmake_path)
346 _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
347 _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
348 _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
351 if(NOT _no_cmake_environment_path)
352 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
353 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
354 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
357 if(NOT _extra_paths STREQUAL "")
358 # Save the PKG_CONFIG_PATH environment variable, and add paths
359 # from the CMAKE_PREFIX_PATH variables
360 set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
361 set(_pkgconfig_path "${_pkgconfig_path_old}")
362 if(NOT _pkgconfig_path STREQUAL "")
363 file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
366 # Create a list of the possible pkgconfig subfolder (depending on
369 if(NOT DEFINED CMAKE_SYSTEM_NAME
370 OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
371 AND NOT CMAKE_CROSSCOMPILING))
372 if(EXISTS "/etc/debian_version") # is this a debian system ?
373 if(CMAKE_LIBRARY_ARCHITECTURE)
374 list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
377 # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
378 get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
379 if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
380 list(APPEND _lib_dirs "lib32/pkgconfig")
382 get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
383 if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
384 list(APPEND _lib_dirs "lib64/pkgconfig")
386 get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
387 if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
388 list(APPEND _lib_dirs "libx32/pkgconfig")
392 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
393 list(APPEND _lib_dirs "libdata/pkgconfig")
395 list(APPEND _lib_dirs "lib/pkgconfig")
396 list(APPEND _lib_dirs "share/pkgconfig")
398 # Check if directories exist and eventually append them to the
399 # pkgconfig path list
400 foreach(_prefix_dir ${_extra_paths})
401 foreach(_lib_dir ${_lib_dirs})
402 if(EXISTS "${_prefix_dir}/${_lib_dir}")
403 list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
404 list(REMOVE_DUPLICATES _pkgconfig_path)
409 # Prepare and set the environment variable
410 if(NOT _pkgconfig_path STREQUAL "")
411 # remove empty values from the list
412 list(REMOVE_ITEM _pkgconfig_path "")
413 file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
415 string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
416 string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
418 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
423 unset(_pkgconfig_path)
426 # Tell pkg-config not to strip any -L paths so we can search them all.
427 if(DEFINED ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
428 set(_pkgconfig_allow_system_libs_old "$ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS}")
430 unset(_pkgconfig_allow_system_libs_old)
432 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} 0)
435 macro(_pkg_restore_path_internal)
436 if(NOT _extra_paths STREQUAL "")
437 # Restore the environment variable
438 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
440 if(DEFINED _pkgconfig_allow_system_libs_old)
441 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} "${_pkgconfig_allow_system_libs_old}")
442 unset(_pkgconfig_allow_system_libs_old)
446 unset(_pkgconfig_path_old)
449 # pkg-config returns frameworks in --libs-only-other
450 # they need to be in ${_prefix}_LIBRARIES so "-framework a -framework b" does
451 # not incorrectly be combined to "-framework a b"
452 function(_pkgconfig_extract_frameworks _prefix)
453 set(ldflags "${${_prefix}_LDFLAGS_OTHER}")
454 list(FIND ldflags "-framework" FR_POS)
455 list(LENGTH ldflags LD_LENGTH)
457 # reduce length by 1 as we need "-framework" and the next entry
458 math(EXPR LD_LENGTH "${LD_LENGTH} - 1")
459 while (FR_POS GREATER -1 AND LD_LENGTH GREATER FR_POS)
460 list(REMOVE_AT ldflags ${FR_POS})
461 list(GET ldflags ${FR_POS} HEAD)
462 list(REMOVE_AT ldflags ${FR_POS})
463 math(EXPR LD_LENGTH "${LD_LENGTH} - 2")
465 list(APPEND LIBS "-framework ${HEAD}")
467 list(FIND ldflags "-framework" FR_POS)
469 set(${_prefix}_LIBRARIES ${${_prefix}_LIBRARIES} ${LIBS} PARENT_SCOPE)
470 set(${_prefix}_LDFLAGS_OTHER "${ldflags}" PARENT_SCOPE)
473 # pkg-config returns -isystem include directories in --cflags-only-other,
474 # depending on the version and if there is a space between -isystem and
476 function(_pkgconfig_extract_isystem _prefix)
477 set(cflags "${${_prefix}_CFLAGS_OTHER}")
479 set(incdirs "${${_prefix}_INCLUDE_DIRS}")
481 set(next_is_isystem FALSE)
482 foreach (THING IN LISTS cflags)
483 # This may filter "-isystem -isystem". That would not work anyway,
485 if (THING STREQUAL "-isystem")
486 set(next_is_isystem TRUE)
490 set(next_is_isystem FALSE)
491 list(APPEND incdirs "${THING}")
492 elseif (THING MATCHES "^-isystem")
493 string(SUBSTRING "${THING}" 8 -1 THING)
494 list(APPEND incdirs "${THING}")
496 list(APPEND outflags "${THING}")
499 set(${_prefix}_CFLAGS_OTHER "${outflags}" PARENT_SCOPE)
500 set(${_prefix}_INCLUDE_DIRS "${incdirs}" PARENT_SCOPE)
504 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global _prefix)
505 _pkgconfig_unset(${_prefix}_FOUND)
506 _pkgconfig_unset(${_prefix}_VERSION)
507 _pkgconfig_unset(${_prefix}_PREFIX)
508 _pkgconfig_unset(${_prefix}_INCLUDEDIR)
509 _pkgconfig_unset(${_prefix}_LIBDIR)
510 _pkgconfig_unset(${_prefix}_MODULE_NAME)
511 _pkgconfig_unset(${_prefix}_LIBS)
512 _pkgconfig_unset(${_prefix}_LIBS_L)
513 _pkgconfig_unset(${_prefix}_LIBS_PATHS)
514 _pkgconfig_unset(${_prefix}_LIBS_OTHER)
515 _pkgconfig_unset(${_prefix}_CFLAGS)
516 _pkgconfig_unset(${_prefix}_CFLAGS_I)
517 _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
518 _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
519 _pkgconfig_unset(${_prefix}_STATIC_LIBS)
520 _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
521 _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
522 _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
523 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
524 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
525 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
527 # create a better addressable variable of the modules and calculate its size
528 set(_pkg_check_modules_list ${ARGN})
529 list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
531 if(PKG_CONFIG_EXECUTABLE)
532 # give out status message telling checked module
533 if (NOT ${_is_silent})
534 if (_pkg_check_modules_cnt EQUAL 1)
535 message(STATUS "Checking for module '${_pkg_check_modules_list}'")
537 message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
541 set(_pkg_check_modules_packages)
542 set(_pkg_check_modules_failed)
544 _pkg_set_path_internal()
546 # iterate through module list and check whether they exist and match the required version
547 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
548 set(_pkg_check_modules_exist_query)
550 # check whether version is given
551 if (_pkg_check_modules_pkg MATCHES "(.*[^><])(=|[><]=?)(.*)")
552 set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
553 set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
554 set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
556 set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
557 set(_pkg_check_modules_pkg_op)
558 set(_pkg_check_modules_pkg_ver)
561 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
562 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
563 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
564 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
566 list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
568 # create the final query which is of the format:
569 # * <pkg-name> > <version>
570 # * <pkg-name> >= <version>
571 # * <pkg-name> = <version>
572 # * <pkg-name> <= <version>
573 # * <pkg-name> < <version>
574 # * --exists <pkg-name>
575 list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
576 if (_pkg_check_modules_pkg_op)
577 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
579 list(APPEND _pkg_check_modules_exist_query --exists)
580 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
585 COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${_pkg_check_modules_exist_query}
586 RESULT_VARIABLE _pkgconfig_retval
587 ERROR_VARIABLE _pkgconfig_error
588 ERROR_STRIP_TRAILING_WHITESPACE)
590 # evaluate result and tell failures
591 if (_pkgconfig_retval)
592 if(NOT ${_is_silent})
593 message(STATUS " ${_pkgconfig_error}")
596 set(_pkg_check_modules_failed 1)
600 if(_pkg_check_modules_failed)
601 # fail when requested
603 message(FATAL_ERROR "A required package was not found")
606 # when we are here, we checked whether requested modules
607 # exist. Now, go through them and set variables
609 _pkgconfig_set(${_prefix}_FOUND 1)
610 list(LENGTH _pkg_check_modules_packages pkg_count)
612 # iterate through all modules again and set individual variables
613 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
614 # handle case when there is only one package required
615 if (pkg_count EQUAL 1)
616 set(_pkg_check_prefix "${_prefix}")
618 set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
621 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
622 pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
623 pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
624 pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
625 foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
626 _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
628 _pkgconfig_set("${_pkg_check_prefix}_MODULE_NAME" "${_pkg_check_modules_pkg}")
630 if (NOT ${_is_silent})
631 message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
635 # set variables which are combined for multiple modules
636 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
637 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
638 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
639 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
641 if (APPLE AND "-framework" IN_LIST ${_prefix}_LDFLAGS_OTHER)
642 _pkgconfig_extract_frameworks("${_prefix}")
645 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )(-I|-isystem ?)" --cflags-only-I )
646 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
647 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
649 if (${_prefix}_CFLAGS_OTHER MATCHES "-isystem")
650 _pkgconfig_extract_isystem("${_prefix}")
653 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
656 _pkg_restore_path_internal()
659 message(SEND_ERROR "pkg-config tool not found")
665 #[========================================[.rst:
666 .. command:: pkg_check_modules
668 Checks for all the given modules, setting a variety of result variables in
671 .. code-block:: cmake
673 pkg_check_modules(<prefix>
676 [NO_CMAKE_ENVIRONMENT_PATH]
677 [IMPORTED_TARGET [GLOBAL]]
678 <moduleSpec> [<moduleSpec>...])
680 When the ``REQUIRED`` argument is given, the command will fail with an error
681 if module(s) could not be found.
683 When the ``QUIET`` argument is given, no status messages will be printed.
685 .. versionadded:: 3.1
686 The :variable:`CMAKE_PREFIX_PATH`,
687 :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
688 and environment variables will be added to the ``pkg-config`` search path.
689 The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
690 disable this behavior for the cache variables and environment variables
692 The :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` variable set to ``FALSE``
693 disables this behavior globally.
695 .. This didn't actually work until 3.3.
697 .. versionadded:: 3.6
698 The ``IMPORTED_TARGET`` argument will create an imported target named
699 ``PkgConfig::<prefix>`` that can be passed directly as an argument to
700 :command:`target_link_libraries`.
702 .. This didn't actually work until 3.7.
704 .. versionadded:: 3.13
705 The ``GLOBAL`` argument will make the
706 imported target available in global scope.
708 .. versionadded:: 3.15
709 Non-library linker options reported by ``pkg-config`` are stored in the
710 :prop_tgt:`INTERFACE_LINK_OPTIONS` target property.
712 .. versionchanged:: 3.18
713 Include directories specified with ``-isystem`` are stored in the
714 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` target property. Previous
715 versions of CMake left them in the :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
718 Each ``<moduleSpec>`` can be either a bare module name or it can be a
719 module name with a version constraint (operators ``=``, ``<``, ``>``,
720 ``<=`` and ``>=`` are supported). The following are examples for a module
721 named ``foo`` with various constraints:
723 - ``foo`` matches any version.
724 - ``foo<2`` only matches versions before 2.
725 - ``foo>=3.1`` matches any version from 3.1 or later.
726 - ``foo=1.2.3`` requires that foo must be exactly version 1.2.3.
728 The following variables may be set upon return. Two sets of values exist:
729 One for the common case (``<XXX> = <prefix>``) and another for the
730 information ``pkg-config`` provides when called with the ``--static``
731 option (``<XXX> = <prefix>_STATIC``).
734 set to 1 if module(s) exist
736 only the libraries (without the '-l')
737 ``<XXX>_LINK_LIBRARIES``
738 the libraries and their absolute paths
739 ``<XXX>_LIBRARY_DIRS``
740 the paths of the libraries (without the '-L')
742 all required linker flags
743 ``<XXX>_LDFLAGS_OTHER``
744 all other linker flags
745 ``<XXX>_INCLUDE_DIRS``
746 the '-I' preprocessor flags (without the '-I')
749 ``<XXX>_CFLAGS_OTHER``
750 the other compiler flags
752 All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
753 associated variable returned from ``pkg-config`` has multiple values.
755 .. versionchanged:: 3.18
756 Include directories specified with ``-isystem`` are stored in the
757 ``<XXX>_INCLUDE_DIRS`` variable. Previous versions of CMake left them
758 in ``<XXX>_CFLAGS_OTHER``.
760 There are some special variables whose prefix depends on the number of
761 ``<moduleSpec>`` given. When there is only one ``<moduleSpec>``,
762 ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
763 items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``.
766 version of the module
768 prefix directory of the module
770 include directory of the module
772 lib directory of the module
774 .. versionchanged:: 3.8
775 For any given ``<prefix>``, ``pkg_check_modules()`` can be called multiple
776 times with different parameters. Previous versions of CMake cached and
777 returned the first successful result.
779 .. versionchanged:: 3.16
780 If a full path to the found library can't be determined, but it's still
781 visible to the linker, pass it through as ``-l<name>``. Previous versions
782 of CMake failed in this case.
786 .. code-block:: cmake
788 pkg_check_modules (GLIB2 glib-2.0)
790 Looks for any version of glib2. If found, the output variable
791 ``GLIB2_VERSION`` will hold the actual version found.
793 .. code-block:: cmake
795 pkg_check_modules (GLIB2 glib-2.0>=2.10)
797 Looks for at least version 2.10 of glib2. If found, the output variable
798 ``GLIB2_VERSION`` will hold the actual version found.
800 .. code-block:: cmake
802 pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
804 Looks for both glib2-2.0 (at least version 2.10) and any version of
805 gtk2+-2.0. Only if both are found will ``FOO`` be considered found.
806 The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
807 set to their respective found module versions.
809 .. code-block:: cmake
811 pkg_check_modules (XRENDER REQUIRED xrender)
813 Requires any version of ``xrender``. Example output variables set by a
816 XRENDER_LIBRARIES=Xrender;X11
817 XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
818 #]========================================]
819 macro(pkg_check_modules _prefix _module0)
820 _pkgconfig_parse_options(_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global "${_module0}" ${ARGN})
822 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
823 (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
824 ( "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
825 _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global} "${_prefix}" ${_pkg_modules})
827 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
828 if (${_prefix}_FOUND)
829 _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
832 if (${_prefix}_FOUND)
833 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
839 #[========================================[.rst:
840 .. command:: pkg_search_module
842 The behavior of this command is the same as :command:`pkg_check_modules`,
843 except that rather than checking for all the specified modules, it searches
844 for just the first successful match.
846 .. code-block:: cmake
848 pkg_search_module(<prefix>
851 [NO_CMAKE_ENVIRONMENT_PATH]
852 [IMPORTED_TARGET [GLOBAL]]
853 <moduleSpec> [<moduleSpec>...])
855 .. versionadded:: 3.16
856 If a module is found, the ``<prefix>_MODULE_NAME`` variable will contain the
857 name of the matching module. This variable can be used if you need to run
858 :command:`pkg_get_variable`.
862 .. code-block:: cmake
864 pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
865 #]========================================]
866 macro(pkg_search_module _prefix _module0)
867 _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global "${_module0}" ${ARGN})
869 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
870 set(_pkg_modules_found 0)
872 if (NOT ${_pkg_is_silent})
873 message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
876 # iterate through all modules and stop at the first working one.
877 foreach(_pkg_alt ${_pkg_modules_alt})
878 if(NOT _pkg_modules_found)
879 _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global} "${_prefix}" "${_pkg_alt}")
882 if (${_prefix}_FOUND)
883 set(_pkg_modules_found 1)
888 if (NOT ${_prefix}_FOUND)
889 if(${_pkg_is_required})
890 message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
894 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
895 elseif (${_prefix}_FOUND)
896 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
900 #[========================================[.rst:
901 .. command:: pkg_get_variable
903 .. versionadded:: 3.4
905 Retrieves the value of a pkg-config variable ``varName`` and stores it in the
906 result variable ``resultVar`` in the calling scope.
908 .. code-block:: cmake
910 pkg_get_variable(<resultVar> <moduleName> <varName>)
912 If ``pkg-config`` returns multiple values for the specified variable,
913 ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
917 .. code-block:: cmake
919 pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
920 #]========================================]
921 function (pkg_get_variable result pkg variable)
922 _pkg_set_path_internal()
923 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
927 _pkg_restore_path_internal()
931 #[========================================[.rst:
932 Variables Affecting Behavior
933 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
935 .. variable:: PKG_CONFIG_EXECUTABLE
937 This cache variable can be set to the path of the pkg-config executable.
938 :command:`find_program` is called internally by the module with this
941 .. versionadded:: 3.1
942 The ``PKG_CONFIG`` environment variable can be used as a hint if
943 ``PKG_CONFIG_EXECUTABLE`` has not yet been set.
945 .. versionchanged:: 3.22
946 If the ``PKG_CONFIG`` environment variable is set, only the first
947 argument is taken from it when using it as a hint.
949 .. variable:: PKG_CONFIG_ARGN
951 .. versionadded:: 3.22
953 This cache variable can be set to a list of arguments to additionally pass
954 to pkg-config if needed. If not provided, it will be initialized from the
955 ``PKG_CONFIG`` environment variable, if set. The first argument in that
956 environment variable is assumed to be the pkg-config program, while all
957 remaining arguments after that are used to initialize ``PKG_CONFIG_ARGN``.
958 If no such environment variable is defined, ``PKG_CONFIG_ARGN`` is
959 initialized to an empty string. The module does not update the variable once
960 it has been set in the cache.
962 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
964 .. versionadded:: 3.1
966 Specifies whether :command:`pkg_check_modules` and
967 :command:`pkg_search_module` should add the paths in the
968 :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
969 :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
970 ``pkg-config`` search path.
972 If this variable is not set, this behavior is enabled by default if
973 :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
975 #]========================================]