cmSourceGroupCommand.cxx 8.9 KB

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