FindPkgConfig.cmake 28 KB

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