cmAddSubDirectoryCommand.cxx 3.9 KB

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