FindPkgConfig.cmake 28 KB

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