cmSourceGroupCommand.cxx 9.1 KB

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