cmSeparateArgumentsCommand.cxx 2.5 KB

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