cmGlobalGenerator.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. cmGlobalGenerator::cmGlobalGenerator()
  18. {
  19. }
  20. cmGlobalGenerator::~cmGlobalGenerator()
  21. {
  22. // Delete any existing cmLocalGenerators
  23. unsigned int i;
  24. for (i = 0; i < m_LocalGenerators.size(); ++i)
  25. {
  26. delete m_LocalGenerators[i];
  27. }
  28. m_LocalGenerators.clear();
  29. }
  30. void cmGlobalGenerator::SetLanguageEnabled(const char* l)
  31. {
  32. m_LanguageEnabled[l] = true;
  33. }
  34. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  35. {
  36. return (m_LanguageEnabled.count(l) > 0);
  37. }
  38. void cmGlobalGenerator::ClearEnabledLanguages()
  39. {
  40. m_LanguageEnabled.clear();
  41. }
  42. void cmGlobalGenerator::Configure()
  43. {
  44. // Delete any existing cmLocalGenerators
  45. unsigned int i;
  46. for (i = 0; i < m_LocalGenerators.size(); ++i)
  47. {
  48. delete m_LocalGenerators[i];
  49. }
  50. m_LocalGenerators.clear();
  51. // start with this directory
  52. cmLocalGenerator *lg = this->CreateLocalGenerator();
  53. m_LocalGenerators.push_back(lg);
  54. // set the Start directories
  55. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  56. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  57. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  58. // now do it
  59. this->RecursiveConfigure(lg);
  60. // after it is all done do a ConfigureFinalPass
  61. for (i = 0; i < m_LocalGenerators.size(); ++i)
  62. {
  63. m_LocalGenerators[i]->ConfigureFinalPass();
  64. }
  65. }
  66. // loop through the directories creating cmLocalGenerators and Configure()
  67. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg)
  68. {
  69. // configure the current directory
  70. lg->Configure();
  71. // get all the subdirectories
  72. std::vector<std::string> subdirs = lg->GetMakefile()->GetSubDirectories();
  73. // for each subdir recurse
  74. unsigned int i;
  75. for (i = 0; i < subdirs.size(); ++i)
  76. {
  77. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  78. m_LocalGenerators.push_back(lg2);
  79. // add the subdir to the start output directory
  80. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  81. outdir += "/";
  82. outdir += subdirs[i];
  83. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  84. // add the subdir to the start source directory
  85. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  86. currentDir += "/";
  87. currentDir += subdirs[i];
  88. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  89. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  90. this->RecursiveConfigure(lg2);
  91. }
  92. }
  93. void cmGlobalGenerator::Generate()
  94. {
  95. // For each existing cmLocalGenerator
  96. unsigned int i;
  97. for (i = 0; i < m_LocalGenerators.size(); ++i)
  98. {
  99. m_LocalGenerators[i]->Generate(true);
  100. }
  101. }
  102. void cmGlobalGenerator::LocalGenerate()
  103. {
  104. // for this case we create one LocalGenerator
  105. // configure it, and then Generate it
  106. // start with this directory
  107. cmLocalGenerator *lg = this->CreateLocalGenerator();
  108. // set the Start directories
  109. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  110. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  111. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  112. // now do trhe configure
  113. lg->Configure();
  114. lg->ConfigureFinalPass();
  115. lg->Generate(false);
  116. delete lg;
  117. }
  118. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  119. const char *, const char *target,
  120. std::string *output)
  121. {
  122. // now build the test
  123. std::string makeCommand =
  124. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  125. if(makeCommand.size() == 0)
  126. {
  127. cmSystemTools::Error(
  128. "Generator cannot find the appropriate make command.");
  129. return 1;
  130. }
  131. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  132. /**
  133. * Run an executable command and put the stdout in output.
  134. */
  135. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  136. cmSystemTools::ChangeDirectory(bindir);
  137. // now build
  138. if (target)
  139. {
  140. makeCommand += " ";
  141. makeCommand += target;
  142. #if defined(_WIN32) || defined(__CYGWIN__)
  143. makeCommand += ".exe";
  144. #endif // WIN32
  145. }
  146. else
  147. {
  148. makeCommand += " all";
  149. }
  150. int retVal;
  151. if (!cmSystemTools::RunCommand(makeCommand.c_str(), *output, retVal, 0, false))
  152. {
  153. cmSystemTools::Error("Generator: execution of make failed.");
  154. // return to the original directory
  155. cmSystemTools::ChangeDirectory(cwd.c_str());
  156. return 1;
  157. }
  158. cmSystemTools::ChangeDirectory(cwd.c_str());
  159. return retVal;
  160. }
  161. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  162. {
  163. cmLocalGenerator *lg = new cmLocalGenerator;
  164. lg->SetGlobalGenerator(this);
  165. return lg;
  166. }
  167. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen,
  168. cmMakefile *)
  169. {
  170. // create a temp generator
  171. cmLocalGenerator *lg = this->CreateLocalGenerator();
  172. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  173. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  174. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  175. // for each existing language call enable Language
  176. std::map<cmStdString, bool>::const_iterator i =
  177. gen->m_LanguageEnabled.begin();
  178. for (;i != gen->m_LanguageEnabled.end(); ++i)
  179. {
  180. this->EnableLanguage(i->first.c_str(),lg->GetMakefile());
  181. }
  182. delete lg;
  183. }