FindPkgConfig.cmake 39 KB

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