cmGlobalGenerator.cxx 14 KB

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