FindPkgConfig.cmake 15 KB

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