cmSourceGroupCommand.cxx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <set>
  6. #include <stddef.h>
  7. #include <utility>
  8. #include "cmMakefile.h"
  9. #include "cmSourceGroup.h"
  10. #include "cmSystemTools.h"
  11. namespace {
  12. const std::string kTreeOptionName = "TREE";
  13. const std::string kPrefixOptionName = "PREFIX";
  14. const std::string kFilesOptionName = "FILES";
  15. const std::string kRegexOptionName = "REGULAR_EXPRESSION";
  16. const std::string kSourceGroupOptionName = "<sg_name>";
  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)) {
  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 (std::string const& file : files) {
  38. ret.insert(file.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 (std::string const& file : files) {
  46. if (!cmSystemTools::StringStartsWith(file, root.c_str())) {
  47. error = "ROOT: " + root + " is not a prefix of file: " + file;
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. std::string prepareFilePathForTree(const std::string& path,
  54. const std::string& currentSourceDir)
  55. {
  56. if (!cmSystemTools::FileIsFullPath(path)) {
  57. return cmSystemTools::CollapseFullPath(currentSourceDir + "/" + path);
  58. }
  59. return cmSystemTools::CollapseFullPath(path);
  60. }
  61. std::vector<std::string> prepareFilesPathsForTree(
  62. const std::vector<std::string>& filesPaths,
  63. const std::string& currentSourceDir)
  64. {
  65. std::vector<std::string> prepared;
  66. for (auto const& filePath : filesPaths) {
  67. prepared.push_back(prepareFilePathForTree(filePath, currentSourceDir));
  68. }
  69. return prepared;
  70. }
  71. bool addFilesToItsSourceGroups(const std::string& root,
  72. const std::set<std::string>& sgFilesPaths,
  73. const std::string& prefix, cmMakefile& makefile,
  74. std::string& errorMsg)
  75. {
  76. cmSourceGroup* sg;
  77. for (std::string const& sgFilesPath : sgFilesPaths) {
  78. std::vector<std::string> tokenizedPath;
  79. if (!prefix.empty()) {
  80. tokenizedPath = tokenizePath(prefix + '/' + sgFilesPath);
  81. } else {
  82. tokenizedPath = tokenizePath(sgFilesPath);
  83. }
  84. if (!tokenizedPath.empty()) {
  85. tokenizedPath.pop_back();
  86. if (tokenizedPath.empty()) {
  87. tokenizedPath.push_back("");
  88. }
  89. sg = makefile.GetOrCreateSourceGroup(tokenizedPath);
  90. if (!sg) {
  91. errorMsg = "Could not create source group for file: " + sgFilesPath;
  92. return false;
  93. }
  94. const std::string fullPath = getFullFilePath(root, sgFilesPath);
  95. sg->AddGroupFile(fullPath);
  96. }
  97. }
  98. return true;
  99. }
  100. }
  101. class cmExecutionStatus;
  102. // cmSourceGroupCommand
  103. cmSourceGroupCommand::ExpectedOptions
  104. cmSourceGroupCommand::getExpectedOptions() const
  105. {
  106. ExpectedOptions options;
  107. options.push_back(kTreeOptionName);
  108. options.push_back(kPrefixOptionName);
  109. options.push_back(kFilesOptionName);
  110. options.push_back(kRegexOptionName);
  111. return options;
  112. }
  113. bool cmSourceGroupCommand::isExpectedOption(
  114. const std::string& argument, const ExpectedOptions& expectedOptions)
  115. {
  116. return std::find(expectedOptions.begin(), expectedOptions.end(), argument) !=
  117. expectedOptions.end();
  118. }
  119. void cmSourceGroupCommand::parseArguments(
  120. const std::vector<std::string>& args,
  121. cmSourceGroupCommand::ParsedArguments& parsedArguments)
  122. {
  123. const ExpectedOptions expectedOptions = getExpectedOptions();
  124. size_t i = 0;
  125. // at this point we know that args vector is not empty
  126. // if first argument is not one of expected options it's source group name
  127. if (!isExpectedOption(args[0], expectedOptions)) {
  128. // get source group name and go to next argument
  129. parsedArguments[kSourceGroupOptionName].push_back(args[0]);
  130. ++i;
  131. }
  132. for (; i < args.size();) {
  133. // get current option and increment index to go to next argument
  134. const std::string& currentOption = args[i++];
  135. // create current option entry in parsed arguments
  136. std::vector<std::string>& currentOptionArguments =
  137. parsedArguments[currentOption];
  138. // collect option arguments while we won't find another expected option
  139. while (i < args.size() && !isExpectedOption(args[i], expectedOptions)) {
  140. currentOptionArguments.push_back(args[i++]);
  141. }
  142. }
  143. }
  144. bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
  145. cmExecutionStatus&)
  146. {
  147. if (args.empty()) {
  148. this->SetError("called with incorrect number of arguments");
  149. return false;
  150. }
  151. // If only two arguments are given, the pre-1.8 version of the
  152. // command is being invoked.
  153. if (args.size() == 2 && args[1] != "FILES") {
  154. cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
  155. if (!sg) {
  156. this->SetError("Could not create or find source group");
  157. return false;
  158. }
  159. sg->SetGroupRegex(args[1].c_str());
  160. return true;
  161. }
  162. ParsedArguments parsedArguments;
  163. std::string errorMsg;
  164. parseArguments(args, parsedArguments);
  165. if (!checkArgumentsPreconditions(parsedArguments, errorMsg)) {
  166. return false;
  167. }
  168. if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) {
  169. if (!processTree(parsedArguments, errorMsg)) {
  170. this->SetError(errorMsg);
  171. return false;
  172. }
  173. } else {
  174. if (parsedArguments.find(kSourceGroupOptionName) ==
  175. parsedArguments.end()) {
  176. this->SetError("Missing source group name.");
  177. return false;
  178. }
  179. cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
  180. if (!sg) {
  181. this->SetError("Could not create or find source group");
  182. return false;
  183. }
  184. // handle regex
  185. if (parsedArguments.find(kRegexOptionName) != parsedArguments.end()) {
  186. const std::string& sgRegex = parsedArguments[kRegexOptionName].front();
  187. sg->SetGroupRegex(sgRegex.c_str());
  188. }
  189. // handle files
  190. const std::vector<std::string>& filesArguments =
  191. parsedArguments[kFilesOptionName];
  192. for (auto const& filesArg : filesArguments) {
  193. std::string src = filesArg;
  194. if (!cmSystemTools::FileIsFullPath(src)) {
  195. src = this->Makefile->GetCurrentSourceDirectory();
  196. src += "/";
  197. src += filesArg;
  198. }
  199. src = cmSystemTools::CollapseFullPath(src);
  200. sg->AddGroupFile(src);
  201. }
  202. }
  203. return true;
  204. }
  205. bool cmSourceGroupCommand::checkArgumentsPreconditions(
  206. const ParsedArguments& parsedArguments, std::string& errorMsg) const
  207. {
  208. if (!checkSingleParameterArgumentPreconditions(kPrefixOptionName,
  209. parsedArguments, errorMsg) ||
  210. !checkSingleParameterArgumentPreconditions(kTreeOptionName,
  211. parsedArguments, errorMsg) ||
  212. !checkSingleParameterArgumentPreconditions(kRegexOptionName,
  213. parsedArguments, errorMsg)) {
  214. return false;
  215. }
  216. return true;
  217. }
  218. bool cmSourceGroupCommand::processTree(ParsedArguments& parsedArguments,
  219. std::string& errorMsg)
  220. {
  221. const std::string root =
  222. cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front());
  223. std::string prefix = parsedArguments[kPrefixOptionName].empty()
  224. ? ""
  225. : parsedArguments[kPrefixOptionName].front();
  226. const std::vector<std::string> filesVector =
  227. prepareFilesPathsForTree(parsedArguments[kFilesOptionName],
  228. this->Makefile->GetCurrentSourceDirectory());
  229. if (!rootIsPrefix(root, filesVector, errorMsg)) {
  230. return false;
  231. }
  232. std::set<std::string> sourceGroupPaths =
  233. getSourceGroupFilesPaths(root, filesVector);
  234. if (!addFilesToItsSourceGroups(root, sourceGroupPaths, prefix,
  235. *(this->Makefile), errorMsg)) {
  236. return false;
  237. }
  238. return true;
  239. }
  240. bool cmSourceGroupCommand::checkSingleParameterArgumentPreconditions(
  241. const std::string& argument, const ParsedArguments& parsedArguments,
  242. std::string& errorMsg) const
  243. {
  244. ParsedArguments::const_iterator foundArgument =
  245. parsedArguments.find(argument);
  246. if (foundArgument != parsedArguments.end()) {
  247. const std::vector<std::string>& optionArguments = foundArgument->second;
  248. if (optionArguments.empty()) {
  249. errorMsg = argument + " argument given without an argument.";
  250. return false;
  251. }
  252. if (optionArguments.size() > 1) {
  253. errorMsg = "too many arguments passed to " + argument + ".";
  254. return false;
  255. }
  256. }
  257. return true;
  258. }