cmGlobalGenerator.cxx 16 KB

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