cmake/Source/cmSetCommand.cxx

161 lines
5.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. */
#include "cmSetCommand.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
#include "cmRange.h"
2017-04-14 19:02:05 +02:00
#include "cmState.h"
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
// cmSetCommand
2020-02-01 23:06:01 +01:00
bool cmSetCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.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);
}
2019-11-11 23:01:05 +01:00
// if there's extra arguments, warn user
// that they are ignored by this command.
if (args.size() > 2) {
std::string m = "Only the first value argument is used when setting "
"an environment variable. Argument '" +
args[2] + "' and later are unused.";
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, m);
2019-11-11 23:01:05 +01:00
}
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) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().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.
2019-11-11 23:01:05 +01:00
if (args.size() == 2 && args.back() == "PARENT_SCOPE") {
2020-02-01 23:06:01 +01:00
status.GetMakefile().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
2019-11-11 23:01:05 +01:00
if (args.size() > 1 && args.back() == "PARENT_SCOPE") {
parentScope = true;
ignoreLastArgs++;
2016-07-09 11:21:54 +02:00
} else {
// look for FORCE argument
2019-11-11 23:01:05 +01:00
if (args.size() > 4 && args.back() == "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) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().RaiseScope(variable, value.c_str());
2014-08-03 19:52:23 +02:00
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
2019-11-11 23:01:05 +01:00
if ((args.back() == "CACHE") ||
2009-10-04 10:30:41 +03:00
(args.size() > 1 && args[args.size() - 2] == "CACHE") ||
2016-07-09 11:21:54 +02:00
(force && !cache)) {
2020-02-01 23:06:01 +01:00
status.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);
2020-08-30 11:54:41 +02:00
if (!cmState::StringToCacheEntryType(args[cacheStart + 1], type)) {
2019-11-11 23:01:05 +01:00
std::string m = "implicitly converting '" + args[cacheStart + 1] +
"' to 'STRING' type.";
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(MessageType::AUTHOR_WARNING, m);
2019-11-11 23:01:05 +01:00
// Setting this may not be required, since it's
// initialized as a string. Keeping this here to
// ensure that the type is actually converting to a string.
type = cmStateEnums::STRING;
}
2016-07-09 11:21:54 +02:00
docstring = args[cacheStart + 2].c_str();
}
// see if this is already in the cache
2020-02-01 23:06:01 +01:00
cmState* state = status.GetMakefile().GetState();
2021-11-20 13:41:27 +01:00
cmValue 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) {
2020-08-30 11:54:41 +02:00
status.GetMakefile().AddCacheDefinition(variable, value, docstring, type,
force);
2016-07-09 11:21:54 +02:00
} else {
// add the definition
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(variable, value);
2016-07-09 11:21:54 +02:00
}
return true;
}