1# Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2# file Copyright.txt or https://cmake.org/licensing for details. 3 4#[========================================[.rst: 5FindPkgConfig 6------------- 7 8A ``pkg-config`` module for CMake. 9 10Finds the ``pkg-config`` executable and adds the :command:`pkg_get_variable`, 11:command:`pkg_check_modules` and :command:`pkg_search_module` commands. The 12following variables will also be set: 13 14``PKG_CONFIG_FOUND`` 15 True if a pkg-config executable was found. 16 17``PKG_CONFIG_VERSION_STRING`` 18 .. versionadded:: 2.8.8 19 20 The version of pkg-config that was found. 21 22``PKG_CONFIG_EXECUTABLE`` 23 The pathname of the pkg-config program. 24 25``PKG_CONFIG_ARGN`` 26 .. versionadded:: 3.22 27 28 A list of arguments to pass to pkg-config. 29 30Both ``PKG_CONFIG_EXECUTABLE`` and ``PKG_CONFIG_ARGN`` are initialized by the 31module, but may be overridden by the user. See `Variables Affecting Behavior`_ 32for how these variables are initialized. 33 34#]========================================] 35 36cmake_policy(PUSH) 37cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced 38cmake_policy(SET CMP0057 NEW) # if IN_LIST 39 40### Common stuff #### 41set(PKG_CONFIG_VERSION 1) 42 43# find pkg-config, use PKG_CONFIG if set 44if((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) 51 endif() 52 set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_FROM_ENV_ARGV0}" CACHE FILEPATH "pkg-config executable") 53 endif() 54endif() 55 56set(PKG_CONFIG_NAMES "pkg-config") 57if(CMAKE_HOST_WIN32) 58 list(PREPEND PKG_CONFIG_NAMES "pkg-config.bat") 59endif() 60list(APPEND PKG_CONFIG_NAMES "pkgconf") 61 62find_program(PKG_CONFIG_EXECUTABLE 63 NAMES ${PKG_CONFIG_NAMES} 64 NAMES_PER_DIR 65 DOC "pkg-config executable") 66mark_as_advanced(PKG_CONFIG_EXECUTABLE) 67 68set(PKG_CONFIG_ARGN "${PKG_CONFIG_ARGN}" CACHE STRING "Arguments to supply to pkg-config") 69mark_as_advanced(PKG_CONFIG_ARGN) 70 71set(_PKG_CONFIG_FAILURE_MESSAGE "") 72if (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 77 ) 78 79 if (NOT _PKG_CONFIG_VERSION_RESULT EQUAL 0) 80 string(REPLACE "\n" "\n " _PKG_CONFIG_VERSION_ERROR " ${_PKG_CONFIG_VERSION_ERROR}") 81 if(PKG_CONFIG_ARGN) 82 string(REPLACE ";" " " PKG_CONFIG_ARGN " ${PKG_CONFIG_ARGN}") 83 endif() 84 string(APPEND _PKG_CONFIG_FAILURE_MESSAGE 85 "The command\n" 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}" 90 ) 91 set(PKG_CONFIG_EXECUTABLE "") 92 set(PKG_CONFIG_ARGN "") 93 unset(PKG_CONFIG_VERSION_STRING) 94 endif () 95 unset(_PKG_CONFIG_VERSION_RESULT) 96endif () 97 98include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) 99find_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) 103 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 106# handled by FPHSA. 107set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}") 108 109# Unsets the given variables 110macro(_pkgconfig_unset var) 111 # Clear normal variable (possibly set by project code). 112 unset(${var}) 113 # Store as cache variable. 114 # FIXME: Add a policy to switch to a normal variable. 115 set(${var} "" CACHE INTERNAL "") 116endmacro() 117 118macro(_pkgconfig_set var value) 119 # Clear normal variable (possibly set by project code). 120 unset(${var}) 121 # Store as cache variable. 122 # FIXME: Add a policy to switch to a normal variable. 123 set(${var} ${value} CACHE INTERNAL "") 124endmacro() 125 126# Invokes pkgconfig, cleans up the result and sets variables 127macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp) 128 set(_pkgconfig_invoke_result) 129 130 execute_process( 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) 135 136 if (_pkgconfig_failed) 137 set(_pkgconfig_${_varname} "") 138 _pkgconfig_unset(${_prefix}_${_varname}) 139 else() 140 string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}") 141 142 if (NOT ${_regexp} STREQUAL "") 143 string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}") 144 endif() 145 146 separate_arguments(_pkgconfig_invoke_result) 147 148 #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}") 149 set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result}) 150 _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}") 151 endif() 152endmacro() 153 154# Internal version of pkg_get_variable; expects PKG_CONFIG_PATH to already be set 155function (_pkg_get_variable result pkg variable) 156 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}") 157 set("${result}" 158 "${prefix_result}" 159 PARENT_SCOPE) 160endfunction () 161 162# Invokes pkgconfig two times; once without '--static' and once with 163# '--static' 164macro(_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}) 167endmacro() 168 169# Splits given arguments into options and a package list 170macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global) 171 set(${_is_req} 0) 172 set(${_is_silent} 0) 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) 181 endif() 182 elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1) 183 set(${_no_cmake_path} 1) 184 set(${_no_cmake_environment_path} 1) 185 endif() 186 187 foreach(_pkg ${ARGN}) 188 if (_pkg STREQUAL "REQUIRED") 189 set(${_is_req} 1) 190 endif () 191 if (_pkg STREQUAL "QUIET") 192 set(${_is_silent} 1) 193 endif () 194 if (_pkg STREQUAL "NO_CMAKE_PATH") 195 set(${_no_cmake_path} 1) 196 endif() 197 if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH") 198 set(${_no_cmake_environment_path} 1) 199 endif() 200 if (_pkg STREQUAL "IMPORTED_TARGET") 201 set(${_imp_target} 1) 202 endif() 203 if (_pkg STREQUAL "GLOBAL") 204 set(${_imp_target_global} 1) 205 endif() 206 endforeach() 207 208 if (${_imp_target_global} AND NOT ${_imp_target}) 209 message(SEND_ERROR "the argument GLOBAL may only be used together with IMPORTED_TARGET") 210 endif() 211 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") 219endmacro() 220 221# Add the content of a variable or an environment variable to a list of 222# paths 223# Usage: 224# - _pkgconfig_add_extra_path(_extra_paths VAR) 225# - _pkgconfig_add_extra_path(_extra_paths ENV VAR) 226function(_pkgconfig_add_extra_path _extra_paths_var _var) 227 set(_is_env 0) 228 if(ARGC GREATER 2 AND _var STREQUAL "ENV") 229 set(_var ${ARGV2}) 230 set(_is_env 1) 231 endif() 232 if(NOT _is_env) 233 if(NOT "${${_var}}" STREQUAL "") 234 list(APPEND ${_extra_paths_var} ${${_var}}) 235 endif() 236 else() 237 if(NOT "$ENV{${_var}}" STREQUAL "") 238 file(TO_CMAKE_PATH "$ENV{${_var}}" _path) 239 list(APPEND ${_extra_paths_var} ${_path}) 240 unset(_path) 241 endif() 242 endif() 243 set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE) 244endfunction() 245 246# scan the LDFLAGS returned by pkg-config for library directories and 247# libraries, figure out the absolute paths of that libraries in the 248# given directories 249function(_pkg_find_libs _prefix _no_cmake_path _no_cmake_environment_path) 250 unset(_libs) 251 unset(_find_opts) 252 253 # set the options that are used as long as the .pc file does not provide a library 254 # path to look into 255 if(_no_cmake_path) 256 list(APPEND _find_opts "NO_CMAKE_PATH") 257 endif() 258 if(_no_cmake_environment_path) 259 list(APPEND _find_opts "NO_CMAKE_ENVIRONMENT_PATH") 260 endif() 261 262 unset(_search_paths) 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) 268 continue() 269 endif () 270 if (flag MATCHES "^-L(.*)") 271 list(APPEND _search_paths ${CMAKE_MATCH_1}) 272 continue() 273 endif() 274 if (flag MATCHES "^-l(.*)") 275 set(_pkg_search "${CMAKE_MATCH_1}") 276 else() 277 if (flag STREQUAL "-framework") 278 set(_next_is_framework TRUE) 279 endif () 280 continue() 281 endif() 282 283 if(_search_paths) 284 # Firstly search in -L paths 285 find_library(pkgcfg_lib_${_prefix}_${_pkg_search} 286 NAMES ${_pkg_search} 287 HINTS ${_search_paths} NO_DEFAULT_PATH) 288 endif() 289 find_library(pkgcfg_lib_${_prefix}_${_pkg_search} 290 NAMES ${_pkg_search} 291 ${_find_opts}) 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}}") 295 else() 296 list(APPEND _libs ${_pkg_search}) 297 endif() 298 endforeach() 299 300 set(${_prefix}_LINK_LIBRARIES "${_libs}" PARENT_SCOPE) 301endfunction() 302 303# create an imported target from all the information returned by pkg-config 304function(_pkg_create_imp_target _prefix _imp_target_global) 305 if (NOT TARGET PkgConfig::${_prefix}) 306 if(${_imp_target_global}) 307 set(_global_opt "GLOBAL") 308 else() 309 unset(_global_opt) 310 endif() 311 add_library(PkgConfig::${_prefix} INTERFACE IMPORTED ${_global_opt}) 312 313 if(${_prefix}_INCLUDE_DIRS) 314 set_property(TARGET PkgConfig::${_prefix} PROPERTY 315 INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}") 316 endif() 317 if(${_prefix}_LINK_LIBRARIES) 318 set_property(TARGET PkgConfig::${_prefix} PROPERTY 319 INTERFACE_LINK_LIBRARIES "${${_prefix}_LINK_LIBRARIES}") 320 endif() 321 if(${_prefix}_LDFLAGS_OTHER) 322 set_property(TARGET PkgConfig::${_prefix} PROPERTY 323 INTERFACE_LINK_OPTIONS "${${_prefix}_LDFLAGS_OTHER}") 324 endif() 325 if(${_prefix}_CFLAGS_OTHER) 326 set_property(TARGET PkgConfig::${_prefix} PROPERTY 327 INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}") 328 endif() 329 endif() 330endfunction() 331 332# recalculate the dynamic output 333# this is a macro and not a function so the result of _pkg_find_libs is automatically propagated 334macro(_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}) 336 if(${_imp_target}) 337 _pkg_create_imp_target(${_prefix} ${_imp_target_global}) 338 endif() 339endmacro() 340 341### 342macro(_pkg_set_path_internal) 343 set(_extra_paths) 344 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) 349 endif() 350 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) 355 endif() 356 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) 364 endif() 365 366 # Create a list of the possible pkgconfig subfolder (depending on 367 # the system 368 set(_lib_dirs) 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") 375 endif() 376 else() 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") 381 endif() 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") 385 endif() 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") 389 endif() 390 endif() 391 endif() 392 if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING) 393 list(APPEND _lib_dirs "libdata/pkgconfig") 394 endif() 395 list(APPEND _lib_dirs "lib/pkgconfig") 396 list(APPEND _lib_dirs "share/pkgconfig") 397 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) 405 endif() 406 endforeach() 407 endforeach() 408 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) 414 if(CMAKE_HOST_UNIX) 415 string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}") 416 string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}") 417 endif() 418 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}") 419 endif() 420 421 # Unset variables 422 unset(_lib_dirs) 423 unset(_pkgconfig_path) 424 endif() 425 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}") 429 else() 430 unset(_pkgconfig_allow_system_libs_old) 431 endif() 432 set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} 0) 433endmacro() 434 435macro(_pkg_restore_path_internal) 436 if(NOT _extra_paths STREQUAL "") 437 # Restore the environment variable 438 set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}") 439 endif() 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) 443 endif() 444 445 unset(_extra_paths) 446 unset(_pkgconfig_path_old) 447endmacro() 448 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" 452function(_pkgconfig_extract_frameworks _prefix) 453 set(ldflags "${${_prefix}_LDFLAGS_OTHER}") 454 list(FIND ldflags "-framework" FR_POS) 455 list(LENGTH ldflags LD_LENGTH) 456 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") 464 465 list(APPEND LIBS "-framework ${HEAD}") 466 467 list(FIND ldflags "-framework" FR_POS) 468 endwhile () 469 set(${_prefix}_LIBRARIES ${${_prefix}_LIBRARIES} ${LIBS} PARENT_SCOPE) 470 set(${_prefix}_LDFLAGS_OTHER "${ldflags}" PARENT_SCOPE) 471endfunction() 472 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 475# the actual path 476function(_pkgconfig_extract_isystem _prefix) 477 set(cflags "${${_prefix}_CFLAGS_OTHER}") 478 set(outflags "") 479 set(incdirs "${${_prefix}_INCLUDE_DIRS}") 480 481 set(next_is_isystem FALSE) 482 foreach (THING IN LISTS cflags) 483 # This may filter "-isystem -isystem". That would not work anyway, 484 # so let it happen. 485 if (THING STREQUAL "-isystem") 486 set(next_is_isystem TRUE) 487 continue() 488 endif () 489 if (next_is_isystem) 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}") 495 else () 496 list(APPEND outflags "${THING}") 497 endif () 498 endforeach () 499 set(${_prefix}_CFLAGS_OTHER "${outflags}" PARENT_SCOPE) 500 set(${_prefix}_INCLUDE_DIRS "${incdirs}" PARENT_SCOPE) 501endfunction() 502 503### 504macro(_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) 526 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) 530 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}'") 536 else() 537 message(STATUS "Checking for modules '${_pkg_check_modules_list}'") 538 endif() 539 endif() 540 541 set(_pkg_check_modules_packages) 542 set(_pkg_check_modules_failed) 543 544 _pkg_set_path_internal() 545 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) 549 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}") 555 else() 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) 559 endif() 560 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) 565 566 list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}") 567 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}") 578 else() 579 list(APPEND _pkg_check_modules_exist_query --exists) 580 list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}") 581 endif() 582 583 # execute the query 584 execute_process( 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) 589 590 # evaluate result and tell failures 591 if (_pkgconfig_retval) 592 if(NOT ${_is_silent}) 593 message(STATUS " ${_pkgconfig_error}") 594 endif() 595 596 set(_pkg_check_modules_failed 1) 597 endif() 598 endforeach() 599 600 if(_pkg_check_modules_failed) 601 # fail when requested 602 if (${_is_required}) 603 message(FATAL_ERROR "A required package was not found") 604 endif () 605 else() 606 # when we are here, we checked whether requested modules 607 # exist. Now, go through them and set variables 608 609 _pkgconfig_set(${_prefix}_FOUND 1) 610 list(LENGTH _pkg_check_modules_packages pkg_count) 611 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}") 617 else() 618 set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}") 619 endif() 620 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}}") 627 endforeach () 628 _pkgconfig_set("${_pkg_check_prefix}_MODULE_NAME" "${_pkg_check_modules_pkg}") 629 630 if (NOT ${_is_silent}) 631 message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}") 632 endif () 633 endforeach() 634 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 ) 640 641 if (APPLE AND "-framework" IN_LIST ${_prefix}_LDFLAGS_OTHER) 642 _pkgconfig_extract_frameworks("${_prefix}") 643 endif() 644 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 ) 648 649 if (${_prefix}_CFLAGS_OTHER MATCHES "-isystem") 650 _pkgconfig_extract_isystem("${_prefix}") 651 endif () 652 653 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global}) 654 endif() 655 656 _pkg_restore_path_internal() 657 else() 658 if (${_is_required}) 659 message(SEND_ERROR "pkg-config tool not found") 660 endif () 661 endif() 662endmacro() 663 664 665#[========================================[.rst: 666.. command:: pkg_check_modules 667 668 Checks for all the given modules, setting a variety of result variables in 669 the calling scope. 670 671 .. code-block:: cmake 672 673 pkg_check_modules(<prefix> 674 [REQUIRED] [QUIET] 675 [NO_CMAKE_PATH] 676 [NO_CMAKE_ENVIRONMENT_PATH] 677 [IMPORTED_TARGET [GLOBAL]] 678 <moduleSpec> [<moduleSpec>...]) 679 680 When the ``REQUIRED`` argument is given, the command will fail with an error 681 if module(s) could not be found. 682 683 When the ``QUIET`` argument is given, no status messages will be printed. 684 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 691 respectively. 692 The :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` variable set to ``FALSE`` 693 disables this behavior globally. 694 695 .. This didn't actually work until 3.3. 696 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`. 701 702 .. This didn't actually work until 3.7. 703 704 .. versionadded:: 3.13 705 The ``GLOBAL`` argument will make the 706 imported target available in global scope. 707 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. 711 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` 716 property. 717 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: 722 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. 727 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``). 732 733 ``<XXX>_FOUND`` 734 set to 1 if module(s) exist 735 ``<XXX>_LIBRARIES`` 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') 741 ``<XXX>_LDFLAGS`` 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') 747 ``<XXX>_CFLAGS`` 748 all required cflags 749 ``<XXX>_CFLAGS_OTHER`` 750 the other compiler flags 751 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. 754 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``. 759 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>``. 764 765 ``<YYY>_VERSION`` 766 version of the module 767 ``<YYY>_PREFIX`` 768 prefix directory of the module 769 ``<YYY>_INCLUDEDIR`` 770 include directory of the module 771 ``<YYY>_LIBDIR`` 772 lib directory of the module 773 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. 778 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. 783 784 Examples: 785 786 .. code-block:: cmake 787 788 pkg_check_modules (GLIB2 glib-2.0) 789 790 Looks for any version of glib2. If found, the output variable 791 ``GLIB2_VERSION`` will hold the actual version found. 792 793 .. code-block:: cmake 794 795 pkg_check_modules (GLIB2 glib-2.0>=2.10) 796 797 Looks for at least version 2.10 of glib2. If found, the output variable 798 ``GLIB2_VERSION`` will hold the actual version found. 799 800 .. code-block:: cmake 801 802 pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0) 803 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. 808 809 .. code-block:: cmake 810 811 pkg_check_modules (XRENDER REQUIRED xrender) 812 813 Requires any version of ``xrender``. Example output variables set by a 814 successful call:: 815 816 XRENDER_LIBRARIES=Xrender;X11 817 XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp 818#]========================================] 819macro(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}) 821 # check cached value 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}) 826 827 _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION}) 828 if (${_prefix}_FOUND) 829 _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}") 830 endif() 831 else() 832 if (${_prefix}_FOUND) 833 _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global}) 834 endif() 835 endif() 836endmacro() 837 838 839#[========================================[.rst: 840.. command:: pkg_search_module 841 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. 845 846 .. code-block:: cmake 847 848 pkg_search_module(<prefix> 849 [REQUIRED] [QUIET] 850 [NO_CMAKE_PATH] 851 [NO_CMAKE_ENVIRONMENT_PATH] 852 [IMPORTED_TARGET [GLOBAL]] 853 <moduleSpec> [<moduleSpec>...]) 854 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`. 859 860 Example: 861 862 .. code-block:: cmake 863 864 pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2) 865#]========================================] 866macro(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}) 868 # check cached value 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) 871 872 if (NOT ${_pkg_is_silent}) 873 message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'") 874 endif () 875 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}") 880 endif() 881 882 if (${_prefix}_FOUND) 883 set(_pkg_modules_found 1) 884 break() 885 endif() 886 endforeach() 887 888 if (NOT ${_prefix}_FOUND) 889 if(${_pkg_is_required}) 890 message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found") 891 endif() 892 endif() 893 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}) 897 endif() 898endmacro() 899 900#[========================================[.rst: 901.. command:: pkg_get_variable 902 903 .. versionadded:: 3.4 904 905 Retrieves the value of a pkg-config variable ``varName`` and stores it in the 906 result variable ``resultVar`` in the calling scope. 907 908 .. code-block:: cmake 909 910 pkg_get_variable(<resultVar> <moduleName> <varName>) 911 912 If ``pkg-config`` returns multiple values for the specified variable, 913 ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`. 914 915 For example: 916 917 .. code-block:: cmake 918 919 pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir) 920#]========================================] 921function (pkg_get_variable result pkg variable) 922 _pkg_set_path_internal() 923 _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}") 924 set("${result}" 925 "${prefix_result}" 926 PARENT_SCOPE) 927 _pkg_restore_path_internal() 928endfunction () 929 930 931#[========================================[.rst: 932Variables Affecting Behavior 933^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 934 935.. variable:: PKG_CONFIG_EXECUTABLE 936 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 939 variable. 940 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. 944 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. 948 949.. variable:: PKG_CONFIG_ARGN 950 951 .. versionadded:: 3.22 952 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. 961 962.. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH 963 964 .. versionadded:: 3.1 965 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. 971 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 974 otherwise. 975#]========================================] 976 977 978### Local Variables: 979### mode: cmake 980### End: 981 982cmake_policy(POP) 983