cmSourceGroupCommand.cxx 8.8 KB

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