cmake/Source/cmCacheManager.cxx

629 lines
20 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCacheManager.h"
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cstdio>
#include <cstring>
2016-10-30 18:24:19 +01:00
#include <sstream>
2018-08-09 18:06:22 +02:00
#include <string>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
#include "cmsys/Glob.hxx"
2017-04-14 19:02:05 +02:00
#include "cmGeneratedFileStream.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2018-04-23 21:13:27 +02:00
#include "cmMessenger.h"
2017-04-14 19:02:05 +02:00
#include "cmState.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmVersion.h"
2015-04-27 22:25:09 +02:00
void cmCacheManager::CleanCMakeFiles(const std::string& path)
{
2020-02-01 23:06:01 +01:00
std::string glob = cmStrCat(path, "/CMakeFiles/*.cmake");
cmsys::Glob globIt;
globIt.FindFiles(glob);
std::vector<std::string> files = globIt.GetFiles();
2015-08-17 11:37:30 +02:00
std::for_each(files.begin(), files.end(), cmSystemTools::RemoveFile);
}
2016-07-09 11:21:54 +02:00
bool cmCacheManager::LoadCache(const std::string& path, bool internal,
2015-04-27 22:25:09 +02:00
std::set<std::string>& excludes,
std::set<std::string>& includes)
{
2020-02-01 23:06:01 +01:00
std::string cacheFile = cmStrCat(path, "/CMakeCache.txt");
// clear the old cache, if we are reading in internal values
2016-07-09 11:21:54 +02:00
if (internal) {
this->Cache.clear();
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileExists(cacheFile)) {
this->CleanCMakeFiles(path);
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin(cacheFile.c_str());
2016-07-09 11:21:54 +02:00
if (!fin) {
return false;
2016-07-09 11:21:54 +02:00
}
const char* realbuffer;
std::string buffer;
std::string entryKey;
2016-03-13 13:35:51 +01:00
unsigned int lineno = 0;
2016-07-09 11:21:54 +02:00
while (fin) {
// Format is key:type=value
2009-10-04 10:30:41 +03:00
std::string helpString;
CacheEntry e;
cmSystemTools::GetLineFromStream(fin, buffer);
2016-03-13 13:35:51 +01:00
lineno++;
realbuffer = buffer.c_str();
2016-07-09 11:21:54 +02:00
while (*realbuffer != '0' &&
(*realbuffer == ' ' || *realbuffer == '\t' || *realbuffer == '\r' ||
*realbuffer == '\n')) {
2016-10-30 18:24:19 +01:00
if (*realbuffer == '\n') {
2016-07-09 11:21:54 +02:00
lineno++;
2016-10-30 18:24:19 +01:00
}
realbuffer++;
2016-07-09 11:21:54 +02:00
}
// skip blank lines and comment lines
2016-07-09 11:21:54 +02:00
if (realbuffer[0] == '#' || realbuffer[0] == 0) {
continue;
2016-07-09 11:21:54 +02:00
}
while (realbuffer[0] == '/' && realbuffer[1] == '/') {
if ((realbuffer[2] == '\\') && (realbuffer[3] == 'n')) {
2020-08-30 11:54:41 +02:00
helpString += '\n';
2009-10-04 10:30:41 +03:00
helpString += &realbuffer[4];
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
helpString += &realbuffer[2];
2016-07-09 11:21:54 +02:00
}
cmSystemTools::GetLineFromStream(fin, buffer);
2016-03-13 13:35:51 +01:00
lineno++;
realbuffer = buffer.c_str();
2016-07-09 11:21:54 +02:00
if (!fin) {
continue;
}
2016-07-09 11:21:54 +02:00
}
2023-07-02 19:51:09 +02:00
e.SetProperty("HELPSTRING", helpString);
2016-07-09 11:21:54 +02:00
if (cmState::ParseCacheEntry(realbuffer, entryKey, e.Value, e.Type)) {
if (excludes.find(entryKey) == excludes.end()) {
// Load internal values if internal is set.
// If the entry is not internal to the cache being loaded
// or if it is in the list of internal entries to be
// imported, load it.
2017-04-14 19:02:05 +02:00
if (internal || (e.Type != cmStateEnums::INTERNAL) ||
2016-07-09 11:21:54 +02:00
(includes.find(entryKey) != includes.end())) {
// If we are loading the cache from another project,
// make all loaded entries internal so that it is
// not visible in the gui
2016-07-09 11:21:54 +02:00
if (!internal) {
2017-04-14 19:02:05 +02:00
e.Type = cmStateEnums::INTERNAL;
2020-02-01 23:06:01 +01:00
helpString = cmStrCat("DO NOT EDIT, ", entryKey,
" loaded from external file. "
"To change this value edit this file: ",
path, "/CMakeCache.txt");
2023-07-02 19:51:09 +02:00
e.SetProperty("HELPSTRING", helpString);
2016-07-09 11:21:54 +02:00
}
if (!this->ReadPropertyEntry(entryKey, e)) {
e.Initialized = true;
this->Cache[entryKey] = e;
}
}
}
2016-07-09 11:21:54 +02:00
} else {
2016-03-13 13:35:51 +01:00
std::ostringstream error;
2020-08-30 11:54:41 +02:00
error << "Parse error in cache file " << cacheFile << " on line "
<< lineno << ". Offending entry: " << realbuffer;
2019-11-11 23:01:05 +01:00
cmSystemTools::Error(error.str());
}
2016-07-09 11:21:54 +02:00
}
this->CacheMajorVersion = 0;
this->CacheMinorVersion = 0;
2021-11-20 13:41:27 +01:00
if (cmValue cmajor =
2016-07-09 11:21:54 +02:00
this->GetInitializedCacheValue("CMAKE_CACHE_MAJOR_VERSION")) {
unsigned int v = 0;
2018-10-28 12:09:07 +01:00
if (sscanf(cmajor->c_str(), "%u", &v) == 1) {
this->CacheMajorVersion = v;
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
if (cmValue cminor =
2016-07-09 11:21:54 +02:00
this->GetInitializedCacheValue("CMAKE_CACHE_MINOR_VERSION")) {
2018-10-28 12:09:07 +01:00
if (sscanf(cminor->c_str(), "%u", &v) == 1) {
this->CacheMinorVersion = v;
}
}
2016-07-09 11:21:54 +02:00
} else {
// CMake version not found in the list file.
// Set as version 0.0
this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION", "0",
"Minor version of cmake used to create the "
2016-07-09 11:21:54 +02:00
"current loaded cache",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION", "0",
"Major version of cmake used to create the "
2016-07-09 11:21:54 +02:00
"current loaded cache",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
2016-07-09 11:21:54 +02:00
}
// check to make sure the cache directory has not
// been moved
2021-11-20 13:41:27 +01:00
cmValue oldDir = this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
2016-07-09 11:21:54 +02:00
if (internal && oldDir) {
std::string currentcwd = path;
2018-10-28 12:09:07 +01:00
std::string oldcwd = *oldDir;
cmSystemTools::ConvertToUnixSlashes(currentcwd);
currentcwd += "/CMakeCache.txt";
oldcwd += "/CMakeCache.txt";
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::SameFile(oldcwd, currentcwd)) {
2021-11-20 13:41:27 +01:00
cmValue dir = this->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR");
2018-08-09 18:06:22 +02:00
std::ostringstream message;
message << "The current CMakeCache.txt directory " << currentcwd
2018-10-28 12:09:07 +01:00
<< " is different than the directory " << (dir ? *dir : "")
2018-08-09 18:06:22 +02:00
<< " where CMakeCache.txt was created. This may result "
"in binaries being created in the wrong place. If you "
"are not sure, reedit the CMakeCache.txt";
2019-11-11 23:01:05 +01:00
cmSystemTools::Error(message.str());
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
this->CacheLoaded = true;
return true;
}
2016-07-09 11:21:54 +02:00
const char* cmCacheManager::PersistentProperties[] = { "ADVANCED", "MODIFIED",
2020-08-30 11:54:41 +02:00
"STRINGS" };
2009-10-04 10:30:41 +03:00
2020-08-30 11:54:41 +02:00
bool cmCacheManager::ReadPropertyEntry(const std::string& entryKey,
const CacheEntry& e)
2009-10-04 10:30:41 +03:00
{
// All property entries are internal.
2017-04-14 19:02:05 +02:00
if (e.Type != cmStateEnums::INTERNAL) {
2009-10-04 10:30:41 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
const char* end = entryKey.c_str() + entryKey.size();
2020-08-30 11:54:41 +02:00
for (const char* p : cmCacheManager::PersistentProperties) {
std::string::size_type plen = strlen(p) + 1;
2016-07-09 11:21:54 +02:00
if (entryKey.size() > plen && *(end - plen) == '-' &&
2020-08-30 11:54:41 +02:00
strcmp(end - plen + 1, p) == 0) {
2009-10-04 10:30:41 +03:00
std::string key = entryKey.substr(0, entryKey.size() - plen);
2021-09-14 00:13:48 +02:00
if (auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
// Store this property on its entry.
2023-07-02 19:51:09 +02:00
entry->SetProperty(p, e.Value);
2020-08-30 11:54:41 +02:00
} else {
2009-10-04 10:30:41 +03:00
// Create an entry and store the property.
CacheEntry& ne = this->Cache[key];
2023-07-02 19:51:09 +02:00
ne.SetProperty(p, e.Value);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
return true;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return false;
}
2020-08-30 11:54:41 +02:00
void cmCacheManager::WritePropertyEntries(std::ostream& os,
const std::string& entryKey,
const CacheEntry& e,
cmMessenger* messenger) const
2009-10-04 10:30:41 +03:00
{
2020-08-30 11:54:41 +02:00
for (const char* p : cmCacheManager::PersistentProperties) {
2021-11-20 13:41:27 +01:00
if (cmValue value = e.GetProperty(p)) {
2020-02-01 23:06:01 +01:00
std::string helpstring =
2020-08-30 11:54:41 +02:00
cmStrCat(p, " property for variable: ", entryKey);
2009-10-04 10:30:41 +03:00
cmCacheManager::OutputHelpString(os, helpstring);
2020-08-30 11:54:41 +02:00
std::string key = cmStrCat(entryKey, '-', p);
2019-11-11 23:01:05 +01:00
cmCacheManager::OutputKey(os, key);
2009-10-04 10:30:41 +03:00
os << ":INTERNAL=";
2020-08-30 11:54:41 +02:00
cmCacheManager::OutputValue(os, *value);
os << '\n';
cmCacheManager::OutputNewlineTruncationWarning(os, key, *value,
2018-04-23 21:13:27 +02:00
messenger);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
2018-04-23 21:13:27 +02:00
bool cmCacheManager::SaveCache(const std::string& path, cmMessenger* messenger)
{
2020-02-01 23:06:01 +01:00
std::string cacheFile = cmStrCat(path, "/CMakeCache.txt");
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream fout(cacheFile);
2012-04-19 19:04:21 +03:00
fout.SetCopyIfDifferent(true);
2016-07-09 11:21:54 +02:00
if (!fout) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Unable to open cache file for save. " + cacheFile);
cmSystemTools::ReportLastSystemError("");
return false;
2016-07-09 11:21:54 +02:00
}
// before writing the cache, update the version numbers
// to the
2018-08-09 18:06:22 +02:00
this->AddCacheEntry("CMAKE_CACHE_MAJOR_VERSION",
2021-11-20 13:41:27 +01:00
std::to_string(cmVersion::GetMajorVersion()),
2018-08-09 18:06:22 +02:00
"Major version of cmake used to create the "
2016-07-09 11:21:54 +02:00
"current loaded cache",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
2018-08-09 18:06:22 +02:00
this->AddCacheEntry("CMAKE_CACHE_MINOR_VERSION",
2021-11-20 13:41:27 +01:00
std::to_string(cmVersion::GetMinorVersion()),
2018-08-09 18:06:22 +02:00
"Minor version of cmake used to create the "
2016-07-09 11:21:54 +02:00
"current loaded cache",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
2018-08-09 18:06:22 +02:00
this->AddCacheEntry("CMAKE_CACHE_PATCH_VERSION",
2021-11-20 13:41:27 +01:00
std::to_string(cmVersion::GetPatchVersion()),
2009-10-04 10:30:41 +03:00
"Patch version of cmake used to create the "
2016-07-09 11:21:54 +02:00
"current loaded cache",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
// Let us store the current working directory so that if somebody
// Copies it, he will not be surprised
std::string currentcwd = path;
2016-07-09 11:21:54 +02:00
if (currentcwd[0] >= 'A' && currentcwd[0] <= 'Z' && currentcwd[1] == ':') {
2009-10-04 10:30:41 +03:00
// Cast added to avoid compiler warning. Cast is ok because
// value is guaranteed to fit in char by the above if...
currentcwd[0] = static_cast<char>(currentcwd[0] - 'A' + 'a');
2016-07-09 11:21:54 +02:00
}
cmSystemTools::ConvertToUnixSlashes(currentcwd);
2021-11-20 13:41:27 +01:00
this->AddCacheEntry("CMAKE_CACHEFILE_DIR", currentcwd,
2009-10-04 10:30:41 +03:00
"This is the directory where this CMakeCache.txt"
2016-07-09 11:21:54 +02:00
" was created",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
2016-07-09 11:21:54 +02:00
/* clang-format off */
fout << "# This is the CMakeCache file.\n"
2020-08-30 11:54:41 +02:00
"# For build in directory: " << currentcwd << "\n"
"# It was generated by CMake: "
<< cmSystemTools::GetCMakeCommand()
<< "\n"
"# You can edit this file to change values found and used by cmake."
"\n"
"# If you do not want to change any of the values, simply exit the "
"editor.\n"
"# If you do want to change a value, simply edit, save, and exit "
"the editor.\n"
"# The syntax for the file is as follows:\n"
"# KEY:TYPE=VALUE\n"
"# KEY is the name of a variable in the cache.\n"
"# TYPE is a hint to GUIs for the type of VALUE, DO NOT EDIT TYPE!."
"\n"
"# VALUE is the current value for the KEY.\n"
"\n"
"########################\n"
"# EXTERNAL cache entries\n"
"########################\n"
"\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2018-01-26 17:06:56 +01:00
for (auto const& i : this->Cache) {
CacheEntry const& ce = i.second;
2017-04-14 19:02:05 +02:00
cmStateEnums::CacheEntryType t = ce.Type;
2016-07-09 11:21:54 +02:00
if (!ce.Initialized) {
/*
// This should be added in, but is not for now.
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Cache entry \"" + i.first + "\" is uninitialized");
*/
2017-04-14 19:02:05 +02:00
} else if (t != cmStateEnums::INTERNAL) {
// Format is key:type=value
2021-11-20 13:41:27 +01:00
if (cmValue help = ce.GetProperty("HELPSTRING")) {
2020-08-30 11:54:41 +02:00
cmCacheManager::OutputHelpString(fout, *help);
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
cmCacheManager::OutputHelpString(fout, "Missing description");
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
cmCacheManager::OutputKey(fout, i.first);
2020-08-30 11:54:41 +02:00
fout << ':' << cmState::CacheEntryTypeToString(t) << '=';
2019-11-11 23:01:05 +01:00
cmCacheManager::OutputValue(fout, ce.Value);
2020-08-30 11:54:41 +02:00
fout << '\n';
2018-04-23 21:13:27 +02:00
cmCacheManager::OutputNewlineTruncationWarning(fout, i.first, ce.Value,
messenger);
2020-08-30 11:54:41 +02:00
fout << '\n';
}
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
fout << "\n"
"########################\n"
"# INTERNAL cache entries\n"
"########################\n"
"\n";
2020-08-30 11:54:41 +02:00
for (auto const& i : this->Cache) {
if (!i.second.Initialized) {
continue;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
cmStateEnums::CacheEntryType t = i.second.GetType();
this->WritePropertyEntries(fout, i.first, i.second, messenger);
2017-04-14 19:02:05 +02:00
if (t == cmStateEnums::INTERNAL) {
// Format is key:type=value
2021-11-20 13:41:27 +01:00
if (cmValue help = i.second.GetProperty("HELPSTRING")) {
2020-08-30 11:54:41 +02:00
cmCacheManager::OutputHelpString(fout, *help);
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
cmCacheManager::OutputKey(fout, i.first);
fout << ':' << cmState::CacheEntryTypeToString(t) << '=';
cmCacheManager::OutputValue(fout, i.second.GetValue());
fout << '\n';
cmCacheManager::OutputNewlineTruncationWarning(
fout, i.first, i.second.GetValue(), messenger);
}
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
fout << '\n';
2012-04-19 19:04:21 +03:00
fout.Close();
2020-02-01 23:06:01 +01:00
std::string checkCacheFile = cmStrCat(path, "/CMakeFiles");
2018-04-23 21:13:27 +02:00
cmSystemTools::MakeDirectory(checkCacheFile);
checkCacheFile += "/cmake.check_cache";
2014-08-03 19:52:23 +02:00
cmsys::ofstream checkCache(checkCacheFile.c_str());
2016-07-09 11:21:54 +02:00
if (!checkCache) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Unable to open check cache file for write. " +
checkCacheFile);
return false;
2016-07-09 11:21:54 +02:00
}
checkCache << "# This file is generated by cmake for dependency checking "
2016-07-09 11:21:54 +02:00
"of the CMakeCache.txt file\n";
return true;
}
2015-04-27 22:25:09 +02:00
bool cmCacheManager::DeleteCache(const std::string& path)
{
std::string cacheFile = path;
cmSystemTools::ConvertToUnixSlashes(cacheFile);
std::string cmakeFiles = cacheFile;
cacheFile += "/CMakeCache.txt";
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(cacheFile)) {
2015-04-27 22:25:09 +02:00
cmSystemTools::RemoveFile(cacheFile);
2013-03-16 19:13:01 +02:00
// now remove the files in the CMakeFiles directory
// this cleans up language cache files
2019-11-11 23:01:05 +01:00
cmakeFiles += "/CMakeFiles";
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileIsDirectory(cmakeFiles)) {
2015-04-27 22:25:09 +02:00
cmSystemTools::RemoveADirectory(cmakeFiles);
}
2016-07-09 11:21:54 +02:00
}
return true;
}
2009-10-04 10:30:41 +03:00
void cmCacheManager::OutputKey(std::ostream& fout, std::string const& key)
{
// support : in key name by double quoting
2016-07-09 11:21:54 +02:00
const char* q =
2020-08-30 11:54:41 +02:00
(key.find(':') != std::string::npos || cmHasLiteralPrefix(key, "//"))
? "\""
: "";
2009-10-04 10:30:41 +03:00
fout << q << key << q;
}
void cmCacheManager::OutputValue(std::ostream& fout, std::string const& value)
2018-04-23 21:13:27 +02:00
{
// look for and truncate newlines
std::string::size_type newline = value.find('\n');
if (newline != std::string::npos) {
std::string truncated = value.substr(0, newline);
OutputValueNoNewlines(fout, truncated);
} else {
OutputValueNoNewlines(fout, value);
}
}
void cmCacheManager::OutputValueNoNewlines(std::ostream& fout,
std::string const& value)
2009-10-04 10:30:41 +03:00
{
// if value has trailing space or tab, enclose it in single quotes
2019-11-11 23:01:05 +01:00
if (!value.empty() && (value.back() == ' ' || value.back() == '\t')) {
2009-10-04 10:30:41 +03:00
fout << '\'' << value << '\'';
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
fout << value;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
void cmCacheManager::OutputHelpString(std::ostream& fout,
const std::string& helpString)
{
std::string::size_type end = helpString.size();
2016-07-09 11:21:54 +02:00
if (end == 0) {
return;
2016-07-09 11:21:54 +02:00
}
std::string oneLine;
std::string::size_type pos = 0;
2016-07-09 11:21:54 +02:00
for (std::string::size_type i = 0; i <= end; i++) {
if ((i == end) || (helpString[i] == '\n') ||
((i - pos >= 60) && (helpString[i] == ' '))) {
fout << "//";
2016-07-09 11:21:54 +02:00
if (helpString[pos] == '\n') {
pos++;
fout << "\\n";
2016-07-09 11:21:54 +02:00
}
oneLine = helpString.substr(pos, i - pos);
2020-08-30 11:54:41 +02:00
fout << oneLine << '\n';
pos = i;
}
2016-07-09 11:21:54 +02:00
}
}
2018-04-23 21:13:27 +02:00
void cmCacheManager::OutputWarningComment(std::ostream& fout,
std::string const& message,
bool wrapSpaces)
{
std::string::size_type end = message.size();
std::string oneLine;
std::string::size_type pos = 0;
for (std::string::size_type i = 0; i <= end; i++) {
if ((i == end) || (message[i] == '\n') ||
((i - pos >= 60) && (message[i] == ' ') && wrapSpaces)) {
fout << "# ";
if (message[pos] == '\n') {
pos++;
fout << "\\n";
}
oneLine = message.substr(pos, i - pos);
2020-08-30 11:54:41 +02:00
fout << oneLine << '\n';
2018-04-23 21:13:27 +02:00
pos = i;
}
}
}
void cmCacheManager::OutputNewlineTruncationWarning(std::ostream& fout,
std::string const& key,
std::string const& value,
cmMessenger* messenger)
{
if (value.find('\n') != std::string::npos) {
if (messenger) {
2020-02-01 23:06:01 +01:00
std::string message =
cmStrCat("Value of ", key, " contained a newline; truncating");
2019-11-11 23:01:05 +01:00
messenger->IssueMessage(MessageType::WARNING, message);
2018-04-23 21:13:27 +02:00
}
2020-02-01 23:06:01 +01:00
std::string comment =
cmStrCat("WARNING: Value of ", key,
" contained a newline and was truncated. Original value:");
2018-04-23 21:13:27 +02:00
OutputWarningComment(fout, comment, true);
OutputWarningComment(fout, value, false);
}
}
2015-04-27 22:25:09 +02:00
void cmCacheManager::RemoveCacheEntry(const std::string& key)
{
2020-08-30 11:54:41 +02:00
this->Cache.erase(key);
}
2016-07-09 11:21:54 +02:00
cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
const std::string& key)
{
2020-02-01 23:06:01 +01:00
auto i = this->Cache.find(key);
2016-07-09 11:21:54 +02:00
if (i != this->Cache.end()) {
return &i->second;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
return nullptr;
}
2020-08-30 11:54:41 +02:00
const cmCacheManager::CacheEntry* cmCacheManager::GetCacheEntry(
const std::string& key) const
{
2020-08-30 11:54:41 +02:00
auto i = this->Cache.find(key);
if (i != this->Cache.end()) {
return &i->second;
}
return nullptr;
}
2021-11-20 13:41:27 +01:00
cmValue cmCacheManager::GetInitializedCacheValue(const std::string& key) const
{
2021-09-14 00:13:48 +02:00
if (const auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
if (entry->Initialized) {
2021-11-20 13:41:27 +01:00
return cmValue(entry->GetValue());
2020-08-30 11:54:41 +02:00
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
return nullptr;
}
void cmCacheManager::PrintCache(std::ostream& out) const
{
2020-08-30 11:54:41 +02:00
out << "=================================================\n"
"CMakeCache Contents:\n";
2018-01-26 17:06:56 +01:00
for (auto const& i : this->Cache) {
if (i.second.Type != cmStateEnums::INTERNAL) {
2020-08-30 11:54:41 +02:00
out << i.first << " = " << i.second.Value << '\n';
}
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
out << "\n\n"
"To change values in the CMakeCache, \n"
"edit CMakeCache.txt in your output directory.\n"
"=================================================\n";
}
2021-11-20 13:41:27 +01:00
void cmCacheManager::AddCacheEntry(const std::string& key, cmValue value,
2023-07-02 19:51:09 +02:00
cmValue helpString,
2017-04-14 19:02:05 +02:00
cmStateEnums::CacheEntryType type)
{
CacheEntry& e = this->Cache[key];
2020-08-30 11:54:41 +02:00
e.SetValue(value);
e.Type = type;
// make sure we only use unix style paths
2017-04-14 19:02:05 +02:00
if (type == cmStateEnums::FILEPATH || type == cmStateEnums::PATH) {
2017-07-20 19:35:53 +02:00
if (e.Value.find(';') != std::string::npos) {
2023-07-02 19:51:09 +02:00
cmList paths{ e.Value };
2018-01-26 17:06:56 +01:00
for (std::string& i : paths) {
cmSystemTools::ConvertToUnixSlashes(i);
}
2023-07-02 19:51:09 +02:00
e.Value = paths.to_string();
2016-07-09 11:21:54 +02:00
} else {
cmSystemTools::ConvertToUnixSlashes(e.Value);
}
2016-07-09 11:21:54 +02:00
}
2023-07-02 19:51:09 +02:00
e.SetProperty(
"HELPSTRING",
helpString ? *helpString
: std::string{
"(This variable does not exist and should not be used)" });
}
2021-11-20 13:41:27 +01:00
void cmCacheManager::CacheEntry::SetValue(cmValue value)
{
2016-07-09 11:21:54 +02:00
if (value) {
2021-11-20 13:41:27 +01:00
this->Value = *value;
2020-08-30 11:54:41 +02:00
this->Initialized = true;
2016-07-09 11:21:54 +02:00
} else {
2020-08-30 11:54:41 +02:00
this->Value.clear();
2016-07-09 11:21:54 +02:00
}
}
2016-10-30 18:24:19 +01:00
std::vector<std::string> cmCacheManager::CacheEntry::GetPropertyList() const
{
2020-02-01 23:06:01 +01:00
return this->Properties.GetKeys();
2016-10-30 18:24:19 +01:00
}
2021-11-20 13:41:27 +01:00
cmValue cmCacheManager::CacheEntry::GetProperty(const std::string& prop) const
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
if (prop == "TYPE") {
2021-11-20 13:41:27 +01:00
return cmValue(cmState::CacheEntryTypeToString(this->Type));
2016-10-30 18:24:19 +01:00
}
if (prop == "VALUE") {
2021-11-20 13:41:27 +01:00
return cmValue(this->Value);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
return this->Properties.GetPropertyValue(prop);
}
2020-08-30 11:54:41 +02:00
bool cmCacheManager::CacheEntry::GetPropertyAsBool(
const std::string& prop) const
{
2021-09-14 00:13:48 +02:00
return cmIsOn(this->GetProperty(prop));
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
void cmCacheManager::CacheEntry::SetProperty(const std::string& prop,
2023-07-02 19:51:09 +02:00
const std::string& value)
{
2016-07-09 11:21:54 +02:00
if (prop == "TYPE") {
2023-07-02 19:51:09 +02:00
this->Type = cmState::StringToCacheEntryType(value);
2016-07-09 11:21:54 +02:00
} else if (prop == "VALUE") {
2023-07-02 19:51:09 +02:00
this->Value = value;
2016-07-09 11:21:54 +02:00
} else {
2015-11-17 17:22:37 +01:00
this->Properties.SetProperty(prop, value);
2016-07-09 11:21:54 +02:00
}
}
2020-08-30 11:54:41 +02:00
void cmCacheManager::CacheEntry::SetProperty(const std::string& p, bool v)
{
2023-07-02 19:51:09 +02:00
this->SetProperty(p, v ? std::string{ "ON" } : std::string{ "OFF" });
}
2023-12-07 09:12:54 +01:00
void cmCacheManager::CacheEntry::RemoveProperty(const std::string& prop)
2023-07-02 19:51:09 +02:00
{
if (prop == "TYPE") {
this->Type = cmState::StringToCacheEntryType("STRING");
} else if (prop == "VALUE") {
2023-12-07 09:12:54 +01:00
this->Value.clear();
2023-07-02 19:51:09 +02:00
} else {
2023-12-07 09:12:54 +01:00
this->Properties.RemoveProperty(prop);
2023-07-02 19:51:09 +02:00
}
2020-08-30 11:54:41 +02:00
}
2015-04-27 22:25:09 +02:00
void cmCacheManager::CacheEntry::AppendProperty(const std::string& prop,
2020-08-30 11:54:41 +02:00
const std::string& value,
2012-02-18 12:40:36 +02:00
bool asString)
{
2016-07-09 11:21:54 +02:00
if (prop == "TYPE") {
2020-08-30 11:54:41 +02:00
this->Type =
cmState::StringToCacheEntryType(!value.empty() ? value : "STRING");
2016-07-09 11:21:54 +02:00
} else if (prop == "VALUE") {
2020-08-30 11:54:41 +02:00
if (!value.empty()) {
if (!this->Value.empty() && !asString) {
2009-10-04 10:30:41 +03:00
this->Value += ";";
}
2016-07-09 11:21:54 +02:00
this->Value += value;
}
2016-07-09 11:21:54 +02:00
} else {
2015-11-17 17:22:37 +01:00
this->Properties.AppendProperty(prop, value, asString);
2016-07-09 11:21:54 +02:00
}
}