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 # Redistribution and use in source and binary forms, with or without
76 # modification, are permitted provided that the following conditions
79 # * Redistributions of source code must retain the above copyright
80 # notice, this list of conditions and the following disclaimer.
82 # * Redistributions in binary form must reproduce the above copyright
83 # notice, this list of conditions and the following disclaimer in the
84 # documentation and/or other materials provided with the distribution.
86 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
87 # nor the names of their contributors may be used to endorse or promote
88 # products derived from this software without specific prior written
91 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
92 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
93 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
94 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
95 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
97 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
98 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
99 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
100 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
101 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103 INCLUDE(FindPackageMessage)
104 INCLUDE(CMakeParseArguments)
106 # internal helper macro
107 MACRO(_FPHSA_FAILURE_MESSAGE _msg)
108 IF (${_NAME}_FIND_REQUIRED)
109 MESSAGE(FATAL_ERROR "${_msg}")
110 ELSE (${_NAME}_FIND_REQUIRED)
111 IF (NOT ${_NAME}_FIND_QUIETLY)
112 MESSAGE(STATUS "${_msg}")
113 ENDIF (NOT ${_NAME}_FIND_QUIETLY)
114 ENDIF (${_NAME}_FIND_REQUIRED)
115 ENDMACRO(_FPHSA_FAILURE_MESSAGE _msg)
118 # internal helper macro to generate the failure message when used in CONFIG_MODE:
119 MACRO(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
120 # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
122 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
123 ELSE(${_NAME}_CONFIG)
124 # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
125 # List them all in the error message:
126 IF(${_NAME}_CONSIDERED_CONFIGS)
128 LIST(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
129 MATH(EXPR configsCount "${configsCount} - 1")
130 FOREACH(currentConfigIndex RANGE ${configsCount})
131 LIST(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
132 LIST(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
133 SET(configsText "${configsText} ${filename} (version ${version})\n")
134 ENDFOREACH(currentConfigIndex)
135 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
137 ELSE(${_NAME}_CONSIDERED_CONFIGS)
138 # Simple case: No Config-file was found at all:
139 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
140 ENDIF(${_NAME}_CONSIDERED_CONFIGS)
141 ENDIF(${_NAME}_CONFIG)
142 ENDMACRO(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
145 FUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
147 # set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in
148 # new extended or in the "old" mode:
149 SET(options CONFIG_MODE)
150 SET(oneValueArgs FAIL_MESSAGE VERSION_VAR)
151 SET(multiValueArgs REQUIRED_VARS)
152 SET(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
153 LIST(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
155 IF(${INDEX} EQUAL -1)
156 SET(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
157 SET(FPHSA_REQUIRED_VARS ${ARGN})
158 SET(FPHSA_VERSION_VAR)
159 ELSE(${INDEX} EQUAL -1)
161 CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
163 IF(FPHSA_UNPARSED_ARGUMENTS)
164 MESSAGE(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
165 ENDIF(FPHSA_UNPARSED_ARGUMENTS)
167 IF(NOT FPHSA_FAIL_MESSAGE)
168 SET(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
169 ENDIF(NOT FPHSA_FAIL_MESSAGE)
170 ENDIF(${INDEX} EQUAL -1)
172 # now that we collected all arguments, process them
174 IF("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
175 SET(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
176 ENDIF("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
178 # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
179 # when it successfully found the config-file, including version checking:
180 IF(FPHSA_CONFIG_MODE)
181 LIST(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
182 LIST(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
183 SET(FPHSA_VERSION_VAR ${_NAME}_VERSION)
184 ENDIF(FPHSA_CONFIG_MODE)
186 IF(NOT FPHSA_REQUIRED_VARS)
187 MESSAGE(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
188 ENDIF(NOT FPHSA_REQUIRED_VARS)
190 LIST(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
192 STRING(TOUPPER ${_NAME} _NAME_UPPER)
193 STRING(TOLOWER ${_NAME} _NAME_LOWER)
195 # collect all variables which were not found, so they can be printed, so the
196 # user knows better what went wrong (#6375)
199 SET(${_NAME_UPPER}_FOUND TRUE)
200 # check if all passed variables are valid
201 FOREACH(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
202 IF(NOT ${_CURRENT_VAR})
203 SET(${_NAME_UPPER}_FOUND FALSE)
204 SET(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
205 ELSE(NOT ${_CURRENT_VAR})
206 SET(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
207 ENDIF(NOT ${_CURRENT_VAR})
208 ENDFOREACH(_CURRENT_VAR)
214 SET(VERSION ${${FPHSA_VERSION_VAR}} )
215 IF (${_NAME}_FIND_VERSION)
219 IF(${_NAME}_FIND_VERSION_EXACT) # exact version required
220 IF (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
221 SET(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
222 SET(VERSION_OK FALSE)
223 ELSE (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
224 SET(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
225 ENDIF (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
227 ELSE(${_NAME}_FIND_VERSION_EXACT) # minimum version specified:
228 IF ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
229 SET(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
230 SET(VERSION_OK FALSE)
231 ELSE ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
232 SET(VERSION_MSG "(found suitable version \"${VERSION}\", required is \"${${_NAME}_FIND_VERSION}\")")
233 ENDIF ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
234 ENDIF(${_NAME}_FIND_VERSION_EXACT)
238 # if the package was not found, but a version was given, add that to the output:
239 IF(${_NAME}_FIND_VERSION_EXACT)
240 SET(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
241 ELSE(${_NAME}_FIND_VERSION_EXACT)
242 SET(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
243 ENDIF(${_NAME}_FIND_VERSION_EXACT)
246 ELSE (${_NAME}_FIND_VERSION)
248 SET(VERSION_MSG "(found version \"${VERSION}\")")
250 ENDIF (${_NAME}_FIND_VERSION)
253 SET(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
255 SET(${_NAME_UPPER}_FOUND FALSE)
260 IF (${_NAME_UPPER}_FOUND)
261 FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG}" "${DETAILS}")
262 ELSE (${_NAME_UPPER}_FOUND)
264 IF(FPHSA_CONFIG_MODE)
265 _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
266 ELSE(FPHSA_CONFIG_MODE)
268 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
270 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
271 ENDIF(NOT VERSION_OK)
272 ENDIF(FPHSA_CONFIG_MODE)
274 ENDIF (${_NAME_UPPER}_FOUND)
276 SET(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
278 ENDFUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _FIRST_ARG)