FindPackageHandleStandardArgs.cmake 12 KB

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