FindPackageHandleStandardArgs.cmake 16 KB

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