FindPkgConfig.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. #
  19. # Distributed under the OSI-approved BSD License (the "License");
  20. # see accompanying file Copyright.txt for details.
  21. #
  22. # This software is distributed WITHOUT ANY WARRANTY; without even the
  23. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. # See the License for more information.
  25. #=============================================================================
  26. # (To distribute this file outside of CMake, substitute the full
  27. # License text for the above reference.)
  28. ### Common stuff ####
  29. set(PKG_CONFIG_VERSION 1)
  30. # find pkg-config, use PKG_CONFIG if set
  31. if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
  32. set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable")
  33. endif()
  34. find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
  35. mark_as_advanced(PKG_CONFIG_EXECUTABLE)
  36. if (PKG_CONFIG_EXECUTABLE)
  37. execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version
  38. OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING
  39. ERROR_QUIET
  40. OUTPUT_STRIP_TRAILING_WHITESPACE)
  41. endif ()
  42. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  43. find_package_handle_standard_args(PkgConfig
  44. REQUIRED_VARS PKG_CONFIG_EXECUTABLE
  45. VERSION_VAR PKG_CONFIG_VERSION_STRING)
  46. # This is needed because the module name is "PkgConfig" but the name of
  47. # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
  48. # handled by FPHSA.
  49. set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
  50. # Unsets the given variables
  51. macro(_pkgconfig_unset var)
  52. set(${var} "" CACHE INTERNAL "")
  53. endmacro()
  54. macro(_pkgconfig_set var value)
  55. set(${var} ${value} CACHE INTERNAL "")
  56. endmacro()
  57. # Invokes pkgconfig, cleans up the result and sets variables
  58. macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
  59. set(_pkgconfig_invoke_result)
  60. execute_process(
  61. COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
  62. OUTPUT_VARIABLE _pkgconfig_invoke_result
  63. RESULT_VARIABLE _pkgconfig_failed)
  64. if (_pkgconfig_failed)
  65. set(_pkgconfig_${_varname} "")
  66. _pkgconfig_unset(${_prefix}_${_varname})
  67. else()
  68. string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  69. string(REGEX REPLACE " +$" "" _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  70. if (NOT ${_regexp} STREQUAL "")
  71. string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  72. endif()
  73. separate_arguments(_pkgconfig_invoke_result)
  74. #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
  75. set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
  76. _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
  77. endif()
  78. endmacro()
  79. # Invokes pkgconfig two times; once without '--static' and once with
  80. # '--static'
  81. macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
  82. _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
  83. _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
  84. endmacro()
  85. # Splits given arguments into options and a package list
  86. macro(_pkgconfig_parse_options _result _is_req _is_silent)
  87. set(${_is_req} 0)
  88. set(${_is_silent} 0)
  89. foreach(_pkg ${ARGN})
  90. if (_pkg STREQUAL "REQUIRED")
  91. set(${_is_req} 1)
  92. endif ()
  93. if (_pkg STREQUAL "QUIET")
  94. set(${_is_silent} 1)
  95. endif ()
  96. endforeach()
  97. set(${_result} ${ARGN})
  98. list(REMOVE_ITEM ${_result} "REQUIRED")
  99. list(REMOVE_ITEM ${_result} "QUIET")
  100. endmacro()
  101. ###
  102. macro(_pkg_check_modules_internal _is_required _is_silent _prefix)
  103. _pkgconfig_unset(${_prefix}_FOUND)
  104. _pkgconfig_unset(${_prefix}_VERSION)
  105. _pkgconfig_unset(${_prefix}_PREFIX)
  106. _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  107. _pkgconfig_unset(${_prefix}_LIBDIR)
  108. _pkgconfig_unset(${_prefix}_LIBS)
  109. _pkgconfig_unset(${_prefix}_LIBS_L)
  110. _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  111. _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  112. _pkgconfig_unset(${_prefix}_CFLAGS)
  113. _pkgconfig_unset(${_prefix}_CFLAGS_I)
  114. _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  115. _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  116. _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  117. _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  118. _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  119. _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  120. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  121. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  122. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
  123. # create a better addressable variable of the modules and calculate its size
  124. set(_pkg_check_modules_list ${ARGN})
  125. list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
  126. if(PKG_CONFIG_EXECUTABLE)
  127. # give out status message telling checked module
  128. if (NOT ${_is_silent})
  129. if (_pkg_check_modules_cnt EQUAL 1)
  130. message(STATUS "checking for module '${_pkg_check_modules_list}'")
  131. else()
  132. message(STATUS "checking for modules '${_pkg_check_modules_list}'")
  133. endif()
  134. endif()
  135. set(_pkg_check_modules_packages)
  136. set(_pkg_check_modules_failed)
  137. # iterate through module list and check whether they exist and match the required version
  138. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
  139. set(_pkg_check_modules_exist_query)
  140. # check whether version is given
  141. if (_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
  142. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\1" _pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  143. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\2" _pkg_check_modules_pkg_op "${_pkg_check_modules_pkg}")
  144. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\3" _pkg_check_modules_pkg_ver "${_pkg_check_modules_pkg}")
  145. else()
  146. set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  147. set(_pkg_check_modules_pkg_op)
  148. set(_pkg_check_modules_pkg_ver)
  149. endif()
  150. # handle the operands
  151. if (_pkg_check_modules_pkg_op STREQUAL ">=")
  152. list(APPEND _pkg_check_modules_exist_query --atleast-version)
  153. endif()
  154. if (_pkg_check_modules_pkg_op STREQUAL "=")
  155. list(APPEND _pkg_check_modules_exist_query --exact-version)
  156. endif()
  157. if (_pkg_check_modules_pkg_op STREQUAL "<=")
  158. list(APPEND _pkg_check_modules_exist_query --max-version)
  159. endif()
  160. # create the final query which is of the format:
  161. # * --atleast-version <version> <pkg-name>
  162. # * --exact-version <version> <pkg-name>
  163. # * --max-version <version> <pkg-name>
  164. # * --exists <pkg-name>
  165. if (_pkg_check_modules_pkg_op)
  166. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
  167. else()
  168. list(APPEND _pkg_check_modules_exist_query --exists)
  169. endif()
  170. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
  171. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
  172. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
  173. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
  174. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
  175. list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
  176. # execute the query
  177. execute_process(
  178. COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
  179. RESULT_VARIABLE _pkgconfig_retval)
  180. # evaluate result and tell failures
  181. if (_pkgconfig_retval)
  182. if(NOT ${_is_silent})
  183. message(STATUS " package '${_pkg_check_modules_pkg}' not found")
  184. endif()
  185. set(_pkg_check_modules_failed 1)
  186. endif()
  187. endforeach()
  188. if(_pkg_check_modules_failed)
  189. # fail when requested
  190. if (${_is_required})
  191. message(SEND_ERROR "A required package was not found")
  192. endif ()
  193. else()
  194. # when we are here, we checked whether requested modules
  195. # exist. Now, go through them and set variables
  196. _pkgconfig_set(${_prefix}_FOUND 1)
  197. list(LENGTH _pkg_check_modules_packages pkg_count)
  198. # iterate through all modules again and set individual variables
  199. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
  200. # handle case when there is only one package required
  201. if (pkg_count EQUAL 1)
  202. set(_pkg_check_prefix "${_prefix}")
  203. else()
  204. set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
  205. endif()
  206. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
  207. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" PREFIX "" --variable=prefix )
  208. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" INCLUDEDIR "" --variable=includedir )
  209. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" LIBDIR "" --variable=libdir )
  210. if (NOT ${_is_silent})
  211. message(STATUS " found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
  212. endif ()
  213. endforeach()
  214. # set variables which are combined for multiple modules
  215. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
  216. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
  217. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
  218. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
  219. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
  220. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
  221. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
  222. endif()
  223. else()
  224. if (${_is_required})
  225. message(SEND_ERROR "pkg-config tool not found")
  226. endif ()
  227. endif()
  228. endmacro()
  229. ###
  230. ### User visible macros start here
  231. ###
  232. #[========================================[.rst:
  233. .. command:: pkg_check_modules
  234. Checks for all the given modules. ::
  235. pkg_check_modules(<PREFIX> [REQUIRED] [QUIET] <MODULE> [<MODULE>]*)
  236. When the ``REQUIRED`` argument was set, macros will fail with an error
  237. when module(s) could not be found.
  238. When the ``QUIET`` argument is set, no status messages will be printed.
  239. It sets the following variables: ::
  240. PKG_CONFIG_FOUND ... if pkg-config executable was found
  241. PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
  242. PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
  243. (since CMake 2.8.8)
  244. For the following variables two sets of values exist; first one is the
  245. common one and has the given PREFIX. The second set contains flags
  246. which are given out when ``pkg-config`` was called with the ``--static``
  247. option. ::
  248. <XPREFIX>_FOUND ... set to 1 if module(s) exist
  249. <XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
  250. <XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
  251. <XPREFIX>_LDFLAGS ... all required linker flags
  252. <XPREFIX>_LDFLAGS_OTHER ... all other linker flags
  253. <XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
  254. <XPREFIX>_CFLAGS ... all required cflags
  255. <XPREFIX>_CFLAGS_OTHER ... the other compiler flags
  256. ::
  257. <XPREFIX> = <PREFIX> for common case
  258. <XPREFIX> = <PREFIX>_STATIC for static linking
  259. There are some special variables whose prefix depends on the count of
  260. given modules. When there is only one module, <PREFIX> stays
  261. unchanged. When there are multiple modules, the prefix will be
  262. changed to <PREFIX>_<MODNAME>: ::
  263. <XPREFIX>_VERSION ... version of the module
  264. <XPREFIX>_PREFIX ... prefix-directory of the module
  265. <XPREFIX>_INCLUDEDIR ... include-dir of the module
  266. <XPREFIX>_LIBDIR ... lib-dir of the module
  267. ::
  268. <XPREFIX> = <PREFIX> when |MODULES| == 1, else
  269. <XPREFIX> = <PREFIX>_<MODNAME>
  270. A <MODULE> parameter can have the following formats: ::
  271. {MODNAME} ... matches any version
  272. {MODNAME}>={VERSION} ... at least version <VERSION> is required
  273. {MODNAME}={VERSION} ... exactly version <VERSION> is required
  274. {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>
  275. Examples
  276. .. code-block:: cmake
  277. pkg_check_modules (GLIB2 glib-2.0)
  278. .. code-block:: cmake
  279. pkg_check_modules (GLIB2 glib-2.0>=2.10)
  280. Requires at least version 2.10 of glib2 and defines e.g.
  281. ``GLIB2_VERSION=2.10.3``
  282. .. code-block:: cmake
  283. pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
  284. Requires both glib2 and gtk2, and defines e.g.
  285. ``FOO_glib-2.0_VERSION=2.10.3`` and ``FOO_gtk+-2.0_VERSION=2.8.20``
  286. .. code-block:: cmake
  287. pkg_check_modules (XRENDER REQUIRED xrender)
  288. Defines e.g.:
  289. ``XRENDER_LIBRARIES=Xrender;X11`` and
  290. ``XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp``
  291. #]========================================]
  292. macro(pkg_check_modules _prefix _module0)
  293. # check cached value
  294. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  295. _pkgconfig_parse_options (_pkg_modules _pkg_is_required _pkg_is_silent "${_module0}" ${ARGN})
  296. _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" "${_prefix}" ${_pkg_modules})
  297. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  298. endif()
  299. endmacro()
  300. #[========================================[.rst:
  301. .. command:: pkg_search_module
  302. Same as :command:`pkg_check_modules`, but instead it checks for given
  303. modules and uses the first working one. ::
  304. pkg_search_module(<PREFIX> [REQUIRED] [QUIET] <MODULE> [<MODULE>]*)
  305. Examples
  306. .. code-block:: cmake
  307. pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
  308. #]========================================]
  309. macro(pkg_search_module _prefix _module0)
  310. # check cached value
  311. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  312. set(_pkg_modules_found 0)
  313. _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent "${_module0}" ${ARGN})
  314. if (NOT ${_pkg_is_silent})
  315. message(STATUS "checking for one of the modules '${_pkg_modules_alt}'")
  316. endif ()
  317. # iterate through all modules and stop at the first working one.
  318. foreach(_pkg_alt ${_pkg_modules_alt})
  319. if(NOT _pkg_modules_found)
  320. _pkg_check_modules_internal(0 1 "${_prefix}" "${_pkg_alt}")
  321. endif()
  322. if (${_prefix}_FOUND)
  323. set(_pkg_modules_found 1)
  324. endif()
  325. endforeach()
  326. if (NOT ${_prefix}_FOUND)
  327. if(${_pkg_is_required})
  328. message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
  329. endif()
  330. endif()
  331. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  332. endif()
  333. endmacro()
  334. #[========================================[.rst:
  335. .. variable:: PKG_CONFIG_EXECUTABLE
  336. Path to the pkg-config executable.
  337. #]========================================]
  338. ### Local Variables:
  339. ### mode: cmake
  340. ### End: