FindPkgConfig.cmake 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 _no_cmake_path _no_cmake_environment_path)
  87. set(${_is_req} 0)
  88. set(${_is_silent} 0)
  89. set(${_no_cmake_path} 0)
  90. set(${_no_cmake_environment_path} 0)
  91. if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  92. if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  93. set(${_no_cmake_path} 1)
  94. set(${_no_cmake_environment_path} 1)
  95. endif()
  96. elseif(${CMAKE_MINIMUM_REQUIRED_VERSION} VERSION_LESS 3.1)
  97. set(${_no_cmake_path} 1)
  98. set(${_no_cmake_environment_path} 1)
  99. endif()
  100. foreach(_pkg ${ARGN})
  101. if (_pkg STREQUAL "REQUIRED")
  102. set(${_is_req} 1)
  103. endif ()
  104. if (_pkg STREQUAL "QUIET")
  105. set(${_is_silent} 1)
  106. endif ()
  107. if (_pkg STREQUAL "NO_CMAKE_PATH")
  108. set(${_no_cmake_path} 1)
  109. endif()
  110. if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
  111. set(${_no_cmake_environment_path} 1)
  112. endif()
  113. endforeach()
  114. set(${_result} ${ARGN})
  115. list(REMOVE_ITEM ${_result} "REQUIRED")
  116. list(REMOVE_ITEM ${_result} "QUIET")
  117. list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
  118. list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
  119. endmacro()
  120. # Add the content of a variable or an environment variable to a list of
  121. # paths
  122. # Usage:
  123. # - _pkgconfig_add_extra_path(_extra_paths VAR)
  124. # - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
  125. function(_pkgconfig_add_extra_path _extra_paths_var _var)
  126. set(_is_env 0)
  127. if(_var STREQUAL "ENV")
  128. set(_var ${ARGV2})
  129. set(_is_env 1)
  130. endif()
  131. if(NOT _is_env)
  132. if(NOT "${${_var}}" STREQUAL "")
  133. list(APPEND ${_extra_paths_var} ${CMAKE_PREFIX_PATH})
  134. endif()
  135. else()
  136. if(NOT "$ENV{${_var}}" STREQUAL "")
  137. file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
  138. list(APPEND ${_extra_paths_var} ${_path})
  139. unset(_path)
  140. endif()
  141. endif()
  142. set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
  143. endfunction()
  144. ###
  145. macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _prefix)
  146. _pkgconfig_unset(${_prefix}_FOUND)
  147. _pkgconfig_unset(${_prefix}_VERSION)
  148. _pkgconfig_unset(${_prefix}_PREFIX)
  149. _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  150. _pkgconfig_unset(${_prefix}_LIBDIR)
  151. _pkgconfig_unset(${_prefix}_LIBS)
  152. _pkgconfig_unset(${_prefix}_LIBS_L)
  153. _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  154. _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  155. _pkgconfig_unset(${_prefix}_CFLAGS)
  156. _pkgconfig_unset(${_prefix}_CFLAGS_I)
  157. _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  158. _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  159. _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  160. _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  161. _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  162. _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  163. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  164. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  165. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
  166. # create a better addressable variable of the modules and calculate its size
  167. set(_pkg_check_modules_list ${ARGN})
  168. list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
  169. if(PKG_CONFIG_EXECUTABLE)
  170. # give out status message telling checked module
  171. if (NOT ${_is_silent})
  172. if (_pkg_check_modules_cnt EQUAL 1)
  173. message(STATUS "checking for module '${_pkg_check_modules_list}'")
  174. else()
  175. message(STATUS "checking for modules '${_pkg_check_modules_list}'")
  176. endif()
  177. endif()
  178. set(_pkg_check_modules_packages)
  179. set(_pkg_check_modules_failed)
  180. set(_extra_paths)
  181. if(NOT _no_cmake_path)
  182. _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
  183. _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
  184. _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
  185. endif()
  186. if(NOT _no_cmake_environment_path)
  187. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
  188. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
  189. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
  190. endif()
  191. if(NOT "${_extra_paths}" STREQUAL "")
  192. # Save the PKG_CONFIG_PATH environment variable, and add paths
  193. # from the CMAKE_PREFIX_PATH variables
  194. set(_pkgconfig_path_old $ENV{PKG_CONFIG_PATH})
  195. set(_pkgconfig_path ${_pkgconfig_path_old})
  196. if(NOT "${_pkgconfig_path}" STREQUAL "")
  197. file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  198. endif()
  199. # Create a list of the possible pkgconfig subfolder (depending on
  200. # the system
  201. set(_lib_dirs)
  202. if(NOT DEFINED CMAKE_SYSTEM_NAME
  203. OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
  204. AND NOT CMAKE_CROSSCOMPILING))
  205. if(EXISTS "/etc/debian_version") # is this a debian system ?
  206. if(CMAKE_LIBRARY_ARCHITECTURE)
  207. list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
  208. endif()
  209. else()
  210. # not debian, chech the FIND_LIBRARY_USE_LIB64_PATHS property
  211. get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
  212. if(uselib64)
  213. list(APPEND _lib_dirs "lib64/pkgconfig")
  214. endif()
  215. endif()
  216. endif()
  217. list(APPEND _lib_dirs "lib/pkgconfig")
  218. # Check if directories exist and eventually append them to the
  219. # pkgconfig path list
  220. foreach(_prefix_dir ${_extra_paths})
  221. foreach(_lib_dir ${_lib_dirs})
  222. if(EXISTS "${_prefix_dir}/${_lib_dir}")
  223. list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
  224. list(REMOVE_DUPLICATES _pkgconfig_path)
  225. endif()
  226. endforeach()
  227. endforeach()
  228. # Prepare and set the environment variable
  229. if(NOT "${_pkgconfig_path}" STREQUAL "")
  230. # remove empty values from the list
  231. list(REMOVE_ITEM _pkgconfig_path "")
  232. file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  233. if(UNIX)
  234. string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
  235. string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
  236. endif()
  237. set(ENV{PKG_CONFIG_PATH} ${_pkgconfig_path})
  238. endif()
  239. # Unset variables
  240. unset(_lib_dirs)
  241. unset(_pkgconfig_path)
  242. endif()
  243. # iterate through module list and check whether they exist and match the required version
  244. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
  245. set(_pkg_check_modules_exist_query)
  246. # check whether version is given
  247. if (_pkg_check_modules_pkg MATCHES ".*(>=|=|<=).*")
  248. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\1" _pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  249. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\2" _pkg_check_modules_pkg_op "${_pkg_check_modules_pkg}")
  250. string(REGEX REPLACE "(.*[^><])(>=|=|<=)(.*)" "\\3" _pkg_check_modules_pkg_ver "${_pkg_check_modules_pkg}")
  251. else()
  252. set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  253. set(_pkg_check_modules_pkg_op)
  254. set(_pkg_check_modules_pkg_ver)
  255. endif()
  256. # handle the operands
  257. if (_pkg_check_modules_pkg_op STREQUAL ">=")
  258. list(APPEND _pkg_check_modules_exist_query --atleast-version)
  259. endif()
  260. if (_pkg_check_modules_pkg_op STREQUAL "=")
  261. list(APPEND _pkg_check_modules_exist_query --exact-version)
  262. endif()
  263. if (_pkg_check_modules_pkg_op STREQUAL "<=")
  264. list(APPEND _pkg_check_modules_exist_query --max-version)
  265. endif()
  266. # create the final query which is of the format:
  267. # * --atleast-version <version> <pkg-name>
  268. # * --exact-version <version> <pkg-name>
  269. # * --max-version <version> <pkg-name>
  270. # * --exists <pkg-name>
  271. if (_pkg_check_modules_pkg_op)
  272. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
  273. else()
  274. list(APPEND _pkg_check_modules_exist_query --exists)
  275. endif()
  276. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
  277. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
  278. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
  279. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
  280. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
  281. list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
  282. # execute the query
  283. execute_process(
  284. COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
  285. RESULT_VARIABLE _pkgconfig_retval)
  286. # evaluate result and tell failures
  287. if (_pkgconfig_retval)
  288. if(NOT ${_is_silent})
  289. message(STATUS " package '${_pkg_check_modules_pkg}' not found")
  290. endif()
  291. set(_pkg_check_modules_failed 1)
  292. endif()
  293. endforeach()
  294. if(_pkg_check_modules_failed)
  295. # fail when requested
  296. if (${_is_required})
  297. message(SEND_ERROR "A required package was not found")
  298. endif ()
  299. else()
  300. # when we are here, we checked whether requested modules
  301. # exist. Now, go through them and set variables
  302. _pkgconfig_set(${_prefix}_FOUND 1)
  303. list(LENGTH _pkg_check_modules_packages pkg_count)
  304. # iterate through all modules again and set individual variables
  305. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
  306. # handle case when there is only one package required
  307. if (pkg_count EQUAL 1)
  308. set(_pkg_check_prefix "${_prefix}")
  309. else()
  310. set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
  311. endif()
  312. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
  313. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" PREFIX "" --variable=prefix )
  314. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" INCLUDEDIR "" --variable=includedir )
  315. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" LIBDIR "" --variable=libdir )
  316. if (NOT ${_is_silent})
  317. message(STATUS " found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
  318. endif ()
  319. endforeach()
  320. # set variables which are combined for multiple modules
  321. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
  322. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
  323. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
  324. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
  325. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
  326. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
  327. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
  328. endif()
  329. if(NOT "${_extra_paths}" STREQUAL "")
  330. # Restore the environment variable
  331. set(ENV{PKG_CONFIG_PATH} ${_pkgconfig_path})
  332. endif()
  333. unset(_extra_paths)
  334. unset(_pkgconfig_path_old)
  335. else()
  336. if (${_is_required})
  337. message(SEND_ERROR "pkg-config tool not found")
  338. endif ()
  339. endif()
  340. endmacro()
  341. ###
  342. ### User visible macros start here
  343. ###
  344. #[========================================[.rst:
  345. .. command:: pkg_check_modules
  346. Checks for all the given modules. ::
  347. pkg_check_modules(<PREFIX> [REQUIRED] [QUIET]
  348. [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
  349. <MODULE> [<MODULE>]*)
  350. When the ``REQUIRED`` argument was set, macros will fail with an error
  351. when module(s) could not be found.
  352. When the ``QUIET`` argument is set, no status messages will be printed.
  353. By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
  354. later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set, the
  355. :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH`, and
  356. :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables will
  357. be added to ``pkg-config`` search path.
  358. The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
  359. disable this behavior for the cache variables and the environment
  360. variables, respectively.
  361. It sets the following variables: ::
  362. PKG_CONFIG_FOUND ... if pkg-config executable was found
  363. PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
  364. PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
  365. (since CMake 2.8.8)
  366. For the following variables two sets of values exist; first one is the
  367. common one and has the given PREFIX. The second set contains flags
  368. which are given out when ``pkg-config`` was called with the ``--static``
  369. option. ::
  370. <XPREFIX>_FOUND ... set to 1 if module(s) exist
  371. <XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
  372. <XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
  373. <XPREFIX>_LDFLAGS ... all required linker flags
  374. <XPREFIX>_LDFLAGS_OTHER ... all other linker flags
  375. <XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
  376. <XPREFIX>_CFLAGS ... all required cflags
  377. <XPREFIX>_CFLAGS_OTHER ... the other compiler flags
  378. ::
  379. <XPREFIX> = <PREFIX> for common case
  380. <XPREFIX> = <PREFIX>_STATIC for static linking
  381. There are some special variables whose prefix depends on the count of
  382. given modules. When there is only one module, <PREFIX> stays
  383. unchanged. When there are multiple modules, the prefix will be
  384. changed to <PREFIX>_<MODNAME>: ::
  385. <XPREFIX>_VERSION ... version of the module
  386. <XPREFIX>_PREFIX ... prefix-directory of the module
  387. <XPREFIX>_INCLUDEDIR ... include-dir of the module
  388. <XPREFIX>_LIBDIR ... lib-dir of the module
  389. ::
  390. <XPREFIX> = <PREFIX> when |MODULES| == 1, else
  391. <XPREFIX> = <PREFIX>_<MODNAME>
  392. A <MODULE> parameter can have the following formats: ::
  393. {MODNAME} ... matches any version
  394. {MODNAME}>={VERSION} ... at least version <VERSION> is required
  395. {MODNAME}={VERSION} ... exactly version <VERSION> is required
  396. {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>
  397. Examples
  398. .. code-block:: cmake
  399. pkg_check_modules (GLIB2 glib-2.0)
  400. .. code-block:: cmake
  401. pkg_check_modules (GLIB2 glib-2.0>=2.10)
  402. Requires at least version 2.10 of glib2 and defines e.g.
  403. ``GLIB2_VERSION=2.10.3``
  404. .. code-block:: cmake
  405. pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
  406. Requires both glib2 and gtk2, and defines e.g.
  407. ``FOO_glib-2.0_VERSION=2.10.3`` and ``FOO_gtk+-2.0_VERSION=2.8.20``
  408. .. code-block:: cmake
  409. pkg_check_modules (XRENDER REQUIRED xrender)
  410. Defines e.g.:
  411. ``XRENDER_LIBRARIES=Xrender;X11`` and
  412. ``XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp``
  413. #]========================================]
  414. macro(pkg_check_modules _prefix _module0)
  415. # check cached value
  416. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  417. _pkgconfig_parse_options (_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path "${_module0}" ${ARGN})
  418. _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} "${_prefix}" ${_pkg_modules})
  419. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  420. endif()
  421. endmacro()
  422. #[========================================[.rst:
  423. .. command:: pkg_search_module
  424. Same as :command:`pkg_check_modules`, but instead it checks for given
  425. modules and uses the first working one. ::
  426. pkg_search_module(<PREFIX> [REQUIRED] [QUIET]
  427. [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
  428. <MODULE> [<MODULE>]*)
  429. Examples
  430. .. code-block:: cmake
  431. pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
  432. #]========================================]
  433. macro(pkg_search_module _prefix _module0)
  434. # check cached value
  435. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  436. set(_pkg_modules_found 0)
  437. _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path "${_module0}" ${ARGN})
  438. if (NOT ${_pkg_is_silent})
  439. message(STATUS "checking for one of the modules '${_pkg_modules_alt}'")
  440. endif ()
  441. # iterate through all modules and stop at the first working one.
  442. foreach(_pkg_alt ${_pkg_modules_alt})
  443. if(NOT _pkg_modules_found)
  444. _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} "${_prefix}" "${_pkg_alt}")
  445. endif()
  446. if (${_prefix}_FOUND)
  447. set(_pkg_modules_found 1)
  448. endif()
  449. endforeach()
  450. if (NOT ${_prefix}_FOUND)
  451. if(${_pkg_is_required})
  452. message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
  453. endif()
  454. endif()
  455. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  456. endif()
  457. endmacro()
  458. #[========================================[.rst:
  459. .. variable:: PKG_CONFIG_EXECUTABLE
  460. Path to the pkg-config executable.
  461. .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
  462. Whether :command:`pkg_check_modules` and :command:`pkg_search_module`
  463. should add the paths in :variable:`CMAKE_PREFIX_PATH`,
  464. :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH`
  465. cache and environment variables to ``pkg-config`` search path.
  466. If this variable is not set, this behavior is enabled by default if
  467. :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
  468. otherwise.
  469. #]========================================]
  470. ### Local Variables:
  471. ### mode: cmake
  472. ### End: