cmake/Source/cmCacheManager.h

228 lines
7.3 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. */
2021-09-14 00:13:48 +02:00
#pragma once
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <iosfwd>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
2017-04-14 19:02:05 +02:00
#include "cmPropertyMap.h"
#include "cmStateTypes.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
2018-04-23 21:13:27 +02:00
class cmMessenger;
/** \class cmCacheManager
* \brief Control class for cmake's cache
*
* Load and Save CMake cache files.
2013-03-16 19:13:01 +02:00
*
*/
class cmCacheManager
{
2020-08-30 11:54:41 +02:00
class CacheEntry
{
2020-08-30 11:54:41 +02:00
friend class cmCacheManager;
public:
2020-08-30 11:54:41 +02:00
const std::string& GetValue() const { return this->Value; }
2021-11-20 13:41:27 +01:00
void SetValue(cmValue);
2020-08-30 11:54:41 +02:00
cmStateEnums::CacheEntryType GetType() const { return this->Type; }
void SetType(cmStateEnums::CacheEntryType ty) { this->Type = ty; }
2016-10-30 18:24:19 +01:00
std::vector<std::string> GetPropertyList() const;
2021-11-20 13:41:27 +01:00
cmValue GetProperty(const std::string& property) const;
2020-08-30 11:54:41 +02:00
bool GetPropertyAsBool(const std::string& property) const;
2015-04-27 22:25:09 +02:00
void SetProperty(const std::string& property, const char* value);
void SetProperty(const std::string& property, bool value);
2020-08-30 11:54:41 +02:00
void AppendProperty(const std::string& property, const std::string& value,
bool asString = false);
2016-07-09 11:21:54 +02:00
private:
2020-08-30 11:54:41 +02:00
std::string Value;
cmStateEnums::CacheEntryType Type = cmStateEnums::UNINITIALIZED;
cmPropertyMap Properties;
bool Initialized = false;
};
2013-03-16 19:13:01 +02:00
2020-08-30 11:54:41 +02:00
public:
2019-11-11 23:01:05 +01:00
//! Load a cache for given makefile. Loads from path/CMakeCache.txt.
2015-04-27 22:25:09 +02:00
bool LoadCache(const std::string& path, bool internal,
std::set<std::string>& excludes,
std::set<std::string>& includes);
2019-11-11 23:01:05 +01:00
//! Save cache for given makefile. Saves to output path/CMakeCache.txt
2018-04-23 21:13:27 +02:00
bool SaveCache(const std::string& path, cmMessenger* messenger);
2019-11-11 23:01:05 +01:00
//! Delete the cache given
2015-04-27 22:25:09 +02:00
bool DeleteCache(const std::string& path);
2019-11-11 23:01:05 +01:00
//! Print the cache to a stream
void PrintCache(std::ostream&) const;
2013-03-16 19:13:01 +02:00
2021-09-14 00:13:48 +02:00
//! Get whether or not cache is loaded
bool IsCacheLoaded() const { return this->CacheLoaded; }
2020-08-30 11:54:41 +02:00
//! Get a value from the cache given a key
2021-11-20 13:41:27 +01:00
cmValue GetInitializedCacheValue(const std::string& key) const;
2013-03-16 19:13:01 +02:00
2021-11-20 13:41:27 +01:00
cmValue GetCacheEntryValue(const std::string& key) const
2020-08-30 11:54:41 +02:00
{
2021-09-14 00:13:48 +02:00
if (const auto* entry = this->GetCacheEntry(key)) {
2021-11-20 13:41:27 +01:00
return cmValue(entry->GetValue());
2020-08-30 11:54:41 +02:00
}
return nullptr;
}
2013-03-16 19:13:01 +02:00
2020-08-30 11:54:41 +02:00
void SetCacheEntryValue(std::string const& key, std::string const& value)
{
2021-09-14 00:13:48 +02:00
if (auto* entry = this->GetCacheEntry(key)) {
2021-11-20 13:41:27 +01:00
entry->SetValue(cmValue(value));
2020-08-30 11:54:41 +02:00
}
}
2015-08-17 11:37:30 +02:00
2020-08-30 11:54:41 +02:00
cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const
2015-08-17 11:37:30 +02:00
{
2021-09-14 00:13:48 +02:00
if (const auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
return entry->GetType();
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
return cmStateEnums::UNINITIALIZED;
2015-08-17 11:37:30 +02:00
}
2020-08-30 11:54:41 +02:00
std::vector<std::string> GetCacheEntryPropertyList(
std::string const& key) const
2015-08-17 11:37:30 +02:00
{
2021-09-14 00:13:48 +02:00
if (const auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
return entry->GetPropertyList();
}
return {};
2015-08-17 11:37:30 +02:00
}
2021-11-20 13:41:27 +01:00
cmValue GetCacheEntryProperty(std::string const& key,
std::string const& propName) const
2015-08-17 11:37:30 +02:00
{
2021-09-14 00:13:48 +02:00
if (const auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
return entry->GetProperty(propName);
}
return nullptr;
2015-08-17 11:37:30 +02:00
}
bool GetCacheEntryPropertyAsBool(std::string const& key,
2020-08-30 11:54:41 +02:00
std::string const& propName) const
2015-08-17 11:37:30 +02:00
{
2021-09-14 00:13:48 +02:00
if (const auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
return entry->GetPropertyAsBool(propName);
}
return false;
2015-08-17 11:37:30 +02:00
}
void SetCacheEntryProperty(std::string const& key,
std::string const& propName,
std::string const& value)
{
2021-09-14 00:13:48 +02:00
if (auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
entry->SetProperty(propName, value.c_str());
}
2015-08-17 11:37:30 +02:00
}
void SetCacheEntryBoolProperty(std::string const& key,
2016-07-09 11:21:54 +02:00
std::string const& propName, bool value)
2015-08-17 11:37:30 +02:00
{
2021-09-14 00:13:48 +02:00
if (auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
entry->SetProperty(propName, value);
}
2015-08-17 11:37:30 +02:00
}
void RemoveCacheEntryProperty(std::string const& key,
std::string const& propName)
{
2021-09-14 00:13:48 +02:00
if (auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
entry->SetProperty(propName, nullptr);
}
2015-08-17 11:37:30 +02:00
}
void AppendCacheEntryProperty(std::string const& key,
std::string const& propName,
std::string const& value,
bool asString = false)
{
2021-09-14 00:13:48 +02:00
if (auto* entry = this->GetCacheEntry(key)) {
2020-08-30 11:54:41 +02:00
entry->AppendProperty(propName, value, asString);
}
2015-08-17 11:37:30 +02:00
}
2020-08-30 11:54:41 +02:00
std::vector<std::string> GetCacheEntryKeys() const
2015-08-17 11:37:30 +02:00
{
std::vector<std::string> definitions;
2020-08-30 11:54:41 +02:00
definitions.reserve(this->Cache.size());
for (auto const& i : this->Cache) {
definitions.push_back(i.first);
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
return definitions;
}
/** Get the version of CMake that wrote the cache. */
2016-07-09 11:21:54 +02:00
unsigned int GetCacheMajorVersion() const { return this->CacheMajorVersion; }
unsigned int GetCacheMinorVersion() const { return this->CacheMinorVersion; }
2019-11-11 23:01:05 +01:00
//! Add an entry into the cache
2015-04-27 22:25:09 +02:00
void AddCacheEntry(const std::string& key, const char* value,
2021-11-20 13:41:27 +01:00
const char* helpString, cmStateEnums::CacheEntryType type)
{
this->AddCacheEntry(key,
value ? cmValue(std::string(value)) : cmValue(nullptr),
helpString, type);
}
void AddCacheEntry(const std::string& key, const std::string& value,
const char* helpString, cmStateEnums::CacheEntryType type)
{
this->AddCacheEntry(key, cmValue(value), helpString, type);
}
void AddCacheEntry(const std::string& key, cmValue value,
2017-04-14 19:02:05 +02:00
const char* helpString,
cmStateEnums::CacheEntryType type);
2020-08-30 11:54:41 +02:00
//! Remove an entry from the cache
void RemoveCacheEntry(const std::string& key);
private:
2019-11-11 23:01:05 +01:00
//! Get a cache entry object for a key
2016-07-09 11:21:54 +02:00
CacheEntry* GetCacheEntry(const std::string& key);
2020-08-30 11:54:41 +02:00
const CacheEntry* GetCacheEntry(const std::string& key) const;
2019-11-11 23:01:05 +01:00
//! Clean out the CMakeFiles directory if no CMakeCache.txt
2015-04-27 22:25:09 +02:00
void CleanCMakeFiles(const std::string& path);
2009-10-04 10:30:41 +03:00
static void OutputHelpString(std::ostream& fout,
const std::string& helpString);
2018-04-23 21:13:27 +02:00
static void OutputWarningComment(std::ostream& fout,
std::string const& message,
bool wrapSpaces);
static void OutputNewlineTruncationWarning(std::ostream& fout,
std::string const& key,
std::string const& value,
cmMessenger* messenger);
2009-10-04 10:30:41 +03:00
static void OutputKey(std::ostream& fout, std::string const& key);
static void OutputValue(std::ostream& fout, std::string const& value);
2018-04-23 21:13:27 +02:00
static void OutputValueNoNewlines(std::ostream& fout,
std::string const& value);
2009-10-04 10:30:41 +03:00
static const char* PersistentProperties[];
2020-08-30 11:54:41 +02:00
bool ReadPropertyEntry(const std::string& key, const CacheEntry& e);
void WritePropertyEntries(std::ostream& os, const std::string& entryKey,
const CacheEntry& e, cmMessenger* messenger) const;
std::map<std::string, CacheEntry> Cache;
2021-09-14 00:13:48 +02:00
bool CacheLoaded = false;
2020-08-30 11:54:41 +02:00
// Cache version info
unsigned int CacheMajorVersion = 0;
unsigned int CacheMinorVersion = 0;
};