1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-19 02:17:27 +08:00

cmState::GetInitializedCacheValue: Return as const std::string*

This commit is contained in:
Vitaly Stakhovsky
2018-09-10 09:31:48 -04:00
parent 11de1492d3
commit 13d10ee616
9 changed files with 64 additions and 66 deletions

View File

@@ -94,11 +94,11 @@ void QCMake::setBinaryDirectory(const QString& _dir)
} }
const char* gen = state->GetCacheEntryValue("CMAKE_GENERATOR"); const char* gen = state->GetCacheEntryValue("CMAKE_GENERATOR");
if (gen) { if (gen) {
const char* extraGen = const std::string* extraGen =
state->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR"); state->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
std::string curGen = std::string curGen =
cmExternalMakefileProjectGenerator::CreateFullGeneratorName( cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
gen, extraGen ? extraGen : ""); gen, extraGen ? *extraGen : "");
this->setGenerator(QString::fromLocal8Bit(curGen.c_str())); this->setGenerator(QString::fromLocal8Bit(curGen.c_str()));
} }

View File

@@ -68,12 +68,12 @@ const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
return ""; return "";
} }
if (strcmp(key, "CACHE") == 0) { if (strcmp(key, "CACHE") == 0) {
if (const char* c = if (const std::string* c =
this->Makefile->GetState()->GetInitializedCacheValue(var)) { this->Makefile->GetState()->GetInitializedCacheValue(var)) {
if (this->EscapeQuotes) { if (this->EscapeQuotes) {
return this->AddString(cmSystemTools::EscapeQuotes(c)); return this->AddString(cmSystemTools::EscapeQuotes(*c));
} }
return this->AddString(c); return this->AddString(*c);
} }
return ""; return "";
} }

View File

@@ -215,7 +215,7 @@ void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
std::string cacheEntryName = "CMAKE_ECLIPSE_ENVVAR_"; std::string cacheEntryName = "CMAKE_ECLIPSE_ENVVAR_";
cacheEntryName += envVar; cacheEntryName += envVar;
const char* cacheValue = const std::string* cacheValue =
lg->GetState()->GetInitializedCacheValue(cacheEntryName); lg->GetState()->GetInitializedCacheValue(cacheEntryName);
// now we have both, decide which one to use // now we have both, decide which one to use
@@ -232,14 +232,14 @@ void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
mf->GetCMakeInstance()->SaveCache(lg->GetBinaryDirectory()); mf->GetCMakeInstance()->SaveCache(lg->GetBinaryDirectory());
} else if (!envVarSet && cacheValue != nullptr) { } else if (!envVarSet && cacheValue != nullptr) {
// It is already in the cache, but not in the env, so use it from the cache // It is already in the cache, but not in the env, so use it from the cache
valueToUse = cacheValue; valueToUse = *cacheValue;
} else { } else {
// It is both in the cache and in the env. // It is both in the cache and in the env.
// Use the version from the env. except if the value from the env is // Use the version from the env. except if the value from the env is
// completely contained in the value from the cache (for the case that we // completely contained in the value from the cache (for the case that we
// now have a PATH without MSVC dirs in the env. but had the full PATH with // now have a PATH without MSVC dirs in the env. but had the full PATH with
// all MSVC dirs during the cmake run which stored the var in the cache: // all MSVC dirs during the cmake run which stored the var in the cache:
valueToUse = cacheValue; valueToUse = *cacheValue;
if (valueToUse.find(envVarValue) == std::string::npos) { if (valueToUse.find(envVarValue) == std::string::npos) {
valueToUse = envVarValue; valueToUse = envVarValue;
mf->AddCacheDefinition(cacheEntryName, valueToUse.c_str(), mf->AddCacheDefinition(cacheEntryName, valueToUse.c_str(),

View File

@@ -215,15 +215,15 @@ void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang,
if (!optional && (path.empty() || !cmSystemTools::FileExists(path))) { if (!optional && (path.empty() || !cmSystemTools::FileExists(path))) {
return; return;
} }
const char* cname = const std::string* cname =
this->GetCMakeInstance()->GetState()->GetInitializedCacheValue(langComp); this->GetCMakeInstance()->GetState()->GetInitializedCacheValue(langComp);
std::string changeVars; std::string changeVars;
if (cname && !optional) { if (cname && !optional) {
std::string cnameString; std::string cnameString;
if (!cmSystemTools::FileIsFullPath(cname)) { if (!cmSystemTools::FileIsFullPath(*cname)) {
cnameString = cmSystemTools::FindProgram(cname); cnameString = cmSystemTools::FindProgram(*cname);
} else { } else {
cnameString = cname; cnameString = *cname;
} }
std::string pathString = path; std::string pathString = path;
// get rid of potentially multiple slashes: // get rid of potentially multiple slashes:
@@ -239,7 +239,7 @@ void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang,
} }
changeVars += langComp; changeVars += langComp;
changeVars += ";"; changeVars += ";";
changeVars += cname; changeVars += *cname;
this->GetCMakeInstance()->GetState()->SetGlobalProperty( this->GetCMakeInstance()->GetState()->SetGlobalProperty(
"__CMAKE_DELETE_CACHE_CHANGE_VARS_", changeVars.c_str()); "__CMAKE_DELETE_CACHE_CHANGE_VARS_", changeVars.c_str());
} }
@@ -1970,7 +1970,7 @@ void cmGlobalGenerator::AddMakefile(cmMakefile* mf)
// update progress // update progress
// estimate how many lg there will be // estimate how many lg there will be
const char* numGenC = const std::string* numGenC =
this->CMakeInstance->GetState()->GetInitializedCacheValue( this->CMakeInstance->GetState()->GetInitializedCacheValue(
"CMAKE_NUMBER_OF_MAKEFILES"); "CMAKE_NUMBER_OF_MAKEFILES");
@@ -1988,7 +1988,7 @@ void cmGlobalGenerator::AddMakefile(cmMakefile* mf)
return; return;
} }
int numGen = atoi(numGenC); int numGen = atoi(numGenC->c_str());
float prog = 0.9f * static_cast<float>(this->Makefiles.size()) / float prog = 0.9f * static_cast<float>(this->Makefiles.size()) /
static_cast<float>(numGen); static_cast<float>(numGen);
if (prog > 0.9f) { if (prog > 0.9f) {

View File

@@ -1718,7 +1718,8 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
cmStateEnums::CacheEntryType type, cmStateEnums::CacheEntryType type,
bool force) bool force)
{ {
const char* existingValue = this->GetState()->GetInitializedCacheValue(name); const std::string* existingValue =
this->GetState()->GetInitializedCacheValue(name);
// must be outside the following if() to keep it alive long enough // must be outside the following if() to keep it alive long enough
std::string nvalue; std::string nvalue;
@@ -1728,7 +1729,7 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
// if this is not a force, then use the value from the cache // if this is not a force, then use the value from the cache
// if it is a force, then use the value being passed in // if it is a force, then use the value being passed in
if (!force) { if (!force) {
value = existingValue; value = existingValue->c_str();
} }
if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) { if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
std::vector<std::string>::size_type cc; std::vector<std::string>::size_type cc;
@@ -1748,7 +1749,7 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
} }
this->GetCMakeInstance()->AddCacheEntry(name, nvalue.c_str(), doc, type); this->GetCMakeInstance()->AddCacheEntry(name, nvalue.c_str(), doc, type);
nvalue = this->GetState()->GetInitializedCacheValue(name); nvalue = *this->GetState()->GetInitializedCacheValue(name);
value = nvalue.c_str(); value = nvalue.c_str();
} }
} }
@@ -2368,17 +2369,15 @@ std::string cmMakefile::GetRequiredDefinition(const std::string& name) const
bool cmMakefile::IsDefinitionSet(const std::string& name) const bool cmMakefile::IsDefinitionSet(const std::string& name) const
{ {
const char* def; const std::string* def = this->StateSnapshot.GetDefinition(name);
if (const std::string* d = this->StateSnapshot.GetDefinition(name)) { if (!def) {
def = d->c_str();
} else {
def = this->GetState()->GetInitializedCacheValue(name); def = this->GetState()->GetInitializedCacheValue(name);
} }
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
if (cmVariableWatch* vv = this->GetVariableWatch()) { if (cmVariableWatch* vv = this->GetVariableWatch()) {
if (!def) { if (!def) {
vv->VariableAccessed( vv->VariableAccessed(
name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS, def, this); name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS, nullptr, this);
} }
} }
#endif #endif
@@ -2387,10 +2386,8 @@ bool cmMakefile::IsDefinitionSet(const std::string& name) const
const char* cmMakefile::GetDefinition(const std::string& name) const const char* cmMakefile::GetDefinition(const std::string& name) const
{ {
const char* def; const std::string* def = this->StateSnapshot.GetDefinition(name);
if (const std::string* d = this->StateSnapshot.GetDefinition(name)) { if (!def) {
def = d->c_str();
} else {
def = this->GetState()->GetInitializedCacheValue(name); def = this->GetState()->GetInitializedCacheValue(name);
} }
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
@@ -2400,21 +2397,20 @@ const char* cmMakefile::GetDefinition(const std::string& name) const
vv->VariableAccessed(name, vv->VariableAccessed(name,
def ? cmVariableWatch::VARIABLE_READ_ACCESS def ? cmVariableWatch::VARIABLE_READ_ACCESS
: cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS, : cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS,
def, this); (def ? def->c_str() : nullptr), this);
if (watch_function_executed) { if (watch_function_executed) {
// A callback was executed and may have caused re-allocation of the // A callback was executed and may have caused re-allocation of the
// variable storage. Look it up again for now. // variable storage. Look it up again for now.
// FIXME: Refactor variable storage to avoid this problem. // FIXME: Refactor variable storage to avoid this problem.
if (const std::string* d = this->StateSnapshot.GetDefinition(name)) { def = this->StateSnapshot.GetDefinition(name);
def = d->c_str(); if (!def) {
} else {
def = this->GetState()->GetInitializedCacheValue(name); def = this->GetState()->GetInitializedCacheValue(name);
} }
} }
} }
#endif #endif
return def; return (def ? def->c_str() : nullptr);
} }
const char* cmMakefile::GetSafeDefinition(const std::string& def) const const char* cmMakefile::GetSafeDefinition(const std::string& def) const

View File

@@ -1340,20 +1340,20 @@ cmServerResponse cmServerProtocol1::ProcessConfigure(
if (cm->LoadCache(buildDir)) { if (cm->LoadCache(buildDir)) {
// build directory has been set up before // build directory has been set up before
const char* cachedSourceDir = const std::string* cachedSourceDir =
cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY"); cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
if (!cachedSourceDir) { if (!cachedSourceDir) {
return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache."); return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
} }
if (sourceDir.empty()) { if (sourceDir.empty()) {
sourceDir = std::string(cachedSourceDir); sourceDir = *cachedSourceDir;
cm->SetHomeDirectory(sourceDir); cm->SetHomeDirectory(sourceDir);
} }
const char* cachedGenerator = const std::string* cachedGenerator =
cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR"); cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
if (cachedGenerator) { if (cachedGenerator) {
if (gg && gg->GetName() != cachedGenerator) { if (gg && gg->GetName() != *cachedGenerator) {
return request.ReportError("Configured generator does not match with " return request.ReportError("Configured generator does not match with "
"CMAKE_GENERATOR found in cache."); "CMAKE_GENERATOR found in cache.");
} }

View File

@@ -140,10 +140,10 @@ const char* cmState::GetCacheEntryValue(std::string const& key) const
return e->Value.c_str(); return e->Value.c_str();
} }
const char* cmState::GetInitializedCacheValue(std::string const& key) const const std::string* cmState::GetInitializedCacheValue(
std::string const& key) const
{ {
const std::string* p = this->CacheManager->GetInitializedCacheValue(key); return this->CacheManager->GetInitializedCacheValue(key);
return p ? p->c_str() : nullptr;
} }
cmStateEnums::CacheEntryType cmState::GetCacheEntryType( cmStateEnums::CacheEntryType cmState::GetCacheEntryType(

View File

@@ -68,7 +68,7 @@ public:
std::vector<std::string> GetCacheEntryKeys() const; std::vector<std::string> GetCacheEntryKeys() const;
const char* GetCacheEntryValue(std::string const& key) const; const char* GetCacheEntryValue(std::string const& key) const;
const char* GetInitializedCacheValue(std::string const& key) const; const std::string* GetInitializedCacheValue(std::string const& key) const;
cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const; cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const;
void SetCacheEntryValue(std::string const& key, std::string const& value); void SetCacheEntryValue(std::string const& key, std::string const& value);
void SetCacheValue(std::string const& key, std::string const& value); void SetCacheValue(std::string const& key, std::string const& value);

View File

@@ -319,9 +319,10 @@ bool cmake::SetCacheArgs(const std::vector<std::string>& args)
bool haveValue = false; bool haveValue = false;
std::string cachedValue; std::string cachedValue;
if (this->WarnUnusedCli) { if (this->WarnUnusedCli) {
if (const char* v = this->State->GetInitializedCacheValue(var)) { if (const std::string* v =
this->State->GetInitializedCacheValue(var)) {
haveValue = true; haveValue = true;
cachedValue = v; cachedValue = *v;
} }
} }
@@ -331,7 +332,7 @@ bool cmake::SetCacheArgs(const std::vector<std::string>& args)
if (this->WarnUnusedCli) { if (this->WarnUnusedCli) {
if (!haveValue || if (!haveValue ||
cachedValue != this->State->GetInitializedCacheValue(var)) { cachedValue != *this->State->GetInitializedCacheValue(var)) {
this->WatchUnusedCli(var); this->WatchUnusedCli(var);
} }
} }
@@ -1107,7 +1108,7 @@ int cmake::DoPreConfigureChecks()
// do a sanity check on some values // do a sanity check on some values
if (this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY")) { if (this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY")) {
std::string cacheStart = std::string cacheStart =
this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY"); *this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
cacheStart += "/CMakeLists.txt"; cacheStart += "/CMakeLists.txt";
std::string currentStart = this->GetHomeDirectory(); std::string currentStart = this->GetHomeDirectory();
currentStart += "/CMakeLists.txt"; currentStart += "/CMakeLists.txt";
@@ -1276,14 +1277,14 @@ int cmake::ActualConfigure()
// no generator specified on the command line // no generator specified on the command line
if (!this->GlobalGenerator) { if (!this->GlobalGenerator) {
const char* genName = const std::string* genName =
this->State->GetInitializedCacheValue("CMAKE_GENERATOR"); this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
const char* extraGenName = const std::string* extraGenName =
this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR"); this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
if (genName) { if (genName) {
std::string fullName = std::string fullName =
cmExternalMakefileProjectGenerator::CreateFullGeneratorName( cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
genName, extraGenName ? extraGenName : ""); *genName, extraGenName ? *extraGenName : "");
this->GlobalGenerator = this->CreateGlobalGenerator(fullName); this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
} }
if (this->GlobalGenerator) { if (this->GlobalGenerator) {
@@ -1301,14 +1302,14 @@ int cmake::ActualConfigure()
} }
} }
const char* genName = const std::string* genName =
this->State->GetInitializedCacheValue("CMAKE_GENERATOR"); this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
if (genName) { if (genName) {
if (!this->GlobalGenerator->MatchesGeneratorName(genName)) { if (!this->GlobalGenerator->MatchesGeneratorName(*genName)) {
std::string message = "Error: generator : "; std::string message = "Error: generator : ";
message += this->GlobalGenerator->GetName(); message += this->GlobalGenerator->GetName();
message += "\nDoes not match the generator used previously: "; message += "\nDoes not match the generator used previously: ";
message += genName; message += *genName;
message += "\nEither remove the CMakeCache.txt file and CMakeFiles " message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
"directory or choose a different binary directory."; "directory or choose a different binary directory.";
cmSystemTools::Error(message.c_str()); cmSystemTools::Error(message.c_str());
@@ -1325,14 +1326,14 @@ int cmake::ActualConfigure()
cmStateEnums::INTERNAL); cmStateEnums::INTERNAL);
} }
if (const char* instance = if (const std::string* instance =
this->State->GetInitializedCacheValue("CMAKE_GENERATOR_INSTANCE")) { this->State->GetInitializedCacheValue("CMAKE_GENERATOR_INSTANCE")) {
if (!this->GeneratorInstance.empty() && if (!this->GeneratorInstance.empty() &&
this->GeneratorInstance != instance) { this->GeneratorInstance != *instance) {
std::string message = "Error: generator instance: "; std::string message = "Error: generator instance: ";
message += this->GeneratorInstance; message += this->GeneratorInstance;
message += "\nDoes not match the instance used previously: "; message += "\nDoes not match the instance used previously: ";
message += instance; message += *instance;
message += "\nEither remove the CMakeCache.txt file and CMakeFiles " message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
"directory or choose a different binary directory."; "directory or choose a different binary directory.";
cmSystemTools::Error(message.c_str()); cmSystemTools::Error(message.c_str());
@@ -1344,14 +1345,14 @@ int cmake::ActualConfigure()
"Generator instance identifier.", cmStateEnums::INTERNAL); "Generator instance identifier.", cmStateEnums::INTERNAL);
} }
if (const char* platformName = if (const std::string* platformName =
this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) { this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) {
if (!this->GeneratorPlatform.empty() && if (!this->GeneratorPlatform.empty() &&
this->GeneratorPlatform != platformName) { this->GeneratorPlatform != *platformName) {
std::string message = "Error: generator platform: "; std::string message = "Error: generator platform: ";
message += this->GeneratorPlatform; message += this->GeneratorPlatform;
message += "\nDoes not match the platform used previously: "; message += "\nDoes not match the platform used previously: ";
message += platformName; message += *platformName;
message += "\nEither remove the CMakeCache.txt file and CMakeFiles " message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
"directory or choose a different binary directory."; "directory or choose a different binary directory.";
cmSystemTools::Error(message.c_str()); cmSystemTools::Error(message.c_str());
@@ -1363,13 +1364,13 @@ int cmake::ActualConfigure()
"Name of generator platform.", cmStateEnums::INTERNAL); "Name of generator platform.", cmStateEnums::INTERNAL);
} }
if (const char* tsName = if (const std::string* tsName =
this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) { this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) {
if (!this->GeneratorToolset.empty() && this->GeneratorToolset != tsName) { if (!this->GeneratorToolset.empty() && this->GeneratorToolset != *tsName) {
std::string message = "Error: generator toolset: "; std::string message = "Error: generator toolset: ";
message += this->GeneratorToolset; message += this->GeneratorToolset;
message += "\nDoes not match the toolset used previously: "; message += "\nDoes not match the toolset used previously: ";
message += tsName; message += *tsName;
message += "\nEither remove the CMakeCache.txt file and CMakeFiles " message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
"directory or choose a different binary directory."; "directory or choose a different binary directory.";
cmSystemTools::Error(message.c_str()); cmSystemTools::Error(message.c_str());
@@ -1685,7 +1686,8 @@ std::string cmake::StripExtension(const std::string& file) const
const char* cmake::GetCacheDefinition(const std::string& name) const const char* cmake::GetCacheDefinition(const std::string& name) const
{ {
return this->State->GetInitializedCacheValue(name); const std::string* p = this->State->GetInitializedCacheValue(name);
return p ? p->c_str() : nullptr;
} }
void cmake::AddScriptingCommands() void cmake::AddScriptingCommands()
@@ -1868,14 +1870,14 @@ void cmake::PrintGeneratorList()
void cmake::UpdateConversionPathTable() void cmake::UpdateConversionPathTable()
{ {
// Update the path conversion table with any specified file: // Update the path conversion table with any specified file:
const char* tablepath = const std::string* tablepath =
this->State->GetInitializedCacheValue("CMAKE_PATH_TRANSLATION_FILE"); this->State->GetInitializedCacheValue("CMAKE_PATH_TRANSLATION_FILE");
if (tablepath) { if (tablepath) {
cmsys::ifstream table(tablepath); cmsys::ifstream table(tablepath->c_str());
if (!table) { if (!table) {
cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath, cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ",
". CMake can not open file."); tablepath->c_str(), ". CMake can not open file.");
cmSystemTools::ReportLastSystemError("CMake can not open file."); cmSystemTools::ReportLastSystemError("CMake can not open file.");
} else { } else {
std::string a, b; std::string a, b;
@@ -2527,11 +2529,11 @@ bool cmake::Open(const std::string& dir, bool dryRun)
std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n"; std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
return false; return false;
} }
const char* extraGenName = const std::string* extraGenName =
this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR"); this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
std::string fullName = std::string fullName =
cmExternalMakefileProjectGenerator::CreateFullGeneratorName( cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
genName, extraGenName ? extraGenName : ""); genName, extraGenName ? *extraGenName : "");
std::unique_ptr<cmGlobalGenerator> gen( std::unique_ptr<cmGlobalGenerator> gen(
this->CreateGlobalGenerator(fullName)); this->CreateGlobalGenerator(fullName));