cmAddSubDirectoryCommand.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 "cmAddSubDirectoryCommand.h"
  4. #include <cstring>
  5. #include <cm/string_view>
  6. #include "cmExecutionStatus.h"
  7. #include "cmMakefile.h"
  8. #include "cmRange.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. bool cmAddSubDirectoryCommand(std::vector<std::string> const& args,
  12. cmExecutionStatus& status)
  13. {
  14. if (args.empty()) {
  15. status.SetError("called with incorrect number of arguments");
  16. return false;
  17. }
  18. cmMakefile& mf = status.GetMakefile();
  19. // store the binpath
  20. std::string const& srcArg = args.front();
  21. std::string binArg;
  22. bool excludeFromAll = false;
  23. // process the rest of the arguments looking for optional args
  24. for (std::string const& arg : cmMakeRange(args).advance(1)) {
  25. if (arg == "EXCLUDE_FROM_ALL") {
  26. excludeFromAll = true;
  27. continue;
  28. }
  29. if (binArg.empty()) {
  30. binArg = arg;
  31. } else {
  32. status.SetError("called with incorrect number of arguments");
  33. return false;
  34. }
  35. }
  36. // Compute the full path to the specified source directory.
  37. // Interpret a relative path with respect to the current source directory.
  38. std::string srcPath;
  39. if (cmSystemTools::FileIsFullPath(srcArg)) {
  40. srcPath = srcArg;
  41. } else {
  42. srcPath = cmStrCat(mf.GetCurrentSourceDirectory(), '/', srcArg);
  43. }
  44. if (!cmSystemTools::FileIsDirectory(srcPath)) {
  45. std::string error = cmStrCat("given source \"", srcArg,
  46. "\" which is not an existing directory.");
  47. status.SetError(error);
  48. return false;
  49. }
  50. srcPath =
  51. cmSystemTools::CollapseFullPath(srcPath, mf.GetHomeOutputDirectory());
  52. // Compute the full path to the binary directory.
  53. std::string binPath;
  54. if (binArg.empty()) {
  55. // No binary directory was specified. If the source directory is
  56. // not a subdirectory of the current directory then it is an
  57. // error.
  58. if (!cmSystemTools::IsSubDirectory(srcPath,
  59. mf.GetCurrentSourceDirectory())) {
  60. status.SetError(
  61. cmStrCat("not given a binary directory but the given source ",
  62. "directory \"", srcPath, "\" is not a subdirectory of \"",
  63. mf.GetCurrentSourceDirectory(),
  64. "\". When specifying an "
  65. "out-of-tree source a binary directory must be explicitly "
  66. "specified."));
  67. return false;
  68. }
  69. // Remove the CurrentDirectory from the srcPath and replace it
  70. // with the CurrentOutputDirectory.
  71. const std::string& src = mf.GetCurrentSourceDirectory();
  72. const std::string& bin = mf.GetCurrentBinaryDirectory();
  73. size_t srcLen = src.length();
  74. size_t binLen = bin.length();
  75. if (srcLen > 0 && src.back() == '/') {
  76. --srcLen;
  77. }
  78. if (binLen > 0 && bin.back() == '/') {
  79. --binLen;
  80. }
  81. binPath = cmStrCat(cm::string_view(bin).substr(0, binLen),
  82. cm::string_view(srcPath).substr(srcLen));
  83. } else {
  84. // Use the binary directory specified.
  85. // Interpret a relative path with respect to the current binary directory.
  86. if (cmSystemTools::FileIsFullPath(binArg)) {
  87. binPath = binArg;
  88. } else {
  89. binPath = cmStrCat(mf.GetCurrentBinaryDirectory(), '/', binArg);
  90. }
  91. }
  92. binPath = cmSystemTools::CollapseFullPath(binPath);
  93. // Add the subdirectory using the computed full paths.
  94. mf.AddSubDirectory(srcPath, binPath, excludeFromAll, true);
  95. return true;
  96. }