cmGlobalGenerator.cxx 18 KB

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