cmAddSubDirectoryCommand.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmAddSubDirectoryCommand.h"
  14. // cmAddSubDirectoryCommand
  15. bool cmAddSubDirectoryCommand::InitialPass
  16. (std::vector<std::string> const& args)
  17. {
  18. if(args.size() < 1 )
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // store the binpath
  24. std::string srcArg = args[0];
  25. std::string binArg;
  26. bool intoplevel = true;
  27. // process the rest of the arguments looking for optional args
  28. std::vector<std::string>::const_iterator i = args.begin();
  29. ++i;
  30. for(;i != args.end(); ++i)
  31. {
  32. if(*i == "EXCLUDE_FROM_ALL")
  33. {
  34. intoplevel = false;
  35. continue;
  36. }
  37. else if (!binArg.size())
  38. {
  39. binArg = *i;
  40. }
  41. else
  42. {
  43. this->SetError("called with incorrect number of arguments");
  44. return false;
  45. }
  46. }
  47. // check for relative arguments
  48. bool relativeSource = true;
  49. std::string binPath = binArg;
  50. std::string srcPath = std::string(this->Makefile->GetCurrentDirectory()) +
  51. "/" + srcArg;
  52. // if the path does not exist then the arg was relative
  53. if (!cmSystemTools::FileIsDirectory(srcPath.c_str()))
  54. {
  55. relativeSource = false;
  56. srcPath = srcArg;
  57. if (!cmSystemTools::FileIsDirectory(srcPath.c_str()))
  58. {
  59. std::string error = "Incorrect ADD_SUBDIRECTORY command. Directory: ";
  60. error += srcArg + " does not exists.";
  61. this->SetError(error.c_str());
  62. return false;
  63. }
  64. }
  65. // at this point srcPath has the full path to the source directory
  66. // now we need to compute the binPath if it was not provided
  67. // if the argument was provided then use it
  68. if (binArg.size())
  69. {
  70. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  71. {
  72. binPath = std::string(this->Makefile->GetCurrentOutputDirectory()) +
  73. "/" + binArg.c_str();
  74. }
  75. }
  76. // otherwise compute the binPath from the srcPath
  77. else
  78. {
  79. // if the srcArg was relative then we just do the same for the binPath
  80. if (relativeSource)
  81. {
  82. binPath = std::string(this->Makefile->GetCurrentOutputDirectory()) +
  83. "/" + srcArg;
  84. }
  85. // otherwise we try to remove the CurrentDirectory from the srcPath and
  86. // replace it with the CurrentOutputDirectory. This may not really work
  87. // because the source dir they provided may not be "in" the source
  88. // tree. This is an error if this happens.
  89. else
  90. {
  91. // try replacing the home dir with the home output dir
  92. binPath = srcPath;
  93. if (!cmSystemTools::FindLastString(binPath.c_str(),
  94. this->Makefile->GetHomeDirectory()))
  95. {
  96. this->SetError("A full source directory was specified that is not "
  97. "in the source tree but no binary directory was "
  98. "specified. If you specify an out of tree source "
  99. "directory then you must provide the binary "
  100. "directory as well.");
  101. return false;
  102. }
  103. cmSystemTools::ReplaceString(binPath,this->Makefile->GetHomeDirectory(),
  104. this->Makefile->GetHomeOutputDirectory());
  105. }
  106. }
  107. // now we have all the arguments
  108. this->Makefile->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  109. intoplevel, false, true);
  110. return true;
  111. }