cmGlobalGenerator.cxx 16 KB

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