952ca9265820d639bc3f430c85796bc7641aec41
[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     unset(_props)
226     if(${_prefix}_INCLUDE_DIRS)
227       set_property(TARGET PkgConfig::${_prefix} PROPERTY
228                    INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
229     endif()
230     if(_libs)
231       set_property(TARGET PkgConfig::${_prefix} PROPERTY
232                    INTERFACE_LINK_LIBRARIES "${_libs}")
233     endif()
234     if(${_prefix}_CFLAGS_OTHER)
235       set_property(TARGET PkgConfig::${_prefix} PROPERTY
236                    INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
237     endif()
238   endif()
239 endfunction()
240
241 ###
242 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _prefix)
243   _pkgconfig_unset(${_prefix}_FOUND)
244   _pkgconfig_unset(${_prefix}_VERSION)
245   _pkgconfig_unset(${_prefix}_PREFIX)
246   _pkgconfig_unset(${_prefix}_INCLUDEDIR)
247   _pkgconfig_unset(${_prefix}_LIBDIR)
248   _pkgconfig_unset(${_prefix}_LIBS)
249   _pkgconfig_unset(${_prefix}_LIBS_L)
250   _pkgconfig_unset(${_prefix}_LIBS_PATHS)
251   _pkgconfig_unset(${_prefix}_LIBS_OTHER)
252   _pkgconfig_unset(${_prefix}_CFLAGS)
253   _pkgconfig_unset(${_prefix}_CFLAGS_I)
254   _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
255   _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
256   _pkgconfig_unset(${_prefix}_STATIC_LIBS)
257   _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
258   _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
259   _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
260   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
261   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
262   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
263
264   # create a better addressable variable of the modules and calculate its size
265   set(_pkg_check_modules_list ${ARGN})
266   list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
267
268   if(PKG_CONFIG_EXECUTABLE)
269     # give out status message telling checked module
270     if (NOT ${_is_silent})
271       if (_pkg_check_modules_cnt EQUAL 1)
272         message(STATUS "Checking for module '${_pkg_check_modules_list}'")
273       else()
274         message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
275       endif()
276     endif()
277
278     set(_pkg_check_modules_packages)
279     set(_pkg_check_modules_failed)
280
281     set(_extra_paths)
282
283     if(NOT _no_cmake_path)
284       _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
285       _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
286       _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
287     endif()
288
289     if(NOT _no_cmake_environment_path)
290       _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
291       _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
292       _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
293     endif()
294
295     if(NOT "${_extra_paths}" STREQUAL "")
296       # Save the PKG_CONFIG_PATH environment variable, and add paths
297       # from the CMAKE_PREFIX_PATH variables
298       set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
299       set(_pkgconfig_path "${_pkgconfig_path_old}")
300       if(NOT "${_pkgconfig_path}" STREQUAL "")
301         file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
302       endif()
303
304       # Create a list of the possible pkgconfig subfolder (depending on
305       # the system
306       set(_lib_dirs)
307       if(NOT DEFINED CMAKE_SYSTEM_NAME
308           OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
309               AND NOT CMAKE_CROSSCOMPILING))
310         if(EXISTS "/etc/debian_version") # is this a debian system ?
311           if(CMAKE_LIBRARY_ARCHITECTURE)
312             list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
313           endif()
314         else()
315           # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
316           get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
317           if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
318             list(APPEND _lib_dirs "lib32/pkgconfig")
319           endif()
320           get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
321           if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
322             list(APPEND _lib_dirs "lib64/pkgconfig")
323           endif()
324           get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
325           if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
326             list(APPEND _lib_dirs "libx32/pkgconfig")
327           endif()
328         endif()
329       endif()
330       if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
331         list(APPEND _lib_dirs "libdata/pkgconfig")
332       endif()
333       list(APPEND _lib_dirs "lib/pkgconfig")
334       list(APPEND _lib_dirs "share/pkgconfig")
335
336       # Check if directories exist and eventually append them to the
337       # pkgconfig path list
338       foreach(_prefix_dir ${_extra_paths})
339         foreach(_lib_dir ${_lib_dirs})
340           if(EXISTS "${_prefix_dir}/${_lib_dir}")
341             list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
342             list(REMOVE_DUPLICATES _pkgconfig_path)
343           endif()
344         endforeach()
345       endforeach()
346
347       # Prepare and set the environment variable
348       if(NOT "${_pkgconfig_path}" STREQUAL "")
349         # remove empty values from the list
350         list(REMOVE_ITEM _pkgconfig_path "")
351         file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
352         if(UNIX)
353           string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
354           string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
355         endif()
356         set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
357       endif()
358
359       # Unset variables
360       unset(_lib_dirs)
361       unset(_pkgconfig_path)
362     endif()
363
364     # iterate through module list and check whether they exist and match the required version
365     foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
366       set(_pkg_check_modules_exist_query)
367
368       # check whether version is given
369       if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
370         set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
371         set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
372         set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
373       else()
374         set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
375         set(_pkg_check_modules_pkg_op)
376         set(_pkg_check_modules_pkg_ver)
377       endif()
378
379       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
380       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
381       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
382       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
383
384       list(APPEND _pkg_check_modules_packages    "${_pkg_check_modules_pkg_name}")
385
386       # create the final query which is of the format:
387       # * <pkg-name> >= <version>
388       # * <pkg-name> = <version>
389       # * <pkg-name> <= <version>
390       # * --exists <pkg-name>
391       list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
392       if (_pkg_check_modules_pkg_op)
393         list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
394       else()
395         list(APPEND _pkg_check_modules_exist_query --exists)
396         list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
397       endif()
398
399       # execute the query
400       execute_process(
401         COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
402         RESULT_VARIABLE _pkgconfig_retval
403         ERROR_VARIABLE _pkgconfig_error
404         ERROR_STRIP_TRAILING_WHITESPACE)
405
406       # evaluate result and tell failures
407       if (_pkgconfig_retval)
408         if(NOT ${_is_silent})
409           message(STATUS "  ${_pkgconfig_error}")
410         endif()
411
412         set(_pkg_check_modules_failed 1)
413       endif()
414     endforeach()
415
416     if(_pkg_check_modules_failed)
417       # fail when requested
418       if (${_is_required})
419         message(FATAL_ERROR "A required package was not found")
420       endif ()
421     else()
422       # when we are here, we checked whether requested modules
423       # exist. Now, go through them and set variables
424
425       _pkgconfig_set(${_prefix}_FOUND 1)
426       list(LENGTH _pkg_check_modules_packages pkg_count)
427
428       # iterate through all modules again and set individual variables
429       foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
430         # handle case when there is only one package required
431         if (pkg_count EQUAL 1)
432           set(_pkg_check_prefix "${_prefix}")
433         else()
434           set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
435         endif()
436
437         _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION    ""   --modversion )
438         pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
439         pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
440         pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
441         foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
442           _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
443         endforeach ()
444
445         if (NOT ${_is_silent})
446           message(STATUS "  Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
447         endif ()
448       endforeach()
449
450       # set variables which are combined for multiple modules
451       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES           "(^| )-l" --libs-only-l )
452       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS        "(^| )-L" --libs-only-L )
453       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS             ""        --libs )
454       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER       ""        --libs-only-other )
455
456       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS        "(^| )-I" --cflags-only-I )
457       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS              ""        --cflags )
458       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER        ""        --cflags-only-other )
459
460       if (_imp_target)
461         _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
462       endif()
463     endif()
464
465     if(NOT "${_extra_paths}" STREQUAL "")
466       # Restore the environment variable
467       set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
468     endif()
469
470     unset(_extra_paths)
471     unset(_pkgconfig_path_old)
472   else()
473     if (${_is_required})
474       message(SEND_ERROR "pkg-config tool not found")
475     endif ()
476   endif()
477 endmacro()
478
479
480 #[========================================[.rst:
481 .. command:: pkg_check_modules
482
483   Checks for all the given modules, setting a variety of result variables in
484   the calling scope. ::
485
486     pkg_check_modules(<prefix>
487                       [REQUIRED] [QUIET]
488                       [NO_CMAKE_PATH]
489                       [NO_CMAKE_ENVIRONMENT_PATH]
490                       [IMPORTED_TARGET]
491                       <moduleSpec> [<moduleSpec>...])
492
493   When the ``REQUIRED`` argument is given, the command will fail with an error
494   if module(s) could not be found.
495
496   When the ``QUIET`` argument is given, no status messages will be printed.
497
498   By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
499   later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set to a
500   boolean ``True`` value, then the :variable:`CMAKE_PREFIX_PATH`,
501   :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
502   and environment variables will be added to the ``pkg-config`` search path.
503   The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
504   disable this behavior for the cache variables and environment variables
505   respectively.
506
507   The ``IMPORTED_TARGET`` argument will create an imported target named
508   ``PkgConfig::<prefix>`` that can be passed directly as an argument to
509   :command:`target_link_libraries`.
510
511   Each ``<moduleSpec>`` must be in one of the following formats::
512
513     {moduleName}            ... matches any version
514     {moduleName}>={version} ... at least version <version> is required
515     {moduleName}={version}  ... exactly version <version> is required
516     {moduleName}<={version} ... modules must not be newer than <version>
517
518   The following variables may be set upon return.  Two sets of values exist,
519   one for the common case (``<XXX> = <prefix>``) and another for the
520   information ``pkg-config`` provides when it is called with the ``--static``
521   option (``<XXX> = <prefix>_STATIC``)::
522
523     <XXX>_FOUND          ... set to 1 if module(s) exist
524     <XXX>_LIBRARIES      ... only the libraries (without the '-l')
525     <XXX>_LIBRARY_DIRS   ... the paths of the libraries (without the '-L')
526     <XXX>_LDFLAGS        ... all required linker flags
527     <XXX>_LDFLAGS_OTHER  ... all other linker flags
528     <XXX>_INCLUDE_DIRS   ... the '-I' preprocessor flags (without the '-I')
529     <XXX>_CFLAGS         ... all required cflags
530     <XXX>_CFLAGS_OTHER   ... the other compiler flags
531
532   All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
533   associated variable returned from ``pkg-config`` has multiple values.
534
535   There are some special variables whose prefix depends on the number of
536   ``<moduleSpec>`` given.  When there is only one ``<moduleSpec>``,
537   ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
538   items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``::
539
540     <YYY>_VERSION    ... version of the module
541     <YYY>_PREFIX     ... prefix directory of the module
542     <YYY>_INCLUDEDIR ... include directory of the module
543     <YYY>_LIBDIR     ... lib directory of the module
544
545   Examples
546
547   .. code-block:: cmake
548
549     pkg_check_modules (GLIB2 glib-2.0)
550
551   Looks for any version of glib2.  If found, the output variable
552   ``GLIB2_VERSION`` will hold the actual version found.
553
554   .. code-block:: cmake
555
556     pkg_check_modules (GLIB2 glib-2.0>=2.10)
557
558   Looks for at least version 2.10 of glib2.  If found, the output variable
559   ``GLIB2_VERSION`` will hold the actual version found.
560
561   .. code-block:: cmake
562
563     pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
564
565   Looks for both glib2-2.0 (at least version 2.10) and any version of
566   gtk2+-2.0.  Only if both are found will ``FOO`` be considered found.
567   The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
568   set to their respective found module versions.
569
570   .. code-block:: cmake
571
572     pkg_check_modules (XRENDER REQUIRED xrender)
573
574   Requires any version of ``xrender``.  Example output variables set by a
575   successful call::
576
577     XRENDER_LIBRARIES=Xrender;X11
578     XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
579 #]========================================]
580 macro(pkg_check_modules _prefix _module0)
581   _pkgconfig_parse_options(_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
582   # check cached value
583   if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
584       (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
585       (    "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
586     _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" ${_pkg_modules})
587
588     _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
589     if (${_prefix}_FOUND)
590       _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
591     endif()
592   elseif (${_prefix}_FOUND AND ${_imp_target})
593     _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
594   endif()
595 endmacro()
596
597
598 #[========================================[.rst:
599 .. command:: pkg_search_module
600
601   The behavior of this command is the same as :command:`pkg_check_modules`,
602   except that rather than checking for all the specified modules, it searches
603   for just the first successful match. ::
604
605     pkg_search_module(<prefix>
606                       [REQUIRED] [QUIET]
607                       [NO_CMAKE_PATH]
608                       [NO_CMAKE_ENVIRONMENT_PATH]
609                       [IMPORTED_TARGET]
610                       <moduleSpec> [<moduleSpec>...])
611
612   Examples
613
614   .. code-block:: cmake
615
616     pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
617 #]========================================]
618 macro(pkg_search_module _prefix _module0)
619   _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
620   # check cached value
621   if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
622     set(_pkg_modules_found 0)
623
624     if (NOT ${_pkg_is_silent})
625       message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
626     endif ()
627
628     # iterate through all modules and stop at the first working one.
629     foreach(_pkg_alt ${_pkg_modules_alt})
630       if(NOT _pkg_modules_found)
631         _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" "${_pkg_alt}")
632       endif()
633
634       if (${_prefix}_FOUND)
635         set(_pkg_modules_found 1)
636       endif()
637     endforeach()
638
639     if (NOT ${_prefix}_FOUND)
640       if(${_pkg_is_required})
641         message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
642       endif()
643     endif()
644
645     _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
646   elseif (${_prefix}_FOUND AND ${_imp_target})
647     _pkg_create_imp_target("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path})
648   endif()
649 endmacro()
650
651
652 #[========================================[.rst:
653 Variables Affecting Behavior
654 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
655
656 .. variable:: PKG_CONFIG_EXECUTABLE
657
658   This can be set to the path of the pkg-config executable.  If not provided,
659   it will be set by the module as a result of calling :command:`find_program`
660   internally.  The ``PKG_CONFIG`` environment variable can be used as a hint.
661
662 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
663
664   Specifies whether :command:`pkg_check_modules` and
665   :command:`pkg_search_module` should add the paths in the
666   :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
667   :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
668   ``pkg-config`` search path.
669
670   If this variable is not set, this behavior is enabled by default if
671   :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
672   otherwise.
673 #]========================================]
674
675
676 ### Local Variables:
677 ### mode: cmake
678 ### End: