1 # FIND_PACKAGE_HANDLE_STANDARD_ARGS(<name> ... )
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.
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).
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.
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>
24 # [FAIL_MESSAGE "Custom failure message"] )
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
39 # Via FAIL_MESSAGE a custom failure message can be specified, if this is not
40 # used, the default message will be displayed.
44 # FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
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.
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
61 # Since no FAIL_MESSAGE is given, the default messages will be printed.
63 # Another example for mode 2:
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.
72 #=============================================================================
73 # Copyright 2007-2009 Kitware, Inc.
75 # Distributed under the OSI-approved BSD License (the "License");
76 # see accompanying file Copyright.txt for details.
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.)
85 INCLUDE(FindPackageMessage)
86 INCLUDE(CMakeParseArguments)
88 # internal helper macro
89 MACRO(_FPHSA_FAILURE_MESSAGE _msg)
90 IF (${_NAME}_FIND_REQUIRED)
91 MESSAGE(FATAL_ERROR "${_msg}")
92 ELSE (${_NAME}_FIND_REQUIRED)
93 IF (NOT ${_NAME}_FIND_QUIETLY)
94 MESSAGE(STATUS "${_msg}")
95 ENDIF (NOT ${_NAME}_FIND_QUIETLY)
96 ENDIF (${_NAME}_FIND_REQUIRED)
97 ENDMACRO(_FPHSA_FAILURE_MESSAGE _msg)
100 # internal helper macro to generate the failure message when used in CONFIG_MODE:
101 MACRO(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
102 # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
104 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
105 ELSE(${_NAME}_CONFIG)
106 # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
107 # List them all in the error message:
108 IF(${_NAME}_CONSIDERED_CONFIGS)
110 LIST(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
111 MATH(EXPR configsCount "${configsCount} - 1")
112 FOREACH(currentConfigIndex RANGE ${configsCount})
113 LIST(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
114 LIST(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
115 SET(configsText "${configsText} ${filename} (version ${version})\n")
116 ENDFOREACH(currentConfigIndex)
117 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
119 ELSE(${_NAME}_CONSIDERED_CONFIGS)
120 # Simple case: No Config-file was found at all:
121 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
122 ENDIF(${_NAME}_CONSIDERED_CONFIGS)
123 ENDIF(${_NAME}_CONFIG)
124 ENDMACRO(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
127 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)
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)
137 IF(${INDEX} EQUAL -1)
138 SET(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
139 SET(FPHSA_REQUIRED_VARS ${ARGN})
140 SET(FPHSA_VERSION_VAR)
141 ELSE(${INDEX} EQUAL -1)
143 CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
145 IF(FPHSA_UNPARSED_ARGUMENTS)
146 MESSAGE(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
147 ENDIF(FPHSA_UNPARSED_ARGUMENTS)
149 IF(NOT FPHSA_FAIL_MESSAGE)
150 SET(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
151 ENDIF(NOT FPHSA_FAIL_MESSAGE)
152 ENDIF(${INDEX} EQUAL -1)
154 # now that we collected all arguments, process them
156 IF("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
157 SET(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
158 ENDIF("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
160 # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
161 # when it successfully found the config-file, including version checking:
162 IF(FPHSA_CONFIG_MODE)
163 LIST(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
164 LIST(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
165 SET(FPHSA_VERSION_VAR ${_NAME}_VERSION)
166 ENDIF(FPHSA_CONFIG_MODE)
168 IF(NOT FPHSA_REQUIRED_VARS)
169 MESSAGE(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
170 ENDIF(NOT FPHSA_REQUIRED_VARS)
172 LIST(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
174 STRING(TOUPPER ${_NAME} _NAME_UPPER)
175 STRING(TOLOWER ${_NAME} _NAME_LOWER)
177 # collect all variables which were not found, so they can be printed, so the
178 # user knows better what went wrong (#6375)
181 SET(${_NAME_UPPER}_FOUND TRUE)
182 # check if all passed variables are valid
183 FOREACH(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
184 IF(NOT ${_CURRENT_VAR})
185 SET(${_NAME_UPPER}_FOUND FALSE)
186 SET(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
187 ELSE(NOT ${_CURRENT_VAR})
188 SET(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
189 ENDIF(NOT ${_CURRENT_VAR})
190 ENDFOREACH(_CURRENT_VAR)
196 SET(VERSION ${${FPHSA_VERSION_VAR}} )
197 IF (${_NAME}_FIND_VERSION)
201 IF(${_NAME}_FIND_VERSION_EXACT) # exact version required
202 IF (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
203 SET(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
204 SET(VERSION_OK FALSE)
205 ELSE (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
206 SET(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
207 ENDIF (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
209 ELSE(${_NAME}_FIND_VERSION_EXACT) # minimum version specified:
210 IF ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
211 SET(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
212 SET(VERSION_OK FALSE)
213 ELSE ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
214 SET(VERSION_MSG "(found suitable version \"${VERSION}\", required is \"${${_NAME}_FIND_VERSION}\")")
215 ENDIF ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
216 ENDIF(${_NAME}_FIND_VERSION_EXACT)
220 # if the package was not found, but a version was given, add that to the output:
221 IF(${_NAME}_FIND_VERSION_EXACT)
222 SET(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
223 ELSE(${_NAME}_FIND_VERSION_EXACT)
224 SET(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
225 ENDIF(${_NAME}_FIND_VERSION_EXACT)
228 ELSE (${_NAME}_FIND_VERSION)
230 SET(VERSION_MSG "(found version \"${VERSION}\")")
232 ENDIF (${_NAME}_FIND_VERSION)
235 SET(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
237 SET(${_NAME_UPPER}_FOUND FALSE)
242 IF (${_NAME_UPPER}_FOUND)
243 FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG}" "${DETAILS}")
244 ELSE (${_NAME_UPPER}_FOUND)
246 IF(FPHSA_CONFIG_MODE)
247 _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
248 ELSE(FPHSA_CONFIG_MODE)
250 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
252 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
253 ENDIF(NOT VERSION_OK)
254 ENDIF(FPHSA_CONFIG_MODE)
256 ENDIF (${_NAME_UPPER}_FOUND)
258 SET(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
260 ENDFUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _FIRST_ARG)