FindPkgConfig.cmake 16 KB

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