cmGlobalGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. this->SetLanguageEnabled("C");
  161. }
  162. if(strcmp(lang, "CXX") == 0 && !mf->GetDefinition("CMAKE_CXX_COMPILER_LOADED"))
  163. {
  164. fpath = rootBin;
  165. fpath += "/CMakeCXXCompiler.cmake";
  166. mf->ReadListFile(0,fpath.c_str());
  167. this->SetLanguageEnabled("CXX");
  168. }
  169. if(strcmp(lang, "JAVA") == 0 && !mf->GetDefinition("CMAKE_JAVA_COMPILER_LOADED"))
  170. {
  171. fpath = rootBin;
  172. fpath += "/CMakeJavaCompiler.cmake";
  173. mf->ReadListFile(0,fpath.c_str());
  174. this->SetLanguageEnabled("JAVA");
  175. }
  176. if ( lang[0] == 'C' && !mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  177. {
  178. fpath = root;
  179. fpath += "/Modules/CMakeSystemSpecificInformation.cmake";
  180. mf->ReadListFile(0,fpath.c_str());
  181. }
  182. if(!isLocal)
  183. {
  184. // At this point we should have enough info for a try compile
  185. // which is used in the backward stuff
  186. if(needCBackwards)
  187. {
  188. if (!m_CMakeInstance->GetIsInTryCompile())
  189. {
  190. // for old versions of CMake ListFiles
  191. const char* versionValue
  192. = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  193. if (!versionValue || atof(versionValue) <= 1.4)
  194. {
  195. std::string ifpath = root + "/Modules/CMakeBackwardCompatibilityC.cmake";
  196. mf->ReadListFile(0,ifpath.c_str());
  197. }
  198. }
  199. }
  200. if(needCXXBackwards)
  201. {
  202. if (!m_CMakeInstance->GetIsInTryCompile())
  203. {
  204. // for old versions of CMake ListFiles
  205. const char* versionValue
  206. = mf->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  207. if (!versionValue || atof(versionValue) <= 1.4)
  208. {
  209. std::string nfpath = root + "/Modules/CMakeBackwardCompatibilityCXX.cmake";
  210. mf->ReadListFile(0,nfpath.c_str());
  211. }
  212. }
  213. }
  214. // if we are from the top, always define this
  215. mf->AddDefinition("RUN_CONFIGURE", true);
  216. }
  217. }
  218. void cmGlobalGenerator::SetLanguageEnabled(const char* l)
  219. {
  220. m_LanguageEnabled[l] = true;
  221. }
  222. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  223. {
  224. return (m_LanguageEnabled.count(l) > 0);
  225. }
  226. void cmGlobalGenerator::ClearEnabledLanguages()
  227. {
  228. m_LanguageEnabled.clear();
  229. }
  230. void cmGlobalGenerator::Configure()
  231. {
  232. // Delete any existing cmLocalGenerators
  233. unsigned int i;
  234. for (i = 0; i < m_LocalGenerators.size(); ++i)
  235. {
  236. delete m_LocalGenerators[i];
  237. }
  238. m_LocalGenerators.clear();
  239. // start with this directory
  240. cmLocalGenerator *lg = this->CreateLocalGenerator();
  241. m_LocalGenerators.push_back(lg);
  242. // set the Start directories
  243. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  244. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  245. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  246. // now do it
  247. this->RecursiveConfigure(lg,0.0f,0.9f);
  248. // after it is all done do a ConfigureFinalPass
  249. for (i = 0; i < m_LocalGenerators.size(); ++i)
  250. {
  251. m_LocalGenerators[i]->ConfigureFinalPass();
  252. m_CMakeInstance->UpdateProgress("Configuring",
  253. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  254. }
  255. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  256. }
  257. // loop through the directories creating cmLocalGenerators and Configure()
  258. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  259. float startProgress,
  260. float endProgress)
  261. {
  262. // configure the current directory
  263. lg->Configure();
  264. // get all the subdirectories
  265. std::vector<std::string> subdirs = lg->GetMakefile()->GetSubDirectories();
  266. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  267. m_CMakeInstance->UpdateProgress("Configuring",
  268. startProgress + progressPiece);
  269. // for each subdir recurse
  270. unsigned int i;
  271. for (i = 0; i < subdirs.size(); ++i)
  272. {
  273. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  274. m_LocalGenerators.push_back(lg2);
  275. // add the subdir to the start output directory
  276. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  277. outdir += "/";
  278. outdir += subdirs[i];
  279. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  280. // add the subdir to the start source directory
  281. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  282. currentDir += "/";
  283. currentDir += subdirs[i];
  284. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  285. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  286. this->RecursiveConfigure(lg2,
  287. startProgress + (i+1.0f)*progressPiece,
  288. startProgress + (i+2.0f)*progressPiece);
  289. }
  290. }
  291. void cmGlobalGenerator::Generate()
  292. {
  293. // For each existing cmLocalGenerator
  294. unsigned int i;
  295. for (i = 0; i < m_LocalGenerators.size(); ++i)
  296. {
  297. m_LocalGenerators[i]->Generate(true);
  298. m_CMakeInstance->UpdateProgress("Generating",
  299. (i+1.0f)/m_LocalGenerators.size());
  300. }
  301. m_CMakeInstance->UpdateProgress("Generating done", -1);
  302. }
  303. void cmGlobalGenerator::LocalGenerate()
  304. {
  305. // for this case we create one LocalGenerator
  306. // configure it, and then Generate it
  307. // start with this directory
  308. cmLocalGenerator *lg = this->CreateLocalGenerator();
  309. // set the Start directories
  310. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  311. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  312. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  313. // now do trhe configure
  314. lg->Configure();
  315. lg->ConfigureFinalPass();
  316. lg->Generate(false);
  317. delete lg;
  318. }
  319. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  320. const char *, const char *target,
  321. std::string *output)
  322. {
  323. // now build the test
  324. std::string makeCommand =
  325. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  326. if(makeCommand.size() == 0)
  327. {
  328. cmSystemTools::Error(
  329. "Generator cannot find the appropriate make command.");
  330. return 1;
  331. }
  332. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  333. /**
  334. * Run an executable command and put the stdout in output.
  335. */
  336. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  337. cmSystemTools::ChangeDirectory(bindir);
  338. // Since we have full control over the invocation of nmake, let us
  339. // make it quiet.
  340. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  341. {
  342. makeCommand += " /NOLOGO ";
  343. }
  344. // now build
  345. if (target)
  346. {
  347. makeCommand += " ";
  348. makeCommand += target;
  349. #if defined(_WIN32) || defined(__CYGWIN__)
  350. makeCommand += ".exe";
  351. #endif // WIN32
  352. }
  353. else
  354. {
  355. makeCommand += " all";
  356. }
  357. int retVal;
  358. if (!cmSystemTools::RunCommand(makeCommand.c_str(), *output, retVal, 0, false))
  359. {
  360. cmSystemTools::Error("Generator: execution of make failed.");
  361. // return to the original directory
  362. cmSystemTools::ChangeDirectory(cwd.c_str());
  363. return 1;
  364. }
  365. cmSystemTools::ChangeDirectory(cwd.c_str());
  366. return retVal;
  367. }
  368. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  369. {
  370. cmLocalGenerator *lg = new cmLocalGenerator;
  371. lg->SetGlobalGenerator(this);
  372. return lg;
  373. }
  374. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  375. {
  376. this->SetConfiguredFilesPath(
  377. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  378. const char* make =
  379. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  380. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  381. "make program",
  382. cmCacheManager::FILEPATH);
  383. // if C, then enable C
  384. if(gen->GetLanguageEnabled("C"))
  385. {
  386. this->SetLanguageEnabled("C");
  387. }
  388. // if CXX
  389. if(gen->GetLanguageEnabled("CXX"))
  390. {
  391. this->SetLanguageEnabled("CXX");
  392. }
  393. }