cmake/Source/cmSetCommand.cxx

143 lines
4.5 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 "cmSetCommand.h"
2017-04-14 19:02:05 +02:00
#include "cmAlgorithms.h"
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmSetCommand
2016-07-09 11:21:54 +02:00
bool cmSetCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// watch for ENV signatures
2018-04-23 21:13:27 +02:00
auto const& variable = args[0]; // VAR is always first
if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
// what is the variable name
2018-04-23 21:13:27 +02:00
auto const& varName = variable.substr(4, variable.size() - 5);
std::string putEnvArg = varName + "=";
2013-03-16 19:13:01 +02:00
// what is the current value if any
2016-10-30 18:24:19 +01:00
std::string currValue;
const bool currValueSet = cmSystemTools::GetEnv(varName, currValue);
// will it be set to something, then set it
2016-07-09 11:21:54 +02:00
if (args.size() > 1 && !args[1].empty()) {
// but only if it is different from current value
2016-10-30 18:24:19 +01:00
if (!currValueSet || currValue != args[1]) {
putEnvArg += args[1];
2015-04-27 22:25:09 +02:00
cmSystemTools::PutEnv(putEnvArg);
}
2016-07-09 11:21:54 +02:00
return true;
}
2013-03-16 19:13:01 +02:00
2015-11-17 17:22:37 +01:00
// if it will be cleared, then clear it if it isn't already clear
2016-10-30 18:24:19 +01:00
if (currValueSet) {
2015-04-27 22:25:09 +02:00
cmSystemTools::PutEnv(putEnvArg);
}
2016-07-09 11:21:54 +02:00
return true;
}
2013-03-16 19:13:01 +02:00
// SET (VAR) // Removes the definition of VAR.
2016-07-09 11:21:54 +02:00
if (args.size() == 1) {
2015-11-17 17:22:37 +01:00
this->Makefile->RemoveDefinition(variable);
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// SET (VAR PARENT_SCOPE) // Removes the definition of VAR
2016-07-09 11:21:54 +02:00
// in the parent scope.
2016-10-30 18:24:19 +01:00
if (args.size() == 2 && args[args.size() - 1] == "PARENT_SCOPE") {
2018-01-26 17:06:56 +01:00
this->Makefile->RaiseScope(variable, nullptr);
2014-08-03 19:52:23 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// here are the remaining options
// SET (VAR value )
2014-08-03 19:52:23 +02:00
// SET (VAR value PARENT_SCOPE)
// SET (VAR CACHE TYPE "doc String" [FORCE])
// SET (VAR value CACHE TYPE "doc string" [FORCE])
std::string value; // optional
bool cache = false; // optional
bool force = false; // optional
bool parentScope = false;
2017-04-14 19:02:05 +02:00
cmStateEnums::CacheEntryType type =
2018-01-26 17:06:56 +01:00
cmStateEnums::STRING; // required if cache
const char* docstring = nullptr; // required if cache
2013-03-16 19:13:01 +02:00
unsigned int ignoreLastArgs = 0;
// look for PARENT_SCOPE argument
2016-07-09 11:21:54 +02:00
if (args.size() > 1 && args[args.size() - 1] == "PARENT_SCOPE") {
parentScope = true;
ignoreLastArgs++;
2016-07-09 11:21:54 +02:00
} else {
// look for FORCE argument
2016-07-09 11:21:54 +02:00
if (args.size() > 4 && args[args.size() - 1] == "FORCE") {
force = true;
ignoreLastArgs++;
2016-07-09 11:21:54 +02:00
}
// check for cache signature
2016-07-09 11:21:54 +02:00
if (args.size() > 3 &&
args[args.size() - 3 - (force ? 1 : 0)] == "CACHE") {
cache = true;
2016-07-09 11:21:54 +02:00
ignoreLastArgs += 3;
}
2016-07-09 11:21:54 +02:00
}
2010-11-13 01:00:53 +02:00
// collect any values into a single semi-colon separated value list
2015-11-17 17:22:37 +01:00
value = cmJoin(cmMakeRange(args).advance(1).retreat(ignoreLastArgs), ";");
2016-07-09 11:21:54 +02:00
if (parentScope) {
2014-08-03 19:52:23 +02:00
this->Makefile->RaiseScope(variable, value.c_str());
return true;
2016-07-09 11:21:54 +02:00
}
// we should be nice and try to catch some simple screwups if the last or
// next to last args are CACHE then they screwed up. If they used FORCE
// without CACHE they screwed up
2009-10-04 10:30:41 +03:00
if ((args[args.size() - 1] == "CACHE") ||
(args.size() > 1 && args[args.size() - 2] == "CACHE") ||
2016-07-09 11:21:54 +02:00
(force && !cache)) {
this->SetError("given invalid arguments for CACHE mode.");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (cache) {
2009-10-04 10:30:41 +03:00
std::string::size_type cacheStart = args.size() - 3 - (force ? 1 : 0);
2016-07-09 11:21:54 +02:00
type = cmState::StringToCacheEntryType(args[cacheStart + 1].c_str());
docstring = args[cacheStart + 2].c_str();
}
// see if this is already in the cache
2015-08-17 11:37:30 +02:00
cmState* state = this->Makefile->GetState();
const char* existingValue = state->GetCacheEntryValue(variable);
2016-07-09 11:21:54 +02:00
if (existingValue &&
2017-04-14 19:02:05 +02:00
(state->GetCacheEntryType(variable) != cmStateEnums::UNINITIALIZED)) {
// if the set is trying to CACHE the value but the value
// is already in the cache and the type is not internal
// then leave now without setting any definitions in the cache
// or the makefile
2017-04-14 19:02:05 +02:00
if (cache && type != cmStateEnums::INTERNAL && !force) {
return true;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// if it is meant to be in the cache then define it in the cache
2016-07-09 11:21:54 +02:00
if (cache) {
this->Makefile->AddCacheDefinition(variable, value.c_str(), docstring,
type, force);
} else {
// add the definition
this->Makefile->AddDefinition(variable, value.c_str());
2016-07-09 11:21:54 +02:00
}
return true;
}