FindPackageHandleStandardArgs.cmake 11 KB

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