cmMathCommand.cxx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmMathCommand.h"
  4. #include "cmExprParserHelper.h"
  5. bool cmMathCommand::InitialPass(std::vector<std::string> const& args,
  6. cmExecutionStatus&)
  7. {
  8. if (args.empty()) {
  9. this->SetError("must be called with at least one argument.");
  10. return false;
  11. }
  12. const std::string& subCommand = args[0];
  13. if (subCommand == "EXPR") {
  14. return this->HandleExprCommand(args);
  15. }
  16. std::string e = "does not recognize sub-command " + subCommand;
  17. this->SetError(e);
  18. return false;
  19. }
  20. bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args)
  21. {
  22. if (args.size() != 3) {
  23. this->SetError("EXPR called with incorrect arguments.");
  24. return false;
  25. }
  26. const std::string& outputVariable = args[1];
  27. const std::string& expression = args[2];
  28. cmExprParserHelper helper;
  29. if (!helper.ParseString(expression.c_str(), 0)) {
  30. std::string e = "cannot parse the expression: \"" + expression + "\": ";
  31. e += helper.GetError();
  32. this->SetError(e);
  33. return false;
  34. }
  35. char buffer[1024];
  36. sprintf(buffer, "%d", helper.GetResult());
  37. this->Makefile->AddDefinition(outputVariable, buffer);
  38. return true;
  39. }