cmSourceGroupCommand.cxx 3.8 KB

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