Imported Upstream version 3.22.5
[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``
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
30 Both ``PKG_CONFIG_EXECUTABLE`` and ``PKG_CONFIG_ARGN`` are initialized by the
31 module, but may be overridden by the user.  See `Variables Affecting Behavior`_
32 for how these variables are initialized.
33
34 #]========================================]
35
36 cmake_policy(PUSH)
37 cmake_policy(SET CMP0054 NEW) # if() quoted variables not dereferenced
38 cmake_policy(SET CMP0057 NEW) # if IN_LIST
39
40 ### Common stuff ####
41 set(PKG_CONFIG_VERSION 1)
42
43 # find pkg-config, use PKG_CONFIG if set
44 if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
45   separate_arguments(PKG_CONFIG_FROM_ENV_SPLIT NATIVE_COMMAND PROGRAM SEPARATE_ARGS "$ENV{PKG_CONFIG}")
46   list(LENGTH PKG_CONFIG_FROM_ENV_SPLIT PKG_CONFIG_FROM_ENV_SPLIT_ARGC)
47   if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 0)
48     list(GET PKG_CONFIG_FROM_ENV_SPLIT 0 PKG_CONFIG_FROM_ENV_ARGV0)
49     if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 1)
50       list(SUBLIST PKG_CONFIG_FROM_ENV_SPLIT 1 -1 PKG_CONFIG_ARGN)
51     endif()
52     set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_FROM_ENV_ARGV0}" CACHE FILEPATH "pkg-config executable")
53   endif()
54 endif()
55
56 set(PKG_CONFIG_NAMES "pkg-config")
57 if(CMAKE_HOST_WIN32)
58   list(PREPEND PKG_CONFIG_NAMES "pkg-config.bat")
59 endif()
60 list(APPEND PKG_CONFIG_NAMES "pkgconf")
61
62 find_program(PKG_CONFIG_EXECUTABLE
63   NAMES ${PKG_CONFIG_NAMES}
64   NAMES_PER_DIR
65   DOC "pkg-config executable")
66 mark_as_advanced(PKG_CONFIG_EXECUTABLE)
67
68 set(PKG_CONFIG_ARGN "${PKG_CONFIG_ARGN}" CACHE STRING "Arguments to supply to pkg-config")
69 mark_as_advanced(PKG_CONFIG_ARGN)
70
71 set(_PKG_CONFIG_FAILURE_MESSAGE "")
72 if (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)
96 endif ()
97
98 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
99 find_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.
107 set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
108
109 # Unsets the given variables
110 macro(_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 "")
116 endmacro()
117
118 macro(_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 "")
124 endmacro()
125
126 # Invokes pkgconfig, cleans up the result and sets variables
127 macro(_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()
152 endmacro()
153
154 # Internal version of pkg_get_variable; expects PKG_CONFIG_PATH to already be set
155 function (_pkg_get_variable result pkg variable)
156   _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
157   set("${result}"
158     "${prefix_result}"
159     PARENT_SCOPE)
160 endfunction ()
161
162 # Invokes pkgconfig two times; once without '--static' and once with
163 # '--static'
164 macro(_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})
167 endmacro()
168
169 # Splits given arguments into options and a package list
170 macro(_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")
219 endmacro()
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)
226 function(_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)
244 endfunction()
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
249 function(_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)
301 endfunction()
302
303 # create an imported target from all the information returned by pkg-config
304 function(_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()
330 endfunction()
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
334 macro(_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()
339 endmacro()
340
341 ###
342 macro(_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} 1)
433 endmacro()
434
435 macro(_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   else()
444     unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
445   endif()
446
447   unset(_extra_paths)
448   unset(_pkgconfig_path_old)
449 endmacro()
450
451 # pkg-config returns frameworks in --libs-only-other
452 # they need to be in ${_prefix}_LIBRARIES so "-framework a -framework b" does
453 # not incorrectly be combined to "-framework a b"
454 function(_pkgconfig_extract_frameworks _prefix)
455   set(ldflags "${${_prefix}_LDFLAGS_OTHER}")
456   list(FIND ldflags "-framework" FR_POS)
457   list(LENGTH ldflags LD_LENGTH)
458
459   # reduce length by 1 as we need "-framework" and the next entry
460   math(EXPR LD_LENGTH "${LD_LENGTH} - 1")
461   while (FR_POS GREATER -1 AND LD_LENGTH GREATER FR_POS)
462     list(REMOVE_AT ldflags ${FR_POS})
463     list(GET ldflags ${FR_POS} HEAD)
464     list(REMOVE_AT ldflags ${FR_POS})
465     math(EXPR LD_LENGTH "${LD_LENGTH} - 2")
466
467     list(APPEND LIBS "-framework ${HEAD}")
468
469     list(FIND ldflags "-framework" FR_POS)
470   endwhile ()
471   set(${_prefix}_LIBRARIES ${${_prefix}_LIBRARIES} ${LIBS} PARENT_SCOPE)
472   set(${_prefix}_LDFLAGS_OTHER "${ldflags}" PARENT_SCOPE)
473 endfunction()
474
475 # pkg-config returns -isystem include directories in --cflags-only-other,
476 # depending on the version and if there is a space between -isystem and
477 # the actual path
478 function(_pkgconfig_extract_isystem _prefix)
479   set(cflags "${${_prefix}_CFLAGS_OTHER}")
480   set(outflags "")
481   set(incdirs "${${_prefix}_INCLUDE_DIRS}")
482
483   set(next_is_isystem FALSE)
484   foreach (THING IN LISTS cflags)
485     # This may filter "-isystem -isystem". That would not work anyway,
486     # so let it happen.
487     if (THING STREQUAL "-isystem")
488       set(next_is_isystem TRUE)
489       continue()
490     endif ()
491     if (next_is_isystem)
492       set(next_is_isystem FALSE)
493       list(APPEND incdirs "${THING}")
494     elseif (THING MATCHES "^-isystem")
495       string(SUBSTRING "${THING}" 8 -1 THING)
496       list(APPEND incdirs "${THING}")
497     else ()
498       list(APPEND outflags "${THING}")
499     endif ()
500   endforeach ()
501   set(${_prefix}_CFLAGS_OTHER "${outflags}" PARENT_SCOPE)
502   set(${_prefix}_INCLUDE_DIRS "${incdirs}" PARENT_SCOPE)
503 endfunction()
504
505 ###
506 macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global _prefix)
507   _pkgconfig_unset(${_prefix}_FOUND)
508   _pkgconfig_unset(${_prefix}_VERSION)
509   _pkgconfig_unset(${_prefix}_PREFIX)
510   _pkgconfig_unset(${_prefix}_INCLUDEDIR)
511   _pkgconfig_unset(${_prefix}_LIBDIR)
512   _pkgconfig_unset(${_prefix}_MODULE_NAME)
513   _pkgconfig_unset(${_prefix}_LIBS)
514   _pkgconfig_unset(${_prefix}_LIBS_L)
515   _pkgconfig_unset(${_prefix}_LIBS_PATHS)
516   _pkgconfig_unset(${_prefix}_LIBS_OTHER)
517   _pkgconfig_unset(${_prefix}_CFLAGS)
518   _pkgconfig_unset(${_prefix}_CFLAGS_I)
519   _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
520   _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
521   _pkgconfig_unset(${_prefix}_STATIC_LIBS)
522   _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
523   _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
524   _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
525   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
526   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
527   _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
528
529   # create a better addressable variable of the modules and calculate its size
530   set(_pkg_check_modules_list ${ARGN})
531   list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
532
533   if(PKG_CONFIG_EXECUTABLE)
534     # give out status message telling checked module
535     if (NOT ${_is_silent})
536       if (_pkg_check_modules_cnt EQUAL 1)
537         message(STATUS "Checking for module '${_pkg_check_modules_list}'")
538       else()
539         message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
540       endif()
541     endif()
542
543     set(_pkg_check_modules_packages)
544     set(_pkg_check_modules_failed)
545
546     _pkg_set_path_internal()
547
548     # iterate through module list and check whether they exist and match the required version
549     foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
550       set(_pkg_check_modules_exist_query)
551
552       # check whether version is given
553       if (_pkg_check_modules_pkg MATCHES "(.*[^><])(=|[><]=?)(.*)")
554         set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
555         set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
556         set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
557       else()
558         set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
559         set(_pkg_check_modules_pkg_op)
560         set(_pkg_check_modules_pkg_ver)
561       endif()
562
563       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
564       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
565       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
566       _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
567
568       list(APPEND _pkg_check_modules_packages    "${_pkg_check_modules_pkg_name}")
569
570       # create the final query which is of the format:
571       # * <pkg-name> > <version>
572       # * <pkg-name> >= <version>
573       # * <pkg-name> = <version>
574       # * <pkg-name> <= <version>
575       # * <pkg-name> < <version>
576       # * --exists <pkg-name>
577       list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
578       if (_pkg_check_modules_pkg_op)
579         list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
580       else()
581         list(APPEND _pkg_check_modules_exist_query --exists)
582         list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
583       endif()
584
585       # execute the query
586       execute_process(
587         COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${_pkg_check_modules_exist_query}
588         RESULT_VARIABLE _pkgconfig_retval
589         ERROR_VARIABLE _pkgconfig_error
590         ERROR_STRIP_TRAILING_WHITESPACE)
591
592       # evaluate result and tell failures
593       if (_pkgconfig_retval)
594         if(NOT ${_is_silent})
595           message(STATUS "  ${_pkgconfig_error}")
596         endif()
597
598         set(_pkg_check_modules_failed 1)
599       endif()
600     endforeach()
601
602     if(_pkg_check_modules_failed)
603       # fail when requested
604       if (${_is_required})
605         message(FATAL_ERROR "A required package was not found")
606       endif ()
607     else()
608       # when we are here, we checked whether requested modules
609       # exist. Now, go through them and set variables
610
611       _pkgconfig_set(${_prefix}_FOUND 1)
612       list(LENGTH _pkg_check_modules_packages pkg_count)
613
614       # iterate through all modules again and set individual variables
615       foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
616         # handle case when there is only one package required
617         if (pkg_count EQUAL 1)
618           set(_pkg_check_prefix "${_prefix}")
619         else()
620           set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
621         endif()
622
623         _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION    ""   --modversion )
624         pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
625         pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
626         pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
627         foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
628           _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
629         endforeach ()
630           _pkgconfig_set("${_pkg_check_prefix}_MODULE_NAME" "${_pkg_check_modules_pkg}")
631
632         if (NOT ${_is_silent})
633           message(STATUS "  Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
634         endif ()
635       endforeach()
636
637       # set variables which are combined for multiple modules
638       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES     "(^| )-l"             --libs-only-l )
639       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS  "(^| )-L"             --libs-only-L )
640       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS       ""                    --libs )
641       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER ""                    --libs-only-other )
642
643       if (APPLE AND "-framework" IN_LIST ${_prefix}_LDFLAGS_OTHER)
644         _pkgconfig_extract_frameworks("${_prefix}")
645       endif()
646
647       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS  "(^| )(-I|-isystem ?)" --cflags-only-I )
648       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS        ""                    --cflags )
649       _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER  ""                    --cflags-only-other )
650
651       if (${_prefix}_CFLAGS_OTHER MATCHES "-isystem")
652         _pkgconfig_extract_isystem("${_prefix}")
653       endif ()
654
655       _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
656     endif()
657
658     _pkg_restore_path_internal()
659   else()
660     if (${_is_required})
661       message(SEND_ERROR "pkg-config tool not found")
662     endif ()
663   endif()
664 endmacro()
665
666
667 #[========================================[.rst:
668 .. command:: pkg_check_modules
669
670   Checks for all the given modules, setting a variety of result variables in
671   the calling scope.
672
673   .. code-block:: cmake
674
675     pkg_check_modules(<prefix>
676                       [REQUIRED] [QUIET]
677                       [NO_CMAKE_PATH]
678                       [NO_CMAKE_ENVIRONMENT_PATH]
679                       [IMPORTED_TARGET [GLOBAL]]
680                       <moduleSpec> [<moduleSpec>...])
681
682   When the ``REQUIRED`` argument is given, the command will fail with an error
683   if module(s) could not be found.
684
685   When the ``QUIET`` argument is given, no status messages will be printed.
686
687   .. versionadded:: 3.1
688     The :variable:`CMAKE_PREFIX_PATH`,
689     :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
690     and environment variables will be added to the ``pkg-config`` search path.
691     The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
692     disable this behavior for the cache variables and environment variables
693     respectively.
694     The :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` variable set to ``FALSE``
695     disables this behavior globally.
696
697     .. This didn't actually work until 3.3.
698
699   .. versionadded:: 3.6
700     The ``IMPORTED_TARGET`` argument will create an imported target named
701     ``PkgConfig::<prefix>`` that can be passed directly as an argument to
702     :command:`target_link_libraries`.
703
704     .. This didn't actually work until 3.7.
705
706   .. versionadded:: 3.13
707     The ``GLOBAL`` argument will make the
708     imported target available in global scope.
709
710   .. versionadded:: 3.15
711     Non-library linker options reported by ``pkg-config`` are stored in the
712     :prop_tgt:`INTERFACE_LINK_OPTIONS` target property.
713
714   .. versionchanged:: 3.18
715     Include directories specified with ``-isystem`` are stored in the
716     :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` target property.  Previous
717     versions of CMake left them in the :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
718     property.
719
720   Each ``<moduleSpec>`` can be either a bare module name or it can be a
721   module name with a version constraint (operators ``=``, ``<``, ``>``,
722   ``<=`` and ``>=`` are supported).  The following are examples for a module
723   named ``foo`` with various constraints:
724
725   - ``foo`` matches any version.
726   - ``foo<2`` only matches versions before 2.
727   - ``foo>=3.1`` matches any version from 3.1 or later.
728   - ``foo=1.2.3`` requires that foo must be exactly version 1.2.3.
729
730   The following variables may be set upon return.  Two sets of values exist:
731   One for the common case (``<XXX> = <prefix>``) and another for the
732   information ``pkg-config`` provides when called with the ``--static``
733   option (``<XXX> = <prefix>_STATIC``).
734
735   ``<XXX>_FOUND``
736     set to 1 if module(s) exist
737   ``<XXX>_LIBRARIES``
738     only the libraries (without the '-l')
739   ``<XXX>_LINK_LIBRARIES``
740     the libraries and their absolute paths
741   ``<XXX>_LIBRARY_DIRS``
742     the paths of the libraries (without the '-L')
743   ``<XXX>_LDFLAGS``
744     all required linker flags
745   ``<XXX>_LDFLAGS_OTHER``
746     all other linker flags
747   ``<XXX>_INCLUDE_DIRS``
748     the '-I' preprocessor flags (without the '-I')
749   ``<XXX>_CFLAGS``
750     all required cflags
751   ``<XXX>_CFLAGS_OTHER``
752     the other compiler flags
753
754   All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
755   associated variable returned from ``pkg-config`` has multiple values.
756
757   .. versionchanged:: 3.18
758     Include directories specified with ``-isystem`` are stored in the
759     ``<XXX>_INCLUDE_DIRS`` variable.  Previous versions of CMake left them
760     in ``<XXX>_CFLAGS_OTHER``.
761
762   There are some special variables whose prefix depends on the number of
763   ``<moduleSpec>`` given.  When there is only one ``<moduleSpec>``,
764   ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
765   items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``.
766
767   ``<YYY>_VERSION``
768     version of the module
769   ``<YYY>_PREFIX``
770     prefix directory of the module
771   ``<YYY>_INCLUDEDIR``
772     include directory of the module
773   ``<YYY>_LIBDIR``
774     lib directory of the module
775
776   .. versionchanged:: 3.8
777     For any given ``<prefix>``, ``pkg_check_modules()`` can be called multiple
778     times with different parameters.  Previous versions of CMake cached and
779     returned the first successful result.
780
781   .. versionchanged:: 3.16
782     If a full path to the found library can't be determined, but it's still
783     visible to the linker, pass it through as ``-l<name>``.  Previous versions
784     of CMake failed in this case.
785
786   Examples:
787
788   .. code-block:: cmake
789
790     pkg_check_modules (GLIB2 glib-2.0)
791
792   Looks for any version of glib2.  If found, the output variable
793   ``GLIB2_VERSION`` will hold the actual version found.
794
795   .. code-block:: cmake
796
797     pkg_check_modules (GLIB2 glib-2.0>=2.10)
798
799   Looks for at least version 2.10 of glib2.  If found, the output variable
800   ``GLIB2_VERSION`` will hold the actual version found.
801
802   .. code-block:: cmake
803
804     pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
805
806   Looks for both glib2-2.0 (at least version 2.10) and any version of
807   gtk2+-2.0.  Only if both are found will ``FOO`` be considered found.
808   The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
809   set to their respective found module versions.
810
811   .. code-block:: cmake
812
813     pkg_check_modules (XRENDER REQUIRED xrender)
814
815   Requires any version of ``xrender``.  Example output variables set by a
816   successful call::
817
818     XRENDER_LIBRARIES=Xrender;X11
819     XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
820 #]========================================]
821 macro(pkg_check_modules _prefix _module0)
822   _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})
823   # check cached value
824   if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
825       (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
826       (    "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
827     _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})
828
829     _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
830     if (${_prefix}_FOUND)
831       _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
832     endif()
833   else()
834     if (${_prefix}_FOUND)
835       _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
836     endif()
837   endif()
838 endmacro()
839
840
841 #[========================================[.rst:
842 .. command:: pkg_search_module
843
844   The behavior of this command is the same as :command:`pkg_check_modules`,
845   except that rather than checking for all the specified modules, it searches
846   for just the first successful match.
847
848   .. code-block:: cmake
849
850     pkg_search_module(<prefix>
851                       [REQUIRED] [QUIET]
852                       [NO_CMAKE_PATH]
853                       [NO_CMAKE_ENVIRONMENT_PATH]
854                       [IMPORTED_TARGET [GLOBAL]]
855                       <moduleSpec> [<moduleSpec>...])
856
857   .. versionadded:: 3.16
858     If a module is found, the ``<prefix>_MODULE_NAME`` variable will contain the
859     name of the matching module. This variable can be used if you need to run
860     :command:`pkg_get_variable`.
861
862   Example:
863
864   .. code-block:: cmake
865
866     pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
867 #]========================================]
868 macro(pkg_search_module _prefix _module0)
869   _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})
870   # check cached value
871   if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
872     set(_pkg_modules_found 0)
873
874     if (NOT ${_pkg_is_silent})
875       message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
876     endif ()
877
878     # iterate through all modules and stop at the first working one.
879     foreach(_pkg_alt ${_pkg_modules_alt})
880       if(NOT _pkg_modules_found)
881         _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global} "${_prefix}" "${_pkg_alt}")
882       endif()
883
884       if (${_prefix}_FOUND)
885         set(_pkg_modules_found 1)
886         break()
887       endif()
888     endforeach()
889
890     if (NOT ${_prefix}_FOUND)
891       if(${_pkg_is_required})
892         message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
893       endif()
894     endif()
895
896     _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
897   elseif (${_prefix}_FOUND)
898     _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
899   endif()
900 endmacro()
901
902 #[========================================[.rst:
903 .. command:: pkg_get_variable
904
905   .. versionadded:: 3.4
906
907   Retrieves the value of a pkg-config variable ``varName`` and stores it in the
908   result variable ``resultVar`` in the calling scope.
909
910   .. code-block:: cmake
911
912     pkg_get_variable(<resultVar> <moduleName> <varName>)
913
914   If ``pkg-config`` returns multiple values for the specified variable,
915   ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
916
917   For example:
918
919   .. code-block:: cmake
920
921     pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
922 #]========================================]
923 function (pkg_get_variable result pkg variable)
924   _pkg_set_path_internal()
925   _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
926   set("${result}"
927     "${prefix_result}"
928     PARENT_SCOPE)
929   _pkg_restore_path_internal()
930 endfunction ()
931
932
933 #[========================================[.rst:
934 Variables Affecting Behavior
935 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
936
937 .. variable:: PKG_CONFIG_EXECUTABLE
938
939   This cache variable can be set to the path of the pkg-config executable.
940   :command:`find_program` is called internally by the module with this
941   variable.
942
943   .. versionadded:: 3.1
944     The ``PKG_CONFIG`` environment variable can be used as a hint if
945     ``PKG_CONFIG_EXECUTABLE`` has not yet been set.
946
947   .. versionchanged:: 3.22
948     If the ``PKG_CONFIG`` environment variable is set, only the first
949     argument is taken from it when using it as a hint.
950
951 .. variable:: PKG_CONFIG_ARGN
952
953   .. versionadded:: 3.22
954
955   This cache variable can be set to a list of arguments to additionally pass
956   to pkg-config if needed. If not provided, it will be initialized from the
957   ``PKG_CONFIG`` environment variable, if set. The first argument in that
958   environment variable is assumed to be the pkg-config program, while all
959   remaining arguments after that are used to initialize ``PKG_CONFIG_ARGN``.
960   If no such environment variable is defined, ``PKG_CONFIG_ARGN`` is
961   initialized to an empty string. The module does not update the variable once
962   it has been set in the cache.
963
964 .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
965
966   .. versionadded:: 3.1
967
968   Specifies whether :command:`pkg_check_modules` and
969   :command:`pkg_search_module` should add the paths in the
970   :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
971   :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
972   ``pkg-config`` search path.
973
974   If this variable is not set, this behavior is enabled by default if
975   :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
976   otherwise.
977 #]========================================]
978
979
980 ### Local Variables:
981 ### mode: cmake
982 ### End:
983
984 cmake_policy(POP)