cmSourceGroupCommand.cxx 6.9 KB

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