FindPackageHandleStandardArgs.cmake 12 KB

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