cmSourceGroupCommand.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <memory>
  7. #include <set>
  8. #include <utility>
  9. #include <cmext/algorithm>
  10. #include "cmExecutionStatus.h"
  11. #include "cmMakefile.h"
  12. #include "cmSourceFile.h"
  13. #include "cmSourceFileLocation.h"
  14. #include "cmSourceGroup.h"
  15. #include "cmStringAlgorithms.h"
  16. #include "cmSystemTools.h"
  17. namespace {
  18. using ParsedArguments = std::map<std::string, std::vector<std::string>>;
  19. using ExpectedOptions = std::vector<std::string>;
  20. const std::string kTreeOptionName = "TREE";
  21. const std::string kPrefixOptionName = "PREFIX";
  22. const std::string kFilesOptionName = "FILES";
  23. const std::string kRegexOptionName = "REGULAR_EXPRESSION";
  24. const std::string kSourceGroupOptionName = "<sg_name>";
  25. std::vector<std::string> tokenizePath(const std::string& path)
  26. {
  27. return cmTokenize(path, "\\/");
  28. }
  29. std::set<std::string> getSourceGroupFilesPaths(
  30. const std::string& root, const std::vector<std::string>& files)
  31. {
  32. std::set<std::string> ret;
  33. const std::string::size_type rootLength = root.length();
  34. for (std::string const& file : files) {
  35. ret.insert(file.substr(rootLength + 1)); // +1 to also omnit last '/'
  36. }
  37. return ret;
  38. }
  39. bool rootIsPrefix(const std::string& root,
  40. const std::vector<std::string>& files, std::string& error)
  41. {
  42. for (std::string const& file : files) {
  43. if (!cmHasPrefix(file, root)) {
  44. error = cmStrCat("ROOT: ", root, " is not a prefix of file: ", file);
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. std::vector<std::string> prepareFilesPathsForTree(
  51. const std::vector<std::string>& filesPaths,
  52. const std::string& currentSourceDir)
  53. {
  54. std::vector<std::string> prepared;
  55. prepared.reserve(filesPaths.size());
  56. for (auto const& filePath : filesPaths) {
  57. std::string fullPath =
  58. cmSystemTools::CollapseFullPath(filePath, currentSourceDir);
  59. // If provided file path is actually not a directory, silently ignore it.
  60. if (cmSystemTools::FileIsDirectory(fullPath)) {
  61. continue;
  62. }
  63. // Handle directory that doesn't exist yet.
  64. if (!fullPath.empty() &&
  65. (fullPath.back() == '/' || fullPath.back() == '\\')) {
  66. continue;
  67. }
  68. prepared.emplace_back(std::move(fullPath));
  69. }
  70. return prepared;
  71. }
  72. bool addFilesToItsSourceGroups(const std::string& root,
  73. const std::set<std::string>& sgFilesPaths,
  74. const std::string& prefix, cmMakefile& makefile,
  75. std::string& errorMsg)
  76. {
  77. cmSourceGroup* sg;
  78. for (std::string const& sgFilesPath : sgFilesPaths) {
  79. std::vector<std::string> tokenizedPath;
  80. if (!prefix.empty()) {
  81. tokenizedPath = tokenizePath(cmStrCat(prefix, '/', sgFilesPath));
  82. } else {
  83. tokenizedPath = tokenizePath(sgFilesPath);
  84. }
  85. if (!tokenizedPath.empty()) {
  86. tokenizedPath.pop_back();
  87. if (tokenizedPath.empty()) {
  88. tokenizedPath.emplace_back();
  89. }
  90. sg = makefile.GetOrCreateSourceGroup(tokenizedPath);
  91. if (!sg) {
  92. errorMsg = "Could not create source group for file: " + sgFilesPath;
  93. return false;
  94. }
  95. const std::string fullPath =
  96. cmSystemTools::CollapseFullPath(sgFilesPath, root);
  97. sg->AddGroupFile(fullPath);
  98. }
  99. }
  100. return true;
  101. }
  102. ExpectedOptions getExpectedOptions()
  103. {
  104. ExpectedOptions options;
  105. options.push_back(kTreeOptionName);
  106. options.push_back(kPrefixOptionName);
  107. options.push_back(kFilesOptionName);
  108. options.push_back(kRegexOptionName);
  109. return options;
  110. }
  111. bool isExpectedOption(const std::string& argument,
  112. const ExpectedOptions& expectedOptions)
  113. {
  114. return cm::contains(expectedOptions, argument);
  115. }
  116. void parseArguments(const std::vector<std::string>& args,
  117. 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. } // namespace
  141. static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
  142. std::string& errorMsg);
  143. static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
  144. std::string& errorMsg);
  145. static bool checkSingleParameterArgumentPreconditions(
  146. const std::string& argument, const ParsedArguments& parsedArguments,
  147. std::string& errorMsg);
  148. bool cmSourceGroupCommand(std::vector<std::string> const& args,
  149. cmExecutionStatus& status)
  150. {
  151. if (args.empty()) {
  152. status.SetError("called with incorrect number of arguments");
  153. return false;
  154. }
  155. cmMakefile& mf = status.GetMakefile();
  156. // If only two arguments are given, the pre-1.8 version of the
  157. // command is being invoked.
  158. bool isShortTreeSyntax =
  159. ((args.size() == 2) && (args[0] == kTreeOptionName) &&
  160. cmSystemTools::FileIsDirectory(args[1]));
  161. if (args.size() == 2 && args[1] != kFilesOptionName && !isShortTreeSyntax) {
  162. cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]);
  163. if (!sg) {
  164. status.SetError("Could not create or find source group");
  165. return false;
  166. }
  167. sg->SetGroupRegex(args[1].c_str());
  168. return true;
  169. }
  170. ParsedArguments parsedArguments;
  171. std::string errorMsg;
  172. parseArguments(args, parsedArguments);
  173. if (!checkArgumentsPreconditions(parsedArguments, errorMsg)) {
  174. return false;
  175. }
  176. if (parsedArguments.find(kTreeOptionName) != parsedArguments.end()) {
  177. if (!processTree(mf, parsedArguments, errorMsg)) {
  178. status.SetError(errorMsg);
  179. return false;
  180. }
  181. } else {
  182. if (parsedArguments.find(kSourceGroupOptionName) ==
  183. parsedArguments.end()) {
  184. status.SetError("Missing source group name.");
  185. return false;
  186. }
  187. cmSourceGroup* sg = mf.GetOrCreateSourceGroup(args[0]);
  188. if (!sg) {
  189. status.SetError("Could not create or find source group");
  190. return false;
  191. }
  192. // handle regex
  193. if (parsedArguments.find(kRegexOptionName) != parsedArguments.end()) {
  194. const std::string& sgRegex = parsedArguments[kRegexOptionName].front();
  195. sg->SetGroupRegex(sgRegex.c_str());
  196. }
  197. // handle files
  198. const std::vector<std::string>& filesArguments =
  199. parsedArguments[kFilesOptionName];
  200. for (auto const& filesArg : filesArguments) {
  201. std::string src = filesArg;
  202. src =
  203. cmSystemTools::CollapseFullPath(src, mf.GetCurrentSourceDirectory());
  204. sg->AddGroupFile(src);
  205. }
  206. }
  207. return true;
  208. }
  209. static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
  210. std::string& errorMsg)
  211. {
  212. return checkSingleParameterArgumentPreconditions(
  213. kPrefixOptionName, parsedArguments, errorMsg) &&
  214. checkSingleParameterArgumentPreconditions(kTreeOptionName, parsedArguments,
  215. errorMsg) &&
  216. checkSingleParameterArgumentPreconditions(kRegexOptionName,
  217. parsedArguments, errorMsg);
  218. }
  219. static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
  220. std::string& errorMsg)
  221. {
  222. const std::string root =
  223. cmSystemTools::CollapseFullPath(parsedArguments[kTreeOptionName].front());
  224. std::string prefix = parsedArguments[kPrefixOptionName].empty()
  225. ? ""
  226. : parsedArguments[kPrefixOptionName].front();
  227. std::vector<std::string> files;
  228. auto filesArgIt = parsedArguments.find(kFilesOptionName);
  229. if (filesArgIt != parsedArguments.end()) {
  230. files = filesArgIt->second;
  231. } else {
  232. const std::vector<std::unique_ptr<cmSourceFile>>& srcFiles =
  233. mf.GetSourceFiles();
  234. for (const auto& srcFile : srcFiles) {
  235. if (!srcFile->GetIsGenerated()) {
  236. files.push_back(srcFile->GetLocation().GetFullPath());
  237. }
  238. }
  239. }
  240. const std::vector<std::string> filesVector =
  241. prepareFilesPathsForTree(files, mf.GetCurrentSourceDirectory());
  242. if (!rootIsPrefix(root, filesVector, errorMsg)) {
  243. return false;
  244. }
  245. std::set<std::string> sourceGroupPaths =
  246. getSourceGroupFilesPaths(root, filesVector);
  247. return addFilesToItsSourceGroups(root, sourceGroupPaths, prefix, mf,
  248. errorMsg);
  249. }
  250. static bool checkSingleParameterArgumentPreconditions(
  251. const std::string& argument, const ParsedArguments& parsedArguments,
  252. std::string& errorMsg)
  253. {
  254. auto foundArgument = parsedArguments.find(argument);
  255. if (foundArgument != parsedArguments.end()) {
  256. const std::vector<std::string>& optionArguments = foundArgument->second;
  257. if (optionArguments.empty()) {
  258. errorMsg = argument + " argument given without an argument.";
  259. return false;
  260. }
  261. if (optionArguments.size() > 1) {
  262. errorMsg = "too many arguments passed to " + argument + ".";
  263. return false;
  264. }
  265. }
  266. return true;
  267. }