cmSourceGroupCommand.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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::set<std::string> getSourceGroupFilesPaths(
  26. const std::string& root, const std::vector<std::string>& files)
  27. {
  28. std::set<std::string> ret;
  29. const std::string::size_type rootLength = root.length();
  30. for (std::string const& file : files) {
  31. ret.insert(file.substr(rootLength + 1)); // +1 to also omnit last '/'
  32. }
  33. return ret;
  34. }
  35. bool rootIsPrefix(const std::string& root,
  36. const std::vector<std::string>& files, std::string& error)
  37. {
  38. for (std::string const& file : files) {
  39. if (!cmHasPrefix(file, root)) {
  40. error = cmStrCat("ROOT: ", root, " is not a prefix of file: ", file);
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. std::vector<std::string> prepareFilesPathsForTree(
  47. const std::vector<std::string>& filesPaths,
  48. const std::string& currentSourceDir)
  49. {
  50. std::vector<std::string> prepared;
  51. prepared.reserve(filesPaths.size());
  52. for (auto const& filePath : filesPaths) {
  53. std::string fullPath =
  54. cmSystemTools::CollapseFullPath(filePath, currentSourceDir);
  55. // If provided file path is actually not a directory, silently ignore it.
  56. if (cmSystemTools::FileIsDirectory(fullPath)) {
  57. continue;
  58. }
  59. // Handle directory that doesn't exist yet.
  60. if (!fullPath.empty() &&
  61. (fullPath.back() == '/' || fullPath.back() == '\\')) {
  62. continue;
  63. }
  64. prepared.emplace_back(std::move(fullPath));
  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 = cmTokenize(
  76. prefix.empty() ? sgFilesPath : cmStrCat(prefix, '/', sgFilesPath),
  77. R"(\/)", cmTokenizerMode::New);
  78. if (tokenizedPath.empty()) {
  79. continue;
  80. }
  81. tokenizedPath.pop_back();
  82. if (tokenizedPath.empty()) {
  83. tokenizedPath.emplace_back();
  84. }
  85. sg = makefile.GetOrCreateSourceGroup(tokenizedPath);
  86. if (!sg) {
  87. errorMsg = "Could not create source group for file: " + sgFilesPath;
  88. return false;
  89. }
  90. const std::string fullPath =
  91. cmSystemTools::CollapseFullPath(sgFilesPath, root);
  92. sg->AddGroupFile(fullPath);
  93. }
  94. return true;
  95. }
  96. ExpectedOptions getExpectedOptions()
  97. {
  98. ExpectedOptions options;
  99. options.push_back(kTreeOptionName);
  100. options.push_back(kPrefixOptionName);
  101. options.push_back(kFilesOptionName);
  102. options.push_back(kRegexOptionName);
  103. return options;
  104. }
  105. bool isExpectedOption(const std::string& argument,
  106. const ExpectedOptions& expectedOptions)
  107. {
  108. return cm::contains(expectedOptions, argument);
  109. }
  110. void parseArguments(const std::vector<std::string>& args,
  111. ParsedArguments& parsedArguments)
  112. {
  113. const ExpectedOptions expectedOptions = getExpectedOptions();
  114. size_t i = 0;
  115. // at this point we know that args vector is not empty
  116. // if first argument is not one of expected options it's source group name
  117. if (!isExpectedOption(args[0], expectedOptions)) {
  118. // get source group name and go to next argument
  119. parsedArguments[kSourceGroupOptionName].push_back(args[0]);
  120. ++i;
  121. }
  122. for (; i < args.size();) {
  123. // get current option and increment index to go to next argument
  124. const std::string& currentOption = args[i++];
  125. // create current option entry in parsed arguments
  126. std::vector<std::string>& currentOptionArguments =
  127. parsedArguments[currentOption];
  128. // collect option arguments while we won't find another expected option
  129. while (i < args.size() && !isExpectedOption(args[i], expectedOptions)) {
  130. currentOptionArguments.push_back(args[i++]);
  131. }
  132. }
  133. }
  134. } // namespace
  135. static bool checkArgumentsPreconditions(const ParsedArguments& parsedArguments,
  136. std::string& errorMsg);
  137. static bool processTree(cmMakefile& mf, ParsedArguments& parsedArguments,
  138. std::string& errorMsg);
  139. static bool checkSingleParameterArgumentPreconditions(
  140. const std::string& argument, const ParsedArguments& parsedArguments,
  141. std::string& errorMsg);
  142. bool cmSourceGroupCommand(std::vector<std::string> const& args,
  143. cmExecutionStatus& status)
  144. {
  145. if (args.empty()) {
  146. status.SetError("called with incorrect number of arguments");
  147. return false;
  148. }
  149. cmMakefile& mf = status.GetMakefile();
  150. // If only two arguments are given, the pre-1.8 version of the
  151. // command is being invoked.
  152. bool isShortTreeSyntax =
  153. ((args.size() == 2) && (args[0] == kTreeOptionName) &&
  154. cmSystemTools::FileIsDirectory(args[1]));
  155. if (args.size() == 2 && args[1] != kFilesOptionName && !isShortTreeSyntax) {
  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. std::vector<std::string> files;
  222. auto filesArgIt = parsedArguments.find(kFilesOptionName);
  223. if (filesArgIt != parsedArguments.end()) {
  224. files = filesArgIt->second;
  225. } else {
  226. const std::vector<std::unique_ptr<cmSourceFile>>& srcFiles =
  227. mf.GetSourceFiles();
  228. for (const auto& srcFile : srcFiles) {
  229. if (!srcFile->GetIsGenerated()) {
  230. files.push_back(srcFile->GetLocation().GetFullPath());
  231. }
  232. }
  233. }
  234. const std::vector<std::string> filesVector =
  235. prepareFilesPathsForTree(files, mf.GetCurrentSourceDirectory());
  236. if (!rootIsPrefix(root, filesVector, errorMsg)) {
  237. return false;
  238. }
  239. std::set<std::string> sourceGroupPaths =
  240. getSourceGroupFilesPaths(root, filesVector);
  241. return addFilesToItsSourceGroups(root, sourceGroupPaths, prefix, mf,
  242. errorMsg);
  243. }
  244. static bool checkSingleParameterArgumentPreconditions(
  245. const std::string& argument, const ParsedArguments& parsedArguments,
  246. std::string& errorMsg)
  247. {
  248. auto foundArgument = parsedArguments.find(argument);
  249. if (foundArgument != parsedArguments.end()) {
  250. const std::vector<std::string>& optionArguments = foundArgument->second;
  251. if (optionArguments.empty()) {
  252. errorMsg = argument + " argument given without an argument.";
  253. return false;
  254. }
  255. if (optionArguments.size() > 1) {
  256. errorMsg = "too many arguments passed to " + argument + ".";
  257. return false;
  258. }
  259. }
  260. return true;
  261. }