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")
59 set(_PKG_CONFIG_VALIDATOR VALIDATOR __FindPkgConfig_EXECUTABLE_VALIDATOR)
60 function(__FindPkgConfig_EXECUTABLE_VALIDATOR result_var candidate)
61 if(candidate MATCHES "\\.[Ee][Xx][Ee]$")
64 # Exclude the pkg-config distributed with Strawberry Perl.
65 execute_process(COMMAND "${candidate}" --help OUTPUT_VARIABLE _output ERROR_VARIABLE _output RESULT_VARIABLE _result)
66 if(NOT _result EQUAL 0 OR _output MATCHES "Pure-Perl")
67 set("${result_var}" FALSE PARENT_SCOPE)
71 set(_PKG_CONFIG_VALIDATOR "")
73 list(APPEND PKG_CONFIG_NAMES "pkgconf")
75 find_program(PKG_CONFIG_EXECUTABLE
76 NAMES ${PKG_CONFIG_NAMES}
78 DOC "pkg-config executable"
79 ${_PKG_CONFIG_VALIDATOR})
80 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
81 unset(_PKG_CONFIG_VALIDATOR)
83 set(PKG_CONFIG_ARGN "${PKG_CONFIG_ARGN}" CACHE STRING "Arguments to supply to pkg-config")
84 mark_as_advanced(PKG_CONFIG_ARGN)
86 set(_PKG_CONFIG_FAILURE_MESSAGE "")
87 if (PKG_CONFIG_EXECUTABLE)
88 execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} --version
89 OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE
90 ERROR_VARIABLE _PKG_CONFIG_VERSION_ERROR ERROR_STRIP_TRAILING_WHITESPACE
91 RESULT_VARIABLE _PKG_CONFIG_VERSION_RESULT
94 if (NOT _PKG_CONFIG_VERSION_RESULT EQUAL 0)
95 string(REPLACE "\n" "\n " _PKG_CONFIG_VERSION_ERROR " ${_PKG_CONFIG_VERSION_ERROR}")
97 string(REPLACE ";" " " PKG_CONFIG_ARGN " ${PKG_CONFIG_ARGN}")
99 string(APPEND _PKG_CONFIG_FAILURE_MESSAGE
101 " \"${PKG_CONFIG_EXECUTABLE}\"${PKG_CONFIG_ARGN} --version\n"
102 " failed with output:\n${PKG_CONFIG_VERSION_STRING}\n"
103 " stderr: \n${_PKG_CONFIG_VERSION_ERROR}\n"
104 " result: \n${_PKG_CONFIG_VERSION_RESULT}"
106 set(PKG_CONFIG_EXECUTABLE "")
107 set(PKG_CONFIG_ARGN "")
108 unset(PKG_CONFIG_VERSION_STRING)
110 unset(_PKG_CONFIG_VERSION_RESULT)
113 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
114 find_package_handle_standard_args(PkgConfig
115 REQUIRED_VARS PKG_CONFIG_EXECUTABLE
116 REASON_FAILURE_MESSAGE "${_PKG_CONFIG_FAILURE_MESSAGE}"
117 VERSION_VAR PKG_CONFIG_VERSION_STRING)
119 # This is needed because the module name is "PkgConfig" but the name of
120 # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
122 set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
124 # Unsets the given variables
125 macro(_pkgconfig_unset var)
126 # Clear normal variable (possibly set by project code).
128 # Store as cache variable.
129 # FIXME: Add a policy to switch to a normal variable.
130 set(${var} "" CACHE INTERNAL "")
133 macro(_pkgconfig_set var value)
134 # Clear normal variable (possibly set by project code).
136 # Store as cache variable.
137 # FIXME: Add a policy to switch to a normal variable.
138 set(${var} ${value} CACHE INTERNAL "")
141 # Invokes pkgconfig, cleans up the result and sets variables
142 macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
143 set(_pkgconfig_invoke_result)
146 COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${ARGN} ${_pkglist}
147 OUTPUT_VARIABLE _pkgconfig_invoke_result
148 RESULT_VARIABLE _pkgconfig_failed
149 OUTPUT_STRIP_TRAILING_WHITESPACE)
151 if (_pkgconfig_failed)
152 set(_pkgconfig_${_varname} "")
153 _pkgconfig_unset(${_prefix}_${_varname})
155 string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
157 if (NOT ${_regexp} STREQUAL "")
158 string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
161 # pkg-config can represent "spaces within an argument" by backslash-escaping the space.
162 # UNIX_COMMAND mode treats backslash-escaped spaces as "not a space that delimits arguments".
163 separate_arguments(_pkgconfig_invoke_result UNIX_COMMAND "${_pkgconfig_invoke_result}")
165 #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
166 set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
167 _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
171 # Internal version of pkg_get_variable; expects PKG_CONFIG_PATH to already be set
172 function (_pkg_get_variable result pkg variable)
173 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
179 # Invokes pkgconfig two times; once without '--static' and once with
181 macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
182 _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
183 _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
186 # Splits given arguments into options and a package list
187 macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global)
190 set(${_no_cmake_path} 0)
191 set(${_no_cmake_environment_path} 0)
192 set(${_imp_target} 0)
193 set(${_imp_target_global} 0)
194 if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
195 if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
196 set(${_no_cmake_path} 1)
197 set(${_no_cmake_environment_path} 1)
199 elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
200 set(${_no_cmake_path} 1)
201 set(${_no_cmake_environment_path} 1)
204 foreach(_pkg ${ARGN})
205 if (_pkg STREQUAL "REQUIRED")
208 if (_pkg STREQUAL "QUIET")
211 if (_pkg STREQUAL "NO_CMAKE_PATH")
212 set(${_no_cmake_path} 1)
214 if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
215 set(${_no_cmake_environment_path} 1)
217 if (_pkg STREQUAL "IMPORTED_TARGET")
218 set(${_imp_target} 1)
220 if (_pkg STREQUAL "GLOBAL")
221 set(${_imp_target_global} 1)
225 if (${_imp_target_global} AND NOT ${_imp_target})
226 message(SEND_ERROR "the argument GLOBAL may only be used together with IMPORTED_TARGET")
229 set(${_result} ${ARGN})
230 list(REMOVE_ITEM ${_result} "REQUIRED")
231 list(REMOVE_ITEM ${_result} "QUIET")
232 list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
233 list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
234 list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
235 list(REMOVE_ITEM ${_result} "GLOBAL")
238 # Add the content of a variable or an environment variable to a list of
241 # - _pkgconfig_add_extra_path(_extra_paths VAR)
242 # - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
243 function(_pkgconfig_add_extra_path _extra_paths_var _var)
245 if(ARGC GREATER 2 AND _var STREQUAL "ENV")
250 if(NOT "${${_var}}" STREQUAL "")
251 list(APPEND ${_extra_paths_var} ${${_var}})
254 if(NOT "$ENV{${_var}}" STREQUAL "")
255 file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
256 list(APPEND ${_extra_paths_var} ${_path})
260 set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
263 # scan the LDFLAGS returned by pkg-config for library directories and
264 # libraries, figure out the absolute paths of that libraries in the
266 function(_pkg_find_libs _prefix _no_cmake_path _no_cmake_environment_path)
270 # set the options that are used as long as the .pc file does not provide a library
273 list(APPEND _find_opts "NO_CMAKE_PATH")
275 if(_no_cmake_environment_path)
276 list(APPEND _find_opts "NO_CMAKE_ENVIRONMENT_PATH")
280 unset(_next_is_framework)
281 foreach (flag IN LISTS ${_prefix}_LDFLAGS)
282 if (_next_is_framework)
283 list(APPEND _libs "-framework ${flag}")
284 unset(_next_is_framework)
287 if (flag MATCHES "^-L(.*)")
288 list(APPEND _search_paths ${CMAKE_MATCH_1})
291 if (flag MATCHES "^-l(.*)")
292 set(_pkg_search "${CMAKE_MATCH_1}")
294 if (flag STREQUAL "-framework")
295 set(_next_is_framework TRUE)
301 # Firstly search in -L paths
302 find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
304 HINTS ${_search_paths} NO_DEFAULT_PATH)
306 find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
309 mark_as_advanced(pkgcfg_lib_${_prefix}_${_pkg_search})
310 if(pkgcfg_lib_${_prefix}_${_pkg_search})
311 list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
313 list(APPEND _libs ${_pkg_search})
317 set(${_prefix}_LINK_LIBRARIES "${_libs}" PARENT_SCOPE)
320 # create an imported target from all the information returned by pkg-config
321 function(_pkg_create_imp_target _prefix _imp_target_global)
322 if (NOT TARGET PkgConfig::${_prefix})
323 if(${_imp_target_global})
324 set(_global_opt "GLOBAL")
328 add_library(PkgConfig::${_prefix} INTERFACE IMPORTED ${_global_opt})
330 if(${_prefix}_INCLUDE_DIRS)
331 set_property(TARGET PkgConfig::${_prefix} PROPERTY
332 INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
334 if(${_prefix}_LINK_LIBRARIES)
335 set_property(TARGET PkgConfig::${_prefix} PROPERTY
336 INTERFACE_LINK_LIBRARIES "${${_prefix}_LINK_LIBRARIES}")
338 if(${_prefix}_LDFLAGS_OTHER)
339 set_property(TARGET PkgConfig::${_prefix} PROPERTY
340 INTERFACE_LINK_OPTIONS "${${_prefix}_LDFLAGS_OTHER}")
342 if(${_prefix}_CFLAGS_OTHER)
343 set_property(TARGET PkgConfig::${_prefix} PROPERTY
344 INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
349 # recalculate the dynamic output
350 # this is a macro and not a function so the result of _pkg_find_libs is automatically propagated
351 macro(_pkg_recalculate _prefix _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global)
352 _pkg_find_libs(${_prefix} ${_no_cmake_path} ${_no_cmake_environment_path})
354 _pkg_create_imp_target(${_prefix} ${_imp_target_global})
359 macro(_pkg_set_path_internal)
362 if(NOT _no_cmake_path)
363 _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
364 _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
365 _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
368 if(NOT _no_cmake_environment_path)
369 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
370 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
371 _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
374 if(NOT _extra_paths STREQUAL "")
375 # Save the PKG_CONFIG_PATH environment variable, and add paths
376 # from the CMAKE_PREFIX_PATH variables
377 set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
378 set(_pkgconfig_path "${_pkgconfig_path_old}")
379 if(NOT _pkgconfig_path STREQUAL "")
380 file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
383 # Create a list of the possible pkgconfig subfolder (depending on
386 if(NOT DEFINED CMAKE_SYSTEM_NAME
387 OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
388 AND NOT CMAKE_CROSSCOMPILING))
389 if(EXISTS "/etc/debian_version") # is this a debian system ?
390 if(CMAKE_LIBRARY_ARCHITECTURE)
391 list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
394 # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
395 get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
396 if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
397 list(APPEND _lib_dirs "lib32/pkgconfig")
399 get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
400 if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
401 list(APPEND _lib_dirs "lib64/pkgconfig")
403 get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
404 if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
405 list(APPEND _lib_dirs "libx32/pkgconfig")
409 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
410 list(APPEND _lib_dirs "libdata/pkgconfig")
412 list(APPEND _lib_dirs "lib/pkgconfig")
413 list(APPEND _lib_dirs "share/pkgconfig")
415 # Check if directories exist and eventually append them to the
416 # pkgconfig path list
417 foreach(_prefix_dir ${_extra_paths})
418 foreach(_lib_dir ${_lib_dirs})
419 if(EXISTS "${_prefix_dir}/${_lib_dir}")
420 list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
421 list(REMOVE_DUPLICATES _pkgconfig_path)
426 # Prepare and set the environment variable
427 if(NOT _pkgconfig_path STREQUAL "")
428 # remove empty values from the list
429 list(REMOVE_ITEM _pkgconfig_path "")
430 file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
432 string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
433 string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
435 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
440 unset(_pkgconfig_path)
443 # Tell pkg-config not to strip any -I or -L paths so we can search them all.
444 if(DEFINED ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
445 set(_pkgconfig_allow_system_libs_old "$ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS}")
447 unset(_pkgconfig_allow_system_libs_old)
449 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} 1)
450 if(DEFINED ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS})
451 set(_pkgconfig_allow_system_cflags_old "$ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}")
453 unset(_pkgconfig_allow_system_cflags_old)
455 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} 1)
458 macro(_pkg_restore_path_internal)
459 if(NOT _extra_paths STREQUAL "")
460 # Restore the environment variable
461 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
463 if(DEFINED _pkgconfig_allow_system_libs_old)
464 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} "${_pkgconfig_allow_system_libs_old}")
465 unset(_pkgconfig_allow_system_libs_old)
467 unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
469 if(DEFINED _pkgconfig_allow_system_cflags_old)
470 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} "${_pkgconfig_allow_system_cflags_old}")
471 unset(_pkgconfig_allow_system_cflags_old)
473 unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS})
477 unset(_pkgconfig_path_old)
480 # pkg-config returns frameworks in --libs-only-other
481 # they need to be in ${_prefix}_LIBRARIES so "-framework a -framework b" does
482 # not incorrectly be combined to "-framework a b"
483 function(_pkgconfig_extract_frameworks _prefix)
484 set(ldflags "${${_prefix}_LDFLAGS_OTHER}")
485 list(FIND ldflags "-framework" FR_POS)
486 list(LENGTH ldflags LD_LENGTH)
488 # reduce length by 1 as we need "-framework" and the next entry
489 math(EXPR LD_LENGTH "${LD_LENGTH} - 1")
490 while (FR_POS GREATER -1 AND LD_LENGTH GREATER FR_POS)
491 list(REMOVE_AT ldflags ${FR_POS})
492 list(GET ldflags ${FR_POS} HEAD)
493 list(REMOVE_AT ldflags ${FR_POS})
494 math(EXPR LD_LENGTH "${LD_LENGTH} - 2")
496 list(APPEND LIBS "-framework ${HEAD}")
498 list(FIND ldflags "-framework" FR_POS)
500 set(${_prefix}_LIBRARIES ${${_prefix}_LIBRARIES} ${LIBS} PARENT_SCOPE)
501 set(${_prefix}_LDFLAGS_OTHER "${ldflags}" PARENT_SCOPE)
504 # pkg-config returns -isystem include directories in --cflags-only-other,
505 # depending on the version and if there is a space between -isystem and
507 function(_pkgconfig_extract_isystem _prefix)
508 set(cflags "${${_prefix}_CFLAGS_OTHER}")
510 set(incdirs "${${_prefix}_INCLUDE_DIRS}")
512 set(next_is_isystem FALSE)
513 foreach (THING IN LISTS cflags)
514 # This may filter "-isystem -isystem". That would not work anyway,
516 if (THING STREQUAL "-isystem")
517 set(next_is_isystem TRUE)
521 set(next_is_isystem FALSE)
522 list(APPEND incdirs "${THING}")
523 elseif (THING MATCHES "^-isystem")
524 string(SUBSTRING "${THING}" 8 -1 THING)
525 list(APPEND incdirs "${THING}")
527 list(APPEND outflags "${THING}")
530 set(${_prefix}_CFLAGS_OTHER "${outflags}" PARENT_SCOPE)
531 set(${_prefix}_INCLUDE_DIRS "${incdirs}" PARENT_SCOPE)
535 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global _prefix)
536 _pkgconfig_unset(${_prefix}_FOUND)
537 _pkgconfig_unset(${_prefix}_VERSION)
538 _pkgconfig_unset(${_prefix}_PREFIX)
539 _pkgconfig_unset(${_prefix}_INCLUDEDIR)
540 _pkgconfig_unset(${_prefix}_LIBDIR)
541 _pkgconfig_unset(${_prefix}_MODULE_NAME)
542 _pkgconfig_unset(${_prefix}_LIBS)
543 _pkgconfig_unset(${_prefix}_LIBS_L)
544 _pkgconfig_unset(${_prefix}_LIBS_PATHS)
545 _pkgconfig_unset(${_prefix}_LIBS_OTHER)
546 _pkgconfig_unset(${_prefix}_CFLAGS)
547 _pkgconfig_unset(${_prefix}_CFLAGS_I)
548 _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
549 _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
550 _pkgconfig_unset(${_prefix}_STATIC_LIBS)
551 _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
552 _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
553 _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
554 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
555 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
556 _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
558 # create a better addressable variable of the modules and calculate its size
559 set(_pkg_check_modules_list ${ARGN})
560 list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
562 if(PKG_CONFIG_EXECUTABLE)
563 # give out status message telling checked module
564 if (NOT ${_is_silent})
565 if (_pkg_check_modules_cnt EQUAL 1)
566 message(STATUS "Checking for module '${_pkg_check_modules_list}'")
568 message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
572 set(_pkg_check_modules_packages)
573 set(_pkg_check_modules_failed "")
575 _pkg_set_path_internal()
577 # iterate through module list and check whether they exist and match the required version
578 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
579 set(_pkg_check_modules_exist_query)
581 # check whether version is given while ignoring whitespace
582 if (_pkg_check_modules_pkg MATCHES "(.*[^>< \t])[ \t]*(=|[><]=?)[ \t]*(.*)")
583 set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
584 set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
585 set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
587 set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
588 set(_pkg_check_modules_pkg_op)
589 set(_pkg_check_modules_pkg_ver)
592 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
593 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
594 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
595 _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
597 list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
599 # create the final query which is of the format:
600 # * <pkg-name> > <version>
601 # * <pkg-name> >= <version>
602 # * <pkg-name> = <version>
603 # * <pkg-name> <= <version>
604 # * <pkg-name> < <version>
605 # * --exists <pkg-name>
606 list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
607 if (_pkg_check_modules_pkg_op)
608 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
610 list(APPEND _pkg_check_modules_exist_query --exists)
611 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
616 COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${_pkg_check_modules_exist_query}
617 RESULT_VARIABLE _pkgconfig_retval
618 ERROR_VARIABLE _pkgconfig_error
619 ERROR_STRIP_TRAILING_WHITESPACE)
621 # evaluate result and tell failures
622 if (_pkgconfig_retval)
623 if(NOT ${_is_silent})
624 message(STATUS " ${_pkgconfig_error}")
627 string(APPEND _pkg_check_modules_failed " - ${_pkg_check_modules_pkg}\n")
631 if(_pkg_check_modules_failed)
632 # fail when requested
634 message(FATAL_ERROR "The following required packages were not found:\n${_pkg_check_modules_failed}")
637 # when we are here, we checked whether requested modules
638 # exist. Now, go through them and set variables
640 _pkgconfig_set(${_prefix}_FOUND 1)
641 list(LENGTH _pkg_check_modules_packages pkg_count)
643 # iterate through all modules again and set individual variables
644 foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
645 # handle case when there is only one package required
646 if (pkg_count EQUAL 1)
647 set(_pkg_check_prefix "${_prefix}")
649 set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
652 _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
653 pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
654 pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
655 pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
656 foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
657 _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
659 _pkgconfig_set("${_pkg_check_prefix}_MODULE_NAME" "${_pkg_check_modules_pkg}")
661 if (NOT ${_is_silent})
662 message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
666 # set variables which are combined for multiple modules
667 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
668 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
669 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
670 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
672 if (APPLE AND "-framework" IN_LIST ${_prefix}_LDFLAGS_OTHER)
673 _pkgconfig_extract_frameworks("${_prefix}")
674 # Using _pkgconfig_set in this scope so that a future policy can switch to normal variables
675 _pkgconfig_set("${_pkg_check_prefix}_LIBRARIES" "${${_pkg_check_prefix}_LIBRARIES}")
676 _pkgconfig_set("${_pkg_check_prefix}_LDFLAGS_OTHER" "${${_pkg_check_prefix}_LDFLAGS_OTHER}")
679 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )(-I|-isystem ?)" --cflags-only-I )
680 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
681 _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
683 if (${_prefix}_CFLAGS_OTHER MATCHES "-isystem")
684 _pkgconfig_extract_isystem("${_prefix}")
685 # Using _pkgconfig_set in this scope so that a future policy can switch to normal variables
686 _pkgconfig_set("${_pkg_check_prefix}_CFLAGS_OTHER" "${${_pkg_check_prefix}_CFLAGS_OTHER}")
687 _pkgconfig_set("${_pkg_check_prefix}_INCLUDE_DIRS" "${${_pkg_check_prefix}_INCLUDE_DIRS}")
690 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
693 _pkg_restore_path_internal()
696 message(SEND_ERROR "pkg-config tool not found")
702 #[========================================[.rst:
703 .. command:: pkg_check_modules
705 Checks for all the given modules, setting a variety of result variables in
708 .. code-block:: cmake
710 pkg_check_modules(<prefix>
713 [NO_CMAKE_ENVIRONMENT_PATH]
714 [IMPORTED_TARGET [GLOBAL]]
715 <moduleSpec> [<moduleSpec>...])
717 When the ``REQUIRED`` argument is given, the command will fail with an error
718 if module(s) could not be found.
720 When the ``QUIET`` argument is given, no status messages will be printed.
722 .. versionadded:: 3.1
723 The :variable:`CMAKE_PREFIX_PATH`,
724 :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
725 and environment variables will be added to the ``pkg-config`` search path.
726 The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
727 disable this behavior for the cache variables and environment variables
729 The :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` variable set to ``FALSE``
730 disables this behavior globally.
732 .. This didn't actually work until 3.3.
734 .. versionadded:: 3.6
735 The ``IMPORTED_TARGET`` argument will create an imported target named
736 ``PkgConfig::<prefix>`` that can be passed directly as an argument to
737 :command:`target_link_libraries`.
739 .. This didn't actually work until 3.7.
741 .. versionadded:: 3.13
742 The ``GLOBAL`` argument will make the
743 imported target available in global scope.
745 .. versionadded:: 3.15
746 Non-library linker options reported by ``pkg-config`` are stored in the
747 :prop_tgt:`INTERFACE_LINK_OPTIONS` target property.
749 .. versionchanged:: 3.18
750 Include directories specified with ``-isystem`` are stored in the
751 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` target property. Previous
752 versions of CMake left them in the :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
755 Each ``<moduleSpec>`` can be either a bare module name or it can be a
756 module name with a version constraint (operators ``=``, ``<``, ``>``,
757 ``<=`` and ``>=`` are supported). The following are examples for a module
758 named ``foo`` with various constraints:
760 - ``foo`` matches any version.
761 - ``foo<2`` only matches versions before 2.
762 - ``foo>=3.1`` matches any version from 3.1 or later.
763 - ``foo=1.2.3`` requires that foo must be exactly version 1.2.3.
765 The following variables may be set upon return. Two sets of values exist:
766 One for the common case (``<XXX> = <prefix>``) and another for the
767 information ``pkg-config`` provides when called with the ``--static``
768 option (``<XXX> = <prefix>_STATIC``).
771 set to 1 if module(s) exist
773 only the libraries (without the '-l')
774 ``<XXX>_LINK_LIBRARIES``
775 the libraries and their absolute paths
776 ``<XXX>_LIBRARY_DIRS``
777 the paths of the libraries (without the '-L')
779 all required linker flags
780 ``<XXX>_LDFLAGS_OTHER``
781 all other linker flags
782 ``<XXX>_INCLUDE_DIRS``
783 the '-I' preprocessor flags (without the '-I')
786 ``<XXX>_CFLAGS_OTHER``
787 the other compiler flags
789 All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
790 associated variable returned from ``pkg-config`` has multiple values.
792 .. versionchanged:: 3.18
793 Include directories specified with ``-isystem`` are stored in the
794 ``<XXX>_INCLUDE_DIRS`` variable. Previous versions of CMake left them
795 in ``<XXX>_CFLAGS_OTHER``.
797 There are some special variables whose prefix depends on the number of
798 ``<moduleSpec>`` given. When there is only one ``<moduleSpec>``,
799 ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
800 items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``.
803 version of the module
805 prefix directory of the module
807 include directory of the module
809 lib directory of the module
811 .. versionchanged:: 3.8
812 For any given ``<prefix>``, ``pkg_check_modules()`` can be called multiple
813 times with different parameters. Previous versions of CMake cached and
814 returned the first successful result.
816 .. versionchanged:: 3.16
817 If a full path to the found library can't be determined, but it's still
818 visible to the linker, pass it through as ``-l<name>``. Previous versions
819 of CMake failed in this case.
823 .. code-block:: cmake
825 pkg_check_modules (GLIB2 glib-2.0)
827 Looks for any version of glib2. If found, the output variable
828 ``GLIB2_VERSION`` will hold the actual version found.
830 .. code-block:: cmake
832 pkg_check_modules (GLIB2 glib-2.0>=2.10)
834 Looks for at least version 2.10 of glib2. If found, the output variable
835 ``GLIB2_VERSION`` will hold the actual version found.
837 .. code-block:: cmake
839 pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
841 Looks for both glib2-2.0 (at least version 2.10) and any version of
842 gtk2+-2.0. Only if both are found will ``FOO`` be considered found.
843 The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
844 set to their respective found module versions.
846 .. code-block:: cmake
848 pkg_check_modules (XRENDER REQUIRED xrender)
850 Requires any version of ``xrender``. Example output variables set by a
853 XRENDER_LIBRARIES=Xrender;X11
854 XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
855 #]========================================]
856 macro(pkg_check_modules _prefix _module0)
857 _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})
859 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
860 (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
861 ( "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
862 _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})
864 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
865 if (${_prefix}_FOUND)
866 _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
869 if (${_prefix}_FOUND)
870 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
876 #[========================================[.rst:
877 .. command:: pkg_search_module
879 The behavior of this command is the same as :command:`pkg_check_modules`,
880 except that rather than checking for all the specified modules, it searches
881 for just the first successful match.
883 .. code-block:: cmake
885 pkg_search_module(<prefix>
888 [NO_CMAKE_ENVIRONMENT_PATH]
889 [IMPORTED_TARGET [GLOBAL]]
890 <moduleSpec> [<moduleSpec>...])
892 .. versionadded:: 3.16
893 If a module is found, the ``<prefix>_MODULE_NAME`` variable will contain the
894 name of the matching module. This variable can be used if you need to run
895 :command:`pkg_get_variable`.
899 .. code-block:: cmake
901 pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
902 #]========================================]
903 macro(pkg_search_module _prefix _module0)
904 _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})
906 if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
907 set(_pkg_modules_found 0)
909 if (NOT ${_pkg_is_silent})
910 message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
913 # iterate through all modules and stop at the first working one.
914 foreach(_pkg_alt ${_pkg_modules_alt})
915 if(NOT _pkg_modules_found)
916 _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global} "${_prefix}" "${_pkg_alt}")
919 if (${_prefix}_FOUND)
920 set(_pkg_modules_found 1)
925 if (NOT ${_prefix}_FOUND)
926 if(${_pkg_is_required})
927 message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
931 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
932 elseif (${_prefix}_FOUND)
933 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
937 #[========================================[.rst:
938 .. command:: pkg_get_variable
940 .. versionadded:: 3.4
942 Retrieves the value of a pkg-config variable ``varName`` and stores it in the
943 result variable ``resultVar`` in the calling scope.
945 .. code-block:: cmake
947 pkg_get_variable(<resultVar> <moduleName> <varName>
948 [DEFINE_VARIABLES <key>=<value>...])
950 If ``pkg-config`` returns multiple values for the specified variable,
951 ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
955 ``DEFINE_VARIABLES <key>=<value>...``
956 .. versionadded:: 3.28
958 Specify key-value pairs to redefine variables affecting the variable
959 retrieved with ``pkg-config``.
963 .. code-block:: cmake
965 pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
966 #]========================================]
967 function (pkg_get_variable result pkg variable)
968 set(_multiValueArgs DEFINE_VARIABLES)
970 CMAKE_PARSE_ARGUMENTS(_parsedArguments "" "" "${_multiValueArgs}" ${ARGN})
971 set(defined_variables )
972 foreach(_def_var ${_parsedArguments_DEFINE_VARIABLES})
973 if(NOT _def_var MATCHES "^.+=.*$")
974 message(FATAL_ERROR "DEFINE_VARIABLES should contain arguments in the form of key=value")
977 list(APPEND defined_variables "--define-variable=${_def_var}")
980 _pkg_set_path_internal()
981 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}" ${defined_variables})
985 _pkg_restore_path_internal()
989 #[========================================[.rst:
990 Variables Affecting Behavior
991 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
993 .. variable:: PKG_CONFIG_EXECUTABLE
995 This cache variable can be set to the path of the pkg-config executable.
996 :command:`find_program` is called internally by the module with this
999 .. versionadded:: 3.1
1000 The ``PKG_CONFIG`` environment variable can be used as a hint if
1001 ``PKG_CONFIG_EXECUTABLE`` has not yet been set.
1003 .. versionchanged:: 3.22
1004 If the ``PKG_CONFIG`` environment variable is set, only the first
1005 argument is taken from it when using it as a hint.
1007 .. variable:: PKG_CONFIG_ARGN
1009 .. versionadded:: 3.22
1011 This cache variable can be set to a list of arguments to additionally pass
1012 to pkg-config if needed. If not provided, it will be initialized from the
1013 ``PKG_CONFIG`` environment variable, if set. The first argument in that
1014 environment variable is assumed to be the pkg-config program, while all
1015 remaining arguments after that are used to initialize ``PKG_CONFIG_ARGN``.
1016 If no such environment variable is defined, ``PKG_CONFIG_ARGN`` is
1017 initialized to an empty string. The module does not update the variable once
1018 it has been set in the cache.
1020 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
1022 .. versionadded:: 3.1
1024 Specifies whether :command:`pkg_check_modules` and
1025 :command:`pkg_search_module` should add the paths in the
1026 :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
1027 :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
1028 ``pkg-config`` search path.
1030 If this variable is not set, this behavior is enabled by default if
1031 :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
1033 #]========================================]
1036 ### Local Variables: