cmSeparateArgumentsCommand.cxx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. // cmSeparateArgumentsCommand
  10. bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
  11. cmExecutionStatus& status)
  12. {
  13. if (args.empty()) {
  14. status.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. status.SetError(cmStrCat("given unknown argument ", arg));
  56. return false;
  57. }
  58. }
  59. if (mode == ModeOld) {
  60. // Original space-replacement version of command.
  61. if (const char* def = status.GetMakefile().GetDefinition(var)) {
  62. std::string value = def;
  63. std::replace(value.begin(), value.end(), ' ', ';');
  64. status.GetMakefile().AddDefinition(var, value);
  65. }
  66. } else {
  67. // Parse the command line.
  68. std::vector<std::string> vec;
  69. if (mode == ModeUnix) {
  70. cmSystemTools::ParseUnixCommandLine(command.c_str(), vec);
  71. } else // if(mode == ModeWindows)
  72. {
  73. cmSystemTools::ParseWindowsCommandLine(command.c_str(), vec);
  74. }
  75. // Construct the result list value.
  76. std::string value;
  77. const char* sep = "";
  78. for (std::string const& vi : vec) {
  79. // Separate from the previous argument.
  80. value += sep;
  81. sep = ";";
  82. // Preserve semicolons.
  83. for (char si : vi) {
  84. if (si == ';') {
  85. value += '\\';
  86. }
  87. value += si;
  88. }
  89. }
  90. status.GetMakefile().AddDefinition(var, value);
  91. }
  92. return true;
  93. }