FindPackageHandleStandardArgs.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #.rst:
  2. # FindPackageHandleStandardArgs
  3. # -----------------------------
  4. #
  5. #
  6. #
  7. # FIND_PACKAGE_HANDLE_STANDARD_ARGS(<name> ... )
  8. #
  9. # This function is intended to be used in FindXXX.cmake modules files.
  10. # It handles the REQUIRED, QUIET and version-related arguments to
  11. # find_package(). It also sets the <packagename>_FOUND variable. The
  12. # package is considered found if all variables <var1>... listed contain
  13. # valid results, e.g. valid filepaths.
  14. #
  15. # There are two modes of this function. The first argument in both
  16. # modes is the name of the Find-module where it is called (in original
  17. # casing).
  18. #
  19. # The first simple mode looks like this:
  20. #
  21. # ::
  22. #
  23. # FIND_PACKAGE_HANDLE_STANDARD_ARGS(<name>
  24. # (DEFAULT_MSG|"Custom failure message") <var1>...<varN> )
  25. #
  26. # If the variables <var1> to <varN> are all valid, then
  27. # <UPPERCASED_NAME>_FOUND will be set to TRUE. If DEFAULT_MSG is given
  28. # as second argument, then the function will generate itself useful
  29. # success and error messages. You can also supply a custom error
  30. # message for the failure case. This is not recommended.
  31. #
  32. # The second mode is more powerful and also supports version checking:
  33. #
  34. # ::
  35. #
  36. # FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME
  37. # [FOUND_VAR <resultVar>]
  38. # [REQUIRED_VARS <var1>...<varN>]
  39. # [VERSION_VAR <versionvar>]
  40. # [HANDLE_COMPONENTS]
  41. # [CONFIG_MODE]
  42. # [FAIL_MESSAGE "Custom failure message"] )
  43. #
  44. # In this mode, the name of the result-variable can be set either to
  45. # either <UPPERCASED_NAME>_FOUND or <OriginalCase_Name>_FOUND using the
  46. # FOUND_VAR option. Other names for the result-variable are not
  47. # allowed. So for a Find-module named FindFooBar.cmake, the two
  48. # possible names are FooBar_FOUND and FOOBAR_FOUND. It is recommended
  49. # to use the original case version. If the FOUND_VAR option is not
  50. # used, the default is <UPPERCASED_NAME>_FOUND.
  51. #
  52. # As in the simple mode, if <var1> through <varN> are all valid,
  53. # <packagename>_FOUND will be set to TRUE. After REQUIRED_VARS the
  54. # variables which are required for this package are listed. Following
  55. # VERSION_VAR the name of the variable can be specified which holds the
  56. # version of the package which has been found. If this is done, this
  57. # version will be checked against the (potentially) specified required
  58. # version used in the find_package() call. The EXACT keyword is also
  59. # handled. The default messages include information about the required
  60. # version and the version which has been actually found, both if the
  61. # version is ok or not. If the package supports components, use the
  62. # HANDLE_COMPONENTS option to enable handling them. In this case,
  63. # find_package_handle_standard_args() will report which components have
  64. # been found and which are missing, and the <packagename>_FOUND variable
  65. # will be set to FALSE if any of the required components (i.e. not the
  66. # ones listed after OPTIONAL_COMPONENTS) are missing. Use the option
  67. # CONFIG_MODE if your FindXXX.cmake module is a wrapper for a
  68. # find_package(... NO_MODULE) call. In this case VERSION_VAR will be
  69. # set to <NAME>_VERSION and the macro will automatically check whether
  70. # the Config module was found. Via FAIL_MESSAGE a custom failure
  71. # message can be specified, if this is not used, the default message
  72. # will be displayed.
  73. #
  74. # Example for mode 1:
  75. #
  76. # ::
  77. #
  78. # find_package_handle_standard_args(LibXml2 DEFAULT_MSG
  79. # LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
  80. #
  81. #
  82. #
  83. # LibXml2 is considered to be found, if both LIBXML2_LIBRARY and
  84. # LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to
  85. # TRUE. If it is not found and REQUIRED was used, it fails with
  86. # FATAL_ERROR, independent whether QUIET was used or not. If it is
  87. # found, success will be reported, including the content of <var1>. On
  88. # repeated Cmake runs, the same message won't be printed again.
  89. #
  90. # Example for mode 2:
  91. #
  92. # ::
  93. #
  94. # find_package_handle_standard_args(LibXslt
  95. # FOUND_VAR LibXslt_FOUND
  96. # REQUIRED_VARS LibXslt_LIBRARIES LibXslt_INCLUDE_DIRS
  97. # VERSION_VAR LibXslt_VERSION_STRING)
  98. #
  99. # In this case, LibXslt is considered to be found if the variable(s)
  100. # listed after REQUIRED_VAR are all valid, i.e. LibXslt_LIBRARIES and
  101. # LibXslt_INCLUDE_DIRS in this case. The result will then be stored in
  102. # LibXslt_FOUND . Also the version of LibXslt will be checked by using
  103. # the version contained in LibXslt_VERSION_STRING. Since no
  104. # FAIL_MESSAGE is given, the default messages will be printed.
  105. #
  106. # Another example for mode 2:
  107. #
  108. # ::
  109. #
  110. # find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
  111. # find_package_handle_standard_args(Automoc4 CONFIG_MODE)
  112. #
  113. # In this case, FindAutmoc4.cmake wraps a call to find_package(Automoc4
  114. # NO_MODULE) and adds an additional search directory for automoc4. Here
  115. # the result will be stored in AUTOMOC4_FOUND. The following
  116. # FIND_PACKAGE_HANDLE_STANDARD_ARGS() call produces a proper
  117. # success/error message.
  118. #=============================================================================
  119. # Copyright 2007-2009 Kitware, Inc.
  120. #
  121. # Distributed under the OSI-approved BSD License (the "License");
  122. # see accompanying file Copyright.txt for details.
  123. #
  124. # This software is distributed WITHOUT ANY WARRANTY; without even the
  125. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  126. # See the License for more information.
  127. #=============================================================================
  128. # (To distribute this file outside of CMake, substitute the full
  129. # License text for the above reference.)
  130. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageMessage.cmake)
  131. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  132. # internal helper macro
  133. macro(_FPHSA_FAILURE_MESSAGE _msg)
  134. if (${_NAME}_FIND_REQUIRED)
  135. message(FATAL_ERROR "${_msg}")
  136. else ()
  137. if (NOT ${_NAME}_FIND_QUIETLY)
  138. message(STATUS "${_msg}")
  139. endif ()
  140. endif ()
  141. endmacro()
  142. # internal helper macro to generate the failure message when used in CONFIG_MODE:
  143. macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
  144. # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
  145. if(${_NAME}_CONFIG)
  146. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
  147. else()
  148. # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
  149. # List them all in the error message:
  150. if(${_NAME}_CONSIDERED_CONFIGS)
  151. set(configsText "")
  152. list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
  153. math(EXPR configsCount "${configsCount} - 1")
  154. foreach(currentConfigIndex RANGE ${configsCount})
  155. list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
  156. list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
  157. set(configsText "${configsText} ${filename} (version ${version})\n")
  158. endforeach()
  159. if (${_NAME}_NOT_FOUND_MESSAGE)
  160. set(configsText "${configsText} Reason given by package: ${${_NAME}_NOT_FOUND_MESSAGE}\n")
  161. endif()
  162. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
  163. else()
  164. # Simple case: No Config-file was found at all:
  165. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
  166. endif()
  167. endif()
  168. endmacro()
  169. function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
  170. # set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in
  171. # new extended or in the "old" mode:
  172. set(options CONFIG_MODE HANDLE_COMPONENTS)
  173. set(oneValueArgs FAIL_MESSAGE VERSION_VAR FOUND_VAR)
  174. set(multiValueArgs REQUIRED_VARS)
  175. set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
  176. list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
  177. if(${INDEX} EQUAL -1)
  178. set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
  179. set(FPHSA_REQUIRED_VARS ${ARGN})
  180. set(FPHSA_VERSION_VAR)
  181. else()
  182. CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
  183. if(FPHSA_UNPARSED_ARGUMENTS)
  184. message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
  185. endif()
  186. if(NOT FPHSA_FAIL_MESSAGE)
  187. set(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
  188. endif()
  189. endif()
  190. # now that we collected all arguments, process them
  191. if("x${FPHSA_FAIL_MESSAGE}" STREQUAL "xDEFAULT_MSG")
  192. set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
  193. endif()
  194. # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
  195. # when it successfully found the config-file, including version checking:
  196. if(FPHSA_CONFIG_MODE)
  197. list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
  198. list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
  199. set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
  200. endif()
  201. if(NOT FPHSA_REQUIRED_VARS)
  202. message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
  203. endif()
  204. list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
  205. string(TOUPPER ${_NAME} _NAME_UPPER)
  206. string(TOLOWER ${_NAME} _NAME_LOWER)
  207. if(FPHSA_FOUND_VAR)
  208. if(FPHSA_FOUND_VAR MATCHES "^${_NAME}_FOUND$" OR FPHSA_FOUND_VAR MATCHES "^${_NAME_UPPER}_FOUND$")
  209. set(_FOUND_VAR ${FPHSA_FOUND_VAR})
  210. else()
  211. message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_NAME}_FOUND\" and \"${_NAME_UPPER}_FOUND\" are valid names.")
  212. endif()
  213. else()
  214. set(_FOUND_VAR ${_NAME_UPPER}_FOUND)
  215. endif()
  216. # collect all variables which were not found, so they can be printed, so the
  217. # user knows better what went wrong (#6375)
  218. set(MISSING_VARS "")
  219. set(DETAILS "")
  220. # check if all passed variables are valid
  221. unset(${_FOUND_VAR})
  222. foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
  223. if(NOT ${_CURRENT_VAR})
  224. set(${_FOUND_VAR} FALSE)
  225. set(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
  226. else()
  227. set(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
  228. endif()
  229. endforeach()
  230. if(NOT "${${_FOUND_VAR}}" STREQUAL "FALSE")
  231. set(${_FOUND_VAR} TRUE)
  232. endif()
  233. # component handling
  234. unset(FOUND_COMPONENTS_MSG)
  235. unset(MISSING_COMPONENTS_MSG)
  236. if(FPHSA_HANDLE_COMPONENTS)
  237. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  238. if(${_NAME}_${comp}_FOUND)
  239. if(NOT DEFINED FOUND_COMPONENTS_MSG)
  240. set(FOUND_COMPONENTS_MSG "found components: ")
  241. endif()
  242. set(FOUND_COMPONENTS_MSG "${FOUND_COMPONENTS_MSG} ${comp}")
  243. else()
  244. if(NOT DEFINED MISSING_COMPONENTS_MSG)
  245. set(MISSING_COMPONENTS_MSG "missing components: ")
  246. endif()
  247. set(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}")
  248. if(${_NAME}_FIND_REQUIRED_${comp})
  249. set(${_FOUND_VAR} FALSE)
  250. set(MISSING_VARS "${MISSING_VARS} ${comp}")
  251. endif()
  252. endif()
  253. endforeach()
  254. set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
  255. set(DETAILS "${DETAILS}[c${COMPONENT_MSG}]")
  256. endif()
  257. # version handling:
  258. set(VERSION_MSG "")
  259. set(VERSION_OK TRUE)
  260. set(VERSION ${${FPHSA_VERSION_VAR}})
  261. # check with DEFINED here as the requested or found version may be "0"
  262. if (DEFINED ${_NAME}_FIND_VERSION)
  263. if(DEFINED ${FPHSA_VERSION_VAR})
  264. if(${_NAME}_FIND_VERSION_EXACT) # exact version required
  265. # count the dots in the version string
  266. string(REGEX REPLACE "[^.]" "" _VERSION_DOTS "${VERSION}")
  267. # add one dot because there is one dot more than there are components
  268. string(LENGTH "${_VERSION_DOTS}." _VERSION_DOTS)
  269. if (_VERSION_DOTS GREATER ${_NAME}_FIND_VERSION_COUNT)
  270. # Because of the C++ implementation of find_package() ${_NAME}_FIND_VERSION_COUNT
  271. # is at most 4 here. Therefore a simple lookup table is used.
  272. if (${_NAME}_FIND_VERSION_COUNT EQUAL 1)
  273. set(_VERSION_REGEX "[^.]*")
  274. elseif (${_NAME}_FIND_VERSION_COUNT EQUAL 2)
  275. set(_VERSION_REGEX "[^.]*\\.[^.]*")
  276. elseif (${_NAME}_FIND_VERSION_COUNT EQUAL 3)
  277. set(_VERSION_REGEX "[^.]*\\.[^.]*\\.[^.]*")
  278. else ()
  279. set(_VERSION_REGEX "[^.]*\\.[^.]*\\.[^.]*\\.[^.]*")
  280. endif ()
  281. string(REGEX REPLACE "^(${_VERSION_REGEX})\\..*" "\\1" _VERSION_HEAD "${VERSION}")
  282. unset(_VERSION_REGEX)
  283. if (NOT ${_NAME}_FIND_VERSION VERSION_EQUAL _VERSION_HEAD)
  284. set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  285. set(VERSION_OK FALSE)
  286. else ()
  287. set(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
  288. endif ()
  289. unset(_VERSION_HEAD)
  290. else ()
  291. if (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
  292. set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  293. set(VERSION_OK FALSE)
  294. else ()
  295. set(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
  296. endif ()
  297. endif ()
  298. unset(_VERSION_DOTS)
  299. else() # minimum version specified:
  300. if ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
  301. set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
  302. set(VERSION_OK FALSE)
  303. else ()
  304. set(VERSION_MSG "(found suitable version \"${VERSION}\", minimum required is \"${${_NAME}_FIND_VERSION}\")")
  305. endif ()
  306. endif()
  307. else()
  308. # if the package was not found, but a version was given, add that to the output:
  309. if(${_NAME}_FIND_VERSION_EXACT)
  310. set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
  311. else()
  312. set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
  313. endif()
  314. endif()
  315. else ()
  316. if(VERSION)
  317. set(VERSION_MSG "(found version \"${VERSION}\")")
  318. endif()
  319. endif ()
  320. if(VERSION_OK)
  321. set(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
  322. else()
  323. set(${_FOUND_VAR} FALSE)
  324. endif()
  325. # print the result:
  326. if (${_FOUND_VAR})
  327. FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
  328. else ()
  329. if(FPHSA_CONFIG_MODE)
  330. _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
  331. else()
  332. if(NOT VERSION_OK)
  333. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
  334. else()
  335. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
  336. endif()
  337. endif()
  338. endif ()
  339. set(${_FOUND_VAR} ${${_FOUND_VAR}} PARENT_SCOPE)
  340. endfunction()