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>]
25 # [FAIL_MESSAGE "Custom failure message"] )
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.
50 # FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
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.
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
67 # Since no FAIL_MESSAGE is given, the default messages will be printed.
69 # Another example for mode 2:
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.
78 #=============================================================================
79 # Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
80 # All rights reserved.
82 # Redistribution and use in source and binary forms, with or without
83 # modification, are permitted provided that the following conditions
86 # * Redistributions of source code must retain the above copyright
87 # notice, this list of conditions and the following disclaimer.
89 # * Redistributions in binary form must reproduce the above copyright
90 # notice, this list of conditions and the following disclaimer in the
91 # documentation and/or other materials provided with the distribution.
93 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
94 # nor the names of their contributors may be used to endorse or promote
95 # products derived from this software without specific prior written
98 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
99 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
100 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
101 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
102 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
103 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
104 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
108 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
109 #=============================================================================
111 INCLUDE(FindPackageMessage)
112 INCLUDE(_CMakeParseArguments)
114 # internal helper macro
115 MACRO(_FPHSA_FAILURE_MESSAGE _msg)
116 IF (${_NAME}_FIND_REQUIRED)
117 MESSAGE(FATAL_ERROR "${_msg}")
118 ELSE (${_NAME}_FIND_REQUIRED)
119 IF (NOT ${_NAME}_FIND_QUIETLY)
120 MESSAGE(STATUS "${_msg}")
121 ENDIF (NOT ${_NAME}_FIND_QUIETLY)
122 ENDIF (${_NAME}_FIND_REQUIRED)
123 ENDMACRO(_FPHSA_FAILURE_MESSAGE _msg)
126 # internal helper macro to generate the failure message when used in CONFIG_MODE:
127 MACRO(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
128 # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
130 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
131 ELSE(${_NAME}_CONFIG)
132 # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
133 # List them all in the error message:
134 IF(${_NAME}_CONSIDERED_CONFIGS)
136 LIST(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
137 MATH(EXPR configsCount "${configsCount} - 1")
138 FOREACH(currentConfigIndex RANGE ${configsCount})
139 LIST(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
140 LIST(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
141 SET(configsText "${configsText} ${filename} (version ${version})\n")
142 ENDFOREACH(currentConfigIndex)
143 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
145 ELSE(${_NAME}_CONSIDERED_CONFIGS)
146 # Simple case: No Config-file was found at all:
147 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
148 ENDIF(${_NAME}_CONSIDERED_CONFIGS)
149 ENDIF(${_NAME}_CONFIG)
150 ENDMACRO(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
153 FUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
155 # set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in
156 # new extended or in the "old" mode:
157 SET(options CONFIG_MODE HANDLE_COMPONENTS)
158 SET(oneValueArgs FAIL_MESSAGE VERSION_VAR)
159 SET(multiValueArgs REQUIRED_VARS)
160 SET(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
161 LIST(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
163 IF(${INDEX} EQUAL -1)
164 SET(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
165 SET(FPHSA_REQUIRED_VARS ${ARGN})
166 SET(FPHSA_VERSION_VAR)
167 ELSE(${INDEX} EQUAL -1)
169 CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
171 IF(FPHSA_UNPARSED_ARGUMENTS)
172 MESSAGE(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
173 ENDIF(FPHSA_UNPARSED_ARGUMENTS)
175 IF(NOT FPHSA_FAIL_MESSAGE)
176 SET(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
177 ENDIF(NOT FPHSA_FAIL_MESSAGE)
178 ENDIF(${INDEX} EQUAL -1)
180 # now that we collected all arguments, process them
182 IF("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
183 SET(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
184 ENDIF("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
186 # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
187 # when it successfully found the config-file, including version checking:
188 IF(FPHSA_CONFIG_MODE)
189 LIST(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
190 LIST(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
191 SET(FPHSA_VERSION_VAR ${_NAME}_VERSION)
192 ENDIF(FPHSA_CONFIG_MODE)
194 IF(NOT FPHSA_REQUIRED_VARS)
195 MESSAGE(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
196 ENDIF(NOT FPHSA_REQUIRED_VARS)
198 LIST(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
200 STRING(TOUPPER ${_NAME} _NAME_UPPER)
201 STRING(TOLOWER ${_NAME} _NAME_LOWER)
203 # collect all variables which were not found, so they can be printed, so the
204 # user knows better what went wrong (#6375)
207 SET(${_NAME_UPPER}_FOUND TRUE)
208 # check if all passed variables are valid
209 FOREACH(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
210 IF(NOT ${_CURRENT_VAR})
211 SET(${_NAME_UPPER}_FOUND FALSE)
212 SET(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
213 ELSE(NOT ${_CURRENT_VAR})
214 SET(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
215 ENDIF(NOT ${_CURRENT_VAR})
216 ENDFOREACH(_CURRENT_VAR)
219 SET(FOUND_COMPONENTS_MSG "")
220 SET(MISSING_COMPONENTS_MSG "")
222 IF(FPHSA_HANDLE_COMPONENTS)
223 FOREACH(comp ${${_NAME}_FIND_COMPONENTS})
224 IF(${_NAME}_${comp}_FOUND)
226 IF(NOT FOUND_COMPONENTS_MSG)
227 SET(FOUND_COMPONENTS_MSG "found components: ")
229 SET(FOUND_COMPONENTS_MSG "${FOUND_COMPONENTS_MSG} ${comp}")
233 IF(NOT MISSING_COMPONENTS_MSG)
234 SET(MISSING_COMPONENTS_MSG "missing components: ")
236 SET(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}")
238 IF(${_NAME}_FIND_REQUIRED_${comp})
239 SET(${_NAME_UPPER}_FOUND FALSE)
240 SET(MISSING_VARS "${MISSING_VARS} ${comp}")
245 SET(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
246 SET(DETAILS "${DETAILS}[c${COMPONENT_MSG}]")
247 ENDIF(FPHSA_HANDLE_COMPONENTS)
252 SET(VERSION ${${FPHSA_VERSION_VAR}} )
253 IF (${_NAME}_FIND_VERSION)
257 IF(${_NAME}_FIND_VERSION_EXACT) # exact version required
258 IF (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
259 SET(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
260 SET(VERSION_OK FALSE)
261 ELSE (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
262 SET(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
263 ENDIF (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
265 ELSE(${_NAME}_FIND_VERSION_EXACT) # minimum version specified:
266 IF ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
267 SET(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
268 SET(VERSION_OK FALSE)
269 ELSE ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
270 SET(VERSION_MSG "(found suitable version \"${VERSION}\", required is \"${${_NAME}_FIND_VERSION}\")")
271 ENDIF ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
272 ENDIF(${_NAME}_FIND_VERSION_EXACT)
276 # if the package was not found, but a version was given, add that to the output:
277 IF(${_NAME}_FIND_VERSION_EXACT)
278 SET(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
279 ELSE(${_NAME}_FIND_VERSION_EXACT)
280 SET(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
281 ENDIF(${_NAME}_FIND_VERSION_EXACT)
284 ELSE (${_NAME}_FIND_VERSION)
286 SET(VERSION_MSG "(found version \"${VERSION}\")")
288 ENDIF (${_NAME}_FIND_VERSION)
291 SET(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
293 SET(${_NAME_UPPER}_FOUND FALSE)
298 IF (${_NAME_UPPER}_FOUND)
299 FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
300 ELSE (${_NAME_UPPER}_FOUND)
302 IF(FPHSA_CONFIG_MODE)
303 _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
304 ELSE(FPHSA_CONFIG_MODE)
306 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
308 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
309 ENDIF(NOT VERSION_OK)
310 ENDIF(FPHSA_CONFIG_MODE)
312 ENDIF (${_NAME_UPPER}_FOUND)
314 SET(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
316 ENDFUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _FIRST_ARG)