cmGlobalGenerator.cxx 17 KB

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