cmMathCommand.cxx 1.7 KB

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