FindPkgConfig.cmake 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[========================================[.rst:
  4. FindPkgConfig
  5. -------------
  6. A ``pkg-config`` module for CMake.
  7. Finds the ``pkg-config`` executable and adds the :command:`pkg_get_variable`,
  8. :command:`pkg_check_modules` and :command:`pkg_search_module` commands. The
  9. following variables will also be set:
  10. ``PKG_CONFIG_FOUND``
  11. True if a pkg-config executable was found.
  12. ``PKG_CONFIG_VERSION_STRING``
  13. .. versionadded:: 2.8.8
  14. The version of pkg-config that was found.
  15. ``PKG_CONFIG_EXECUTABLE``
  16. The pathname of the pkg-config program.
  17. ``PKG_CONFIG_ARGN``
  18. .. versionadded:: 3.22
  19. A list of arguments to pass to pkg-config.
  20. Both ``PKG_CONFIG_EXECUTABLE`` and ``PKG_CONFIG_ARGN`` are initialized by the
  21. module, but may be overridden by the user. See `Variables Affecting Behavior`_
  22. for how these variables are initialized.
  23. #]========================================]
  24. ### Common stuff ####
  25. set(PKG_CONFIG_VERSION 1)
  26. # find pkg-config, use PKG_CONFIG if set
  27. if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
  28. separate_arguments(PKG_CONFIG_FROM_ENV_SPLIT NATIVE_COMMAND PROGRAM SEPARATE_ARGS "$ENV{PKG_CONFIG}")
  29. list(LENGTH PKG_CONFIG_FROM_ENV_SPLIT PKG_CONFIG_FROM_ENV_SPLIT_ARGC)
  30. if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 0)
  31. list(GET PKG_CONFIG_FROM_ENV_SPLIT 0 PKG_CONFIG_FROM_ENV_ARGV0)
  32. if(PKG_CONFIG_FROM_ENV_SPLIT_ARGC GREATER 1)
  33. list(SUBLIST PKG_CONFIG_FROM_ENV_SPLIT 1 -1 PKG_CONFIG_ARGN)
  34. endif()
  35. set(PKG_CONFIG_EXECUTABLE "${PKG_CONFIG_FROM_ENV_ARGV0}" CACHE FILEPATH "pkg-config executable")
  36. endif()
  37. endif()
  38. set(PKG_CONFIG_NAMES "pkg-config")
  39. if(CMAKE_HOST_WIN32)
  40. list(PREPEND PKG_CONFIG_NAMES "pkg-config.bat")
  41. set(_PKG_CONFIG_VALIDATOR VALIDATOR __FindPkgConfig_EXECUTABLE_VALIDATOR)
  42. function(__FindPkgConfig_EXECUTABLE_VALIDATOR result_var candidate)
  43. if(candidate MATCHES "\\.[Ee][Xx][Ee]$")
  44. return()
  45. endif()
  46. # Exclude the pkg-config distributed with Strawberry Perl.
  47. execute_process(COMMAND "${candidate}" --help OUTPUT_VARIABLE _output ERROR_VARIABLE _output RESULT_VARIABLE _result)
  48. if(NOT _result EQUAL 0 OR _output MATCHES "Pure-Perl")
  49. set("${result_var}" FALSE PARENT_SCOPE)
  50. endif()
  51. endfunction()
  52. else()
  53. set(_PKG_CONFIG_VALIDATOR "")
  54. endif()
  55. list(APPEND PKG_CONFIG_NAMES "pkgconf")
  56. find_program(PKG_CONFIG_EXECUTABLE
  57. NAMES ${PKG_CONFIG_NAMES}
  58. NAMES_PER_DIR
  59. DOC "pkg-config executable"
  60. ${_PKG_CONFIG_VALIDATOR})
  61. mark_as_advanced(PKG_CONFIG_EXECUTABLE)
  62. unset(_PKG_CONFIG_VALIDATOR)
  63. set(PKG_CONFIG_ARGN "${PKG_CONFIG_ARGN}" CACHE STRING "Arguments to supply to pkg-config")
  64. mark_as_advanced(PKG_CONFIG_ARGN)
  65. set(_PKG_CONFIG_FAILURE_MESSAGE "")
  66. if (PKG_CONFIG_EXECUTABLE)
  67. execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} --version
  68. OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE
  69. ERROR_VARIABLE _PKG_CONFIG_VERSION_ERROR ERROR_STRIP_TRAILING_WHITESPACE
  70. RESULT_VARIABLE _PKG_CONFIG_VERSION_RESULT
  71. )
  72. if (NOT _PKG_CONFIG_VERSION_RESULT EQUAL 0)
  73. string(REPLACE "\n" "\n " _PKG_CONFIG_VERSION_ERROR " ${_PKG_CONFIG_VERSION_ERROR}")
  74. if(PKG_CONFIG_ARGN)
  75. string(REPLACE ";" " " PKG_CONFIG_ARGN " ${PKG_CONFIG_ARGN}")
  76. endif()
  77. string(APPEND _PKG_CONFIG_FAILURE_MESSAGE
  78. "The command\n"
  79. " \"${PKG_CONFIG_EXECUTABLE}\"${PKG_CONFIG_ARGN} --version\n"
  80. " failed with output:\n${PKG_CONFIG_VERSION_STRING}\n"
  81. " stderr: \n${_PKG_CONFIG_VERSION_ERROR}\n"
  82. " result: \n${_PKG_CONFIG_VERSION_RESULT}"
  83. )
  84. set(PKG_CONFIG_EXECUTABLE "")
  85. set(PKG_CONFIG_ARGN "")
  86. unset(PKG_CONFIG_VERSION_STRING)
  87. endif ()
  88. unset(_PKG_CONFIG_VERSION_RESULT)
  89. endif ()
  90. include(FindPackageHandleStandardArgs)
  91. find_package_handle_standard_args(PkgConfig
  92. REQUIRED_VARS PKG_CONFIG_EXECUTABLE
  93. REASON_FAILURE_MESSAGE "${_PKG_CONFIG_FAILURE_MESSAGE}"
  94. VERSION_VAR PKG_CONFIG_VERSION_STRING)
  95. # This is needed because the module name is "PkgConfig" but the name of
  96. # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
  97. # handled by FPHSA.
  98. set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
  99. # Unsets the given variables
  100. macro(_pkgconfig_unset var)
  101. # Clear normal variable (possibly set by project code).
  102. unset(${var})
  103. # Store as cache variable.
  104. # FIXME: Add a policy to switch to a normal variable.
  105. set(${var} "" CACHE INTERNAL "")
  106. endmacro()
  107. macro(_pkgconfig_set var value)
  108. # Clear normal variable (possibly set by project code).
  109. unset(${var})
  110. # Store as cache variable.
  111. # FIXME: Add a policy to switch to a normal variable.
  112. set(${var} ${value} CACHE INTERNAL "")
  113. endmacro()
  114. # Invokes pkgconfig, cleans up the result and sets variables
  115. macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
  116. set(_pkgconfig_invoke_result)
  117. execute_process(
  118. COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${ARGN} ${_pkglist}
  119. OUTPUT_VARIABLE _pkgconfig_invoke_result
  120. RESULT_VARIABLE _pkgconfig_failed
  121. OUTPUT_STRIP_TRAILING_WHITESPACE)
  122. if (_pkgconfig_failed)
  123. set(_pkgconfig_${_varname} "")
  124. _pkgconfig_unset(${_prefix}_${_varname})
  125. else()
  126. string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  127. if (NOT ${_regexp} STREQUAL "")
  128. string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  129. endif()
  130. # pkg-config <0.29.1 and pkgconf <1.5.1 prints quoted variables without unquoting
  131. # unquote only if quotes are first and last characters
  132. if((PKG_CONFIG_VERSION_STRING VERSION_LESS 0.29.1) OR
  133. (PKG_CONFIG_VERSION_STRING VERSION_GREATER_EQUAL 1.0 AND PKG_CONFIG_VERSION_STRING VERSION_LESS 1.5.1))
  134. if (_pkgconfig_invoke_result MATCHES "^\"(.*)\"$")
  135. set(_pkgconfig_invoke_result "${CMAKE_MATCH_1}")
  136. elseif(_pkgconfig_invoke_result MATCHES "^'(.*)'$")
  137. set(_pkgconfig_invoke_result "${CMAKE_MATCH_1}")
  138. endif()
  139. endif()
  140. # pkg-config can represent "spaces within an argument" by backslash-escaping the space.
  141. # UNIX_COMMAND mode treats backslash-escaped spaces as "not a space that delimits arguments".
  142. separate_arguments(_pkgconfig_invoke_result UNIX_COMMAND "${_pkgconfig_invoke_result}")
  143. #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
  144. set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
  145. _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
  146. endif()
  147. endmacro()
  148. # Internal version of pkg_get_variable; expects PKG_CONFIG_PATH to already be set
  149. function (_pkg_get_variable result pkg variable)
  150. _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
  151. set("${result}"
  152. "${prefix_result}"
  153. PARENT_SCOPE)
  154. endfunction ()
  155. # Invokes pkgconfig two times; once without '--static' and once with
  156. # '--static'
  157. macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
  158. _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
  159. _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
  160. endmacro()
  161. # Splits given arguments into options and a package list
  162. macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global)
  163. set(${_is_req} 0)
  164. set(${_is_silent} 0)
  165. set(${_no_cmake_path} 0)
  166. set(${_no_cmake_environment_path} 0)
  167. set(${_imp_target} 0)
  168. set(${_imp_target_global} 0)
  169. if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  170. if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  171. set(${_no_cmake_path} 1)
  172. set(${_no_cmake_environment_path} 1)
  173. endif()
  174. elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
  175. set(${_no_cmake_path} 1)
  176. set(${_no_cmake_environment_path} 1)
  177. endif()
  178. foreach(_pkg ${ARGN})
  179. if (_pkg STREQUAL "REQUIRED")
  180. set(${_is_req} 1)
  181. endif ()
  182. if (_pkg STREQUAL "QUIET")
  183. set(${_is_silent} 1)
  184. endif ()
  185. if (_pkg STREQUAL "NO_CMAKE_PATH")
  186. set(${_no_cmake_path} 1)
  187. endif()
  188. if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
  189. set(${_no_cmake_environment_path} 1)
  190. endif()
  191. if (_pkg STREQUAL "IMPORTED_TARGET")
  192. set(${_imp_target} 1)
  193. endif()
  194. if (_pkg STREQUAL "GLOBAL")
  195. set(${_imp_target_global} 1)
  196. endif()
  197. endforeach()
  198. if (${_imp_target_global} AND NOT ${_imp_target})
  199. message(SEND_ERROR "the argument GLOBAL may only be used together with IMPORTED_TARGET")
  200. endif()
  201. set(${_result} ${ARGN})
  202. list(REMOVE_ITEM ${_result} "REQUIRED")
  203. list(REMOVE_ITEM ${_result} "QUIET")
  204. list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
  205. list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
  206. list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
  207. list(REMOVE_ITEM ${_result} "GLOBAL")
  208. endmacro()
  209. # Add the content of a variable or an environment variable to a list of
  210. # paths
  211. # Usage:
  212. # - _pkgconfig_add_extra_path(_extra_paths VAR)
  213. # - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
  214. function(_pkgconfig_add_extra_path _extra_paths_var _var)
  215. set(_is_env 0)
  216. if(ARGC GREATER 2 AND _var STREQUAL "ENV")
  217. set(_var ${ARGV2})
  218. set(_is_env 1)
  219. endif()
  220. if(NOT _is_env)
  221. if(NOT "${${_var}}" STREQUAL "")
  222. list(APPEND ${_extra_paths_var} ${${_var}})
  223. endif()
  224. else()
  225. if(NOT "$ENV{${_var}}" STREQUAL "")
  226. file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
  227. list(APPEND ${_extra_paths_var} ${_path})
  228. unset(_path)
  229. endif()
  230. endif()
  231. set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
  232. endfunction()
  233. # scan the LDFLAGS returned by pkg-config for library directories and
  234. # libraries, figure out the absolute paths of that libraries in the
  235. # given directories
  236. function(_pkg_find_libs _prefix _no_cmake_path _no_cmake_environment_path)
  237. unset(_libs)
  238. unset(_find_opts)
  239. # set the options that are used as long as the .pc file does not provide a library
  240. # path to look into
  241. if(_no_cmake_path)
  242. list(APPEND _find_opts "NO_CMAKE_PATH")
  243. endif()
  244. if(_no_cmake_environment_path)
  245. list(APPEND _find_opts "NO_CMAKE_ENVIRONMENT_PATH")
  246. endif()
  247. unset(_search_paths)
  248. unset(_next_is_framework)
  249. foreach (flag IN LISTS ${_prefix}_LDFLAGS)
  250. if (_next_is_framework)
  251. list(APPEND _libs "-framework ${flag}")
  252. unset(_next_is_framework)
  253. continue()
  254. endif ()
  255. if (flag MATCHES "^-L(.*)")
  256. list(APPEND _search_paths ${CMAKE_MATCH_1})
  257. continue()
  258. endif()
  259. if (flag MATCHES "^-l(.*)")
  260. set(_pkg_search "${CMAKE_MATCH_1}")
  261. else()
  262. if (flag STREQUAL "-framework")
  263. set(_next_is_framework TRUE)
  264. endif ()
  265. continue()
  266. endif()
  267. if(_search_paths)
  268. # Firstly search in -L paths
  269. find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
  270. NAMES ${_pkg_search}
  271. HINTS ${_search_paths} NO_DEFAULT_PATH)
  272. endif()
  273. find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
  274. NAMES ${_pkg_search}
  275. ${_find_opts})
  276. mark_as_advanced(pkgcfg_lib_${_prefix}_${_pkg_search})
  277. if(pkgcfg_lib_${_prefix}_${_pkg_search})
  278. list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
  279. else()
  280. list(APPEND _libs ${_pkg_search})
  281. endif()
  282. endforeach()
  283. set(${_prefix}_LINK_LIBRARIES "${_libs}" PARENT_SCOPE)
  284. endfunction()
  285. # create an imported target from all the information returned by pkg-config
  286. function(_pkg_create_imp_target _prefix _imp_target_global)
  287. if (NOT TARGET PkgConfig::${_prefix})
  288. if(${_imp_target_global})
  289. set(_global_opt "GLOBAL")
  290. else()
  291. unset(_global_opt)
  292. endif()
  293. add_library(PkgConfig::${_prefix} INTERFACE IMPORTED ${_global_opt})
  294. if(${_prefix}_INCLUDE_DIRS)
  295. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  296. INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
  297. endif()
  298. if(${_prefix}_LINK_LIBRARIES)
  299. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  300. INTERFACE_LINK_LIBRARIES "${${_prefix}_LINK_LIBRARIES}")
  301. endif()
  302. if(${_prefix}_LDFLAGS_OTHER)
  303. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  304. INTERFACE_LINK_OPTIONS "${${_prefix}_LDFLAGS_OTHER}")
  305. endif()
  306. if(${_prefix}_CFLAGS_OTHER)
  307. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  308. INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
  309. endif()
  310. endif()
  311. endfunction()
  312. # recalculate the dynamic output
  313. # this is a macro and not a function so the result of _pkg_find_libs is automatically propagated
  314. macro(_pkg_recalculate _prefix _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global)
  315. _pkg_find_libs(${_prefix} ${_no_cmake_path} ${_no_cmake_environment_path})
  316. if(${_imp_target})
  317. _pkg_create_imp_target(${_prefix} ${_imp_target_global})
  318. endif()
  319. endmacro()
  320. ###
  321. macro(_pkg_set_path_internal)
  322. set(_extra_paths)
  323. if(NOT _no_cmake_path)
  324. _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
  325. _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
  326. _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
  327. endif()
  328. if(NOT _no_cmake_environment_path)
  329. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
  330. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
  331. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
  332. endif()
  333. if(NOT _extra_paths STREQUAL "")
  334. # Save the PKG_CONFIG_PATH environment variable, and add paths
  335. # from the CMAKE_PREFIX_PATH variables
  336. set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
  337. set(_pkgconfig_path "${_pkgconfig_path_old}")
  338. if(NOT _pkgconfig_path STREQUAL "")
  339. file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  340. endif()
  341. # Create a list of the possible pkgconfig subfolder (depending on
  342. # the system
  343. set(_lib_dirs)
  344. if(NOT DEFINED CMAKE_SYSTEM_NAME
  345. OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
  346. AND NOT CMAKE_CROSSCOMPILING))
  347. if(EXISTS "/etc/debian_version") # is this a debian system ?
  348. if(CMAKE_LIBRARY_ARCHITECTURE)
  349. list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
  350. endif()
  351. else()
  352. # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
  353. get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
  354. if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
  355. list(APPEND _lib_dirs "lib32/pkgconfig")
  356. endif()
  357. get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
  358. if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
  359. list(APPEND _lib_dirs "lib64/pkgconfig")
  360. endif()
  361. get_property(uselibx32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIBX32_PATHS)
  362. if(uselibx32 AND CMAKE_INTERNAL_PLATFORM_ABI STREQUAL "ELF X32")
  363. list(APPEND _lib_dirs "libx32/pkgconfig")
  364. endif()
  365. endif()
  366. endif()
  367. if(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" AND NOT CMAKE_CROSSCOMPILING)
  368. list(APPEND _lib_dirs "libdata/pkgconfig")
  369. endif()
  370. list(APPEND _lib_dirs "lib/pkgconfig")
  371. list(APPEND _lib_dirs "share/pkgconfig")
  372. # Check if directories exist and eventually append them to the
  373. # pkgconfig path list
  374. foreach(_prefix_dir ${_extra_paths})
  375. foreach(_lib_dir ${_lib_dirs})
  376. if(EXISTS "${_prefix_dir}/${_lib_dir}")
  377. list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
  378. list(REMOVE_DUPLICATES _pkgconfig_path)
  379. endif()
  380. endforeach()
  381. endforeach()
  382. # Prepare and set the environment variable
  383. if(NOT _pkgconfig_path STREQUAL "")
  384. # remove empty values from the list
  385. list(REMOVE_ITEM _pkgconfig_path "")
  386. file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  387. if(CMAKE_HOST_UNIX)
  388. string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
  389. string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
  390. endif()
  391. set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
  392. endif()
  393. # Unset variables
  394. unset(_lib_dirs)
  395. unset(_pkgconfig_path)
  396. endif()
  397. # Tell pkg-config not to strip any -I or -L paths so we can search them all.
  398. if(DEFINED ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
  399. set(_pkgconfig_allow_system_libs_old "$ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS}")
  400. else()
  401. unset(_pkgconfig_allow_system_libs_old)
  402. endif()
  403. set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} 1)
  404. if(DEFINED ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS})
  405. set(_pkgconfig_allow_system_cflags_old "$ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS}")
  406. else()
  407. unset(_pkgconfig_allow_system_cflags_old)
  408. endif()
  409. set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} 1)
  410. endmacro()
  411. macro(_pkg_restore_path_internal)
  412. if(NOT _extra_paths STREQUAL "")
  413. # Restore the environment variable
  414. set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
  415. endif()
  416. if(DEFINED _pkgconfig_allow_system_libs_old)
  417. set(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS} "${_pkgconfig_allow_system_libs_old}")
  418. unset(_pkgconfig_allow_system_libs_old)
  419. else()
  420. unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_LIBS})
  421. endif()
  422. if(DEFINED _pkgconfig_allow_system_cflags_old)
  423. set(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS} "${_pkgconfig_allow_system_cflags_old}")
  424. unset(_pkgconfig_allow_system_cflags_old)
  425. else()
  426. unset(ENV{PKG_CONFIG_ALLOW_SYSTEM_CFLAGS})
  427. endif()
  428. unset(_extra_paths)
  429. unset(_pkgconfig_path_old)
  430. endmacro()
  431. # pkg-config returns frameworks in --libs-only-other
  432. # they need to be in ${_prefix}_LIBRARIES so "-framework a -framework b" does
  433. # not incorrectly be combined to "-framework a b"
  434. function(_pkgconfig_extract_frameworks _prefix)
  435. set(ldflags "${${_prefix}_LDFLAGS_OTHER}")
  436. list(FIND ldflags "-framework" FR_POS)
  437. list(LENGTH ldflags LD_LENGTH)
  438. # reduce length by 1 as we need "-framework" and the next entry
  439. math(EXPR LD_LENGTH "${LD_LENGTH} - 1")
  440. while (FR_POS GREATER -1 AND LD_LENGTH GREATER FR_POS)
  441. list(REMOVE_AT ldflags ${FR_POS})
  442. list(GET ldflags ${FR_POS} HEAD)
  443. list(REMOVE_AT ldflags ${FR_POS})
  444. math(EXPR LD_LENGTH "${LD_LENGTH} - 2")
  445. list(APPEND LIBS "-framework ${HEAD}")
  446. list(FIND ldflags "-framework" FR_POS)
  447. endwhile ()
  448. set(${_prefix}_LIBRARIES ${${_prefix}_LIBRARIES} ${LIBS} PARENT_SCOPE)
  449. set(${_prefix}_LDFLAGS_OTHER "${ldflags}" PARENT_SCOPE)
  450. endfunction()
  451. # pkg-config returns -isystem include directories in --cflags-only-other,
  452. # depending on the version and if there is a space between -isystem and
  453. # the actual path
  454. function(_pkgconfig_extract_isystem _prefix)
  455. set(cflags "${${_prefix}_CFLAGS_OTHER}")
  456. set(outflags "")
  457. set(incdirs "${${_prefix}_INCLUDE_DIRS}")
  458. set(next_is_isystem FALSE)
  459. foreach (THING IN LISTS cflags)
  460. # This may filter "-isystem -isystem". That would not work anyway,
  461. # so let it happen.
  462. if (THING STREQUAL "-isystem")
  463. set(next_is_isystem TRUE)
  464. continue()
  465. endif ()
  466. if (next_is_isystem)
  467. set(next_is_isystem FALSE)
  468. list(APPEND incdirs "${THING}")
  469. elseif (THING MATCHES "^-isystem")
  470. string(SUBSTRING "${THING}" 8 -1 THING)
  471. list(APPEND incdirs "${THING}")
  472. else ()
  473. list(APPEND outflags "${THING}")
  474. endif ()
  475. endforeach ()
  476. set(${_prefix}_CFLAGS_OTHER "${outflags}" PARENT_SCOPE)
  477. set(${_prefix}_INCLUDE_DIRS "${incdirs}" PARENT_SCOPE)
  478. endfunction()
  479. ###
  480. macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _imp_target_global _prefix)
  481. _pkgconfig_unset(${_prefix}_FOUND)
  482. _pkgconfig_unset(${_prefix}_VERSION)
  483. _pkgconfig_unset(${_prefix}_PREFIX)
  484. _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  485. _pkgconfig_unset(${_prefix}_LIBDIR)
  486. _pkgconfig_unset(${_prefix}_MODULE_NAME)
  487. _pkgconfig_unset(${_prefix}_LIBS)
  488. _pkgconfig_unset(${_prefix}_LIBS_L)
  489. _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  490. _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  491. _pkgconfig_unset(${_prefix}_CFLAGS)
  492. _pkgconfig_unset(${_prefix}_CFLAGS_I)
  493. _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  494. _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  495. _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  496. _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  497. _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  498. _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  499. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  500. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  501. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
  502. # create a better addressable variable of the modules and calculate its size
  503. set(_pkg_check_modules_list ${ARGN})
  504. list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
  505. if(PKG_CONFIG_EXECUTABLE)
  506. # give out status message telling checked module
  507. if (NOT ${_is_silent})
  508. if (_pkg_check_modules_cnt EQUAL 1)
  509. message(STATUS "Checking for module '${_pkg_check_modules_list}'")
  510. else()
  511. message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
  512. endif()
  513. endif()
  514. set(_pkg_check_modules_packages)
  515. set(_pkg_check_modules_failed "")
  516. _pkg_set_path_internal()
  517. # iterate through module list and check whether they exist and match the required version
  518. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
  519. set(_pkg_check_modules_exist_query)
  520. # check whether version is given while ignoring whitespace
  521. if (_pkg_check_modules_pkg MATCHES "(.*[^>< \t])[ \t]*(=|[><]=?)[ \t]*(.*)")
  522. set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
  523. set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
  524. set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
  525. else()
  526. set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  527. set(_pkg_check_modules_pkg_op)
  528. set(_pkg_check_modules_pkg_ver)
  529. endif()
  530. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
  531. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
  532. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
  533. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
  534. list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
  535. # create the final query which is of the format:
  536. # * <pkg-name> > <version>
  537. # * <pkg-name> >= <version>
  538. # * <pkg-name> = <version>
  539. # * <pkg-name> <= <version>
  540. # * <pkg-name> < <version>
  541. # * --exists <pkg-name>
  542. list(APPEND _pkg_check_modules_exist_query --print-errors --short-errors)
  543. if (_pkg_check_modules_pkg_op)
  544. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name} ${_pkg_check_modules_pkg_op} ${_pkg_check_modules_pkg_ver}")
  545. else()
  546. list(APPEND _pkg_check_modules_exist_query --exists)
  547. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
  548. endif()
  549. # execute the query
  550. execute_process(
  551. COMMAND ${PKG_CONFIG_EXECUTABLE} ${PKG_CONFIG_ARGN} ${_pkg_check_modules_exist_query}
  552. RESULT_VARIABLE _pkgconfig_retval
  553. ERROR_VARIABLE _pkgconfig_error
  554. ERROR_STRIP_TRAILING_WHITESPACE)
  555. # evaluate result and tell failures
  556. if (_pkgconfig_retval)
  557. if(NOT ${_is_silent})
  558. message(STATUS " ${_pkgconfig_error}")
  559. endif()
  560. string(APPEND _pkg_check_modules_failed " - ${_pkg_check_modules_pkg}\n")
  561. endif()
  562. endforeach()
  563. if(_pkg_check_modules_failed)
  564. # fail when requested
  565. if (${_is_required})
  566. message(FATAL_ERROR "The following required packages were not found:\n${_pkg_check_modules_failed}")
  567. endif ()
  568. else()
  569. # when we are here, we checked whether requested modules
  570. # exist. Now, go through them and set variables
  571. _pkgconfig_set(${_prefix}_FOUND 1)
  572. list(LENGTH _pkg_check_modules_packages pkg_count)
  573. # iterate through all modules again and set individual variables
  574. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
  575. # handle case when there is only one package required
  576. if (pkg_count EQUAL 1)
  577. set(_pkg_check_prefix "${_prefix}")
  578. else()
  579. set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
  580. endif()
  581. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
  582. pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
  583. pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
  584. pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
  585. foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
  586. _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
  587. endforeach ()
  588. _pkgconfig_set("${_pkg_check_prefix}_MODULE_NAME" "${_pkg_check_modules_pkg}")
  589. if (NOT ${_is_silent})
  590. message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
  591. endif ()
  592. endforeach()
  593. # set variables which are combined for multiple modules
  594. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
  595. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
  596. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
  597. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
  598. if (APPLE AND "-framework" IN_LIST ${_prefix}_LDFLAGS_OTHER)
  599. _pkgconfig_extract_frameworks("${_prefix}")
  600. # Using _pkgconfig_set in this scope so that a future policy can switch to normal variables
  601. _pkgconfig_set("${_pkg_check_prefix}_LIBRARIES" "${${_pkg_check_prefix}_LIBRARIES}")
  602. _pkgconfig_set("${_pkg_check_prefix}_LDFLAGS_OTHER" "${${_pkg_check_prefix}_LDFLAGS_OTHER}")
  603. endif()
  604. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )(-I|-isystem ?)" --cflags-only-I )
  605. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
  606. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
  607. if (${_prefix}_CFLAGS_OTHER MATCHES "-isystem")
  608. _pkgconfig_extract_isystem("${_prefix}")
  609. # Using _pkgconfig_set in this scope so that a future policy can switch to normal variables
  610. _pkgconfig_set("${_pkg_check_prefix}_CFLAGS_OTHER" "${${_pkg_check_prefix}_CFLAGS_OTHER}")
  611. _pkgconfig_set("${_pkg_check_prefix}_INCLUDE_DIRS" "${${_pkg_check_prefix}_INCLUDE_DIRS}")
  612. endif ()
  613. _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
  614. endif()
  615. _pkg_restore_path_internal()
  616. else()
  617. if (${_is_required})
  618. message(SEND_ERROR "pkg-config tool not found")
  619. endif ()
  620. endif()
  621. endmacro()
  622. #[========================================[.rst:
  623. .. command:: pkg_check_modules
  624. Checks for all the given modules, setting a variety of result variables in
  625. the calling scope.
  626. .. code-block:: cmake
  627. pkg_check_modules(<prefix>
  628. [REQUIRED] [QUIET]
  629. [NO_CMAKE_PATH]
  630. [NO_CMAKE_ENVIRONMENT_PATH]
  631. [IMPORTED_TARGET [GLOBAL]]
  632. <moduleSpec> [<moduleSpec>...])
  633. When the ``REQUIRED`` argument is given, the command will fail with an error
  634. if module(s) could not be found.
  635. When the ``QUIET`` argument is given, no status messages will be printed.
  636. .. versionadded:: 3.3
  637. The :variable:`CMAKE_PREFIX_PATH`,
  638. :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH` cache
  639. and environment variables will be added to the ``pkg-config`` search path.
  640. The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
  641. disable this behavior for the cache variables and environment variables
  642. respectively.
  643. The :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` variable set to ``FALSE``
  644. disables this behavior globally.
  645. .. This was actually added in 3.1, but didn't work until 3.3.
  646. .. versionadded:: 3.7
  647. The ``IMPORTED_TARGET`` argument will create an imported target named
  648. ``PkgConfig::<prefix>`` that can be passed directly as an argument to
  649. :command:`target_link_libraries`.
  650. .. This was actually added in 3.6, but didn't work until 3.7.
  651. .. versionadded:: 3.13
  652. The ``GLOBAL`` argument will make the
  653. imported target available in global scope.
  654. .. versionadded:: 3.15
  655. Non-library linker options reported by ``pkg-config`` are stored in the
  656. :prop_tgt:`INTERFACE_LINK_OPTIONS` target property.
  657. .. versionchanged:: 3.18
  658. Include directories specified with ``-isystem`` are stored in the
  659. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` target property. Previous
  660. versions of CMake left them in the :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
  661. property.
  662. Each ``<moduleSpec>`` can be either a bare module name or it can be a
  663. module name with a version constraint (operators ``=``, ``<``, ``>``,
  664. ``<=`` and ``>=`` are supported). The following are examples for a module
  665. named ``foo`` with various constraints:
  666. - ``foo`` matches any version.
  667. - ``foo<2`` only matches versions before 2.
  668. - ``foo>=3.1`` matches any version from 3.1 or later.
  669. - ``foo=1.2.3`` requires that foo must be exactly version 1.2.3.
  670. The following variables may be set upon return. Two sets of values exist:
  671. One for the common case (``<XXX> = <prefix>``) and another for the
  672. information ``pkg-config`` provides when called with the ``--static``
  673. option (``<XXX> = <prefix>_STATIC``).
  674. ``<XXX>_FOUND``
  675. set to 1 if module(s) exist
  676. ``<XXX>_LIBRARIES``
  677. only the libraries (without the '-l')
  678. ``<XXX>_LINK_LIBRARIES``
  679. the libraries and their absolute paths
  680. ``<XXX>_LIBRARY_DIRS``
  681. the paths of the libraries (without the '-L')
  682. ``<XXX>_LDFLAGS``
  683. all required linker flags
  684. ``<XXX>_LDFLAGS_OTHER``
  685. all other linker flags
  686. ``<XXX>_INCLUDE_DIRS``
  687. the '-I' preprocessor flags (without the '-I')
  688. ``<XXX>_CFLAGS``
  689. all required cflags
  690. ``<XXX>_CFLAGS_OTHER``
  691. the other compiler flags
  692. All but ``<XXX>_FOUND`` may be a :ref:`;-list <CMake Language Lists>` if the
  693. associated variable returned from ``pkg-config`` has multiple values.
  694. .. versionchanged:: 3.18
  695. Include directories specified with ``-isystem`` are stored in the
  696. ``<XXX>_INCLUDE_DIRS`` variable. Previous versions of CMake left them
  697. in ``<XXX>_CFLAGS_OTHER``.
  698. There are some special variables whose prefix depends on the number of
  699. ``<moduleSpec>`` given. When there is only one ``<moduleSpec>``,
  700. ``<YYY>`` will simply be ``<prefix>``, but if two or more ``<moduleSpec>``
  701. items are given, ``<YYY>`` will be ``<prefix>_<moduleName>``.
  702. ``<YYY>_VERSION``
  703. version of the module
  704. ``<YYY>_PREFIX``
  705. prefix directory of the module
  706. ``<YYY>_INCLUDEDIR``
  707. include directory of the module
  708. ``<YYY>_LIBDIR``
  709. lib directory of the module
  710. .. versionchanged:: 3.8
  711. For any given ``<prefix>``, ``pkg_check_modules()`` can be called multiple
  712. times with different parameters. Previous versions of CMake cached and
  713. returned the first successful result.
  714. .. versionchanged:: 3.16
  715. If a full path to the found library can't be determined, but it's still
  716. visible to the linker, pass it through as ``-l<name>``. Previous versions
  717. of CMake failed in this case.
  718. Examples:
  719. .. code-block:: cmake
  720. pkg_check_modules (GLIB2 glib-2.0)
  721. Looks for any version of glib2. If found, the output variable
  722. ``GLIB2_VERSION`` will hold the actual version found.
  723. .. code-block:: cmake
  724. pkg_check_modules (GLIB2 glib-2.0>=2.10)
  725. Looks for at least version 2.10 of glib2. If found, the output variable
  726. ``GLIB2_VERSION`` will hold the actual version found.
  727. .. code-block:: cmake
  728. pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
  729. Looks for both glib2-2.0 (at least version 2.10) and any version of
  730. gtk2+-2.0. Only if both are found will ``FOO`` be considered found.
  731. The ``FOO_glib-2.0_VERSION`` and ``FOO_gtk+-2.0_VERSION`` variables will be
  732. set to their respective found module versions.
  733. .. code-block:: cmake
  734. pkg_check_modules (XRENDER REQUIRED xrender)
  735. Requires any version of ``xrender``. Example output variables set by a
  736. successful call::
  737. XRENDER_LIBRARIES=Xrender;X11
  738. XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
  739. #]========================================]
  740. macro(pkg_check_modules _prefix _module0)
  741. _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})
  742. # check cached value
  743. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND OR
  744. (NOT "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0};${ARGN}") OR
  745. ( "${ARGN}" STREQUAL "" AND NOT "${__pkg_config_arguments_${_prefix}}" STREQUAL "${_module0}"))
  746. _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})
  747. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  748. if (${_prefix}_FOUND)
  749. _pkgconfig_set(__pkg_config_arguments_${_prefix} "${_module0};${ARGN}")
  750. endif()
  751. else()
  752. if (${_prefix}_FOUND)
  753. _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
  754. endif()
  755. endif()
  756. endmacro()
  757. #[========================================[.rst:
  758. .. command:: pkg_search_module
  759. The behavior of this command is the same as :command:`pkg_check_modules`,
  760. except that rather than checking for all the specified modules, it searches
  761. for just the first successful match.
  762. .. code-block:: cmake
  763. pkg_search_module(<prefix>
  764. [REQUIRED] [QUIET]
  765. [NO_CMAKE_PATH]
  766. [NO_CMAKE_ENVIRONMENT_PATH]
  767. [IMPORTED_TARGET [GLOBAL]]
  768. <moduleSpec> [<moduleSpec>...])
  769. .. versionadded:: 3.16
  770. If a module is found, the ``<prefix>_MODULE_NAME`` variable will contain the
  771. name of the matching module. This variable can be used if you need to run
  772. :command:`pkg_get_variable`.
  773. Example:
  774. .. code-block:: cmake
  775. pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
  776. #]========================================]
  777. macro(pkg_search_module _prefix _module0)
  778. _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})
  779. # check cached value
  780. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  781. set(_pkg_modules_found 0)
  782. if (NOT ${_pkg_is_silent})
  783. message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
  784. endif ()
  785. # iterate through all modules and stop at the first working one.
  786. foreach(_pkg_alt ${_pkg_modules_alt})
  787. if(NOT _pkg_modules_found)
  788. _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global} "${_prefix}" "${_pkg_alt}")
  789. endif()
  790. if (${_prefix}_FOUND)
  791. set(_pkg_modules_found 1)
  792. break()
  793. endif()
  794. endforeach()
  795. if (NOT ${_prefix}_FOUND)
  796. if(${_pkg_is_required})
  797. message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
  798. endif()
  799. endif()
  800. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  801. elseif (${_prefix}_FOUND)
  802. _pkg_recalculate("${_prefix}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} ${_imp_target_global})
  803. endif()
  804. endmacro()
  805. #[========================================[.rst:
  806. .. command:: pkg_get_variable
  807. .. versionadded:: 3.4
  808. Retrieves the value of a pkg-config variable ``varName`` and stores it in the
  809. result variable ``resultVar`` in the calling scope.
  810. .. code-block:: cmake
  811. pkg_get_variable(<resultVar> <moduleName> <varName>
  812. [DEFINE_VARIABLES <key>=<value>...])
  813. If ``pkg-config`` returns multiple values for the specified variable,
  814. ``resultVar`` will contain a :ref:`;-list <CMake Language Lists>`.
  815. Options:
  816. ``DEFINE_VARIABLES <key>=<value>...``
  817. .. versionadded:: 3.28
  818. Specify key-value pairs to redefine variables affecting the variable
  819. retrieved with ``pkg-config``.
  820. For example:
  821. .. code-block:: cmake
  822. pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
  823. #]========================================]
  824. function (pkg_get_variable result pkg variable)
  825. set(_multiValueArgs DEFINE_VARIABLES)
  826. cmake_parse_arguments(_parsedArguments "" "" "${_multiValueArgs}" ${ARGN})
  827. set(defined_variables )
  828. foreach(_def_var ${_parsedArguments_DEFINE_VARIABLES})
  829. if(NOT _def_var MATCHES "^.+=.*$")
  830. message(FATAL_ERROR "DEFINE_VARIABLES should contain arguments in the form of key=value")
  831. endif()
  832. list(APPEND defined_variables "--define-variable=${_def_var}")
  833. endforeach()
  834. _pkg_set_path_internal()
  835. _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}" ${defined_variables})
  836. set("${result}"
  837. "${prefix_result}"
  838. PARENT_SCOPE)
  839. _pkg_restore_path_internal()
  840. endfunction ()
  841. #[========================================[.rst:
  842. Variables Affecting Behavior
  843. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  844. .. variable:: PKG_CONFIG_EXECUTABLE
  845. This cache variable can be set to the path of the pkg-config executable.
  846. :command:`find_program` is called internally by the module with this
  847. variable.
  848. .. versionadded:: 3.1
  849. The ``PKG_CONFIG`` environment variable can be used as a hint if
  850. ``PKG_CONFIG_EXECUTABLE`` has not yet been set.
  851. .. versionchanged:: 3.22
  852. If the ``PKG_CONFIG`` environment variable is set, only the first
  853. argument is taken from it when using it as a hint.
  854. .. variable:: PKG_CONFIG_ARGN
  855. .. versionadded:: 3.22
  856. This cache variable can be set to a list of arguments to additionally pass
  857. to pkg-config if needed. If not provided, it will be initialized from the
  858. ``PKG_CONFIG`` environment variable, if set. The first argument in that
  859. environment variable is assumed to be the pkg-config program, while all
  860. remaining arguments after that are used to initialize ``PKG_CONFIG_ARGN``.
  861. If no such environment variable is defined, ``PKG_CONFIG_ARGN`` is
  862. initialized to an empty string. The module does not update the variable once
  863. it has been set in the cache.
  864. .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
  865. .. versionadded:: 3.1
  866. Specifies whether :command:`pkg_check_modules` and
  867. :command:`pkg_search_module` should add the paths in the
  868. :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH` and
  869. :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables to the
  870. ``pkg-config`` search path.
  871. If this variable is not set, this behavior is enabled by default if
  872. :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
  873. otherwise.
  874. #]========================================]
  875. ### Local Variables:
  876. ### mode: cmake
  877. ### End: