cmGlobalGenerator.cxx 13 KB

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