cmMathCommand.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "cmMakefile.h"
  6. #include "cm_kwiml.h"
  7. #include <stdio.h>
  8. class cmExecutionStatus;
  9. bool cmMathCommand::InitialPass(std::vector<std::string> const& args,
  10. cmExecutionStatus&)
  11. {
  12. if (args.empty()) {
  13. this->SetError("must be called with at least one argument.");
  14. return false;
  15. }
  16. const std::string& subCommand = args[0];
  17. if (subCommand == "EXPR") {
  18. return this->HandleExprCommand(args);
  19. }
  20. std::string e = "does not recognize sub-command " + subCommand;
  21. this->SetError(e);
  22. return false;
  23. }
  24. bool cmMathCommand::HandleExprCommand(std::vector<std::string> const& args)
  25. {
  26. if (args.size() != 3) {
  27. this->SetError("EXPR called with incorrect arguments.");
  28. return false;
  29. }
  30. const std::string& outputVariable = args[1];
  31. const std::string& expression = args[2];
  32. cmExprParserHelper helper;
  33. if (!helper.ParseString(expression.c_str(), 0)) {
  34. std::string e = "cannot parse the expression: \"" + expression + "\": ";
  35. e += helper.GetError();
  36. this->SetError(e);
  37. return false;
  38. }
  39. char buffer[1024];
  40. sprintf(buffer, "%" KWIML_INT_PRId64, helper.GetResult());
  41. this->Makefile->AddDefinition(outputVariable, buffer);
  42. return true;
  43. }