cmSeparateArgumentsCommand.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <cmext/string_view>
  6. #include "cmArgumentParser.h"
  7. #include "cmExecutionStatus.h"
  8. #include "cmMakefile.h"
  9. #include "cmProperty.h"
  10. #include "cmRange.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. // cmSeparateArgumentsCommand
  14. bool cmSeparateArgumentsCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. if (args.empty()) {
  18. status.SetError("must be given at least one argument.");
  19. return false;
  20. }
  21. std::string const& var = args.front();
  22. if (args.size() == 1) {
  23. // Original space-replacement version of command.
  24. if (cmProp def = status.GetMakefile().GetDefinition(var)) {
  25. std::string value = *def;
  26. std::replace(value.begin(), value.end(), ' ', ';');
  27. status.GetMakefile().AddDefinition(var, value);
  28. }
  29. return true;
  30. }
  31. struct Arguments
  32. {
  33. bool UnixCommand = false;
  34. bool WindowsCommand = false;
  35. bool NativeCommand = false;
  36. };
  37. static auto const parser =
  38. cmArgumentParser<Arguments>{}
  39. .Bind("UNIX_COMMAND"_s, &Arguments::UnixCommand)
  40. .Bind("WINDOWS_COMMAND"_s, &Arguments::WindowsCommand)
  41. .Bind("NATIVE_COMMAND"_s, &Arguments::NativeCommand);
  42. std::vector<std::string> unparsedArguments;
  43. Arguments arguments =
  44. parser.Parse(cmMakeRange(args).advance(1), &unparsedArguments);
  45. if (!arguments.UnixCommand && !arguments.WindowsCommand &&
  46. !arguments.NativeCommand) {
  47. status.SetError("missing required option: 'UNIX_COMMAND' or "
  48. "'WINDOWS_COMMAND' or 'NATIVE_COMMAND'");
  49. return false;
  50. }
  51. if ((arguments.UnixCommand && arguments.WindowsCommand) ||
  52. (arguments.UnixCommand && arguments.NativeCommand) ||
  53. (arguments.WindowsCommand && arguments.NativeCommand)) {
  54. status.SetError("'UNIX_COMMAND', 'WINDOWS_COMMAND' and 'NATIVE_COMMAND' "
  55. "are mutually exclusive");
  56. return false;
  57. }
  58. if (unparsedArguments.size() > 1) {
  59. status.SetError("given unexpected argument(s)");
  60. return false;
  61. }
  62. std::string& command = unparsedArguments.front();
  63. if (command.empty()) {
  64. status.GetMakefile().AddDefinition(var, command);
  65. return true;
  66. }
  67. // split command given
  68. std::vector<std::string> values;
  69. if (arguments.NativeCommand) {
  70. #if defined(_WIN32)
  71. arguments.WindowsCommand = true;
  72. #else
  73. arguments.UnixCommand = true;
  74. #endif
  75. }
  76. if (arguments.UnixCommand) {
  77. cmSystemTools::ParseUnixCommandLine(command.c_str(), values);
  78. } else {
  79. cmSystemTools::ParseWindowsCommandLine(command.c_str(), values);
  80. }
  81. // preserve semicolons in arguments
  82. std::for_each(values.begin(), values.end(), [](std::string& value) {
  83. std::string::size_type pos = 0;
  84. while ((pos = value.find_first_of(';', pos)) != std::string::npos) {
  85. value.insert(pos, 1, '\\');
  86. pos += 2;
  87. }
  88. });
  89. auto value = cmJoin(values, ";");
  90. status.GetMakefile().AddDefinition(var, value);
  91. return true;
  92. }