cmSourceGroupCommand.cxx 7.4 KB

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