FindPackageHandleStandardArgs.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. FindPackageHandleStandardArgs
  5. -----------------------------
  6. This module provides a function intended to be used in :ref:`Find Modules`
  7. implementing :command:`find_package(<PackageName>)` calls. It handles the
  8. ``REQUIRED``, ``QUIET`` and version-related arguments of ``find_package``.
  9. It also sets the ``<PackageName>_FOUND`` variable. The package is
  10. considered found if all variables listed contain valid results, e.g.
  11. valid filepaths.
  12. .. command:: find_package_handle_standard_args
  13. There are two signatures::
  14. find_package_handle_standard_args(<PackageName>
  15. (DEFAULT_MSG|<custom-failure-message>)
  16. <required-var>...
  17. )
  18. find_package_handle_standard_args(<PackageName>
  19. [FOUND_VAR <result-var>]
  20. [REQUIRED_VARS <required-var>...]
  21. [VERSION_VAR <version-var>]
  22. [HANDLE_COMPONENTS]
  23. [CONFIG_MODE]
  24. [REASON_FAILURE_MESSAGE <reason-failure-message>]
  25. [FAIL_MESSAGE <custom-failure-message>]
  26. )
  27. The ``<PackageName>_FOUND`` variable will be set to ``TRUE`` if all
  28. the variables ``<required-var>...`` are valid and any optional
  29. constraints are satisfied, and ``FALSE`` otherwise. A success or
  30. failure message may be displayed based on the results and on
  31. whether the ``REQUIRED`` and/or ``QUIET`` option was given to
  32. the :command:`find_package` call.
  33. The options are:
  34. ``(DEFAULT_MSG|<custom-failure-message>)``
  35. In the simple signature this specifies the failure message.
  36. Use ``DEFAULT_MSG`` to ask for a default message to be computed
  37. (recommended). Not valid in the full signature.
  38. ``FOUND_VAR <result-var>``
  39. Obsolete. Specifies either ``<PackageName>_FOUND`` or
  40. ``<PACKAGENAME>_FOUND`` as the result variable. This exists only
  41. for compatibility with older versions of CMake and is now ignored.
  42. Result variables of both names are always set for compatibility.
  43. ``REQUIRED_VARS <required-var>...``
  44. Specify the variables which are required for this package.
  45. These may be named in the generated failure message asking the
  46. user to set the missing variable values. Therefore these should
  47. typically be cache entries such as ``FOO_LIBRARY`` and not output
  48. variables like ``FOO_LIBRARIES``.
  49. ``VERSION_VAR <version-var>``
  50. Specify the name of a variable that holds the version of the package
  51. that has been found. This version will be checked against the
  52. (potentially) specified required version given to the
  53. :command:`find_package` call, including its ``EXACT`` option.
  54. The default messages include information about the required
  55. version and the version which has been actually found, both
  56. if the version is ok or not.
  57. ``HANDLE_COMPONENTS``
  58. Enable handling of package components. In this case, the command
  59. will report which components have been found and which are missing,
  60. and the ``<PackageName>_FOUND`` variable will be set to ``FALSE``
  61. if any of the required components (i.e. not the ones listed after
  62. the ``OPTIONAL_COMPONENTS`` option of :command:`find_package`) are
  63. missing.
  64. ``CONFIG_MODE``
  65. Specify that the calling find module is a wrapper around a
  66. call to ``find_package(<PackageName> NO_MODULE)``. This implies
  67. a ``VERSION_VAR`` value of ``<PackageName>_VERSION``. The command
  68. will automatically check whether the package configuration file
  69. was found.
  70. ``REASON_FAILURE_MESSAGE <reason-failure-message>``
  71. Specify a custom message of the reason for the failure which will be
  72. appended to the default generated message.
  73. ``FAIL_MESSAGE <custom-failure-message>``
  74. Specify a custom failure message instead of using the default
  75. generated message. Not recommended.
  76. Example for the simple signature:
  77. .. code-block:: cmake
  78. find_package_handle_standard_args(LibXml2 DEFAULT_MSG
  79. LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
  80. The ``LibXml2`` package is considered to be found if both
  81. ``LIBXML2_LIBRARY`` and ``LIBXML2_INCLUDE_DIR`` are valid.
  82. Then also ``LibXml2_FOUND`` is set to ``TRUE``. If it is not found
  83. and ``REQUIRED`` was used, it fails with a
  84. :command:`message(FATAL_ERROR)`, independent whether ``QUIET`` was
  85. used or not. If it is found, success will be reported, including
  86. the content of the first ``<required-var>``. On repeated CMake runs,
  87. the same message will not be printed again.
  88. Example for the full signature:
  89. .. code-block:: cmake
  90. find_package_handle_standard_args(LibArchive
  91. REQUIRED_VARS LibArchive_LIBRARY LibArchive_INCLUDE_DIR
  92. VERSION_VAR LibArchive_VERSION)
  93. In this case, the ``LibArchive`` package is considered to be found if
  94. both ``LibArchive_LIBRARY`` and ``LibArchive_INCLUDE_DIR`` are valid.
  95. Also the version of ``LibArchive`` will be checked by using the version
  96. contained in ``LibArchive_VERSION``. Since no ``FAIL_MESSAGE`` is given,
  97. the default messages will be printed.
  98. Another example for the full signature:
  99. .. code-block:: cmake
  100. find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
  101. find_package_handle_standard_args(Automoc4 CONFIG_MODE)
  102. In this case, a ``FindAutmoc4.cmake`` module wraps a call to
  103. ``find_package(Automoc4 NO_MODULE)`` and adds an additional search
  104. directory for ``automoc4``. Then the call to
  105. ``find_package_handle_standard_args`` produces a proper success/failure
  106. message.
  107. #]=======================================================================]
  108. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageMessage.cmake)
  109. # internal helper macro
  110. macro(_FPHSA_FAILURE_MESSAGE _msg)
  111. set (__msg "${_msg}")
  112. if (FPHSA_REASON_FAILURE_MESSAGE)
  113. string(APPEND __msg "\n Reason given by package: ${FPHSA_REASON_FAILURE_MESSAGE}\n")
  114. endif()
  115. if (${_NAME}_FIND_REQUIRED)
  116. message(FATAL_ERROR "${__msg}")
  117. else ()
  118. if (NOT ${_NAME}_FIND_QUIETLY)
  119. message(STATUS "${__msg}")
  120. endif ()
  121. endif ()
  122. endmacro()
  123. # internal helper macro to generate the failure message when used in CONFIG_MODE:
  124. macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
  125. # <PackageName>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
  126. if(${_NAME}_CONFIG)
  127. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing:${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
  128. else()
  129. # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
  130. # List them all in the error message:
  131. if(${_NAME}_CONSIDERED_CONFIGS)
  132. set(configsText "")
  133. list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
  134. math(EXPR configsCount "${configsCount} - 1")
  135. foreach(currentConfigIndex RANGE ${configsCount})
  136. list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
  137. list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
  138. string(APPEND configsText "\n ${filename} (version ${version})")
  139. endforeach()
  140. if (${_NAME}_NOT_FOUND_MESSAGE)
  141. if (FPHSA_REASON_FAILURE_MESSAGE)
  142. string(PREPEND FPHSA_REASON_FAILURE_MESSAGE "${${_NAME}_NOT_FOUND_MESSAGE}\n ")
  143. else()
  144. set(FPHSA_REASON_FAILURE_MESSAGE "${${_NAME}_NOT_FOUND_MESSAGE}")
  145. endif()
  146. else()
  147. string(APPEND configsText "\n")
  148. endif()
  149. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:${configsText}")
  150. else()
  151. # Simple case: No Config-file was found at all:
  152. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
  153. endif()
  154. endif()
  155. endmacro()
  156. function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
  157. # Set up the arguments for `cmake_parse_arguments`.
  158. set(options CONFIG_MODE HANDLE_COMPONENTS)
  159. set(oneValueArgs FAIL_MESSAGE REASON_FAILURE_MESSAGE VERSION_VAR FOUND_VAR)
  160. set(multiValueArgs REQUIRED_VARS)
  161. # Check whether we are in 'simple' or 'extended' mode:
  162. set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
  163. list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
  164. if(${INDEX} EQUAL -1)
  165. set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
  166. set(FPHSA_REQUIRED_VARS ${ARGN})
  167. set(FPHSA_VERSION_VAR)
  168. else()
  169. cmake_parse_arguments(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
  170. if(FPHSA_UNPARSED_ARGUMENTS)
  171. message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
  172. endif()
  173. if(NOT FPHSA_FAIL_MESSAGE)
  174. set(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
  175. endif()
  176. # In config-mode, we rely on the variable <PackageName>_CONFIG, which is set by find_package()
  177. # when it successfully found the config-file, including version checking:
  178. if(FPHSA_CONFIG_MODE)
  179. list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
  180. list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
  181. set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
  182. endif()
  183. if(NOT FPHSA_REQUIRED_VARS)
  184. message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
  185. endif()
  186. endif()
  187. # now that we collected all arguments, process them
  188. if("x${FPHSA_FAIL_MESSAGE}" STREQUAL "xDEFAULT_MSG")
  189. set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
  190. endif()
  191. list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
  192. string(TOUPPER ${_NAME} _NAME_UPPER)
  193. string(TOLOWER ${_NAME} _NAME_LOWER)
  194. if(FPHSA_FOUND_VAR)
  195. if(FPHSA_FOUND_VAR MATCHES "^${_NAME}_FOUND$" OR FPHSA_FOUND_VAR MATCHES "^${_NAME_UPPER}_FOUND$")
  196. set(_FOUND_VAR ${FPHSA_FOUND_VAR})
  197. else()
  198. message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_NAME}_FOUND\" and \"${_NAME_UPPER}_FOUND\" are valid names.")
  199. endif()
  200. else()
  201. set(_FOUND_VAR ${_NAME_UPPER}_FOUND)
  202. endif()
  203. # collect all variables which were not found, so they can be printed, so the
  204. # user knows better what went wrong (#6375)
  205. set(MISSING_VARS "")
  206. set(DETAILS "")
  207. # check if all passed variables are valid
  208. set(FPHSA_FOUND_${_NAME} TRUE)
  209. foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
  210. if(NOT ${_CURRENT_VAR})
  211. set(FPHSA_FOUND_${_NAME} FALSE)
  212. string(APPEND MISSING_VARS " ${_CURRENT_VAR}")
  213. else()
  214. string(APPEND DETAILS "[${${_CURRENT_VAR}}]")
  215. endif()
  216. endforeach()
  217. if(FPHSA_FOUND_${_NAME})
  218. set(${_NAME}_FOUND TRUE)
  219. set(${_NAME_UPPER}_FOUND TRUE)
  220. else()
  221. set(${_NAME}_FOUND FALSE)
  222. set(${_NAME_UPPER}_FOUND FALSE)
  223. endif()
  224. # component handling
  225. unset(FOUND_COMPONENTS_MSG)
  226. unset(MISSING_COMPONENTS_MSG)
  227. if(FPHSA_HANDLE_COMPONENTS)
  228. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  229. if(${_NAME}_${comp}_FOUND)
  230. if(NOT DEFINED FOUND_COMPONENTS_MSG)
  231. set(FOUND_COMPONENTS_MSG "found components:")
  232. endif()
  233. string(APPEND FOUND_COMPONENTS_MSG " ${comp}")
  234. else()
  235. if(NOT DEFINED MISSING_COMPONENTS_MSG)
  236. set(MISSING_COMPONENTS_MSG "missing components:")
  237. endif()
  238. string(APPEND MISSING_COMPONENTS_MSG " ${comp}")
  239. if(${_NAME}_FIND_REQUIRED_${comp})
  240. set(${_NAME}_FOUND FALSE)
  241. string(APPEND MISSING_VARS " ${comp}")
  242. endif()
  243. endif()
  244. endforeach()
  245. set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
  246. string(APPEND DETAILS "[c${COMPONENT_MSG}]")
  247. endif()
  248. # version handling:
  249. set(VERSION_MSG "")
  250. set(VERSION_OK TRUE)
  251. # check with DEFINED here as the requested or found version may be "0"
  252. if (DEFINED ${_NAME}_FIND_VERSION)
  253. if(DEFINED ${FPHSA_VERSION_VAR})
  254. set(_FOUND_VERSION ${${FPHSA_VERSION_VAR}})
  255. if(${_NAME}_FIND_VERSION_EXACT) # exact version required
  256. # count the dots in the version string
  257. string(REGEX REPLACE "[^.]" "" _VERSION_DOTS "${_FOUND_VERSION}")
  258. # add one dot because there is one dot more than there are components
  259. string(LENGTH "${_VERSION_DOTS}." _VERSION_DOTS)
  260. if (_VERSION_DOTS GREATER ${_NAME}_FIND_VERSION_COUNT)
  261. # Because of the C++ implementation of find_package() ${_NAME}_FIND_VERSION_COUNT
  262. # is at most 4 here. Therefore a simple lookup table is used.
  263. if (${_NAME}_FIND_VERSION_COUNT EQUAL 1)
  264. set(_VERSION_REGEX "[^.]*")
  265. elseif (${_NAME}_FIND_VERSION_COUNT EQUAL 2)
  266. set(_VERSION_REGEX "[^.]*\\.[^.]*")
  267. elseif (${_NAME}_FIND_VERSION_COUNT EQUAL 3)
  268. set(_VERSION_REGEX "[^.]*\\.[^.]*\\.[^.]*")
  269. else ()
  270. set(_VERSION_REGEX "[^.]*\\.[^.]*\\.[^.]*\\.[^.]*")
  271. endif ()
  272. string(REGEX REPLACE "^(${_VERSION_REGEX})\\..*" "\\1" _VERSION_HEAD "${_FOUND_VERSION}")
  273. unset(_VERSION_REGEX)
  274. if (NOT ${_NAME}_FIND_VERSION VERSION_EQUAL _VERSION_HEAD)
  275. set(VERSION_MSG "Found unsuitable version \"${_FOUND_VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  276. set(VERSION_OK FALSE)
  277. else ()
  278. set(VERSION_MSG "(found suitable exact version \"${_FOUND_VERSION}\")")
  279. endif ()
  280. unset(_VERSION_HEAD)
  281. else ()
  282. if (NOT ${_NAME}_FIND_VERSION VERSION_EQUAL _FOUND_VERSION)
  283. set(VERSION_MSG "Found unsuitable version \"${_FOUND_VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  284. set(VERSION_OK FALSE)
  285. else ()
  286. set(VERSION_MSG "(found suitable exact version \"${_FOUND_VERSION}\")")
  287. endif ()
  288. endif ()
  289. unset(_VERSION_DOTS)
  290. else() # minimum version specified:
  291. if (${_NAME}_FIND_VERSION VERSION_GREATER _FOUND_VERSION)
  292. set(VERSION_MSG "Found unsuitable version \"${_FOUND_VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
  293. set(VERSION_OK FALSE)
  294. else ()
  295. set(VERSION_MSG "(found suitable version \"${_FOUND_VERSION}\", minimum required is \"${${_NAME}_FIND_VERSION}\")")
  296. endif ()
  297. endif()
  298. else()
  299. # if the package was not found, but a version was given, add that to the output:
  300. if(${_NAME}_FIND_VERSION_EXACT)
  301. set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
  302. else()
  303. set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
  304. endif()
  305. endif()
  306. else ()
  307. # Check with DEFINED as the found version may be 0.
  308. if(DEFINED ${FPHSA_VERSION_VAR})
  309. set(VERSION_MSG "(found version \"${${FPHSA_VERSION_VAR}}\")")
  310. endif()
  311. endif ()
  312. if(VERSION_OK)
  313. string(APPEND DETAILS "[v${${FPHSA_VERSION_VAR}}(${${_NAME}_FIND_VERSION})]")
  314. else()
  315. set(${_NAME}_FOUND FALSE)
  316. endif()
  317. # print the result:
  318. if (${_NAME}_FOUND)
  319. FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
  320. else ()
  321. if(FPHSA_CONFIG_MODE)
  322. _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
  323. else()
  324. if(NOT VERSION_OK)
  325. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
  326. else()
  327. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing:${MISSING_VARS}) ${VERSION_MSG}")
  328. endif()
  329. endif()
  330. endif ()
  331. set(${_NAME}_FOUND ${${_NAME}_FOUND} PARENT_SCOPE)
  332. set(${_NAME_UPPER}_FOUND ${${_NAME}_FOUND} PARENT_SCOPE)
  333. endfunction()