simplyfy checksum code
[platform/upstream/libsolv.git] / cmake / modules / FindPackageHandleStandardArgs.cmake
1 # FIND_PACKAGE_HANDLE_STANDARD_ARGS(<name> ... )
2 #
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.
8 #
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).
11 #
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.
19 #
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>]
23 #                                           [HANDLE_COMPONENTS]
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 # 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.
47 #
48 # Example for mode 1:
49 #
50 #    FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibXml2  DEFAULT_MSG  LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
51 #
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.
58 #
59 # Example for mode 2:
60 #
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
66 # in BISON_VERSION.
67 # Since no FAIL_MESSAGE is given, the default messages will be printed.
68 #
69 # Another example for mode 2:
70 #
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.
77
78 #=============================================================================
79 # Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
80 # All rights reserved.
81 #
82 # Redistribution and use in source and binary forms, with or without
83 # modification, are permitted provided that the following conditions
84 # are met:
85 #
86 # * Redistributions of source code must retain the above copyright
87 #   notice, this list of conditions and the following disclaimer.
88 #
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.
92 #
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
96 #   permission.
97 #
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 #=============================================================================
110
111 INCLUDE(FindPackageMessage)
112 INCLUDE(_CMakeParseArguments)
113
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)
124
125
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:
129   IF(${_NAME}_CONFIG)
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)
135       SET(configsText "")
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}")
144
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)
151
152
153 FUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
154
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)
162
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)
168
169     CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}"  ${_FIRST_ARG} ${ARGN})
170
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)
174
175     IF(NOT FPHSA_FAIL_MESSAGE)
176       SET(FPHSA_FAIL_MESSAGE  "DEFAULT_MSG")
177     ENDIF(NOT FPHSA_FAIL_MESSAGE)
178   ENDIF(${INDEX} EQUAL -1)
179
180 # now that we collected all arguments, process them
181
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")
185
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)
193
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)
197
198   LIST(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
199
200   STRING(TOUPPER ${_NAME} _NAME_UPPER)
201   STRING(TOLOWER ${_NAME} _NAME_LOWER)
202
203   # collect all variables which were not found, so they can be printed, so the
204   # user knows better what went wrong (#6375)
205   SET(MISSING_VARS "")
206   SET(DETAILS "")
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)
217
218   # component handling
219   SET(FOUND_COMPONENTS_MSG "")
220   SET(MISSING_COMPONENTS_MSG "")
221
222   IF(FPHSA_HANDLE_COMPONENTS)
223     FOREACH(comp ${${_NAME}_FIND_COMPONENTS})
224       IF(${_NAME}_${comp}_FOUND)
225
226         IF(NOT FOUND_COMPONENTS_MSG)
227           SET(FOUND_COMPONENTS_MSG "found components: ")
228         ENDIF()
229         SET(FOUND_COMPONENTS_MSG "${FOUND_COMPONENTS_MSG} ${comp}")
230
231       ELSE()
232
233         IF(NOT MISSING_COMPONENTS_MSG)
234           SET(MISSING_COMPONENTS_MSG "missing components: ")
235         ENDIF()
236         SET(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}")
237
238         IF(${_NAME}_FIND_REQUIRED_${comp})
239           SET(${_NAME_UPPER}_FOUND FALSE)
240           SET(MISSING_VARS "${MISSING_VARS} ${comp}")
241         ENDIF()
242
243       ENDIF()
244     ENDFOREACH(comp)
245     SET(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
246     SET(DETAILS "${DETAILS}[c${COMPONENT_MSG}]")
247   ENDIF(FPHSA_HANDLE_COMPONENTS)
248
249   # version handling:
250   SET(VERSION_MSG "")
251   SET(VERSION_OK TRUE)
252   SET(VERSION ${${FPHSA_VERSION_VAR}} )
253   IF (${_NAME}_FIND_VERSION)
254
255     IF(VERSION)
256
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}")
264
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)
273
274     ELSE(VERSION)
275
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)
282
283     ENDIF(VERSION)
284   ELSE (${_NAME}_FIND_VERSION)
285     IF(VERSION)
286       SET(VERSION_MSG "(found version \"${VERSION}\")")
287     ENDIF(VERSION)
288   ENDIF (${_NAME}_FIND_VERSION)
289
290   IF(VERSION_OK)
291     SET(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
292   ELSE(VERSION_OK)
293     SET(${_NAME_UPPER}_FOUND FALSE)
294   ENDIF(VERSION_OK)
295
296
297   # print the result:
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)
301
302     IF(FPHSA_CONFIG_MODE)
303       _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
304     ELSE(FPHSA_CONFIG_MODE)
305       IF(NOT VERSION_OK)
306         _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
307       ELSE(NOT VERSION_OK)
308         _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
309       ENDIF(NOT VERSION_OK)
310     ENDIF(FPHSA_CONFIG_MODE)
311
312   ENDIF (${_NAME_UPPER}_FOUND)
313
314   SET(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
315
316 ENDFUNCTION(FIND_PACKAGE_HANDLE_STANDARD_ARGS _FIRST_ARG)