cmAddSubDirectoryCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.c_str());
  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::FindLastString(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().c_str());
  83. return false;
  84. }
  85. // Remove the CurrentDirectory from the srcPath and replace it
  86. // with the CurrentOutputDirectory.
  87. binPath = srcPath;
  88. cmSystemTools::ReplaceString(binPath,
  89. this->Makefile->GetCurrentDirectory(),
  90. this->Makefile->GetCurrentOutputDirectory());
  91. }
  92. else
  93. {
  94. // Use the binary directory specified.
  95. // Interpret a relative path with respect to the current binary directory.
  96. if(cmSystemTools::FileIsFullPath(binArg.c_str()))
  97. {
  98. binPath = binArg;
  99. }
  100. else
  101. {
  102. binPath = this->Makefile->GetCurrentOutputDirectory();
  103. binPath += "/";
  104. binPath += binArg;
  105. }
  106. }
  107. binPath = cmSystemTools::CollapseFullPath(binPath.c_str());
  108. // Add the subdirectory using the computed full paths.
  109. this->Makefile->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  110. excludeFromAll, false, true);
  111. return true;
  112. }