cmGlobalGenerator.cxx 63 KB

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