FindPkgConfig.cmake 21.6 KB
Newer Older
1 2 3 4
#.rst:
# FindPkgConfig
# -------------
#
5
# A `pkg-config` module for CMake.
6
#
7 8 9
# Finds the ``pkg-config`` executable and add the
# :command:`pkg_check_modules` and :command:`pkg_search_module`
# commands.
10
#
11 12 13
# In order to find the ``pkg-config`` executable, it uses the
# :variable:`PKG_CONFIG_EXECUTABLE` variable or the ``PKG_CONFIG``
# environment variable first.
14

15
#=============================================================================
16 17 18
# Copyright 2006-2014 Kitware, Inc.
# Copyright 2014      Christoph Grüninger <foss@grueninger.de>
# Copyright 2006      Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
19 20 21 22 23 24 25 26
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
27
# (To distribute this file outside of CMake, substitute the full
28
#  License text for the above reference.)
29 30 31 32

### Common stuff ####
set(PKG_CONFIG_VERSION 1)

33 34 35 36
# find pkg-config, use PKG_CONFIG if set
if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
  set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable")
endif()
37 38 39
find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
mark_as_advanced(PKG_CONFIG_EXECUTABLE)

40 41 42 43 44
if (PKG_CONFIG_EXECUTABLE)
  execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version
    OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING
    ERROR_QUIET
    OUTPUT_STRIP_TRAILING_WHITESPACE)
45
endif ()
46

47 48 49 50
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
find_package_handle_standard_args(PkgConfig
                                  REQUIRED_VARS PKG_CONFIG_EXECUTABLE
                                  VERSION_VAR PKG_CONFIG_VERSION_STRING)
51

52 53 54 55 56
# This is needed because the module name is "PkgConfig" but the name of
# this variable has always been PKG_CONFIG_FOUND so this isn't automatically
# handled by FPHSA.
set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")

57 58 59
# Unsets the given variables
macro(_pkgconfig_unset var)
  set(${var} "" CACHE INTERNAL "")
60
endmacro()
61 62 63

macro(_pkgconfig_set var value)
  set(${var} ${value} CACHE INTERNAL "")
64
endmacro()
65 66 67 68 69 70 71 72

# Invokes pkgconfig, cleans up the result and sets variables
macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
  set(_pkgconfig_invoke_result)

  execute_process(
    COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
    OUTPUT_VARIABLE _pkgconfig_invoke_result
73 74
    RESULT_VARIABLE _pkgconfig_failed
    OUTPUT_STRIP_TRAILING_WHITESPACE)
75 76 77 78

  if (_pkgconfig_failed)
    set(_pkgconfig_${_varname} "")
    _pkgconfig_unset(${_prefix}_${_varname})
79
  else()
80 81 82 83
    string(REGEX REPLACE "[\r\n]"                  " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")

    if (NOT ${_regexp} STREQUAL "")
      string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
84
    endif()
85 86 87 88 89 90

    separate_arguments(_pkgconfig_invoke_result)

    #message(STATUS "  ${_varname} ... ${_pkgconfig_invoke_result}")
    set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
    _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
91 92
  endif()
endmacro()
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#[========================================[.rst:
.. command:: pkg_get_variable

  Retrieves the value of a variable from a package::

    pkg_get_variable(<RESULT> <MODULE> <VARIABLE>)

  For example:

  .. code-block:: cmake

    pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
#]========================================]
function (pkg_get_variable result pkg variable)
  _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
  set("${result}"
    "${prefix_result}"
    PARENT_SCOPE)
endfunction ()

114 115 116 117 118
# Invokes pkgconfig two times; once without '--static' and once with
# '--static'
macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
  _pkgconfig_invoke("${_pkglist}" ${_prefix}        ${_varname} "${cleanup_regexp}" ${ARGN})
  _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static  ${ARGN})
119
endmacro()
120 121

# Splits given arguments into options and a package list
122
macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path)
123
  set(${_is_req} 0)
124
  set(${_is_silent} 0)
125 126 127 128 129 130 131
  set(${_no_cmake_path} 0)
  set(${_no_cmake_environment_path} 0)
  if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
    if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
      set(${_no_cmake_path} 1)
      set(${_no_cmake_environment_path} 1)
    endif()
132
  elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
133 134 135
    set(${_no_cmake_path} 1)
    set(${_no_cmake_environment_path} 1)
  endif()
136

137 138 139
  foreach(_pkg ${ARGN})
    if (_pkg STREQUAL "REQUIRED")
      set(${_is_req} 1)
140
    endif ()
141 142
    if (_pkg STREQUAL "QUIET")
      set(${_is_silent} 1)
143
    endif ()
144 145 146 147 148 149
    if (_pkg STREQUAL "NO_CMAKE_PATH")
      set(${_no_cmake_path} 1)
    endif()
    if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
      set(${_no_cmake_environment_path} 1)
    endif()
150
  endforeach()
151 152 153

  set(${_result} ${ARGN})
  list(REMOVE_ITEM ${_result} "REQUIRED")
154
  list(REMOVE_ITEM ${_result} "QUIET")
155 156
  list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
  list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
157
endmacro()
158

159 160 161 162 163 164 165
# Add the content of a variable or an environment variable to a list of
# paths
# Usage:
#  - _pkgconfig_add_extra_path(_extra_paths VAR)
#  - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
function(_pkgconfig_add_extra_path _extra_paths_var _var)
  set(_is_env 0)
166
  if(ARGC GREATER 2 AND _var STREQUAL "ENV")
167 168 169 170 171
    set(_var ${ARGV2})
    set(_is_env 1)
  endif()
  if(NOT _is_env)
    if(NOT "${${_var}}" STREQUAL "")
172
      list(APPEND ${_extra_paths_var} ${${_var}})
173 174 175 176 177 178 179 180 181 182 183
    endif()
  else()
    if(NOT "$ENV{${_var}}" STREQUAL "")
      file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
      list(APPEND ${_extra_paths_var} ${_path})
      unset(_path)
    endif()
  endif()
  set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
endfunction()

184
###
185
macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _prefix)
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  _pkgconfig_unset(${_prefix}_FOUND)
  _pkgconfig_unset(${_prefix}_VERSION)
  _pkgconfig_unset(${_prefix}_PREFIX)
  _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  _pkgconfig_unset(${_prefix}_LIBDIR)
  _pkgconfig_unset(${_prefix}_LIBS)
  _pkgconfig_unset(${_prefix}_LIBS_L)
  _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  _pkgconfig_unset(${_prefix}_CFLAGS)
  _pkgconfig_unset(${_prefix}_CFLAGS_I)
  _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)

  # create a better addressable variable of the modules and calculate its size
  set(_pkg_check_modules_list ${ARGN})
  list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)

  if(PKG_CONFIG_EXECUTABLE)
    # give out status message telling checked module
    if (NOT ${_is_silent})
      if (_pkg_check_modules_cnt EQUAL 1)
215
        message(STATUS "Checking for module '${_pkg_check_modules_list}'")
216
      else()
217
        message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
218 219
      endif()
    endif()
220

221 222 223
    set(_pkg_check_modules_packages)
    set(_pkg_check_modules_failed)

224 225 226
    set(_extra_paths)

    if(NOT _no_cmake_path)
227 228 229
      _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
      _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
      _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
230 231 232
    endif()

    if(NOT _no_cmake_environment_path)
233 234 235
      _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
      _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
      _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    endif()

    if(NOT "${_extra_paths}" STREQUAL "")
      # Save the PKG_CONFIG_PATH environment variable, and add paths
      # from the CMAKE_PREFIX_PATH variables
      set(_pkgconfig_path_old $ENV{PKG_CONFIG_PATH})
      set(_pkgconfig_path ${_pkgconfig_path_old})
      if(NOT "${_pkgconfig_path}" STREQUAL "")
        file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
      endif()

      # Create a list of the possible pkgconfig subfolder (depending on
      # the system
      set(_lib_dirs)
      if(NOT DEFINED CMAKE_SYSTEM_NAME
          OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
              AND NOT CMAKE_CROSSCOMPILING))
        if(EXISTS "/etc/debian_version") # is this a debian system ?
          if(CMAKE_LIBRARY_ARCHITECTURE)
            list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
          endif()
        else()
          # not debian, chech the FIND_LIBRARY_USE_LIB64_PATHS property
          get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
          if(uselib64)
            list(APPEND _lib_dirs "lib64/pkgconfig")
          endif()
        endif()
      endif()
      list(APPEND _lib_dirs "lib/pkgconfig")

      # Check if directories exist and eventually append them to the
      # pkgconfig path list
      foreach(_prefix_dir ${_extra_paths})
        foreach(_lib_dir ${_lib_dirs})
          if(EXISTS "${_prefix_dir}/${_lib_dir}")
            list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
            list(REMOVE_DUPLICATES _pkgconfig_path)
          endif()
        endforeach()
      endforeach()

      # Prepare and set the environment variable
      if(NOT "${_pkgconfig_path}" STREQUAL "")
        # remove empty values from the list
        list(REMOVE_ITEM _pkgconfig_path "")
        file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
        if(UNIX)
          string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
          string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
        endif()
        set(ENV{PKG_CONFIG_PATH} ${_pkgconfig_path})
      endif()

      # Unset variables
      unset(_lib_dirs)
      unset(_pkgconfig_path)
    endif()

295 296 297 298 299
    # iterate through module list and check whether they exist and match the required version
    foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
      set(_pkg_check_modules_exist_query)

      # check whether version is given
300 301 302 303
      if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
        set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
        set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
        set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
304
      else()
305 306 307
        set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
        set(_pkg_check_modules_pkg_op)
        set(_pkg_check_modules_pkg_ver)
308
      endif()
309 310 311 312

      # handle the operands
      if (_pkg_check_modules_pkg_op STREQUAL ">=")
        list(APPEND _pkg_check_modules_exist_query --atleast-version)
313
      endif()
314 315 316

      if (_pkg_check_modules_pkg_op STREQUAL "=")
        list(APPEND _pkg_check_modules_exist_query --exact-version)
317
      endif()
318

319 320
      if (_pkg_check_modules_pkg_op STREQUAL "<=")
        list(APPEND _pkg_check_modules_exist_query --max-version)
321
      endif()
322 323 324

      # create the final query which is of the format:
      # * --atleast-version <version> <pkg-name>
325
      # * --exact-version <version> <pkg-name>
326 327 328 329
      # * --max-version <version> <pkg-name>
      # * --exists <pkg-name>
      if (_pkg_check_modules_pkg_op)
        list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
330
      else()
331
        list(APPEND _pkg_check_modules_exist_query --exists --print-errors --short-errors)
332
      endif()
333 334 335 336 337 338 339 340 341 342 343 344

      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
      _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)

      list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
      list(APPEND _pkg_check_modules_packages    "${_pkg_check_modules_pkg_name}")

      # execute the query
      execute_process(
        COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
345 346 347
        RESULT_VARIABLE _pkgconfig_retval
        ERROR_VARIABLE _pkgconfig_error
        ERROR_STRIP_TRAILING_WHITESPACE)
348 349 350 351

      # evaluate result and tell failures
      if (_pkgconfig_retval)
        if(NOT ${_is_silent})
352
          message(STATUS "  ${_pkgconfig_error}")
353
        endif()
354 355

        set(_pkg_check_modules_failed 1)
356 357
      endif()
    endforeach()
358 359 360 361

    if(_pkg_check_modules_failed)
      # fail when requested
      if (${_is_required})
362
        message(FATAL_ERROR "A required package was not found")
363 364
      endif ()
    else()
365 366
      # when we are here, we checked whether requested modules
      # exist. Now, go through them and set variables
367

368 369 370 371 372 373 374 375
      _pkgconfig_set(${_prefix}_FOUND 1)
      list(LENGTH _pkg_check_modules_packages pkg_count)

      # iterate through all modules again and set individual variables
      foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
        # handle case when there is only one package required
        if (pkg_count EQUAL 1)
          set(_pkg_check_prefix "${_prefix}")
376
        else()
377
          set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
378
        endif()
379

380
        _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION    ""   --modversion )
381 382 383
        pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
        pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
        pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
384

385
        if (NOT ${_is_silent})
386
          message(STATUS "  Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
387 388
        endif ()
      endforeach()
389 390 391 392 393 394 395 396 397 398

      # set variables which are combined for multiple modules
      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES           "(^| )-l" --libs-only-l )
      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS        "(^| )-L" --libs-only-L )
      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS             ""        --libs )
      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER       ""        --libs-only-other )

      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS        "(^| )-I" --cflags-only-I )
      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS              ""        --cflags )
      _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER        ""        --cflags-only-other )
399
    endif()
400 401 402 403 404 405 406 407

    if(NOT "${_extra_paths}" STREQUAL "")
      # Restore the environment variable
      set(ENV{PKG_CONFIG_PATH} ${_pkgconfig_path})
    endif()

    unset(_extra_paths)
    unset(_pkgconfig_path_old)
408
  else()
409 410
    if (${_is_required})
      message(SEND_ERROR "pkg-config tool not found")
411 412 413
    endif ()
  endif()
endmacro()
414 415 416 417 418

###
### User visible macros start here
###

419 420 421 422 423
#[========================================[.rst:
.. command:: pkg_check_modules

 Checks for all the given modules. ::

424 425 426 427
    pkg_check_modules(<PREFIX> [REQUIRED] [QUIET]
                      [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
                      <MODULE> [<MODULE>]*)

428 429 430 431 432 433

 When the ``REQUIRED`` argument was set, macros will fail with an error
 when module(s) could not be found.

 When the ``QUIET`` argument is set, no status messages will be printed.

434 435 436 437 438 439 440 441 442
 By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
 later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set, the
 :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH`, and
 :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables will
 be added to ``pkg-config`` search path.
 The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
 disable this behavior for the cache variables and the environment
 variables, respectively.

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
 It sets the following variables: ::

    PKG_CONFIG_FOUND          ... if pkg-config executable was found
    PKG_CONFIG_EXECUTABLE     ... pathname of the pkg-config program
    PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
                                  (since CMake 2.8.8)

 For the following variables two sets of values exist; first one is the
 common one and has the given PREFIX.  The second set contains flags
 which are given out when ``pkg-config`` was called with the ``--static``
 option. ::

    <XPREFIX>_FOUND          ... set to 1 if module(s) exist
    <XPREFIX>_LIBRARIES      ... only the libraries (w/o the '-l')
    <XPREFIX>_LIBRARY_DIRS   ... the paths of the libraries (w/o the '-L')
    <XPREFIX>_LDFLAGS        ... all required linker flags
    <XPREFIX>_LDFLAGS_OTHER  ... all other linker flags
    <XPREFIX>_INCLUDE_DIRS   ... the '-I' preprocessor flags (w/o the '-I')
    <XPREFIX>_CFLAGS         ... all required cflags
    <XPREFIX>_CFLAGS_OTHER   ... the other compiler flags

 ::

    <XPREFIX> = <PREFIX>        for common case
    <XPREFIX> = <PREFIX>_STATIC for static linking

 There are some special variables whose prefix depends on the count of
 given modules.  When there is only one module, <PREFIX> stays
 unchanged.  When there are multiple modules, the prefix will be
 changed to <PREFIX>_<MODNAME>: ::

    <XPREFIX>_VERSION    ... version of the module
    <XPREFIX>_PREFIX     ... prefix-directory of the module
    <XPREFIX>_INCLUDEDIR ... include-dir of the module
    <XPREFIX>_LIBDIR     ... lib-dir of the module

 ::

    <XPREFIX> = <PREFIX>  when |MODULES| == 1, else
    <XPREFIX> = <PREFIX>_<MODNAME>

 A <MODULE> parameter can have the following formats: ::

    {MODNAME}            ... matches any version
    {MODNAME}>={VERSION} ... at least version <VERSION> is required
    {MODNAME}={VERSION}  ... exactly version <VERSION> is required
    {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>

 Examples

 .. code-block:: cmake

    pkg_check_modules (GLIB2   glib-2.0)

 .. code-block:: cmake

    pkg_check_modules (GLIB2   glib-2.0>=2.10)

 Requires at least version 2.10 of glib2 and defines e.g.
 ``GLIB2_VERSION=2.10.3``

 .. code-block:: cmake

    pkg_check_modules (FOO     glib-2.0>=2.10 gtk+-2.0)

 Requires both glib2 and gtk2, and defines e.g.
 ``FOO_glib-2.0_VERSION=2.10.3`` and ``FOO_gtk+-2.0_VERSION=2.8.20``

 .. code-block:: cmake

    pkg_check_modules (XRENDER REQUIRED xrender)

515 516 517 518
 Defines for example::

   XRENDER_LIBRARIES=Xrender;X11``
   XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
519
#]========================================]
520 521
macro(pkg_check_modules _prefix _module0)
  # check cached value
522
  if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
523 524
    _pkgconfig_parse_options   (_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path "${_module0}" ${ARGN})
    _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} "${_prefix}" ${_pkg_modules})
525 526

    _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
527 528
  endif()
endmacro()
529

530 531 532 533 534 535 536

#[========================================[.rst:
.. command:: pkg_search_module

 Same as :command:`pkg_check_modules`, but instead it checks for given
 modules and uses the first working one. ::

537 538 539
    pkg_search_module(<PREFIX> [REQUIRED] [QUIET]
                      [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
                      <MODULE> [<MODULE>]*)
540 541 542 543 544 545 546

 Examples

 .. code-block:: cmake

    pkg_search_module (BAR     libxml-2.0 libxml2 libxml>=2)
#]========================================]
547 548
macro(pkg_search_module _prefix _module0)
  # check cached value
549
  if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
550
    set(_pkg_modules_found 0)
551
    _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path "${_module0}" ${ARGN})
552

553
    if (NOT ${_pkg_is_silent})
554
      message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
555
    endif ()
556 557 558 559

    # iterate through all modules and stop at the first working one.
    foreach(_pkg_alt ${_pkg_modules_alt})
      if(NOT _pkg_modules_found)
560
        _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} "${_prefix}" "${_pkg_alt}")
561
      endif()
562 563 564

      if (${_prefix}_FOUND)
        set(_pkg_modules_found 1)
565 566
      endif()
    endforeach()
567 568 569 570

    if (NOT ${_prefix}_FOUND)
      if(${_pkg_is_required})
        message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
571 572
      endif()
    endif()
573

574
    _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
575 576
  endif()
endmacro()
577

578 579 580 581 582

#[========================================[.rst:
.. variable:: PKG_CONFIG_EXECUTABLE

 Path to the pkg-config executable.
583 584 585 586 587 588 589 590 591 592 593 594


.. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH

 Whether :command:`pkg_check_modules` and :command:`pkg_search_module`
 should add the paths in :variable:`CMAKE_PREFIX_PATH`,
 :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH`
 cache and environment variables to ``pkg-config`` search path.

 If this variable is not set, this behavior is enabled by default if
 :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
 otherwise.
595 596 597
#]========================================]


598 599 600
### Local Variables:
### mode: cmake
### End: