cmGlobalGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. // Find the make program for the generator, required for try compiles
  38. void cmGlobalGenerator::FindMakeProgram(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. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  47. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  48. {
  49. std::string setMakeProgram = mf->GetModulesFile(m_FindMakeProgramFile.c_str());
  50. if(setMakeProgram.size())
  51. {
  52. mf->ReadListFile(0, setMakeProgram.c_str());
  53. }
  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->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  67. // if there are spaces in the make program use short path
  68. // but do not short path the actual program name, as
  69. // this can cause trouble with VSExpress
  70. if(makeProgram.find(' ') != makeProgram.npos)
  71. {
  72. std::string dir;
  73. std::string file;
  74. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  75. dir, file);
  76. std::string saveFile = file;
  77. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  78. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  79. dir, file);
  80. makeProgram = dir;
  81. makeProgram += "/";
  82. makeProgram += saveFile;
  83. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  84. "make program",
  85. cmCacheManager::FILEPATH);
  86. }
  87. }
  88. // enable the given language
  89. void cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  90. cmMakefile *mf)
  91. {
  92. if(languages.size() == 0)
  93. {
  94. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  95. cmSystemTools::SetFatalErrorOccured();
  96. return;
  97. }
  98. // setup some variables for the EnableLanguage function
  99. bool isLocal = m_CMakeInstance->GetLocal();
  100. // if we are from the top, always define this
  101. if(!isLocal)
  102. {
  103. mf->AddDefinition("RUN_CONFIGURE", true);
  104. }
  105. bool needTestLanguage = false;
  106. std::string rootBin = mf->GetHomeOutputDirectory();
  107. // If the configuration files path has been set,
  108. // then we are in a try compile and need to copy the enable language
  109. // files into the try compile directory
  110. if(m_ConfiguredFilesPath.size())
  111. {
  112. std::string src = m_ConfiguredFilesPath;
  113. src += "/CMakeSystem.cmake";
  114. std::string dst = rootBin;
  115. dst += "/CMakeSystem.cmake";
  116. cmSystemTools::CopyFileIfDifferent(src.c_str(), dst.c_str());
  117. for(std::vector<std::string>::const_iterator l = languages.begin();
  118. l != languages.end(); ++l)
  119. {
  120. const char* lang = l->c_str();
  121. std::string src2 = m_ConfiguredFilesPath;
  122. src2 += "/CMake";
  123. src2 += lang;
  124. src2 += "Compiler.cmake";
  125. std::string dst2 = rootBin;
  126. dst2 += "/CMake";
  127. dst2 += lang;
  128. dst2 += "Compiler.cmake";
  129. cmSystemTools::CopyFileIfDifferent(src2.c_str(), dst2.c_str());
  130. src2 = m_ConfiguredFilesPath;
  131. src2 += "/CMake";
  132. src2 += lang;
  133. src2 += "Platform.cmake";
  134. dst2 = rootBin;
  135. dst2 += "/CMake";
  136. dst2 += lang;
  137. dst2 += "Platform.cmake";
  138. cmSystemTools::CopyFileIfDifferent(src2.c_str(), dst2.c_str());
  139. }
  140. rootBin = m_ConfiguredFilesPath;
  141. }
  142. // **** Step 1, find and make sure CMAKE_MAKE_PROGRAM is defined
  143. this->FindMakeProgram(mf);
  144. // try and load the CMakeSystem.cmake if it is there
  145. std::string fpath = rootBin;
  146. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  147. {
  148. fpath += "/CMakeSystem.cmake";
  149. if(cmSystemTools::FileExists(fpath.c_str()))
  150. {
  151. mf->ReadListFile(0,fpath.c_str());
  152. }
  153. }
  154. // **** Step 2, Load the CMakeDetermineSystem.cmake file and find out
  155. // what platform we are running on
  156. if (!isLocal && !mf->GetDefinition("CMAKE_SYSTEM_NAME"))
  157. {
  158. #if defined(_WIN32) && !defined(__CYGWIN__)
  159. /* Windows version number data. */
  160. OSVERSIONINFO osvi;
  161. ZeroMemory(&osvi, sizeof(osvi));
  162. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  163. GetVersionEx (&osvi);
  164. cmOStringStream windowsVersionString;
  165. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  166. windowsVersionString.str();
  167. mf->AddDefinition("CMAKE_SYSTEM_VERSION", windowsVersionString.str().c_str());
  168. #endif
  169. // Read the DetermineSystem file
  170. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  171. mf->ReadListFile(0, systemFile.c_str());
  172. }
  173. // **** Step 3, load the CMakeSystem.cmake from the binary directory
  174. // this file is configured by the CMakeDetermineSystem.cmake file
  175. fpath = rootBin;
  176. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  177. {
  178. fpath += "/CMakeSystem.cmake";
  179. mf->ReadListFile(0,fpath.c_str());
  180. }
  181. // **** Step 4, foreach language
  182. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  183. // the compiler
  184. for(std::vector<std::string>::const_iterator l = languages.begin();
  185. l != languages.end(); ++l)
  186. {
  187. const char* lang = l->c_str();
  188. if(!isLocal && !this->GetLanguageEnabled(lang) )
  189. {
  190. if (m_CMakeInstance->GetIsInTryCompile())
  191. {
  192. cmSystemTools::Error("This should not have happen. "
  193. "If you see this message, you are probably using a "
  194. "broken CMakeLists.txt file or a problematic release of "
  195. "CMake");
  196. }
  197. // try and load the configured file first
  198. std::string loadedLang = "CMAKE_";
  199. loadedLang += lang;
  200. loadedLang += "_COMPILER_LOADED";
  201. if(!mf->GetDefinition(loadedLang.c_str()))
  202. {
  203. fpath = rootBin;
  204. fpath += "/CMake";
  205. fpath += lang;
  206. fpath += "Compiler.cmake";
  207. if(cmSystemTools::FileExists(fpath.c_str()))
  208. {
  209. if(!mf->ReadListFile(0,fpath.c_str()))
  210. {
  211. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  212. }
  213. this->SetLanguageEnabled(lang, mf);
  214. }
  215. }
  216. needTestLanguage = true; // must test a language after finding it
  217. // read determine LANG compiler
  218. std::string determineCompiler = "CMakeDetermine";
  219. determineCompiler += lang;
  220. determineCompiler += "Compiler.cmake";
  221. std::string determineFile = mf->GetModulesFile(determineCompiler.c_str());
  222. if(!mf->ReadListFile(0,determineFile.c_str()))
  223. {
  224. cmSystemTools::Error("Could not find cmake module file:", determineFile.c_str());
  225. }
  226. // Some generators like visual studio should not use the env variables
  227. // So the global generator can specify that in this variable
  228. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  229. {
  230. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER into the
  231. // environment, in case user scripts want to run configure, or sub cmakes
  232. std::string compilerName = "CMAKE_";
  233. compilerName += lang;
  234. compilerName += "_COMPILER";
  235. std::string compilerEnv = "CMAKE_";
  236. compilerEnv += lang;
  237. compilerEnv += "_COMPILER_ENV_VAR";
  238. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  239. std::string envVarValue = mf->GetRequiredDefinition(compilerName.c_str());
  240. std::string env = envVar;
  241. env += "=";
  242. env += envVarValue;
  243. cmSystemTools::PutEnv(env.c_str());
  244. }
  245. }
  246. // **** Step 5, Load the configured language compiler file, if not loaded.
  247. // look to see if CMAKE_(LANG)_COMPILER_LOADED is set,
  248. // if not then load the CMake(LANG)Compiler.cmake file from the
  249. // binary tree, this is a configured file provided by
  250. // CMakeDetermine(LANG)Compiler.cmake
  251. std::string loadedLang = "CMAKE_";
  252. loadedLang += lang;
  253. loadedLang += "_COMPILER_LOADED";
  254. if(!mf->GetDefinition(loadedLang.c_str()))
  255. {
  256. fpath = rootBin;
  257. fpath += "/CMake";
  258. fpath += lang;
  259. fpath += "Compiler.cmake";
  260. if(!mf->ReadListFile(0,fpath.c_str()))
  261. {
  262. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  263. }
  264. this->SetLanguageEnabled(lang, mf);
  265. }
  266. }
  267. // **** Step 6, Load the system specific information if not yet loaded
  268. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  269. {
  270. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  271. if(!mf->ReadListFile(0,fpath.c_str()))
  272. {
  273. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  274. }
  275. }
  276. for(std::vector<std::string>::const_iterator l = languages.begin();
  277. l != languages.end(); ++l)
  278. {
  279. const char* lang = l->c_str();
  280. std::string langLoadedVar = "CMAKE_";
  281. langLoadedVar += lang;
  282. langLoadedVar += "_INFORMATION_LOADED";
  283. if (!mf->GetDefinition(langLoadedVar.c_str()))
  284. {
  285. fpath = "CMake";
  286. fpath += lang;
  287. fpath += "Information.cmake";
  288. fpath = mf->GetModulesFile(fpath.c_str());
  289. if(!mf->ReadListFile(0,fpath.c_str()))
  290. {
  291. cmSystemTools::Error("Could not find cmake module file:", fpath.c_str());
  292. }
  293. }
  294. // **** Step 7, Test the compiler for the language just setup
  295. // At this point we should have enough info for a try compile
  296. // which is used in the backward stuff
  297. if(!isLocal)
  298. {
  299. if(needTestLanguage)
  300. {
  301. if (!m_CMakeInstance->GetIsInTryCompile())
  302. {
  303. std::string testLang = "CMakeTest";
  304. testLang += lang;
  305. testLang += "Compiler.cmake";
  306. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  307. if(!mf->ReadListFile(0,ifpath.c_str()))
  308. {
  309. cmSystemTools::Error("Could not find cmake module file:", ifpath.c_str());
  310. }
  311. // **** Step 8, load backwards compatibility stuff for C and CXX
  312. // for old versions of CMake ListFiles C and CXX had some
  313. // backwards compatibility files they have to load
  314. const char* versionValue
  315. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  316. if (atof(versionValue) <= 1.4)
  317. {
  318. if(strcmp(lang, "C") == 0)
  319. {
  320. ifpath = mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  321. mf->ReadListFile(0,ifpath.c_str());
  322. }
  323. if(strcmp(lang, "CXX") == 0)
  324. {
  325. ifpath = mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  326. mf->ReadListFile(0,ifpath.c_str());
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. const char* cmGlobalGenerator::GetLanguageOutputExtensionForLanguage(const char* lang)
  335. {
  336. if(!lang)
  337. {
  338. return "";
  339. }
  340. if(m_LanguageToOutputExtension.count(lang) > 0)
  341. {
  342. return m_LanguageToOutputExtension[lang].c_str();
  343. }
  344. return "";
  345. }
  346. const char* cmGlobalGenerator::GetLanguageOutputExtensionFromExtension(const char* ext)
  347. {
  348. if(!ext)
  349. {
  350. return "";
  351. }
  352. const char* lang = this->GetLanguageFromExtension(ext);
  353. if(!lang || *lang == 0)
  354. {
  355. // if no language is found then check to see if it is already an
  356. // ouput extension for some language. In that case it should be ignored
  357. // and in this map, so it will not be compiled but will just be used.
  358. if(m_OutputExtensions.count(ext))
  359. {
  360. return ext;
  361. }
  362. }
  363. return this->GetLanguageOutputExtensionForLanguage(lang);
  364. }
  365. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  366. {
  367. // if there is an extension and it starts with . then
  368. // move past the . because the extensions are not stored with a .
  369. // in the map
  370. if(ext && *ext == '.')
  371. {
  372. ++ext;
  373. }
  374. if(m_ExtensionToLanguage.count(ext) > 0)
  375. {
  376. return m_ExtensionToLanguage[ext].c_str();
  377. }
  378. return 0;
  379. }
  380. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  381. {
  382. if(m_LanguageEnabled.count(l) > 0)
  383. {
  384. return;
  385. }
  386. std::string outputExtensionVar = std::string("CMAKE_") +
  387. std::string(l) + std::string("_OUTPUT_EXTENSION");
  388. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  389. if(outputExtension)
  390. {
  391. m_LanguageToOutputExtension[l] = outputExtension;
  392. m_OutputExtensions[outputExtension] = outputExtension;
  393. if(outputExtension[0] == '.')
  394. {
  395. m_OutputExtensions[outputExtension+1] = outputExtension+1;
  396. }
  397. }
  398. std::string linkerPrefVar = std::string("CMAKE_") +
  399. std::string(l) + std::string("_LINKER_PREFERENCE");
  400. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  401. if(!linkerPref)
  402. {
  403. linkerPref = "None";
  404. }
  405. m_LanguageToLinkerPreference[l] = linkerPref;
  406. std::string extensionsVar = std::string("CMAKE_") +
  407. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  408. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  409. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  410. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  411. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  412. std::vector<std::string> extensionList;
  413. cmSystemTools::ExpandListArgument(exts, extensionList);
  414. for(std::vector<std::string>::iterator i = extensionList.begin();
  415. i != extensionList.end(); ++i)
  416. {
  417. m_ExtensionToLanguage[*i] = l;
  418. }
  419. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  420. for(std::vector<std::string>::iterator i = extensionList.begin();
  421. i != extensionList.end(); ++i)
  422. {
  423. m_IgnoreExtensions[*i] = true;
  424. }
  425. m_LanguageEnabled[l] = true;
  426. }
  427. bool cmGlobalGenerator::IgnoreFile(const char* l)
  428. {
  429. if(this->GetLanguageFromExtension(l))
  430. {
  431. return false;
  432. }
  433. return (m_IgnoreExtensions.count(l) > 0);
  434. }
  435. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  436. {
  437. return (m_LanguageEnabled.count(l) > 0);
  438. }
  439. void cmGlobalGenerator::ClearEnabledLanguages()
  440. {
  441. m_LanguageEnabled.clear();
  442. }
  443. void cmGlobalGenerator::Configure()
  444. {
  445. // Delete any existing cmLocalGenerators
  446. unsigned int i;
  447. for (i = 0; i < m_LocalGenerators.size(); ++i)
  448. {
  449. delete m_LocalGenerators[i];
  450. }
  451. m_LocalGenerators.clear();
  452. // start with this directory
  453. cmLocalGenerator *lg = this->CreateLocalGenerator();
  454. m_LocalGenerators.push_back(lg);
  455. // set the Start directories
  456. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  457. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  458. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  459. // now do it
  460. this->RecursiveConfigure(lg,0.0f,0.9f);
  461. std::set<cmStdString> notFoundMap;
  462. // after it is all done do a ConfigureFinalPass
  463. cmCacheManager* manager = 0;
  464. for (i = 0; i < m_LocalGenerators.size(); ++i)
  465. {
  466. manager = m_LocalGenerators[i]->GetMakefile()->GetCacheManager();
  467. m_LocalGenerators[i]->ConfigureFinalPass();
  468. cmTargets const& targets = m_LocalGenerators[i]->GetMakefile()->GetTargets();
  469. for (cmTargets::const_iterator l = targets.begin();
  470. l != targets.end(); l++)
  471. {
  472. cmTarget::LinkLibraries libs = l->second.GetLinkLibraries();
  473. for(cmTarget::LinkLibraries::iterator lib = libs.begin();
  474. lib != libs.end(); ++lib)
  475. {
  476. if(lib->first.size() > 9 &&
  477. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  478. {
  479. std::string varName = lib->first.substr(0, lib->first.size()-9);
  480. notFoundMap.insert(varName);
  481. }
  482. }
  483. std::vector<std::string>& incs =
  484. m_LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  485. for( std::vector<std::string>::iterator lib = incs.begin();
  486. lib != incs.end(); ++lib)
  487. {
  488. if(lib->size() > 9 &&
  489. cmSystemTools::IsNOTFOUND(lib->c_str()))
  490. {
  491. std::string varName = lib->substr(0, lib->size()-9);
  492. notFoundMap.insert(varName);
  493. }
  494. }
  495. m_CMakeInstance->UpdateProgress("Configuring",
  496. 0.9f+0.1f*(i+1.0f)/m_LocalGenerators.size());
  497. m_LocalGenerators[i]->GetMakefile()->CheckInfiniteLoops();
  498. }
  499. }
  500. if(notFoundMap.size())
  501. {
  502. std::string notFoundVars;
  503. for(std::set<cmStdString>::iterator ii = notFoundMap.begin();
  504. ii != notFoundMap.end(); ++ii)
  505. {
  506. notFoundVars += *ii;
  507. if(manager)
  508. {
  509. cmCacheManager::CacheIterator it =
  510. manager->GetCacheIterator(ii->c_str());
  511. if(it.GetPropertyAsBool("ADVANCED"))
  512. {
  513. notFoundVars += " (ADVANCED)";
  514. }
  515. }
  516. notFoundVars += "\n";
  517. }
  518. cmSystemTools::Error("This project requires some variables to be set,\n"
  519. "and cmake can not find them.\n"
  520. "Please set the following variables:\n",
  521. notFoundVars.c_str());
  522. }
  523. // at this point m_LocalGenerators has been filled,
  524. // so create the map from project name to vector of local generators
  525. this->FillProjectMap();
  526. if ( !m_CMakeInstance->GetScriptMode() )
  527. {
  528. m_CMakeInstance->UpdateProgress("Configuring done", -1);
  529. }
  530. }
  531. // loop through the directories creating cmLocalGenerators and Configure()
  532. void cmGlobalGenerator::RecursiveConfigure(cmLocalGenerator *lg,
  533. float startProgress,
  534. float endProgress)
  535. {
  536. // configure the current directory
  537. lg->Configure();
  538. // get all the subdirectories
  539. std::vector<std::pair<cmStdString, bool> > subdirs = lg->GetMakefile()->GetSubDirectories();
  540. float progressPiece = (endProgress - startProgress)/(1.0f+subdirs.size());
  541. m_CMakeInstance->UpdateProgress("Configuring",
  542. startProgress + progressPiece);
  543. // for each subdir recurse
  544. unsigned int i;
  545. for (i = 0; i < subdirs.size(); ++i)
  546. {
  547. cmLocalGenerator *lg2 = this->CreateLocalGenerator();
  548. lg2->SetParent(lg);
  549. m_LocalGenerators.push_back(lg2);
  550. // add the subdir to the start output directory
  551. std::string outdir = lg->GetMakefile()->GetStartOutputDirectory();
  552. outdir += "/";
  553. outdir += subdirs[i].first;
  554. lg2->GetMakefile()->SetStartOutputDirectory(outdir.c_str());
  555. lg2->SetExcludeAll(!subdirs[i].second);
  556. // add the subdir to the start source directory
  557. std::string currentDir = lg->GetMakefile()->GetStartDirectory();
  558. currentDir += "/";
  559. currentDir += subdirs[i].first;
  560. lg2->GetMakefile()->SetStartDirectory(currentDir.c_str());
  561. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  562. this->RecursiveConfigure(lg2,
  563. startProgress + (i+1.0f)*progressPiece,
  564. startProgress + (i+2.0f)*progressPiece);
  565. }
  566. }
  567. void cmGlobalGenerator::Generate()
  568. {
  569. // For each existing cmLocalGenerator
  570. unsigned int i;
  571. for (i = 0; i < m_LocalGenerators.size(); ++i)
  572. {
  573. m_LocalGenerators[i]->Generate(true);
  574. m_LocalGenerators[i]->GenerateInstallRules();
  575. m_CMakeInstance->UpdateProgress("Generating",
  576. (i+1.0f)/m_LocalGenerators.size());
  577. }
  578. m_CMakeInstance->UpdateProgress("Generating done", -1);
  579. }
  580. void cmGlobalGenerator::LocalGenerate()
  581. {
  582. // for this case we create one LocalGenerator
  583. // configure it, and then Generate it
  584. // start with this directory
  585. cmLocalGenerator *lg = this->CreateLocalGenerator();
  586. // set the Start directories
  587. lg->GetMakefile()->SetStartDirectory(m_CMakeInstance->GetStartDirectory());
  588. lg->GetMakefile()->SetStartOutputDirectory(m_CMakeInstance->GetStartOutputDirectory());
  589. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  590. // now do trhe configure
  591. lg->Configure();
  592. lg->ConfigureFinalPass();
  593. lg->Generate(false);
  594. delete lg;
  595. }
  596. int cmGlobalGenerator::TryCompile(const char *, const char *bindir,
  597. const char *, const char *target,
  598. std::string *output, cmMakefile*)
  599. {
  600. // now build the test
  601. std::string makeCommand =
  602. m_CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  603. if(makeCommand.size() == 0)
  604. {
  605. cmSystemTools::Error(
  606. "Generator cannot find the appropriate make command.");
  607. return 1;
  608. }
  609. makeCommand = cmSystemTools::ConvertToOutputPath(makeCommand.c_str());
  610. /**
  611. * Run an executable command and put the stdout in output.
  612. */
  613. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  614. cmSystemTools::ChangeDirectory(bindir);
  615. // Since we have full control over the invocation of nmake, let us
  616. // make it quiet.
  617. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  618. {
  619. makeCommand += " /NOLOGO ";
  620. }
  621. // now build
  622. if (target)
  623. {
  624. makeCommand += " ";
  625. makeCommand += target;
  626. #if defined(_WIN32) || defined(__CYGWIN__)
  627. std::string tmp = target;
  628. // if the target does not already end in . something
  629. // then assume .exe
  630. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  631. {
  632. makeCommand += ".exe";
  633. }
  634. #endif // WIN32
  635. }
  636. else
  637. {
  638. makeCommand += " all";
  639. }
  640. int retVal;
  641. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  642. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  643. cmSystemTools::SetRunCommandHideConsole(true);
  644. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  645. &retVal, 0, false, timeout))
  646. {
  647. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  648. cmSystemTools::Error("Generator: execution of make failed.");
  649. // return to the original directory
  650. cmSystemTools::ChangeDirectory(cwd.c_str());
  651. return 1;
  652. }
  653. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  654. // The SGI MipsPro 7.3 compiler does not return an error code when
  655. // the source has a #error in it! This is a work-around for such
  656. // compilers.
  657. if((retVal == 0) && (output->find("#error") != std::string::npos))
  658. {
  659. retVal = 1;
  660. }
  661. cmSystemTools::ChangeDirectory(cwd.c_str());
  662. return retVal;
  663. }
  664. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  665. {
  666. cmLocalGenerator *lg = new cmLocalGenerator;
  667. lg->SetGlobalGenerator(this);
  668. return lg;
  669. }
  670. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  671. {
  672. this->SetConfiguredFilesPath(
  673. gen->GetCMakeInstance()->GetHomeOutputDirectory());
  674. const char* make =
  675. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  676. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  677. "make program",
  678. cmCacheManager::FILEPATH);
  679. // copy the enabled languages
  680. this->m_LanguageEnabled = gen->m_LanguageEnabled;
  681. this->m_ExtensionToLanguage = gen->m_ExtensionToLanguage;
  682. this->m_IgnoreExtensions = gen->m_IgnoreExtensions;
  683. this->m_LanguageToOutputExtension = gen->m_LanguageToOutputExtension;
  684. this->m_LanguageToLinkerPreference = gen->m_LanguageToLinkerPreference;
  685. this->m_OutputExtensions = gen->m_OutputExtensions;
  686. }
  687. //----------------------------------------------------------------------------
  688. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  689. {
  690. entry.name = this->GetName();
  691. entry.brief = "";
  692. entry.full = "";
  693. }
  694. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  695. cmLocalGenerator* gen)
  696. {
  697. cmLocalGenerator* cur = gen->GetParent();
  698. while(cur && cur != root)
  699. {
  700. if(cur->GetExcludeAll())
  701. {
  702. return true;
  703. }
  704. cur = cur->GetParent();
  705. }
  706. return false;
  707. }
  708. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  709. {
  710. for(std::map<cmStdString, bool>::iterator i = m_LanguageEnabled.begin();
  711. i != m_LanguageEnabled.end(); ++i)
  712. {
  713. lang.push_back(i->first);
  714. }
  715. }
  716. const char* cmGlobalGenerator::GetLinkerPreference(const char* lang)
  717. {
  718. if(m_LanguageToLinkerPreference.count(lang))
  719. {
  720. return m_LanguageToLinkerPreference[lang].c_str();
  721. }
  722. return "None";
  723. }
  724. void cmGlobalGenerator::FillProjectMap()
  725. {
  726. unsigned int i;
  727. for(i = 0; i < m_LocalGenerators.size(); ++i)
  728. {
  729. std::string name = m_LocalGenerators[i]->GetMakefile()->GetProjectName();
  730. // for each local generator add the local generator to the project that
  731. // it is in
  732. m_ProjectMap[name].push_back(m_LocalGenerators[i]);
  733. // now add the local generator to any parent project it is part of
  734. std::vector<std::string> const& pprojects
  735. = m_LocalGenerators[i]->GetMakefile()->GetParentProjects();
  736. for(unsigned int k =0; k < pprojects.size(); ++k)
  737. {
  738. m_ProjectMap[pprojects[k]].push_back(m_LocalGenerators[i]);
  739. }
  740. }
  741. }
  742. cmTarget* cmGlobalGenerator::FindTarget(const char* name)
  743. {
  744. for(unsigned int i = 0; i < m_LocalGenerators.size(); ++i)
  745. {
  746. cmTarget* ret = m_LocalGenerators[i]->GetMakefile()->FindTarget(name);
  747. if(ret)
  748. {
  749. return ret;
  750. }
  751. }
  752. return 0;
  753. }