Debian Codegraph @

cmake/backports/3.15.7/FindPkgConfig.cmake

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