cmSourceGroupCommand.cxx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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.size() > 1) {
  85. tokenizedPath.pop_back();
  86. sg = makefile.GetOrCreateSourceGroup(tokenizedPath);
  87. if (!sg) {
  88. errorMsg = "Could not create source group for file: " + sgFilesPath;
  89. return false;
  90. }
  91. const std::string fullPath = getFullFilePath(root, sgFilesPath);
  92. sg->AddGroupFile(fullPath);
  93. }
  94. }
  95. return true;
  96. }
  97. }
  98. class cmExecutionStatus;
  99. // cmSourceGroupCommand
  100. cmSourceGroupCommand::ExpectedOptions
  101. cmSourceGroupCommand::getExpectedOptions() const
  102. {
  103. ExpectedOptions options;
  104. options.push_back(kTreeOptionName);
  105. options.push_back(kPrefixOptionName);
  106. options.push_back(kFilesOptionName);
  107. options.push_back(kRegexOptionName);
  108. return options;
  109. }
  110. bool cmSourceGroupCommand::isExpectedOption(
  111. const std::string& argument, const ExpectedOptions& expectedOptions)
  112. {
  113. return std::find(expectedOptions.begin(), expectedOptions.end(), argument) !=
  114. expectedOptions.end();
  115. }
  116. void cmSourceGroupCommand::parseArguments(
  117. const std::vector<std::string>& args,
  118. cmSourceGroupCommand::ParsedArguments& parsedArguments)
  119. {
  120. const ExpectedOptions expectedOptions = getExpectedOptions();
  121. size_t i = 0;
  122. // at this point we know that args vector is not empty
  123. // if first argument is not one of expected options it's source group name
  124. if (!isExpectedOption(args[0], expectedOptions)) {
  125. // get source group name and go to next argument
  126. parsedArguments[kSourceGroupOptionName].push_back(args[0]);
  127. ++i;
  128. }
  129. for (; i < args.size();) {
  130. // get current option and increment index to go to next argument
  131. const std::string& currentOption = args[i++];
  132. // create current option entry in parsed arguments
  133. std::vector<std::string>& currentOptionArguments =
  134. parsedArguments[currentOption];
  135. // collect option arguments while we won't find another expected option
  136. while (i < args.size() && !isExpectedOption(args[i], expectedOptions)) {
  137. currentOptionArguments.push_back(args[i++]);
  138. }
  139. }
  140. }
  141. bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
  142. cmExecutionStatus&)
  143. {
  144. if (args.empty()) {
  145. this->SetError("called with incorrect number of arguments");
  146. return false;
  147. }
  148. // If only two arguments are given, the pre-1.8 version of the
  149. // command is being invoked.
  150. if (args.size() == 2 && args[1] != "FILES") {
  151. cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
  152. if (!sg) {
  153. this->SetError("Could not create or find source group");
  154. return false;
  155. }
  156. sg->SetGroupRegex(args[1].c_str());
  157. return true;
  158. }
  159. ParsedArguments parsedArguments;
  160. std::string errorMsg;
  161. parseArguments(args, parsedArguments);
  162. if (!checkArgumentsPreconditions(parsedArguments, errorMsg)) {
  163. return false;
  164. }
  165. if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) {
  166. if (!processTree(parsedArguments, errorMsg)) {
  167. this->SetError(errorMsg);
  168. return false;
  169. }
  170. } else {
  171. if (parsedArguments.find(kSourceGroupOptionName) ==
  172. parsedArguments.end()) {
  173. this->SetError("Missing source group name.");
  174. return false;
  175. }
  176. cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
  177. if (!sg) {
  178. this->SetError("Could not create or find source group");
  179. return false;
  180. }
  181. // handle regex
  182. if (parsedArguments.find(kRegexOptionName) != parsedArguments.end()) {
  183. const std::string& sgRegex = parsedArguments[kRegexOptionName].front();
  184. sg->SetGroupRegex(sgRegex.c_str());
  185. }
  186. // handle files
  187. const std::vector<std::string>& filesArguments =
  188. parsedArguments[kFilesOptionName];
  189. for (auto const& filesArg : filesArguments) {
  190. std::string src = filesArg;
  191. if (!cmSystemTools::FileIsFullPath(src)) {
  192. src = this->Makefile->GetCurrentSourceDirectory();
  193. src += "/";
  194. src += filesArg;
  195. }
  196. src = cmSystemTools::CollapseFullPath(src);
  197. sg->AddGroupFile(src);
  198. }
  199. }
  200. return true;
  201. }
  202. bool cmSourceGroupCommand::checkArgumentsPreconditions(
  203. const ParsedArguments& parsedArguments, std::string& errorMsg) const
  204. {
  205. if (!checkSingleParameterArgumentPreconditions(kPrefixOptionName,
  206. parsedArguments, errorMsg) ||
  207. !checkSingleParameterArgumentPreconditions(kTreeOptionName,
  208. parsedArguments, errorMsg) ||
  209. !checkSingleParameterArgumentPreconditions(kRegexOptionName,
  210. parsedArguments, errorMsg)) {
  211. return false;
  212. }
  213. return true;
  214. }
  215. bool cmSourceGroupCommand::processTree(ParsedArguments& parsedArguments,
  216. std::string& errorMsg)
  217. {
  218. const std::string root =
  219. cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front());
  220. std::string prefix = parsedArguments[kPrefixOptionName].empty()
  221. ? ""
  222. : parsedArguments[kPrefixOptionName].front();
  223. const std::vector<std::string> filesVector =
  224. prepareFilesPathsForTree(parsedArguments[kFilesOptionName],
  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. }
  237. bool cmSourceGroupCommand::checkSingleParameterArgumentPreconditions(
  238. const std::string& argument, const ParsedArguments& parsedArguments,
  239. std::string& errorMsg) const
  240. {
  241. ParsedArguments::const_iterator foundArgument =
  242. parsedArguments.find(argument);
  243. if (foundArgument != parsedArguments.end()) {
  244. const std::vector<std::string>& optionArguments = foundArgument->second;
  245. if (optionArguments.empty()) {
  246. errorMsg = argument + " argument given without an argument.";
  247. return false;
  248. }
  249. if (optionArguments.size() > 1) {
  250. errorMsg = "too many arguments passed to " + argument + ".";
  251. return false;
  252. }
  253. }
  254. return true;
  255. }