FindPackageHandleStandardArgs¶
FIND_PACKAGE_HANDLE_STANDARD_ARGS(<name> … )
This function is intended to be used in FindXXX.cmake modules files. It handles the REQUIRED, QUIET and version-related arguments to find_package(). It also sets the <packagename>_FOUND variable. The package is considered found if all variables <var1>… listed contain valid results, e.g. valid filepaths.
There are two modes of this function. The first argument in both modes is the name of the Find-module where it is called (in original casing).
The first simple mode looks like this:
FIND_PACKAGE_HANDLE_STANDARD_ARGS(<name> (DEFAULT_MSG|"Custom failure message") <var1>...<varN> )
If the variables <var1> to <varN> are all valid, then <UPPERCASED_NAME>_FOUND will be set to TRUE. If DEFAULT_MSG is given as second argument, then the function will generate itself useful success and error messages. You can also supply a custom error message for the failure case. This is not recommended.
The second mode is more powerful and also supports version checking:
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NAME [FOUND_VAR <resultVar>]
[REQUIRED_VARS <var1>...<varN>]
[VERSION_VAR <versionvar>]
[HANDLE_COMPONENTS]
[CONFIG_MODE]
[FAIL_MESSAGE "Custom failure message"] )
In this mode, the name of the result-variable can be set either to either <UPPERCASED_NAME>_FOUND or <OriginalCase_Name>_FOUND using the FOUND_VAR option. Other names for the result-variable are not allowed. So for a Find-module named FindFooBar.cmake, the two possible names are FooBar_FOUND and FOOBAR_FOUND. It is recommended to use the original case version. If the FOUND_VAR option is not used, the default is <UPPERCASED_NAME>_FOUND.
As in the simple mode, if <var1> through <varN> are all valid, <packagename>_FOUND will be set to TRUE. After REQUIRED_VARS the variables which are required for this package are listed. Following VERSION_VAR the name of the variable can be specified which holds the version of the package which has been found. If this is done, this version will be checked against the (potentially) specified required version used in the find_package() call. The EXACT keyword is also handled. The default messages include information about the required version and the version which has been actually found, both if the version is ok or not. If the package supports components, use the HANDLE_COMPONENTS option to enable handling them. In this case, find_package_handle_standard_args() will report which components have been found and which are missing, and the <packagename>_FOUND variable will be set to FALSE if any of the required components (i.e. not the ones listed after OPTIONAL_COMPONENTS) are missing. Use the option CONFIG_MODE if your FindXXX.cmake module is a wrapper for a find_package(… NO_MODULE) call. In this case VERSION_VAR will be set to <NAME>_VERSION and the macro will automatically check whether the Config module was found. Via FAIL_MESSAGE a custom failure message can be specified, if this is not used, the default message will be displayed.
Example for mode 1:
find_package_handle_standard_args(LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
LibXml2 is considered to be found, if both LIBXML2_LIBRARY and LIBXML2_INCLUDE_DIR are valid. Then also LIBXML2_FOUND is set to TRUE. If it is not found and REQUIRED was used, it fails with FATAL_ERROR, independent whether QUIET was used or not. If it is found, success will be reported, including the content of <var1>. On repeated Cmake runs, the same message won’t be printed again.
Example for mode 2:
find_package_handle_standard_args(LibXslt FOUND_VAR LibXslt_FOUND
REQUIRED_VARS LibXslt_LIBRARIES LibXslt_INCLUDE_DIRS
VERSION_VAR LibXslt_VERSION_STRING)
In this case, LibXslt is considered to be found if the variable(s) listed after REQUIRED_VAR are all valid, i.e. LibXslt_LIBRARIES and LibXslt_INCLUDE_DIRS in this case. The result will then be stored in LibXslt_FOUND . Also the version of LibXslt will be checked by using the version contained in LibXslt_VERSION_STRING. Since no FAIL_MESSAGE is given, the default messages will be printed.
Another example for mode 2:
find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4)
find_package_handle_standard_args(Automoc4 CONFIG_MODE)
In this case, FindAutmoc4.cmake wraps a call to find_package(Automoc4 NO_MODULE) and adds an additional search directory for automoc4. Here the result will be stored in AUTOMOC4_FOUND. The following FIND_PACKAGE_HANDLE_STANDARD_ARGS() call produces a proper success/error message.