mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-06 05:40:54 -06:00
cmLoadCacheCommand: Port away from cmCommand
This commit is contained in:
@@ -284,8 +284,7 @@ void GetProjectCommands(cmState* state)
|
||||
cm::make_unique<cmTargetLinkOptionsCommand>());
|
||||
state->AddBuiltinCommand("target_link_directories",
|
||||
cm::make_unique<cmTargetLinkDirectoriesCommand>());
|
||||
state->AddBuiltinCommand("load_cache",
|
||||
cm::make_unique<cmLoadCacheCommand>());
|
||||
state->AddBuiltinCommand("load_cache", cmLoadCacheCommand);
|
||||
state->AddBuiltinCommand("qt_wrap_cpp", cmQTWrapCPPCommand);
|
||||
state->AddBuiltinCommand("qt_wrap_ui", cmQTWrapUICommand);
|
||||
state->AddBuiltinCommand("remove_definitions", cmRemoveDefinitionsCommand);
|
||||
|
||||
@@ -3,24 +3,30 @@
|
||||
#include "cmLoadCacheCommand.h"
|
||||
|
||||
#include "cmsys/FStream.hxx"
|
||||
#include <set>
|
||||
|
||||
#include "cmExecutionStatus.h"
|
||||
#include "cmMakefile.h"
|
||||
#include "cmStateTypes.h"
|
||||
#include "cmSystemTools.h"
|
||||
#include "cmake.h"
|
||||
|
||||
class cmExecutionStatus;
|
||||
static bool ReadWithPrefix(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status);
|
||||
|
||||
// cmLoadCacheCommand
|
||||
bool cmLoadCacheCommand::InitialPass(std::vector<std::string> const& args,
|
||||
cmExecutionStatus&)
|
||||
static void CheckLine(cmMakefile& mf, std::string const& prefix,
|
||||
std::set<std::string> const& variablesToRead,
|
||||
const char* line);
|
||||
|
||||
bool cmLoadCacheCommand(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status)
|
||||
{
|
||||
if (args.empty()) {
|
||||
this->SetError("called with wrong number of arguments.");
|
||||
status.SetError("called with wrong number of arguments.");
|
||||
}
|
||||
|
||||
if (args.size() >= 2 && args[1] == "READ_WITH_PREFIX") {
|
||||
return this->ReadWithPrefix(args);
|
||||
return ReadWithPrefix(args, status);
|
||||
}
|
||||
|
||||
// Cache entries to be excluded from the import list.
|
||||
@@ -59,24 +65,26 @@ bool cmLoadCacheCommand::InitialPass(std::vector<std::string> const& args,
|
||||
}
|
||||
}
|
||||
|
||||
cmMakefile& mf = status.GetMakefile();
|
||||
|
||||
// Loop over each build directory listed in the arguments. Each
|
||||
// directory has a cache file.
|
||||
for (std::string const& arg : args) {
|
||||
if ((arg == "EXCLUDE") || (arg == "INCLUDE_INTERNALS")) {
|
||||
break;
|
||||
}
|
||||
this->Makefile->GetCMakeInstance()->LoadCache(arg, false, excludes,
|
||||
includes);
|
||||
mf.GetCMakeInstance()->LoadCache(arg, false, excludes, includes);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cmLoadCacheCommand::ReadWithPrefix(std::vector<std::string> const& args)
|
||||
static bool ReadWithPrefix(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status)
|
||||
{
|
||||
// Make sure we have a prefix.
|
||||
if (args.size() < 3) {
|
||||
this->SetError("READ_WITH_PREFIX form must specify a prefix.");
|
||||
status.SetError("READ_WITH_PREFIX form must specify a prefix.");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -84,17 +92,19 @@ bool cmLoadCacheCommand::ReadWithPrefix(std::vector<std::string> const& args)
|
||||
std::string cacheFile = args[0] + "/CMakeCache.txt";
|
||||
if (!cmSystemTools::FileExists(cacheFile)) {
|
||||
std::string e = "Cannot load cache file from " + cacheFile;
|
||||
this->SetError(e);
|
||||
status.SetError(e);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Prepare the table of variables to read.
|
||||
this->Prefix = args[2];
|
||||
this->VariablesToRead.insert(args.begin() + 3, args.end());
|
||||
std::string const prefix = args[2];
|
||||
std::set<std::string> const variablesToRead(args.begin() + 3, args.end());
|
||||
|
||||
// Read the cache file.
|
||||
cmsys::ifstream fin(cacheFile.c_str());
|
||||
|
||||
cmMakefile& mf = status.GetMakefile();
|
||||
|
||||
// This is a big hack read loop to overcome a buggy ifstream
|
||||
// implementation on HP-UX. This should work on all platforms even
|
||||
// for small buffer sizes.
|
||||
@@ -123,7 +133,7 @@ bool cmLoadCacheCommand::ReadWithPrefix(std::vector<std::string> const& args)
|
||||
}
|
||||
if (i != end) {
|
||||
// Completed a line.
|
||||
this->CheckLine(line.c_str());
|
||||
CheckLine(mf, prefix, variablesToRead, line.c_str());
|
||||
line.clear();
|
||||
|
||||
// Skip the newline character.
|
||||
@@ -134,13 +144,15 @@ bool cmLoadCacheCommand::ReadWithPrefix(std::vector<std::string> const& args)
|
||||
}
|
||||
if (!line.empty()) {
|
||||
// Partial last line.
|
||||
this->CheckLine(line.c_str());
|
||||
CheckLine(mf, prefix, variablesToRead, line.c_str());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void cmLoadCacheCommand::CheckLine(const char* line)
|
||||
static void CheckLine(cmMakefile& mf, std::string const& prefix,
|
||||
std::set<std::string> const& variablesToRead,
|
||||
const char* line)
|
||||
{
|
||||
// Check one line of the cache file.
|
||||
std::string var;
|
||||
@@ -148,14 +160,14 @@ void cmLoadCacheCommand::CheckLine(const char* line)
|
||||
cmStateEnums::CacheEntryType type = cmStateEnums::UNINITIALIZED;
|
||||
if (cmake::ParseCacheEntry(line, var, value, type)) {
|
||||
// Found a real entry. See if this one was requested.
|
||||
if (this->VariablesToRead.find(var) != this->VariablesToRead.end()) {
|
||||
if (variablesToRead.find(var) != variablesToRead.end()) {
|
||||
// This was requested. Set this variable locally with the given
|
||||
// prefix.
|
||||
var = this->Prefix + var;
|
||||
var = prefix + var;
|
||||
if (!value.empty()) {
|
||||
this->Makefile->AddDefinition(var, value);
|
||||
mf.AddDefinition(var, value);
|
||||
} else {
|
||||
this->Makefile->RemoveDefinition(var);
|
||||
mf.RemoveDefinition(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,45 +5,12 @@
|
||||
|
||||
#include "cmConfigure.h" // IWYU pragma: keep
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <cm/memory>
|
||||
|
||||
#include "cmCommand.h"
|
||||
|
||||
class cmExecutionStatus;
|
||||
|
||||
/** \class cmLoadCacheCommand
|
||||
* \brief load a cache file
|
||||
*
|
||||
* cmLoadCacheCommand loads the non internal values of a cache file
|
||||
*/
|
||||
class cmLoadCacheCommand : public cmCommand
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* This is a virtual constructor for the command.
|
||||
*/
|
||||
std::unique_ptr<cmCommand> Clone() override
|
||||
{
|
||||
return cm::make_unique<cmLoadCacheCommand>();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is called when the command is first encountered in
|
||||
* the CMakeLists.txt file.
|
||||
*/
|
||||
bool InitialPass(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status) override;
|
||||
|
||||
protected:
|
||||
std::set<std::string> VariablesToRead;
|
||||
std::string Prefix;
|
||||
|
||||
bool ReadWithPrefix(std::vector<std::string> const& args);
|
||||
void CheckLine(const char* line);
|
||||
};
|
||||
bool cmLoadCacheCommand(std::vector<std::string> const& args,
|
||||
cmExecutionStatus& status);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user