cmSeparateArgumentsCommand.cxx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "cmSeparateArgumentsCommand.h"
  4. #include "cmSystemTools.h"
  5. // cmSeparateArgumentsCommand
  6. bool cmSeparateArgumentsCommand::InitialPass(
  7. std::vector<std::string> const& args, cmExecutionStatus&)
  8. {
  9. if (args.empty()) {
  10. this->SetError("must be given at least one argument.");
  11. return false;
  12. }
  13. std::string var;
  14. std::string command;
  15. enum Mode
  16. {
  17. ModeOld,
  18. ModeUnix,
  19. ModeWindows
  20. };
  21. Mode mode = ModeOld;
  22. enum Doing
  23. {
  24. DoingNone,
  25. DoingVariable,
  26. DoingMode,
  27. DoingCommand
  28. };
  29. Doing doing = DoingVariable;
  30. for (unsigned int i = 0; i < args.size(); ++i) {
  31. if (doing == DoingVariable) {
  32. var = args[i];
  33. doing = DoingMode;
  34. } else if (doing == DoingMode && args[i] == "UNIX_COMMAND") {
  35. mode = ModeUnix;
  36. doing = DoingCommand;
  37. } else if (doing == DoingMode && args[i] == "WINDOWS_COMMAND") {
  38. mode = ModeWindows;
  39. doing = DoingCommand;
  40. } else if (doing == DoingCommand) {
  41. command = args[i];
  42. doing = DoingNone;
  43. } else {
  44. std::ostringstream e;
  45. e << "given unknown argument " << args[i];
  46. this->SetError(e.str());
  47. return false;
  48. }
  49. }
  50. if (mode == ModeOld) {
  51. // Original space-replacement version of command.
  52. if (const char* def = this->Makefile->GetDefinition(var)) {
  53. std::string value = def;
  54. std::replace(value.begin(), value.end(), ' ', ';');
  55. this->Makefile->AddDefinition(var, value.c_str());
  56. }
  57. } else {
  58. // Parse the command line.
  59. std::vector<std::string> vec;
  60. if (mode == ModeUnix) {
  61. cmSystemTools::ParseUnixCommandLine(command.c_str(), vec);
  62. } else // if(mode == ModeWindows)
  63. {
  64. cmSystemTools::ParseWindowsCommandLine(command.c_str(), vec);
  65. }
  66. // Construct the result list value.
  67. std::string value;
  68. const char* sep = "";
  69. for (std::vector<std::string>::const_iterator vi = vec.begin();
  70. vi != vec.end(); ++vi) {
  71. // Separate from the previous argument.
  72. value += sep;
  73. sep = ";";
  74. // Preserve semicolons.
  75. for (std::string::const_iterator si = vi->begin(); si != vi->end();
  76. ++si) {
  77. if (*si == ';') {
  78. value += '\\';
  79. }
  80. value += *si;
  81. }
  82. }
  83. this->Makefile->AddDefinition(var, value.c_str());
  84. }
  85. return true;
  86. }