FindPackageHandleStandardArgs.cmake 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 functions intended to be used in :ref:`Find Modules`
  7. implementing :command:`find_package(<PackageName>)` calls.
  8. .. command:: find_package_handle_standard_args
  9. This command handles the ``REQUIRED``, ``QUIET`` and version-related
  10. arguments of :command:`find_package`. It also sets the
  11. ``<PackageName>_FOUND`` variable. The package is considered found if all
  12. variables listed contain valid results, e.g. valid filepaths.
  13. There are two signatures:
  14. .. code-block:: cmake
  15. find_package_handle_standard_args(<PackageName>
  16. (DEFAULT_MSG|<custom-failure-message>)
  17. <required-var>...
  18. )
  19. find_package_handle_standard_args(<PackageName>
  20. [FOUND_VAR <result-var>]
  21. [REQUIRED_VARS <required-var>...]
  22. [VERSION_VAR <version-var>]
  23. [HANDLE_VERSION_RANGE]
  24. [HANDLE_COMPONENTS]
  25. [CONFIG_MODE]
  26. [NAME_MISMATCHED]
  27. [REASON_FAILURE_MESSAGE <reason-failure-message>]
  28. [FAIL_MESSAGE <custom-failure-message>]
  29. )
  30. The ``<PackageName>_FOUND`` variable will be set to ``TRUE`` if all
  31. the variables ``<required-var>...`` are valid and any optional
  32. constraints are satisfied, and ``FALSE`` otherwise. A success or
  33. failure message may be displayed based on the results and on
  34. whether the ``REQUIRED`` and/or ``QUIET`` option was given to
  35. the :command:`find_package` call.
  36. The options are:
  37. ``(DEFAULT_MSG|<custom-failure-message>)``
  38. In the simple signature this specifies the failure message.
  39. Use ``DEFAULT_MSG`` to ask for a default message to be computed
  40. (recommended). Not valid in the full signature.
  41. ``FOUND_VAR <result-var>``
  42. .. deprecated:: 3.3
  43. Specifies either ``<PackageName>_FOUND`` or
  44. ``<PACKAGENAME>_FOUND`` as the result variable. This exists only
  45. for compatibility with older versions of CMake and is now ignored.
  46. Result variables of both names are always set for compatibility.
  47. ``REQUIRED_VARS <required-var>...``
  48. Specify the variables which are required for this package.
  49. These may be named in the generated failure message asking the
  50. user to set the missing variable values. Therefore these should
  51. typically be cache entries such as ``FOO_LIBRARY`` and not output
  52. variables like ``FOO_LIBRARIES``.
  53. .. versionchanged:: 3.18
  54. If ``HANDLE_COMPONENTS`` is specified, this option can be omitted.
  55. ``VERSION_VAR <version-var>``
  56. Specify the name of a variable that holds the version of the package
  57. that has been found. This version will be checked against the
  58. (potentially) specified required version given to the
  59. :command:`find_package` call, including its ``EXACT`` option.
  60. The default messages include information about the required
  61. version and the version which has been actually found, both
  62. if the version is ok or not.
  63. ``HANDLE_VERSION_RANGE``
  64. .. versionadded:: 3.19
  65. Enable handling of a version range, if one is specified. Without this
  66. option, a developer warning will be displayed if a version range is
  67. specified.
  68. ``HANDLE_COMPONENTS``
  69. Enable handling of package components. In this case, the command
  70. will report which components have been found and which are missing,
  71. and the ``<PackageName>_FOUND`` variable will be set to ``FALSE``
  72. if any of the required components (i.e. not the ones listed after
  73. the ``OPTIONAL_COMPONENTS`` option of :command:`find_package`) are
  74. missing.
  75. ``CONFIG_MODE``
  76. Specify that the calling find module is a wrapper around a
  77. call to ``find_package(<PackageName> NO_MODULE)``. This implies
  78. a ``VERSION_VAR`` value of ``<PackageName>_VERSION``. The command
  79. will automatically check whether the package configuration file
  80. was found.
  81. ``REASON_FAILURE_MESSAGE <reason-failure-message>``
  82. .. versionadded:: 3.16
  83. Specify a custom message of the reason for the failure which will be
  84. appended to the default generated message.
  85. ``FAIL_MESSAGE <custom-failure-message>``
  86. Specify a custom failure message instead of using the default
  87. generated message. Not recommended.
  88. ``NAME_MISMATCHED``
  89. .. versionadded:: 3.17
  90. Indicate that the ``<PackageName>`` does not match
  91. ``${CMAKE_FIND_PACKAGE_NAME}``. This is usually a mistake and raises a
  92. warning, but it may be intentional for usage of the command for components
  93. of a larger package.
  94. Example for the simple signature:
  95. .. code-block:: cmake
  96. find_package_handle_standard_args(LibXml2 DEFAULT_MSG
  97. LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
  98. The ``LibXml2`` package is considered to be found if both
  99. ``LIBXML2_LIBRARY`` and ``LIBXML2_INCLUDE_DIR`` are valid.
  100. Then also ``LibXml2_FOUND`` is set to ``TRUE``. If it is not found
  101. and ``REQUIRED`` was used, it fails with a
  102. :command:`message(FATAL_ERROR)`, independent whether ``QUIET`` was
  103. used or not. If it is found, success will be reported, including
  104. the content of the first ``<required-var>``. On repeated CMake runs,
  105. the same message will not be printed again.
  106. .. note::
  107. If ``<PackageName>`` does not match ``CMAKE_FIND_PACKAGE_NAME`` for the
  108. calling module, a warning that there is a mismatch is given. The
  109. ``FPHSA_NAME_MISMATCHED`` variable may be set to bypass the warning if using
  110. the old signature and the ``NAME_MISMATCHED`` argument using the new
  111. signature. To avoid forcing the caller to require newer versions of CMake for
  112. usage, the variable's value will be used if defined when the
  113. ``NAME_MISMATCHED`` argument is not passed for the new signature (but using
  114. both is an error)..
  115. Example for the full signature:
  116. .. code-block:: cmake
  117. find_package_handle_standard_args(LibArchive
  118. REQUIRED_VARS LibArchive_LIBRARY LibArchive_INCLUDE_DIR
  119. VERSION_VAR LibArchive_VERSION)
  120. In this case, the ``LibArchive`` package is considered to be found if
  121. both ``LibArchive_LIBRARY`` and ``LibArchive_INCLUDE_DIR`` are valid.
  122. Also the version of ``LibArchive`` will be checked by using the version
  123. contained in ``LibArchive_VERSION``. Since no ``FAIL_MESSAGE`` is given,
  124. the default messages will be printed.
  125. Another example for the full signature:
  126. .. code-block:: cmake
  127. find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
  128. find_package_handle_standard_args(Automoc4 CONFIG_MODE)
  129. In this case, a ``FindAutmoc4.cmake`` module wraps a call to
  130. ``find_package(Automoc4 NO_MODULE)`` and adds an additional search
  131. directory for ``automoc4``. Then the call to
  132. ``find_package_handle_standard_args`` produces a proper success/failure
  133. message.
  134. .. command:: find_package_check_version
  135. .. versionadded:: 3.19
  136. Helper function which can be used to check if a ``<version>`` is valid
  137. against version-related arguments of :command:`find_package`.
  138. .. code-block:: cmake
  139. find_package_check_version(<version> <result-var>
  140. [HANDLE_VERSION_RANGE]
  141. [RESULT_MESSAGE_VARIABLE <message-var>]
  142. )
  143. The ``<result-var>`` will hold a boolean value giving the result of the check.
  144. The options are:
  145. ``HANDLE_VERSION_RANGE``
  146. Enable handling of a version range, if one is specified. Without this
  147. option, a developer warning will be displayed if a version range is
  148. specified.
  149. ``RESULT_MESSAGE_VARIABLE <message-var>``
  150. Specify a variable to get back a message describing the result of the check.
  151. Example for the usage:
  152. .. code-block:: cmake
  153. find_package_check_version(1.2.3 result HANDLE_VERSION_RANGE
  154. RESULT_MESSAGE_VARIABLE reason)
  155. if (result)
  156. message (STATUS "${reason}")
  157. else()
  158. message (FATAL_ERROR "${reason}")
  159. endif()
  160. #]=======================================================================]
  161. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageMessage.cmake)
  162. cmake_policy(PUSH)
  163. # numbers and boolean constants
  164. cmake_policy (SET CMP0012 NEW)
  165. # IN_LIST operator
  166. cmake_policy (SET CMP0057 NEW)
  167. # internal helper macro
  168. macro(_FPHSA_FAILURE_MESSAGE _msg)
  169. set (__msg "${_msg}")
  170. if (FPHSA_REASON_FAILURE_MESSAGE)
  171. string(APPEND __msg "\n Reason given by package: ${FPHSA_REASON_FAILURE_MESSAGE}\n")
  172. endif()
  173. if (${_NAME}_FIND_REQUIRED)
  174. message(FATAL_ERROR "${__msg}")
  175. else ()
  176. if (NOT ${_NAME}_FIND_QUIETLY)
  177. message(STATUS "${__msg}")
  178. endif ()
  179. endif ()
  180. endmacro()
  181. # internal helper macro to generate the failure message when used in CONFIG_MODE:
  182. macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
  183. # <PackageName>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
  184. if(${_NAME}_CONFIG)
  185. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing:${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
  186. else()
  187. # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
  188. # List them all in the error message:
  189. if(${_NAME}_CONSIDERED_CONFIGS)
  190. set(configsText "")
  191. list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
  192. math(EXPR configsCount "${configsCount} - 1")
  193. foreach(currentConfigIndex RANGE ${configsCount})
  194. list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
  195. list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
  196. string(APPEND configsText "\n ${filename} (version ${version})")
  197. endforeach()
  198. if (${_NAME}_NOT_FOUND_MESSAGE)
  199. if (FPHSA_REASON_FAILURE_MESSAGE)
  200. string(PREPEND FPHSA_REASON_FAILURE_MESSAGE "${${_NAME}_NOT_FOUND_MESSAGE}\n ")
  201. else()
  202. set(FPHSA_REASON_FAILURE_MESSAGE "${${_NAME}_NOT_FOUND_MESSAGE}")
  203. endif()
  204. else()
  205. string(APPEND configsText "\n")
  206. endif()
  207. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:${configsText}")
  208. else()
  209. # Simple case: No Config-file was found at all:
  210. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
  211. endif()
  212. endif()
  213. endmacro()
  214. function(FIND_PACKAGE_CHECK_VERSION version result)
  215. cmake_parse_arguments (PARSE_ARGV 2 FPCV "HANDLE_VERSION_RANGE;NO_AUTHOR_WARNING_VERSION_RANGE" "RESULT_MESSAGE_VARIABLE" "")
  216. if (FPCV_UNPARSED_ARGUMENTS)
  217. message (FATAL_ERROR "find_package_check_version(): ${FPCV_UNPARSED_ARGUMENTS}: unexpected arguments")
  218. endif()
  219. if ("RESULT_MESSAGE_VARIABLE" IN_LIST FPCV_KEYWORDS_MISSING_VALUES)
  220. message (FATAL_ERROR "find_package_check_version(): RESULT_MESSAGE_VARIABLE expects an argument")
  221. endif()
  222. set (${result} FALSE PARENT_SCOPE)
  223. if (FPCV_RESULT_MESSAGE_VARIABLE)
  224. unset (${FPCV_RESULT_MESSAGE_VARIABLE} PARENT_SCOPE)
  225. endif()
  226. if (_CMAKE_FPHSA_PACKAGE_NAME)
  227. set (package "${_CMAKE_FPHSA_PACKAGE_NAME}")
  228. elseif (CMAKE_FIND_PACKAGE_NAME)
  229. set (package "${CMAKE_FIND_PACKAGE_NAME}")
  230. else()
  231. message (FATAL_ERROR "find_package_check_version(): Cannot be used outside a 'Find Module'")
  232. endif()
  233. if (NOT FPCV_NO_AUTHOR_WARNING_VERSION_RANGE
  234. AND ${package}_FIND_VERSION_RANGE AND NOT FPCV_HANDLE_VERSION_RANGE)
  235. message(AUTHOR_WARNING
  236. "`find_package()` specify a version range but the option "
  237. "HANDLE_VERSION_RANGE` is not passed to `find_package_check_version()`. "
  238. "Only the lower endpoint of the range will be used.")
  239. endif()
  240. set (version_ok FALSE)
  241. unset (version_msg)
  242. if (FPCV_HANDLE_VERSION_RANGE AND ${package}_FIND_VERSION_RANGE)
  243. if ((${package}_FIND_VERSION_RANGE_MIN STREQUAL "INCLUDE"
  244. AND version VERSION_GREATER_EQUAL ${package}_FIND_VERSION_MIN)
  245. AND ((${package}_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE"
  246. AND version VERSION_LESS_EQUAL ${package}_FIND_VERSION_MAX)
  247. OR (${package}_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE"
  248. AND version VERSION_LESS ${package}_FIND_VERSION_MAX)))
  249. set (version_ok TRUE)
  250. set(version_msg "(found suitable version \"${version}\", required range is \"${${package}_FIND_VERSION_RANGE}\")")
  251. else()
  252. set(version_msg "Found unsuitable version \"${version}\", required range is \"${${package}_FIND_VERSION_RANGE}\"")
  253. endif()
  254. elseif (DEFINED ${package}_FIND_VERSION)
  255. if(${package}_FIND_VERSION_EXACT) # exact version required
  256. # count the dots in the version string
  257. string(REGEX REPLACE "[^.]" "" version_dots "${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 ${package}_FIND_VERSION_COUNT)
  261. # Because of the C++ implementation of find_package() ${package}_FIND_VERSION_COUNT
  262. # is at most 4 here. Therefore a simple lookup table is used.
  263. if (${package}_FIND_VERSION_COUNT EQUAL 1)
  264. set(version_regex "[^.]*")
  265. elseif (${package}_FIND_VERSION_COUNT EQUAL 2)
  266. set(version_regex "[^.]*\\.[^.]*")
  267. elseif (${package}_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 "${version}")
  273. if (NOT ${package}_FIND_VERSION VERSION_EQUAL version_head)
  274. set(version_msg "Found unsuitable version \"${version}\", but required is exact version \"${${package}_FIND_VERSION}\"")
  275. else ()
  276. set(version_ok TRUE)
  277. set(version_msg "(found suitable exact version \"${version}\")")
  278. endif ()
  279. else ()
  280. if (NOT ${package}_FIND_VERSION VERSION_EQUAL version)
  281. set(version_msg "Found unsuitable version \"${version}\", but required is exact version \"${${package}_FIND_VERSION}\"")
  282. else ()
  283. set(version_ok TRUE)
  284. set(version_msg "(found suitable exact version \"${version}\")")
  285. endif ()
  286. endif ()
  287. else() # minimum version
  288. if (${package}_FIND_VERSION VERSION_GREATER version)
  289. set(version_msg "Found unsuitable version \"${version}\", but required is at least \"${${package}_FIND_VERSION}\"")
  290. else()
  291. set(version_ok TRUE)
  292. set(version_msg "(found suitable version \"${version}\", minimum required is \"${${package}_FIND_VERSION}\")")
  293. endif()
  294. endif()
  295. else ()
  296. set(version_ok TRUE)
  297. set(version_msg "(found version \"${version}\")")
  298. endif()
  299. set (${result} ${version_ok} PARENT_SCOPE)
  300. if (FPCV_RESULT_MESSAGE_VARIABLE)
  301. set (${FPCV_RESULT_MESSAGE_VARIABLE} "${version_msg}" PARENT_SCOPE)
  302. endif()
  303. endfunction()
  304. function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
  305. # Set up the arguments for `cmake_parse_arguments`.
  306. set(options CONFIG_MODE HANDLE_COMPONENTS NAME_MISMATCHED HANDLE_VERSION_RANGE)
  307. set(oneValueArgs FAIL_MESSAGE REASON_FAILURE_MESSAGE VERSION_VAR FOUND_VAR)
  308. set(multiValueArgs REQUIRED_VARS)
  309. # Check whether we are in 'simple' or 'extended' mode:
  310. set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
  311. list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
  312. unset(FPHSA_NAME_MISMATCHED_override)
  313. if (DEFINED FPHSA_NAME_MISMATCHED)
  314. # If the variable NAME_MISMATCHED variable is set, error if it is passed as
  315. # an argument. The former is for old signatures, the latter is for new
  316. # signatures.
  317. list(FIND ARGN "NAME_MISMATCHED" name_mismatched_idx)
  318. if (NOT name_mismatched_idx EQUAL "-1")
  319. message(FATAL_ERROR
  320. "The `NAME_MISMATCHED` argument may only be specified by the argument or "
  321. "the variable, not both.")
  322. endif ()
  323. # But use the variable if it is not an argument to avoid forcing minimum
  324. # CMake version bumps for calling modules.
  325. set(FPHSA_NAME_MISMATCHED_override "${FPHSA_NAME_MISMATCHED}")
  326. endif ()
  327. if(${INDEX} EQUAL -1)
  328. set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
  329. set(FPHSA_REQUIRED_VARS ${ARGN})
  330. set(FPHSA_VERSION_VAR)
  331. else()
  332. cmake_parse_arguments(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
  333. if(FPHSA_UNPARSED_ARGUMENTS)
  334. message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
  335. endif()
  336. if(NOT FPHSA_FAIL_MESSAGE)
  337. set(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
  338. endif()
  339. # In config-mode, we rely on the variable <PackageName>_CONFIG, which is set by find_package()
  340. # when it successfully found the config-file, including version checking:
  341. if(FPHSA_CONFIG_MODE)
  342. list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
  343. list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
  344. set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
  345. endif()
  346. if(NOT FPHSA_REQUIRED_VARS AND NOT FPHSA_HANDLE_COMPONENTS)
  347. message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
  348. endif()
  349. endif()
  350. if (DEFINED FPHSA_NAME_MISMATCHED_override)
  351. set(FPHSA_NAME_MISMATCHED "${FPHSA_NAME_MISMATCHED_override}")
  352. endif ()
  353. if (DEFINED CMAKE_FIND_PACKAGE_NAME
  354. AND NOT FPHSA_NAME_MISMATCHED
  355. AND NOT _NAME STREQUAL CMAKE_FIND_PACKAGE_NAME)
  356. message(AUTHOR_WARNING
  357. "The package name passed to `find_package_handle_standard_args` "
  358. "(${_NAME}) does not match the name of the calling package "
  359. "(${CMAKE_FIND_PACKAGE_NAME}). This can lead to problems in calling "
  360. "code that expects `find_package` result variables (e.g., `_FOUND`) "
  361. "to follow a certain pattern.")
  362. endif ()
  363. if (${_NAME}_FIND_VERSION_RANGE AND NOT FPHSA_HANDLE_VERSION_RANGE)
  364. message(AUTHOR_WARNING
  365. "`find_package()` specify a version range but the module ${_NAME} does "
  366. "not support this capability. Only the lower endpoint of the range "
  367. "will be used.")
  368. endif()
  369. # to propagate package name to FIND_PACKAGE_CHECK_VERSION
  370. set(_CMAKE_FPHSA_PACKAGE_NAME "${_NAME}")
  371. # now that we collected all arguments, process them
  372. if("x${FPHSA_FAIL_MESSAGE}" STREQUAL "xDEFAULT_MSG")
  373. set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
  374. endif()
  375. if (FPHSA_REQUIRED_VARS)
  376. list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
  377. endif()
  378. string(TOUPPER ${_NAME} _NAME_UPPER)
  379. string(TOLOWER ${_NAME} _NAME_LOWER)
  380. if(FPHSA_FOUND_VAR)
  381. set(_FOUND_VAR_UPPER ${_NAME_UPPER}_FOUND)
  382. set(_FOUND_VAR_MIXED ${_NAME}_FOUND)
  383. if(FPHSA_FOUND_VAR STREQUAL _FOUND_VAR_MIXED OR FPHSA_FOUND_VAR STREQUAL _FOUND_VAR_UPPER)
  384. set(_FOUND_VAR ${FPHSA_FOUND_VAR})
  385. else()
  386. message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_FOUND_VAR_MIXED}\" and \"${_FOUND_VAR_UPPER}\" are valid names.")
  387. endif()
  388. else()
  389. set(_FOUND_VAR ${_NAME_UPPER}_FOUND)
  390. endif()
  391. # collect all variables which were not found, so they can be printed, so the
  392. # user knows better what went wrong (#6375)
  393. set(MISSING_VARS "")
  394. set(DETAILS "")
  395. # check if all passed variables are valid
  396. set(FPHSA_FOUND_${_NAME} TRUE)
  397. foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
  398. if(NOT ${_CURRENT_VAR})
  399. set(FPHSA_FOUND_${_NAME} FALSE)
  400. string(APPEND MISSING_VARS " ${_CURRENT_VAR}")
  401. else()
  402. string(APPEND DETAILS "[${${_CURRENT_VAR}}]")
  403. endif()
  404. endforeach()
  405. if(FPHSA_FOUND_${_NAME})
  406. set(${_NAME}_FOUND TRUE)
  407. set(${_NAME_UPPER}_FOUND TRUE)
  408. else()
  409. set(${_NAME}_FOUND FALSE)
  410. set(${_NAME_UPPER}_FOUND FALSE)
  411. endif()
  412. # component handling
  413. unset(FOUND_COMPONENTS_MSG)
  414. unset(MISSING_COMPONENTS_MSG)
  415. if(FPHSA_HANDLE_COMPONENTS)
  416. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  417. if(${_NAME}_${comp}_FOUND)
  418. if(NOT DEFINED FOUND_COMPONENTS_MSG)
  419. set(FOUND_COMPONENTS_MSG "found components:")
  420. endif()
  421. string(APPEND FOUND_COMPONENTS_MSG " ${comp}")
  422. else()
  423. if(NOT DEFINED MISSING_COMPONENTS_MSG)
  424. set(MISSING_COMPONENTS_MSG "missing components:")
  425. endif()
  426. string(APPEND MISSING_COMPONENTS_MSG " ${comp}")
  427. if(${_NAME}_FIND_REQUIRED_${comp})
  428. set(${_NAME}_FOUND FALSE)
  429. string(APPEND MISSING_VARS " ${comp}")
  430. endif()
  431. endif()
  432. endforeach()
  433. set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
  434. string(APPEND DETAILS "[c${COMPONENT_MSG}]")
  435. endif()
  436. # version handling:
  437. set(VERSION_MSG "")
  438. set(VERSION_OK TRUE)
  439. # check that the version variable is not empty to avoid emitting a misleading
  440. # message (i.e. `Found unsuitable version ""`)
  441. if (DEFINED ${_NAME}_FIND_VERSION)
  442. if(DEFINED ${FPHSA_VERSION_VAR})
  443. if(NOT "${${FPHSA_VERSION_VAR}}" STREQUAL "")
  444. set(_FOUND_VERSION ${${FPHSA_VERSION_VAR}})
  445. if (FPHSA_HANDLE_VERSION_RANGE)
  446. set (FPCV_HANDLE_VERSION_RANGE HANDLE_VERSION_RANGE)
  447. else()
  448. set(FPCV_HANDLE_VERSION_RANGE NO_AUTHOR_WARNING_VERSION_RANGE)
  449. endif()
  450. find_package_check_version ("${_FOUND_VERSION}" VERSION_OK RESULT_MESSAGE_VARIABLE VERSION_MSG
  451. ${FPCV_HANDLE_VERSION_RANGE})
  452. else()
  453. set(VERSION_OK FALSE)
  454. endif()
  455. endif()
  456. if("${${FPHSA_VERSION_VAR}}" STREQUAL "")
  457. # if the package was not found, but a version was given, add that to the output:
  458. if(${_NAME}_FIND_VERSION_EXACT)
  459. set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
  460. elseif (FPHSA_HANDLE_VERSION_RANGE AND ${_NAME}_FIND_VERSION_RANGE)
  461. set(VERSION_MSG "(Required is version range \"${${_NAME}_FIND_VERSION_RANGE}\")")
  462. else()
  463. set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
  464. endif()
  465. endif()
  466. else ()
  467. # Check with DEFINED as the found version may be 0.
  468. if(DEFINED ${FPHSA_VERSION_VAR})
  469. set(VERSION_MSG "(found version \"${${FPHSA_VERSION_VAR}}\")")
  470. endif()
  471. endif ()
  472. if(VERSION_OK)
  473. string(APPEND DETAILS "[v${${FPHSA_VERSION_VAR}}(${${_NAME}_FIND_VERSION})]")
  474. else()
  475. set(${_NAME}_FOUND FALSE)
  476. endif()
  477. # print the result:
  478. if (${_NAME}_FOUND)
  479. FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
  480. else ()
  481. if(FPHSA_CONFIG_MODE)
  482. _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
  483. else()
  484. if(NOT VERSION_OK)
  485. set(RESULT_MSG)
  486. if (_FIRST_REQUIRED_VAR)
  487. string (APPEND RESULT_MSG "found ${${_FIRST_REQUIRED_VAR}}")
  488. endif()
  489. if (COMPONENT_MSG)
  490. if (RESULT_MSG)
  491. string (APPEND RESULT_MSG ", ")
  492. endif()
  493. string (APPEND RESULT_MSG "${FOUND_COMPONENTS_MSG}")
  494. endif()
  495. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (${RESULT_MSG})")
  496. else()
  497. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing:${MISSING_VARS}) ${VERSION_MSG}")
  498. endif()
  499. endif()
  500. endif ()
  501. set(${_NAME}_FOUND ${${_NAME}_FOUND} PARENT_SCOPE)
  502. set(${_NAME_UPPER}_FOUND ${${_NAME}_FOUND} PARENT_SCOPE)
  503. endfunction()
  504. cmake_policy(POP)