cmake/Source/cmOptionCommand.cxx

87 lines
2.8 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 "cmOptionCommand.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"
2018-10-28 12:09:07 +01:00
#include "cmPolicies.h"
2020-08-30 11:54:41 +02:00
#include "cmProperty.h"
2017-04-14 19:02:05 +02:00
#include "cmState.h"
2018-10-28 12:09:07 +01:00
#include "cmStateSnapshot.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
// cmOptionCommand
2020-02-01 23:06:01 +01:00
bool cmOptionCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2018-10-28 12:09:07 +01:00
const bool argError = (args.size() < 2) || (args.size() > 3);
2016-07-09 11:21:54 +02:00
if (argError) {
2020-02-01 23:06:01 +01:00
std::string m = cmStrCat("called with incorrect number of arguments: ",
cmJoin(args, " "));
status.SetError(m);
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2018-10-28 12:09:07 +01:00
// Determine the state of the option policy
bool checkAndWarn = false;
{
2020-02-01 23:06:01 +01:00
auto policyStatus =
status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0077);
2018-10-28 12:09:07 +01:00
const auto* existsBeforeSet =
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetStateSnapshot().GetDefinition(args[0]);
switch (policyStatus) {
2018-10-28 12:09:07 +01:00
case cmPolicies::WARN:
checkAndWarn = (existsBeforeSet != nullptr);
break;
case cmPolicies::OLD:
// OLD behavior does not warn.
break;
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::NEW: {
// See if a local variable with this name already exists.
// If so we ignore the option command.
if (existsBeforeSet) {
return true;
}
} break;
}
}
// See if a cache variable with this name already exists
// If so just make sure the doc state is correct
2020-02-01 23:06:01 +01:00
cmState* state = status.GetMakefile().GetState();
2020-08-30 11:54:41 +02:00
cmProp existingValue = state->GetCacheEntryValue(args[0]);
2018-10-28 12:09:07 +01:00
if (existingValue &&
(state->GetCacheEntryType(args[0]) != cmStateEnums::UNINITIALIZED)) {
state->SetCacheEntryProperty(args[0], "HELPSTRING", args[1]);
return true;
2016-07-09 11:21:54 +02:00
}
2018-10-28 12:09:07 +01:00
// Nothing in the cache so add it
2020-08-30 11:54:41 +02:00
std::string initialValue = existingValue ? *existingValue : "Off";
2016-07-09 11:21:54 +02:00
if (args.size() == 3) {
initialValue = args[2];
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
bool init = cmIsOn(initialValue);
status.GetMakefile().AddCacheDefinition(args[0], init ? "ON" : "OFF",
args[1].c_str(), cmStateEnums::BOOL);
2018-10-28 12:09:07 +01:00
if (checkAndWarn) {
const auto* existsAfterSet =
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetStateSnapshot().GetDefinition(args[0]);
2018-10-28 12:09:07 +01:00
if (!existsAfterSet) {
2020-02-01 23:06:01 +01:00
status.GetMakefile().IssueMessage(
MessageType::AUTHOR_WARNING,
cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0077),
"\n"
"For compatibility with older versions of CMake, option "
"is clearing the normal variable '",
args[0], "'."));
2018-10-28 12:09:07 +01:00
}
}
return true;
}