cmSourceGroupCommand.cxx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <stddef.h>
  6. #include <utility>
  7. #include "cmAlgorithms.h"
  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 = cmStrCat("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(cmStrCat(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 cmContains(expectedOptions, argument);
  116. }
  117. void cmSourceGroupCommand::parseArguments(
  118. const std::vector<std::string>& args,
  119. cmSourceGroupCommand::ParsedArguments& parsedArguments)
  120. {
  121. const ExpectedOptions expectedOptions = getExpectedOptions();
  122. size_t i = 0;
  123. // at this point we know that args vector is not empty
  124. // if first argument is not one of expected options it's source group name
  125. if (!isExpectedOption(args[0], expectedOptions)) {
  126. // get source group name and go to next argument
  127. parsedArguments[kSourceGroupOptionName].push_back(args[0]);
  128. ++i;
  129. }
  130. for (; i < args.size();) {
  131. // get current option and increment index to go to next argument
  132. const std::string& currentOption = args[i++];
  133. // create current option entry in parsed arguments
  134. std::vector<std::string>& currentOptionArguments =
  135. parsedArguments[currentOption];
  136. // collect option arguments while we won't find another expected option
  137. while (i < args.size() && !isExpectedOption(args[i], expectedOptions)) {
  138. currentOptionArguments.push_back(args[i++]);
  139. }
  140. }
  141. }
  142. bool cmSourceGroupCommand::InitialPass(std::vector<std::string> const& args,
  143. cmExecutionStatus&)
  144. {
  145. if (args.empty()) {
  146. this->SetError("called with incorrect number of arguments");
  147. return false;
  148. }
  149. // If only two arguments are given, the pre-1.8 version of the
  150. // command is being invoked.
  151. if (args.size() == 2 && args[1] != "FILES") {
  152. cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
  153. if (!sg) {
  154. this->SetError("Could not create or find source group");
  155. return false;
  156. }
  157. sg->SetGroupRegex(args[1].c_str());
  158. return true;
  159. }
  160. ParsedArguments parsedArguments;
  161. std::string errorMsg;
  162. parseArguments(args, parsedArguments);
  163. if (!checkArgumentsPreconditions(parsedArguments, errorMsg)) {
  164. return false;
  165. }
  166. if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) {
  167. if (!processTree(parsedArguments, errorMsg)) {
  168. this->SetError(errorMsg);
  169. return false;
  170. }
  171. } else {
  172. if (parsedArguments.find(kSourceGroupOptionName) ==
  173. parsedArguments.end()) {
  174. this->SetError("Missing source group name.");
  175. return false;
  176. }
  177. cmSourceGroup* sg = this->Makefile->GetOrCreateSourceGroup(args[0]);
  178. if (!sg) {
  179. this->SetError("Could not create or find source group");
  180. return false;
  181. }
  182. // handle regex
  183. if (parsedArguments.find(kRegexOptionName) != parsedArguments.end()) {
  184. const std::string& sgRegex = parsedArguments[kRegexOptionName].front();
  185. sg->SetGroupRegex(sgRegex.c_str());
  186. }
  187. // handle files
  188. const std::vector<std::string>& filesArguments =
  189. parsedArguments[kFilesOptionName];
  190. for (auto const& filesArg : filesArguments) {
  191. std::string src = filesArg;
  192. if (!cmSystemTools::FileIsFullPath(src)) {
  193. src = this->Makefile->GetCurrentSourceDirectory();
  194. src += "/";
  195. src += filesArg;
  196. }
  197. src = cmSystemTools::CollapseFullPath(src);
  198. sg->AddGroupFile(src);
  199. }
  200. }
  201. return true;
  202. }
  203. bool cmSourceGroupCommand::checkArgumentsPreconditions(
  204. const ParsedArguments& parsedArguments, std::string& errorMsg) const
  205. {
  206. return checkSingleParameterArgumentPreconditions(
  207. kPrefixOptionName, parsedArguments, errorMsg) &&
  208. checkSingleParameterArgumentPreconditions(kTreeOptionName, parsedArguments,
  209. errorMsg) &&
  210. checkSingleParameterArgumentPreconditions(kRegexOptionName,
  211. parsedArguments, errorMsg);
  212. }
  213. bool cmSourceGroupCommand::processTree(ParsedArguments& parsedArguments,
  214. std::string& errorMsg)
  215. {
  216. const std::string root =
  217. cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front());
  218. std::string prefix = parsedArguments[kPrefixOptionName].empty()
  219. ? ""
  220. : parsedArguments[kPrefixOptionName].front();
  221. const std::vector<std::string> filesVector =
  222. prepareFilesPathsForTree(parsedArguments[kFilesOptionName],
  223. this->Makefile->GetCurrentSourceDirectory());
  224. if (!rootIsPrefix(root, filesVector, errorMsg)) {
  225. return false;
  226. }
  227. std::set<std::string> sourceGroupPaths =
  228. getSourceGroupFilesPaths(root, filesVector);
  229. return addFilesToItsSourceGroups(root, sourceGroupPaths, prefix,
  230. *(this->Makefile), errorMsg);
  231. }
  232. bool cmSourceGroupCommand::checkSingleParameterArgumentPreconditions(
  233. const std::string& argument, const ParsedArguments& parsedArguments,
  234. std::string& errorMsg) const
  235. {
  236. ParsedArguments::const_iterator foundArgument =
  237. parsedArguments.find(argument);
  238. if (foundArgument != parsedArguments.end()) {
  239. const std::vector<std::string>& optionArguments = foundArgument->second;
  240. if (optionArguments.empty()) {
  241. errorMsg = argument + " argument given without an argument.";
  242. return false;
  243. }
  244. if (optionArguments.size() > 1) {
  245. errorMsg = "too many arguments passed to " + argument + ".";
  246. return false;
  247. }
  248. }
  249. return true;
  250. }