cmAddSubDirectoryCommand.cxx 3.3 KB

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