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 <packagename>_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 [FOUND_VAR <resultVar>]
22 # [REQUIRED_VARS <var1>...<varN>]
23 # [VERSION_VAR <versionvar>]
26 # [FAIL_MESSAGE "Custom failure message"] )
28 # In this mode, the name of the result-variable can be set either to either
29 # <UPPERCASED_NAME>_FOUND or <OriginalCase_Name>_FOUND using the FOUND_VAR option.
30 # Other names for the result-variable are not allowed.
31 # So for a Find-module named FindFooBar.cmake, the two possible names are
32 # FooBar_FOUND and FOOBAR_FOUND. It is recommended to use the original case version.
33 # If the FOUND_VAR option is not used, the default is <UPPERCASED_NAME>_FOUND.
35 # As in the simple mode, if <var1> through <varN> are all valid,
36 # <packagename>_FOUND will be set to TRUE.
37 # After REQUIRED_VARS the variables which are required for this package are listed.
38 # Following VERSION_VAR the name of the variable can be specified which holds
39 # the version of the package which has been found. If this is done, this version
40 # will be checked against the (potentially) specified required version used
41 # in the find_package() call. The EXACT keyword is also handled. The default
42 # messages include information about the required version and the version
43 # which has been actually found, both if the version is ok or not.
44 # If the package supports components, use the HANDLE_COMPONENTS option to enable
45 # handling them. In this case, find_package_handle_standard_args() will report
46 # which components have been found and which are missing, and the <packagename>_FOUND
47 # variable will be set to FALSE if any of the required components (i.e. not the
48 # ones listed after OPTIONAL_COMPONENTS) are missing.
49 # Use the option CONFIG_MODE if your FindXXX.cmake module is a wrapper for
50 # a find_package(... NO_MODULE) call. In this case VERSION_VAR will be set
51 # to <NAME>_VERSION and the macro will automatically check whether the
52 # Config module was found.
53 # Via FAIL_MESSAGE a custom failure message can be specified, if this is not
54 # used, the default message will be displayed.
58 # find_package_handle_standard_args(LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
60 # LibXml2 is considered to be found, if both LIBXML2_LIBRARY and
61 # LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to TRUE.
62 # If it is not found and REQUIRED was used, it fails with FATAL_ERROR,
63 # independent whether QUIET was used or not.
64 # If it is found, success will be reported, including the content of <var1>.
65 # On repeated Cmake runs, the same message won't be printed again.
69 # find_package_handle_standard_args(LibXslt FOUND_VAR LibXslt_FOUND
70 # REQUIRED_VARS LibXslt_LIBRARIES LibXslt_INCLUDE_DIRS
71 # VERSION_VAR LibXslt_VERSION_STRING)
72 # In this case, LibXslt is considered to be found if the variable(s) listed
73 # after REQUIRED_VAR are all valid, i.e. LibXslt_LIBRARIES and LibXslt_INCLUDE_DIRS
74 # in this case. The result will then be stored in LibXslt_FOUND .
75 # Also the version of LibXslt will be checked by using the version contained
76 # in LibXslt_VERSION_STRING.
77 # Since no FAIL_MESSAGE is given, the default messages will be printed.
79 # Another example for mode 2:
81 # find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
82 # find_package_handle_standard_args(Automoc4 CONFIG_MODE)
83 # In this case, FindAutmoc4.cmake wraps a call to find_package(Automoc4 NO_MODULE)
84 # and adds an additional search directory for automoc4.
85 # Here the result will be stored in AUTOMOC4_FOUND.
86 # The following FIND_PACKAGE_HANDLE_STANDARD_ARGS() call produces a proper
87 # success/error message.
89 #=============================================================================
90 # Copyright 2007-2009 Kitware, Inc.
92 # Distributed under the OSI-approved BSD License (the "License");
93 # see accompanying file Copyright.txt for details.
95 # This software is distributed WITHOUT ANY WARRANTY; without even the
96 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
97 # See the License for more information.
98 #=============================================================================
99 # (To distribute this file outside of CMake, substitute the full
100 # License text for the above reference.)
102 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageMessage.cmake)
103 include(${CMAKE_CURRENT_LIST_DIR}/CMakeParseArguments.cmake)
105 # internal helper macro
106 macro(_FPHSA_FAILURE_MESSAGE _msg)
107 if (${_NAME}_FIND_REQUIRED)
108 message(FATAL_ERROR "${_msg}")
110 if (NOT ${_NAME}_FIND_QUIETLY)
111 message(STATUS "${_msg}")
117 # internal helper macro to generate the failure message when used in CONFIG_MODE:
118 macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
119 # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
121 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
123 # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
124 # List them all in the error message:
125 if(${_NAME}_CONSIDERED_CONFIGS)
127 list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
128 math(EXPR configsCount "${configsCount} - 1")
129 foreach(currentConfigIndex RANGE ${configsCount})
130 list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
131 list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
132 set(configsText "${configsText} ${filename} (version ${version})\n")
134 if (${_NAME}_NOT_FOUND_MESSAGE)
135 set(configsText "${configsText} Reason given by package: ${${_NAME}_NOT_FOUND_MESSAGE}\n")
137 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
140 # Simple case: No Config-file was found at all:
141 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
147 function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
149 # set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in
150 # new extended or in the "old" mode:
151 set(options CONFIG_MODE HANDLE_COMPONENTS)
152 set(oneValueArgs FAIL_MESSAGE VERSION_VAR FOUND_VAR)
153 set(multiValueArgs REQUIRED_VARS)
154 set(_KEYWORDS_FOR_EXTENDED_MODE ${options} ${oneValueArgs} ${multiValueArgs} )
155 list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
157 if(${INDEX} EQUAL -1)
158 set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
159 set(FPHSA_REQUIRED_VARS ${ARGN})
160 set(FPHSA_VERSION_VAR)
163 CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}" ${_FIRST_ARG} ${ARGN})
165 if(FPHSA_UNPARSED_ARGUMENTS)
166 message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
169 if(NOT FPHSA_FAIL_MESSAGE)
170 set(FPHSA_FAIL_MESSAGE "DEFAULT_MSG")
174 # now that we collected all arguments, process them
176 if("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
177 set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
180 # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
181 # when it successfully found the config-file, including version checking:
182 if(FPHSA_CONFIG_MODE)
183 list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
184 list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
185 set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
188 if(NOT FPHSA_REQUIRED_VARS)
189 message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
192 list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
194 string(TOUPPER ${_NAME} _NAME_UPPER)
195 string(TOLOWER ${_NAME} _NAME_LOWER)
198 if(FPHSA_FOUND_VAR MATCHES "^${_NAME}_FOUND$" OR FPHSA_FOUND_VAR MATCHES "^${_NAME_UPPER}_FOUND$")
199 set(_FOUND_VAR ${FPHSA_FOUND_VAR})
201 message(FATAL_ERROR "The argument for FOUND_VAR is \"${FPHSA_FOUND_VAR}\", but only \"${_NAME}_FOUND\" and \"${_NAME_UPPER}_FOUND\" are valid names.")
204 set(_FOUND_VAR ${_NAME_UPPER}_FOUND)
207 # collect all variables which were not found, so they can be printed, so the
208 # user knows better what went wrong (#6375)
211 # check if all passed variables are valid
213 foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
214 if(NOT ${_CURRENT_VAR})
215 set(${_FOUND_VAR} FALSE)
216 set(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
218 set(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
221 if(NOT "${${_FOUND_VAR}}" STREQUAL "FALSE")
222 set(${_FOUND_VAR} TRUE)
226 unset(FOUND_COMPONENTS_MSG)
227 unset(MISSING_COMPONENTS_MSG)
229 if(FPHSA_HANDLE_COMPONENTS)
230 foreach(comp ${${_NAME}_FIND_COMPONENTS})
231 if(${_NAME}_${comp}_FOUND)
233 if(NOT DEFINED FOUND_COMPONENTS_MSG)
234 set(FOUND_COMPONENTS_MSG "found components: ")
236 set(FOUND_COMPONENTS_MSG "${FOUND_COMPONENTS_MSG} ${comp}")
240 if(NOT DEFINED MISSING_COMPONENTS_MSG)
241 set(MISSING_COMPONENTS_MSG "missing components: ")
243 set(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}")
245 if(${_NAME}_FIND_REQUIRED_${comp})
246 set(${_FOUND_VAR} FALSE)
247 set(MISSING_VARS "${MISSING_VARS} ${comp}")
252 set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
253 set(DETAILS "${DETAILS}[c${COMPONENT_MSG}]")
259 set(VERSION ${${FPHSA_VERSION_VAR}} )
260 if (${_NAME}_FIND_VERSION)
264 if(${_NAME}_FIND_VERSION_EXACT) # exact version required
265 if (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
266 set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
267 set(VERSION_OK FALSE)
269 set(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
272 else() # minimum version specified:
273 if ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
274 set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
275 set(VERSION_OK FALSE)
277 set(VERSION_MSG "(found suitable version \"${VERSION}\", minimum required is \"${${_NAME}_FIND_VERSION}\")")
283 # if the package was not found, but a version was given, add that to the output:
284 if(${_NAME}_FIND_VERSION_EXACT)
285 set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
287 set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
293 set(VERSION_MSG "(found version \"${VERSION}\")")
298 set(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
300 set(${_FOUND_VAR} FALSE)
306 FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
309 if(FPHSA_CONFIG_MODE)
310 _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
313 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
315 _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
321 set(${_FOUND_VAR} ${${_FOUND_VAR}} PARENT_SCOPE)