cmGlobalGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. std::set<std::string> notFoundMap;
  264. // after it is all done do a ConfigureFinalPass
  265. cmCacheManager* manager = 0;
  266. for (i = 0; i < m_LocalGenerators.size(); ++i)
  267. {
  268. manager = m_LocalGenerators[i]->GetMakefile()->GetCacheManager();
  269. m_LocalGenerators[i]->ConfigureFinalPass();
  270. cmTargets const& targets = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  271. for (cmTargets::const_iterator l = targets.begin();
  272. l != targets.end(); l++)
  273. {
  274. cmTarget::LinkLibraries libs = l->second.GetLinkLibraries();
  275. for(cmTarget::LinkLibraries::iterator lib = libs.begin();
  276. lib != libs.end(); ++lib)
  277. {
  278. if(cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  279. {
  280. std::string not = lib->first.substr(0, lib->first.size()-9);
  281. notFoundMap.insert(not);
  282. }
  283. }
  284. std::vector<std::string>& incs =
  285. m_LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  286. for( std::vector<std::string>::iterator lib = incs.begin();
  287. lib != incs.end(); ++lib)
  288. {
  289. if(cmSystemTools::IsNOTFOUND(lib->c_str()))
  290. {
  291. std::string not = lib->substr(0, lib->size()-9);
  292. notFoundMap.insert(not);
  293. }
  294. }
  295. m_CMakeInstance->UpdateProgress("Configuring",
  296. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  297. }
  298. }
  299. if(notFoundMap.size())
  300. {
  301. std::string notFoundVars;
  302. for(std::set<std::string>::iterator i = notFoundMap.begin();
  303. i != notFoundMap.end(); ++i)
  304. {
  305. notFoundVars += *i;
  306. if(manager)
  307. {
  308. cmCacheManager::CacheIterator it =
  309. manager->GetCacheIterator(i->c_str());
  310. if(it.GetPropertyAsBool("ADVANCED"))
  311. {
  312. notFoundVars += " (ADVANCED)";
  313. }
  314. }
  315. notFoundVars += "\n";
  316. }
  317. cmSystemTools::Error("This project requires some variables to be set,\n"
  318. "and cmake can not find them.\n"
  319. "Please set the following variables:\n",
  320. notFoundVars.c_str());
  321. }
  322. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  323. }
  324. // loop through the directories creating cmLocalGenerators and Configure()
  325. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  326. float startProgress,
  327. float endProgress)
  328. {
  329. // configure the current directory
  330. lg->Configure();
  331. // get all the subdirectories
  332. std::vector<std::string> subdirs = lg->GetMakefile()->GetSubDirectories();
  333. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  334. m_CMakeInstance->UpdateProgress("Configuring",
  335. startProgress + progressPiece);
  336. // for each subdir recurse
  337. unsigned int i;
  338. for (i = 0; i < subdirs.size(); ++i)
  339. {
  340. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  341. m_LocalGenerators.push_back(lg2);
  342. // add the subdir to the start output directory
  343. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  344. outdir += "/";
  345. outdir += subdirs[i];
  346. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  347. // add the subdir to the start source directory
  348. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  349. currentDir += "/";
  350. currentDir += subdirs[i];
  351. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  352. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  353. this->RecursiveConfigure(lg2,
  354. startProgress + (i+1.0f)*progressPiece,
  355. startProgress + (i+2.0f)*progressPiece);
  356. }
  357. }
  358. void cmGlobalGenerator::Generate()
  359. {
  360. // For each existing cmLocalGenerator
  361. unsigned int i;
  362. for (i = 0; i < m_LocalGenerators.size(); ++i)
  363. {
  364. m_LocalGenerators[i]->Generate(true);
  365. m_CMakeInstance->UpdateProgress("Generating",
  366. (i+1.0f)/m_LocalGenerators.size());
  367. }
  368. m_CMakeInstance->UpdateProgress("Generating done", -1);
  369. }
  370. void cmGlobalGenerator::LocalGenerate()
  371. {
  372. // for this case we create one LocalGenerator
  373. // configure it, and then Generate it
  374. // start with this directory
  375. cmLocalGenerator *lg = this->CreateLocalGenerator();
  376. // set the Start directories
  377. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  378. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  379. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  380. // now do trhe configure
  381. lg->Configure();
  382. lg->ConfigureFinalPass();
  383. lg->Generate(false);
  384. delete lg;
  385. }
  386. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  387. const char *, const char *target,
  388. std::string *output)
  389. {
  390. // now build the test
  391. std::string makeCommand =
  392. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  393. if(makeCommand.size() == 0)
  394. {
  395. cmSystemTools::Error(
  396. "Generator cannot find the appropriate make command.");
  397. return 1;
  398. }
  399. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  400. /**
  401. * Run an executable command and put the stdout in output.
  402. */
  403. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  404. cmSystemTools::ChangeDirectory(bindir);
  405. // Since we have full control over the invocation of nmake, let us
  406. // make it quiet.
  407. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  408. {
  409. makeCommand += " /NOLOGO ";
  410. }
  411. // now build
  412. if (target)
  413. {
  414. makeCommand += " ";
  415. makeCommand += target;
  416. #if defined(_WIN32) || defined(__CYGWIN__)
  417. makeCommand += ".exe";
  418. #endif // WIN32
  419. }
  420. else
  421. {
  422. makeCommand += " all";
  423. }
  424. int retVal;
  425. if (!cmSystemTools::RunCommand(makeCommand.c_str(), *output, retVal, 0, false))
  426. {
  427. cmSystemTools::Error("Generator: execution of make failed.");
  428. // return to the original directory
  429. cmSystemTools::ChangeDirectory(cwd.c_str());
  430. return 1;
  431. }
  432. cmSystemTools::ChangeDirectory(cwd.c_str());
  433. return retVal;
  434. }
  435. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  436. {
  437. cmLocalGenerator *lg = new cmLocalGenerator;
  438. lg->SetGlobalGenerator(this);
  439. return lg;
  440. }
  441. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  442. {
  443. this->SetConfiguredFilesPath(
  444. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  445. const char* make =
  446. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  447. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  448. "make program",
  449. cmCacheManager::FILEPATH);
  450. // if C, then enable C
  451. if(gen->GetLanguageEnabled("C"))
  452. {
  453. this->SetLanguageEnabled("C");
  454. }
  455. // if CXX
  456. if(gen->GetLanguageEnabled("CXX"))
  457. {
  458. this->SetLanguageEnabled("CXX");
  459. }
  460. }