FindPkgConfig.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 <foss@grueninger.de>
  17. # Copyright 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
  18. # Copyright 2016 Rolf Eike Beer <eike@sf-mail.de>
  19. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distribute this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. ### Common stuff ####
  30. set(PKG_CONFIG_VERSION 1)
  31. # find pkg-config, use PKG_CONFIG if set
  32. if((NOT PKG_CONFIG_EXECUTABLE) AND (NOT "$ENV{PKG_CONFIG}" STREQUAL ""))
  33. set(PKG_CONFIG_EXECUTABLE "$ENV{PKG_CONFIG}" CACHE FILEPATH "pkg-config executable")
  34. endif()
  35. find_program(PKG_CONFIG_EXECUTABLE NAMES pkg-config DOC "pkg-config executable")
  36. mark_as_advanced(PKG_CONFIG_EXECUTABLE)
  37. if (PKG_CONFIG_EXECUTABLE)
  38. execute_process(COMMAND ${PKG_CONFIG_EXECUTABLE} --version
  39. OUTPUT_VARIABLE PKG_CONFIG_VERSION_STRING
  40. ERROR_QUIET
  41. OUTPUT_STRIP_TRAILING_WHITESPACE)
  42. endif ()
  43. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  44. find_package_handle_standard_args(PkgConfig
  45. REQUIRED_VARS PKG_CONFIG_EXECUTABLE
  46. VERSION_VAR PKG_CONFIG_VERSION_STRING)
  47. # This is needed because the module name is "PkgConfig" but the name of
  48. # this variable has always been PKG_CONFIG_FOUND so this isn't automatically
  49. # handled by FPHSA.
  50. set(PKG_CONFIG_FOUND "${PKGCONFIG_FOUND}")
  51. # Unsets the given variables
  52. macro(_pkgconfig_unset var)
  53. set(${var} "" CACHE INTERNAL "")
  54. endmacro()
  55. macro(_pkgconfig_set var value)
  56. set(${var} ${value} CACHE INTERNAL "")
  57. endmacro()
  58. # Invokes pkgconfig, cleans up the result and sets variables
  59. macro(_pkgconfig_invoke _pkglist _prefix _varname _regexp)
  60. set(_pkgconfig_invoke_result)
  61. execute_process(
  62. COMMAND ${PKG_CONFIG_EXECUTABLE} ${ARGN} ${_pkglist}
  63. OUTPUT_VARIABLE _pkgconfig_invoke_result
  64. RESULT_VARIABLE _pkgconfig_failed
  65. OUTPUT_STRIP_TRAILING_WHITESPACE)
  66. if (_pkgconfig_failed)
  67. set(_pkgconfig_${_varname} "")
  68. _pkgconfig_unset(${_prefix}_${_varname})
  69. else()
  70. string(REGEX REPLACE "[\r\n]" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  71. if (NOT ${_regexp} STREQUAL "")
  72. string(REGEX REPLACE "${_regexp}" " " _pkgconfig_invoke_result "${_pkgconfig_invoke_result}")
  73. endif()
  74. separate_arguments(_pkgconfig_invoke_result)
  75. #message(STATUS " ${_varname} ... ${_pkgconfig_invoke_result}")
  76. set(_pkgconfig_${_varname} ${_pkgconfig_invoke_result})
  77. _pkgconfig_set(${_prefix}_${_varname} "${_pkgconfig_invoke_result}")
  78. endif()
  79. endmacro()
  80. #[========================================[.rst:
  81. .. command:: pkg_get_variable
  82. Retrieves the value of a variable from a package::
  83. pkg_get_variable(<RESULT> <MODULE> <VARIABLE>)
  84. For example:
  85. .. code-block:: cmake
  86. pkg_get_variable(GI_GIRDIR gobject-introspection-1.0 girdir)
  87. #]========================================]
  88. function (pkg_get_variable result pkg variable)
  89. _pkgconfig_invoke("${pkg}" "prefix" "result" "" "--variable=${variable}")
  90. set("${result}"
  91. "${prefix_result}"
  92. PARENT_SCOPE)
  93. endfunction ()
  94. # Invokes pkgconfig two times; once without '--static' and once with
  95. # '--static'
  96. macro(_pkgconfig_invoke_dyn _pkglist _prefix _varname cleanup_regexp)
  97. _pkgconfig_invoke("${_pkglist}" ${_prefix} ${_varname} "${cleanup_regexp}" ${ARGN})
  98. _pkgconfig_invoke("${_pkglist}" ${_prefix} STATIC_${_varname} "${cleanup_regexp}" --static ${ARGN})
  99. endmacro()
  100. # Splits given arguments into options and a package list
  101. macro(_pkgconfig_parse_options _result _is_req _is_silent _no_cmake_path _no_cmake_environment_path _imp_target)
  102. set(${_is_req} 0)
  103. set(${_is_silent} 0)
  104. set(${_no_cmake_path} 0)
  105. set(${_no_cmake_environment_path} 0)
  106. set(${_imp_target} 0)
  107. if(DEFINED PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  108. if(NOT PKG_CONFIG_USE_CMAKE_PREFIX_PATH)
  109. set(${_no_cmake_path} 1)
  110. set(${_no_cmake_environment_path} 1)
  111. endif()
  112. elseif(CMAKE_MINIMUM_REQUIRED_VERSION VERSION_LESS 3.1)
  113. set(${_no_cmake_path} 1)
  114. set(${_no_cmake_environment_path} 1)
  115. endif()
  116. foreach(_pkg ${ARGN})
  117. if (_pkg STREQUAL "REQUIRED")
  118. set(${_is_req} 1)
  119. endif ()
  120. if (_pkg STREQUAL "QUIET")
  121. set(${_is_silent} 1)
  122. endif ()
  123. if (_pkg STREQUAL "NO_CMAKE_PATH")
  124. set(${_no_cmake_path} 1)
  125. endif()
  126. if (_pkg STREQUAL "NO_CMAKE_ENVIRONMENT_PATH")
  127. set(${_no_cmake_environment_path} 1)
  128. endif()
  129. if (_pkg STREQUAL "IMPORTED_TARGET")
  130. set(${_imp_target} 1)
  131. endif()
  132. endforeach()
  133. set(${_result} ${ARGN})
  134. list(REMOVE_ITEM ${_result} "REQUIRED")
  135. list(REMOVE_ITEM ${_result} "QUIET")
  136. list(REMOVE_ITEM ${_result} "NO_CMAKE_PATH")
  137. list(REMOVE_ITEM ${_result} "NO_CMAKE_ENVIRONMENT_PATH")
  138. list(REMOVE_ITEM ${_result} "IMPORTED_TARGET")
  139. endmacro()
  140. # Add the content of a variable or an environment variable to a list of
  141. # paths
  142. # Usage:
  143. # - _pkgconfig_add_extra_path(_extra_paths VAR)
  144. # - _pkgconfig_add_extra_path(_extra_paths ENV VAR)
  145. function(_pkgconfig_add_extra_path _extra_paths_var _var)
  146. set(_is_env 0)
  147. if(ARGC GREATER 2 AND _var STREQUAL "ENV")
  148. set(_var ${ARGV2})
  149. set(_is_env 1)
  150. endif()
  151. if(NOT _is_env)
  152. if(NOT "${${_var}}" STREQUAL "")
  153. list(APPEND ${_extra_paths_var} ${${_var}})
  154. endif()
  155. else()
  156. if(NOT "$ENV{${_var}}" STREQUAL "")
  157. file(TO_CMAKE_PATH "$ENV{${_var}}" _path)
  158. list(APPEND ${_extra_paths_var} ${_path})
  159. unset(_path)
  160. endif()
  161. endif()
  162. set(${_extra_paths_var} ${${_extra_paths_var}} PARENT_SCOPE)
  163. endfunction()
  164. # scan the LDFLAGS returned by pkg-config for library directories and
  165. # libraries, figure out the absolute paths of that libraries in the
  166. # given directories, and create an imported target from them
  167. function(_pkg_create_imp_target _prefix _no_cmake_path _no_cmake_environment_path)
  168. unset(_libs)
  169. unset(_find_opts)
  170. # set the options that are used as long as the .pc file does not provide a library
  171. # path to look into
  172. if(_no_cmake_path)
  173. set(_find_opts "NO_CMAKE_PATH")
  174. endif()
  175. if(_no_cmake_environment_path)
  176. set(_find_opts "${_find_opts} NO_CMAKE_ENVIRONMENT_PATH")
  177. endif()
  178. foreach (flag IN LISTS ${_prefix}_LDFLAGS)
  179. if (flag MATCHES "^-L(.*)")
  180. # only look into the given paths from now on
  181. set(_find_opts "HINTS ${${CMAKE_MATCH_1}} NO_DEFAULT_PATH")
  182. continue()
  183. endif()
  184. if (flag MATCHES "^-l(.*)")
  185. set(_pkg_search "${CMAKE_MATCH_1}")
  186. else()
  187. continue()
  188. endif()
  189. find_library(pkgcfg_lib_${_prefix}_${_pkg_search}
  190. NAMES ${_pkg_search}
  191. ${_find_opts})
  192. list(APPEND _libs "${pkgcfg_lib_${_prefix}_${_pkg_search}}")
  193. endforeach()
  194. # only create the target if it is linkable, i.e. no executables
  195. if (NOT TARGET PkgConfig::${_prefix}
  196. AND ( ${_prefix}_INCLUDE_DIRS OR _libs OR ${_prefix}_CFLAGS_OTHER ))
  197. add_library(PkgConfig::${_prefix} INTERFACE IMPORTED)
  198. unset(_props)
  199. if(${_prefix}_INCLUDE_DIRS)
  200. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  201. INTERFACE_INCLUDE_DIRECTORIES "${${_prefix}_INCLUDE_DIRS}")
  202. endif()
  203. if(_libs)
  204. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  205. INTERFACE_LINK_LIBRARIES "${_libs}")
  206. endif()
  207. if(${_prefix}_CFLAGS_OTHER)
  208. set_property(TARGET PkgConfig::${_prefix} PROPERTY
  209. INTERFACE_COMPILE_OPTIONS "${${_prefix}_CFLAGS_OTHER}")
  210. endif()
  211. endif()
  212. endfunction()
  213. ###
  214. macro(_pkg_check_modules_internal _is_required _is_silent _no_cmake_path _no_cmake_environment_path _imp_target _prefix)
  215. _pkgconfig_unset(${_prefix}_FOUND)
  216. _pkgconfig_unset(${_prefix}_VERSION)
  217. _pkgconfig_unset(${_prefix}_PREFIX)
  218. _pkgconfig_unset(${_prefix}_INCLUDEDIR)
  219. _pkgconfig_unset(${_prefix}_LIBDIR)
  220. _pkgconfig_unset(${_prefix}_LIBS)
  221. _pkgconfig_unset(${_prefix}_LIBS_L)
  222. _pkgconfig_unset(${_prefix}_LIBS_PATHS)
  223. _pkgconfig_unset(${_prefix}_LIBS_OTHER)
  224. _pkgconfig_unset(${_prefix}_CFLAGS)
  225. _pkgconfig_unset(${_prefix}_CFLAGS_I)
  226. _pkgconfig_unset(${_prefix}_CFLAGS_OTHER)
  227. _pkgconfig_unset(${_prefix}_STATIC_LIBDIR)
  228. _pkgconfig_unset(${_prefix}_STATIC_LIBS)
  229. _pkgconfig_unset(${_prefix}_STATIC_LIBS_L)
  230. _pkgconfig_unset(${_prefix}_STATIC_LIBS_PATHS)
  231. _pkgconfig_unset(${_prefix}_STATIC_LIBS_OTHER)
  232. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS)
  233. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_I)
  234. _pkgconfig_unset(${_prefix}_STATIC_CFLAGS_OTHER)
  235. # create a better addressable variable of the modules and calculate its size
  236. set(_pkg_check_modules_list ${ARGN})
  237. list(LENGTH _pkg_check_modules_list _pkg_check_modules_cnt)
  238. if(PKG_CONFIG_EXECUTABLE)
  239. # give out status message telling checked module
  240. if (NOT ${_is_silent})
  241. if (_pkg_check_modules_cnt EQUAL 1)
  242. message(STATUS "Checking for module '${_pkg_check_modules_list}'")
  243. else()
  244. message(STATUS "Checking for modules '${_pkg_check_modules_list}'")
  245. endif()
  246. endif()
  247. set(_pkg_check_modules_packages)
  248. set(_pkg_check_modules_failed)
  249. set(_extra_paths)
  250. if(NOT _no_cmake_path)
  251. _pkgconfig_add_extra_path(_extra_paths CMAKE_PREFIX_PATH)
  252. _pkgconfig_add_extra_path(_extra_paths CMAKE_FRAMEWORK_PATH)
  253. _pkgconfig_add_extra_path(_extra_paths CMAKE_APPBUNDLE_PATH)
  254. endif()
  255. if(NOT _no_cmake_environment_path)
  256. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_PREFIX_PATH)
  257. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_FRAMEWORK_PATH)
  258. _pkgconfig_add_extra_path(_extra_paths ENV CMAKE_APPBUNDLE_PATH)
  259. endif()
  260. if(NOT "${_extra_paths}" STREQUAL "")
  261. # Save the PKG_CONFIG_PATH environment variable, and add paths
  262. # from the CMAKE_PREFIX_PATH variables
  263. set(_pkgconfig_path_old "$ENV{PKG_CONFIG_PATH}")
  264. set(_pkgconfig_path "${_pkgconfig_path_old}")
  265. if(NOT "${_pkgconfig_path}" STREQUAL "")
  266. file(TO_CMAKE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  267. endif()
  268. # Create a list of the possible pkgconfig subfolder (depending on
  269. # the system
  270. set(_lib_dirs)
  271. if(NOT DEFINED CMAKE_SYSTEM_NAME
  272. OR (CMAKE_SYSTEM_NAME MATCHES "^(Linux|kFreeBSD|GNU)$"
  273. AND NOT CMAKE_CROSSCOMPILING))
  274. if(EXISTS "/etc/debian_version") # is this a debian system ?
  275. if(CMAKE_LIBRARY_ARCHITECTURE)
  276. list(APPEND _lib_dirs "lib/${CMAKE_LIBRARY_ARCHITECTURE}/pkgconfig")
  277. endif()
  278. else()
  279. # not debian, chech the FIND_LIBRARY_USE_LIB64_PATHS property
  280. get_property(uselib64 GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS)
  281. if(uselib64)
  282. list(APPEND _lib_dirs "lib64/pkgconfig")
  283. endif()
  284. endif()
  285. endif()
  286. list(APPEND _lib_dirs "lib/pkgconfig")
  287. list(APPEND _lib_dirs "share/pkgconfig")
  288. # Check if directories exist and eventually append them to the
  289. # pkgconfig path list
  290. foreach(_prefix_dir ${_extra_paths})
  291. foreach(_lib_dir ${_lib_dirs})
  292. if(EXISTS "${_prefix_dir}/${_lib_dir}")
  293. list(APPEND _pkgconfig_path "${_prefix_dir}/${_lib_dir}")
  294. list(REMOVE_DUPLICATES _pkgconfig_path)
  295. endif()
  296. endforeach()
  297. endforeach()
  298. # Prepare and set the environment variable
  299. if(NOT "${_pkgconfig_path}" STREQUAL "")
  300. # remove empty values from the list
  301. list(REMOVE_ITEM _pkgconfig_path "")
  302. file(TO_NATIVE_PATH "${_pkgconfig_path}" _pkgconfig_path)
  303. if(UNIX)
  304. string(REPLACE ";" ":" _pkgconfig_path "${_pkgconfig_path}")
  305. string(REPLACE "\\ " " " _pkgconfig_path "${_pkgconfig_path}")
  306. endif()
  307. set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path}")
  308. endif()
  309. # Unset variables
  310. unset(_lib_dirs)
  311. unset(_pkgconfig_path)
  312. endif()
  313. # iterate through module list and check whether they exist and match the required version
  314. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_list})
  315. set(_pkg_check_modules_exist_query)
  316. # check whether version is given
  317. if (_pkg_check_modules_pkg MATCHES "(.*[^><])(>=|=|<=)(.*)")
  318. set(_pkg_check_modules_pkg_name "${CMAKE_MATCH_1}")
  319. set(_pkg_check_modules_pkg_op "${CMAKE_MATCH_2}")
  320. set(_pkg_check_modules_pkg_ver "${CMAKE_MATCH_3}")
  321. else()
  322. set(_pkg_check_modules_pkg_name "${_pkg_check_modules_pkg}")
  323. set(_pkg_check_modules_pkg_op)
  324. set(_pkg_check_modules_pkg_ver)
  325. endif()
  326. # handle the operands
  327. if (_pkg_check_modules_pkg_op STREQUAL ">=")
  328. list(APPEND _pkg_check_modules_exist_query --atleast-version)
  329. endif()
  330. if (_pkg_check_modules_pkg_op STREQUAL "=")
  331. list(APPEND _pkg_check_modules_exist_query --exact-version)
  332. endif()
  333. if (_pkg_check_modules_pkg_op STREQUAL "<=")
  334. list(APPEND _pkg_check_modules_exist_query --max-version)
  335. endif()
  336. # create the final query which is of the format:
  337. # * --atleast-version <version> <pkg-name>
  338. # * --exact-version <version> <pkg-name>
  339. # * --max-version <version> <pkg-name>
  340. # * --exists <pkg-name>
  341. if (_pkg_check_modules_pkg_op)
  342. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_ver}")
  343. else()
  344. list(APPEND _pkg_check_modules_exist_query --exists --print-errors --short-errors)
  345. endif()
  346. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_VERSION)
  347. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_PREFIX)
  348. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_INCLUDEDIR)
  349. _pkgconfig_unset(${_prefix}_${_pkg_check_modules_pkg_name}_LIBDIR)
  350. list(APPEND _pkg_check_modules_exist_query "${_pkg_check_modules_pkg_name}")
  351. list(APPEND _pkg_check_modules_packages "${_pkg_check_modules_pkg_name}")
  352. # execute the query
  353. execute_process(
  354. COMMAND ${PKG_CONFIG_EXECUTABLE} ${_pkg_check_modules_exist_query}
  355. RESULT_VARIABLE _pkgconfig_retval
  356. ERROR_VARIABLE _pkgconfig_error
  357. ERROR_STRIP_TRAILING_WHITESPACE)
  358. # evaluate result and tell failures
  359. if (_pkgconfig_retval)
  360. if(NOT ${_is_silent})
  361. message(STATUS " ${_pkgconfig_error}")
  362. endif()
  363. set(_pkg_check_modules_failed 1)
  364. endif()
  365. endforeach()
  366. if(_pkg_check_modules_failed)
  367. # fail when requested
  368. if (${_is_required})
  369. message(FATAL_ERROR "A required package was not found")
  370. endif ()
  371. else()
  372. # when we are here, we checked whether requested modules
  373. # exist. Now, go through them and set variables
  374. _pkgconfig_set(${_prefix}_FOUND 1)
  375. list(LENGTH _pkg_check_modules_packages pkg_count)
  376. # iterate through all modules again and set individual variables
  377. foreach (_pkg_check_modules_pkg ${_pkg_check_modules_packages})
  378. # handle case when there is only one package required
  379. if (pkg_count EQUAL 1)
  380. set(_pkg_check_prefix "${_prefix}")
  381. else()
  382. set(_pkg_check_prefix "${_prefix}_${_pkg_check_modules_pkg}")
  383. endif()
  384. _pkgconfig_invoke(${_pkg_check_modules_pkg} "${_pkg_check_prefix}" VERSION "" --modversion )
  385. pkg_get_variable("${_pkg_check_prefix}_PREFIX" ${_pkg_check_modules_pkg} "prefix")
  386. pkg_get_variable("${_pkg_check_prefix}_INCLUDEDIR" ${_pkg_check_modules_pkg} "includedir")
  387. pkg_get_variable("${_pkg_check_prefix}_LIBDIR" ${_pkg_check_modules_pkg} "libdir")
  388. foreach (variable IN ITEMS PREFIX INCLUDEDIR LIBDIR)
  389. _pkgconfig_set("${_pkg_check_prefix}_${variable}" "${${_pkg_check_prefix}_${variable}}")
  390. endforeach ()
  391. if (NOT ${_is_silent})
  392. message(STATUS " Found ${_pkg_check_modules_pkg}, version ${_pkgconfig_VERSION}")
  393. endif ()
  394. endforeach()
  395. # set variables which are combined for multiple modules
  396. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARIES "(^| )-l" --libs-only-l )
  397. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LIBRARY_DIRS "(^| )-L" --libs-only-L )
  398. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS "" --libs )
  399. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" LDFLAGS_OTHER "" --libs-only-other )
  400. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" INCLUDE_DIRS "(^| )-I" --cflags-only-I )
  401. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS "" --cflags )
  402. _pkgconfig_invoke_dyn("${_pkg_check_modules_packages}" "${_prefix}" CFLAGS_OTHER "" --cflags-only-other )
  403. if (_imp_target)
  404. _pkg_create_imp_target("${_prefix}" _no_cmake_path _no_cmake_environment_path)
  405. endif()
  406. endif()
  407. if(NOT "${_extra_paths}" STREQUAL "")
  408. # Restore the environment variable
  409. set(ENV{PKG_CONFIG_PATH} "${_pkgconfig_path_old}")
  410. endif()
  411. unset(_extra_paths)
  412. unset(_pkgconfig_path_old)
  413. else()
  414. if (${_is_required})
  415. message(SEND_ERROR "pkg-config tool not found")
  416. endif ()
  417. endif()
  418. endmacro()
  419. ###
  420. ### User visible macros start here
  421. ###
  422. #[========================================[.rst:
  423. .. command:: pkg_check_modules
  424. Checks for all the given modules. ::
  425. pkg_check_modules(<PREFIX> [REQUIRED] [QUIET]
  426. [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
  427. [IMPORTED_TARGET]
  428. <MODULE> [<MODULE>]*)
  429. When the ``REQUIRED`` argument was set, macros will fail with an error
  430. when module(s) could not be found.
  431. When the ``QUIET`` argument is set, no status messages will be printed.
  432. By default, if :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or
  433. later, or if :variable:`PKG_CONFIG_USE_CMAKE_PREFIX_PATH` is set, the
  434. :variable:`CMAKE_PREFIX_PATH`, :variable:`CMAKE_FRAMEWORK_PATH`, and
  435. :variable:`CMAKE_APPBUNDLE_PATH` cache and environment variables will
  436. be added to ``pkg-config`` search path.
  437. The ``NO_CMAKE_PATH`` and ``NO_CMAKE_ENVIRONMENT_PATH`` arguments
  438. disable this behavior for the cache variables and the environment
  439. variables, respectively.
  440. The ``IMPORTED_TARGET`` argument will create an imported target named
  441. PkgConfig::<PREFIX>> that can be passed directly as an argument to
  442. :command:`target_link_libraries`.
  443. It sets the following variables: ::
  444. PKG_CONFIG_FOUND ... if pkg-config executable was found
  445. PKG_CONFIG_EXECUTABLE ... pathname of the pkg-config program
  446. PKG_CONFIG_VERSION_STRING ... the version of the pkg-config program found
  447. (since CMake 2.8.8)
  448. For the following variables two sets of values exist; first one is the
  449. common one and has the given PREFIX. The second set contains flags
  450. which are given out when ``pkg-config`` was called with the ``--static``
  451. option. ::
  452. <XPREFIX>_FOUND ... set to 1 if module(s) exist
  453. <XPREFIX>_LIBRARIES ... only the libraries (w/o the '-l')
  454. <XPREFIX>_LIBRARY_DIRS ... the paths of the libraries (w/o the '-L')
  455. <XPREFIX>_LDFLAGS ... all required linker flags
  456. <XPREFIX>_LDFLAGS_OTHER ... all other linker flags
  457. <XPREFIX>_INCLUDE_DIRS ... the '-I' preprocessor flags (w/o the '-I')
  458. <XPREFIX>_CFLAGS ... all required cflags
  459. <XPREFIX>_CFLAGS_OTHER ... the other compiler flags
  460. ::
  461. <XPREFIX> = <PREFIX> for common case
  462. <XPREFIX> = <PREFIX>_STATIC for static linking
  463. There are some special variables whose prefix depends on the count of
  464. given modules. When there is only one module, <PREFIX> stays
  465. unchanged. When there are multiple modules, the prefix will be
  466. changed to <PREFIX>_<MODNAME>: ::
  467. <XPREFIX>_VERSION ... version of the module
  468. <XPREFIX>_PREFIX ... prefix-directory of the module
  469. <XPREFIX>_INCLUDEDIR ... include-dir of the module
  470. <XPREFIX>_LIBDIR ... lib-dir of the module
  471. ::
  472. <XPREFIX> = <PREFIX> when |MODULES| == 1, else
  473. <XPREFIX> = <PREFIX>_<MODNAME>
  474. A <MODULE> parameter can have the following formats: ::
  475. {MODNAME} ... matches any version
  476. {MODNAME}>={VERSION} ... at least version <VERSION> is required
  477. {MODNAME}={VERSION} ... exactly version <VERSION> is required
  478. {MODNAME}<={VERSION} ... modules must not be newer than <VERSION>
  479. Examples
  480. .. code-block:: cmake
  481. pkg_check_modules (GLIB2 glib-2.0)
  482. .. code-block:: cmake
  483. pkg_check_modules (GLIB2 glib-2.0>=2.10)
  484. Requires at least version 2.10 of glib2 and defines e.g.
  485. ``GLIB2_VERSION=2.10.3``
  486. .. code-block:: cmake
  487. pkg_check_modules (FOO glib-2.0>=2.10 gtk+-2.0)
  488. Requires both glib2 and gtk2, and defines e.g.
  489. ``FOO_glib-2.0_VERSION=2.10.3`` and ``FOO_gtk+-2.0_VERSION=2.8.20``
  490. .. code-block:: cmake
  491. pkg_check_modules (XRENDER REQUIRED xrender)
  492. Defines for example::
  493. XRENDER_LIBRARIES=Xrender;X11``
  494. XRENDER_STATIC_LIBRARIES=Xrender;X11;pthread;Xau;Xdmcp
  495. #]========================================]
  496. macro(pkg_check_modules _prefix _module0)
  497. _pkgconfig_parse_options(_pkg_modules _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
  498. # check cached value
  499. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  500. _pkg_check_modules_internal("${_pkg_is_required}" "${_pkg_is_silent}" ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" ${_pkg_modules})
  501. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  502. elseif (${_prefix}_FOUND AND ${_imp_target})
  503. _pkg_create_imp_target("${_prefix}" _no_cmake_path _no_cmake_environment_path)
  504. endif()
  505. endmacro()
  506. #[========================================[.rst:
  507. .. command:: pkg_search_module
  508. Same as :command:`pkg_check_modules`, but instead it checks for given
  509. modules and uses the first working one. ::
  510. pkg_search_module(<PREFIX> [REQUIRED] [QUIET]
  511. [NO_CMAKE_PATH] [NO_CMAKE_ENVIRONMENT_PATH]
  512. [IMPORTED_TARGET]
  513. <MODULE> [<MODULE>]*)
  514. Examples
  515. .. code-block:: cmake
  516. pkg_search_module (BAR libxml-2.0 libxml2 libxml>=2)
  517. #]========================================]
  518. macro(pkg_search_module _prefix _module0)
  519. _pkgconfig_parse_options(_pkg_modules_alt _pkg_is_required _pkg_is_silent _no_cmake_path _no_cmake_environment_path _imp_target "${_module0}" ${ARGN})
  520. # check cached value
  521. if (NOT DEFINED __pkg_config_checked_${_prefix} OR __pkg_config_checked_${_prefix} LESS ${PKG_CONFIG_VERSION} OR NOT ${_prefix}_FOUND)
  522. set(_pkg_modules_found 0)
  523. if (NOT ${_pkg_is_silent})
  524. message(STATUS "Checking for one of the modules '${_pkg_modules_alt}'")
  525. endif ()
  526. # iterate through all modules and stop at the first working one.
  527. foreach(_pkg_alt ${_pkg_modules_alt})
  528. if(NOT _pkg_modules_found)
  529. _pkg_check_modules_internal(0 1 ${_no_cmake_path} ${_no_cmake_environment_path} ${_imp_target} "${_prefix}" "${_pkg_alt}")
  530. endif()
  531. if (${_prefix}_FOUND)
  532. set(_pkg_modules_found 1)
  533. endif()
  534. endforeach()
  535. if (NOT ${_prefix}_FOUND)
  536. if(${_pkg_is_required})
  537. message(SEND_ERROR "None of the required '${_pkg_modules_alt}' found")
  538. endif()
  539. endif()
  540. _pkgconfig_set(__pkg_config_checked_${_prefix} ${PKG_CONFIG_VERSION})
  541. elseif (${_prefix}_FOUND AND ${_imp_target})
  542. _pkg_create_imp_target("${_prefix}" _no_cmake_path _no_cmake_environment_path)
  543. endif()
  544. endmacro()
  545. #[========================================[.rst:
  546. .. variable:: PKG_CONFIG_EXECUTABLE
  547. Path to the pkg-config executable.
  548. .. variable:: PKG_CONFIG_USE_CMAKE_PREFIX_PATH
  549. Whether :command:`pkg_check_modules` and :command:`pkg_search_module`
  550. should add the paths in :variable:`CMAKE_PREFIX_PATH`,
  551. :variable:`CMAKE_FRAMEWORK_PATH`, and :variable:`CMAKE_APPBUNDLE_PATH`
  552. cache and environment variables to ``pkg-config`` search path.
  553. If this variable is not set, this behavior is enabled by default if
  554. :variable:`CMAKE_MINIMUM_REQUIRED_VERSION` is 3.1 or later, disabled
  555. otherwise.
  556. #]========================================]
  557. ### Local Variables:
  558. ### mode: cmake
  559. ### End: