cmGlobalGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 "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::EnableLanguage(const char* lang,
  31. cmMakefile *mf)
  32. {
  33. if(m_FindMakeProgramFile.size() == 0)
  34. {
  35. cmSystemTools::Error(
  36. "Generator implementation error, "
  37. "all generators must specify m_FindMakeProgramFile");
  38. }
  39. std::string root = mf->GetDefinition("CMAKE_ROOT");
  40. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  41. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  42. {
  43. std::string setMakeProgram = root;
  44. setMakeProgram += "/Modules/";
  45. setMakeProgram += m_FindMakeProgramFile;
  46. mf->ReadListFile(0, setMakeProgram.c_str());
  47. }
  48. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  49. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  50. {
  51. cmSystemTools::Error("EnableLanguage was unable to find a CMAKE_MAKE_PROGRAM");
  52. return;
  53. }
  54. std::string makeProgram = mf->GetDefinition("CMAKE_MAKE_PROGRAM");
  55. if(makeProgram.find(' ') != makeProgram.npos)
  56. {
  57. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  58. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  59. "make program",
  60. cmCacheManager::FILEPATH);
  61. }
  62. bool isLocal = m_CMakeInstance->GetLocal();
  63. // if no lang specified use CXX
  64. if(!lang )
  65. {
  66. lang = "CXX";
  67. }
  68. std::string rootBin = mf->GetHomeOutputDirectory();
  69. if(m_ConfiguredFilesPath.size())
  70. {
  71. rootBin = m_ConfiguredFilesPath;
  72. }
  73. bool needCBackwards = false;
  74. bool needCXXBackwards = false;
  75. // check for a C compiler and configure it
  76. if(!isLocal &&
  77. !this->GetLanguageEnabled("C") &&
  78. lang[0] == 'C')
  79. {
  80. if (m_CMakeInstance->GetIsInTryCompile())
  81. {
  82. cmSystemTools::Error("This should not have happen. "
  83. "If you see this message, you are probably using a "
  84. "broken CMakeLists.txt file or a problematic release of "
  85. "CMake");
  86. }
  87. needCBackwards = true;
  88. // Read the DetermineSystem file
  89. std::string systemFile = root;
  90. systemFile += "/Modules/CMakeDetermineSystem.cmake";
  91. mf->ReadListFile(0, systemFile.c_str());
  92. // read determine C compiler
  93. std::string determineCFile = root;
  94. determineCFile += "/Modules/CMakeDetermineCCompiler.cmake";
  95. mf->ReadListFile(0,determineCFile.c_str());
  96. this->SetLanguageEnabled("C");
  97. // put CC in the environment in case user scripts want
  98. // to run configure
  99. // see man putenv for explaination of this stupid code...
  100. if(mf->GetDefinition("CMAKE_C_COMPILER"))
  101. {
  102. static char envCC[5000];
  103. std::string env = "CC=${CMAKE_C_COMPILER}";
  104. mf->ExpandVariablesInString(env);
  105. strncpy(envCC, env.c_str(), 4999);
  106. envCC[4999] = 0;
  107. putenv(envCC);
  108. }
  109. }
  110. // check for a CXX compiler and configure it
  111. if(!isLocal &&
  112. !this->GetLanguageEnabled("CXX") &&
  113. strcmp(lang, "CXX") == 0)
  114. {
  115. needCXXBackwards = true;
  116. std::string determineCFile = root;
  117. determineCFile += "/Modules/CMakeDetermineCXXCompiler.cmake";
  118. mf->ReadListFile(0,determineCFile.c_str());
  119. this->SetLanguageEnabled("CXX");
  120. // put CXX in the environment in case user scripts want
  121. // to run configure
  122. // see man putenv for explaination of this stupid code...
  123. static char envCXX[5000];
  124. if(mf->GetDefinition("CMAKE_CXX_COMPILER"))
  125. {
  126. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  127. mf->ExpandVariablesInString(env);
  128. strncpy(envCXX, env.c_str(), 4999);
  129. envCXX[4999] = 0;
  130. putenv(envCXX);
  131. }
  132. }
  133. std::string fpath = rootBin;
  134. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  135. {
  136. fpath += "/CMakeSystem.cmake";
  137. mf->ReadListFile(0,fpath.c_str());
  138. }
  139. // if C, then enable C
  140. if(lang[0] == 'C' && !mf->GetDefinition("CMAKE_C_COMPILER_LOADED"))
  141. {
  142. fpath = rootBin;
  143. fpath += "/CMakeCCompiler.cmake";
  144. mf->ReadListFile(0,fpath.c_str());
  145. }
  146. if(strcmp(lang, "CXX") == 0 && !mf->GetDefinition("CMAKE_CXX_COMPILER_LOADED"))
  147. {
  148. fpath = rootBin;
  149. fpath += "/CMakeCXXCompiler.cmake";
  150. mf->ReadListFile(0,fpath.c_str());
  151. }
  152. if(!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  153. {
  154. fpath = root;
  155. fpath += "/Modules/CMakeSystemSpecificInformation.cmake";
  156. mf->ReadListFile(0,fpath.c_str());
  157. }
  158. if(!isLocal)
  159. {
  160. // At this point we should have enough info for a try compile
  161. // which is used in the backward stuff
  162. if(needCBackwards)
  163. {
  164. if (!m_CMakeInstance->GetIsInTryCompile())
  165. {
  166. // for old versions of CMake ListFiles
  167. const char* versionValue
  168. = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  169. if (!versionValue || atof(versionValue) <= 1.4)
  170. {
  171. std::string ifpath = root + "/Modules/CMakeBackwardCompatibilityC.cmake";
  172. mf->ReadListFile(0,ifpath.c_str());
  173. }
  174. }
  175. }
  176. if(needCXXBackwards)
  177. {
  178. if (!m_CMakeInstance->GetIsInTryCompile())
  179. {
  180. // for old versions of CMake ListFiles
  181. const char* versionValue
  182. = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  183. if (!versionValue || atof(versionValue) <= 1.4)
  184. {
  185. std::string nfpath = root + "/Modules/CMakeBackwardCompatibilityCXX.cmake";
  186. mf->ReadListFile(0,nfpath.c_str());
  187. }
  188. }
  189. }
  190. // if we are from the top, always define this
  191. mf->AddDefinition("RUN_CONFIGURE", true);
  192. }
  193. }
  194. void cmGlobalGenerator::SetLanguageEnabled(const char* l)
  195. {
  196. m_LanguageEnabled[l] = true;
  197. }
  198. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  199. {
  200. return (m_LanguageEnabled.count(l) > 0);
  201. }
  202. void cmGlobalGenerator::ClearEnabledLanguages()
  203. {
  204. m_LanguageEnabled.clear();
  205. }
  206. void cmGlobalGenerator::Configure()
  207. {
  208. // Delete any existing cmLocalGenerators
  209. unsigned int i;
  210. for (i = 0; i < m_LocalGenerators.size(); ++i)
  211. {
  212. delete m_LocalGenerators[i];
  213. }
  214. m_LocalGenerators.clear();
  215. // start with this directory
  216. cmLocalGenerator *lg = this->CreateLocalGenerator();
  217. m_LocalGenerators.push_back(lg);
  218. // set the Start directories
  219. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  220. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  221. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  222. // now do it
  223. this->RecursiveConfigure(lg,0.0f,0.9f);
  224. // after it is all done do a ConfigureFinalPass
  225. for (i = 0; i < m_LocalGenerators.size(); ++i)
  226. {
  227. m_LocalGenerators[i]->ConfigureFinalPass();
  228. m_CMakeInstance->UpdateProgress("Configuring",
  229. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  230. }
  231. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  232. }
  233. // loop through the directories creating cmLocalGenerators and Configure()
  234. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  235. float startProgress,
  236. float endProgress)
  237. {
  238. // configure the current directory
  239. lg->Configure();
  240. // get all the subdirectories
  241. std::vector<std::string> subdirs = lg->GetMakefile()->GetSubDirectories();
  242. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  243. m_CMakeInstance->UpdateProgress("Configuring",
  244. startProgress + progressPiece);
  245. // for each subdir recurse
  246. unsigned int i;
  247. for (i = 0; i < subdirs.size(); ++i)
  248. {
  249. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  250. m_LocalGenerators.push_back(lg2);
  251. // add the subdir to the start output directory
  252. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  253. outdir += "/";
  254. outdir += subdirs[i];
  255. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  256. // add the subdir to the start source directory
  257. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  258. currentDir += "/";
  259. currentDir += subdirs[i];
  260. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  261. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  262. this->RecursiveConfigure(lg2,
  263. startProgress + (i+1.0f)*progressPiece,
  264. startProgress + (i+2.0f)*progressPiece);
  265. }
  266. }
  267. void cmGlobalGenerator::Generate()
  268. {
  269. // For each existing cmLocalGenerator
  270. unsigned int i;
  271. for (i = 0; i < m_LocalGenerators.size(); ++i)
  272. {
  273. m_LocalGenerators[i]->Generate(true);
  274. m_CMakeInstance->UpdateProgress("Generating",
  275. (i+1.0f)/m_LocalGenerators.size());
  276. }
  277. m_CMakeInstance->UpdateProgress("Generating done", -1);
  278. }
  279. void cmGlobalGenerator::LocalGenerate()
  280. {
  281. // for this case we create one LocalGenerator
  282. // configure it, and then Generate it
  283. // start with this directory
  284. cmLocalGenerator *lg = this->CreateLocalGenerator();
  285. // set the Start directories
  286. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  287. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  288. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  289. // now do trhe configure
  290. lg->Configure();
  291. lg->ConfigureFinalPass();
  292. lg->Generate(false);
  293. delete lg;
  294. }
  295. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  296. const char *, const char *target,
  297. std::string *output)
  298. {
  299. // now build the test
  300. std::string makeCommand =
  301. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  302. if(makeCommand.size() == 0)
  303. {
  304. cmSystemTools::Error(
  305. "Generator cannot find the appropriate make command.");
  306. return 1;
  307. }
  308. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  309. /**
  310. * Run an executable command and put the stdout in output.
  311. */
  312. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  313. cmSystemTools::ChangeDirectory(bindir);
  314. // Since we have full control over the invocation of nmake, let us
  315. // make it quiet.
  316. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  317. {
  318. makeCommand += " /NOLOGO ";
  319. }
  320. // now build
  321. if (target)
  322. {
  323. makeCommand += " ";
  324. makeCommand += target;
  325. #if defined(_WIN32) || defined(__CYGWIN__)
  326. makeCommand += ".exe";
  327. #endif // WIN32
  328. }
  329. else
  330. {
  331. makeCommand += " all";
  332. }
  333. int retVal;
  334. if (!cmSystemTools::RunCommand(makeCommand.c_str(), *output, retVal, 0, false))
  335. {
  336. cmSystemTools::Error("Generator: execution of make failed.");
  337. // return to the original directory
  338. cmSystemTools::ChangeDirectory(cwd.c_str());
  339. return 1;
  340. }
  341. cmSystemTools::ChangeDirectory(cwd.c_str());
  342. return retVal;
  343. }
  344. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  345. {
  346. cmLocalGenerator *lg = new cmLocalGenerator;
  347. lg->SetGlobalGenerator(this);
  348. return lg;
  349. }
  350. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  351. {
  352. this->SetConfiguredFilesPath(
  353. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  354. const char* make =
  355. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  356. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  357. "make program",
  358. cmCacheManager::FILEPATH);
  359. // if C, then enable C
  360. if(gen->GetLanguageEnabled("C"))
  361. {
  362. this->SetLanguageEnabled("C");
  363. }
  364. // if CXX
  365. if(gen->GetLanguageEnabled("CXX"))
  366. {
  367. this->SetLanguageEnabled("CXX");
  368. }
  369. }