xref: /libbtbb/cmake/modules/FindPackageHandleStandardArgs.cmake (revision e25b118a40ed6b5c2ea76bae29e388cfbc2f6e92)
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 2007-2009 Kitware, Inc.
80#
81# Distributed under the OSI-approved BSD License (the "License");
82# see accompanying file Copyright.txt for details.
83#
84# This software is distributed WITHOUT ANY WARRANTY; without even the
85# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
86# See the License for more information.
87#=============================================================================
88# (To distribute this file outside of CMake, substitute the full
89#  License text for the above reference.)
90
91include(FindPackageMessage)
92include(CMakeParseArguments)
93
94# internal helper macro
95macro(_FPHSA_FAILURE_MESSAGE _msg)
96  if (${_NAME}_FIND_REQUIRED)
97    message(FATAL_ERROR "${_msg}")
98  else ()
99    if (NOT ${_NAME}_FIND_QUIETLY)
100      message(STATUS "${_msg}")
101    endif ()
102  endif ()
103endmacro()
104
105
106# internal helper macro to generate the failure message when used in CONFIG_MODE:
107macro(_FPHSA_HANDLE_FAILURE_CONFIG_MODE)
108  # <name>_CONFIG is set, but FOUND is false, this means that some other of the REQUIRED_VARS was not found:
109  if(${_NAME}_CONFIG)
110    _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: missing: ${MISSING_VARS} (found ${${_NAME}_CONFIG} ${VERSION_MSG})")
111  else()
112    # If _CONSIDERED_CONFIGS is set, the config-file has been found, but no suitable version.
113    # List them all in the error message:
114    if(${_NAME}_CONSIDERED_CONFIGS)
115      set(configsText "")
116      list(LENGTH ${_NAME}_CONSIDERED_CONFIGS configsCount)
117      math(EXPR configsCount "${configsCount} - 1")
118      foreach(currentConfigIndex RANGE ${configsCount})
119        list(GET ${_NAME}_CONSIDERED_CONFIGS ${currentConfigIndex} filename)
120        list(GET ${_NAME}_CONSIDERED_VERSIONS ${currentConfigIndex} version)
121        set(configsText "${configsText}    ${filename} (version ${version})\n")
122      endforeach()
123      if (${_NAME}_NOT_FOUND_MESSAGE)
124        set(configsText "${configsText}    Reason given by package: ${${_NAME}_NOT_FOUND_MESSAGE}\n")
125      endif()
126      _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} ${VERSION_MSG}, checked the following files:\n${configsText}")
127
128    else()
129      # Simple case: No Config-file was found at all:
130      _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: found neither ${_NAME}Config.cmake nor ${_NAME_LOWER}-config.cmake ${VERSION_MSG}")
131    endif()
132  endif()
133endmacro()
134
135
136function(FIND_PACKAGE_HANDLE_STANDARD_ARGS _NAME _FIRST_ARG)
137
138# set up the arguments for CMAKE_PARSE_ARGUMENTS and check whether we are in
139# new extended or in the "old" mode:
140  set(options CONFIG_MODE HANDLE_COMPONENTS)
141  set(oneValueArgs FAIL_MESSAGE VERSION_VAR)
142  set(multiValueArgs REQUIRED_VARS)
143  set(_KEYWORDS_FOR_EXTENDED_MODE  ${options} ${oneValueArgs} ${multiValueArgs} )
144  list(FIND _KEYWORDS_FOR_EXTENDED_MODE "${_FIRST_ARG}" INDEX)
145
146  if(${INDEX} EQUAL -1)
147    set(FPHSA_FAIL_MESSAGE ${_FIRST_ARG})
148    set(FPHSA_REQUIRED_VARS ${ARGN})
149    set(FPHSA_VERSION_VAR)
150  else()
151
152    CMAKE_PARSE_ARGUMENTS(FPHSA "${options}" "${oneValueArgs}" "${multiValueArgs}"  ${_FIRST_ARG} ${ARGN})
153
154    if(FPHSA_UNPARSED_ARGUMENTS)
155      message(FATAL_ERROR "Unknown keywords given to FIND_PACKAGE_HANDLE_STANDARD_ARGS(): \"${FPHSA_UNPARSED_ARGUMENTS}\"")
156    endif()
157
158    if(NOT FPHSA_FAIL_MESSAGE)
159      set(FPHSA_FAIL_MESSAGE  "DEFAULT_MSG")
160    endif()
161  endif()
162
163# now that we collected all arguments, process them
164
165  if("${FPHSA_FAIL_MESSAGE}" STREQUAL "DEFAULT_MSG")
166    set(FPHSA_FAIL_MESSAGE "Could NOT find ${_NAME}")
167  endif()
168
169  # In config-mode, we rely on the variable <package>_CONFIG, which is set by find_package()
170  # when it successfully found the config-file, including version checking:
171  if(FPHSA_CONFIG_MODE)
172    list(INSERT FPHSA_REQUIRED_VARS 0 ${_NAME}_CONFIG)
173    list(REMOVE_DUPLICATES FPHSA_REQUIRED_VARS)
174    set(FPHSA_VERSION_VAR ${_NAME}_VERSION)
175  endif()
176
177  if(NOT FPHSA_REQUIRED_VARS)
178    message(FATAL_ERROR "No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()")
179  endif()
180
181  list(GET FPHSA_REQUIRED_VARS 0 _FIRST_REQUIRED_VAR)
182
183  string(TOUPPER ${_NAME} _NAME_UPPER)
184  string(TOLOWER ${_NAME} _NAME_LOWER)
185
186  # collect all variables which were not found, so they can be printed, so the
187  # user knows better what went wrong (#6375)
188  set(MISSING_VARS "")
189  set(DETAILS "")
190  set(${_NAME_UPPER}_FOUND TRUE)
191  # check if all passed variables are valid
192  foreach(_CURRENT_VAR ${FPHSA_REQUIRED_VARS})
193    if(NOT ${_CURRENT_VAR})
194      set(${_NAME_UPPER}_FOUND FALSE)
195      set(MISSING_VARS "${MISSING_VARS} ${_CURRENT_VAR}")
196    else()
197      set(DETAILS "${DETAILS}[${${_CURRENT_VAR}}]")
198    endif()
199  endforeach()
200
201  # component handling
202  unset(FOUND_COMPONENTS_MSG)
203  unset(MISSING_COMPONENTS_MSG)
204
205  if(FPHSA_HANDLE_COMPONENTS)
206    foreach(comp ${${_NAME}_FIND_COMPONENTS})
207      if(${_NAME}_${comp}_FOUND)
208
209        if(NOT DEFINED FOUND_COMPONENTS_MSG)
210          set(FOUND_COMPONENTS_MSG "found components: ")
211        endif()
212        set(FOUND_COMPONENTS_MSG "${FOUND_COMPONENTS_MSG} ${comp}")
213
214      else()
215
216        if(NOT DEFINED MISSING_COMPONENTS_MSG)
217          set(MISSING_COMPONENTS_MSG "missing components: ")
218        endif()
219        set(MISSING_COMPONENTS_MSG "${MISSING_COMPONENTS_MSG} ${comp}")
220
221        if(${_NAME}_FIND_REQUIRED_${comp})
222          set(${_NAME_UPPER}_FOUND FALSE)
223          set(MISSING_VARS "${MISSING_VARS} ${comp}")
224        endif()
225
226      endif()
227    endforeach()
228    set(COMPONENT_MSG "${FOUND_COMPONENTS_MSG} ${MISSING_COMPONENTS_MSG}")
229    set(DETAILS "${DETAILS}[c${COMPONENT_MSG}]")
230  endif()
231
232  # version handling:
233  set(VERSION_MSG "")
234  set(VERSION_OK TRUE)
235  set(VERSION ${${FPHSA_VERSION_VAR}} )
236  if (${_NAME}_FIND_VERSION)
237
238    if(VERSION)
239
240      if(${_NAME}_FIND_VERSION_EXACT)       # exact version required
241        if (NOT "${${_NAME}_FIND_VERSION}" VERSION_EQUAL "${VERSION}")
242          set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is exact version \"${${_NAME}_FIND_VERSION}\"")
243          set(VERSION_OK FALSE)
244        else ()
245          set(VERSION_MSG "(found suitable exact version \"${VERSION}\")")
246        endif ()
247
248      else()     # minimum version specified:
249        if ("${${_NAME}_FIND_VERSION}" VERSION_GREATER "${VERSION}")
250          set(VERSION_MSG "Found unsuitable version \"${VERSION}\", but required is at least \"${${_NAME}_FIND_VERSION}\"")
251          set(VERSION_OK FALSE)
252        else ()
253          set(VERSION_MSG "(found suitable version \"${VERSION}\", minimum required is \"${${_NAME}_FIND_VERSION}\")")
254        endif ()
255      endif()
256
257    else()
258
259      # if the package was not found, but a version was given, add that to the output:
260      if(${_NAME}_FIND_VERSION_EXACT)
261         set(VERSION_MSG "(Required is exact version \"${${_NAME}_FIND_VERSION}\")")
262      else()
263         set(VERSION_MSG "(Required is at least version \"${${_NAME}_FIND_VERSION}\")")
264      endif()
265
266    endif()
267  else ()
268    if(VERSION)
269      set(VERSION_MSG "(found version \"${VERSION}\")")
270    endif()
271  endif ()
272
273  if(VERSION_OK)
274    set(DETAILS "${DETAILS}[v${VERSION}(${${_NAME}_FIND_VERSION})]")
275  else()
276    set(${_NAME_UPPER}_FOUND FALSE)
277  endif()
278
279
280  # print the result:
281  if (${_NAME_UPPER}_FOUND)
282    FIND_PACKAGE_MESSAGE(${_NAME} "Found ${_NAME}: ${${_FIRST_REQUIRED_VAR}} ${VERSION_MSG} ${COMPONENT_MSG}" "${DETAILS}")
283  else ()
284
285    if(FPHSA_CONFIG_MODE)
286      _FPHSA_HANDLE_FAILURE_CONFIG_MODE()
287    else()
288      if(NOT VERSION_OK)
289        _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE}: ${VERSION_MSG} (found ${${_FIRST_REQUIRED_VAR}})")
290      else()
291        _FPHSA_FAILURE_MESSAGE("${FPHSA_FAIL_MESSAGE} (missing: ${MISSING_VARS}) ${VERSION_MSG}")
292      endif()
293    endif()
294
295  endif ()
296
297  set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
298
299endfunction()
300