FindPkgConfig.cmake 27 KB

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