summaryrefslogtreecommitdiffstats
path: root/Source/cmFindPackageCommand.cxx
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2008-12-09 15:08:54 (GMT)
committerBrad King <brad.king@kitware.com>2008-12-09 15:08:54 (GMT)
commitae28ec9f243e2fd6a6d4462e890249cd43dd318a (patch)
treef2af70f4424e46ccbb9154fff5df7bd3a1dba5de /Source/cmFindPackageCommand.cxx
parent360d9465a20507d4acdc59f66eb7cb3694b43dc5 (diff)
downloadCMake-ae28ec9f243e2fd6a6d4462e890249cd43dd318a.zip
CMake-ae28ec9f243e2fd6a6d4462e890249cd43dd318a.tar.gz
CMake-ae28ec9f243e2fd6a6d4462e890249cd43dd318a.tar.bz2
ENH: Preserve <pkg>_FIND_XXX vars in find_package
When the find_package command loads a module it sets several <pkg>_FIND_XXX variables to communicate information about the command invocation to the module. This restores the original state of the variables when the command returns. This behavior is useful when a find-module recursively calls find_package with NO_MODULE so that the inner call does not change the values in the find-module.
Diffstat (limited to 'Source/cmFindPackageCommand.cxx')
-rw-r--r--Source/cmFindPackageCommand.cxx58
1 files changed, 46 insertions, 12 deletions
diff --git a/Source/cmFindPackageCommand.cxx b/Source/cmFindPackageCommand.cxx
index c68e86c..8ec9aa9 100644
--- a/Source/cmFindPackageCommand.cxx
+++ b/Source/cmFindPackageCommand.cxx
@@ -422,7 +422,7 @@ bool cmFindPackageCommand
// Set a variable telling the find script this component
// is required.
std::string req_var = this->Name + "_FIND_REQUIRED_" + args[i];
- this->Makefile->AddDefinition(req_var.c_str(), "1");
+ this->AddFindDefinition(req_var.c_str(), "1");
// Append to the list of required components.
components += components_sep;
@@ -579,7 +579,7 @@ void cmFindPackageCommand::SetModuleVariables(const std::string& components)
{
// Store the list of components.
std::string components_var = this->Name + "_FIND_COMPONENTS";
- this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
+ this->AddFindDefinition(components_var.c_str(), components.c_str());
if(this->Quiet)
{
@@ -587,7 +587,7 @@ void cmFindPackageCommand::SetModuleVariables(const std::string& components)
// quietly.
std::string quietly = this->Name;
quietly += "_FIND_QUIETLY";
- this->Makefile->AddDefinition(quietly.c_str(), "1");
+ this->AddFindDefinition(quietly.c_str(), "1");
}
if(this->Required)
@@ -596,7 +596,7 @@ void cmFindPackageCommand::SetModuleVariables(const std::string& components)
// a fatal error if the package is not found.
std::string req = this->Name;
req += "_FIND_REQUIRED";
- this->Makefile->AddDefinition(req.c_str(), "1");
+ this->AddFindDefinition(req.c_str(), "1");
}
if(!this->Version.empty())
@@ -605,27 +605,58 @@ void cmFindPackageCommand::SetModuleVariables(const std::string& components)
// package has been requested.
std::string ver = this->Name;
ver += "_FIND_VERSION";
- this->Makefile->AddDefinition(ver.c_str(), this->Version.c_str());
+ this->AddFindDefinition(ver.c_str(), this->Version.c_str());
char buf[64];
sprintf(buf, "%u", this->VersionMajor);
- this->Makefile->AddDefinition((ver+"_MAJOR").c_str(), buf);
+ this->AddFindDefinition((ver+"_MAJOR").c_str(), buf);
sprintf(buf, "%u", this->VersionMinor);
- this->Makefile->AddDefinition((ver+"_MINOR").c_str(), buf);
+ this->AddFindDefinition((ver+"_MINOR").c_str(), buf);
sprintf(buf, "%u", this->VersionPatch);
- this->Makefile->AddDefinition((ver+"_PATCH").c_str(), buf);
+ this->AddFindDefinition((ver+"_PATCH").c_str(), buf);
sprintf(buf, "%u", this->VersionTweak);
- this->Makefile->AddDefinition((ver+"_TWEAK").c_str(), buf);
+ this->AddFindDefinition((ver+"_TWEAK").c_str(), buf);
sprintf(buf, "%u", this->VersionCount);
- this->Makefile->AddDefinition((ver+"_COUNT").c_str(), buf);
+ this->AddFindDefinition((ver+"_COUNT").c_str(), buf);
// Tell the module whether an exact version has been requested.
std::string exact = this->Name;
exact += "_FIND_VERSION_EXACT";
- this->Makefile->AddDefinition(exact.c_str(),
- this->VersionExact? "1":"0");
+ this->AddFindDefinition(exact.c_str(), this->VersionExact? "1":"0");
}
}
+//----------------------------------------------------------------------------
+void cmFindPackageCommand::AddFindDefinition(const char* var, const char* val)
+{
+ if(const char* old = this->Makefile->GetDefinition(var))
+ {
+ this->OriginalDefs[var].exists = true;
+ this->OriginalDefs[var].value = old;
+ }
+ else
+ {
+ this->OriginalDefs[var].exists = false;
+ }
+ this->Makefile->AddDefinition(var, val);
+}
+
+//----------------------------------------------------------------------------
+void cmFindPackageCommand::RestoreFindDefinitions()
+{
+ for(std::map<cmStdString, OriginalDef>::iterator
+ i = this->OriginalDefs.begin(); i != this->OriginalDefs.end(); ++i)
+ {
+ OriginalDef const& od = i->second;
+ if(od.exists)
+ {
+ this->Makefile->AddDefinition(i->first.c_str(), od.value.c_str());
+ }
+ else
+ {
+ this->Makefile->RemoveDefinition(i->first.c_str());
+ }
+ }
+}
//----------------------------------------------------------------------------
bool cmFindPackageCommand::FindModule(bool& found)
@@ -1008,6 +1039,9 @@ void cmFindPackageCommand::AppendSuccessInformation()
this->AppendToProperty("DISABLED_FEATURES");
}
}
+
+ // Restore original state of "_FIND_" variables we set.
+ this->RestoreFindDefinitions();
}
//----------------------------------------------------------------------------