1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmMathCommand.h"
14 #include "cmExprParserHelper.h"
16 //----------------------------------------------------------------------------
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
20 if ( args.size() < 1 )
22 this->SetError("must be called with at least one argument.");
25 const std::string &subCommand = args[0];
26 if(subCommand == "EXPR")
28 return this->HandleExprCommand(args);
30 std::string e = "does not recognize sub-command "+subCommand;
31 this->SetError(e.c_str());
35 //----------------------------------------------------------------------------
36 bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args)
38 if ( args.size() != 3 )
40 this->SetError("EXPR called with incorrect arguments.");
44 const std::string& outputVariable = args[1];
45 const std::string& expression = args[2];
47 cmExprParserHelper helper;
48 if ( !helper.ParseString(expression.c_str(), 0) )
50 std::string e = "cannot parse the expression: \""+expression+"\": ";
51 e += helper.GetError();
52 this->SetError(e.c_str());
57 sprintf(buffer, "%d", helper.GetResult());
59 this->Makefile->AddDefinition(outputVariable.c_str(), buffer);