find_package: Fixed CMAKE_FIND_PACKAGE_PREFER_CONFIG Module fallback

Fixes: #19361
This commit is contained in:
Cristian Adam 2019-06-13 23:58:30 +02:00
parent a423194311
commit 22e65d10c1
4 changed files with 38 additions and 5 deletions

View File

@ -502,8 +502,13 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args,
if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG")) { if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG")) {
if (this->UseConfigFiles && this->FindPackageUsingConfigMode()) { if (this->UseConfigFiles && this->FindPackageUsingConfigMode()) {
loadedPackage = true; loadedPackage = true;
} else if (this->FindPackageUsingModuleMode()) { } else {
if (this->FindPackageUsingModuleMode()) {
loadedPackage = true; loadedPackage = true;
} else {
// The package was not loaded. Report errors.
HandlePackageMode(HandlePackageModeType::Module);
}
} }
} else { } else {
if (this->UseFindModules && this->FindPackageUsingModuleMode()) { if (this->UseFindModules && this->FindPackageUsingModuleMode()) {
@ -603,7 +608,7 @@ bool cmFindPackageCommand::FindPackageUsingConfigMode()
this->IgnoredPaths.insert(ignored.begin(), ignored.end()); this->IgnoredPaths.insert(ignored.begin(), ignored.end());
// Find and load the package. // Find and load the package.
return this->HandlePackageMode(); return this->HandlePackageMode(HandlePackageModeType::Config);
} }
void cmFindPackageCommand::SetModuleVariables(const std::string& components) void cmFindPackageCommand::SetModuleVariables(const std::string& components)
@ -722,7 +727,8 @@ bool cmFindPackageCommand::FindModule(bool& found)
return true; return true;
} }
bool cmFindPackageCommand::HandlePackageMode() bool cmFindPackageCommand::HandlePackageMode(
HandlePackageModeType handlePackageModeType)
{ {
this->ConsideredConfigs.clear(); this->ConsideredConfigs.clear();
@ -817,6 +823,12 @@ bool cmFindPackageCommand::HandlePackageMode()
} }
} }
if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG") && !found &&
handlePackageModeType == HandlePackageModeType::Config) {
// Config mode failed. Allow Module case.
result = false;
}
// package not found // package not found
if (result && !found) { if (result && !found) {
// warn if package required or neither quiet nor in config mode // warn if package required or neither quiet nor in config mode

View File

@ -103,7 +103,14 @@ private:
bool FindModule(bool& found); bool FindModule(bool& found);
void AddFindDefinition(const std::string& var, const char* val); void AddFindDefinition(const std::string& var, const char* val);
void RestoreFindDefinitions(); void RestoreFindDefinitions();
bool HandlePackageMode();
enum /*class*/ HandlePackageModeType
{
Module,
Config
};
bool HandlePackageMode(HandlePackageModeType type);
bool FindConfig(); bool FindConfig();
bool FindPrefixedConfig(); bool FindPrefixedConfig();
bool FindFrameworkConfig(); bool FindFrameworkConfig();

View File

@ -579,3 +579,16 @@ endif()
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF) set(CMAKE_FIND_PACKAGE_PREFER_CONFIG OFF)
set(CMAKE_PREFIX_PATH) set(CMAKE_PREFIX_PATH)
############################################################################
##Test find_package CMAKE_FIND_PACKAGE_PREFER_CONFIG with module fallback
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/PreferConfigOnlyModule)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
find_package(ACME REQUIRED)
if(NOT ACME_FOUND)
message(SEND_ERROR "Did not find ACME package")
endif()

View File

@ -0,0 +1 @@
set(ACME_FOUND TRUE)