FindPackageHandleStandardArgs¶
This module provides commands intended for use in Find Modules
implementing find_package(<PackageName>) calls.
Load this module in a CMake find module with:
FindFoo.cmake¶include(FindPackageHandleStandardArgs)
Commands¶
This module provides the following commands:
- find_package_handle_standard_args¶
Handles the
REQUIRED,QUIETand version-related arguments offind_package().There are two signatures:
find_package_handle_standard_args( <PackageName> (DEFAULT_MSG|<custom-failure-message>) <required-vars>... ) find_package_handle_standard_args( <PackageName> [REQUIRED_VARS <required-vars>...] [VERSION_VAR <version-var>] [HANDLE_VERSION_RANGE] [HANDLE_COMPONENTS] [CONFIG_MODE] [NAME_MISMATCHED] [REASON_FAILURE_MESSAGE <reason-failure-message>] [FAIL_MESSAGE <custom-failure-message>] [FOUND_VAR <result-var>] # Deprecated )
This command sets the
<PackageName>_FOUNDvariable toTRUEif all the variables listed in<required-vars>...contain valid results (e.g., valid filepaths) and any optional constraints are satisfied, andFALSEotherwise. A success or failure message may be displayed based on the results and on whether theREQUIREDand/orQUIEToption was given to thefind_package()call.The arguments are:
<PackageName>The name of the package. For example, as written in the
Find<PackageName>.cmakefind module filename.(DEFAULT_MSG|<custom-failure-message>)In the simple signature this specifies the failure message. Use
DEFAULT_MSGto ask for a default message to be computed (recommended). Not valid in the full signature.REQUIRED_VARS <required-vars>...Specify the variables which are required for this package. These may be named in the generated failure message asking the user to set the missing variable values. Therefore these should typically be cache entries such as
Foo_LIBRARYand not output variables likeFoo_LIBRARIES.Changed in version 3.18: If
HANDLE_COMPONENTSis specified, this option can be omitted.VERSION_VAR <version-var>Specify the name of a variable that holds the version of the package that has been found. This version will be checked against the (potentially) specified required version given to the
find_package()call, including itsEXACToption. 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.HANDLE_VERSION_RANGENew in version 3.19.
Enable handling of a version range, if one is specified. Without this option, a developer warning will be displayed if a version range is specified.
HANDLE_COMPONENTSEnable handling of package components. In this case, the command will report which components have been found and which are missing, and the
<PackageName>_FOUNDvariable will be set toFALSEif any of the required components (i.e. not the ones listed after theOPTIONAL_COMPONENTSoption offind_package()) are missing.CONFIG_MODESpecify that the calling find module is a wrapper around a call to
find_package(<PackageName> NO_MODULE). This implies aVERSION_VARvalue of<PackageName>_VERSION. The command will automatically check whether the package configuration file was found.NAME_MISMATCHEDNew in version 3.17.
Indicate that the
<PackageName>does not match the value ofCMAKE_FIND_PACKAGE_NAMEvariable. This is usually a mistake and raises a warning, but it may be intentional for usage of the command for components of a larger package.REASON_FAILURE_MESSAGE <reason-failure-message>New in version 3.16.
Specify a custom message of the reason for the failure which will be appended to the default generated message.
FAIL_MESSAGE <custom-failure-message>Specify a custom failure message instead of using the default generated message. Not recommended.
FOUND_VAR <result-var>Deprecated since version 3.3: This option should no longer be used.
Specifies either
<PackageName>_FOUNDor<PACKAGENAME>_FOUNDas the result variable. This exists only for backward compatibility with older versions of CMake and is now ignored. Result variables of both names are now always set for compatibility also with or without this option.
Note
If
<PackageName>does not matchCMAKE_FIND_PACKAGE_NAMEfor the calling module, a warning that there is a mismatch is given. TheFPHSA_NAME_MISMATCHEDvariable may be set to bypass the warning if using the old signature and theNAME_MISMATCHEDargument using the new signature. To avoid forcing the caller to require newer versions of CMake for usage, the variable's value will be used if defined when theNAME_MISMATCHEDargument is not passed for the new signature (but using both is an error).- find_package_check_version¶
New in version 3.19.
Checks if a given version is valid against the version-related arguments of
find_package():find_package_check_version( <version> <result-var> [HANDLE_VERSION_RANGE] [RESULT_MESSAGE_VARIABLE <message-var>] )
The arguments are:
<version>The version string to check.
<result-var>Name of the result variable that will hold a boolean value giving the result of the check.
HANDLE_VERSION_RANGEEnable handling of a version range, if one is specified. Without this option, a developer warning will be displayed if a version range is specified.
RESULT_MESSAGE_VARIABLE <message-var>Specify a variable to get back a message describing the result of the check.
Examples¶
Examples: Full Signature¶
Example for using a full signature of
find_package_handle_standard_args():FindLibArchive.cmake¶include(FindPackageHandleStandardArgs) find_package_handle_standard_args( LibArchive REQUIRED_VARS LibArchive_LIBRARY LibArchive_INCLUDE_DIR VERSION_VAR LibArchive_VERSION )
In this case, the
LibArchivepackage is considered to be found if bothLibArchive_LIBRARYandLibArchive_INCLUDE_DIRare valid. Also the version ofLibArchivewill be checked by using the version contained inLibArchive_VERSION. Since noFAIL_MESSAGEis given, the default messages will be printed.Another example for the full signature of
find_package_handle_standard_args():FindAutomoc4.cmake¶find_package(Automoc4 QUIET NO_MODULE HINTS /opt/automoc4) include(FindPackageHandleStandardArgs) find_package_handle_standard_args(Automoc4 CONFIG_MODE)
In this case, a
FindAutomoc4.cmakemodule wraps a call tofind_package(Automoc4 NO_MODULE)and adds an additional search directory forautomoc4. Then the call tofind_package_handle_standard_args()produces a proper success/failure message.Example: Simple Signature¶
Example for using a simple signature of
find_package_handle_standard_args():FindLibXml2.cmake¶include(FindPackageHandleStandardArgs) find_package_handle_standard_args( LibXml2 DEFAULT_MSG LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR )
In this example, the
LibXml2package is considered to be found if bothLIBXML2_LIBRARYandLIBXML2_INCLUDE_DIRvariables are valid. Then alsoLibXml2_FOUNDis set toTRUE. If it is not found andREQUIREDwas used, it fails with amessage(FATAL_ERROR), independent whetherQUIETwas used or not. If it is found, success will be reported, including the content of the first required variable specified in<required-vars>.... On repeated CMake runs, the same message will not be printed again.Example: Checking Version¶
Example for the
find_package_check_version()usage:FindFoo.cmake¶include(FindPackageHandleStandardArgs) find_package_check_version( 1.2.3 result HANDLE_VERSION_RANGE RESULT_MESSAGE_VARIABLE reason ) if(result) message(STATUS "${reason}") else() # Logic when version check is not successful. message(WARNING "${reason}") endif()
See Also¶
Find Modules for details how to write a find module.