cmSourceGroupCommand.cxx 7.6 KB

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