cmake/Source/cmMathCommand.cxx

52 lines
1.4 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 "cmMathCommand.h"
2017-04-14 19:02:05 +02:00
#include <stdio.h>
#include "cmExprParserHelper.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
class cmExecutionStatus;
2016-07-09 11:21:54 +02:00
bool cmMathCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
this->SetError("must be called with at least one argument.");
return false;
2016-07-09 11:21:54 +02:00
}
const std::string& subCommand = args[0];
if (subCommand == "EXPR") {
return this->HandleExprCommand(args);
2016-07-09 11:21:54 +02:00
}
std::string e = "does not recognize sub-command " + subCommand;
2015-04-27 22:25:09 +02:00
this->SetError(e);
return false;
}
bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args)
{
2016-07-09 11:21:54 +02:00
if (args.size() != 3) {
this->SetError("EXPR called with incorrect arguments.");
return false;
2016-07-09 11:21:54 +02:00
}
const std::string& outputVariable = args[1];
const std::string& expression = args[2];
2013-03-16 19:13:01 +02:00
cmExprParserHelper helper;
2016-07-09 11:21:54 +02:00
if (!helper.ParseString(expression.c_str(), 0)) {
std::string e = "cannot parse the expression: \"" + expression + "\": ";
e += helper.GetError();
2015-04-27 22:25:09 +02:00
this->SetError(e);
return false;
2016-07-09 11:21:54 +02:00
}
char buffer[1024];
sprintf(buffer, "%d", helper.GetResult());
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(outputVariable, buffer);
return true;
}