cmGlobalGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. #include <stdlib.h> // required for atof
  18. #if defined(_WIN32) && !defined(__CYGWIN__)
  19. #include <windows.h>
  20. #endif
  21. int cmGlobalGenerator::s_TryCompileTimeout = 0;
  22. cmGlobalGenerator::cmGlobalGenerator()
  23. {
  24. // do nothing duh
  25. }
  26. cmGlobalGenerator::~cmGlobalGenerator()
  27. {
  28. // Delete any existing cmLocalGenerators
  29. unsigned int i;
  30. for (i = 0; i < m_LocalGenerators.size(); ++i)
  31. {
  32. delete m_LocalGenerators[i];
  33. }
  34. m_LocalGenerators.clear();
  35. }
  36. void cmGlobalGenerator::EnableLanguage(const char* lang,
  37. cmMakefile *mf)
  38. {
  39. if(m_FindMakeProgramFile.size() == 0)
  40. {
  41. cmSystemTools::Error(
  42. "Generator implementation error, "
  43. "all generators must specify m_FindMakeProgramFile");
  44. }
  45. std::string root = mf->GetDefinition("CMAKE_ROOT");
  46. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  47. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  48. {
  49. std::string setMakeProgram = root;
  50. setMakeProgram += "/Modules/";
  51. setMakeProgram += m_FindMakeProgramFile;
  52. mf->ReadListFile(0, setMakeProgram.c_str());
  53. }
  54. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  55. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  56. {
  57. cmOStringStream err;
  58. err << "CMake was unable to find a build program corresponding to \""
  59. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  60. << "probably need to select a different build tool.";
  61. cmSystemTools::Error(err.str().c_str());
  62. cmSystemTools::SetFatalErrorOccured();
  63. return;
  64. }
  65. std::string makeProgram = mf->GetDefinition("CMAKE_MAKE_PROGRAM");
  66. if(makeProgram.find(' ') != makeProgram.npos)
  67. {
  68. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  69. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  70. "make program",
  71. cmCacheManager::FILEPATH);
  72. }
  73. bool isLocal = m_CMakeInstance->GetLocal();
  74. // if no lang specified use CXX
  75. if(!lang )
  76. {
  77. lang = "CXX";
  78. }
  79. std::string rootBin = mf->GetHomeOutputDirectory();
  80. if(m_ConfiguredFilesPath.size())
  81. {
  82. rootBin = m_ConfiguredFilesPath;
  83. }
  84. bool needCBackwards = false;
  85. bool needCXXBackwards = false;
  86. if (!isLocal &&
  87. !this->GetLanguageEnabled("C") && !this->GetLanguageEnabled("CXX") &&
  88. !this->GetLanguageEnabled("JAVA"))
  89. {
  90. #if defined(_WIN32) && !defined(__CYGWIN__)
  91. /* Windows version number data. */
  92. OSVERSIONINFO osvi;
  93. ZeroMemory(&osvi, sizeof(osvi));
  94. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  95. GetVersionEx (&osvi);
  96. cmOStringStream windowsVersionString;
  97. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  98. windowsVersionString.str();
  99. mf->AddDefinition("CMAKE_SYSTEM_VERSION", windowsVersionString.str().c_str());
  100. #endif
  101. // Read the DetermineSystem file
  102. std::string systemFile = root;
  103. systemFile += "/Modules/CMakeDetermineSystem.cmake";
  104. mf->ReadListFile(0, systemFile.c_str());
  105. }
  106. // check for a C compiler and configure it
  107. if(!isLocal &&
  108. !this->GetLanguageEnabled("C") &&
  109. lang[0] == 'C')
  110. {
  111. if (m_CMakeInstance->GetIsInTryCompile())
  112. {
  113. cmSystemTools::Error("This should not have happen. "
  114. "If you see this message, you are probably using a "
  115. "broken CMakeLists.txt file or a problematic release of "
  116. "CMake");
  117. }
  118. needCBackwards = true;
  119. // read determine C compiler
  120. std::string determineCFile = root;
  121. determineCFile += "/Modules/CMakeDetermineCCompiler.cmake";
  122. mf->ReadListFile(0,determineCFile.c_str());
  123. this->SetLanguageEnabled("C");
  124. // put CC in the environment in case user scripts want
  125. // to run configure
  126. // see man putenv for explaination of this stupid code...
  127. if(mf->GetDefinition("CMAKE_C_COMPILER"))
  128. {
  129. static char envCC[5000];
  130. std::string env = "CC=${CMAKE_C_COMPILER}";
  131. mf->ExpandVariablesInString(env);
  132. unsigned int size = static_cast<unsigned int>(env.size());
  133. if(size > 4999)
  134. {
  135. size = 4999;
  136. }
  137. strncpy(envCC, env.c_str(), size);
  138. envCC[size] = 0;
  139. putenv(envCC);
  140. }
  141. }
  142. // check for a CXX compiler and configure it
  143. if(!isLocal &&
  144. !this->GetLanguageEnabled("CXX") &&
  145. strcmp(lang, "CXX") == 0)
  146. {
  147. needCXXBackwards = true;
  148. std::string determineCFile = root;
  149. determineCFile += "/Modules/CMakeDetermineCXXCompiler.cmake";
  150. mf->ReadListFile(0,determineCFile.c_str());
  151. this->SetLanguageEnabled("CXX");
  152. // put CXX in the environment in case user scripts want
  153. // to run configure
  154. // see man putenv for explaination of this stupid code...
  155. static char envCXX[5000];
  156. if(mf->GetDefinition("CMAKE_CXX_COMPILER"))
  157. {
  158. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  159. mf->ExpandVariablesInString(env);
  160. unsigned int size = static_cast<unsigned int>(env.size());
  161. if(size > 4999)
  162. {
  163. size = 4999;
  164. }
  165. strncpy(envCXX, env.c_str(), size);
  166. envCXX[4999] = 0;
  167. putenv(envCXX);
  168. }
  169. }
  170. // check for a Java compiler and configure it
  171. if(!isLocal &&
  172. !this->GetLanguageEnabled("JAVA") &&
  173. strcmp(lang, "JAVA") == 0)
  174. {
  175. std::string determineCFile = root;
  176. determineCFile += "/Modules/CMakeDetermineJavaCompiler.cmake";
  177. mf->ReadListFile(0,determineCFile.c_str());
  178. this->SetLanguageEnabled("JAVA");
  179. }
  180. std::string fpath = rootBin;
  181. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  182. {
  183. fpath += "/CMakeSystem.cmake";
  184. mf->ReadListFile(0,fpath.c_str());
  185. }
  186. // if C, then enable C
  187. if(lang[0] == 'C' && !mf->GetDefinition("CMAKE_C_COMPILER_LOADED"))
  188. {
  189. fpath = rootBin;
  190. fpath += "/CMakeCCompiler.cmake";
  191. mf->ReadListFile(0,fpath.c_str());
  192. this->SetLanguageEnabled("C");
  193. }
  194. if(strcmp(lang, "CXX") == 0 && !mf->GetDefinition("CMAKE_CXX_COMPILER_LOADED"))
  195. {
  196. fpath = rootBin;
  197. fpath += "/CMakeCXXCompiler.cmake";
  198. mf->ReadListFile(0,fpath.c_str());
  199. this->SetLanguageEnabled("CXX");
  200. }
  201. if(strcmp(lang, "JAVA") == 0 && !mf->GetDefinition("CMAKE_JAVA_COMPILER_LOADED"))
  202. {
  203. fpath = rootBin;
  204. fpath += "/CMakeJavaCompiler.cmake";
  205. mf->ReadListFile(0,fpath.c_str());
  206. this->SetLanguageEnabled("JAVA");
  207. }
  208. if ( lang[0] == 'C' && !mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  209. {
  210. fpath = root;
  211. fpath += "/Modules/CMakeSystemSpecificInformation.cmake";
  212. mf->ReadListFile(0,fpath.c_str());
  213. }
  214. if(!isLocal)
  215. {
  216. // At this point we should have enough info for a try compile
  217. // which is used in the backward stuff
  218. if(needCBackwards)
  219. {
  220. if (!m_CMakeInstance->GetIsInTryCompile())
  221. {
  222. std::string ifpath = root + "/Modules/CMakeTestCCompiler.cmake";
  223. mf->ReadListFile(0,ifpath.c_str());
  224. // for old versions of CMake ListFiles
  225. const char* versionValue
  226. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  227. if (atof(versionValue) <= 1.4)
  228. {
  229. ifpath = root + "/Modules/CMakeBackwardCompatibilityC.cmake";
  230. mf->ReadListFile(0,ifpath.c_str());
  231. }
  232. }
  233. }
  234. if(needCXXBackwards)
  235. {
  236. if (!m_CMakeInstance->GetIsInTryCompile())
  237. {
  238. std::string ifpath = root + "/Modules/CMakeTestCXXCompiler.cmake";
  239. mf->ReadListFile(0,ifpath.c_str());
  240. // for old versions of CMake ListFiles
  241. const char* versionValue
  242. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  243. if (atof(versionValue) <= 1.4)
  244. {
  245. std::string nfpath = root + "/Modules/CMakeBackwardCompatibilityCXX.cmake";
  246. mf->ReadListFile(0,nfpath.c_str());
  247. }
  248. }
  249. }
  250. // if we are from the top, always define this
  251. mf->AddDefinition("RUN_CONFIGURE", true);
  252. }
  253. }
  254. void cmGlobalGenerator::SetLanguageEnabled(const char* l)
  255. {
  256. m_LanguageEnabled[l] = true;
  257. }
  258. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  259. {
  260. return (m_LanguageEnabled.count(l) > 0);
  261. }
  262. void cmGlobalGenerator::ClearEnabledLanguages()
  263. {
  264. m_LanguageEnabled.clear();
  265. }
  266. void cmGlobalGenerator::Configure()
  267. {
  268. // Delete any existing cmLocalGenerators
  269. unsigned int i;
  270. for (i = 0; i < m_LocalGenerators.size(); ++i)
  271. {
  272. delete m_LocalGenerators[i];
  273. }
  274. m_LocalGenerators.clear();
  275. // start with this directory
  276. cmLocalGenerator *lg = this->CreateLocalGenerator();
  277. m_LocalGenerators.push_back(lg);
  278. // set the Start directories
  279. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  280. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  281. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  282. // now do it
  283. this->RecursiveConfigure(lg,0.0f,0.9f);
  284. std::set<std::string> notFoundMap;
  285. // after it is all done do a ConfigureFinalPass
  286. cmCacheManager* manager = 0;
  287. for (i = 0; i < m_LocalGenerators.size(); ++i)
  288. {
  289. manager = m_LocalGenerators[i]->GetMakefile()->GetCacheManager();
  290. m_LocalGenerators[i]->ConfigureFinalPass();
  291. cmTargets const& targets = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  292. for (cmTargets::const_iterator l = targets.begin();
  293. l != targets.end(); l++)
  294. {
  295. cmTarget::LinkLibraries libs = l->second.GetLinkLibraries();
  296. for(cmTarget::LinkLibraries::iterator lib = libs.begin();
  297. lib != libs.end(); ++lib)
  298. {
  299. if(lib->first.size() > 9 &&
  300. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  301. {
  302. std::string varName = lib->first.substr(0, lib->first.size()-9);
  303. notFoundMap.insert(varName);
  304. }
  305. }
  306. std::vector<std::string>& incs =
  307. m_LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  308. for( std::vector<std::string>::iterator lib = incs.begin();
  309. lib != incs.end(); ++lib)
  310. {
  311. if(lib->size() > 9 &&
  312. cmSystemTools::IsNOTFOUND(lib->c_str()))
  313. {
  314. std::string varName = lib->substr(0, lib->size()-9);
  315. notFoundMap.insert(varName);
  316. }
  317. }
  318. m_CMakeInstance->UpdateProgress("Configuring",
  319. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  320. }
  321. }
  322. if(notFoundMap.size())
  323. {
  324. std::string notFoundVars;
  325. for(std::set<std::string>::iterator ii = notFoundMap.begin();
  326. ii != notFoundMap.end(); ++ii)
  327. {
  328. notFoundVars += *ii;
  329. if(manager)
  330. {
  331. cmCacheManager::CacheIterator it =
  332. manager->GetCacheIterator(ii->c_str());
  333. if(it.GetPropertyAsBool("ADVANCED"))
  334. {
  335. notFoundVars += " (ADVANCED)";
  336. }
  337. }
  338. notFoundVars += "\n";
  339. }
  340. cmSystemTools::Error("This project requires some variables to be set,\n"
  341. "and cmake can not find them.\n"
  342. "Please set the following variables:\n",
  343. notFoundVars.c_str());
  344. }
  345. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  346. }
  347. // loop through the directories creating cmLocalGenerators and Configure()
  348. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  349. float startProgress,
  350. float endProgress)
  351. {
  352. // configure the current directory
  353. lg->Configure();
  354. // get all the subdirectories
  355. std::vector<std::string> subdirs = lg->GetMakefile()->GetSubDirectories();
  356. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  357. m_CMakeInstance->UpdateProgress("Configuring",
  358. startProgress + progressPiece);
  359. // for each subdir recurse
  360. unsigned int i;
  361. for (i = 0; i < subdirs.size(); ++i)
  362. {
  363. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  364. m_LocalGenerators.push_back(lg2);
  365. // add the subdir to the start output directory
  366. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  367. outdir += "/";
  368. outdir += subdirs[i];
  369. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  370. // add the subdir to the start source directory
  371. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  372. currentDir += "/";
  373. currentDir += subdirs[i];
  374. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  375. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  376. this->RecursiveConfigure(lg2,
  377. startProgress + (i+1.0f)*progressPiece,
  378. startProgress + (i+2.0f)*progressPiece);
  379. }
  380. }
  381. void cmGlobalGenerator::Generate()
  382. {
  383. // For each existing cmLocalGenerator
  384. unsigned int i;
  385. for (i = 0; i < m_LocalGenerators.size(); ++i)
  386. {
  387. m_LocalGenerators[i]->Generate(true);
  388. m_CMakeInstance->UpdateProgress("Generating",
  389. (i+1.0f)/m_LocalGenerators.size());
  390. }
  391. m_CMakeInstance->UpdateProgress("Generating done", -1);
  392. }
  393. void cmGlobalGenerator::LocalGenerate()
  394. {
  395. // for this case we create one LocalGenerator
  396. // configure it, and then Generate it
  397. // start with this directory
  398. cmLocalGenerator *lg = this->CreateLocalGenerator();
  399. // set the Start directories
  400. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  401. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  402. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  403. // now do trhe configure
  404. lg->Configure();
  405. lg->ConfigureFinalPass();
  406. lg->Generate(false);
  407. delete lg;
  408. }
  409. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  410. const char *, const char *target,
  411. std::string *output)
  412. {
  413. // now build the test
  414. std::string makeCommand =
  415. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  416. if(makeCommand.size() == 0)
  417. {
  418. cmSystemTools::Error(
  419. "Generator cannot find the appropriate make command.");
  420. return 1;
  421. }
  422. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  423. /**
  424. * Run an executable command and put the stdout in output.
  425. */
  426. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  427. cmSystemTools::ChangeDirectory(bindir);
  428. // Since we have full control over the invocation of nmake, let us
  429. // make it quiet.
  430. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  431. {
  432. makeCommand += " /NOLOGO ";
  433. }
  434. // now build
  435. if (target)
  436. {
  437. makeCommand += " ";
  438. makeCommand += target;
  439. #if defined(_WIN32) || defined(__CYGWIN__)
  440. makeCommand += ".exe";
  441. #endif // WIN32
  442. }
  443. else
  444. {
  445. makeCommand += " all";
  446. }
  447. int retVal;
  448. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  449. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  450. &retVal, 0, false, timeout))
  451. {
  452. cmSystemTools::Error("Generator: execution of make failed.");
  453. // return to the original directory
  454. cmSystemTools::ChangeDirectory(cwd.c_str());
  455. return 1;
  456. }
  457. // The SGI MipsPro 7.3 compiler does not return an error code when
  458. // the source has a #error in it! This is a work-around for such
  459. // compilers.
  460. if((retVal == 0) && (output->find("#error") != std::string::npos))
  461. {
  462. retVal = 1;
  463. }
  464. cmSystemTools::ChangeDirectory(cwd.c_str());
  465. return retVal;
  466. }
  467. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  468. {
  469. cmLocalGenerator *lg = new cmLocalGenerator;
  470. lg->SetGlobalGenerator(this);
  471. return lg;
  472. }
  473. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  474. {
  475. this->SetConfiguredFilesPath(
  476. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  477. const char* make =
  478. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  479. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  480. "make program",
  481. cmCacheManager::FILEPATH);
  482. // if C, then enable C
  483. if(gen->GetLanguageEnabled("C"))
  484. {
  485. this->SetLanguageEnabled("C");
  486. }
  487. // if CXX
  488. if(gen->GetLanguageEnabled("CXX"))
  489. {
  490. this->SetLanguageEnabled("CXX");
  491. }
  492. }
  493. //----------------------------------------------------------------------------
  494. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  495. {
  496. entry.name = this->GetName();
  497. entry.brief = "";
  498. entry.full = "";
  499. }