cmGlobalGenerator.cxx 17 KB

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