FindPkgConfig.cmake 37 KB

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