cmSourceGroupCommand.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "cmSourceGroupCommand.h"
  11. inline std::vector<std::string> tokenize(const std::string& str,
  12. const std::string& sep)
  13. {
  14. std::vector<std::string> tokens;
  15. std::string::size_type tokend = 0;
  16. do
  17. {
  18. std::string::size_type tokstart=str.find_first_not_of(sep, tokend);
  19. if (tokstart==std::string::npos)
  20. {
  21. break; // no more tokens
  22. }
  23. tokend=str.find_first_of(sep,tokstart);
  24. if (tokend==std::string::npos)
  25. {
  26. tokens.push_back(str.substr(tokstart));
  27. }
  28. else
  29. {
  30. tokens.push_back(str.substr(tokstart,tokend-tokstart));
  31. }
  32. } while (tokend!=std::string::npos);
  33. if (tokens.empty())
  34. {
  35. tokens.push_back("");
  36. }
  37. return tokens;
  38. }
  39. // cmSourceGroupCommand
  40. bool cmSourceGroupCommand
  41. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  42. {
  43. if(args.size() < 1)
  44. {
  45. this->SetError("called with incorrect number of arguments");
  46. return false;
  47. }
  48. std::string delimiter = "\\";
  49. if(this->Makefile->GetDefinition("SOURCE_GROUP_DELIMITER"))
  50. {
  51. delimiter = this->Makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
  52. }
  53. std::vector<std::string> folders = tokenize(args[0], delimiter);
  54. cmSourceGroup* sg = 0;
  55. sg = this->Makefile->GetSourceGroup(folders);
  56. if(!sg)
  57. {
  58. this->Makefile->AddSourceGroup(folders);
  59. sg = this->Makefile->GetSourceGroup(folders);
  60. }
  61. if(!sg)
  62. {
  63. this->SetError("Could not create or find source group");
  64. return false;
  65. }
  66. // If only two arguments are given, the pre-1.8 version of the
  67. // command is being invoked.
  68. if(args.size() == 2 && args[1] != "FILES")
  69. {
  70. sg->SetGroupRegex(args[1].c_str());
  71. return true;
  72. }
  73. // Process arguments.
  74. bool doingFiles = false;
  75. for(unsigned int i=1; i < args.size(); ++i)
  76. {
  77. if(args[i] == "REGULAR_EXPRESSION")
  78. {
  79. // Next argument must specify the regex.
  80. if(i+1 < args.size())
  81. {
  82. ++i;
  83. sg->SetGroupRegex(args[i].c_str());
  84. }
  85. else
  86. {
  87. this->SetError("REGULAR_EXPRESSION argument given without a regex.");
  88. return false;
  89. }
  90. doingFiles = false;
  91. }
  92. else if(args[i] == "FILES")
  93. {
  94. // Next arguments will specify files.
  95. doingFiles = true;
  96. }
  97. else if(doingFiles)
  98. {
  99. // Convert name to full path and add to the group's list.
  100. std::string src = args[i].c_str();
  101. if(!cmSystemTools::FileIsFullPath(src.c_str()))
  102. {
  103. src = this->Makefile->GetCurrentDirectory();
  104. src += "/";
  105. src += args[i];
  106. }
  107. src = cmSystemTools::CollapseFullPath(src.c_str());
  108. sg->AddGroupFile(src.c_str());
  109. }
  110. else
  111. {
  112. cmOStringStream err;
  113. err << "Unknown argument \"" << args[i].c_str() << "\". "
  114. << "Perhaps the FILES keyword is missing.\n";
  115. this->SetError(err.str().c_str());
  116. return false;
  117. }
  118. }
  119. return true;
  120. }