Merge topic 'FetchContent_find_package_integration'

29e31e2825 Packages: Integrate FetchContent and find_package()

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Acked-by: huangqinjin <huangqinjin@gmail.com>
Merge-request: !5688
This commit is contained in:
Craig Scott
2022-05-06 12:33:22 +00:00
committed by Kitware Robot
51 changed files with 1010 additions and 60 deletions
+91 -7
View File
@@ -23,6 +23,7 @@
#include "cmsys/String.h"
#include "cmAlgorithms.h"
#include "cmListFileCache.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmPolicies.h"
@@ -44,6 +45,8 @@
class cmExecutionStatus;
class cmFileList;
cmFindPackageCommand::PathLabel
cmFindPackageCommand::PathLabel::PackageRedirect("PACKAGE_REDIRECT");
cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::UserRegistry(
"PACKAGE_REGISTRY");
cmFindPackageCommand::PathLabel cmFindPackageCommand::PathLabel::Builds(
@@ -111,8 +114,10 @@ void cmFindPackageCommand::AppendSearchPathGroups()
{
std::vector<cmFindCommon::PathLabel>* labels;
// Update the All group with new paths
// Update the All group with new paths. Note that package redirection must
// take precedence over everything else, so it has to be first in the array.
labels = &this->PathGroupLabelMap[PathGroup::All];
labels->insert(labels->begin(), PathLabel::PackageRedirect);
labels->insert(
std::find(labels->begin(), labels->end(), PathLabel::CMakeSystem),
PathLabel::UserRegistry);
@@ -123,6 +128,8 @@ void cmFindPackageCommand::AppendSearchPathGroups()
PathLabel::SystemRegistry);
// Create the new path objects
this->LabeledPaths.insert(
std::make_pair(PathLabel::PackageRedirect, cmSearchPath(this)));
this->LabeledPaths.insert(
std::make_pair(PathLabel::UserRegistry, cmSearchPath(this)));
this->LabeledPaths.insert(
@@ -568,9 +575,62 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
this->SetModuleVariables(components);
// See if we have been told to delegate to FetchContent or some other
// redirected config package first. We have to check all names that
// find_package() may look for, but only need to invoke the override for the
// first one that matches.
auto overrideNames = this->Names;
if (overrideNames.empty()) {
overrideNames.push_back(this->Name);
}
bool forceConfigMode = false;
const auto redirectsDir =
this->Makefile->GetSafeDefinition("CMAKE_FIND_PACKAGE_REDIRECTS_DIR");
for (const auto& overrideName : overrideNames) {
const auto nameLower = cmSystemTools::LowerCase(overrideName);
const auto delegatePropName =
cmStrCat("_FetchContent_", nameLower, "_override_find_package");
const cmValue delegateToFetchContentProp =
this->Makefile->GetState()->GetGlobalProperty(delegatePropName);
if (delegateToFetchContentProp.IsOn()) {
// When this property is set, the FetchContent module has already been
// included at least once, so we know the FetchContent_MakeAvailable()
// command will be defined. Any future find_package() calls after this
// one for this package will by-pass this once-only delegation.
// The following call will typically create a <name>-config.cmake file
// in the redirectsDir, which we still want to process like any other
// config file to ensure we follow normal find_package() processing.
cmListFileFunction func(
"FetchContent_MakeAvailable", 0, 0,
{ cmListFileArgument(overrideName, cmListFileArgument::Unquoted, 0) });
if (!this->Makefile->ExecuteCommand(func, this->Status)) {
return false;
}
}
if (cmSystemTools::FileExists(
cmStrCat(redirectsDir, '/', nameLower, "-config.cmake")) ||
cmSystemTools::FileExists(
cmStrCat(redirectsDir, '/', overrideName, "Config.cmake"))) {
// Force the use of this redirected config package file, regardless of
// the type of find_package() call. Files in the redirectsDir must always
// take priority over everything else.
forceConfigMode = true;
this->UseConfigFiles = true;
this->UseFindModules = false;
this->Names.clear();
this->Names.emplace_back(overrideName); // Force finding this one
this->Variable = cmStrCat(this->Name, "_DIR");
this->SetConfigDirCacheVariable(redirectsDir);
break;
}
}
// See if there is a Find<PackageName>.cmake module.
bool loadedPackage = false;
if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG")) {
if (forceConfigMode) {
loadedPackage = this->FindPackageUsingConfigMode();
} else if (this->Makefile->IsOn("CMAKE_FIND_PACKAGE_PREFER_CONFIG")) {
if (this->UseConfigFiles && this->FindPackageUsingConfigMode()) {
loadedPackage = true;
} else {
@@ -1185,19 +1245,24 @@ bool cmFindPackageCommand::FindConfig()
} else {
init = this->Variable + "-NOTFOUND";
}
// We force the value since we do not get here if it was already set.
this->SetConfigDirCacheVariable(init);
return found;
}
void cmFindPackageCommand::SetConfigDirCacheVariable(const std::string& value)
{
std::string help =
cmStrCat("The directory containing a CMake configuration file for ",
this->Name, '.');
// We force the value since we do not get here if it was already set.
this->Makefile->AddCacheDefinition(this->Variable, init, help.c_str(),
this->Makefile->AddCacheDefinition(this->Variable, value, help.c_str(),
cmStateEnums::PATH, true);
if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0126) ==
cmPolicies::NEW &&
this->Makefile->IsNormalDefinitionSet(this->Variable)) {
this->Makefile->AddDefinition(this->Variable, init);
this->Makefile->AddDefinition(this->Variable, value);
}
return found;
}
bool cmFindPackageCommand::FindPrefixedConfig()
@@ -1348,6 +1413,8 @@ inline std::size_t collectPathsForDebug(std::string& buffer,
void cmFindPackageCommand::ComputePrefixes()
{
this->FillPrefixesPackageRedirect();
if (!this->NoDefaultPath) {
if (!this->NoPackageRootPath) {
this->FillPrefixesPackageRoot();
@@ -1381,6 +1448,23 @@ void cmFindPackageCommand::ComputePrefixes()
this->ComputeFinalPaths(IgnorePaths::No);
}
void cmFindPackageCommand::FillPrefixesPackageRedirect()
{
cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRedirect];
const auto redirectDir =
this->Makefile->GetDefinition("CMAKE_FIND_PACKAGE_REDIRECTS_DIR");
if (redirectDir && !redirectDir->empty()) {
paths.AddPath(*redirectDir);
}
if (this->DebugMode) {
std::string debugBuffer =
"The internally managed CMAKE_FIND_PACKAGE_REDIRECTS_DIR.\n";
collectPathsForDebug(debugBuffer, paths);
this->DebugBuffer = cmStrCat(this->DebugBuffer, debugBuffer);
}
}
void cmFindPackageCommand::FillPrefixesPackageRoot()
{
cmSearchPath& paths = this->LabeledPaths[PathLabel::PackageRoot];