cmSourceGroupCommand.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "cmSourceGroupCommand.h"
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <set>
  7. #include <sstream>
  8. #include "cmMakefile.h"
  9. #include "cmSourceGroup.h"
  10. #include "cmSystemTools.h"
  11. namespace {
  12. const size_t RootIndex = 1;
  13. const size_t FilesWithoutPrefixKeywordIndex = 2;
  14. const size_t FilesWithPrefixKeywordIndex = 4;
  15. const size_t PrefixKeywordIdex = 2;
  16. std::vector<std::string> tokenizePath(const std::string& path)
  17. {
  18. return cmSystemTools::tokenize(path, "\\/");
  19. }
  20. std::string getFullFilePath(const std::string& currentPath,
  21. const std::string& path)
  22. {
  23. std::string fullPath = path;
  24. if (!cmSystemTools::FileIsFullPath(path.c_str())) {
  25. fullPath = currentPath;
  26. fullPath += "/";
  27. fullPath += path;
  28. }
  29. return cmSystemTools::CollapseFullPath(fullPath);
  30. }
  31. std::set<std::string> getSourceGroupFilesPaths(
  32. const std::string& root, const std::vector<std::string>& files)
  33. {
  34. std::set<std::string> ret;
  35. const std::string::size_type rootLength = root.length();
  36. for (size_t i = 0; i < files.size(); ++i) {
  37. ret.insert(files[i].substr(rootLength + 1)); // +1 to also omnit last '/'
  38. }
  39. return ret;
  40. }
  41. bool rootIsPrefix(const std::string& root,
  42. const std::vector<std::string>& files, std::string& error)
  43. {
  44. for (size_t i = 0; i < files.size(); ++i) {
  45. if (!cmSystemTools::StringStartsWith(files[i], root.c_str())) {
  46. error = "ROOT: " + root + " is not a prefix of file: " + files[i];
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. cmSourceGroup* addSourceGroup(const std::vector<std::string>& tokenizedPath,
  53. cmMakefile& makefile)
  54. {
  55. cmSourceGroup* sg;
  56. sg = makefile.GetSourceGroup(tokenizedPath);
  57. if (!sg) {
  58. makefile.AddSourceGroup(tokenizedPath);
  59. sg = makefile.GetSourceGroup(tokenizedPath);
  60. if (!sg) {
  61. return CM_NULLPTR;
  62. }
  63. }
  64. return sg;
  65. }
  66. std::string prepareFilePathForTree(const std::string& path)
  67. {
  68. return cmSystemTools::CollapseFullPath(path);
  69. }
  70. std::vector<std::string> prepareFilesPathsForTree(
  71. std::vector<std::string>::const_iterator begin,
  72. std::vector<std::string>::const_iterator end)
  73. {
  74. std::vector<std::string> prepared(std::distance(begin, end));
  75. std::transform(begin, end, prepared.begin(), prepareFilePathForTree);
  76. return prepared;
  77. }
  78. bool addFilesToItsSourceGroups(const std::string& root,
  79. const std::set<std::string>& sgFilesPaths,
  80. const std::string& prefix, cmMakefile& makefile,
  81. std::string& errorMsg)
  82. {
  83. cmSourceGroup* sg;
  84. for (std::set<std::string>::const_iterator it = sgFilesPaths.begin();
  85. it != sgFilesPaths.end(); ++it) {
  86. std::vector<std::string> tokenizedPath;
  87. if (!prefix.empty()) {
  88. tokenizedPath = tokenizePath(prefix + '/' + *it);
  89. } else {
  90. tokenizedPath = tokenizePath(*it);
  91. }
  92. if (tokenizedPath.size() > 1) {
  93. tokenizedPath.pop_back();
  94. sg = addSourceGroup(tokenizedPath, makefile);
  95. if (!sg) {
  96. errorMsg = "Could not create source group for file: " + *it;
  97. return false;
  98. }
  99. const std::string fullPath = getFullFilePath(root, *it);
  100. sg->AddGroupFile(fullPath);
  101. }
  102. }
  103. return true;
  104. }
  105. }
  106. class cmExecutionStatus;
  107. // cmSourceGroupCommand
  108. bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
  109. cmExecutionStatus&)
  110. {
  111. if (args.empty()) {
  112. this->SetError("called with incorrect number of arguments");
  113. return false;
  114. }
  115. if (args[0] == "TREE") {
  116. std::string error;
  117. if (!processTree(args, error)) {
  118. this->SetError(error);
  119. return false;
  120. }
  121. return true;
  122. }
  123. std::string delimiter = "\\";
  124. if (this->Makefile->GetDefinition("SOURCE_GROUP_DELIMITER")) {
  125. delimiter = this->Makefile->GetDefinition("SOURCE_GROUP_DELIMITER");
  126. }
  127. std::vector<std::string> folders =
  128. cmSystemTools::tokenize(args[0], delimiter);
  129. cmSourceGroup* sg = CM_NULLPTR;
  130. sg = this->Makefile->GetSourceGroup(folders);
  131. if (!sg) {
  132. this->Makefile->AddSourceGroup(folders);
  133. sg = this->Makefile->GetSourceGroup(folders);
  134. }
  135. if (!sg) {
  136. this->SetError("Could not create or find source group");
  137. return false;
  138. }
  139. // If only two arguments are given, the pre-1.8 version of the
  140. // command is being invoked.
  141. if (args.size() == 2 && args[1] != "FILES") {
  142. sg->SetGroupRegex(args[1].c_str());
  143. return true;
  144. }
  145. // Process arguments.
  146. bool doingFiles = false;
  147. for (unsigned int i = 1; i < args.size(); ++i) {
  148. if (args[i] == "REGULAR_EXPRESSION") {
  149. // Next argument must specify the regex.
  150. if (i + 1 < args.size()) {
  151. ++i;
  152. sg->SetGroupRegex(args[i].c_str());
  153. } else {
  154. this->SetError("REGULAR_EXPRESSION argument given without a regex.");
  155. return false;
  156. }
  157. doingFiles = false;
  158. } else if (args[i] == "FILES") {
  159. // Next arguments will specify files.
  160. doingFiles = true;
  161. } else if (doingFiles) {
  162. // Convert name to full path and add to the group's list.
  163. std::string src = args[i];
  164. if (!cmSystemTools::FileIsFullPath(src.c_str())) {
  165. src = this->Makefile->GetCurrentSourceDirectory();
  166. src += "/";
  167. src += args[i];
  168. }
  169. src = cmSystemTools::CollapseFullPath(src);
  170. sg->AddGroupFile(src);
  171. } else {
  172. std::ostringstream err;
  173. err << "Unknown argument \"" << args[i] << "\". "
  174. << "Perhaps the FILES keyword is missing.\n";
  175. this->SetError(err.str());
  176. return false;
  177. }
  178. }
  179. return true;
  180. }
  181. bool cmSourceGroupCommand::checkTreeArgumentsPreconditions(
  182. const std::vector<std::string>& args, std::string& errorMsg) const
  183. {
  184. if (args.size() == 1) {
  185. errorMsg = "TREE argument given without a root.";
  186. return false;
  187. }
  188. if (args.size() < 3) {
  189. errorMsg = "Missing FILES arguments.";
  190. return false;
  191. }
  192. if (args[FilesWithoutPrefixKeywordIndex] != "FILES" &&
  193. args[PrefixKeywordIdex] != "PREFIX") {
  194. errorMsg = "Unknown argument \"" + args[2] +
  195. "\". Perhaps the FILES keyword is missing.\n";
  196. return false;
  197. }
  198. if (args[PrefixKeywordIdex] == "PREFIX" &&
  199. (args.size() < 5 || args[FilesWithPrefixKeywordIndex] != "FILES")) {
  200. errorMsg = "Missing FILES arguments.";
  201. return false;
  202. }
  203. return true;
  204. }
  205. bool cmSourceGroupCommand::processTree(const std::vector<std::string>& args,
  206. std::string& errorMsg)
  207. {
  208. if (!checkTreeArgumentsPreconditions(args, errorMsg)) {
  209. return false;
  210. }
  211. const std::string root = cmSystemTools::CollapseFullPath(args[RootIndex]);
  212. std::string prefix;
  213. size_t filesBegin = FilesWithoutPrefixKeywordIndex + 1;
  214. if (args[PrefixKeywordIdex] == "PREFIX") {
  215. prefix = args[PrefixKeywordIdex + 1];
  216. filesBegin = FilesWithPrefixKeywordIndex + 1;
  217. }
  218. const std::vector<std::string> filesVector =
  219. prepareFilesPathsForTree(args.begin() + filesBegin, args.end());
  220. if (!rootIsPrefix(root, filesVector, errorMsg)) {
  221. return false;
  222. }
  223. std::set<std::string> sourceGroupPaths =
  224. getSourceGroupFilesPaths(root, filesVector);
  225. if (!addFilesToItsSourceGroups(root, sourceGroupPaths, prefix,
  226. *(this->Makefile), errorMsg)) {
  227. return false;
  228. }
  229. return true;
  230. }