cmSeparateArgumentsCommand.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 (unsigned int i = 0; i < args.size(); ++i) {
  35. if (doing == DoingVariable) {
  36. var = args[i];
  37. doing = DoingMode;
  38. } else if (doing == DoingMode && args[i] == "NATIVE_COMMAND") {
  39. #ifdef _WIN32
  40. mode = ModeWindows;
  41. #else
  42. mode = ModeUnix;
  43. #endif
  44. doing = DoingCommand;
  45. } else if (doing == DoingMode && args[i] == "UNIX_COMMAND") {
  46. mode = ModeUnix;
  47. doing = DoingCommand;
  48. } else if (doing == DoingMode && args[i] == "WINDOWS_COMMAND") {
  49. mode = ModeWindows;
  50. doing = DoingCommand;
  51. } else if (doing == DoingCommand) {
  52. command = args[i];
  53. doing = DoingNone;
  54. } else {
  55. std::ostringstream e;
  56. e << "given unknown argument " << args[i];
  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::vector<std::string>::const_iterator vi = vec.begin();
  81. vi != vec.end(); ++vi) {
  82. // Separate from the previous argument.
  83. value += sep;
  84. sep = ";";
  85. // Preserve semicolons.
  86. for (std::string::const_iterator si = vi->begin(); si != vi->end();
  87. ++si) {
  88. if (*si == ';') {
  89. value += '\\';
  90. }
  91. value += *si;
  92. }
  93. }
  94. this->Makefile->AddDefinition(var, value.c_str());
  95. }
  96. return true;
  97. }