cmAddSubDirectoryCommand.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmAddSubDirectoryCommand.h"
  11. // cmAddSubDirectoryCommand
  12. bool cmAddSubDirectoryCommand::InitialPass(
  13. std::vector<std::string> const& args, cmExecutionStatus&)
  14. {
  15. if (args.empty()) {
  16. this->SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. // store the binpath
  20. std::string srcArg = args[0];
  21. std::string binArg;
  22. bool excludeFromAll = false;
  23. // process the rest of the arguments looking for optional args
  24. std::vector<std::string>::const_iterator i = args.begin();
  25. ++i;
  26. for (; i != args.end(); ++i) {
  27. if (*i == "EXCLUDE_FROM_ALL") {
  28. excludeFromAll = true;
  29. continue;
  30. } else if (binArg.empty()) {
  31. binArg = *i;
  32. } else {
  33. this->SetError("called with incorrect number of arguments");
  34. return false;
  35. }
  36. }
  37. // Compute the full path to the specified source directory.
  38. // Interpret a relative path with respect to the current source directory.
  39. std::string srcPath;
  40. if (cmSystemTools::FileIsFullPath(srcArg.c_str())) {
  41. srcPath = srcArg;
  42. } else {
  43. srcPath = this->Makefile->GetCurrentSourceDirectory();
  44. srcPath += "/";
  45. srcPath += srcArg;
  46. }
  47. if (!cmSystemTools::FileIsDirectory(srcPath)) {
  48. std::string error = "given source \"";
  49. error += srcArg;
  50. error += "\" which is not an existing directory.";
  51. this->SetError(error);
  52. return false;
  53. }
  54. srcPath = cmSystemTools::CollapseFullPath(srcPath);
  55. // Compute the full path to the binary directory.
  56. std::string binPath;
  57. if (binArg.empty()) {
  58. // No binary directory was specified. If the source directory is
  59. // not a subdirectory of the current directory then it is an
  60. // error.
  61. if (!cmSystemTools::IsSubDirectory(
  62. srcPath, this->Makefile->GetCurrentSourceDirectory())) {
  63. std::ostringstream e;
  64. e << "not given a binary directory but the given source directory "
  65. << "\"" << srcPath << "\" is not a subdirectory of \""
  66. << this->Makefile->GetCurrentSourceDirectory() << "\". "
  67. << "When specifying an out-of-tree source a binary directory "
  68. << "must be explicitly specified.";
  69. this->SetError(e.str());
  70. return false;
  71. }
  72. // Remove the CurrentDirectory from the srcPath and replace it
  73. // with the CurrentOutputDirectory.
  74. const char* src = this->Makefile->GetCurrentSourceDirectory();
  75. const char* bin = this->Makefile->GetCurrentBinaryDirectory();
  76. size_t srcLen = strlen(src);
  77. size_t binLen = strlen(bin);
  78. if (srcLen > 0 && src[srcLen - 1] == '/') {
  79. --srcLen;
  80. }
  81. if (binLen > 0 && bin[binLen - 1] == '/') {
  82. --binLen;
  83. }
  84. binPath = std::string(bin, binLen) + srcPath.substr(srcLen);
  85. } else {
  86. // Use the binary directory specified.
  87. // Interpret a relative path with respect to the current binary directory.
  88. if (cmSystemTools::FileIsFullPath(binArg.c_str())) {
  89. binPath = binArg;
  90. } else {
  91. binPath = this->Makefile->GetCurrentBinaryDirectory();
  92. binPath += "/";
  93. binPath += binArg;
  94. }
  95. }
  96. binPath = cmSystemTools::CollapseFullPath(binPath);
  97. // Add the subdirectory using the computed full paths.
  98. this->Makefile->AddSubDirectory(srcPath, binPath, excludeFromAll, true);
  99. return true;
  100. }