FindPackageHandleStandardArgs.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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> (DEFAULT_MSG|"Custom failure message") <var1>...<varN> )
  24. #
  25. # If the variables <var1> to <varN> are all valid, then
  26. # <UPPERCASED_NAME>_FOUND will be set to TRUE. If DEFAULT_MSG is given
  27. # as second argument, then the function will generate itself useful
  28. # success and error messages. You can also supply a custom error
  29. # message for the failure case. This is not recommended.
  30. #
  31. # The second mode is more powerful and also supports version checking:
  32. #
  33. # ::
  34. #
  35. # FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME [FOUND_VAR <resultVar>]
  36. # [REQUIRED_VARS <var1>...<varN>]
  37. # [VERSION_VAR <versionvar>]
  38. # [HANDLE_COMPONENTS]
  39. # [CONFIG_MODE]
  40. # [FAIL_MESSAGE "Custom failure message"] )
  41. #
  42. #
  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 LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
  79. #
  80. #
  81. #
  82. # LibXml2 is considered to be found, if both LIBXML2_LIBRARY and
  83. # LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to
  84. # TRUE. If it is not found and REQUIRED was used, it fails with
  85. # FATAL_ERROR, independent whether QUIET was used or not. If it is
  86. # found, success will be reported, including the content of <var1>. On
  87. # repeated Cmake runs, the same message won't be printed again.
  88. #
  89. # Example for mode 2:
  90. #
  91. # ::
  92. #
  93. # find_package_handle_standard_args(LibXslt FOUND_VAR LibXslt_FOUND
  94. # REQUIRED_VARS LibXslt_LIBRARIES LibXslt_INCLUDE_DIRS
  95. # VERSION_VAR LibXslt_VERSION_STRING)
  96. #
  97. # In this case, LibXslt is considered to be found if the variable(s)
  98. # listed after REQUIRED_VAR are all valid, i.e. LibXslt_LIBRARIES and
  99. # LibXslt_INCLUDE_DIRS in this case. The result will then be stored in
  100. # LibXslt_FOUND . Also the version of LibXslt will be checked by using
  101. # the version contained in LibXslt_VERSION_STRING. Since no
  102. # FAIL_MESSAGE is given, the default messages will be printed.
  103. #
  104. # Another example for mode 2:
  105. #
  106. # ::
  107. #
  108. # find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
  109. # find_package_handle_standard_args(Automoc4 CONFIG_MODE)
  110. #
  111. # In this case, FindAutmoc4.cmake wraps a call to find_package(Automoc4
  112. # NO_MODULE) and adds an additional search directory for automoc4. Here
  113. # the result will be stored in AUTOMOC4_FOUND. The following
  114. # FIND_PACKAGE_HANDLE_STANDARD_ARGS() call produces a proper
  115. # success/error message.
  116. #=============================================================================
  117. # Copyright 2007-2009 Kitware, Inc.
  118. #
  119. # Distributed under the OSI-approved BSD License (the "License");
  120. # see accompanying file Copyright.txt for details.
  121. #
  122. # This software is distributed WITHOUT ANY WARRANTY; without even the
  123. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  124. # See the License for more information.
  125. #=============================================================================
  126. # (To distribute this file outside of CMake, substitute the full
  127. # License text for the above reference.)
  128. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageMessage.cmake)
  129. include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
  130. # internal helper macro
  131. macro(_FPHSA_FAILURE_MESSAGE _msg)
  132. if (${_NAME}_FIND_REQUIRED)
  133. message(FATAL_ERROR "${_msg}")
  134. else ()
  135. if (NOT ${_NAME}_FIND_QUIETLY)
  136. message(STATUS "${_msg}")
  137. endif ()
  138. endif ()
  139. endmacro()
  140. # internal helper macro to generate the failure message when used in CONFIG_MODE:
  141. macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
  142. # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
  143. if(${_NAME}_CONFIG)
  144. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
  145. else()
  146. # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
  147. # List them all in the error message:
  148. if(${_NAME}_CONSIDERED_CONFIGS)
  149. set(configsText "")
  150. list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
  151. math(EXPR configsCount "${configsCount} - 1")
  152. foreach(currentConfigIndex RANGE ${configsCount})
  153. list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
  154. list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
  155. set(configsText "${configsText} ${filename} (version ${version})\n")
  156. endforeach()
  157. if (${_NAME}_NOT_FOUND_MESSAGE)
  158. set(configsText "${configsText} Reason given by package: ${${_NAME}_NOT_FOUND_MESSAGE}\n")
  159. endif()
  160. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
  161. else()
  162. # Simple case: No Config-file was found at all:
  163. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
  164. endif()
  165. endif()
  166. endmacro()
  167. function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
  168. # set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in
  169. # new extended or in the "old" mode:
  170. set(options CONFIG_MODE HANDLE_COMPONENTS)
  171. set(oneValueArgs FAIL_MESSAGE VERSION_VAR FOUND_VAR)
  172. set(multiValueArgs REQUIRED_VARS)
  173. set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
  174. list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
  175. if(${INDEX} EQUAL -1)
  176. set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
  177. set(FPHSA_REQUIRED_VARS ${ARGN})
  178. set(FPHSA_VERSION_VAR)
  179. else()
  180. CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
  181. if(FPHSA_UNPARSED_ARGUMENTS)
  182. message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
  183. endif()
  184. if(NOT FPHSA_FAIL_MESSAGE)
  185. set(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
  186. endif()
  187. endif()
  188. # now that we collected all arguments, process them
  189. if("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
  190. set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
  191. endif()
  192. # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
  193. # when it successfully found the config-file, including version checking:
  194. if(FPHSA_CONFIG_MODE)
  195. list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
  196. list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
  197. set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
  198. endif()
  199. if(NOT FPHSA_REQUIRED_VARS)
  200. message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
  201. endif()
  202. list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
  203. string(TOUPPER ${_NAME} _NAME_UPPER)
  204. string(TOLOWER ${_NAME} _NAME_LOWER)
  205. if(FPHSA_FOUND_VAR)
  206. if(FPHSA_FOUND_VAR MATCHES "^${_NAME}_FOUND$" OR FPHSA_FOUND_VAR MATCHES "^${_NAME_UPPER}_FOUND$")
  207. set(_FOUND_VAR ${FPHSA_FOUND_VAR})
  208. else()
  209. message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_NAME}_FOUND\" and \"${_NAME_UPPER}_FOUND\" are valid names.")
  210. endif()
  211. else()
  212. set(_FOUND_VAR ${_NAME_UPPER}_FOUND)
  213. endif()
  214. # collect all variables which were not found, so they can be printed, so the
  215. # user knows better what went wrong (#6375)
  216. set(MISSING_VARS "")
  217. set(DETAILS "")
  218. # check if all passed variables are valid
  219. unset(${_FOUND_VAR})
  220. foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
  221. if(NOT ${_CURRENT_VAR})
  222. set(${_FOUND_VAR} FALSE)
  223. set(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
  224. else()
  225. set(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
  226. endif()
  227. endforeach()
  228. if(NOT "${${_FOUND_VAR}}" STREQUAL "FALSE")
  229. set(${_FOUND_VAR} TRUE)
  230. endif()
  231. # component handling
  232. unset(FOUND_COMPONENTS_MSG)
  233. unset(MISSING_COMPONENTS_MSG)
  234. if(FPHSA_HANDLE_COMPONENTS)
  235. foreach(comp ${${_NAME}_FIND_COMPONENTS})
  236. if(${_NAME}_${comp}_FOUND)
  237. if(NOT DEFINED FOUND_COMPONENTS_MSG)
  238. set(FOUND_COMPONENTS_MSG "found components: ")
  239. endif()
  240. set(FOUND_COMPONENTS_MSG "${FOUND_COMPONENTS_MSG} ${comp}")
  241. else()
  242. if(NOT DEFINED MISSING_COMPONENTS_MSG)
  243. set(MISSING_COMPONENTS_MSG "missing components: ")
  244. endif()
  245. set(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}")
  246. if(${_NAME}_FIND_REQUIRED_${comp})
  247. set(${_FOUND_VAR} FALSE)
  248. set(MISSING_VARS "${MISSING_VARS} ${comp}")
  249. endif()
  250. endif()
  251. endforeach()
  252. set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
  253. set(DETAILS "${DETAILS}[c${COMPONENT_MSG}]")
  254. endif()
  255. # version handling:
  256. set(VERSION_MSG "")
  257. set(VERSION_OK TRUE)
  258. set(VERSION ${${FPHSA_VERSION_VAR}} )
  259. if (${_NAME}_FIND_VERSION)
  260. if(VERSION)
  261. if(${_NAME}_FIND_VERSION_EXACT) # exact version required
  262. if (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
  263. set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
  264. set(VERSION_OK FALSE)
  265. else ()
  266. set(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
  267. endif ()
  268. else() # minimum version specified:
  269. if ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
  270. set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
  271. set(VERSION_OK FALSE)
  272. else ()
  273. set(VERSION_MSG "(found suitable version \"${VERSION}\", minimum required is \"${${_NAME}_FIND_VERSION}\")")
  274. endif ()
  275. endif()
  276. else()
  277. # if the package was not found, but a version was given, add that to the output:
  278. if(${_NAME}_FIND_VERSION_EXACT)
  279. set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
  280. else()
  281. set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
  282. endif()
  283. endif()
  284. else ()
  285. if(VERSION)
  286. set(VERSION_MSG "(found version \"${VERSION}\")")
  287. endif()
  288. endif ()
  289. if(VERSION_OK)
  290. set(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
  291. else()
  292. set(${_FOUND_VAR} FALSE)
  293. endif()
  294. # print the result:
  295. if (${_FOUND_VAR})
  296. FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
  297. else ()
  298. if(FPHSA_CONFIG_MODE)
  299. _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
  300. else()
  301. if(NOT VERSION_OK)
  302. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
  303. else()
  304. _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
  305. endif()
  306. endif()
  307. endif ()
  308. set(${_FOUND_VAR} ${${_FOUND_VAR}} PARENT_SCOPE)
  309. endfunction()