cmSourceGroupCommand.cxx 8.7 KB

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