FindPkgConfig.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #.rst:
  2. # FindPkgConfig
  3. # -------------
  4. #
  5. # A `pkg-config` module for CMake.
  6. #
  7. # Finds the ``pkg-config`` executable and add the
  8. # :command:`pkg_check_modules` and :command:`pkg_search_module`
  9. # commands.
  10. #
  11. # In order to find the ``pkg-config`` executable, it uses the
  12. # :variable:`PKG_CONFIG_EXECUTABLE` variable or the ``PKG_CONFIG``
  13. # environment variable first.
  14. #=============================================================================
  15. # Copyright 2006-2014 Kitware, Inc.
  16. # Copyright 2014 Christoph Grüninger <[email protected]>
  17. # Copyright 2006 Enrico Scholz <[email protected]>
  18. # Copyright 2016 Rolf Eike Beer <[email protected]>
  19. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distribute this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. ### Common stuff ####
  30. set(PKG_CONFIG_VERSION 1)
  31. # find pkg-config, use PKG_CONFIG if set
  32. if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
  33. set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable")
  34. endif()
  35. find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
  36. mark_as_advanced(PKG_CONFIG_EXECUTABLE)
  37. if (PKG_CONFIG_EXECUTABLE)
  38. execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version
  39. OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING
  40. ERROR_QUIET
  41. OUTPUT_STRIP_TRAILING_WHITESPACE)
  42. endif ()
  43. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  44. find_package_handle_standard_args(PkgConfig
  45. REQUIRED_VARS PKG_CONFIG_EXECUTABLE
  46. VERSION_VAR PKG_CONFIG_VERSION_STRING)
  47. # This is needed because the module name is "PkgConfig" but the name of
  48. # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
  49. # handled by FPHSA.
  50. set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
  51. # Unsets the given variables
  52. macro(_pkgconfig_unset var)
  53. set(${var} "" CACHE INTERNAL "")
  54. endmacro()
  55. macro(_pkgconfig_set var value)
  56. set(${var} ${value} CACHE INTERNAL "")
  57. endmacro()
  58. # Invokes pkgconfig, cleans up the result and sets variables
  59. macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
  60. set(_pkgconfig_invoke_result)
  61. execute_process(
  62. COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
  63. OUTPUT_VARIABLE _pkgconfig_invoke_result
  64. RESULT_VARIABLE _pkgconfig_failed
  65. OUTPUT_STRIP_TRAILING_WHITESPACE)
  66. if (_pkgconfig_failed)
  67. set(_pkgconfig_${_varname} "")
  68. _pkgconfig_unset(${_prefix}_${_varname})
  69. else()
  70. string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  71. if (NOT ${_regexp} STREQUAL "")
  72. string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  73. endif()
  74. separate_arguments(_pkgconfig_invoke_result)
  75. #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
  76. set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
  77. _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
  78. endif()
  79. endmacro()
  80. #[========================================[.rst:
  81. .. command:: pkg_get_variable
  82. Retrieves the value of a variable from a package::
  83. pkg_get_variable(<RESULT> <MODULE> <VARIABLE>)
  84. For example:
  85. .. code-block:: cmake
  86. pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
  87. #]========================================]
  88. function (pkg_get_variable result pkg variable)
  89. _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
  90. set("${result}"
  91. "${prefix_result}"
  92. PARENT_SCOPE)
  93. endfunction ()
  94. # Invokes pkgconfig two times; once without '--static' and once with
  95. # '--static'
  96. macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
  97. _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
  98. _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
  99. endmacro()
  100. # Splits given arguments into options and a package list
  101. macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target)
  102. set(${_is_req} 0)
  103. set(${_is_silent} 0)
  104. set(${_no_cmake_path} 0)
  105. set(${_no_cmake_environment_path} 0)
  106. set(${_imp_target} 0)
  107. if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  108. if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  109. set(${_no_cmake_path} 1)
  110. set(${_no_cmake_environment_path} 1)
  111. endif()
  112. elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
  113. set(${_no_cmake_path} 1)
  114. set(${_no_cmake_environment_path} 1)
  115. endif()
  116. foreach(_pkg ${ARGN})
  117. if (_pkg STREQUAL "REQUIRED")
  118. set(${_is_req} 1)
  119. endif ()
  120. if (_pkg STREQUAL "QUIET")
  121. set(${_is_silent} 1)
  122. endif ()
  123. if (_pkg STREQUAL "NO_CMAKE_PATH")
  124. set(${_no_cmake_path} 1)
  125. endif()
  126. if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
  127. set(${_no_cmake_environment_path} 1)
  128. endif()
  129. if (_pkg STREQUAL "IMPORTED_TARGET")
  130. set(${_imp_target} 1)
  131. endif()
  132. endforeach()
  133. set(${_result} ${ARGN})
  134. list(REMOVE_ITEM ${_result} "REQUIRED")
  135. list(REMOVE_ITEM ${_result} "QUIET")
  136. list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
  137. list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
  138. list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
  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, and create an imported target from them
  167. function(_pkg_create_imp_target _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. set(_find_opts "NO_CMAKE_PATH")
  174. endif()
  175. if(_no_cmake_environment_path)
  176. set(_find_opts "${_find_opts} NO_CMAKE_ENVIRONMENT_PATH")
  177. endif()
  178. foreach (flag IN LISTS ${_prefix}_LDFLAGS)
  179. if (flag MATCHES "^-L(.*)")
  180. # only look into the given paths from now on
  181. set(_find_opts "HINTS ${${CMAKE_MATCH_1}} NO_DEFAULT_PATH")
  182. continue()
  183. endif()
  184. if (flag MATCHES "^-l(.*)")
  185. set(_pkg_search "${CMAKE_MATCH_1}")
  186. else()
  187. continue()
  188. endif()
  189. find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
  190. NAMES ${_pkg_search}
  191. ${_find_opts})
  192. list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
  193. endforeach()
  194. # only create the target if it is linkable, i.e. no executables
  195. if (NOT TARGET PkgConfig::${_prefix}
  196. AND ( ${_prefix}_INCLUDE_DIRS OR _libs OR ${_prefix}_CFLAGS_OTHER ))
  197. add_library(PkgConfig::${_prefix} INTERFACE IMPORTED)
  198. unset(_props)
  199. if(${_prefix}_INCLUDE_DIRS)
  200. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  201. INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
  202. endif()
  203. if(_libs)
  204. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  205. INTERFACE_LINK_LIBRARIES "${_libs}")
  206. endif()
  207. if(${_prefix}_CFLAGS_OTHER)
  208. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  209. INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
  210. endif()
  211. endif()
  212. endfunction()
  213. ###
  214. macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _prefix)
  215. _pkgconfig_unset(${_prefix}_FOUND)
  216. _pkgconfig_unset(${_prefix}_VERSION)
  217. _pkgconfig_unset(${_prefix}_PREFIX)
  218. _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  219. _pkgconfig_unset(${_prefix}_LIBDIR)
  220. _pkgconfig_unset(${_prefix}_LIBS)
  221. _pkgconfig_unset(${_prefix}_LIBS_L)
  222. _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  223. _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  224. _pkgconfig_unset(${_prefix}_CFLAGS)
  225. _pkgconfig_unset(${_prefix}_CFLAGS_I)
  226. _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  227. _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  228. _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  229. _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  230. _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  231. _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  232. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  233. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  234. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
  235. # create a better addressable variable of the modules and calculate its size
  236. set(_pkg_check_modules_list ${ARGN})
  237. list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
  238. if(PKG_CONFIG_EXECUTABLE)
  239. # give out status message telling checked module
  240. if (NOT ${_is_silent})
  241. if (_pkg_check_modules_cnt EQUAL 1)
  242. message(STATUS "Checking for module '${_pkg_check_modules_list}'")
  243. else()
  244. message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
  245. endif()
  246. endif()
  247. set(_pkg_check_modules_packages)
  248. set(_pkg_check_modules_failed)
  249. set(_extra_paths)
  250. if(NOT _no_cmake_path)
  251. _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
  252. _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
  253. _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
  254. endif()
  255. if(NOT _no_cmake_environment_path)
  256. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
  257. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
  258. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
  259. endif()
  260. if(NOT "${_extra_paths}" STREQUAL "")
  261. # Save the PKG_CONFIG_PATH environment variable, and add paths
  262. # from the CMAKE_PREFIX_PATH variables
  263. set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
  264. set(_pkgconfig_path "${_pkgconfig_path_old}")
  265. if(NOT "${_pkgconfig_path}" STREQUAL "")
  266. file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  267. endif()
  268. # Create a list of the possible pkgconfig subfolder (depending on
  269. # the system
  270. set(_lib_dirs)
  271. if(NOT DEFINED CMAKE_SYSTEM_NAME
  272. OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
  273. AND NOT CMAKE_CROSSCOMPILING))
  274. if(EXISTS "/etc/debian_version") # is this a debian system ?
  275. if(CMAKE_LIBRARY_ARCHITECTURE)
  276. list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
  277. endif()
  278. else()
  279. # not debian, check the FIND_LIBRARY_USE_LIB32_PATHS and FIND_LIBRARY_USE_LIB64_PATHS properties
  280. get_property(uselib32 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS)
  281. if(uselib32 AND CMAKE_SIZEOF_VOID_P EQUAL 4)
  282. list(APPEND _lib_dirs "lib32/pkgconfig")
  283. endif()
  284. get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
  285. if(uselib64 AND CMAKE_SIZEOF_VOID_P EQUAL 8)
  286. list(APPEND _lib_dirs "lib64/pkgconfig")
  287. endif()
  288. endif()
  289. endif()
  290. list(APPEND _lib_dirs "lib/pkgconfig")
  291. list(APPEND _lib_dirs "share/pkgconfig")
  292. # Check if directories exist and eventually append them to the
  293. # pkgconfig path list
  294. foreach(_prefix_dir ${_extra_paths})
  295. foreach(_lib_dir ${_lib_dirs})
  296. if(EXISTS "${_prefix_dir}/${_lib_dir}")
  297. list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
  298. list(REMOVE_DUPLICATES _pkgconfig_path)
  299. endif()
  300. endforeach()
  301. endforeach()
  302. # Prepare and set the environment variable
  303. if(NOT "${_pkgconfig_path}" STREQUAL "")
  304. # remove empty values from the list
  305. list(REMOVE_ITEM _pkgconfig_path "")
  306. file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  307. if(UNIX)
  308. string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
  309. string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
  310. endif()
  311. set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
  312. endif()
  313. # Unset variables
  314. unset(_lib_dirs)
  315. unset(_pkgconfig_path)
  316. endif()
  317. # iterate through module list and check whether they exist and match the required version
  318. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
  319. set(_pkg_check_modules_exist_query)
  320. # check whether version is given
  321. if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
  322. set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
  323. set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
  324. set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
  325. else()
  326. set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  327. set(_pkg_check_modules_pkg_op)
  328. set(_pkg_check_modules_pkg_ver)
  329. endif()
  330. # handle the operands
  331. if (_pkg_check_modules_pkg_op STREQUAL ">=")
  332. list(APPEND _pkg_check_modules_exist_query --atleast-version)
  333. endif()
  334. if (_pkg_check_modules_pkg_op STREQUAL "=")
  335. list(APPEND _pkg_check_modules_exist_query --exact-version)
  336. endif()
  337. if (_pkg_check_modules_pkg_op STREQUAL "<=")
  338. list(APPEND _pkg_check_modules_exist_query --max-version)
  339. endif()
  340. # create the final query which is of the format:
  341. # * --atleast-version <version> <pkg-name>
  342. # * --exact-version <version> <pkg-name>
  343. # * --max-version <version> <pkg-name>
  344. # * --exists <pkg-name>
  345. if (_pkg_check_modules_pkg_op)
  346. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
  347. else()
  348. list(APPEND _pkg_check_modules_exist_query --exists --print-errors --short-errors)
  349. endif()
  350. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
  351. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
  352. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
  353. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
  354. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
  355. list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
  356. # execute the query
  357. execute_process(
  358. COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
  359. RESULT_VARIABLE _pkgconfig_retval
  360. ERROR_VARIABLE _pkgconfig_error
  361. ERROR_STRIP_TRAILING_WHITESPACE)
  362. # evaluate result and tell failures
  363. if (_pkgconfig_retval)
  364. if(NOT ${_is_silent})
  365. message(STATUS " ${_pkgconfig_error}")
  366. endif()
  367. set(_pkg_check_modules_failed 1)
  368. endif()
  369. endforeach()
  370. if(_pkg_check_modules_failed)
  371. # fail when requested
  372. if (${_is_required})
  373. message(FATAL_ERROR "A required package was not found")
  374. endif ()
  375. else()
  376. # when we are here, we checked whether requested modules
  377. # exist. Now, go through them and set variables
  378. _pkgconfig_set(${_prefix}_FOUND 1)
  379. list(LENGTH _pkg_check_modules_packages pkg_count)
  380. # iterate through all modules again and set individual variables
  381. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
  382. # handle case when there is only one package required
  383. if (pkg_count EQUAL 1)
  384. set(_pkg_check_prefix "${_prefix}")
  385. else()
  386. set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
  387. endif()
  388. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
  389. pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
  390. pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
  391. pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
  392. foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
  393. _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
  394. endforeach ()
  395. if (NOT ${_is_silent})
  396. message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
  397. endif ()
  398. endforeach()
  399. # set variables which are combined for multiple modules
  400. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
  401. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
  402. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
  403. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
  404. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
  405. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
  406. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
  407. if (_imp_target)
  408. _pkg_create_imp_target("${_prefix}" _no_cmake_path _no_cmake_environment_path)
  409. endif()
  410. endif()
  411. if(NOT "${_extra_paths}" STREQUAL "")
  412. # Restore the environment variable
  413. set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
  414. endif()
  415. unset(_extra_paths)
  416. unset(_pkgconfig_path_old)
  417. else()
  418. if (${_is_required})
  419. message(SEND_ERROR "pkg-config tool not found")
  420. endif ()
  421. endif()
  422. endmacro()
  423. ###
  424. ### User visible macros start here
  425. ###
  426. #[========================================[.rst:
  427. .. command:: pkg_check_modules
  428. Checks for all the given modules. ::
  429. pkg_check_modules(<PREFIX> [REQUIRED] [QUIET]
  430. [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
  431. [IMPORTED_TARGET]
  432. <MODULE> [<MODULE>]*)
  433. When the ``REQUIRED`` argument was set, macros will fail with an error
  434. when module(s) could not be found.
  435. When the ``QUIET`` argument is set, no status messages will be printed.
  436. By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
  437. later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set, the
  438. :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH`, and
  439. :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables will
  440. be added to ``pkg-config`` search path.
  441. The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
  442. disable this behavior for the cache variables and the environment
  443. variables, respectively.
  444. The ``IMPORTED_TARGET`` argument will create an imported target named
  445. PkgConfig::<PREFIX>> that can be passed directly as an argument to
  446. :command:`target_link_libraries`.
  447. It sets the following variables: ::
  448. PKG_CONFIG_FOUND ... if pkg-config executable was found
  449. PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
  450. PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
  451. (since CMake 2.8.8)
  452. For the following variables two sets of values exist; first one is the
  453. common one and has the given PREFIX. The second set contains flags
  454. which are given out when ``pkg-config`` was called with the ``--static``
  455. option. ::
  456. <XPREFIX>_FOUND ... set to 1 if module(s) exist
  457. <XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
  458. <XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
  459. <XPREFIX>_LDFLAGS ... all required linker flags
  460. <XPREFIX>_LDFLAGS_OTHER ... all other linker flags
  461. <XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
  462. <XPREFIX>_CFLAGS ... all required cflags
  463. <XPREFIX>_CFLAGS_OTHER ... the other compiler flags
  464. ::
  465. <XPREFIX> = <PREFIX> for common case
  466. <XPREFIX> = <PREFIX>_STATIC for static linking
  467. There are some special variables whose prefix depends on the count of
  468. given modules. When there is only one module, <PREFIX> stays
  469. unchanged. When there are multiple modules, the prefix will be
  470. changed to <PREFIX>_<MODNAME>: ::
  471. <XPREFIX>_VERSION ... version of the module
  472. <XPREFIX>_PREFIX ... prefix-directory of the module
  473. <XPREFIX>_INCLUDEDIR ... include-dir of the module
  474. <XPREFIX>_LIBDIR ... lib-dir of the module
  475. ::
  476. <XPREFIX> = <PREFIX> when |MODULES| == 1, else
  477. <XPREFIX> = <PREFIX>_<MODNAME>
  478. A <MODULE> parameter can have the following formats: ::
  479. {MODNAME} ... matches any version
  480. {MODNAME}>={VERSION} ... at least version <VERSION> is required
  481. {MODNAME}={VERSION} ... exactly version <VERSION> is required
  482. {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>
  483. Examples
  484. .. code-block:: cmake
  485. pkg_check_modules (GLIB2 glib-2.0)
  486. .. code-block:: cmake
  487. pkg_check_modules (GLIB2 glib-2.0>=2.10)
  488. Requires at least version 2.10 of glib2 and defines e.g.
  489. ``GLIB2_VERSION=2.10.3``
  490. .. code-block:: cmake
  491. pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
  492. Requires both glib2 and gtk2, and defines e.g.
  493. ``FOO_glib-2.0_VERSION=2.10.3`` and ``FOO_gtk+-2.0_VERSION=2.8.20``
  494. .. code-block:: cmake
  495. pkg_check_modules (XRENDER REQUIRED xrender)
  496. Defines for example::
  497. XRENDER_LIBRARIES=Xrender;X11``
  498. XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
  499. #]========================================]
  500. macro(pkg_check_modules _prefix _module0)
  501. # check cached value
  502. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  503. _pkgconfig_parse_options (_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
  504. _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" ${_pkg_modules})
  505. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  506. endif()
  507. endmacro()
  508. #[========================================[.rst:
  509. .. command:: pkg_search_module
  510. Same as :command:`pkg_check_modules`, but instead it checks for given
  511. modules and uses the first working one. ::
  512. pkg_search_module(<PREFIX> [REQUIRED] [QUIET]
  513. [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
  514. [IMPORTED_TARGET]
  515. <MODULE> [<MODULE>]*)
  516. Examples
  517. .. code-block:: cmake
  518. pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
  519. #]========================================]
  520. macro(pkg_search_module _prefix _module0)
  521. # check cached value
  522. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  523. set(_pkg_modules_found 0)
  524. _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
  525. if (NOT ${_pkg_is_silent})
  526. message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
  527. endif ()
  528. # iterate through all modules and stop at the first working one.
  529. foreach(_pkg_alt ${_pkg_modules_alt})
  530. if(NOT _pkg_modules_found)
  531. _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" "${_pkg_alt}")
  532. endif()
  533. if (${_prefix}_FOUND)
  534. set(_pkg_modules_found 1)
  535. endif()
  536. endforeach()
  537. if (NOT ${_prefix}_FOUND)
  538. if(${_pkg_is_required})
  539. message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
  540. endif()
  541. endif()
  542. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  543. endif()
  544. endmacro()
  545. #[========================================[.rst:
  546. .. variable:: PKG_CONFIG_EXECUTABLE
  547. Path to the pkg-config executable.
  548. .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
  549. Whether :command:`pkg_check_modules` and :command:`pkg_search_module`
  550. should add the paths in :variable:`CMAKE_PREFIX_PATH`,
  551. :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH`
  552. cache and environment variables to ``pkg-config`` search path.
  553. If this variable is not set, this behavior is enabled by default if
  554. :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
  555. otherwise.
  556. #]========================================]
  557. ### Local Variables:
  558. ### mode: cmake
  559. ### End: