FindPkgConfig.cmake 24 KB

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