You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FindThirdPartyPkgConfig.cmake 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. # - a pkg-config module for CMake
  2. #
  3. # Usage:
  4. # pkg_check_modules(<PREFIX> [REQUIRED] <MODULE> [<MODULE>]*)
  5. # checks for all the given modules
  6. #
  7. # pkg_search_module(<PREFIX> [REQUIRED] <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. # It sets the following variables:
  14. # PKG_CONFIG_FOUND ... true iff pkg-config works on the system
  15. # PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
  16. # <PREFIX>_FOUND ... set to 1 iff module(s) exist
  17. #
  18. # For the following variables two sets of values exist; first one is the
  19. # common one and has the given PREFIX. The second set contains flags
  20. # which are given out when pkgconfig was called with the '--static'
  21. # option.
  22. # <XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
  23. # <XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
  24. # <XPREFIX>_LDFLAGS ... all required linker flags
  25. # <XPREFIX>_LDFLAGS_OTHERS ... all other linker flags
  26. # <XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
  27. # <XPREFIX>_CFLAGS ... all required cflags
  28. # <XPREFIX>_CFLAGS_OTHERS ... the other compiler flags
  29. #
  30. # <XPREFIX> = <PREFIX> for common case
  31. # <XPREFIX> = <PREFIX>_STATIC for static linking
  32. #
  33. # There are some special variables whose prefix depends on the count
  34. # of given modules. When there is only one module, <PREFIX> stays
  35. # unchanged. When there are multiple modules, the prefix will be
  36. # changed to <PREFIX>_<MODNAME>:
  37. # <XPREFIX>_VERSION ... version of the module
  38. # <XPREFIX>_PREFIX ... prefix-directory of the module
  39. # <XPREFIX>_INCLUDEDIR ... include-dir of the module
  40. # <XPREFIX>_LIBDIR ... lib-dir of the module
  41. #
  42. # <XPREFIX> = <PREFIX> when |MODULES| == 1, else
  43. # <XPREFIX> = <PREFIX>_<MODNAME>
  44. #
  45. # A <MODULE> parameter can have the following formats:
  46. # {MODNAME} ... matches any version
  47. # {MODNAME}>={VERSION} ... at least version <VERSION> is required
  48. # {MODNAME}={VERSION} ... exactly version <VERSION> is required
  49. # {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>
  50. #
  51. # Examples
  52. # pkg_check_modules (GLIB2 glib-2.0)
  53. #
  54. # pkg_check_modules (GLIB2 glib-2.0>=2.10)
  55. # requires at least version 2.10 of glib2 and defines e.g.
  56. # GLIB2_VERSION=2.10.3
  57. #
  58. # pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
  59. # requires both glib2 and gtk2, and defines e.g.
  60. # FOO_glib-2.0_VERSION=2.10.3
  61. # FOO_gtk+-2.0_VERSION=2.8.20
  62. #
  63. # pkg_check_modules (XRENDER REQUIRED xrender)
  64. # defines e.g.:
  65. # XRENDER_LIBRARIES=Xrender;X11
  66. # XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
  67. #
  68. # pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
  69. # Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
  70. #
  71. # Redistribution and use, with or without modification, are permitted
  72. # provided that the following conditions are met:
  73. #
  74. # 1. Redistributions must retain the above copyright notice, this
  75. # list of conditions and the following disclaimer.
  76. # 2. The name of the author may not be used to endorse or promote
  77. # products derived from this software without specific prior
  78. # written permission.
  79. #
  80. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  81. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  82. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  83. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  84. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  86. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  87. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  88. # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  89. # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  90. # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  91. ### Common stuff ####
  92. set(PKG_CONFIG_VERSION 1)
  93. set(PKG_CONFIG_FOUND 0)
  94. find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
  95. mark_as_advanced(PKG_CONFIG_EXECUTABLE)
  96. if(PKG_CONFIG_EXECUTABLE)
  97. set(PKG_CONFIG_FOUND 1)
  98. endif(PKG_CONFIG_EXECUTABLE)
  99. # Unsets the given variables
  100. macro(_pkgconfig_unset var)
  101. set(${var} "" CACHE INTERNAL "")
  102. endmacro(_pkgconfig_unset)
  103. macro(_pkgconfig_set var value)
  104. set(${var} ${value} CACHE INTERNAL "")
  105. endmacro(_pkgconfig_set)
  106. # Invokes pkgconfig, cleans up the result and sets variables
  107. macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
  108. set(_pkgconfig_invoke_result)
  109. execute_process(
  110. COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
  111. OUTPUT_VARIABLE _pkgconfig_invoke_result
  112. RESULT_VARIABLE _pkgconfig_failed)
  113. if (_pkgconfig_failed)
  114. set(_pkgconfig_${_varname} "")
  115. _pkgconfig_unset(${_prefix}_${_varname})
  116. else(_pkgconfig_failed)
  117. string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  118. string(REGEX REPLACE " +$" "" _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  119. if (NOT ${_regexp} STREQUAL "")
  120. string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  121. endif(NOT ${_regexp} STREQUAL "")
  122. separate_arguments(_pkgconfig_invoke_result)
  123. #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
  124. set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
  125. _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
  126. endif(_pkgconfig_failed)
  127. endmacro(_pkgconfig_invoke)
  128. # Invokes pkgconfig two times; once without '--static' and once with
  129. # '--static'
  130. macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
  131. _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
  132. _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
  133. endmacro(_pkgconfig_invoke_dyn)
  134. # Splits given arguments into options and a package list
  135. macro(_pkgconfig_parse_options _result _is_req)
  136. set(${_is_req} 0)
  137. foreach(_pkg ${ARGN})
  138. if (_pkg STREQUAL "REQUIRED")
  139. set(${_is_req} 1)
  140. endif (_pkg STREQUAL "REQUIRED")
  141. endforeach(_pkg ${ARGN})
  142. set(${_result} ${ARGN})
  143. list(REMOVE_ITEM ${_result} "REQUIRED")
  144. endmacro(_pkgconfig_parse_options)
  145. ###
  146. macro(_pkg_check_modules_internal _is_required _is_silent _prefix)
  147. _pkgconfig_unset(${_prefix}_FOUND)
  148. _pkgconfig_unset(${_prefix}_VERSION)
  149. _pkgconfig_unset(${_prefix}_PREFIX)
  150. _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  151. _pkgconfig_unset(${_prefix}_LIBDIR)
  152. _pkgconfig_unset(${_prefix}_LIBS)
  153. _pkgconfig_unset(${_prefix}_LIBS_L)
  154. _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  155. _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  156. _pkgconfig_unset(${_prefix}_CFLAGS)
  157. _pkgconfig_unset(${_prefix}_CFLAGS_I)
  158. _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  159. _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  160. _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  161. _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  162. _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  163. _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  164. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  165. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  166. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
  167. # create a better addressable variable of the modules and calculate its size
  168. set(_pkg_check_modules_list ${ARGN})
  169. list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
  170. if(PKG_CONFIG_EXECUTABLE)
  171. # give out status message telling checked module
  172. if (NOT ${_is_silent})
  173. if (_pkg_check_modules_cnt EQUAL 1)
  174. # message(STATUS "checking for module '${_pkg_check_modules_list}'")
  175. else(_pkg_check_modules_cnt EQUAL 1)
  176. # message(STATUS "checking for modules '${_pkg_check_modules_list}'")
  177. endif(_pkg_check_modules_cnt EQUAL 1)
  178. endif(NOT ${_is_silent})
  179. set(_pkg_check_modules_packages)
  180. set(_pkg_check_modules_failed)
  181. # iterate through module list and check whether they exist and match the required version
  182. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
  183. set(_pkg_check_modules_exist_query)
  184. # check whether version is given
  185. if (_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
  186. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\1" _pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  187. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\2" _pkg_check_modules_pkg_op "${_pkg_check_modules_pkg}")
  188. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\3" _pkg_check_modules_pkg_ver "${_pkg_check_modules_pkg}")
  189. else(_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
  190. set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  191. set(_pkg_check_modules_pkg_op)
  192. set(_pkg_check_modules_pkg_ver)
  193. endif(_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
  194. # handle the operands
  195. if (_pkg_check_modules_pkg_op STREQUAL ">=")
  196. list(APPEND _pkg_check_modules_exist_query --atleast-version)
  197. endif(_pkg_check_modules_pkg_op STREQUAL ">=")
  198. if (_pkg_check_modules_pkg_op STREQUAL "=")
  199. list(APPEND _pkg_check_modules_exist_query --exact-version)
  200. endif(_pkg_check_modules_pkg_op STREQUAL "=")
  201. if (_pkg_check_modules_pkg_op STREQUAL "<=")
  202. list(APPEND _pkg_check_modules_exist_query --max-version)
  203. endif(_pkg_check_modules_pkg_op STREQUAL "<=")
  204. # create the final query which is of the format:
  205. # * --atleast-version <version> <pkg-name>
  206. # * --exact-version <version> <pkg-name>
  207. # * --max-version <version> <pkg-name>
  208. # * --exists <pkg-name>
  209. if (_pkg_check_modules_pkg_op)
  210. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
  211. else(_pkg_check_modules_pkg_op)
  212. list(APPEND _pkg_check_modules_exist_query --exists)
  213. endif(_pkg_check_modules_pkg_op)
  214. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
  215. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
  216. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
  217. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
  218. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
  219. list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
  220. # execute the query
  221. execute_process(
  222. COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
  223. RESULT_VARIABLE _pkgconfig_retval)
  224. # evaluate result and tell failures
  225. if (_pkgconfig_retval)
  226. if(NOT ${_is_silent})
  227. message(STATUS " package '${_pkg_check_modules_pkg}' not found")
  228. endif(NOT ${_is_silent})
  229. set(_pkg_check_modules_failed 1)
  230. endif(_pkgconfig_retval)
  231. endforeach(_pkg_check_modules_pkg)
  232. if(_pkg_check_modules_failed)
  233. # fail when requested
  234. if (${_is_required})
  235. message(SEND_ERROR "A required package was not found")
  236. endif (${_is_required})
  237. else(_pkg_check_modules_failed)
  238. # when we are here, we checked whether requested modules
  239. # exist. Now, go through them and set variables
  240. _pkgconfig_set(${_prefix}_FOUND 1)
  241. list(LENGTH _pkg_check_modules_packages pkg_count)
  242. # iterate through all modules again and set individual variables
  243. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
  244. # handle case when there is only one package required
  245. if (pkg_count EQUAL 1)
  246. set(_pkg_check_prefix "${_prefix}")
  247. else(pkg_count EQUAL 1)
  248. set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
  249. endif(pkg_count EQUAL 1)
  250. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
  251. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" PREFIX "" --variable=prefix )
  252. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" INCLUDEDIR "" --variable=includedir )
  253. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" LIBDIR "" --variable=libdir )
  254. # message(STATUS " found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
  255. endforeach(_pkg_check_modules_pkg)
  256. # set variables which are combined for multiple modules
  257. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
  258. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
  259. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
  260. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
  261. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
  262. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
  263. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
  264. endif(_pkg_check_modules_failed)
  265. else(PKG_CONFIG_EXECUTABLE)
  266. if (${_is_required})
  267. message(SEND_ERROR "pkg-config tool not found")
  268. endif (${_is_required})
  269. endif(PKG_CONFIG_EXECUTABLE)
  270. endmacro(_pkg_check_modules_internal)
  271. ###
  272. ### User visible macros start here
  273. ###
  274. ###
  275. macro(pkg_check_modules _prefix _module0)
  276. # check cached value
  277. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
  278. _pkgconfig_parse_options (_pkg_modules _pkg_is_required "${_module0}" ${ARGN})
  279. _pkg_check_modules_internal("${_pkg_is_required}" 0 "${_prefix}" ${_pkg_modules})
  280. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  281. endif(NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
  282. endmacro(pkg_check_modules)
  283. ###
  284. macro(pkg_search_module _prefix _module0)
  285. # check cached value
  286. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
  287. set(_pkg_modules_found 0)
  288. _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required "${_module0}" ${ARGN})
  289. # message(STATUS "checking for one of the modules '${_pkg_modules_alt}'")
  290. # iterate through all modules and stop at the first working one.
  291. foreach(_pkg_alt ${_pkg_modules_alt})
  292. if(NOT _pkg_modules_found)
  293. _pkg_check_modules_internal(0 1 "${_prefix}" "${_pkg_alt}")
  294. endif(NOT _pkg_modules_found)
  295. if (${_prefix}_FOUND)
  296. set(_pkg_modules_found 1)
  297. endif(${_prefix}_FOUND)
  298. endforeach(_pkg_alt)
  299. if (NOT ${_prefix}_FOUND)
  300. if(${_pkg_is_required})
  301. message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
  302. endif(${_pkg_is_required})
  303. endif(NOT ${_prefix}_FOUND)
  304. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  305. endif(NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION})
  306. endmacro(pkg_search_module)
  307. ###
  308. macro(PKGCONFIG _package _include_DIR _link_DIR _link_FLAGS _cflags)
  309. message(STATUS "WARNING: you are using the obsolete 'PKGCONFIG' macro")
  310. _pkg_check_modules_internal(0 0 _PKGCONFIG_TMP "${_package}")
  311. if (_PKGCONFIG_TMP_FOUND)
  312. set(${_include_DIR} ${_PKGCONFIG_TMP_INCLUDEDIR})
  313. set(${_link_DIR} ${_PKGCONFIG_TMP_LIBDIR})
  314. set(${_link_FLAGS} ${_PKGCONFIG_TMP_LDFLAGS})
  315. set(${_cflags} ${_PKGCONFIG_TMP_CFLAGS})
  316. set(_return_VALUE 0)
  317. else(_PKGCONFIG_TMP_FOUND)
  318. set(${_include_DIR})
  319. set(${_link_DIR})
  320. set(${_link_FLAGS})
  321. set(${_cflags})
  322. set(_return_VALUE 1)
  323. endif(_PKGCONFIG_TMP_FOUND)
  324. endmacro(PKGCONFIG)
  325. ### Local Variables:
  326. ### mode: cmake
  327. ### End: