cmMathCommand.cxx 1.4 KB

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