/Source/cmSeparateArgumentsCommand.cxx

https://github.com/thewtex/CMake · C++ · 109 lines · 86 code · 6 blank · 17 comment · 29 complexity · 2fdf78f6aeec47b5cf16820076f10456 MD5 · raw file

  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmSeparateArgumentsCommand.h"
  11. // cmSeparateArgumentsCommand
  12. bool cmSeparateArgumentsCommand
  13. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  14. {
  15. if(args.empty())
  16. {
  17. this->SetError("must be given at least one argument.");
  18. return false;
  19. }
  20. std::string var;
  21. std::string command;
  22. enum Mode { ModeOld, ModeUnix, ModeWindows };
  23. Mode mode = ModeOld;
  24. enum Doing { DoingNone, DoingVariable, DoingMode, DoingCommand };
  25. Doing doing = DoingVariable;
  26. for(unsigned int i=0; i < args.size(); ++i)
  27. {
  28. if(doing == DoingVariable)
  29. {
  30. var = args[i];
  31. doing = DoingMode;
  32. }
  33. else if(doing == DoingMode && args[i] == "UNIX_COMMAND")
  34. {
  35. mode = ModeUnix;
  36. doing = DoingCommand;
  37. }
  38. else if(doing == DoingMode && args[i] == "WINDOWS_COMMAND")
  39. {
  40. mode = ModeWindows;
  41. doing = DoingCommand;
  42. }
  43. else if(doing == DoingCommand)
  44. {
  45. command = args[i];
  46. doing = DoingNone;
  47. }
  48. else
  49. {
  50. std::ostringstream e;
  51. e << "given unknown argument " << args[i];
  52. this->SetError(e.str());
  53. return false;
  54. }
  55. }
  56. if(mode == ModeOld)
  57. {
  58. // Original space-replacement version of command.
  59. if(const char* def = this->Makefile->GetDefinition(var))
  60. {
  61. std::string value = def;
  62. cmSystemTools::ReplaceString(value, " ", ";");
  63. this->Makefile->AddDefinition(var, value.c_str());
  64. }
  65. }
  66. else
  67. {
  68. // Parse the command line.
  69. std::vector<std::string> vec;
  70. if(mode == ModeUnix)
  71. {
  72. cmSystemTools::ParseUnixCommandLine(command.c_str(), vec);
  73. }
  74. else // if(mode == ModeWindows)
  75. {
  76. cmSystemTools::ParseWindowsCommandLine(command.c_str(), vec);
  77. }
  78. // Construct the result list value.
  79. std::string value;
  80. const char* sep = "";
  81. for(std::vector<std::string>::const_iterator vi = vec.begin();
  82. vi != vec.end(); ++vi)
  83. {
  84. // Separate from the previous argument.
  85. value += sep;
  86. sep = ";";
  87. // Preserve semicolons.
  88. for(std::string::const_iterator si = vi->begin();
  89. si != vi->end(); ++si)
  90. {
  91. if(*si == ';')
  92. {
  93. value += '\\';
  94. }
  95. value += *si;
  96. }
  97. }
  98. this->Makefile->AddDefinition(var, value.c_str());
  99. }
  100. return true;
  101. }