cmGlobalGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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::string> 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. m_LocalGenerators.push_back(lg2);
  351. // add the subdir to the start output directory
  352. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  353. outdir += "/";
  354. outdir += subdirs[i];
  355. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  356. // add the subdir to the start source directory
  357. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  358. currentDir += "/";
  359. currentDir += subdirs[i];
  360. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  361. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  362. this->RecursiveConfigure(lg2,
  363. startProgress + (i+1.0f)*progressPiece,
  364. startProgress + (i+2.0f)*progressPiece);
  365. }
  366. }
  367. void cmGlobalGenerator::Generate()
  368. {
  369. // For each existing cmLocalGenerator
  370. unsigned int i;
  371. for (i = 0; i < m_LocalGenerators.size(); ++i)
  372. {
  373. m_LocalGenerators[i]->Generate(true);
  374. m_LocalGenerators[i]->GenerateInstallRules();
  375. m_CMakeInstance->UpdateProgress("Generating",
  376. (i+1.0f)/m_LocalGenerators.size());
  377. }
  378. m_CMakeInstance->UpdateProgress("Generating done", -1);
  379. }
  380. void cmGlobalGenerator::LocalGenerate()
  381. {
  382. // for this case we create one LocalGenerator
  383. // configure it, and then Generate it
  384. // start with this directory
  385. cmLocalGenerator *lg = this->CreateLocalGenerator();
  386. // set the Start directories
  387. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  388. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  389. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  390. // now do trhe configure
  391. lg->Configure();
  392. lg->ConfigureFinalPass();
  393. lg->Generate(false);
  394. delete lg;
  395. }
  396. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  397. const char *, const char *target,
  398. std::string *output)
  399. {
  400. // now build the test
  401. std::string makeCommand =
  402. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  403. if(makeCommand.size() == 0)
  404. {
  405. cmSystemTools::Error(
  406. "Generator cannot find the appropriate make command.");
  407. return 1;
  408. }
  409. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  410. /**
  411. * Run an executable command and put the stdout in output.
  412. */
  413. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  414. cmSystemTools::ChangeDirectory(bindir);
  415. // Since we have full control over the invocation of nmake, let us
  416. // make it quiet.
  417. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  418. {
  419. makeCommand += " /NOLOGO ";
  420. }
  421. // now build
  422. if (target)
  423. {
  424. makeCommand += " ";
  425. makeCommand += target;
  426. #if defined(_WIN32) || defined(__CYGWIN__)
  427. std::string tmp = target;
  428. // if the target does not already end in . something
  429. // then assume .exe
  430. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  431. {
  432. makeCommand += ".exe";
  433. }
  434. #endif // WIN32
  435. }
  436. else
  437. {
  438. makeCommand += " all";
  439. }
  440. int retVal;
  441. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  442. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  443. &retVal, 0, false, timeout))
  444. {
  445. cmSystemTools::Error("Generator: execution of make failed.");
  446. // return to the original directory
  447. cmSystemTools::ChangeDirectory(cwd.c_str());
  448. return 1;
  449. }
  450. // The SGI MipsPro 7.3 compiler does not return an error code when
  451. // the source has a #error in it! This is a work-around for such
  452. // compilers.
  453. if((retVal == 0) && (output->find("#error") != std::string::npos))
  454. {
  455. retVal = 1;
  456. }
  457. cmSystemTools::ChangeDirectory(cwd.c_str());
  458. return retVal;
  459. }
  460. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  461. {
  462. cmLocalGenerator *lg = new cmLocalGenerator;
  463. lg->SetGlobalGenerator(this);
  464. return lg;
  465. }
  466. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  467. {
  468. this->SetConfiguredFilesPath(
  469. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  470. const char* make =
  471. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  472. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  473. "make program",
  474. cmCacheManager::FILEPATH);
  475. // if C, then enable C
  476. if(gen->GetLanguageEnabled("C"))
  477. {
  478. this->SetLanguageEnabled("C");
  479. }
  480. // if CXX
  481. if(gen->GetLanguageEnabled("CXX"))
  482. {
  483. this->SetLanguageEnabled("CXX");
  484. }
  485. }
  486. //----------------------------------------------------------------------------
  487. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  488. {
  489. entry.name = this->GetName();
  490. entry.brief = "";
  491. entry.full = "";
  492. }