cmGlobalGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. m_LocalGenerators[i]->GetMakefile()->CheckInfiniteLoops();
  304. }
  305. }
  306. if(notFoundMap.size())
  307. {
  308. std::string notFoundVars;
  309. for(std::set<std::string>::iterator ii = notFoundMap.begin();
  310. ii != notFoundMap.end(); ++ii)
  311. {
  312. notFoundVars += *ii;
  313. if(manager)
  314. {
  315. cmCacheManager::CacheIterator it =
  316. manager->GetCacheIterator(ii->c_str());
  317. if(it.GetPropertyAsBool("ADVANCED"))
  318. {
  319. notFoundVars += " (ADVANCED)";
  320. }
  321. }
  322. notFoundVars += "\n";
  323. }
  324. cmSystemTools::Error("This project requires some variables to be set,\n"
  325. "and cmake can not find them.\n"
  326. "Please set the following variables:\n",
  327. notFoundVars.c_str());
  328. }
  329. if ( !m_CMakeInstance->GetScriptMode() )
  330. {
  331. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  332. }
  333. }
  334. // loop through the directories creating cmLocalGenerators and Configure()
  335. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  336. float startProgress,
  337. float endProgress)
  338. {
  339. // configure the current directory
  340. lg->Configure();
  341. // get all the subdirectories
  342. std::vector<std::pair<cmStdString, bool> > subdirs = lg->GetMakefile()->GetSubDirectories();
  343. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  344. m_CMakeInstance->UpdateProgress("Configuring",
  345. startProgress + progressPiece);
  346. // for each subdir recurse
  347. unsigned int i;
  348. for (i = 0; i < subdirs.size(); ++i)
  349. {
  350. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  351. lg2->SetParent(lg);
  352. m_LocalGenerators.push_back(lg2);
  353. // add the subdir to the start output directory
  354. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  355. outdir += "/";
  356. outdir += subdirs[i].first;
  357. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  358. lg2->SetExcludeAll(!subdirs[i].second);
  359. // add the subdir to the start source directory
  360. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  361. currentDir += "/";
  362. currentDir += subdirs[i].first;
  363. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  364. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  365. this->RecursiveConfigure(lg2,
  366. startProgress + (i+1.0f)*progressPiece,
  367. startProgress + (i+2.0f)*progressPiece);
  368. }
  369. }
  370. void cmGlobalGenerator::Generate()
  371. {
  372. // For each existing cmLocalGenerator
  373. unsigned int i;
  374. for (i = 0; i < m_LocalGenerators.size(); ++i)
  375. {
  376. m_LocalGenerators[i]->Generate(true);
  377. m_LocalGenerators[i]->GenerateInstallRules();
  378. m_CMakeInstance->UpdateProgress("Generating",
  379. (i+1.0f)/m_LocalGenerators.size());
  380. }
  381. m_CMakeInstance->UpdateProgress("Generating done", -1);
  382. }
  383. void cmGlobalGenerator::LocalGenerate()
  384. {
  385. // for this case we create one LocalGenerator
  386. // configure it, and then Generate it
  387. // start with this directory
  388. cmLocalGenerator *lg = this->CreateLocalGenerator();
  389. // set the Start directories
  390. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  391. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  392. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  393. // now do trhe configure
  394. lg->Configure();
  395. lg->ConfigureFinalPass();
  396. lg->Generate(false);
  397. delete lg;
  398. }
  399. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  400. const char *, const char *target,
  401. std::string *output)
  402. {
  403. // now build the test
  404. std::string makeCommand =
  405. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  406. if(makeCommand.size() == 0)
  407. {
  408. cmSystemTools::Error(
  409. "Generator cannot find the appropriate make command.");
  410. return 1;
  411. }
  412. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  413. /**
  414. * Run an executable command and put the stdout in output.
  415. */
  416. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  417. cmSystemTools::ChangeDirectory(bindir);
  418. // Since we have full control over the invocation of nmake, let us
  419. // make it quiet.
  420. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  421. {
  422. makeCommand += " /NOLOGO ";
  423. }
  424. // now build
  425. if (target)
  426. {
  427. makeCommand += " ";
  428. makeCommand += target;
  429. #if defined(_WIN32) || defined(__CYGWIN__)
  430. std::string tmp = target;
  431. // if the target does not already end in . something
  432. // then assume .exe
  433. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  434. {
  435. makeCommand += ".exe";
  436. }
  437. #endif // WIN32
  438. }
  439. else
  440. {
  441. makeCommand += " all";
  442. }
  443. int retVal;
  444. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  445. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  446. &retVal, 0, false, timeout))
  447. {
  448. cmSystemTools::Error("Generator: execution of make failed.");
  449. // return to the original directory
  450. cmSystemTools::ChangeDirectory(cwd.c_str());
  451. return 1;
  452. }
  453. // The SGI MipsPro 7.3 compiler does not return an error code when
  454. // the source has a #error in it! This is a work-around for such
  455. // compilers.
  456. if((retVal == 0) && (output->find("#error") != std::string::npos))
  457. {
  458. retVal = 1;
  459. }
  460. cmSystemTools::ChangeDirectory(cwd.c_str());
  461. return retVal;
  462. }
  463. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  464. {
  465. cmLocalGenerator *lg = new cmLocalGenerator;
  466. lg->SetGlobalGenerator(this);
  467. return lg;
  468. }
  469. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  470. {
  471. this->SetConfiguredFilesPath(
  472. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  473. const char* make =
  474. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  475. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  476. "make program",
  477. cmCacheManager::FILEPATH);
  478. // if C, then enable C
  479. if(gen->GetLanguageEnabled("C"))
  480. {
  481. this->SetLanguageEnabled("C");
  482. }
  483. // if CXX
  484. if(gen->GetLanguageEnabled("CXX"))
  485. {
  486. this->SetLanguageEnabled("CXX");
  487. }
  488. }
  489. //----------------------------------------------------------------------------
  490. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  491. {
  492. entry.name = this->GetName();
  493. entry.brief = "";
  494. entry.full = "";
  495. }
  496. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  497. cmLocalGenerator* gen)
  498. {
  499. cmLocalGenerator* cur = gen->GetParent();
  500. while(cur && cur != root)
  501. {
  502. if(cur->GetExcludeAll())
  503. {
  504. return true;
  505. }
  506. cur = cur->GetParent();
  507. }
  508. return false;
  509. }