cmake: Factor out method to find the CMakeCache.txt file

This commit is contained in:
Tobias Hunger 2016-09-09 14:49:30 +02:00
parent c73967cb4a
commit 3e58b9af57
2 changed files with 26 additions and 17 deletions

View File

@ -1046,6 +1046,28 @@ const char* cmake::GetHomeOutputDirectory() const
return this->State->GetBinaryDirectory();
}
std::string cmake::FindCacheFile(const std::string& binaryDir) const
{
std::string cachePath = binaryDir;
cmSystemTools::ConvertToUnixSlashes(cachePath);
std::string cacheFile = cachePath;
cacheFile += "/CMakeCache.txt";
if (!cmSystemTools::FileExists(cacheFile.c_str())) {
// search in parent directories for cache
std::string cmakeFiles = cachePath;
cmakeFiles += "/CMakeFiles";
if (cmSystemTools::FileExists(cmakeFiles.c_str())) {
std::string cachePathFound =
cmSystemTools::FileExistsInParentDirectories("CMakeCache.txt",
cachePath.c_str(), "/");
if (!cachePathFound.empty()) {
cachePath = cmSystemTools::GetFilenamePath(cachePathFound);
}
}
}
return cachePath;
}
void cmake::SetGlobalGenerator(cmGlobalGenerator* gg)
{
if (!gg) {
@ -2344,24 +2366,8 @@ int cmake::Build(const std::string& dir, const std::string& target,
std::cerr << "Error: " << dir << " is not a directory\n";
return 1;
}
std::string cachePath = dir;
cmSystemTools::ConvertToUnixSlashes(cachePath);
std::string cacheFile = cachePath;
cacheFile += "/CMakeCache.txt";
if (!cmSystemTools::FileExists(cacheFile.c_str())) {
// search in parent directories for cache
std::string cmakeFiles = cachePath;
cmakeFiles += "/CMakeFiles";
if (cmSystemTools::FileExists(cmakeFiles.c_str())) {
std::string cachePathFound =
cmSystemTools::FileExistsInParentDirectories("CMakeCache.txt",
cachePath.c_str(), "/");
if (!cachePathFound.empty()) {
cachePath = cmSystemTools::GetFilenamePath(cachePathFound);
}
}
}
std::string cachePath = FindCacheFile(dir);
if (!this->LoadCache(cachePath)) {
std::cerr << "Error: could not load cache\n";
return 1;

View File

@ -193,6 +193,9 @@ public:
return this->GlobalGenerator;
}
///! Return the full path to where the CMakeCache.txt file should be.
std::string FindCacheFile(const std::string& binaryDir) const;
///! Return the global generator assigned to this instance of cmake
void SetGlobalGenerator(cmGlobalGenerator*);