cmLocalGenerator.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // do not use relative paths for network build trees
  55. // the network paths do not work
  56. const char* outputDirectory = m_Makefile->GetHomeOutputDirectory();
  57. if ( outputDirectory && *outputDirectory && *(outputDirectory+1) &&
  58. outputDirectory[0] == '/' && outputDirectory[1] == '/' )
  59. {
  60. return cmSystemTools::ConvertToOutputPath(p);
  61. }
  62. // The first time this is called, initialize all
  63. // the path ivars that are used. This can not
  64. // be moved to the constructor because all the paths are not set yet.
  65. if(m_CurrentOutputDirectory.size() == 0)
  66. {
  67. m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
  68. m_HomeOutputDirectory = m_Makefile->GetHomeOutputDirectory();
  69. m_HomeDirectory = m_Makefile->GetHomeDirectory();
  70. if(m_RelativePathToSourceDir.size() == 0)
  71. {
  72. m_RelativePathToSourceDir = cmSystemTools::RelativePath(
  73. m_CurrentOutputDirectory.c_str(),
  74. m_HomeDirectory.c_str());
  75. std::string path = m_CurrentOutputDirectory;
  76. cmSystemTools::ReplaceString(path, m_HomeOutputDirectory.c_str(), "");
  77. unsigned i;
  78. m_RelativePathToBinaryDir = "";
  79. for(i =0; i < path.size(); ++i)
  80. {
  81. if(path[i] == '/')
  82. {
  83. m_RelativePathToBinaryDir += "../";
  84. }
  85. }
  86. }
  87. m_HomeOutputDirectoryNoSlash = m_HomeOutputDirectory;
  88. m_HomeOutputDirectory += "/";
  89. m_CurrentOutputDirectory += "/";
  90. }
  91. // Do the work of converting to a relative path
  92. std::string pathIn = p;
  93. if(pathIn.find('/') == pathIn.npos)
  94. {
  95. return pathIn;
  96. }
  97. if(pathIn.size() && pathIn[0] == '\"')
  98. {
  99. pathIn = pathIn.substr(1, pathIn.size()-2);
  100. }
  101. std::string ret = pathIn;
  102. if(m_CurrentOutputDirectory.size() <= ret.size())
  103. {
  104. std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
  105. if(
  106. #if defined(_WIN32) || defined(__APPLE__)
  107. cmSystemTools::LowerCase(sub) ==
  108. cmSystemTools::LowerCase(m_CurrentOutputDirectory)
  109. #else
  110. sub == m_CurrentOutputDirectory
  111. #endif
  112. )
  113. {
  114. ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
  115. }
  116. }
  117. if(m_HomeDirectory.size() <= ret.size())
  118. {
  119. std::string sub = ret.substr(0, m_HomeDirectory.size());
  120. if(
  121. #if defined(_WIN32) || defined(__APPLE__)
  122. cmSystemTools::LowerCase(sub) ==
  123. cmSystemTools::LowerCase(m_HomeDirectory)
  124. #else
  125. sub == m_HomeDirectory
  126. #endif
  127. )
  128. {
  129. ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
  130. }
  131. }
  132. if(m_HomeOutputDirectory.size() <= ret.size())
  133. {
  134. std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
  135. if(
  136. #if defined(_WIN32) || defined(__APPLE__)
  137. cmSystemTools::LowerCase(sub) ==
  138. cmSystemTools::LowerCase(m_HomeOutputDirectory)
  139. #else
  140. sub == m_HomeOutputDirectory
  141. #endif
  142. )
  143. {
  144. ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
  145. }
  146. }
  147. std::string relpath = m_RelativePathToBinaryDir;
  148. if(relpath.size())
  149. {
  150. relpath.erase(relpath.size()-1, 1);
  151. }
  152. else
  153. {
  154. relpath = ".";
  155. }
  156. if(
  157. #if defined(_WIN32) || defined(__APPLE__)
  158. cmSystemTools::LowerCase(ret) ==
  159. cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
  160. #else
  161. ret == m_HomeOutputDirectoryNoSlash
  162. #endif
  163. )
  164. {
  165. ret = relpath;
  166. }
  167. if(ret.size()
  168. && ret[0] != '\"' && ret[0] != '/' && ret[0] != '.')
  169. {
  170. if(ret.size() > 1 && ret[1] != ':')
  171. {
  172. ret = std::string("./") + ret;
  173. }
  174. }
  175. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  176. return ret;
  177. }