cmSeparateArgumentsCommand.cxx 3.0 KB

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