cmGlobalGenerator.cxx 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  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 "cmExternalMakefileProjectGenerator.h"
  16. #include "cmake.h"
  17. #include "cmMakefile.h"
  18. #include "cmSourceFile.h"
  19. #include "cmVersion.h"
  20. #include "cmInstallExportGenerator.h"
  21. #include <stdlib.h> // required for atof
  22. #if defined(_WIN32) && !defined(__CYGWIN__)
  23. #include <windows.h>
  24. #endif
  25. #include <assert.h>
  26. cmGlobalGenerator::cmGlobalGenerator()
  27. {
  28. // By default the .SYMBOLIC dependency is not needed on symbolic rules.
  29. this->NeedSymbolicMark = false;
  30. // by default use the native paths
  31. this->ForceUnixPaths = false;
  32. // By default do not try to support color.
  33. this->ToolSupportsColor = false;
  34. // By default do not use link scripts.
  35. this->UseLinkScript = false;
  36. // Whether an install target is needed.
  37. this->InstallTargetEnabled = false;
  38. // how long to let try compiles run
  39. this->TryCompileTimeout = 0;
  40. this->ExtraGenerator = 0;
  41. }
  42. cmGlobalGenerator::~cmGlobalGenerator()
  43. {
  44. // Delete any existing cmLocalGenerators
  45. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  46. {
  47. delete this->LocalGenerators[i];
  48. }
  49. this->LocalGenerators.clear();
  50. if (this->ExtraGenerator)
  51. {
  52. delete this->ExtraGenerator;
  53. }
  54. for (std::map<cmStdString, std::vector<cmTargetExport*> >::iterator
  55. setIt = this->ExportSets.begin();
  56. setIt != this->ExportSets.end();
  57. ++setIt)
  58. {
  59. for (unsigned int i = 0; i < setIt->second.size(); ++i)
  60. {
  61. delete setIt->second[i];
  62. }
  63. }
  64. }
  65. // Find the make program for the generator, required for try compiles
  66. void cmGlobalGenerator::FindMakeProgram(cmMakefile* mf)
  67. {
  68. if(this->FindMakeProgramFile.size() == 0)
  69. {
  70. cmSystemTools::Error(
  71. "Generator implementation error, "
  72. "all generators must specify this->FindMakeProgramFile");
  73. }
  74. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  75. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  76. {
  77. std::string setMakeProgram =
  78. mf->GetModulesFile(this->FindMakeProgramFile.c_str());
  79. if(setMakeProgram.size())
  80. {
  81. mf->ReadListFile(0, setMakeProgram.c_str());
  82. }
  83. }
  84. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  85. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  86. {
  87. cmOStringStream err;
  88. err << "CMake was unable to find a build program corresponding to \""
  89. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  90. << "probably need to select a different build tool.";
  91. cmSystemTools::Error(err.str().c_str());
  92. cmSystemTools::SetFatalErrorOccured();
  93. return;
  94. }
  95. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  96. // if there are spaces in the make program use short path
  97. // but do not short path the actual program name, as
  98. // this can cause trouble with VSExpress
  99. if(makeProgram.find(' ') != makeProgram.npos)
  100. {
  101. std::string dir;
  102. std::string file;
  103. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  104. dir, file);
  105. std::string saveFile = file;
  106. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  107. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  108. dir, file);
  109. makeProgram = dir;
  110. makeProgram += "/";
  111. makeProgram += saveFile;
  112. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  113. "make program",
  114. cmCacheManager::FILEPATH);
  115. }
  116. if(makeProgram.find("xcodebuild") != makeProgram.npos)
  117. {
  118. // due to the text file busy /bin/sh problem with xcodebuild
  119. // use the cmakexbuild wrapper instead. This program
  120. // will run xcodebuild and if it sees the error text file busy
  121. // it will stop forwarding output, and let the build finish.
  122. // Then it will retry the build. It will continue this
  123. // untill no text file busy errors occur.
  124. std::string cmakexbuild =
  125. this->CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_COMMAND");
  126. cmakexbuild = cmakexbuild.substr(0, cmakexbuild.length()-5);
  127. cmakexbuild += "cmakexbuild";
  128. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM",
  129. cmakexbuild.c_str(),
  130. "make program",
  131. cmCacheManager::FILEPATH);
  132. }
  133. }
  134. // enable the given language
  135. //
  136. // The following files are loaded in this order:
  137. //
  138. // First figure out what OS we are running on:
  139. //
  140. // CMakeSystem.cmake - configured file created by CMakeDetermineSystem.cmake
  141. // CMakeDetermineSystem.cmake - figure out os info and create
  142. // CMakeSystem.cmake IF CMAKE_SYSTEM
  143. // not set
  144. // CMakeSystem.cmake - configured file created by
  145. // CMakeDetermineSystem.cmake IF CMAKE_SYSTEM_LOADED
  146. // Next try and enable all languages found in the languages vector
  147. //
  148. // FOREACH LANG in languages
  149. // CMake(LANG)Compiler.cmake - configured file create by
  150. // CMakeDetermine(LANG)Compiler.cmake
  151. // CMakeDetermine(LANG)Compiler.cmake - Finds compiler for LANG and
  152. // creates CMake(LANG)Compiler.cmake
  153. // CMake(LANG)Compiler.cmake - configured file created by
  154. // CMakeDetermine(LANG)Compiler.cmake
  155. //
  156. // CMakeSystemSpecificInformation.cmake
  157. // - includes Platform/${CMAKE_SYSTEM_NAME}.cmake
  158. // may use compiler stuff
  159. // FOREACH LANG in languages
  160. // CMake(LANG)Information.cmake
  161. // - loads Platform/${CMAKE_SYSTEM_NAME}-${COMPILER}.cmake
  162. // CMakeTest(LANG)Compiler.cmake
  163. // - Make sure the compiler works with a try compile if
  164. // CMakeDetermine(LANG) was loaded
  165. //
  166. // Now load a few files that can override values set in any of the above
  167. // CMake(PROJECTNAME)Compatibility.cmake
  168. // - load any backwards compatibility stuff for current project
  169. // ${CMAKE_USER_MAKE_RULES_OVERRIDE}
  170. // - allow users a chance to override system variables
  171. //
  172. //
  173. void
  174. cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  175. cmMakefile *mf, bool)
  176. {
  177. if(languages.size() == 0)
  178. {
  179. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  180. cmSystemTools::SetFatalErrorOccured();
  181. return;
  182. }
  183. mf->AddDefinition("RUN_CONFIGURE", true);
  184. std::string rootBin = mf->GetHomeOutputDirectory();
  185. rootBin += cmake::GetCMakeFilesDirectory();
  186. // If the configuration files path has been set,
  187. // then we are in a try compile and need to copy the enable language
  188. // files from the parent cmake bin dir, into the try compile bin dir
  189. if(this->ConfiguredFilesPath.size())
  190. {
  191. for(std::vector<std::string>::const_iterator l = languages.begin();
  192. l != languages.end(); ++l)
  193. {
  194. if(*l == "NONE")
  195. {
  196. this->SetLanguageEnabled("NONE", mf);
  197. break;
  198. }
  199. }
  200. rootBin = this->ConfiguredFilesPath;
  201. }
  202. // set the dir for parent files so they can be used by modules
  203. mf->AddDefinition("CMAKE_PLATFORM_ROOT_BIN",rootBin.c_str());
  204. // find and make sure CMAKE_MAKE_PROGRAM is defined
  205. this->FindMakeProgram(mf);
  206. // try and load the CMakeSystem.cmake if it is there
  207. std::string fpath = rootBin;
  208. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  209. {
  210. fpath += "/CMakeSystem.cmake";
  211. if(cmSystemTools::FileExists(fpath.c_str()))
  212. {
  213. mf->ReadListFile(0,fpath.c_str());
  214. }
  215. }
  216. // Load the CMakeDetermineSystem.cmake file and find out
  217. // what platform we are running on
  218. if (!mf->GetDefinition("CMAKE_SYSTEM"))
  219. {
  220. #if defined(_WIN32) && !defined(__CYGWIN__)
  221. /* Windows version number data. */
  222. OSVERSIONINFO osvi;
  223. ZeroMemory(&osvi, sizeof(osvi));
  224. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  225. GetVersionEx (&osvi);
  226. cmOStringStream windowsVersionString;
  227. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  228. windowsVersionString.str();
  229. mf->AddDefinition("CMAKE_HOST_SYSTEM_VERSION",
  230. windowsVersionString.str().c_str());
  231. #endif
  232. // Read the DetermineSystem file
  233. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  234. mf->ReadListFile(0, systemFile.c_str());
  235. // load the CMakeSystem.cmake from the binary directory
  236. // this file is configured by the CMakeDetermineSystem.cmake file
  237. fpath = rootBin;
  238. fpath += "/CMakeSystem.cmake";
  239. mf->ReadListFile(0,fpath.c_str());
  240. }
  241. std::map<cmStdString, bool> needTestLanguage;
  242. std::map<cmStdString, bool> needSetLanguageEnabledMaps;
  243. // foreach language
  244. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  245. // the compiler
  246. for(std::vector<std::string>::const_iterator l = languages.begin();
  247. l != languages.end(); ++l)
  248. {
  249. const char* lang = l->c_str();
  250. needSetLanguageEnabledMaps[lang] = false;
  251. if(*l == "NONE")
  252. {
  253. this->SetLanguageEnabled("NONE", mf);
  254. continue;
  255. }
  256. std::string loadedLang = "CMAKE_";
  257. loadedLang += lang;
  258. loadedLang += "_COMPILER_LOADED";
  259. // If the existing build tree was already configured with this
  260. // version of CMake then try to load the configured file first
  261. // to avoid duplicate compiler tests.
  262. unsigned int cacheMajor = mf->GetCacheMajorVersion();
  263. unsigned int cacheMinor = mf->GetCacheMinorVersion();
  264. unsigned int selfMajor = cmVersion::GetMajorVersion();
  265. unsigned int selfMinor = cmVersion::GetMinorVersion();
  266. if((this->CMakeInstance->GetIsInTryCompile() ||
  267. (selfMajor == cacheMajor && selfMinor == cacheMinor))
  268. && !mf->GetDefinition(loadedLang.c_str()))
  269. {
  270. fpath = rootBin;
  271. fpath += "/CMake";
  272. fpath += lang;
  273. fpath += "Compiler.cmake";
  274. if(cmSystemTools::FileExists(fpath.c_str()))
  275. {
  276. if(!mf->ReadListFile(0,fpath.c_str()))
  277. {
  278. cmSystemTools::Error("Could not find cmake module file:",
  279. fpath.c_str());
  280. }
  281. // if this file was found then the language was already determined
  282. // to be working
  283. needTestLanguage[lang] = false;
  284. this->SetLanguageEnabledFlag(lang, mf);
  285. needSetLanguageEnabledMaps[lang] = true;
  286. // this can only be called after loading CMake(LANG)Compiler.cmake
  287. }
  288. }
  289. if(!this->GetLanguageEnabled(lang) )
  290. {
  291. if (this->CMakeInstance->GetIsInTryCompile())
  292. {
  293. cmSystemTools::Error("This should not have happen. "
  294. "If you see this message, you are probably "
  295. "using a broken CMakeLists.txt file or a "
  296. "problematic release of CMake");
  297. }
  298. // if the CMake(LANG)Compiler.cmake file was not found then
  299. // load CMakeDetermine(LANG)Compiler.cmake
  300. std::string determineCompiler = "CMakeDetermine";
  301. determineCompiler += lang;
  302. determineCompiler += "Compiler.cmake";
  303. std::string determineFile =
  304. mf->GetModulesFile(determineCompiler.c_str());
  305. if(!mf->ReadListFile(0,determineFile.c_str()))
  306. {
  307. cmSystemTools::Error("Could not find cmake module file:",
  308. determineFile.c_str());
  309. }
  310. needTestLanguage[lang] = true;
  311. // Some generators like visual studio should not use the env variables
  312. // So the global generator can specify that in this variable
  313. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  314. {
  315. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
  316. // into the environment, in case user scripts want to run
  317. // configure, or sub cmakes
  318. std::string compilerName = "CMAKE_";
  319. compilerName += lang;
  320. compilerName += "_COMPILER";
  321. std::string compilerEnv = "CMAKE_";
  322. compilerEnv += lang;
  323. compilerEnv += "_COMPILER_ENV_VAR";
  324. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  325. std::string envVarValue =
  326. mf->GetRequiredDefinition(compilerName.c_str());
  327. std::string env = envVar;
  328. env += "=";
  329. env += envVarValue;
  330. cmSystemTools::PutEnv(env.c_str());
  331. }
  332. // if determineLanguage was called then load the file it
  333. // configures CMake(LANG)Compiler.cmake
  334. fpath = rootBin;
  335. fpath += "/CMake";
  336. fpath += lang;
  337. fpath += "Compiler.cmake";
  338. if(!mf->ReadListFile(0,fpath.c_str()))
  339. {
  340. cmSystemTools::Error("Could not find cmake module file:",
  341. fpath.c_str());
  342. }
  343. this->SetLanguageEnabledFlag(lang, mf);
  344. needSetLanguageEnabledMaps[lang] = true;
  345. // this can only be called after loading CMake(LANG)Compiler.cmake
  346. // the language must be enabled for try compile to work, but we do
  347. // not know if it is a working compiler yet so set the test language
  348. // flag
  349. needTestLanguage[lang] = true;
  350. } // end if(!this->GetLanguageEnabled(lang) )
  351. } // end loop over languages
  352. // **** Load the system specific information if not yet loaded
  353. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  354. {
  355. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  356. if(!mf->ReadListFile(0,fpath.c_str()))
  357. {
  358. cmSystemTools::Error("Could not find cmake module file:",
  359. fpath.c_str());
  360. }
  361. }
  362. // loop over languages again loading CMake(LANG)Information.cmake
  363. //
  364. for(std::vector<std::string>::const_iterator l = languages.begin();
  365. l != languages.end(); ++l)
  366. {
  367. const char* lang = l->c_str();
  368. if(*l == "NONE")
  369. {
  370. this->SetLanguageEnabled("NONE", mf);
  371. continue;
  372. }
  373. std::string langLoadedVar = "CMAKE_";
  374. langLoadedVar += lang;
  375. langLoadedVar += "_INFORMATION_LOADED";
  376. if (!mf->GetDefinition(langLoadedVar.c_str()))
  377. {
  378. fpath = "CMake";
  379. fpath += lang;
  380. fpath += "Information.cmake";
  381. fpath = mf->GetModulesFile(fpath.c_str());
  382. if(!mf->ReadListFile(0,fpath.c_str()))
  383. {
  384. cmSystemTools::Error("Could not find cmake module file:",
  385. fpath.c_str());
  386. }
  387. }
  388. if (needSetLanguageEnabledMaps[lang])
  389. {
  390. this->SetLanguageEnabledMaps(lang, mf);
  391. }
  392. // Test the compiler for the language just setup
  393. // At this point we should have enough info for a try compile
  394. // which is used in the backward stuff
  395. // If the language is untested then test it now with a try compile.
  396. if(needTestLanguage[lang])
  397. {
  398. if (!this->CMakeInstance->GetIsInTryCompile())
  399. {
  400. std::string testLang = "CMakeTest";
  401. testLang += lang;
  402. testLang += "Compiler.cmake";
  403. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  404. if(!mf->ReadListFile(0,ifpath.c_str()))
  405. {
  406. cmSystemTools::Error("Could not find cmake module file:",
  407. ifpath.c_str());
  408. }
  409. std::string compilerWorks = "CMAKE_";
  410. compilerWorks += lang;
  411. compilerWorks += "_COMPILER_WORKS";
  412. // if the compiler did not work, then remove the
  413. // CMake(LANG)Compiler.cmake file so that it will get tested the
  414. // next time cmake is run
  415. if(!mf->IsOn(compilerWorks.c_str()))
  416. {
  417. fpath = rootBin;
  418. fpath += "/CMake";
  419. fpath += lang;
  420. fpath += "Compiler.cmake";
  421. cmSystemTools::RemoveFile(fpath.c_str());
  422. }
  423. else
  424. {
  425. // load backwards compatibility stuff for C and CXX
  426. // for old versions of CMake ListFiles C and CXX had some
  427. // backwards compatibility files they have to load
  428. // These files have a bunch of try compiles in them so
  429. // should only be done
  430. const char* versionValue
  431. = mf->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  432. if (atof(versionValue) <= 1.4)
  433. {
  434. if(strcmp(lang, "C") == 0)
  435. {
  436. ifpath =
  437. mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  438. mf->ReadListFile(0,ifpath.c_str());
  439. }
  440. if(strcmp(lang, "CXX") == 0)
  441. {
  442. ifpath =
  443. mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  444. mf->ReadListFile(0,ifpath.c_str());
  445. }
  446. }
  447. }
  448. } // end if in try compile
  449. } // end need test language
  450. } // end for each language
  451. // Now load files that can override any settings on the platform or for
  452. // the project First load the project compatibility file if it is in
  453. // cmake
  454. std::string projectCompatibility = mf->GetDefinition("CMAKE_ROOT");
  455. projectCompatibility += "/Modules/";
  456. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  457. projectCompatibility += "Compatibility.cmake";
  458. if(cmSystemTools::FileExists(projectCompatibility.c_str()))
  459. {
  460. mf->ReadListFile(0,projectCompatibility.c_str());
  461. }
  462. }
  463. //----------------------------------------------------------------------------
  464. const char*
  465. cmGlobalGenerator::GetLanguageOutputExtension(cmSourceFile const& source)
  466. {
  467. if(const char* lang = source.GetLanguage())
  468. {
  469. if(this->LanguageToOutputExtension.count(lang) > 0)
  470. {
  471. return this->LanguageToOutputExtension[lang].c_str();
  472. }
  473. }
  474. else
  475. {
  476. // if no language is found then check to see if it is already an
  477. // ouput extension for some language. In that case it should be ignored
  478. // and in this map, so it will not be compiled but will just be used.
  479. std::string const& ext = source.GetExtension();
  480. if(!ext.empty())
  481. {
  482. if(this->OutputExtensions.count(ext))
  483. {
  484. return ext.c_str();
  485. }
  486. }
  487. }
  488. return "";
  489. }
  490. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  491. {
  492. // if there is an extension and it starts with . then move past the
  493. // . because the extensions are not stored with a . in the map
  494. if(ext && *ext == '.')
  495. {
  496. ++ext;
  497. }
  498. if(this->ExtensionToLanguage.count(ext) > 0)
  499. {
  500. return this->ExtensionToLanguage[ext].c_str();
  501. }
  502. return 0;
  503. }
  504. /* SetLanguageEnabled() is now split in two parts:
  505. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  506. for checking whether the language is already enabled. After setting this
  507. flag still the values from the cmake variables have to be copied into the
  508. internal maps, this is done in SetLanguageEnabledMaps() which is called
  509. after the system- and compiler specific files have been loaded.
  510. */
  511. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  512. {
  513. this->SetLanguageEnabledFlag(l, mf);
  514. this->SetLanguageEnabledMaps(l, mf);
  515. }
  516. void cmGlobalGenerator::SetLanguageEnabledFlag(const char* l, cmMakefile*)
  517. {
  518. this->LanguageEnabled[l] = true;
  519. }
  520. void cmGlobalGenerator::SetLanguageEnabledMaps(const char* l, cmMakefile* mf)
  521. {
  522. // use LanguageToLinkerPreference to detect whether this functions has
  523. // run before
  524. if (this->LanguageToLinkerPreference.find(l) !=
  525. this->LanguageToLinkerPreference.end())
  526. {
  527. return;
  528. }
  529. std::string linkerPrefVar = std::string("CMAKE_") +
  530. std::string(l) + std::string("_LINKER_PREFERENCE");
  531. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  532. if(!linkerPref)
  533. {
  534. linkerPref = "None";
  535. }
  536. this->LanguageToLinkerPreference[l] = linkerPref;
  537. std::string outputExtensionVar = std::string("CMAKE_") +
  538. std::string(l) + std::string("_OUTPUT_EXTENSION");
  539. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  540. if(outputExtension)
  541. {
  542. this->LanguageToOutputExtension[l] = outputExtension;
  543. this->OutputExtensions[outputExtension] = outputExtension;
  544. if(outputExtension[0] == '.')
  545. {
  546. this->OutputExtensions[outputExtension+1] = outputExtension+1;
  547. }
  548. }
  549. std::string extensionsVar = std::string("CMAKE_") +
  550. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  551. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  552. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  553. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  554. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  555. std::vector<std::string> extensionList;
  556. cmSystemTools::ExpandListArgument(exts, extensionList);
  557. for(std::vector<std::string>::iterator i = extensionList.begin();
  558. i != extensionList.end(); ++i)
  559. {
  560. this->ExtensionToLanguage[*i] = l;
  561. }
  562. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  563. for(std::vector<std::string>::iterator i = extensionList.begin();
  564. i != extensionList.end(); ++i)
  565. {
  566. this->IgnoreExtensions[*i] = true;
  567. }
  568. }
  569. bool cmGlobalGenerator::IgnoreFile(const char* l)
  570. {
  571. if(this->GetLanguageFromExtension(l))
  572. {
  573. return false;
  574. }
  575. return (this->IgnoreExtensions.count(l) > 0);
  576. }
  577. bool cmGlobalGenerator::GetLanguageEnabled(const char* l)
  578. {
  579. return (this->LanguageEnabled.count(l) > 0);
  580. }
  581. void cmGlobalGenerator::ClearEnabledLanguages()
  582. {
  583. this->LanguageEnabled.clear();
  584. }
  585. bool cmGlobalGenerator::IsDependedOn(const char* project,
  586. cmTarget* targetIn)
  587. {
  588. // Get all local gens for this project
  589. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  590. // loop over local gens and get the targets for each one
  591. for(unsigned int i = 0; i < gens->size(); ++i)
  592. {
  593. cmTargets& targets = (*gens)[i]->GetMakefile()->GetTargets();
  594. for (cmTargets::iterator l = targets.begin();
  595. l != targets.end(); l++)
  596. {
  597. cmTarget& target = l->second;
  598. std::set<cmStdString>::const_iterator pos =
  599. target.GetUtilities().find(targetIn->GetName());
  600. if(pos != target.GetUtilities().end())
  601. {
  602. return true;
  603. }
  604. }
  605. }
  606. return false;
  607. }
  608. void cmGlobalGenerator::Configure()
  609. {
  610. // Delete any existing cmLocalGenerators
  611. unsigned int i;
  612. for (i = 0; i < this->LocalGenerators.size(); ++i)
  613. {
  614. delete this->LocalGenerators[i];
  615. }
  616. this->LocalGenerators.clear();
  617. this->TargetDependencies.clear();
  618. this->TotalTargets.clear();
  619. this->ImportedTotalTargets.clear();
  620. this->ProjectToTargetMap.clear();
  621. this->ProjectMap.clear();
  622. // start with this directory
  623. cmLocalGenerator *lg = this->CreateLocalGenerator();
  624. this->LocalGenerators.push_back(lg);
  625. // set the Start directories
  626. lg->GetMakefile()->SetStartDirectory
  627. (this->CMakeInstance->GetStartDirectory());
  628. lg->GetMakefile()->SetStartOutputDirectory
  629. (this->CMakeInstance->GetStartOutputDirectory());
  630. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  631. // now do it
  632. lg->Configure();
  633. // update the cache entry for the number of local generators, this is used
  634. // for progress
  635. char num[100];
  636. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  637. this->GetCMakeInstance()->AddCacheEntry
  638. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  639. "number of local generators", cmCacheManager::INTERNAL);
  640. std::set<cmStdString> notFoundMap;
  641. // after it is all done do a ConfigureFinalPass
  642. cmCacheManager* manager = 0;
  643. for (i = 0; i < this->LocalGenerators.size(); ++i)
  644. {
  645. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  646. this->LocalGenerators[i]->ConfigureFinalPass();
  647. cmTargets & targets =
  648. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  649. for (cmTargets::iterator l = targets.begin();
  650. l != targets.end(); l++)
  651. {
  652. cmTarget::LinkLibraryVectorType libs = l->second.GetLinkLibraries();
  653. for(cmTarget::LinkLibraryVectorType::iterator lib = libs.begin();
  654. lib != libs.end(); ++lib)
  655. {
  656. if(lib->first.size() > 9 &&
  657. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  658. {
  659. std::string varName = lib->first.substr(0, lib->first.size()-9);
  660. notFoundMap.insert(varName);
  661. }
  662. }
  663. std::vector<std::string>& incs =
  664. this->LocalGenerators[i]->GetMakefile()->GetIncludeDirectories();
  665. for( std::vector<std::string>::iterator lib = incs.begin();
  666. lib != incs.end(); ++lib)
  667. {
  668. if(lib->size() > 9 &&
  669. cmSystemTools::IsNOTFOUND(lib->c_str()))
  670. {
  671. std::string varName = lib->substr(0, lib->size()-9);
  672. notFoundMap.insert(varName);
  673. }
  674. }
  675. this->CMakeInstance->UpdateProgress
  676. ("Configuring", 0.9f+0.1f*(i+1.0f)/this->LocalGenerators.size());
  677. this->LocalGenerators[i]->GetMakefile()->CheckInfiniteLoops();
  678. }
  679. }
  680. if(notFoundMap.size())
  681. {
  682. std::string notFoundVars;
  683. for(std::set<cmStdString>::iterator ii = notFoundMap.begin();
  684. ii != notFoundMap.end(); ++ii)
  685. {
  686. notFoundVars += *ii;
  687. if(manager)
  688. {
  689. cmCacheManager::CacheIterator it =
  690. manager->GetCacheIterator(ii->c_str());
  691. if(it.GetPropertyAsBool("ADVANCED"))
  692. {
  693. notFoundVars += " (ADVANCED)";
  694. }
  695. }
  696. notFoundVars += "\n";
  697. }
  698. cmSystemTools::Error("This project requires some variables to be set,\n"
  699. "and cmake can not find them.\n"
  700. "Please set the following variables:\n",
  701. notFoundVars.c_str());
  702. }
  703. // at this point this->LocalGenerators has been filled,
  704. // so create the map from project name to vector of local generators
  705. this->FillProjectMap();
  706. // now create project to target map
  707. // This will make sure that targets have all the
  708. // targets they depend on as part of the build.
  709. this->FillProjectToTargetMap();
  710. if ( !this->CMakeInstance->GetScriptMode() )
  711. {
  712. this->CMakeInstance->UpdateProgress("Configuring done", -1);
  713. }
  714. }
  715. void cmGlobalGenerator::Generate()
  716. {
  717. // For each existing cmLocalGenerator
  718. unsigned int i;
  719. // Consolidate global targets
  720. cmTargets globalTargets;
  721. this->CreateDefaultGlobalTargets(&globalTargets);
  722. for (i = 0; i < this->LocalGenerators.size(); ++i)
  723. {
  724. cmTargets* targets =
  725. &(this->LocalGenerators[i]->GetMakefile()->GetTargets());
  726. cmTargets::iterator tarIt;
  727. for ( tarIt = targets->begin(); tarIt != targets->end(); ++ tarIt )
  728. {
  729. if ( tarIt->second.GetType() == cmTarget::GLOBAL_TARGET )
  730. {
  731. globalTargets[tarIt->first] = tarIt->second;
  732. }
  733. }
  734. }
  735. for (i = 0; i < this->LocalGenerators.size(); ++i)
  736. {
  737. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  738. cmTargets* targets = &(mf->GetTargets());
  739. cmTargets::iterator tit;
  740. for ( tit = globalTargets.begin(); tit != globalTargets.end(); ++ tit )
  741. {
  742. (*targets)[tit->first] = tit->second;
  743. (*targets)[tit->first].SetMakefile(mf);
  744. }
  745. }
  746. // Add generator specific helper commands
  747. for (i = 0; i < this->LocalGenerators.size(); ++i)
  748. {
  749. this->LocalGenerators[i]->AddHelperCommands();
  750. }
  751. // Trace the dependencies, after that no custom commands should be added
  752. // because their dependencies might not be handled correctly
  753. for (i = 0; i < this->LocalGenerators.size(); ++i)
  754. {
  755. this->LocalGenerators[i]->TraceDependencies();
  756. }
  757. // Compute the manifest of main targets generated.
  758. for (i = 0; i < this->LocalGenerators.size(); ++i)
  759. {
  760. this->LocalGenerators[i]->GenerateTargetManifest(this->TargetManifest);
  761. }
  762. // Generate project files
  763. for (i = 0; i < this->LocalGenerators.size(); ++i)
  764. {
  765. this->LocalGenerators[i]->Generate();
  766. this->LocalGenerators[i]->GenerateInstallRules();
  767. this->LocalGenerators[i]->GenerateTestFiles();
  768. this->CMakeInstance->UpdateProgress("Generating",
  769. (i+1.0f)/this->LocalGenerators.size());
  770. }
  771. if (this->ExtraGenerator != 0)
  772. {
  773. this->ExtraGenerator->Generate();
  774. }
  775. this->CMakeInstance->UpdateProgress("Generating done", -1);
  776. }
  777. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  778. const char *projectName,
  779. const char *target,
  780. std::string *output, cmMakefile *mf)
  781. {
  782. std::string makeCommand = this->CMakeInstance->
  783. GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  784. if(makeCommand.size() == 0)
  785. {
  786. cmSystemTools::Error(
  787. "Generator cannot find the appropriate make command.");
  788. return 1;
  789. }
  790. std::string newTarget;
  791. if (target && strlen(target))
  792. {
  793. newTarget += target;
  794. #if 0
  795. #if defined(_WIN32) || defined(__CYGWIN__)
  796. std::string tmp = target;
  797. // if the target does not already end in . something
  798. // then assume .exe
  799. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  800. {
  801. newTarget += ".exe";
  802. }
  803. #endif // WIN32
  804. #endif
  805. }
  806. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  807. return this->Build(srcdir,bindir,projectName,
  808. newTarget.c_str(),
  809. output,makeCommand.c_str(),config,false,true,
  810. this->TryCompileTimeout);
  811. }
  812. std::string cmGlobalGenerator
  813. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  814. const char* additionalOptions, const char *targetName,
  815. const char* config, bool ignoreErrors, bool)
  816. {
  817. // Project name and config are not used yet.
  818. (void)projectName;
  819. (void)config;
  820. std::string makeCommand =
  821. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  822. // Since we have full control over the invocation of nmake, let us
  823. // make it quiet.
  824. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  825. {
  826. makeCommand += " /NOLOGO ";
  827. }
  828. if ( ignoreErrors )
  829. {
  830. makeCommand += " -i";
  831. }
  832. if ( additionalOptions )
  833. {
  834. makeCommand += " ";
  835. makeCommand += additionalOptions;
  836. }
  837. if ( targetName )
  838. {
  839. makeCommand += " ";
  840. makeCommand += targetName;
  841. }
  842. return makeCommand;
  843. }
  844. int cmGlobalGenerator::Build(
  845. const char *, const char *bindir,
  846. const char *projectName, const char *target,
  847. std::string *output,
  848. const char *makeCommandCSTR,
  849. const char *config,
  850. bool clean, bool fast,
  851. double timeout)
  852. {
  853. *output += "\nTesting TryCompileWithoutMakefile\n";
  854. /**
  855. * Run an executable command and put the stdout in output.
  856. */
  857. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  858. cmSystemTools::ChangeDirectory(bindir);
  859. int retVal;
  860. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  861. cmSystemTools::SetRunCommandHideConsole(true);
  862. // should we do a clean first?
  863. if (clean)
  864. {
  865. std::string cleanCommand =
  866. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  867. 0, "clean", config, false, fast);
  868. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), output,
  869. &retVal, 0, false, timeout))
  870. {
  871. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  872. cmSystemTools::Error("Generator: execution of make clean failed.");
  873. if (output)
  874. {
  875. *output += "\nGenerator: execution of make clean failed.\n";
  876. }
  877. // return to the original directory
  878. cmSystemTools::ChangeDirectory(cwd.c_str());
  879. return 1;
  880. }
  881. }
  882. // now build
  883. std::string makeCommand =
  884. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  885. 0, target, config, false, fast);
  886. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output,
  887. &retVal, 0, false, timeout))
  888. {
  889. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  890. cmSystemTools::Error
  891. ("Generator: execution of make failed. Make command was: ",
  892. makeCommand.c_str());
  893. if (output)
  894. {
  895. *output += "\nGenerator: execution of make failed. Make command was: "
  896. + makeCommand + "\n";
  897. }
  898. // return to the original directory
  899. cmSystemTools::ChangeDirectory(cwd.c_str());
  900. return 1;
  901. }
  902. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  903. // The SGI MipsPro 7.3 compiler does not return an error code when
  904. // the source has a #error in it! This is a work-around for such
  905. // compilers.
  906. if((retVal == 0) && (output->find("#error") != std::string::npos))
  907. {
  908. retVal = 1;
  909. }
  910. cmSystemTools::ChangeDirectory(cwd.c_str());
  911. return retVal;
  912. }
  913. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  914. {
  915. this->LocalGenerators.push_back(lg);
  916. // update progress
  917. // estimate how many lg there will be
  918. const char *numGenC =
  919. this->CMakeInstance->GetCacheManager()->GetCacheValue
  920. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  921. if (!numGenC)
  922. {
  923. return;
  924. }
  925. int numGen = atoi(numGenC);
  926. float prog = 0.9f*this->LocalGenerators.size()/numGen;
  927. if (prog > 0.9f)
  928. {
  929. prog = 0.9f;
  930. }
  931. this->CMakeInstance->UpdateProgress("Configuring", prog);
  932. }
  933. void cmGlobalGenerator::AddInstallComponent(const char* component)
  934. {
  935. if(component && *component)
  936. {
  937. this->InstallComponents.insert(component);
  938. }
  939. }
  940. void cmGlobalGenerator::AddTargetToExports(const char* exportSetName,
  941. cmTarget* target,
  942. cmInstallTargetGenerator* archive,
  943. cmInstallTargetGenerator* runTime,
  944. cmInstallTargetGenerator* library)
  945. {
  946. if ((exportSetName) && (*exportSetName) && (target))
  947. {
  948. cmTargetExport* te = new cmTargetExport(target, archive, runTime, library);
  949. this->ExportSets[exportSetName].push_back(te);
  950. }
  951. }
  952. const std::vector<cmTargetExport*>* cmGlobalGenerator::GetExportSet(
  953. const char* name) const
  954. {
  955. std::map<cmStdString, std::vector<cmTargetExport*> >::const_iterator
  956. exportSetIt = this->ExportSets.find(name);
  957. if (exportSetIt != this->ExportSets.end())
  958. {
  959. return &exportSetIt->second;
  960. }
  961. return 0;
  962. }
  963. void cmGlobalGenerator::EnableInstallTarget()
  964. {
  965. this->InstallTargetEnabled = true;
  966. }
  967. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  968. {
  969. cmLocalGenerator *lg = new cmLocalGenerator;
  970. lg->SetGlobalGenerator(this);
  971. return lg;
  972. }
  973. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen )
  974. {
  975. std::string cfp = gen->GetCMakeInstance()->GetHomeOutputDirectory();
  976. cfp += cmake::GetCMakeFilesDirectory();
  977. this->SetConfiguredFilesPath(cfp.c_str());
  978. const char* make =
  979. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  980. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  981. "make program",
  982. cmCacheManager::FILEPATH);
  983. // copy the enabled languages
  984. this->LanguageEnabled = gen->LanguageEnabled;
  985. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  986. this->IgnoreExtensions = gen->IgnoreExtensions;
  987. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  988. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  989. this->OutputExtensions = gen->OutputExtensions;
  990. }
  991. //----------------------------------------------------------------------------
  992. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  993. {
  994. entry.name = this->GetName();
  995. entry.brief = "";
  996. entry.full = "";
  997. }
  998. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  999. cmLocalGenerator* gen)
  1000. {
  1001. if(gen == root)
  1002. {
  1003. return false;
  1004. }
  1005. cmLocalGenerator* cur = gen->GetParent();
  1006. while(cur && cur != root)
  1007. {
  1008. if(cur->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1009. {
  1010. return true;
  1011. }
  1012. cur = cur->GetParent();
  1013. }
  1014. return gen->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL");
  1015. }
  1016. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  1017. {
  1018. for(std::map<cmStdString, bool>::iterator i =
  1019. this->LanguageEnabled.begin(); i != this->LanguageEnabled.end(); ++i)
  1020. {
  1021. lang.push_back(i->first);
  1022. }
  1023. }
  1024. const char* cmGlobalGenerator::GetLinkerPreference(const char* lang)
  1025. {
  1026. if(this->LanguageToLinkerPreference.count(lang))
  1027. {
  1028. return this->LanguageToLinkerPreference[lang].c_str();
  1029. }
  1030. return "None";
  1031. }
  1032. void cmGlobalGenerator::FillProjectMap()
  1033. {
  1034. this->ProjectMap.clear(); // make sure we start with a clean map
  1035. unsigned int i;
  1036. for(i = 0; i < this->LocalGenerators.size(); ++i)
  1037. {
  1038. // for each local generator add all projects
  1039. cmLocalGenerator *lg = this->LocalGenerators[i];
  1040. std::string name;
  1041. do
  1042. {
  1043. if (name != lg->GetMakefile()->GetProjectName())
  1044. {
  1045. name = lg->GetMakefile()->GetProjectName();
  1046. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1047. }
  1048. lg = lg->GetParent();
  1049. }
  1050. while (lg);
  1051. }
  1052. }
  1053. // Build a map that contains a the set of targets used by each project
  1054. void cmGlobalGenerator::FillProjectToTargetMap()
  1055. {
  1056. // loop over each project in the build
  1057. for(std::map<cmStdString,
  1058. std::vector<cmLocalGenerator*> >::iterator m =
  1059. this->ProjectMap.begin();
  1060. m != this->ProjectMap.end(); ++m)
  1061. {
  1062. std::vector<cmLocalGenerator*>& lgs = m->second;
  1063. if(lgs.size() == 0)
  1064. {
  1065. continue;
  1066. }
  1067. cmStdString const & projectName = m->first;
  1068. cmMakefile* projectMakefile = lgs[0]->GetMakefile();
  1069. // get the current EXCLUDE_FROM_ALL value from projectMakefile
  1070. const char* exclude = 0;
  1071. std::string excludeSave;
  1072. bool chain = false;
  1073. exclude =
  1074. projectMakefile->GetProperties().
  1075. GetPropertyValue("EXCLUDE_FROM_ALL",
  1076. cmProperty::DIRECTORY, chain);
  1077. if(exclude)
  1078. {
  1079. excludeSave = exclude;
  1080. }
  1081. // Set EXCLUDE_FROM_ALL to FALSE for the top level makefile because
  1082. // in the current project nothing is excluded yet
  1083. projectMakefile->SetProperty("EXCLUDE_FROM_ALL", "FALSE");
  1084. // now loop over all cmLocalGenerators in this project and pull
  1085. // out all the targets that depend on each other, even if those
  1086. // targets come from a target that is excluded.
  1087. for(std::vector<cmLocalGenerator*>::iterator lg =
  1088. lgs.begin(); lg != lgs.end(); ++lg)
  1089. {
  1090. cmMakefile* mf = (*lg)->GetMakefile();
  1091. cmTargets& targets = mf->GetTargets();
  1092. for(cmTargets::iterator t = targets.begin();
  1093. t != targets.end(); ++t)
  1094. {
  1095. cmTarget& target = t->second;
  1096. // if the target is in all then add it to the project
  1097. if(!target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1098. {
  1099. // add this target to the project
  1100. this->ProjectToTargetMap[projectName].insert(&target);
  1101. // get the target's dependencies
  1102. std::vector<cmTarget *>& tgtdeps = this->GetTargetDepends(target);
  1103. this->ProjectToTargetMap[projectName].insert(tgtdeps.begin(),
  1104. tgtdeps.end());
  1105. }
  1106. }
  1107. }
  1108. // Now restore the EXCLUDE_FROM_ALL property on the project top
  1109. // makefile
  1110. if(exclude)
  1111. {
  1112. exclude = excludeSave.c_str();
  1113. }
  1114. projectMakefile->SetProperty("EXCLUDE_FROM_ALL", exclude);
  1115. }
  1116. // dump the map for debug purposes
  1117. // right now this map is not being used, but it was
  1118. // checked in to avoid constant conflicts.
  1119. // It is also the first step to creating sub projects
  1120. // that contain all of the targets they need.
  1121. #if 0
  1122. std::map<cmStdString, std::set<cmTarget*> >::iterator i =
  1123. this->ProjectToTargetMap.begin();
  1124. for(; i != this->ProjectToTargetMap.end(); ++i)
  1125. {
  1126. std::cerr << i->first << "\n";
  1127. std::set<cmTarget*>::iterator t = i->second.begin();
  1128. for(; t != i->second.end(); ++t)
  1129. {
  1130. cmTarget* target = *t;
  1131. std::cerr << "\t" << target->GetName() << "\n";
  1132. }
  1133. }
  1134. #endif
  1135. }
  1136. ///! Find a local generator by its startdirectory
  1137. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  1138. {
  1139. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  1140. for(unsigned int i = 0; i < gens->size(); ++i)
  1141. {
  1142. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  1143. if (sd == start_dir)
  1144. {
  1145. return (*gens)[i];
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. cmTarget* cmGlobalGenerator::FindTarget(const char* project,
  1151. const char* name,
  1152. bool useImportedTargets)
  1153. {
  1154. // if project specific
  1155. if(project)
  1156. {
  1157. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  1158. for(unsigned int i = 0; i < gens->size(); ++i)
  1159. {
  1160. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name,
  1161. useImportedTargets);
  1162. if(ret)
  1163. {
  1164. return ret;
  1165. }
  1166. }
  1167. }
  1168. // if all projects/directories
  1169. else
  1170. {
  1171. std::map<cmStdString,cmTarget *>::iterator i =
  1172. this->TotalTargets.find ( name );
  1173. if ( i != this->TotalTargets.end() )
  1174. {
  1175. return i->second;
  1176. }
  1177. if ( useImportedTargets )
  1178. {
  1179. std::map<cmStdString,cmTarget *>::iterator importedTarget =
  1180. this->ImportedTotalTargets.find ( name );
  1181. if ( importedTarget != this->ImportedTotalTargets.end() )
  1182. {
  1183. return importedTarget->second;
  1184. }
  1185. }
  1186. }
  1187. return 0;
  1188. }
  1189. inline std::string removeQuotes(const std::string& s)
  1190. {
  1191. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1192. {
  1193. return s.substr(1, s.size()-2);
  1194. }
  1195. return s;
  1196. }
  1197. void cmGlobalGenerator::SetCMakeInstance(cmake* cm)
  1198. {
  1199. // Store a pointer to the cmake object instance.
  1200. this->CMakeInstance = cm;
  1201. }
  1202. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1203. {
  1204. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1205. const char* cmakeCfgIntDir = this->GetCMakeCFGInitDirectory();
  1206. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1207. // CPack
  1208. cmCustomCommandLines cpackCommandLines;
  1209. std::vector<std::string> depends;
  1210. cmCustomCommandLine singleLine;
  1211. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1212. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1213. {
  1214. singleLine.push_back("-C");
  1215. singleLine.push_back(cmakeCfgIntDir);
  1216. }
  1217. singleLine.push_back("--config");
  1218. std::string configFile = mf->GetStartOutputDirectory();;
  1219. configFile += "/CPackConfig.cmake";
  1220. singleLine.push_back(configFile);
  1221. cpackCommandLines.push_back(singleLine);
  1222. if ( this->GetPreinstallTargetName() )
  1223. {
  1224. depends.push_back("preinstall");
  1225. }
  1226. if(cmSystemTools::FileExists(configFile.c_str()))
  1227. {
  1228. (*targets)[this->GetPackageTargetName()]
  1229. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1230. "Run CPack packaging tool...",
  1231. &cpackCommandLines, depends);
  1232. }
  1233. // CPack source
  1234. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1235. if ( packageSourceTargetName )
  1236. {
  1237. cpackCommandLines.erase(cpackCommandLines.begin(),
  1238. cpackCommandLines.end());
  1239. singleLine.erase(singleLine.begin(), singleLine.end());
  1240. depends.erase(depends.begin(), depends.end());
  1241. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1242. singleLine.push_back("--config");
  1243. configFile = mf->GetStartOutputDirectory();;
  1244. configFile += "/CPackSourceConfig.cmake";
  1245. if(cmSystemTools::FileExists(configFile.c_str()))
  1246. {
  1247. singleLine.push_back(configFile);
  1248. cpackCommandLines.push_back(singleLine);
  1249. (*targets)[packageSourceTargetName]
  1250. = this->CreateGlobalTarget(packageSourceTargetName,
  1251. "Run CPack packaging tool for source...",
  1252. &cpackCommandLines, depends);
  1253. }
  1254. }
  1255. // Test
  1256. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1257. {
  1258. cpackCommandLines.erase(cpackCommandLines.begin(),
  1259. cpackCommandLines.end());
  1260. singleLine.erase(singleLine.begin(), singleLine.end());
  1261. depends.erase(depends.begin(), depends.end());
  1262. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1263. singleLine.push_back("--force-new-ctest-process");
  1264. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1265. {
  1266. singleLine.push_back("-C");
  1267. singleLine.push_back(cmakeCfgIntDir);
  1268. }
  1269. else // TODO: This is a hack. Should be something to do with the generator
  1270. {
  1271. singleLine.push_back("$(ARGS)");
  1272. }
  1273. cpackCommandLines.push_back(singleLine);
  1274. (*targets)[this->GetTestTargetName()]
  1275. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1276. "Running tests...", &cpackCommandLines, depends);
  1277. }
  1278. //Edit Cache
  1279. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1280. if ( editCacheTargetName )
  1281. {
  1282. cpackCommandLines.erase(cpackCommandLines.begin(),
  1283. cpackCommandLines.end());
  1284. singleLine.erase(singleLine.begin(), singleLine.end());
  1285. depends.erase(depends.begin(), depends.end());
  1286. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1287. // Otherwise default to the interactive command-line interface.
  1288. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1289. {
  1290. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1291. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1292. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1293. cpackCommandLines.push_back(singleLine);
  1294. (*targets)[editCacheTargetName] =
  1295. this->CreateGlobalTarget(
  1296. editCacheTargetName, "Running CMake cache editor...",
  1297. &cpackCommandLines, depends);
  1298. }
  1299. else
  1300. {
  1301. singleLine.push_back(cmakeCommand);
  1302. singleLine.push_back("-i");
  1303. singleLine.push_back(".");
  1304. cpackCommandLines.push_back(singleLine);
  1305. (*targets)[editCacheTargetName] =
  1306. this->CreateGlobalTarget(
  1307. editCacheTargetName,
  1308. "Running interactive CMake command-line interface...",
  1309. &cpackCommandLines, depends);
  1310. }
  1311. }
  1312. //Rebuild Cache
  1313. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1314. if ( rebuildCacheTargetName )
  1315. {
  1316. cpackCommandLines.erase(cpackCommandLines.begin(),
  1317. cpackCommandLines.end());
  1318. singleLine.erase(singleLine.begin(), singleLine.end());
  1319. depends.erase(depends.begin(), depends.end());
  1320. singleLine.push_back(cmakeCommand);
  1321. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1322. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1323. cpackCommandLines.push_back(singleLine);
  1324. (*targets)[rebuildCacheTargetName] =
  1325. this->CreateGlobalTarget(
  1326. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1327. &cpackCommandLines, depends);
  1328. }
  1329. //Install
  1330. if(this->InstallTargetEnabled)
  1331. {
  1332. if(!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.')
  1333. {
  1334. std::set<cmStdString>* componentsSet = &this->InstallComponents;
  1335. cpackCommandLines.erase(cpackCommandLines.begin(),
  1336. cpackCommandLines.end());
  1337. depends.erase(depends.begin(), depends.end());
  1338. cmOStringStream ostr;
  1339. if ( componentsSet->size() > 0 )
  1340. {
  1341. ostr << "Available install components are:";
  1342. std::set<cmStdString>::iterator it;
  1343. for (
  1344. it = componentsSet->begin();
  1345. it != componentsSet->end();
  1346. ++ it )
  1347. {
  1348. ostr << " \"" << it->c_str() << "\"";
  1349. }
  1350. }
  1351. else
  1352. {
  1353. ostr << "Only default component available";
  1354. }
  1355. singleLine.push_back(ostr.str().c_str());
  1356. (*targets)["list_install_components"]
  1357. = this->CreateGlobalTarget("list_install_components",
  1358. ostr.str().c_str(),
  1359. &cpackCommandLines, depends);
  1360. }
  1361. std::string cmd;
  1362. cpackCommandLines.erase(cpackCommandLines.begin(),
  1363. cpackCommandLines.end());
  1364. singleLine.erase(singleLine.begin(), singleLine.end());
  1365. depends.erase(depends.begin(), depends.end());
  1366. if ( this->GetPreinstallTargetName() )
  1367. {
  1368. depends.push_back(this->GetPreinstallTargetName());
  1369. }
  1370. else
  1371. {
  1372. const char* noall =
  1373. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1374. if(!noall || cmSystemTools::IsOff(noall))
  1375. {
  1376. depends.push_back(this->GetAllTargetName());
  1377. }
  1378. }
  1379. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1380. {
  1381. // We are building CMake itself. We cannot use the original
  1382. // executable to install over itself.
  1383. cmd = mf->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  1384. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1385. {
  1386. cmd += "/";
  1387. cmd += cmakeCfgIntDir;
  1388. }
  1389. cmd += "/cmake";
  1390. }
  1391. else
  1392. {
  1393. cmd = cmakeCommand;
  1394. }
  1395. singleLine.push_back(cmd.c_str());
  1396. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1397. {
  1398. std::string cfgArg = "-DBUILD_TYPE=";
  1399. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  1400. singleLine.push_back(cfgArg);
  1401. }
  1402. singleLine.push_back("-P");
  1403. singleLine.push_back("cmake_install.cmake");
  1404. cpackCommandLines.push_back(singleLine);
  1405. (*targets)[this->GetInstallTargetName()] =
  1406. this->CreateGlobalTarget(
  1407. this->GetInstallTargetName(), "Install the project...",
  1408. &cpackCommandLines, depends);
  1409. // install_local
  1410. if(const char* install_local = this->GetInstallLocalTargetName())
  1411. {
  1412. cmCustomCommandLine localCmdLine = singleLine;
  1413. localCmdLine.insert(localCmdLine.begin()+1, "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  1414. cpackCommandLines.erase(cpackCommandLines.begin(),
  1415. cpackCommandLines.end());
  1416. cpackCommandLines.push_back(localCmdLine);
  1417. (*targets)[install_local] =
  1418. this->CreateGlobalTarget(
  1419. install_local, "Installing only the local directory...",
  1420. &cpackCommandLines, depends);
  1421. }
  1422. // install_strip
  1423. const char* install_strip = this->GetInstallStripTargetName();
  1424. if((install_strip !=0) && (mf->IsSet("CMAKE_STRIP")))
  1425. {
  1426. cmCustomCommandLine stripCmdLine = singleLine;
  1427. stripCmdLine.insert(stripCmdLine.begin()+1,"-DCMAKE_INSTALL_DO_STRIP=1");
  1428. cpackCommandLines.erase(cpackCommandLines.begin(),
  1429. cpackCommandLines.end());
  1430. cpackCommandLines.push_back(stripCmdLine);
  1431. (*targets)[install_strip] =
  1432. this->CreateGlobalTarget(
  1433. install_strip, "Installing the project stripped...",
  1434. &cpackCommandLines, depends);
  1435. }
  1436. }
  1437. }
  1438. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  1439. const char* name, const char* message,
  1440. const cmCustomCommandLines* commandLines,
  1441. std::vector<std::string> depends,
  1442. bool depends_on_all /* = false */)
  1443. {
  1444. // Package
  1445. cmTarget target;
  1446. target.GetProperties().SetCMakeInstance(this->CMakeInstance);
  1447. target.SetType(cmTarget::GLOBAL_TARGET, name);
  1448. target.SetProperty("EXCLUDE_FROM_ALL","TRUE");
  1449. std::vector<std::string> no_outputs;
  1450. std::vector<std::string> no_depends;
  1451. // Store the custom command in the target.
  1452. cmCustomCommand cc(no_outputs, no_depends, *commandLines, 0, 0);
  1453. target.GetPostBuildCommands().push_back(cc);
  1454. target.SetProperty("EchoString", message);
  1455. if ( depends_on_all )
  1456. {
  1457. target.AddUtility("all");
  1458. }
  1459. std::vector<std::string>::iterator dit;
  1460. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  1461. {
  1462. target.AddUtility(dit->c_str());
  1463. }
  1464. return target;
  1465. }
  1466. //----------------------------------------------------------------------------
  1467. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  1468. const char*, std::string&)
  1469. {
  1470. // Subclasses that support multiple configurations should implement
  1471. // this method to append the subdirectory for the given build
  1472. // configuration.
  1473. }
  1474. //----------------------------------------------------------------------------
  1475. void cmGlobalGenerator::CheckMultipleOutputs(cmMakefile*, bool)
  1476. {
  1477. // Only certain generators need this check. They define this
  1478. // method.
  1479. }
  1480. //----------------------------------------------------------------------------
  1481. std::vector<cmTarget *>& cmGlobalGenerator
  1482. ::GetTargetDepends(cmTarget& target)
  1483. {
  1484. // if the depends are already in the map then return
  1485. std::map<cmStdString, std::vector<cmTarget *> >::iterator tgtI =
  1486. this->TargetDependencies.find(target.GetName());
  1487. if (tgtI != this->TargetDependencies.end())
  1488. {
  1489. return tgtI->second;
  1490. }
  1491. // A target should not depend on itself.
  1492. std::set<cmStdString> emitted;
  1493. emitted.insert(target.GetName());
  1494. // the vector of results
  1495. std::vector<cmTarget *>& result =
  1496. this->TargetDependencies[target.GetName()];
  1497. // Loop over all library dependencies but not for static libs
  1498. if (target.GetType() != cmTarget::STATIC_LIBRARY)
  1499. {
  1500. const cmTarget::LinkLibraryVectorType& tlibs = target.GetLinkLibraries();
  1501. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  1502. lib != tlibs.end(); ++lib)
  1503. {
  1504. // Don't emit the same library twice for this target.
  1505. if(emitted.insert(lib->first).second)
  1506. {
  1507. cmTarget *target2 =
  1508. target.GetMakefile()->FindTarget(lib->first.c_str(), false);
  1509. // search each local generator until a match is found
  1510. if (!target2)
  1511. {
  1512. target2 = this->FindTarget(0,lib->first.c_str(), false);
  1513. }
  1514. // if a match was found then ...
  1515. if (target2)
  1516. {
  1517. // Add this dependency.
  1518. result.push_back(target2);
  1519. }
  1520. }
  1521. }
  1522. }
  1523. // Loop over all utility dependencies.
  1524. const std::set<cmStdString>& tutils = target.GetUtilities();
  1525. for(std::set<cmStdString>::const_iterator util = tutils.begin();
  1526. util != tutils.end(); ++util)
  1527. {
  1528. // Don't emit the same utility twice for this target.
  1529. if(emitted.insert(*util).second)
  1530. {
  1531. cmTarget *target2 = target.GetMakefile()->FindTarget(util->c_str(), false);
  1532. // search each local generator until a match is found
  1533. if (!target2)
  1534. {
  1535. target2 = this->FindTarget(0,util->c_str(), false);
  1536. }
  1537. // if a match was found then ...
  1538. if (target2)
  1539. {
  1540. // Add this dependency.
  1541. result.push_back(target2);
  1542. }
  1543. }
  1544. }
  1545. return result;
  1546. }
  1547. void cmGlobalGenerator::AddTarget(cmTargets::value_type &v)
  1548. {
  1549. if (v.second.IsImported())
  1550. {
  1551. this->ImportedTotalTargets[v.first] = &v.second;
  1552. }
  1553. else
  1554. {
  1555. this->TotalTargets[v.first] = &v.second;
  1556. }
  1557. }
  1558. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  1559. cmExternalMakefileProjectGenerator *extraGenerator)
  1560. {
  1561. this->ExtraGenerator = extraGenerator;
  1562. if (this->ExtraGenerator!=0)
  1563. {
  1564. this->ExtraGenerator->SetGlobalGenerator(this);
  1565. }
  1566. }
  1567. const char* cmGlobalGenerator::GetExtraGeneratorName() const
  1568. {
  1569. return this->ExtraGenerator==0 ? 0 : this->ExtraGenerator->GetName();
  1570. }