Imported Upstream version 3.11.2
[platform/upstream/cmake.git] / Modules / FindPkgConfig.cmake
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:
5 FindPkgConfig
6 -------------
7
8 A ``pkg-config`` module for CMake.
9
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::
13
14   PKG_CONFIG_FOUND          ... if pkg-config executable was found
15   PKG_CONFIG_EXECUTABLE     ... pathname of the pkg-config program
16   PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
17                                 (since CMake 2.8.8)
18
19 #]========================================]
20
21 ### Common stuff ####
22 set(PKG_CONFIG_VERSION 1)
23
24 # find pkg-config, use PKG_CONFIG if set
25 if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
26   set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable")
27 endif()
28 find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
29 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
30
31 if (PKG_CONFIG_EXECUTABLE)
32   execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version
33     OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING
34     ERROR_QUIET
35     OUTPUT_STRIP_TRAILING_WHITESPACE)
36 endif ()
37
38 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
39 find_package_handle_standard_args(PkgConfig
40                                   REQUIRED_VARS PKG_CONFIG_EXECUTABLE
41                                   VERSION_VAR PKG_CONFIG_VERSION_STRING)
42
43 # This is needed because the module name is "PkgConfig" but the name of
44 # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
45 # handled by FPHSA.
46 set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
47
48 # Unsets the given variables
49 macro(_pkgconfig_unset var)
50   set(${var} "" CACHE INTERNAL "")
51 endmacro()
52
53 macro(_pkgconfig_set var value)
54   set(${var} ${value} CACHE INTERNAL "")
55 endmacro()
56
57 # Invokes pkgconfig, cleans up the result and sets variables
58 macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
59   set(_pkgconfig_invoke_result)
60
61   execute_process(
62     COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
63     OUTPUT_VARIABLE _pkgconfig_invoke_result
64     RESULT_VARIABLE _pkgconfig_failed
65     OUTPUT_STRIP_TRAILING_WHITESPACE)
66
67   if (_pkgconfig_failed)
68     set(_pkgconfig_${_varname} "")
69     _pkgconfig_unset(${_prefix}_${_varname})
70   else()
71     string(REGEX REPLACE "[\r\n]"       " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
72
73     if (NOT ${_regexp} STREQUAL "")
74       string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
75     endif()
76
77     separate_arguments(_pkgconfig_invoke_result)
78
79     #message(STATUS "  ${_varname} ... ${_pkgconfig_invoke_result}")
80     set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
81     _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
82   endif()
83 endmacro()
84
85 #[========================================[.rst:
86 .. command:: pkg_get_variable
87
88   Retrieves the value of a pkg-config variable ``varName`` and stores it in the
89   result variable ``resultVar`` in the calling scope. ::
90
91     pkg_get_variable(<resultVar> <moduleName> <varName>)
92
93   If ``pkg-config`` returns multiple values for the specified variable,
94   ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
95
96   For example:
97
98   .. code-block:: cmake
99
100     pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
101 #]========================================]
102 function (pkg_get_variable result pkg variable)
103   _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
104   set("${result}"
105     "${prefix_result}"
106     PARENT_SCOPE)
107 endfunction ()
108
109 # Invokes pkgconfig two times; once without '--static' and once with
110 # '--static'
111 macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
112   _pkgconfig_invoke("${_pkglist}" ${_prefix}        ${_varname} "${cleanup_regexp}" ${ARGN})
113   _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static  ${ARGN})
114 endmacro()
115
116 # Splits given arguments into options and a package list
117 macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target)
118   set(${_is_req} 0)
119   set(${_is_silent} 0)
120   set(${_no_cmake_path} 0)
121   set(${_no_cmake_environment_path} 0)
122   set(${_imp_target} 0)
123   if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
124     if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
125       set(${_no_cmake_path} 1)
126       set(${_no_cmake_environment_path} 1)
127     endif()
128   elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
129     set(${_no_cmake_path} 1)
130     set(${_no_cmake_environment_path} 1)
131   endif()
132
133   foreach(_pkg ${ARGN})
134     if (_pkg STREQUAL "REQUIRED")
135       set(${_is_req} 1)
136     endif ()
137     if (_pkg STREQUAL "QUIET")
138       set(${_is_silent} 1)
139     endif ()
140     if (_pkg STREQUAL "NO_CMAKE_PATH")
141       set(${_no_cmake_path} 1)
142     endif()
143     if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
144       set(${_no_cmake_environment_path} 1)
145     endif()
146     if (_pkg STREQUAL "IMPORTED_TARGET")
147       set(${_imp_target} 1)
148     endif()
149   endforeach()
150
151   set(${_result} ${ARGN})
152   list(REMOVE_ITEM ${_result} "REQUIRED")
153   list(REMOVE_ITEM ${_result} "QUIET")
154   list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
155   list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
156   list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
157 endmacro()
158
159 # Add the content of a variable or an environment variable to a list of
160 # paths
161 # Usage:
162 #  - _pkgconfig_add_extra_path(_extra_paths VAR)
163 #  - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
164 function(_pkgconfig_add_extra_path _extra_paths_var _var)
165   set(_is_env 0)
166   if(ARGC GREATER 2 AND _var STREQUAL "ENV")
167     set(_var ${ARGV2})
168     set(_is_env 1)
169   endif()
170   if(NOT _is_env)
171     if(NOT "${${_var}}" STREQUAL "")
172       list(APPEND ${_extra_paths_var} ${${_var}})
173     endif()
174   else()
175     if(NOT "$ENV{${_var}}" STREQUAL "")
176       file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
177       list(APPEND ${_extra_paths_var} ${_path})
178       unset(_path)
179     endif()
180   endif()
181   set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
182 endfunction()
183
184 # scan the LDFLAGS returned by pkg-config for library directories and
185 # libraries, figure out the absolute paths of that libraries in the
186 # given directories, and create an imported target from them
187 function(_pkg_create_imp_target _prefix _no_cmake_path _no_cmake_environment_path)
188   unset(_libs)
189   unset(_find_opts)
190
191   # set the options that are used as long as the .pc file does not provide a library
192   # path to look into
193   if(_no_cmake_path)
194     list(APPEND _find_opts "NO_CMAKE_PATH")
195   endif()
196   if(_no_cmake_environment_path)
197     list(APPEND _find_opts "NO_CMAKE_ENVIRONMENT_PATH")
198   endif()
199
200   unset(_search_paths)
201   foreach (flag IN LISTS ${_prefix}_LDFLAGS)
202     if (flag MATCHES "^-L(.*)")
203       # only look into the given paths from now on
204       list(APPEND _search_paths ${CMAKE_MATCH_1})
205       set(_find_opts HINTS ${_search_paths} NO_DEFAULT_PATH)
206       continue()
207     endif()
208     if (flag MATCHES "^-l(.*)")
209       set(_pkg_search "${CMAKE_MATCH_1}")
210     else()
211       continue()
212     endif()
213
214     find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
215                  NAMES ${_pkg_search}
216                  ${_find_opts})
217     list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
218   endforeach()
219
220   # only create the target if it is linkable, i.e. no executables
221   if (NOT TARGET PkgConfig::${_prefix}
222       AND ( ${_prefix}_INCLUDE_DIRS OR _libs OR ${_prefix}_CFLAGS_OTHER ))
223     add_library(PkgConfig::${_prefix} INTERFACE IMPORTED)
224
225     if(${_prefix}_INCLUDE_DIRS)
226       set_property(TARGET PkgConfig::${_prefix} PROPERTY
227                    INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
228     endif()
229     if(_libs)
230       set_property(TARGET PkgConfig::${_prefix} PROPERTY
231                    INTERFACE_LINK_LIBRARIES "${_libs}")
232     endif()
233     if(${_prefix}_CFLAGS_OTHER)
234       set_property(TARGET PkgConfig::${_prefix} PROPERTY
235                    INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
236     endif()
237   endif()
238 endfunction()
239
240 ###
241 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _prefix)
242   _pkgconfig_unset(${_prefix}_FOUND)
243   _pkgconfig_unset(${_prefix}_VERSION)
244   _pkgconfig_unset(${_prefix}_PREFIX)
245   _pkgconfig_unset(${_prefix}_INCLUDEDIR)
246   _pkgconfig_unset(${_prefix}_LIBDIR)
247   _pkgconfig_unset(${_prefix}_LIBS)
248   _pkgconfig_unset(${_prefix}_LIBS_L)
249   _pkgconfig_unset(${_prefix}_LIBS_PATHS)
250   _pkgconfig_unset(${_prefix}_LIBS_OTHER)
251   _pkgconfig_unset(${_prefix}_CFLAGS)
252   _pkgconfig_unset(${_prefix}_CFLAGS_I)
253   _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
254   _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
255   _pkgconfig_unset(${_prefix}_STATIC_LIBS)
256   _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
257   _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
258   _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
259   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
260   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
261   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
262
263   # create a better addressable variable of the modules and calculate its size
264   set(_pkg_check_modules_list ${ARGN})
265   list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
266
267   if(PKG_CONFIG_EXECUTABLE)
268     # give out status message telling checked module
269     if (NOT ${_is_silent})
270       if (_pkg_check_modules_cnt EQUAL 1)
271         message(STATUS "Checking for module '${_pkg_check_modules_list}'")
272       else()
273         message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
274       endif()
275     endif()
276
277     set(_pkg_check_modules_packages)
278     set(_pkg_check_modules_failed)
279
280     set(_extra_paths)
281
282     if(NOT _no_cmake_path)
283       _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
284       _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
285       _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
286     endif()
287
288     if(NOT _no_cmake_environment_path)
289       _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
290       _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
291       _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
292     endif()
293
294     if(NOT "${_extra_paths}" STREQUAL "")
295       # Save the PKG_CONFIG_PATH environment variable, and add paths
296       # from the CMAKE_PREFIX_PATH variables
297       set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
298       set(_pkgconfig_path "${_pkgconfig_path_old}")
299       if(NOT "${_pkgconfig_path}" STREQUAL "")
300         file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
301       endif()
302
303       # Create a list of the possible pkgconfig subfolder (depending on
304       # the system
305       set(_lib_dirs)
306       if(NOT DEFINED CMAKE_SYSTEM_NAME
307           OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
308               AND NOT CMAKE_CROSSCOMPILING))
309         if(EXISTS "/etc/debian_version") # is this a debian system ?
310           if(CMAKE_LIBRARY_ARCHITECTURE)
311             list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
312           endif()
313         else()
314           # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
315           get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
316           if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
317             list(APPEND _lib_dirs "lib32/pkgconfig")
318           endif()
319           get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
320           if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
321             list(APPEND _lib_dirs "lib64/pkgconfig")
322           endif()
323           get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
324           if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
325             list(APPEND _lib_dirs "libx32/pkgconfig")
326           endif()
327         endif()
328       endif()
329       if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
330         list(APPEND _lib_dirs "libdata/pkgconfig")
331       endif()
332       list(APPEND _lib_dirs "lib/pkgconfig")
333       list(APPEND _lib_dirs "share/pkgconfig")
334
335       # Check if directories exist and eventually append them to the
336       # pkgconfig path list
337       foreach(_prefix_dir ${_extra_paths})
338         foreach(_lib_dir ${_lib_dirs})
339           if(EXISTS "${_prefix_dir}/${_lib_dir}")
340             list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
341             list(REMOVE_DUPLICATES _pkgconfig_path)
342           endif()
343         endforeach()
344       endforeach()
345
346       # Prepare and set the environment variable
347       if(NOT "${_pkgconfig_path}" STREQUAL "")
348         # remove empty values from the list
349         list(REMOVE_ITEM _pkgconfig_path "")
350         file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
351         if(UNIX)
352           string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
353           string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
354         endif()
355         set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
356       endif()
357
358       # Unset variables
359       unset(_lib_dirs)
360       unset(_pkgconfig_path)
361     endif()
362
363     # iterate through module list and check whether they exist and match the required version
364     foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
365       set(_pkg_check_modules_exist_query)
366
367       # check whether version is given
368       if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
369         set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
370         set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
371         set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
372       else()
373         set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
374         set(_pkg_check_modules_pkg_op)
375         set(_pkg_check_modules_pkg_ver)
376       endif()
377
378       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
379       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
380       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
381       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
382
383       list(APPEND _pkg_check_modules_packages    "${_pkg_check_modules_pkg_name}")
384
385       # create the final query which is of the format:
386       # * <pkg-name> >= <version>
387       # * <pkg-name> = <version>
388       # * <pkg-name> <= <version>
389       # * --exists <pkg-name>
390       list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
391       if (_pkg_check_modules_pkg_op)
392         list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
393       else()
394         list(APPEND _pkg_check_modules_exist_query --exists)
395         list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
396       endif()
397
398       # execute the query
399       execute_process(
400         COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
401         RESULT_VARIABLE _pkgconfig_retval
402         ERROR_VARIABLE _pkgconfig_error
403         ERROR_STRIP_TRAILING_WHITESPACE)
404
405       # evaluate result and tell failures
406       if (_pkgconfig_retval)
407         if(NOT ${_is_silent})
408           message(STATUS "  ${_pkgconfig_error}")
409         endif()
410
411         set(_pkg_check_modules_failed 1)
412       endif()
413     endforeach()
414
415     if(_pkg_check_modules_failed)
416       # fail when requested
417       if (${_is_required})
418         message(FATAL_ERROR "A required package was not found")
419       endif ()
420     else()
421       # when we are here, we checked whether requested modules
422       # exist. Now, go through them and set variables
423
424       _pkgconfig_set(${_prefix}_FOUND 1)
425       list(LENGTH _pkg_check_modules_packages pkg_count)
426
427       # iterate through all modules again and set individual variables
428       foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
429         # handle case when there is only one package required
430         if (pkg_count EQUAL 1)
431           set(_pkg_check_prefix "${_prefix}")
432         else()
433           set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
434         endif()
435
436         _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION    ""   --modversion )
437         pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
438         pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
439         pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
440         foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
441           _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
442         endforeach ()
443
444         if (NOT ${_is_silent})
445           message(STATUS "  Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
446         endif ()
447       endforeach()
448
449       # set variables which are combined for multiple modules
450       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES           "(^| )-l" --libs-only-l )
451       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS        "(^| )-L" --libs-only-L )
452       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS             ""        --libs )
453       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER       ""        --libs-only-other )
454
455       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS        "(^| )-I" --cflags-only-I )
456       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS              ""        --cflags )
457       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER        ""        --cflags-only-other )
458
459       if (_imp_target)
460         _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
461       endif()
462     endif()
463
464     if(NOT "${_extra_paths}" STREQUAL "")
465       # Restore the environment variable
466       set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
467     endif()
468
469     unset(_extra_paths)
470     unset(_pkgconfig_path_old)
471   else()
472     if (${_is_required})
473       message(SEND_ERROR "pkg-config tool not found")
474     endif ()
475   endif()
476 endmacro()
477
478
479 #[========================================[.rst:
480 .. command:: pkg_check_modules
481
482   Checks for all the given modules, setting a variety of result variables in
483   the calling scope. ::
484
485     pkg_check_modules(<prefix>
486                       [REQUIRED] [QUIET]
487                       [NO_CMAKE_PATH]
488                       [NO_CMAKE_ENVIRONMENT_PATH]
489                       [IMPORTED_TARGET]
490                       <moduleSpec> [<moduleSpec>...])
491
492   When the ``REQUIRED`` argument is given, the command will fail with an error
493   if module(s) could not be found.
494
495   When the ``QUIET`` argument is given, no status messages will be printed.
496
497   By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
498   later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set to a
499   boolean ``True`` value, then the :variable:`CMAKE_PREFIX_PATH`,
500   :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
501   and environment variables will be added to the ``pkg-config`` search path.
502   The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
503   disable this behavior for the cache variables and environment variables
504   respectively.
505
506   The ``IMPORTED_TARGET`` argument will create an imported target named
507   ``PkgConfig::<prefix>`` that can be passed directly as an argument to
508   :command:`target_link_libraries`.
509
510   Each ``<moduleSpec>`` must be in one of the following formats::
511
512     {moduleName}            ... matches any version
513     {moduleName}>={version} ... at least version <version> is required
514     {moduleName}={version}  ... exactly version <version> is required
515     {moduleName}<={version} ... modules must not be newer than <version>
516
517   The following variables may be set upon return.  Two sets of values exist,
518   one for the common case (``<XXX> = <prefix>``) and another for the
519   information ``pkg-config`` provides when it is called with the ``--static``
520   option (``<XXX> = <prefix>_STATIC``)::
521
522     <XXX>_FOUND          ... set to 1 if module(s) exist
523     <XXX>_LIBRARIES      ... only the libraries (without the '-l')
524     <XXX>_LIBRARY_DIRS   ... the paths of the libraries (without the '-L')
525     <XXX>_LDFLAGS        ... all required linker flags
526     <XXX>_LDFLAGS_OTHER  ... all other linker flags
527     <XXX>_INCLUDE_DIRS   ... the '-I' preprocessor flags (without the '-I')
528     <XXX>_CFLAGS         ... all required cflags
529     <XXX>_CFLAGS_OTHER   ... the other compiler flags
530
531   All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
532   associated variable returned from ``pkg-config`` has multiple values.
533
534   There are some special variables whose prefix depends on the number of
535   ``<moduleSpec>`` given.  When there is only one ``<moduleSpec>``,
536   ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
537   items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``::
538
539     <YYY>_VERSION    ... version of the module
540     <YYY>_PREFIX     ... prefix directory of the module
541     <YYY>_INCLUDEDIR ... include directory of the module
542     <YYY>_LIBDIR     ... lib directory of the module
543
544   Examples
545
546   .. code-block:: cmake
547
548     pkg_check_modules (GLIB2 glib-2.0)
549
550   Looks for any version of glib2.  If found, the output variable
551   ``GLIB2_VERSION`` will hold the actual version found.
552
553   .. code-block:: cmake
554
555     pkg_check_modules (GLIB2 glib-2.0>=2.10)
556
557   Looks for at least version 2.10 of glib2.  If found, the output variable
558   ``GLIB2_VERSION`` will hold the actual version found.
559
560   .. code-block:: cmake
561
562     pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
563
564   Looks for both glib2-2.0 (at least version 2.10) and any version of
565   gtk2+-2.0.  Only if both are found will ``FOO`` be considered found.
566   The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
567   set to their respective found module versions.
568
569   .. code-block:: cmake
570
571     pkg_check_modules (XRENDER REQUIRED xrender)
572
573   Requires any version of ``xrender``.  Example output variables set by a
574   successful call::
575
576     XRENDER_LIBRARIES=Xrender;X11
577     XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
578 #]========================================]
579 macro(pkg_check_modules _prefix _module0)
580   _pkgconfig_parse_options(_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
581   # check cached value
582   if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
583       (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
584       (    "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
585     _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" ${_pkg_modules})
586
587     _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
588     if (${_prefix}_FOUND)
589       _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
590     endif()
591   elseif (${_prefix}_FOUND AND ${_imp_target})
592     _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
593   endif()
594 endmacro()
595
596
597 #[========================================[.rst:
598 .. command:: pkg_search_module
599
600   The behavior of this command is the same as :command:`pkg_check_modules`,
601   except that rather than checking for all the specified modules, it searches
602   for just the first successful match. ::
603
604     pkg_search_module(<prefix>
605                       [REQUIRED] [QUIET]
606                       [NO_CMAKE_PATH]
607                       [NO_CMAKE_ENVIRONMENT_PATH]
608                       [IMPORTED_TARGET]
609                       <moduleSpec> [<moduleSpec>...])
610
611   Examples
612
613   .. code-block:: cmake
614
615     pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
616 #]========================================]
617 macro(pkg_search_module _prefix _module0)
618   _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
619   # check cached value
620   if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
621     set(_pkg_modules_found 0)
622
623     if (NOT ${_pkg_is_silent})
624       message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
625     endif ()
626
627     # iterate through all modules and stop at the first working one.
628     foreach(_pkg_alt ${_pkg_modules_alt})
629       if(NOT _pkg_modules_found)
630         _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" "${_pkg_alt}")
631       endif()
632
633       if (${_prefix}_FOUND)
634         set(_pkg_modules_found 1)
635       endif()
636     endforeach()
637
638     if (NOT ${_prefix}_FOUND)
639       if(${_pkg_is_required})
640         message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
641       endif()
642     endif()
643
644     _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
645   elseif (${_prefix}_FOUND AND ${_imp_target})
646     _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
647   endif()
648 endmacro()
649
650
651 #[========================================[.rst:
652 Variables Affecting Behavior
653 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
654
655 .. variable:: PKG_CONFIG_EXECUTABLE
656
657   This can be set to the path of the pkg-config executable.  If not provided,
658   it will be set by the module as a result of calling :command:`find_program`
659   internally.  The ``PKG_CONFIG`` environment variable can be used as a hint.
660
661 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
662
663   Specifies whether :command:`pkg_check_modules` and
664   :command:`pkg_search_module` should add the paths in the
665   :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
666   :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
667   ``pkg-config`` search path.
668
669   If this variable is not set, this behavior is enabled by default if
670   :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
671   otherwise.
672 #]========================================]
673
674
675 ### Local Variables:
676 ### mode: cmake
677 ### End: