FindPackageHandleStandardArgs.cmake 17 KB

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