cmLocalGenerator.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "cmLocalGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. cmLocalGenerator::cmLocalGenerator()
  18. {
  19. m_Makefile = new cmMakefile;
  20. m_Makefile->SetLocalGenerator(this);
  21. }
  22. cmLocalGenerator::~cmLocalGenerator()
  23. {
  24. delete m_Makefile;
  25. }
  26. void cmLocalGenerator::Configure()
  27. {
  28. // set the PROJECT_SOURCE_DIR and PROJECT_BIN_DIR to default values
  29. // just in case the project does not include a PROJECT command
  30. m_Makefile->AddDefinition("PROJECT_BINARY_DIR",
  31. m_Makefile->GetHomeOutputDirectory());
  32. m_Makefile->AddDefinition("PROJECT_SOURCE_DIR",
  33. m_Makefile->GetHomeDirectory());
  34. // find & read the list file
  35. std::string currentStart = m_Makefile->GetStartDirectory();
  36. currentStart += "/CMakeLists.txt";
  37. m_Makefile->ReadListFile(currentStart.c_str());
  38. }
  39. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  40. {
  41. m_GlobalGenerator = gg;
  42. // setup the home directories
  43. m_Makefile->SetHomeDirectory(
  44. gg->GetCMakeInstance()->GetHomeDirectory());
  45. m_Makefile->SetHomeOutputDirectory(
  46. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  47. }
  48. void cmLocalGenerator::ConfigureFinalPass()
  49. {
  50. m_Makefile->ConfigureFinalPass();
  51. }
  52. std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
  53. {
  54. // The first time this is called, initialize all
  55. // the path ivars that are used. This can not
  56. // be moved to the constructor because all the paths are not set yet.
  57. if(m_CurrentOutputDirectory.size() == 0)
  58. {
  59. m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
  60. m_HomeOutputDirectory = m_Makefile->GetHomeOutputDirectory();
  61. m_HomeDirectory = m_Makefile->GetHomeDirectory();
  62. if(m_RelativePathToSourceDir.size() == 0)
  63. {
  64. m_RelativePathToSourceDir = cmSystemTools::RelativePath(
  65. m_CurrentOutputDirectory.c_str(),
  66. m_HomeDirectory.c_str());
  67. std::string path = m_CurrentOutputDirectory;
  68. cmSystemTools::ReplaceString(path, m_HomeOutputDirectory.c_str(), "");
  69. unsigned i;
  70. m_RelativePathToBinaryDir = "";
  71. for(i =0; i < path.size(); ++i)
  72. {
  73. if(path[i] == '/')
  74. {
  75. m_RelativePathToBinaryDir += "../";
  76. }
  77. }
  78. }
  79. m_HomeOutputDirectoryNoSlash = m_HomeOutputDirectory;
  80. m_HomeOutputDirectory += "/";
  81. m_CurrentOutputDirectory += "/";
  82. }
  83. // Do the work of converting to a relative path
  84. std::string pathIn = p;
  85. std::string ret = pathIn;
  86. if(m_CurrentOutputDirectory.size() <= ret.size())
  87. {
  88. std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
  89. if(
  90. #if defined(_WIN32) || defined(__APPLE__)
  91. cmSystemTools::LowerCase(sub) ==
  92. cmSystemTools::LowerCase(m_CurrentOutputDirectory)
  93. #else
  94. sub == m_CurrentOutputDirectory
  95. #endif
  96. )
  97. {
  98. ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
  99. }
  100. }
  101. if(m_HomeDirectory.size() <= ret.size())
  102. {
  103. std::string sub = ret.substr(0, m_HomeDirectory.size());
  104. if(
  105. #if defined(_WIN32) || defined(__APPLE__)
  106. cmSystemTools::LowerCase(sub) ==
  107. cmSystemTools::LowerCase(m_HomeDirectory)
  108. #else
  109. sub == m_HomeDirectory
  110. #endif
  111. )
  112. {
  113. ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
  114. }
  115. }
  116. if(m_HomeOutputDirectory.size() <= ret.size())
  117. {
  118. std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
  119. if(
  120. #if defined(_WIN32) || defined(__APPLE__)
  121. cmSystemTools::LowerCase(sub) ==
  122. cmSystemTools::LowerCase(m_HomeOutputDirectory)
  123. #else
  124. sub == m_HomeOutputDirectory
  125. #endif
  126. )
  127. {
  128. ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
  129. }
  130. }
  131. std::string relpath = m_RelativePathToBinaryDir;
  132. if(relpath.size())
  133. {
  134. relpath.erase(relpath.size()-1, 1);
  135. }
  136. else
  137. {
  138. relpath = ".";
  139. }
  140. if(
  141. #if defined(_WIN32) || defined(__APPLE__)
  142. cmSystemTools::LowerCase(ret) ==
  143. cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
  144. #else
  145. ret == m_HomeOutputDirectoryNoSlash
  146. #endif
  147. )
  148. {
  149. ret = relpath;
  150. }
  151. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  152. return ret;
  153. }