FindPkgConfig.cmake 26 KB

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