FindPackageHandleStandardArgs.cmake 13 KB

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