FindPkgConfig.cmake 21 KB

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