cmGlobalGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. // see man putenv for explaination of this stupid code...
  128. if(mf->GetDefinition("CMAKE_C_COMPILER"))
  129. {
  130. static char envCC[5000];
  131. std::string env = "CC=${CMAKE_C_COMPILER}";
  132. mf->ExpandVariablesInString(env);
  133. unsigned int size = static_cast<unsigned int>(env.size());
  134. if(size > 4999)
  135. {
  136. size = 4999;
  137. }
  138. strncpy(envCC, env.c_str(), size);
  139. envCC[size] = 0;
  140. putenv(envCC);
  141. }
  142. }
  143. // check for a CXX compiler and configure it
  144. if(!isLocal &&
  145. !this->GetLanguageEnabled("CXX") &&
  146. strcmp(lang, "CXX") == 0)
  147. {
  148. needCXXBackwards = true;
  149. std::string determineCFile = root;
  150. determineCFile += "/Modules/CMakeDetermineCXXCompiler.cmake";
  151. mf->ReadListFile(0,determineCFile.c_str());
  152. this->SetLanguageEnabled("CXX");
  153. // put CXX in the environment in case user scripts want
  154. // to run configure
  155. // see man putenv for explaination of this stupid code...
  156. static char envCXX[5000];
  157. if(mf->GetDefinition("CMAKE_CXX_COMPILER"))
  158. {
  159. std::string env = "CXX=${CMAKE_CXX_COMPILER}";
  160. mf->ExpandVariablesInString(env);
  161. unsigned int size = static_cast<unsigned int>(env.size());
  162. if(size > 4999)
  163. {
  164. size = 4999;
  165. }
  166. strncpy(envCXX, env.c_str(), size);
  167. envCXX[4999] = 0;
  168. putenv(envCXX);
  169. }
  170. }
  171. // check for a Java compiler and configure it
  172. if(!isLocal &&
  173. !this->GetLanguageEnabled("JAVA") &&
  174. strcmp(lang, "JAVA") == 0)
  175. {
  176. std::string determineCFile = root;
  177. determineCFile += "/Modules/CMakeDetermineJavaCompiler.cmake";
  178. mf->ReadListFile(0,determineCFile.c_str());
  179. this->SetLanguageEnabled("JAVA");
  180. }
  181. std::string fpath = rootBin;
  182. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  183. {
  184. fpath += "/CMakeSystem.cmake";
  185. mf->ReadListFile(0,fpath.c_str());
  186. }
  187. // if C, then enable C
  188. if(lang[0] == 'C' && !mf->GetDefinition("CMAKE_C_COMPILER_LOADED"))
  189. {
  190. fpath = rootBin;
  191. fpath += "/CMakeCCompiler.cmake";
  192. mf->ReadListFile(0,fpath.c_str());
  193. this->SetLanguageEnabled("C");
  194. }
  195. if(strcmp(lang, "CXX") == 0 && !mf->GetDefinition("CMAKE_CXX_COMPILER_LOADED"))
  196. {
  197. fpath = rootBin;
  198. fpath += "/CMakeCXXCompiler.cmake";
  199. mf->ReadListFile(0,fpath.c_str());
  200. this->SetLanguageEnabled("CXX");
  201. }
  202. if(strcmp(lang, "JAVA") == 0 && !mf->GetDefinition("CMAKE_JAVA_COMPILER_LOADED"))
  203. {
  204. fpath = rootBin;
  205. fpath += "/CMakeJavaCompiler.cmake";
  206. mf->ReadListFile(0,fpath.c_str());
  207. this->SetLanguageEnabled("JAVA");
  208. }
  209. if ( lang[0] == 'C' && !mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  210. {
  211. fpath = root;
  212. fpath += "/Modules/CMakeSystemSpecificInformation.cmake";
  213. mf->ReadListFile(0,fpath.c_str());
  214. }
  215. if(!isLocal)
  216. {
  217. // At this point we should have enough info for a try compile
  218. // which is used in the backward stuff
  219. if(needCBackwards)
  220. {
  221. if (!m_CMakeInstance->GetIsInTryCompile())
  222. {
  223. std::string ifpath = root + "/Modules/CMakeTestCCompiler.cmake";
  224. mf->ReadListFile(0,ifpath.c_str());
  225. // for old versions of CMake ListFiles
  226. const char* versionValue
  227. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  228. if (atof(versionValue) <= 1.4)
  229. {
  230. ifpath = root + "/Modules/CMakeBackwardCompatibilityC.cmake";
  231. mf->ReadListFile(0,ifpath.c_str());
  232. }
  233. }
  234. }
  235. if(needCXXBackwards)
  236. {
  237. if (!m_CMakeInstance->GetIsInTryCompile())
  238. {
  239. std::string ifpath = root + "/Modules/CMakeTestCXXCompiler.cmake";
  240. mf->ReadListFile(0,ifpath.c_str());
  241. // for old versions of CMake ListFiles
  242. const char* versionValue
  243. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  244. if (atof(versionValue) <= 1.4)
  245. {
  246. std::string nfpath = root + "/Modules/CMakeBackwardCompatibilityCXX.cmake";
  247. mf->ReadListFile(0,nfpath.c_str());
  248. }
  249. }
  250. }
  251. // if we are from the top, always define this
  252. mf->AddDefinition("RUN_CONFIGURE", true);
  253. }
  254. }
  255. void cmGlobalGenerator::SetLanguageEnabled(const char* l)
  256. {
  257. m_LanguageEnabled[l] = true;
  258. }
  259. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  260. {
  261. return (m_LanguageEnabled.count(l) > 0);
  262. }
  263. void cmGlobalGenerator::ClearEnabledLanguages()
  264. {
  265. m_LanguageEnabled.clear();
  266. }
  267. void cmGlobalGenerator::Configure()
  268. {
  269. // Delete any existing cmLocalGenerators
  270. unsigned int i;
  271. for (i = 0; i < m_LocalGenerators.size(); ++i)
  272. {
  273. delete m_LocalGenerators[i];
  274. }
  275. m_LocalGenerators.clear();
  276. // start with this directory
  277. cmLocalGenerator *lg = this->CreateLocalGenerator();
  278. m_LocalGenerators.push_back(lg);
  279. // set the Start directories
  280. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  281. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  282. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  283. // now do it
  284. this->RecursiveConfigure(lg,0.0f,0.9f);
  285. std::set<std::string> notFoundMap;
  286. // after it is all done do a ConfigureFinalPass
  287. cmCacheManager* manager = 0;
  288. for (i = 0; i < m_LocalGenerators.size(); ++i)
  289. {
  290. manager = m_LocalGenerators[i]->GetMakefile()->GetCacheManager();
  291. m_LocalGenerators[i]->ConfigureFinalPass();
  292. cmTargets const& targets = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  293. for (cmTargets::const_iterator l = targets.begin();
  294. l != targets.end(); l++)
  295. {
  296. cmTarget::LinkLibraries libs = l->second.GetLinkLibraries();
  297. for(cmTarget::LinkLibraries::iterator lib = libs.begin();
  298. lib != libs.end(); ++lib)
  299. {
  300. if(lib->first.size() > 9 &&
  301. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  302. {
  303. std::string varName = lib->first.substr(0, lib->first.size()-9);
  304. notFoundMap.insert(varName);
  305. }
  306. }
  307. std::vector<std::string>& incs =
  308. m_LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  309. for( std::vector<std::string>::iterator lib = incs.begin();
  310. lib != incs.end(); ++lib)
  311. {
  312. if(lib->size() > 9 &&
  313. cmSystemTools::IsNOTFOUND(lib->c_str()))
  314. {
  315. std::string varName = lib->substr(0, lib->size()-9);
  316. notFoundMap.insert(varName);
  317. }
  318. }
  319. m_CMakeInstance->UpdateProgress("Configuring",
  320. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  321. }
  322. }
  323. if(notFoundMap.size())
  324. {
  325. std::string notFoundVars;
  326. for(std::set<std::string>::iterator ii = notFoundMap.begin();
  327. ii != notFoundMap.end(); ++ii)
  328. {
  329. notFoundVars += *ii;
  330. if(manager)
  331. {
  332. cmCacheManager::CacheIterator it =
  333. manager->GetCacheIterator(ii->c_str());
  334. if(it.GetPropertyAsBool("ADVANCED"))
  335. {
  336. notFoundVars += " (ADVANCED)";
  337. }
  338. }
  339. notFoundVars += "\n";
  340. }
  341. cmSystemTools::Error("This project requires some variables to be set,\n"
  342. "and cmake can not find them.\n"
  343. "Please set the following variables:\n",
  344. notFoundVars.c_str());
  345. }
  346. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  347. }
  348. // loop through the directories creating cmLocalGenerators and Configure()
  349. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  350. float startProgress,
  351. float endProgress)
  352. {
  353. // configure the current directory
  354. lg->Configure();
  355. // get all the subdirectories
  356. std::vector<std::string> subdirs = lg->GetMakefile()->GetSubDirectories();
  357. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  358. m_CMakeInstance->UpdateProgress("Configuring",
  359. startProgress + progressPiece);
  360. // for each subdir recurse
  361. unsigned int i;
  362. for (i = 0; i < subdirs.size(); ++i)
  363. {
  364. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  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];
  370. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  371. // add the subdir to the start source directory
  372. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  373. currentDir += "/";
  374. currentDir += subdirs[i];
  375. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  376. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  377. this->RecursiveConfigure(lg2,
  378. startProgress + (i+1.0f)*progressPiece,
  379. startProgress + (i+2.0f)*progressPiece);
  380. }
  381. }
  382. void cmGlobalGenerator::Generate()
  383. {
  384. // For each existing cmLocalGenerator
  385. unsigned int i;
  386. for (i = 0; i < m_LocalGenerators.size(); ++i)
  387. {
  388. m_LocalGenerators[i]->Generate(true);
  389. m_CMakeInstance->UpdateProgress("Generating",
  390. (i+1.0f)/m_LocalGenerators.size());
  391. }
  392. m_CMakeInstance->UpdateProgress("Generating done", -1);
  393. }
  394. void cmGlobalGenerator::LocalGenerate()
  395. {
  396. // for this case we create one LocalGenerator
  397. // configure it, and then Generate it
  398. // start with this directory
  399. cmLocalGenerator *lg = this->CreateLocalGenerator();
  400. // set the Start directories
  401. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  402. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  403. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  404. // now do trhe configure
  405. lg->Configure();
  406. lg->ConfigureFinalPass();
  407. lg->Generate(false);
  408. delete lg;
  409. }
  410. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  411. const char *, const char *target,
  412. std::string *output)
  413. {
  414. // now build the test
  415. std::string makeCommand =
  416. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  417. if(makeCommand.size() == 0)
  418. {
  419. cmSystemTools::Error(
  420. "Generator cannot find the appropriate make command.");
  421. return 1;
  422. }
  423. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  424. /**
  425. * Run an executable command and put the stdout in output.
  426. */
  427. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  428. cmSystemTools::ChangeDirectory(bindir);
  429. // Since we have full control over the invocation of nmake, let us
  430. // make it quiet.
  431. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  432. {
  433. makeCommand += " /NOLOGO ";
  434. }
  435. // now build
  436. if (target)
  437. {
  438. makeCommand += " ";
  439. makeCommand += target;
  440. #if defined(_WIN32) || defined(__CYGWIN__)
  441. makeCommand += ".exe";
  442. #endif // WIN32
  443. }
  444. else
  445. {
  446. makeCommand += " all";
  447. }
  448. int retVal;
  449. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  450. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  451. &retVal, 0, false, timeout))
  452. {
  453. cmSystemTools::Error("Generator: execution of make failed.");
  454. // return to the original directory
  455. cmSystemTools::ChangeDirectory(cwd.c_str());
  456. return 1;
  457. }
  458. // The SGI MipsPro 7.3 compiler does not return an error code when
  459. // the source has a #error in it! This is a work-around for such
  460. // compilers.
  461. if((retVal == 0) && (output->find("#error") != std::string::npos))
  462. {
  463. retVal = 1;
  464. }
  465. cmSystemTools::ChangeDirectory(cwd.c_str());
  466. return retVal;
  467. }
  468. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  469. {
  470. cmLocalGenerator *lg = new cmLocalGenerator;
  471. lg->SetGlobalGenerator(this);
  472. return lg;
  473. }
  474. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  475. {
  476. this->SetConfiguredFilesPath(
  477. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  478. const char* make =
  479. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  480. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  481. "make program",
  482. cmCacheManager::FILEPATH);
  483. // if C, then enable C
  484. if(gen->GetLanguageEnabled("C"))
  485. {
  486. this->SetLanguageEnabled("C");
  487. }
  488. // if CXX
  489. if(gen->GetLanguageEnabled("CXX"))
  490. {
  491. this->SetLanguageEnabled("CXX");
  492. }
  493. }
  494. //----------------------------------------------------------------------------
  495. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  496. {
  497. entry.name = this->GetName();
  498. entry.brief = "";
  499. entry.full = "";
  500. }