1
0

cmSourceGroupCommand.cxx 7.6 KB

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