cmGlobalGenerator.cxx 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #if defined(_WIN32) && !defined(__CYGWIN__)
  11. #include "windows.h" // this must be first to define GetCurrentDirectory
  12. #endif
  13. #include "cmGlobalGenerator.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmExternalMakefileProjectGenerator.h"
  16. #include "cmake.h"
  17. #include "cmMakefile.h"
  18. #include "cmQtAutomoc.h"
  19. #include "cmSourceFile.h"
  20. #include "cmVersion.h"
  21. #include "cmExportInstallFileGenerator.h"
  22. #include "cmComputeTargetDepends.h"
  23. #include "cmGeneratedFileStream.h"
  24. #include "cmGeneratorTarget.h"
  25. #include <cmsys/Directory.hxx>
  26. #if defined(CMAKE_BUILD_WITH_CMAKE)
  27. # include <cmsys/MD5.h>
  28. #endif
  29. #include <stdlib.h> // required for atof
  30. #include <assert.h>
  31. cmGlobalGenerator::cmGlobalGenerator()
  32. {
  33. // By default the .SYMBOLIC dependency is not needed on symbolic rules.
  34. this->NeedSymbolicMark = false;
  35. // by default use the native paths
  36. this->ForceUnixPaths = false;
  37. // By default do not try to support color.
  38. this->ToolSupportsColor = false;
  39. // By default do not use link scripts.
  40. this->UseLinkScript = false;
  41. // Whether an install target is needed.
  42. this->InstallTargetEnabled = false;
  43. // how long to let try compiles run
  44. this->TryCompileTimeout = 0;
  45. this->ExtraGenerator = 0;
  46. this->CurrentLocalGenerator = 0;
  47. this->TryCompileOuterMakefile = 0;
  48. }
  49. cmGlobalGenerator::~cmGlobalGenerator()
  50. {
  51. // Delete any existing cmLocalGenerators
  52. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  53. {
  54. delete this->LocalGenerators[i];
  55. }
  56. this->LocalGenerators.clear();
  57. if (this->ExtraGenerator)
  58. {
  59. delete this->ExtraGenerator;
  60. }
  61. this->ClearGeneratorTargets();
  62. this->ClearExportSets();
  63. }
  64. void cmGlobalGenerator::ResolveLanguageCompiler(const std::string &lang,
  65. cmMakefile *mf,
  66. bool optional) {
  67. std::string langComp = "CMAKE_";
  68. langComp += lang;
  69. langComp += "_COMPILER";
  70. if(!mf->GetDefinition(langComp.c_str()))
  71. {
  72. if(!optional)
  73. {
  74. cmSystemTools::Error(langComp.c_str(),
  75. " not set, after EnableLanguage");
  76. }
  77. return;
  78. }
  79. const char* name = mf->GetRequiredDefinition(langComp.c_str());
  80. std::string path;
  81. if(!cmSystemTools::FileIsFullPath(name))
  82. {
  83. path = cmSystemTools::FindProgram(name);
  84. }
  85. else
  86. {
  87. path = name;
  88. }
  89. if((path.size() == 0 || !cmSystemTools::FileExists(path.c_str()))
  90. && (optional==false))
  91. {
  92. std::string message = "your ";
  93. message += lang;
  94. message += " compiler: \"";
  95. message += name;
  96. message += "\" was not found. Please set ";
  97. message += langComp;
  98. message += " to a valid compiler path or name.";
  99. cmSystemTools::Error(message.c_str());
  100. path = name;
  101. }
  102. std::string doc = lang;
  103. doc += " compiler.";
  104. const char* cname = this->GetCMakeInstance()->
  105. GetCacheManager()->GetCacheValue(langComp.c_str());
  106. std::string changeVars;
  107. if(cname && (path != cname) && (optional==false))
  108. {
  109. std::string cnameString = cname;
  110. std::string pathString = path;
  111. // get rid of potentially multiple slashes:
  112. cmSystemTools::ConvertToUnixSlashes(cnameString);
  113. cmSystemTools::ConvertToUnixSlashes(pathString);
  114. if (cnameString != pathString)
  115. {
  116. const char* cvars =
  117. this->GetCMakeInstance()->GetProperty(
  118. "__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  119. if(cvars)
  120. {
  121. changeVars += cvars;
  122. changeVars += ";";
  123. }
  124. changeVars += langComp;
  125. changeVars += ";";
  126. changeVars += cname;
  127. this->GetCMakeInstance()->SetProperty(
  128. "__CMAKE_DELETE_CACHE_CHANGE_VARS_",
  129. changeVars.c_str());
  130. }
  131. }
  132. mf->AddCacheDefinition(langComp.c_str(), path.c_str(),
  133. doc.c_str(), cmCacheManager::FILEPATH);
  134. }
  135. // Find the make program for the generator, required for try compiles
  136. void cmGlobalGenerator::FindMakeProgram(cmMakefile* mf)
  137. {
  138. if(this->FindMakeProgramFile.size() == 0)
  139. {
  140. cmSystemTools::Error(
  141. "Generator implementation error, "
  142. "all generators must specify this->FindMakeProgramFile");
  143. }
  144. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  145. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  146. {
  147. std::string setMakeProgram =
  148. mf->GetModulesFile(this->FindMakeProgramFile.c_str());
  149. if(setMakeProgram.size())
  150. {
  151. mf->ReadListFile(0, setMakeProgram.c_str());
  152. }
  153. }
  154. if(!mf->GetDefinition("CMAKE_MAKE_PROGRAM")
  155. || cmSystemTools::IsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM")))
  156. {
  157. cmOStringStream err;
  158. err << "CMake was unable to find a build program corresponding to \""
  159. << this->GetName() << "\". CMAKE_MAKE_PROGRAM is not set. You "
  160. << "probably need to select a different build tool.";
  161. cmSystemTools::Error(err.str().c_str());
  162. cmSystemTools::SetFatalErrorOccured();
  163. return;
  164. }
  165. std::string makeProgram = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  166. // if there are spaces in the make program use short path
  167. // but do not short path the actual program name, as
  168. // this can cause trouble with VSExpress
  169. if(makeProgram.find(' ') != makeProgram.npos)
  170. {
  171. std::string dir;
  172. std::string file;
  173. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  174. dir, file);
  175. std::string saveFile = file;
  176. cmSystemTools::GetShortPath(makeProgram.c_str(), makeProgram);
  177. cmSystemTools::SplitProgramPath(makeProgram.c_str(),
  178. dir, file);
  179. makeProgram = dir;
  180. makeProgram += "/";
  181. makeProgram += saveFile;
  182. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM", makeProgram.c_str(),
  183. "make program",
  184. cmCacheManager::FILEPATH);
  185. }
  186. if(makeProgram.find("xcodebuild") != makeProgram.npos)
  187. {
  188. // due to the text file busy /bin/sh problem with xcodebuild
  189. // use the cmakexbuild wrapper instead. This program
  190. // will run xcodebuild and if it sees the error text file busy
  191. // it will stop forwarding output, and let the build finish.
  192. // Then it will retry the build. It will continue this
  193. // untill no text file busy errors occur.
  194. std::string cmakexbuild =
  195. this->CMakeInstance->GetCacheManager()->GetCacheValue("CMAKE_COMMAND");
  196. cmakexbuild = cmakexbuild.substr(0, cmakexbuild.length()-5);
  197. cmakexbuild += "cmakexbuild";
  198. mf->AddCacheDefinition("CMAKE_MAKE_PROGRAM",
  199. cmakexbuild.c_str(),
  200. "make program",
  201. cmCacheManager::FILEPATH);
  202. }
  203. }
  204. // enable the given language
  205. //
  206. // The following files are loaded in this order:
  207. //
  208. // First figure out what OS we are running on:
  209. //
  210. // CMakeSystem.cmake - configured file created by CMakeDetermineSystem.cmake
  211. // CMakeDetermineSystem.cmake - figure out os info and create
  212. // CMakeSystem.cmake IF CMAKE_SYSTEM
  213. // not set
  214. // CMakeSystem.cmake - configured file created by
  215. // CMakeDetermineSystem.cmake IF CMAKE_SYSTEM_LOADED
  216. // Next try and enable all languages found in the languages vector
  217. //
  218. // FOREACH LANG in languages
  219. // CMake(LANG)Compiler.cmake - configured file create by
  220. // CMakeDetermine(LANG)Compiler.cmake
  221. // CMakeDetermine(LANG)Compiler.cmake - Finds compiler for LANG and
  222. // creates CMake(LANG)Compiler.cmake
  223. // CMake(LANG)Compiler.cmake - configured file created by
  224. // CMakeDetermine(LANG)Compiler.cmake
  225. //
  226. // CMakeSystemSpecificInformation.cmake
  227. // - includes Platform/${CMAKE_SYSTEM_NAME}.cmake
  228. // may use compiler stuff
  229. // FOREACH LANG in languages
  230. // CMake(LANG)Information.cmake
  231. // - loads Platform/${CMAKE_SYSTEM_NAME}-${COMPILER}.cmake
  232. // CMakeTest(LANG)Compiler.cmake
  233. // - Make sure the compiler works with a try compile if
  234. // CMakeDetermine(LANG) was loaded
  235. //
  236. // Now load a few files that can override values set in any of the above
  237. // (PROJECTNAME)Compatibility.cmake
  238. // - load any backwards compatibility stuff for current project
  239. // ${CMAKE_USER_MAKE_RULES_OVERRIDE}
  240. // - allow users a chance to override system variables
  241. //
  242. //
  243. void
  244. cmGlobalGenerator::EnableLanguage(std::vector<std::string>const& languages,
  245. cmMakefile *mf, bool)
  246. {
  247. if(languages.size() == 0)
  248. {
  249. cmSystemTools::Error("EnableLanguage must have a lang specified!");
  250. cmSystemTools::SetFatalErrorOccured();
  251. return;
  252. }
  253. if(this->TryCompileOuterMakefile)
  254. {
  255. // In a try-compile we can only enable languages provided by caller.
  256. for(std::vector<std::string>::const_iterator li = languages.begin();
  257. li != languages.end(); ++li)
  258. {
  259. if(*li == "NONE")
  260. {
  261. this->SetLanguageEnabled("NONE", mf);
  262. }
  263. else
  264. {
  265. const char* lang = li->c_str();
  266. if(this->LanguagesReady.find(lang) == this->LanguagesReady.end())
  267. {
  268. cmOStringStream e;
  269. e << "The test project needs language "
  270. << lang << " which is not enabled.";
  271. this->TryCompileOuterMakefile
  272. ->IssueMessage(cmake::FATAL_ERROR, e.str());
  273. cmSystemTools::SetFatalErrorOccured();
  274. return;
  275. }
  276. }
  277. }
  278. }
  279. mf->AddDefinition("RUN_CONFIGURE", true);
  280. std::string rootBin = mf->GetHomeOutputDirectory();
  281. rootBin += cmake::GetCMakeFilesDirectory();
  282. // If the configuration files path has been set,
  283. // then we are in a try compile and need to copy the enable language
  284. // files from the parent cmake bin dir, into the try compile bin dir
  285. if(this->ConfiguredFilesPath.size())
  286. {
  287. rootBin = this->ConfiguredFilesPath;
  288. }
  289. rootBin += "/";
  290. rootBin += cmVersion::GetCMakeVersion();
  291. // set the dir for parent files so they can be used by modules
  292. mf->AddDefinition("CMAKE_PLATFORM_INFO_DIR",rootBin.c_str());
  293. // find and make sure CMAKE_MAKE_PROGRAM is defined
  294. this->FindMakeProgram(mf);
  295. // try and load the CMakeSystem.cmake if it is there
  296. std::string fpath = rootBin;
  297. if(!mf->GetDefinition("CMAKE_SYSTEM_LOADED"))
  298. {
  299. fpath += "/CMakeSystem.cmake";
  300. if(cmSystemTools::FileExists(fpath.c_str()))
  301. {
  302. mf->ReadListFile(0,fpath.c_str());
  303. }
  304. }
  305. // Load the CMakeDetermineSystem.cmake file and find out
  306. // what platform we are running on
  307. if (!mf->GetDefinition("CMAKE_SYSTEM"))
  308. {
  309. #if defined(_WIN32) && !defined(__CYGWIN__)
  310. /* Windows version number data. */
  311. OSVERSIONINFO osvi;
  312. ZeroMemory(&osvi, sizeof(osvi));
  313. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  314. GetVersionEx (&osvi);
  315. cmOStringStream windowsVersionString;
  316. windowsVersionString << osvi.dwMajorVersion << "." << osvi.dwMinorVersion;
  317. windowsVersionString.str();
  318. mf->AddDefinition("CMAKE_HOST_SYSTEM_VERSION",
  319. windowsVersionString.str().c_str());
  320. #endif
  321. // Read the DetermineSystem file
  322. std::string systemFile = mf->GetModulesFile("CMakeDetermineSystem.cmake");
  323. mf->ReadListFile(0, systemFile.c_str());
  324. // load the CMakeSystem.cmake from the binary directory
  325. // this file is configured by the CMakeDetermineSystem.cmake file
  326. fpath = rootBin;
  327. fpath += "/CMakeSystem.cmake";
  328. mf->ReadListFile(0,fpath.c_str());
  329. }
  330. std::map<cmStdString, bool> needTestLanguage;
  331. std::map<cmStdString, bool> needSetLanguageEnabledMaps;
  332. // foreach language
  333. // load the CMakeDetermine(LANG)Compiler.cmake file to find
  334. // the compiler
  335. for(std::vector<std::string>::const_iterator l = languages.begin();
  336. l != languages.end(); ++l)
  337. {
  338. const char* lang = l->c_str();
  339. needSetLanguageEnabledMaps[lang] = false;
  340. if(*l == "NONE")
  341. {
  342. this->SetLanguageEnabled("NONE", mf);
  343. continue;
  344. }
  345. std::string loadedLang = "CMAKE_";
  346. loadedLang += lang;
  347. loadedLang += "_COMPILER_LOADED";
  348. if(!mf->GetDefinition(loadedLang.c_str()))
  349. {
  350. fpath = rootBin;
  351. fpath += "/CMake";
  352. fpath += lang;
  353. fpath += "Compiler.cmake";
  354. // If the existing build tree was already configured with this
  355. // version of CMake then try to load the configured file first
  356. // to avoid duplicate compiler tests.
  357. if(cmSystemTools::FileExists(fpath.c_str()))
  358. {
  359. if(!mf->ReadListFile(0,fpath.c_str()))
  360. {
  361. cmSystemTools::Error("Could not find cmake module file:",
  362. fpath.c_str());
  363. }
  364. // if this file was found then the language was already determined
  365. // to be working
  366. needTestLanguage[lang] = false;
  367. this->SetLanguageEnabledFlag(lang, mf);
  368. needSetLanguageEnabledMaps[lang] = true;
  369. // this can only be called after loading CMake(LANG)Compiler.cmake
  370. }
  371. }
  372. if(!this->GetLanguageEnabled(lang) )
  373. {
  374. if (this->CMakeInstance->GetIsInTryCompile())
  375. {
  376. cmSystemTools::Error("This should not have happen. "
  377. "If you see this message, you are probably "
  378. "using a broken CMakeLists.txt file or a "
  379. "problematic release of CMake");
  380. }
  381. // if the CMake(LANG)Compiler.cmake file was not found then
  382. // load CMakeDetermine(LANG)Compiler.cmake
  383. std::string determineCompiler = "CMakeDetermine";
  384. determineCompiler += lang;
  385. determineCompiler += "Compiler.cmake";
  386. std::string determineFile =
  387. mf->GetModulesFile(determineCompiler.c_str());
  388. if(!mf->ReadListFile(0,determineFile.c_str()))
  389. {
  390. cmSystemTools::Error("Could not find cmake module file:",
  391. determineFile.c_str());
  392. }
  393. needTestLanguage[lang] = true;
  394. // Some generators like visual studio should not use the env variables
  395. // So the global generator can specify that in this variable
  396. if(!mf->GetDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV"))
  397. {
  398. // put ${CMake_(LANG)_COMPILER_ENV_VAR}=${CMAKE_(LANG)_COMPILER
  399. // into the environment, in case user scripts want to run
  400. // configure, or sub cmakes
  401. std::string compilerName = "CMAKE_";
  402. compilerName += lang;
  403. compilerName += "_COMPILER";
  404. std::string compilerEnv = "CMAKE_";
  405. compilerEnv += lang;
  406. compilerEnv += "_COMPILER_ENV_VAR";
  407. std::string envVar = mf->GetRequiredDefinition(compilerEnv.c_str());
  408. std::string envVarValue =
  409. mf->GetRequiredDefinition(compilerName.c_str());
  410. std::string env = envVar;
  411. env += "=";
  412. env += envVarValue;
  413. cmSystemTools::PutEnv(env.c_str());
  414. }
  415. // if determineLanguage was called then load the file it
  416. // configures CMake(LANG)Compiler.cmake
  417. fpath = rootBin;
  418. fpath += "/CMake";
  419. fpath += lang;
  420. fpath += "Compiler.cmake";
  421. if(!mf->ReadListFile(0,fpath.c_str()))
  422. {
  423. cmSystemTools::Error("Could not find cmake module file:",
  424. fpath.c_str());
  425. }
  426. this->SetLanguageEnabledFlag(lang, mf);
  427. needSetLanguageEnabledMaps[lang] = true;
  428. // this can only be called after loading CMake(LANG)Compiler.cmake
  429. // the language must be enabled for try compile to work, but we do
  430. // not know if it is a working compiler yet so set the test language
  431. // flag
  432. needTestLanguage[lang] = true;
  433. } // end if(!this->GetLanguageEnabled(lang) )
  434. } // end loop over languages
  435. // **** Load the system specific information if not yet loaded
  436. if (!mf->GetDefinition("CMAKE_SYSTEM_SPECIFIC_INFORMATION_LOADED"))
  437. {
  438. fpath = mf->GetModulesFile("CMakeSystemSpecificInformation.cmake");
  439. if(!mf->ReadListFile(0,fpath.c_str()))
  440. {
  441. cmSystemTools::Error("Could not find cmake module file:",
  442. fpath.c_str());
  443. }
  444. }
  445. // loop over languages again loading CMake(LANG)Information.cmake
  446. //
  447. for(std::vector<std::string>::const_iterator l = languages.begin();
  448. l != languages.end(); ++l)
  449. {
  450. const char* lang = l->c_str();
  451. if(*l == "NONE")
  452. {
  453. this->SetLanguageEnabled("NONE", mf);
  454. continue;
  455. }
  456. std::string langLoadedVar = "CMAKE_";
  457. langLoadedVar += lang;
  458. langLoadedVar += "_INFORMATION_LOADED";
  459. if (!mf->GetDefinition(langLoadedVar.c_str()))
  460. {
  461. fpath = "CMake";
  462. fpath += lang;
  463. fpath += "Information.cmake";
  464. std::string informationFile = mf->GetModulesFile(fpath.c_str());
  465. if (informationFile.empty())
  466. {
  467. cmSystemTools::Error("Could not find cmake module file:",
  468. fpath.c_str());
  469. }
  470. else if(!mf->ReadListFile(0, informationFile.c_str()))
  471. {
  472. cmSystemTools::Error("Could not process cmake module file:",
  473. informationFile.c_str());
  474. }
  475. }
  476. if (needSetLanguageEnabledMaps[lang])
  477. {
  478. this->SetLanguageEnabledMaps(lang, mf);
  479. }
  480. this->LanguagesReady.insert(lang);
  481. std::string compilerName = "CMAKE_";
  482. compilerName += lang;
  483. compilerName += "_COMPILER";
  484. std::string compilerLangFile = rootBin;
  485. compilerLangFile += "/CMake";
  486. compilerLangFile += lang;
  487. compilerLangFile += "Compiler.cmake";
  488. // Test the compiler for the language just setup
  489. // (but only if a compiler has been actually found)
  490. // At this point we should have enough info for a try compile
  491. // which is used in the backward stuff
  492. // If the language is untested then test it now with a try compile.
  493. if (!mf->IsSet(compilerName.c_str()))
  494. {
  495. // if the compiler did not work, then remove the
  496. // CMake(LANG)Compiler.cmake file so that it will get tested the
  497. // next time cmake is run
  498. cmSystemTools::RemoveFile(compilerLangFile.c_str());
  499. }
  500. else if(needTestLanguage[lang])
  501. {
  502. if (!this->CMakeInstance->GetIsInTryCompile())
  503. {
  504. std::string testLang = "CMakeTest";
  505. testLang += lang;
  506. testLang += "Compiler.cmake";
  507. std::string ifpath = mf->GetModulesFile(testLang.c_str());
  508. if(!mf->ReadListFile(0,ifpath.c_str()))
  509. {
  510. cmSystemTools::Error("Could not find cmake module file:",
  511. ifpath.c_str());
  512. }
  513. std::string compilerWorks = "CMAKE_";
  514. compilerWorks += lang;
  515. compilerWorks += "_COMPILER_WORKS";
  516. // if the compiler did not work, then remove the
  517. // CMake(LANG)Compiler.cmake file so that it will get tested the
  518. // next time cmake is run
  519. if(!mf->IsOn(compilerWorks.c_str()))
  520. {
  521. cmSystemTools::RemoveFile(compilerLangFile.c_str());
  522. }
  523. else
  524. {
  525. // load backwards compatibility stuff for C and CXX
  526. // for old versions of CMake ListFiles C and CXX had some
  527. // backwards compatibility files they have to load
  528. // These files have a bunch of try compiles in them so
  529. // should only be done
  530. if (mf->NeedBackwardsCompatibility(1,4))
  531. {
  532. if(strcmp(lang, "C") == 0)
  533. {
  534. ifpath =
  535. mf->GetModulesFile("CMakeBackwardCompatibilityC.cmake");
  536. mf->ReadListFile(0,ifpath.c_str());
  537. }
  538. if(strcmp(lang, "CXX") == 0)
  539. {
  540. ifpath =
  541. mf->GetModulesFile("CMakeBackwardCompatibilityCXX.cmake");
  542. mf->ReadListFile(0,ifpath.c_str());
  543. }
  544. }
  545. }
  546. } // end if in try compile
  547. } // end need test language
  548. // Store the shared library flags so that we can satisfy CMP0018
  549. std::string sharedLibFlagsVar = "CMAKE_SHARED_LIBRARY_";
  550. sharedLibFlagsVar += lang;
  551. sharedLibFlagsVar += "_FLAGS";
  552. const char* sharedLibFlags =
  553. mf->GetSafeDefinition(sharedLibFlagsVar.c_str());
  554. if (sharedLibFlags)
  555. {
  556. this->LanguageToOriginalSharedLibFlags[lang] = sharedLibFlags;
  557. }
  558. } // end for each language
  559. // Now load files that can override any settings on the platform or for
  560. // the project First load the project compatibility file if it is in
  561. // cmake
  562. std::string projectCompatibility = mf->GetDefinition("CMAKE_ROOT");
  563. projectCompatibility += "/Modules/";
  564. projectCompatibility += mf->GetSafeDefinition("PROJECT_NAME");
  565. projectCompatibility += "Compatibility.cmake";
  566. if(cmSystemTools::FileExists(projectCompatibility.c_str()))
  567. {
  568. mf->ReadListFile(0,projectCompatibility.c_str());
  569. }
  570. }
  571. //----------------------------------------------------------------------------
  572. const char*
  573. cmGlobalGenerator::GetLanguageOutputExtension(cmSourceFile const& source)
  574. {
  575. if(const char* lang = source.GetLanguage())
  576. {
  577. if(this->LanguageToOutputExtension.count(lang) > 0)
  578. {
  579. return this->LanguageToOutputExtension[lang].c_str();
  580. }
  581. }
  582. else
  583. {
  584. // if no language is found then check to see if it is already an
  585. // ouput extension for some language. In that case it should be ignored
  586. // and in this map, so it will not be compiled but will just be used.
  587. std::string const& ext = source.GetExtension();
  588. if(!ext.empty())
  589. {
  590. if(this->OutputExtensions.count(ext))
  591. {
  592. return ext.c_str();
  593. }
  594. }
  595. }
  596. return "";
  597. }
  598. const char* cmGlobalGenerator::GetLanguageFromExtension(const char* ext)
  599. {
  600. // if there is an extension and it starts with . then move past the
  601. // . because the extensions are not stored with a . in the map
  602. if(ext && *ext == '.')
  603. {
  604. ++ext;
  605. }
  606. if(this->ExtensionToLanguage.count(ext) > 0)
  607. {
  608. return this->ExtensionToLanguage[ext].c_str();
  609. }
  610. return 0;
  611. }
  612. /* SetLanguageEnabled() is now split in two parts:
  613. at first the enabled-flag is set. This can then be used in EnabledLanguage()
  614. for checking whether the language is already enabled. After setting this
  615. flag still the values from the cmake variables have to be copied into the
  616. internal maps, this is done in SetLanguageEnabledMaps() which is called
  617. after the system- and compiler specific files have been loaded.
  618. This split was done originally so that compiler-specific configuration
  619. files could change the object file extension
  620. (CMAKE_<LANG>_OUTPUT_EXTENSION) before the CMake variables were copied
  621. to the C++ maps.
  622. */
  623. void cmGlobalGenerator::SetLanguageEnabled(const char* l, cmMakefile* mf)
  624. {
  625. this->SetLanguageEnabledFlag(l, mf);
  626. this->SetLanguageEnabledMaps(l, mf);
  627. }
  628. void cmGlobalGenerator::SetLanguageEnabledFlag(const char* l, cmMakefile* mf)
  629. {
  630. this->LanguageEnabled[l] = true;
  631. // Fill the language-to-extension map with the current variable
  632. // settings to make sure it is available for the try_compile()
  633. // command source file signature. In SetLanguageEnabledMaps this
  634. // will be done again to account for any compiler- or
  635. // platform-specific entries.
  636. this->FillExtensionToLanguageMap(l, mf);
  637. }
  638. void cmGlobalGenerator::SetLanguageEnabledMaps(const char* l, cmMakefile* mf)
  639. {
  640. // use LanguageToLinkerPreference to detect whether this functions has
  641. // run before
  642. if (this->LanguageToLinkerPreference.find(l) !=
  643. this->LanguageToLinkerPreference.end())
  644. {
  645. return;
  646. }
  647. std::string linkerPrefVar = std::string("CMAKE_") +
  648. std::string(l) + std::string("_LINKER_PREFERENCE");
  649. const char* linkerPref = mf->GetDefinition(linkerPrefVar.c_str());
  650. int preference = 0;
  651. if(linkerPref)
  652. {
  653. if (sscanf(linkerPref, "%d", &preference)!=1)
  654. {
  655. // backward compatibility: before 2.6 LINKER_PREFERENCE
  656. // was either "None" or "Prefered", and only the first character was
  657. // tested. So if there is a custom language out there and it is
  658. // "Prefered", set its preference high
  659. if (linkerPref[0]=='P')
  660. {
  661. preference = 100;
  662. }
  663. else
  664. {
  665. preference = 0;
  666. }
  667. }
  668. }
  669. if (preference < 0)
  670. {
  671. std::string msg = linkerPrefVar;
  672. msg += " is negative, adjusting it to 0";
  673. cmSystemTools::Message(msg.c_str(), "Warning");
  674. preference = 0;
  675. }
  676. this->LanguageToLinkerPreference[l] = preference;
  677. std::string outputExtensionVar = std::string("CMAKE_") +
  678. std::string(l) + std::string("_OUTPUT_EXTENSION");
  679. const char* outputExtension = mf->GetDefinition(outputExtensionVar.c_str());
  680. if(outputExtension)
  681. {
  682. this->LanguageToOutputExtension[l] = outputExtension;
  683. this->OutputExtensions[outputExtension] = outputExtension;
  684. if(outputExtension[0] == '.')
  685. {
  686. this->OutputExtensions[outputExtension+1] = outputExtension+1;
  687. }
  688. }
  689. // The map was originally filled by SetLanguageEnabledFlag, but
  690. // since then the compiler- and platform-specific files have been
  691. // loaded which might have added more entries.
  692. this->FillExtensionToLanguageMap(l, mf);
  693. std::string ignoreExtensionsVar = std::string("CMAKE_") +
  694. std::string(l) + std::string("_IGNORE_EXTENSIONS");
  695. std::string ignoreExts = mf->GetSafeDefinition(ignoreExtensionsVar.c_str());
  696. std::vector<std::string> extensionList;
  697. cmSystemTools::ExpandListArgument(ignoreExts, extensionList);
  698. for(std::vector<std::string>::iterator i = extensionList.begin();
  699. i != extensionList.end(); ++i)
  700. {
  701. this->IgnoreExtensions[*i] = true;
  702. }
  703. }
  704. void cmGlobalGenerator::FillExtensionToLanguageMap(const char* l,
  705. cmMakefile* mf)
  706. {
  707. std::string extensionsVar = std::string("CMAKE_") +
  708. std::string(l) + std::string("_SOURCE_FILE_EXTENSIONS");
  709. std::string exts = mf->GetSafeDefinition(extensionsVar.c_str());
  710. std::vector<std::string> extensionList;
  711. cmSystemTools::ExpandListArgument(exts, extensionList);
  712. for(std::vector<std::string>::iterator i = extensionList.begin();
  713. i != extensionList.end(); ++i)
  714. {
  715. this->ExtensionToLanguage[*i] = l;
  716. }
  717. }
  718. bool cmGlobalGenerator::IgnoreFile(const char* l)
  719. {
  720. if(this->GetLanguageFromExtension(l))
  721. {
  722. return false;
  723. }
  724. return (this->IgnoreExtensions.count(l) > 0);
  725. }
  726. bool cmGlobalGenerator::GetLanguageEnabled(const char* l) const
  727. {
  728. return (this->LanguageEnabled.find(l)!= this->LanguageEnabled.end());
  729. }
  730. void cmGlobalGenerator::ClearEnabledLanguages()
  731. {
  732. this->LanguageEnabled.clear();
  733. }
  734. bool cmGlobalGenerator::IsDependedOn(const char* project,
  735. cmTarget* targetIn)
  736. {
  737. // Get all local gens for this project
  738. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  739. // loop over local gens and get the targets for each one
  740. for(unsigned int i = 0; i < gens->size(); ++i)
  741. {
  742. cmTargets& targets = (*gens)[i]->GetMakefile()->GetTargets();
  743. for (cmTargets::iterator l = targets.begin();
  744. l != targets.end(); l++)
  745. {
  746. cmTarget& target = l->second;
  747. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  748. if(tgtdeps.count(targetIn))
  749. {
  750. return true;
  751. }
  752. }
  753. }
  754. return false;
  755. }
  756. void cmGlobalGenerator::Configure()
  757. {
  758. this->FirstTimeProgress = 0.0f;
  759. this->ClearGeneratorTargets();
  760. this->ClearExportSets();
  761. // Delete any existing cmLocalGenerators
  762. unsigned int i;
  763. for (i = 0; i < this->LocalGenerators.size(); ++i)
  764. {
  765. delete this->LocalGenerators[i];
  766. }
  767. this->LocalGenerators.clear();
  768. this->TargetDependencies.clear();
  769. this->TotalTargets.clear();
  770. this->LocalGeneratorToTargetMap.clear();
  771. this->ProjectMap.clear();
  772. this->RuleHashes.clear();
  773. this->DirectoryContentMap.clear();
  774. this->BinaryDirectories.clear();
  775. // start with this directory
  776. cmLocalGenerator *lg = this->CreateLocalGenerator();
  777. this->LocalGenerators.push_back(lg);
  778. // set the Start directories
  779. cmMakefile* mf = lg->GetMakefile();
  780. lg->GetMakefile()->SetStartDirectory
  781. (this->CMakeInstance->GetStartDirectory());
  782. lg->GetMakefile()->SetStartOutputDirectory
  783. (this->CMakeInstance->GetStartOutputDirectory());
  784. lg->GetMakefile()->MakeStartDirectoriesCurrent();
  785. this->BinaryDirectories.insert(mf->GetStartOutputDirectory());
  786. // now do it
  787. lg->Configure();
  788. // update the cache entry for the number of local generators, this is used
  789. // for progress
  790. char num[100];
  791. sprintf(num,"%d",static_cast<int>(this->LocalGenerators.size()));
  792. this->GetCMakeInstance()->AddCacheEntry
  793. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS", num,
  794. "number of local generators", cmCacheManager::INTERNAL);
  795. // check for link libraries and include directories containing "NOTFOUND"
  796. // and for infinite loops
  797. this->CheckLocalGenerators();
  798. // at this point this->LocalGenerators has been filled,
  799. // so create the map from project name to vector of local generators
  800. this->FillProjectMap();
  801. if ( this->CMakeInstance->GetWorkingMode() == cmake::NORMAL_MODE)
  802. {
  803. const char* msg = "Configuring done";
  804. if(cmSystemTools::GetErrorOccuredFlag())
  805. {
  806. msg = "Configuring incomplete, errors occurred!";
  807. }
  808. this->CMakeInstance->UpdateProgress(msg, -1);
  809. }
  810. }
  811. bool cmGlobalGenerator::CheckALLOW_DUPLICATE_CUSTOM_TARGETS()
  812. {
  813. // If the property is not enabled then okay.
  814. if(!this->CMakeInstance
  815. ->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  816. {
  817. return true;
  818. }
  819. // This generator does not support duplicate custom targets.
  820. cmOStringStream e;
  821. e << "This project has enabled the ALLOW_DUPLICATE_CUSTOM_TARGETS "
  822. << "global property. "
  823. << "The \"" << this->GetName() << "\" generator does not support "
  824. << "duplicate custom targets. "
  825. << "Consider using a Makefiles generator or fix the project to not "
  826. << "use duplicat target names.";
  827. cmSystemTools::Error(e.str().c_str());
  828. return false;
  829. }
  830. void cmGlobalGenerator::Generate()
  831. {
  832. // Some generators track files replaced during the Generate.
  833. // Start with an empty vector:
  834. this->FilesReplacedDuringGenerate.clear();
  835. // Check whether this generator is allowed to run.
  836. if(!this->CheckALLOW_DUPLICATE_CUSTOM_TARGETS())
  837. {
  838. return;
  839. }
  840. // Check that all targets are valid.
  841. if(!this->CheckTargets())
  842. {
  843. return;
  844. }
  845. // Iterate through all targets and set up automoc for those which have
  846. // the AUTOMOC property set
  847. this->CreateAutomocTargets();
  848. // For each existing cmLocalGenerator
  849. unsigned int i;
  850. // Put a copy of each global target in every directory.
  851. cmTargets globalTargets;
  852. this->CreateDefaultGlobalTargets(&globalTargets);
  853. for (i = 0; i < this->LocalGenerators.size(); ++i)
  854. {
  855. cmMakefile* mf = this->LocalGenerators[i]->GetMakefile();
  856. cmTargets* targets = &(mf->GetTargets());
  857. cmTargets::iterator tit;
  858. for ( tit = globalTargets.begin(); tit != globalTargets.end(); ++ tit )
  859. {
  860. (*targets)[tit->first] = tit->second;
  861. (*targets)[tit->first].SetMakefile(mf);
  862. }
  863. }
  864. // Add generator specific helper commands
  865. for (i = 0; i < this->LocalGenerators.size(); ++i)
  866. {
  867. this->LocalGenerators[i]->AddHelperCommands();
  868. }
  869. // Trace the dependencies, after that no custom commands should be added
  870. // because their dependencies might not be handled correctly
  871. for (i = 0; i < this->LocalGenerators.size(); ++i)
  872. {
  873. this->LocalGenerators[i]->TraceDependencies();
  874. }
  875. // Compute the manifest of main targets generated.
  876. for (i = 0; i < this->LocalGenerators.size(); ++i)
  877. {
  878. this->LocalGenerators[i]->GenerateTargetManifest();
  879. }
  880. // Create per-target generator information.
  881. this->CreateGeneratorTargets();
  882. // Compute the inter-target dependencies.
  883. if(!this->ComputeTargetDepends())
  884. {
  885. return;
  886. }
  887. // Create a map from local generator to the complete set of targets
  888. // it builds by default.
  889. this->FillLocalGeneratorToTargetMap();
  890. // Generate project files
  891. for (i = 0; i < this->LocalGenerators.size(); ++i)
  892. {
  893. this->SetCurrentLocalGenerator(this->LocalGenerators[i]);
  894. this->LocalGenerators[i]->Generate();
  895. this->LocalGenerators[i]->GenerateInstallRules();
  896. this->LocalGenerators[i]->GenerateTestFiles();
  897. this->CMakeInstance->UpdateProgress("Generating",
  898. (static_cast<float>(i)+1.0f)/
  899. static_cast<float>(this->LocalGenerators.size()));
  900. }
  901. this->SetCurrentLocalGenerator(0);
  902. // Update rule hashes.
  903. this->CheckRuleHashes();
  904. this->WriteSummary();
  905. if (this->ExtraGenerator != 0)
  906. {
  907. this->ExtraGenerator->Generate();
  908. }
  909. this->CMakeInstance->UpdateProgress("Generating done", -1);
  910. }
  911. //----------------------------------------------------------------------------
  912. bool cmGlobalGenerator::ComputeTargetDepends()
  913. {
  914. cmComputeTargetDepends ctd(this);
  915. if(!ctd.Compute())
  916. {
  917. return false;
  918. }
  919. std::vector<cmTarget*> const& targets = ctd.GetTargets();
  920. for(std::vector<cmTarget*>::const_iterator ti = targets.begin();
  921. ti != targets.end(); ++ti)
  922. {
  923. ctd.GetTargetDirectDepends(*ti, this->TargetDependencies[*ti]);
  924. }
  925. return true;
  926. }
  927. //----------------------------------------------------------------------------
  928. bool cmGlobalGenerator::CheckTargets()
  929. {
  930. // Make sure all targets can find their source files.
  931. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  932. {
  933. cmTargets& targets =
  934. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  935. for(cmTargets::iterator ti = targets.begin();
  936. ti != targets.end(); ++ti)
  937. {
  938. cmTarget& target = ti->second;
  939. if(target.GetType() == cmTarget::EXECUTABLE ||
  940. target.GetType() == cmTarget::STATIC_LIBRARY ||
  941. target.GetType() == cmTarget::SHARED_LIBRARY ||
  942. target.GetType() == cmTarget::MODULE_LIBRARY ||
  943. target.GetType() == cmTarget::UTILITY)
  944. {
  945. if(!target.FindSourceFiles())
  946. {
  947. return false;
  948. }
  949. }
  950. }
  951. }
  952. return true;
  953. }
  954. //----------------------------------------------------------------------------
  955. void cmGlobalGenerator::CreateAutomocTargets()
  956. {
  957. #ifdef CMAKE_BUILD_WITH_CMAKE
  958. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  959. {
  960. cmTargets& targets =
  961. this->LocalGenerators[i]->GetMakefile()->GetTargets();
  962. for(cmTargets::iterator ti = targets.begin();
  963. ti != targets.end(); ++ti)
  964. {
  965. cmTarget& target = ti->second;
  966. if(target.GetType() == cmTarget::EXECUTABLE ||
  967. target.GetType() == cmTarget::STATIC_LIBRARY ||
  968. target.GetType() == cmTarget::SHARED_LIBRARY ||
  969. target.GetType() == cmTarget::MODULE_LIBRARY)
  970. {
  971. if(target.GetPropertyAsBool("AUTOMOC") && !target.IsImported())
  972. {
  973. cmQtAutomoc automoc;
  974. automoc.SetupAutomocTarget(&target);
  975. }
  976. }
  977. }
  978. }
  979. #endif
  980. }
  981. //----------------------------------------------------------------------------
  982. void cmGlobalGenerator::CreateGeneratorTargets()
  983. {
  984. // Construct per-target generator information.
  985. for(unsigned int i=0; i < this->LocalGenerators.size(); ++i)
  986. {
  987. cmGeneratorTargetsType generatorTargets;
  988. cmMakefile *mf = this->LocalGenerators[i]->GetMakefile();
  989. const char *noconfig_compile_definitions =
  990. mf->GetProperty("COMPILE_DEFINITIONS");
  991. std::vector<std::string> configs;
  992. mf->GetConfigurations(configs);
  993. cmTargets& targets = mf->GetTargets();
  994. for(cmTargets::iterator ti = targets.begin();
  995. ti != targets.end(); ++ti)
  996. {
  997. cmTarget* t = &ti->second;
  998. {
  999. t->AppendProperty("COMPILE_DEFINITIONS", noconfig_compile_definitions);
  1000. for(std::vector<std::string>::const_iterator ci = configs.begin();
  1001. ci != configs.end(); ++ci)
  1002. {
  1003. std::string defPropName = "COMPILE_DEFINITIONS_";
  1004. defPropName += cmSystemTools::UpperCase(*ci);
  1005. t->AppendProperty(defPropName.c_str(),
  1006. mf->GetProperty(defPropName.c_str()));
  1007. }
  1008. }
  1009. cmGeneratorTarget* gt = new cmGeneratorTarget(t);
  1010. this->GeneratorTargets[t] = gt;
  1011. this->ComputeTargetObjects(gt);
  1012. generatorTargets[t] = gt;
  1013. }
  1014. mf->SetGeneratorTargets(generatorTargets);
  1015. }
  1016. }
  1017. //----------------------------------------------------------------------------
  1018. void cmGlobalGenerator::ClearGeneratorTargets()
  1019. {
  1020. for(cmGeneratorTargetsType::iterator i = this->GeneratorTargets.begin();
  1021. i != this->GeneratorTargets.end(); ++i)
  1022. {
  1023. delete i->second;
  1024. }
  1025. this->GeneratorTargets.clear();
  1026. }
  1027. //----------------------------------------------------------------------------
  1028. cmGeneratorTarget* cmGlobalGenerator::GetGeneratorTarget(cmTarget* t) const
  1029. {
  1030. cmGeneratorTargetsType::const_iterator ti = this->GeneratorTargets.find(t);
  1031. if(ti == this->GeneratorTargets.end())
  1032. {
  1033. this->CMakeInstance->IssueMessage(
  1034. cmake::INTERNAL_ERROR, "Missing cmGeneratorTarget instance!",
  1035. cmListFileBacktrace());
  1036. return 0;
  1037. }
  1038. return ti->second;
  1039. }
  1040. //----------------------------------------------------------------------------
  1041. void cmGlobalGenerator::ComputeTargetObjects(cmGeneratorTarget*) const
  1042. {
  1043. // Implemented in generator subclasses that need this.
  1044. }
  1045. void cmGlobalGenerator::CheckLocalGenerators()
  1046. {
  1047. std::map<cmStdString, cmStdString> notFoundMap;
  1048. // std::set<cmStdString> notFoundMap;
  1049. // after it is all done do a ConfigureFinalPass
  1050. cmCacheManager* manager = 0;
  1051. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i)
  1052. {
  1053. manager = this->LocalGenerators[i]->GetMakefile()->GetCacheManager();
  1054. this->LocalGenerators[i]->ConfigureFinalPass();
  1055. cmGeneratorTargetsType targets =
  1056. this->LocalGenerators[i]->GetMakefile()->GetGeneratorTargets();
  1057. for (cmGeneratorTargetsType::iterator l = targets.begin();
  1058. l != targets.end(); l++)
  1059. {
  1060. const cmTarget::LinkLibraryVectorType& libs =
  1061. l->second->Target->GetOriginalLinkLibraries();
  1062. for(cmTarget::LinkLibraryVectorType::const_iterator lib = libs.begin();
  1063. lib != libs.end(); ++lib)
  1064. {
  1065. if(lib->first.size() > 9 &&
  1066. cmSystemTools::IsNOTFOUND(lib->first.c_str()))
  1067. {
  1068. std::string varName = lib->first.substr(0, lib->first.size()-9);
  1069. cmCacheManager::CacheIterator it =
  1070. manager->GetCacheIterator(varName.c_str());
  1071. if(it.GetPropertyAsBool("ADVANCED"))
  1072. {
  1073. varName += " (ADVANCED)";
  1074. }
  1075. std::string text = notFoundMap[varName];
  1076. text += "\n linked by target \"";
  1077. text += l->second->GetName();
  1078. text += "\" in directory ";
  1079. text+=this->LocalGenerators[i]->GetMakefile()->GetCurrentDirectory();
  1080. notFoundMap[varName] = text;
  1081. }
  1082. }
  1083. std::vector<std::string> incs;
  1084. this->LocalGenerators[i]->GetIncludeDirectories(incs, l->second);
  1085. for( std::vector<std::string>::const_iterator incDir = incs.begin();
  1086. incDir != incs.end(); ++incDir)
  1087. {
  1088. if(incDir->size() > 9 &&
  1089. cmSystemTools::IsNOTFOUND(incDir->c_str()))
  1090. {
  1091. std::string varName = incDir->substr(0, incDir->size()-9);
  1092. cmCacheManager::CacheIterator it =
  1093. manager->GetCacheIterator(varName.c_str());
  1094. if(it.GetPropertyAsBool("ADVANCED"))
  1095. {
  1096. varName += " (ADVANCED)";
  1097. }
  1098. std::string text = notFoundMap[varName];
  1099. text += "\n used as include directory in directory ";
  1100. text += this->LocalGenerators[i]
  1101. ->GetMakefile()->GetCurrentDirectory();
  1102. notFoundMap[varName] = text;
  1103. }
  1104. }
  1105. }
  1106. this->CMakeInstance->UpdateProgress
  1107. ("Configuring", 0.9f+0.1f*(static_cast<float>(i)+1.0f)/
  1108. static_cast<float>(this->LocalGenerators.size()));
  1109. }
  1110. if(notFoundMap.size())
  1111. {
  1112. std::string notFoundVars;
  1113. for(std::map<cmStdString, cmStdString>::const_iterator
  1114. ii = notFoundMap.begin();
  1115. ii != notFoundMap.end();
  1116. ++ii)
  1117. {
  1118. notFoundVars += ii->first;
  1119. notFoundVars += ii->second;
  1120. notFoundVars += "\n";
  1121. }
  1122. cmSystemTools::Error("The following variables are used in this project, "
  1123. "but they are set to NOTFOUND.\n"
  1124. "Please set them or make sure they are set and "
  1125. "tested correctly in the CMake files:\n",
  1126. notFoundVars.c_str());
  1127. }
  1128. }
  1129. int cmGlobalGenerator::TryCompile(const char *srcdir, const char *bindir,
  1130. const char *projectName,
  1131. const char *target, bool fast,
  1132. std::string *output, cmMakefile *mf)
  1133. {
  1134. // if this is not set, then this is a first time configure
  1135. // and there is a good chance that the try compile stuff will
  1136. // take the bulk of the time, so try and guess some progress
  1137. // by getting closer and closer to 100 without actually getting there.
  1138. if (!this->CMakeInstance->GetCacheManager()->GetCacheValue
  1139. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS"))
  1140. {
  1141. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  1142. // we are in the first time progress and we have no
  1143. // idea how long it will be. So, just move 1/10th of the way
  1144. // there each time, and don't go over 95%
  1145. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  1146. if(this->FirstTimeProgress > 0.95f)
  1147. {
  1148. this->FirstTimeProgress = 0.95f;
  1149. }
  1150. this->CMakeInstance->UpdateProgress("Configuring",
  1151. this->FirstTimeProgress);
  1152. }
  1153. std::string makeCommand = this->CMakeInstance->
  1154. GetCacheManager()->GetCacheValue("CMAKE_MAKE_PROGRAM");
  1155. if(makeCommand.size() == 0)
  1156. {
  1157. cmSystemTools::Error(
  1158. "Generator cannot find the appropriate make command.");
  1159. return 1;
  1160. }
  1161. std::string newTarget;
  1162. if (target && strlen(target))
  1163. {
  1164. newTarget += target;
  1165. #if 0
  1166. #if defined(_WIN32) || defined(__CYGWIN__)
  1167. std::string tmp = target;
  1168. // if the target does not already end in . something
  1169. // then assume .exe
  1170. if(tmp.size() < 4 || tmp[tmp.size()-4] != '.')
  1171. {
  1172. newTarget += ".exe";
  1173. }
  1174. #endif // WIN32
  1175. #endif
  1176. }
  1177. const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
  1178. return this->Build(srcdir,bindir,projectName,
  1179. newTarget.c_str(),
  1180. output,makeCommand.c_str(),config,false,fast,
  1181. this->TryCompileTimeout);
  1182. }
  1183. std::string cmGlobalGenerator
  1184. ::GenerateBuildCommand(const char* makeProgram, const char *projectName,
  1185. const char* additionalOptions, const char *targetName,
  1186. const char* config, bool ignoreErrors, bool)
  1187. {
  1188. // Project name and config are not used yet.
  1189. (void)projectName;
  1190. (void)config;
  1191. std::string makeCommand =
  1192. cmSystemTools::ConvertToUnixOutputPath(makeProgram);
  1193. // Since we have full control over the invocation of nmake, let us
  1194. // make it quiet.
  1195. if ( strcmp(this->GetName(), "NMake Makefiles") == 0 )
  1196. {
  1197. makeCommand += " /NOLOGO ";
  1198. }
  1199. if ( ignoreErrors )
  1200. {
  1201. makeCommand += " -i";
  1202. }
  1203. if ( additionalOptions )
  1204. {
  1205. makeCommand += " ";
  1206. makeCommand += additionalOptions;
  1207. }
  1208. if ( targetName )
  1209. {
  1210. makeCommand += " ";
  1211. makeCommand += targetName;
  1212. }
  1213. return makeCommand;
  1214. }
  1215. int cmGlobalGenerator::Build(
  1216. const char *, const char *bindir,
  1217. const char *projectName, const char *target,
  1218. std::string *output,
  1219. const char *makeCommandCSTR,
  1220. const char *config,
  1221. bool clean, bool fast,
  1222. double timeout,
  1223. cmSystemTools::OutputOption outputflag,
  1224. const char* extraOptions,
  1225. std::vector<std::string> const& nativeOptions)
  1226. {
  1227. /**
  1228. * Run an executable command and put the stdout in output.
  1229. */
  1230. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1231. cmSystemTools::ChangeDirectory(bindir);
  1232. if(output)
  1233. {
  1234. *output += "Change Dir: ";
  1235. *output += bindir;
  1236. *output += "\n";
  1237. }
  1238. int retVal;
  1239. bool hideconsole = cmSystemTools::GetRunCommandHideConsole();
  1240. cmSystemTools::SetRunCommandHideConsole(true);
  1241. std::string outputBuffer;
  1242. std::string* outputPtr = 0;
  1243. if(output)
  1244. {
  1245. outputPtr = &outputBuffer;
  1246. }
  1247. // should we do a clean first?
  1248. if (clean)
  1249. {
  1250. std::string cleanCommand =
  1251. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  1252. 0, "clean", config, false, fast);
  1253. if(output)
  1254. {
  1255. *output += "\nRun Clean Command:";
  1256. *output += cleanCommand;
  1257. *output += "\n";
  1258. }
  1259. if (!cmSystemTools::RunSingleCommand(cleanCommand.c_str(), outputPtr,
  1260. &retVal, 0, outputflag, timeout))
  1261. {
  1262. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1263. cmSystemTools::Error("Generator: execution of make clean failed.");
  1264. if (output)
  1265. {
  1266. *output += *outputPtr;
  1267. *output += "\nGenerator: execution of make clean failed.\n";
  1268. }
  1269. // return to the original directory
  1270. cmSystemTools::ChangeDirectory(cwd.c_str());
  1271. return 1;
  1272. }
  1273. if (output)
  1274. {
  1275. *output += *outputPtr;
  1276. }
  1277. }
  1278. // now build
  1279. std::string makeCommand =
  1280. this->GenerateBuildCommand(makeCommandCSTR, projectName,
  1281. extraOptions, target,
  1282. config, false, fast);
  1283. if(output)
  1284. {
  1285. *output += "\nRun Build Command:";
  1286. *output += makeCommand;
  1287. *output += "\n";
  1288. }
  1289. std::vector<cmStdString> command =
  1290. cmSystemTools::ParseArguments(makeCommand.c_str());
  1291. for(std::vector<std::string>::const_iterator ni = nativeOptions.begin();
  1292. ni != nativeOptions.end(); ++ni)
  1293. {
  1294. command.push_back(*ni);
  1295. }
  1296. if (!cmSystemTools::RunSingleCommand(command, outputPtr,
  1297. &retVal, 0, outputflag, timeout))
  1298. {
  1299. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1300. cmSystemTools::Error
  1301. ("Generator: execution of make failed. Make command was: ",
  1302. makeCommand.c_str());
  1303. if (output)
  1304. {
  1305. *output += *outputPtr;
  1306. *output += "\nGenerator: execution of make failed. Make command was: "
  1307. + makeCommand + "\n";
  1308. }
  1309. // return to the original directory
  1310. cmSystemTools::ChangeDirectory(cwd.c_str());
  1311. return 1;
  1312. }
  1313. if (output)
  1314. {
  1315. *output += *outputPtr;
  1316. }
  1317. cmSystemTools::SetRunCommandHideConsole(hideconsole);
  1318. // The SGI MipsPro 7.3 compiler does not return an error code when
  1319. // the source has a #error in it! This is a work-around for such
  1320. // compilers.
  1321. if((retVal == 0) && (output->find("#error") != std::string::npos))
  1322. {
  1323. retVal = 1;
  1324. }
  1325. cmSystemTools::ChangeDirectory(cwd.c_str());
  1326. return retVal;
  1327. }
  1328. void cmGlobalGenerator::AddLocalGenerator(cmLocalGenerator *lg)
  1329. {
  1330. this->LocalGenerators.push_back(lg);
  1331. // update progress
  1332. // estimate how many lg there will be
  1333. const char *numGenC =
  1334. this->CMakeInstance->GetCacheManager()->GetCacheValue
  1335. ("CMAKE_NUMBER_OF_LOCAL_GENERATORS");
  1336. if (!numGenC)
  1337. {
  1338. // If CMAKE_NUMBER_OF_LOCAL_GENERATORS is not set
  1339. // we are in the first time progress and we have no
  1340. // idea how long it will be. So, just move half way
  1341. // there each time, and don't go over 95%
  1342. this->FirstTimeProgress += ((1.0f - this->FirstTimeProgress) /30.0f);
  1343. if(this->FirstTimeProgress > 0.95f)
  1344. {
  1345. this->FirstTimeProgress = 0.95f;
  1346. }
  1347. this->CMakeInstance->UpdateProgress("Configuring",
  1348. this->FirstTimeProgress);
  1349. return;
  1350. }
  1351. int numGen = atoi(numGenC);
  1352. float prog = 0.9f*static_cast<float>(this->LocalGenerators.size())/
  1353. static_cast<float>(numGen);
  1354. if (prog > 0.9f)
  1355. {
  1356. prog = 0.9f;
  1357. }
  1358. this->CMakeInstance->UpdateProgress("Configuring", prog);
  1359. }
  1360. void cmGlobalGenerator::AddInstallComponent(const char* component)
  1361. {
  1362. if(component && *component)
  1363. {
  1364. this->InstallComponents.insert(component);
  1365. }
  1366. }
  1367. void cmGlobalGenerator::AddTargetToExports(const char* exportSetName,
  1368. cmTarget* target,
  1369. cmInstallTargetGenerator* archive,
  1370. cmInstallTargetGenerator* runTime,
  1371. cmInstallTargetGenerator* library,
  1372. cmInstallTargetGenerator* framework,
  1373. cmInstallTargetGenerator* bundle,
  1374. cmInstallFilesGenerator* headers)
  1375. {
  1376. if ((exportSetName) && (*exportSetName) && (target))
  1377. {
  1378. cmTargetExport* te = new cmTargetExport(target, archive, runTime, library,
  1379. framework, bundle, headers);
  1380. this->ExportSets[exportSetName].push_back(te);
  1381. }
  1382. }
  1383. //----------------------------------------------------------------------------
  1384. void cmGlobalGenerator::ClearExportSets()
  1385. {
  1386. for(std::map<cmStdString, std::vector<cmTargetExport*> >::iterator
  1387. setIt = this->ExportSets.begin();
  1388. setIt != this->ExportSets.end(); ++setIt)
  1389. {
  1390. for(unsigned int i = 0; i < setIt->second.size(); ++i)
  1391. {
  1392. delete setIt->second[i];
  1393. }
  1394. }
  1395. this->ExportSets.clear();
  1396. }
  1397. const std::vector<cmTargetExport*>* cmGlobalGenerator::GetExportSet(
  1398. const char* name) const
  1399. {
  1400. std::map<cmStdString, std::vector<cmTargetExport*> >::const_iterator
  1401. exportSetIt = this->ExportSets.find(name);
  1402. if (exportSetIt != this->ExportSets.end())
  1403. {
  1404. return &exportSetIt->second;
  1405. }
  1406. return 0;
  1407. }
  1408. void cmGlobalGenerator::EnableInstallTarget()
  1409. {
  1410. this->InstallTargetEnabled = true;
  1411. }
  1412. cmLocalGenerator *cmGlobalGenerator::CreateLocalGenerator()
  1413. {
  1414. cmLocalGenerator *lg = new cmLocalGenerator;
  1415. lg->SetGlobalGenerator(this);
  1416. return lg;
  1417. }
  1418. void cmGlobalGenerator::EnableLanguagesFromGenerator(cmGlobalGenerator *gen,
  1419. cmMakefile* mf)
  1420. {
  1421. this->SetConfiguredFilesPath(gen);
  1422. this->TryCompileOuterMakefile = mf;
  1423. const char* make =
  1424. gen->GetCMakeInstance()->GetCacheDefinition("CMAKE_MAKE_PROGRAM");
  1425. this->GetCMakeInstance()->AddCacheEntry("CMAKE_MAKE_PROGRAM", make,
  1426. "make program",
  1427. cmCacheManager::FILEPATH);
  1428. // copy the enabled languages
  1429. this->LanguageEnabled = gen->LanguageEnabled;
  1430. this->LanguagesReady = gen->LanguagesReady;
  1431. this->ExtensionToLanguage = gen->ExtensionToLanguage;
  1432. this->IgnoreExtensions = gen->IgnoreExtensions;
  1433. this->LanguageToOutputExtension = gen->LanguageToOutputExtension;
  1434. this->LanguageToLinkerPreference = gen->LanguageToLinkerPreference;
  1435. this->OutputExtensions = gen->OutputExtensions;
  1436. }
  1437. //----------------------------------------------------------------------------
  1438. void cmGlobalGenerator::SetConfiguredFilesPath(cmGlobalGenerator* gen)
  1439. {
  1440. if(!gen->ConfiguredFilesPath.empty())
  1441. {
  1442. this->ConfiguredFilesPath = gen->ConfiguredFilesPath;
  1443. }
  1444. else
  1445. {
  1446. this->ConfiguredFilesPath = gen->CMakeInstance->GetHomeOutputDirectory();
  1447. this->ConfiguredFilesPath += cmake::GetCMakeFilesDirectory();
  1448. }
  1449. }
  1450. //----------------------------------------------------------------------------
  1451. void cmGlobalGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  1452. {
  1453. entry.Name = this->GetName();
  1454. entry.Brief = "";
  1455. entry.Full = "";
  1456. }
  1457. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1458. cmLocalGenerator* gen)
  1459. {
  1460. if(!gen || gen == root)
  1461. {
  1462. // No directory excludes itself.
  1463. return false;
  1464. }
  1465. if(gen->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1466. {
  1467. // This directory is excluded from its parent.
  1468. return true;
  1469. }
  1470. // This directory is included in its parent. Check whether the
  1471. // parent is excluded.
  1472. return this->IsExcluded(root, gen->GetParent());
  1473. }
  1474. bool cmGlobalGenerator::IsExcluded(cmLocalGenerator* root,
  1475. cmTarget& target)
  1476. {
  1477. if(target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  1478. {
  1479. // This target is excluded from its directory.
  1480. return true;
  1481. }
  1482. else
  1483. {
  1484. // This target is included in its directory. Check whether the
  1485. // directory is excluded.
  1486. return this->IsExcluded(root, target.GetMakefile()->GetLocalGenerator());
  1487. }
  1488. }
  1489. void cmGlobalGenerator::GetEnabledLanguages(std::vector<std::string>& lang)
  1490. {
  1491. for(std::map<cmStdString, bool>::iterator i =
  1492. this->LanguageEnabled.begin(); i != this->LanguageEnabled.end(); ++i)
  1493. {
  1494. lang.push_back(i->first);
  1495. }
  1496. }
  1497. int cmGlobalGenerator::GetLinkerPreference(const char* lang)
  1498. {
  1499. std::map<cmStdString, int>::const_iterator it =
  1500. this->LanguageToLinkerPreference.find(lang);
  1501. if (it != this->LanguageToLinkerPreference.end())
  1502. {
  1503. return it->second;
  1504. }
  1505. return 0;
  1506. }
  1507. void cmGlobalGenerator::FillProjectMap()
  1508. {
  1509. this->ProjectMap.clear(); // make sure we start with a clean map
  1510. unsigned int i;
  1511. for(i = 0; i < this->LocalGenerators.size(); ++i)
  1512. {
  1513. // for each local generator add all projects
  1514. cmLocalGenerator *lg = this->LocalGenerators[i];
  1515. std::string name;
  1516. do
  1517. {
  1518. if (name != lg->GetMakefile()->GetProjectName())
  1519. {
  1520. name = lg->GetMakefile()->GetProjectName();
  1521. this->ProjectMap[name].push_back(this->LocalGenerators[i]);
  1522. }
  1523. lg = lg->GetParent();
  1524. }
  1525. while (lg);
  1526. }
  1527. }
  1528. // Build a map that contains a the set of targets used by each local
  1529. // generator directory level.
  1530. void cmGlobalGenerator::FillLocalGeneratorToTargetMap()
  1531. {
  1532. this->LocalGeneratorToTargetMap.clear();
  1533. // Loop over all targets in all local generators.
  1534. for(std::vector<cmLocalGenerator*>::const_iterator
  1535. lgi = this->LocalGenerators.begin();
  1536. lgi != this->LocalGenerators.end(); ++lgi)
  1537. {
  1538. cmLocalGenerator* lg = *lgi;
  1539. cmMakefile* mf = lg->GetMakefile();
  1540. cmTargets& targets = mf->GetTargets();
  1541. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  1542. {
  1543. cmTarget& target = t->second;
  1544. // Consider the directory containing the target and all its
  1545. // parents until something excludes the target.
  1546. for(cmLocalGenerator* clg = lg; clg && !this->IsExcluded(clg, target);
  1547. clg = clg->GetParent())
  1548. {
  1549. // This local generator includes the target.
  1550. std::set<cmTarget*>& targetSet =
  1551. this->LocalGeneratorToTargetMap[clg];
  1552. targetSet.insert(&target);
  1553. // Add dependencies of the included target. An excluded
  1554. // target may still be included if it is a dependency of a
  1555. // non-excluded target.
  1556. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  1557. for(TargetDependSet::const_iterator ti = tgtdeps.begin();
  1558. ti != tgtdeps.end(); ++ti)
  1559. {
  1560. targetSet.insert(*ti);
  1561. }
  1562. }
  1563. }
  1564. }
  1565. }
  1566. ///! Find a local generator by its startdirectory
  1567. cmLocalGenerator* cmGlobalGenerator::FindLocalGenerator(const char* start_dir)
  1568. {
  1569. std::vector<cmLocalGenerator*>* gens = &this->LocalGenerators;
  1570. for(unsigned int i = 0; i < gens->size(); ++i)
  1571. {
  1572. std::string sd = (*gens)[i]->GetMakefile()->GetStartDirectory();
  1573. if (sd == start_dir)
  1574. {
  1575. return (*gens)[i];
  1576. }
  1577. }
  1578. return 0;
  1579. }
  1580. //----------------------------------------------------------------------------
  1581. cmTarget*
  1582. cmGlobalGenerator::FindTarget(const char* project, const char* name)
  1583. {
  1584. // if project specific
  1585. if(project)
  1586. {
  1587. std::vector<cmLocalGenerator*>* gens = &this->ProjectMap[project];
  1588. for(unsigned int i = 0; i < gens->size(); ++i)
  1589. {
  1590. cmTarget* ret = (*gens)[i]->GetMakefile()->FindTarget(name);
  1591. if(ret)
  1592. {
  1593. return ret;
  1594. }
  1595. }
  1596. }
  1597. // if all projects/directories
  1598. else
  1599. {
  1600. std::map<cmStdString,cmTarget *>::iterator i =
  1601. this->TotalTargets.find ( name );
  1602. if ( i != this->TotalTargets.end() )
  1603. {
  1604. return i->second;
  1605. }
  1606. i = this->ImportedTargets.find(name);
  1607. if ( i != this->ImportedTargets.end() )
  1608. {
  1609. return i->second;
  1610. }
  1611. }
  1612. return 0;
  1613. }
  1614. //----------------------------------------------------------------------------
  1615. bool cmGlobalGenerator::NameResolvesToFramework(const std::string& libname)
  1616. {
  1617. if(cmSystemTools::IsPathToFramework(libname.c_str()))
  1618. {
  1619. return true;
  1620. }
  1621. if(cmTarget* tgt = this->FindTarget(0, libname.c_str()))
  1622. {
  1623. if(tgt->IsFrameworkOnApple())
  1624. {
  1625. return true;
  1626. }
  1627. }
  1628. return false;
  1629. }
  1630. //----------------------------------------------------------------------------
  1631. inline std::string removeQuotes(const std::string& s)
  1632. {
  1633. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1634. {
  1635. return s.substr(1, s.size()-2);
  1636. }
  1637. return s;
  1638. }
  1639. void cmGlobalGenerator::SetCMakeInstance(cmake* cm)
  1640. {
  1641. // Store a pointer to the cmake object instance.
  1642. this->CMakeInstance = cm;
  1643. }
  1644. void cmGlobalGenerator::CreateDefaultGlobalTargets(cmTargets* targets)
  1645. {
  1646. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  1647. const char* cmakeCfgIntDir = this->GetCMakeCFGIntDir();
  1648. const char* cmakeCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1649. // CPack
  1650. std::string workingDir = mf->GetStartOutputDirectory();
  1651. cmCustomCommandLines cpackCommandLines;
  1652. std::vector<std::string> depends;
  1653. cmCustomCommandLine singleLine;
  1654. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1655. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1656. {
  1657. singleLine.push_back("-C");
  1658. singleLine.push_back(cmakeCfgIntDir);
  1659. }
  1660. singleLine.push_back("--config");
  1661. std::string configFile = mf->GetStartOutputDirectory();;
  1662. configFile += "/CPackConfig.cmake";
  1663. std::string relConfigFile = "./CPackConfig.cmake";
  1664. singleLine.push_back(relConfigFile);
  1665. cpackCommandLines.push_back(singleLine);
  1666. if ( this->GetPreinstallTargetName() )
  1667. {
  1668. depends.push_back(this->GetPreinstallTargetName());
  1669. }
  1670. else
  1671. {
  1672. const char* noPackageAll =
  1673. mf->GetDefinition("CMAKE_SKIP_PACKAGE_ALL_DEPENDENCY");
  1674. if(!noPackageAll || cmSystemTools::IsOff(noPackageAll))
  1675. {
  1676. depends.push_back(this->GetAllTargetName());
  1677. }
  1678. }
  1679. if(cmSystemTools::FileExists(configFile.c_str()))
  1680. {
  1681. (*targets)[this->GetPackageTargetName()]
  1682. = this->CreateGlobalTarget(this->GetPackageTargetName(),
  1683. "Run CPack packaging tool...",
  1684. &cpackCommandLines, depends,
  1685. workingDir.c_str());
  1686. }
  1687. // CPack source
  1688. const char* packageSourceTargetName = this->GetPackageSourceTargetName();
  1689. if ( packageSourceTargetName )
  1690. {
  1691. cpackCommandLines.erase(cpackCommandLines.begin(),
  1692. cpackCommandLines.end());
  1693. singleLine.erase(singleLine.begin(), singleLine.end());
  1694. depends.erase(depends.begin(), depends.end());
  1695. singleLine.push_back(this->GetCMakeInstance()->GetCPackCommand());
  1696. singleLine.push_back("--config");
  1697. configFile = mf->GetStartOutputDirectory();;
  1698. configFile += "/CPackSourceConfig.cmake";
  1699. relConfigFile = "./CPackSourceConfig.cmake";
  1700. singleLine.push_back(relConfigFile);
  1701. if(cmSystemTools::FileExists(configFile.c_str()))
  1702. {
  1703. singleLine.push_back(configFile);
  1704. cpackCommandLines.push_back(singleLine);
  1705. (*targets)[packageSourceTargetName]
  1706. = this->CreateGlobalTarget(packageSourceTargetName,
  1707. "Run CPack packaging tool for source...",
  1708. &cpackCommandLines, depends,
  1709. workingDir.c_str()
  1710. );
  1711. }
  1712. }
  1713. // Test
  1714. if(mf->IsOn("CMAKE_TESTING_ENABLED"))
  1715. {
  1716. cpackCommandLines.erase(cpackCommandLines.begin(),
  1717. cpackCommandLines.end());
  1718. singleLine.erase(singleLine.begin(), singleLine.end());
  1719. depends.erase(depends.begin(), depends.end());
  1720. singleLine.push_back(this->GetCMakeInstance()->GetCTestCommand());
  1721. singleLine.push_back("--force-new-ctest-process");
  1722. if(cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.')
  1723. {
  1724. singleLine.push_back("-C");
  1725. singleLine.push_back(cmakeCfgIntDir);
  1726. }
  1727. else // TODO: This is a hack. Should be something to do with the generator
  1728. {
  1729. singleLine.push_back("$(ARGS)");
  1730. }
  1731. cpackCommandLines.push_back(singleLine);
  1732. (*targets)[this->GetTestTargetName()]
  1733. = this->CreateGlobalTarget(this->GetTestTargetName(),
  1734. "Running tests...", &cpackCommandLines, depends, 0);
  1735. }
  1736. //Edit Cache
  1737. const char* editCacheTargetName = this->GetEditCacheTargetName();
  1738. if ( editCacheTargetName )
  1739. {
  1740. cpackCommandLines.erase(cpackCommandLines.begin(),
  1741. cpackCommandLines.end());
  1742. singleLine.erase(singleLine.begin(), singleLine.end());
  1743. depends.erase(depends.begin(), depends.end());
  1744. // Use CMAKE_EDIT_COMMAND for the edit_cache rule if it is defined.
  1745. // Otherwise default to the interactive command-line interface.
  1746. if(mf->GetDefinition("CMAKE_EDIT_COMMAND"))
  1747. {
  1748. singleLine.push_back(mf->GetDefinition("CMAKE_EDIT_COMMAND"));
  1749. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1750. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1751. cpackCommandLines.push_back(singleLine);
  1752. (*targets)[editCacheTargetName] =
  1753. this->CreateGlobalTarget(
  1754. editCacheTargetName, "Running CMake cache editor...",
  1755. &cpackCommandLines, depends, 0);
  1756. }
  1757. else
  1758. {
  1759. singleLine.push_back(cmakeCommand);
  1760. singleLine.push_back("-i");
  1761. singleLine.push_back(".");
  1762. cpackCommandLines.push_back(singleLine);
  1763. (*targets)[editCacheTargetName] =
  1764. this->CreateGlobalTarget(
  1765. editCacheTargetName,
  1766. "Running interactive CMake command-line interface...",
  1767. &cpackCommandLines, depends, 0);
  1768. }
  1769. }
  1770. //Rebuild Cache
  1771. const char* rebuildCacheTargetName = this->GetRebuildCacheTargetName();
  1772. if ( rebuildCacheTargetName )
  1773. {
  1774. cpackCommandLines.erase(cpackCommandLines.begin(),
  1775. cpackCommandLines.end());
  1776. singleLine.erase(singleLine.begin(), singleLine.end());
  1777. depends.erase(depends.begin(), depends.end());
  1778. singleLine.push_back(cmakeCommand);
  1779. singleLine.push_back("-H$(CMAKE_SOURCE_DIR)");
  1780. singleLine.push_back("-B$(CMAKE_BINARY_DIR)");
  1781. cpackCommandLines.push_back(singleLine);
  1782. (*targets)[rebuildCacheTargetName] =
  1783. this->CreateGlobalTarget(
  1784. rebuildCacheTargetName, "Running CMake to regenerate build system...",
  1785. &cpackCommandLines, depends, 0);
  1786. }
  1787. //Install
  1788. if(this->InstallTargetEnabled)
  1789. {
  1790. if(!cmakeCfgIntDir || !*cmakeCfgIntDir || cmakeCfgIntDir[0] == '.')
  1791. {
  1792. std::set<cmStdString>* componentsSet = &this->InstallComponents;
  1793. cpackCommandLines.erase(cpackCommandLines.begin(),
  1794. cpackCommandLines.end());
  1795. depends.erase(depends.begin(), depends.end());
  1796. cmOStringStream ostr;
  1797. if ( componentsSet->size() > 0 )
  1798. {
  1799. ostr << "Available install components are:";
  1800. std::set<cmStdString>::iterator it;
  1801. for (
  1802. it = componentsSet->begin();
  1803. it != componentsSet->end();
  1804. ++ it )
  1805. {
  1806. ostr << " \"" << it->c_str() << "\"";
  1807. }
  1808. }
  1809. else
  1810. {
  1811. ostr << "Only default component available";
  1812. }
  1813. singleLine.push_back(ostr.str().c_str());
  1814. (*targets)["list_install_components"]
  1815. = this->CreateGlobalTarget("list_install_components",
  1816. ostr.str().c_str(),
  1817. &cpackCommandLines, depends, 0);
  1818. }
  1819. std::string cmd = cmakeCommand;
  1820. cpackCommandLines.erase(cpackCommandLines.begin(),
  1821. cpackCommandLines.end());
  1822. singleLine.erase(singleLine.begin(), singleLine.end());
  1823. depends.erase(depends.begin(), depends.end());
  1824. if ( this->GetPreinstallTargetName() )
  1825. {
  1826. depends.push_back(this->GetPreinstallTargetName());
  1827. }
  1828. else
  1829. {
  1830. const char* noall =
  1831. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  1832. if(!noall || cmSystemTools::IsOff(noall))
  1833. {
  1834. depends.push_back(this->GetAllTargetName());
  1835. }
  1836. }
  1837. if(mf->GetDefinition("CMake_BINARY_DIR"))
  1838. {
  1839. // We are building CMake itself. We cannot use the original
  1840. // executable to install over itself. The generator will
  1841. // automatically convert this name to the build-time location.
  1842. cmd = "cmake";
  1843. }
  1844. singleLine.push_back(cmd.c_str());
  1845. if ( cmakeCfgIntDir && *cmakeCfgIntDir && cmakeCfgIntDir[0] != '.' )
  1846. {
  1847. std::string cfgArg = "-DBUILD_TYPE=";
  1848. cfgArg += mf->GetDefinition("CMAKE_CFG_INTDIR");
  1849. singleLine.push_back(cfgArg);
  1850. }
  1851. singleLine.push_back("-P");
  1852. singleLine.push_back("cmake_install.cmake");
  1853. cpackCommandLines.push_back(singleLine);
  1854. (*targets)[this->GetInstallTargetName()] =
  1855. this->CreateGlobalTarget(
  1856. this->GetInstallTargetName(), "Install the project...",
  1857. &cpackCommandLines, depends, 0);
  1858. // install_local
  1859. if(const char* install_local = this->GetInstallLocalTargetName())
  1860. {
  1861. cmCustomCommandLine localCmdLine = singleLine;
  1862. localCmdLine.insert(localCmdLine.begin()+1,
  1863. "-DCMAKE_INSTALL_LOCAL_ONLY=1");
  1864. cpackCommandLines.erase(cpackCommandLines.begin(),
  1865. cpackCommandLines.end());
  1866. cpackCommandLines.push_back(localCmdLine);
  1867. (*targets)[install_local] =
  1868. this->CreateGlobalTarget(
  1869. install_local, "Installing only the local directory...",
  1870. &cpackCommandLines, depends, 0);
  1871. }
  1872. // install_strip
  1873. const char* install_strip = this->GetInstallStripTargetName();
  1874. if((install_strip !=0) && (mf->IsSet("CMAKE_STRIP")))
  1875. {
  1876. cmCustomCommandLine stripCmdLine = singleLine;
  1877. stripCmdLine.insert(stripCmdLine.begin()+1,"-DCMAKE_INSTALL_DO_STRIP=1");
  1878. cpackCommandLines.erase(cpackCommandLines.begin(),
  1879. cpackCommandLines.end());
  1880. cpackCommandLines.push_back(stripCmdLine);
  1881. (*targets)[install_strip] =
  1882. this->CreateGlobalTarget(
  1883. install_strip, "Installing the project stripped...",
  1884. &cpackCommandLines, depends, 0);
  1885. }
  1886. }
  1887. }
  1888. //----------------------------------------------------------------------------
  1889. const char* cmGlobalGenerator::GetPredefinedTargetsFolder()
  1890. {
  1891. const char* prop =
  1892. this->GetCMakeInstance()->GetProperty("PREDEFINED_TARGETS_FOLDER");
  1893. if (prop)
  1894. {
  1895. return prop;
  1896. }
  1897. return "CMakePredefinedTargets";
  1898. }
  1899. //----------------------------------------------------------------------------
  1900. bool cmGlobalGenerator::UseFolderProperty()
  1901. {
  1902. const char* prop = this->GetCMakeInstance()->GetProperty("USE_FOLDERS");
  1903. // If this property is defined, let the setter turn this on or off...
  1904. //
  1905. if (prop)
  1906. {
  1907. return cmSystemTools::IsOn(prop);
  1908. }
  1909. // By default, this feature is OFF, since it is not supported in the
  1910. // Visual Studio Express editions:
  1911. //
  1912. return false;
  1913. }
  1914. //----------------------------------------------------------------------------
  1915. cmTarget cmGlobalGenerator::CreateGlobalTarget(
  1916. const char* name, const char* message,
  1917. const cmCustomCommandLines* commandLines,
  1918. std::vector<std::string> depends,
  1919. const char* workingDirectory)
  1920. {
  1921. // Package
  1922. cmTarget target;
  1923. target.GetProperties().SetCMakeInstance(this->CMakeInstance);
  1924. target.SetType(cmTarget::GLOBAL_TARGET, name);
  1925. target.SetProperty("EXCLUDE_FROM_ALL","TRUE");
  1926. std::vector<std::string> no_outputs;
  1927. std::vector<std::string> no_depends;
  1928. // Store the custom command in the target.
  1929. cmCustomCommand cc(0, no_outputs, no_depends, *commandLines, 0,
  1930. workingDirectory);
  1931. target.GetPostBuildCommands().push_back(cc);
  1932. target.SetProperty("EchoString", message);
  1933. std::vector<std::string>::iterator dit;
  1934. for ( dit = depends.begin(); dit != depends.end(); ++ dit )
  1935. {
  1936. target.AddUtility(dit->c_str());
  1937. }
  1938. // Organize in the "predefined targets" folder:
  1939. //
  1940. if (this->UseFolderProperty())
  1941. {
  1942. target.SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  1943. }
  1944. return target;
  1945. }
  1946. //----------------------------------------------------------------------------
  1947. std::string
  1948. cmGlobalGenerator::GenerateRuleFile(std::string const& output) const
  1949. {
  1950. std::string ruleFile = output;
  1951. ruleFile += ".rule";
  1952. const char* dir = this->GetCMakeCFGIntDir();
  1953. if(dir && dir[0] == '$')
  1954. {
  1955. cmSystemTools::ReplaceString(ruleFile, dir,
  1956. cmake::GetCMakeFilesDirectory());
  1957. }
  1958. return ruleFile;
  1959. }
  1960. //----------------------------------------------------------------------------
  1961. std::string cmGlobalGenerator::GetSharedLibFlagsForLanguage(
  1962. std::string const& l)
  1963. {
  1964. if(this->LanguageToOriginalSharedLibFlags.count(l) > 0)
  1965. {
  1966. return this->LanguageToOriginalSharedLibFlags[l];
  1967. }
  1968. return "";
  1969. }
  1970. //----------------------------------------------------------------------------
  1971. void cmGlobalGenerator::AppendDirectoryForConfig(const char*, const char*,
  1972. const char*, std::string&)
  1973. {
  1974. // Subclasses that support multiple configurations should implement
  1975. // this method to append the subdirectory for the given build
  1976. // configuration.
  1977. }
  1978. //----------------------------------------------------------------------------
  1979. cmGlobalGenerator::TargetDependSet const&
  1980. cmGlobalGenerator::GetTargetDirectDepends(cmTarget & target)
  1981. {
  1982. return this->TargetDependencies[&target];
  1983. }
  1984. void cmGlobalGenerator::AddTarget(cmTarget* t)
  1985. {
  1986. if(t->IsImported())
  1987. {
  1988. this->ImportedTargets[t->GetName()] = t;
  1989. }
  1990. else
  1991. {
  1992. this->TotalTargets[t->GetName()] = t;
  1993. }
  1994. }
  1995. void cmGlobalGenerator::SetExternalMakefileProjectGenerator(
  1996. cmExternalMakefileProjectGenerator *extraGenerator)
  1997. {
  1998. this->ExtraGenerator = extraGenerator;
  1999. if (this->ExtraGenerator!=0)
  2000. {
  2001. this->ExtraGenerator->SetGlobalGenerator(this);
  2002. }
  2003. }
  2004. const char* cmGlobalGenerator::GetExtraGeneratorName() const
  2005. {
  2006. return this->ExtraGenerator==0 ? 0 : this->ExtraGenerator->GetName();
  2007. }
  2008. void cmGlobalGenerator::FileReplacedDuringGenerate(const std::string& filename)
  2009. {
  2010. this->FilesReplacedDuringGenerate.push_back(filename);
  2011. }
  2012. void
  2013. cmGlobalGenerator
  2014. ::GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames)
  2015. {
  2016. filenames.clear();
  2017. std::copy(
  2018. this->FilesReplacedDuringGenerate.begin(),
  2019. this->FilesReplacedDuringGenerate.end(),
  2020. std::back_inserter(filenames));
  2021. }
  2022. //----------------------------------------------------------------------------
  2023. void cmGlobalGenerator::GetTargetSets(TargetDependSet& projectTargets,
  2024. TargetDependSet& originalTargets,
  2025. cmLocalGenerator* root,
  2026. GeneratorVector const& generators)
  2027. {
  2028. // loop over all local generators
  2029. for(std::vector<cmLocalGenerator*>::const_iterator i = generators.begin();
  2030. i != generators.end(); ++i)
  2031. {
  2032. // check to make sure generator is not excluded
  2033. if(this->IsExcluded(root, *i))
  2034. {
  2035. continue;
  2036. }
  2037. cmMakefile* mf = (*i)->GetMakefile();
  2038. // Get the targets in the makefile
  2039. cmTargets &tgts = mf->GetTargets();
  2040. // loop over all the targets
  2041. for (cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2042. {
  2043. cmTarget* target = &l->second;
  2044. if(this->IsRootOnlyTarget(target) &&
  2045. target->GetMakefile() != root->GetMakefile())
  2046. {
  2047. continue;
  2048. }
  2049. // put the target in the set of original targets
  2050. originalTargets.insert(target);
  2051. // Get the set of targets that depend on target
  2052. this->AddTargetDepends(target, projectTargets);
  2053. }
  2054. }
  2055. }
  2056. //----------------------------------------------------------------------------
  2057. bool cmGlobalGenerator::IsRootOnlyTarget(cmTarget* target)
  2058. {
  2059. return (target->GetType() == cmTarget::GLOBAL_TARGET ||
  2060. strcmp(target->GetName(), this->GetAllTargetName()) == 0);
  2061. }
  2062. //----------------------------------------------------------------------------
  2063. void cmGlobalGenerator::AddTargetDepends(cmTarget* target,
  2064. TargetDependSet& projectTargets)
  2065. {
  2066. // add the target itself
  2067. if(projectTargets.insert(target).second)
  2068. {
  2069. // This is the first time we have encountered the target.
  2070. // Recursively follow its dependencies.
  2071. TargetDependSet const& ts = this->GetTargetDirectDepends(*target);
  2072. for(TargetDependSet::const_iterator i = ts.begin(); i != ts.end(); ++i)
  2073. {
  2074. cmTarget* dtarget = *i;
  2075. this->AddTargetDepends(dtarget, projectTargets);
  2076. }
  2077. }
  2078. }
  2079. //----------------------------------------------------------------------------
  2080. void cmGlobalGenerator::AddToManifest(const char* config,
  2081. std::string const& f)
  2082. {
  2083. // Add to the main manifest for this configuration.
  2084. this->TargetManifest[config].insert(f);
  2085. // Add to the content listing for the file's directory.
  2086. std::string dir = cmSystemTools::GetFilenamePath(f);
  2087. std::string file = cmSystemTools::GetFilenameName(f);
  2088. this->DirectoryContentMap[dir].insert(file);
  2089. }
  2090. //----------------------------------------------------------------------------
  2091. std::set<cmStdString> const&
  2092. cmGlobalGenerator::GetDirectoryContent(std::string const& dir, bool needDisk)
  2093. {
  2094. DirectoryContent& dc = this->DirectoryContentMap[dir];
  2095. if(needDisk && !dc.LoadedFromDisk)
  2096. {
  2097. // Load the directory content from disk.
  2098. cmsys::Directory d;
  2099. if(d.Load(dir.c_str()))
  2100. {
  2101. unsigned long n = d.GetNumberOfFiles();
  2102. for(unsigned long i = 0; i < n; ++i)
  2103. {
  2104. const char* f = d.GetFile(i);
  2105. if(strcmp(f, ".") != 0 && strcmp(f, "..") != 0)
  2106. {
  2107. dc.insert(f);
  2108. }
  2109. }
  2110. }
  2111. dc.LoadedFromDisk = true;
  2112. }
  2113. return dc;
  2114. }
  2115. //----------------------------------------------------------------------------
  2116. void
  2117. cmGlobalGenerator::AddRuleHash(const std::vector<std::string>& outputs,
  2118. std::string const& content)
  2119. {
  2120. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2121. // Ignore if there are no outputs.
  2122. if(outputs.empty())
  2123. {
  2124. return;
  2125. }
  2126. // Compute a hash of the rule.
  2127. RuleHash hash;
  2128. {
  2129. unsigned char const* data =
  2130. reinterpret_cast<unsigned char const*>(content.c_str());
  2131. int length = static_cast<int>(content.length());
  2132. cmsysMD5* sum = cmsysMD5_New();
  2133. cmsysMD5_Initialize(sum);
  2134. cmsysMD5_Append(sum, data, length);
  2135. cmsysMD5_FinalizeHex(sum, hash.Data);
  2136. cmsysMD5_Delete(sum);
  2137. }
  2138. // Shorten the output name (in expected use case).
  2139. cmLocalGenerator* lg = this->GetLocalGenerators()[0];
  2140. std::string fname = lg->Convert(outputs[0].c_str(),
  2141. cmLocalGenerator::HOME_OUTPUT);
  2142. // Associate the hash with this output.
  2143. this->RuleHashes[fname] = hash;
  2144. #else
  2145. (void)outputs;
  2146. (void)content;
  2147. #endif
  2148. }
  2149. //----------------------------------------------------------------------------
  2150. void cmGlobalGenerator::CheckRuleHashes()
  2151. {
  2152. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2153. std::string home = this->GetCMakeInstance()->GetHomeOutputDirectory();
  2154. std::string pfile = home;
  2155. pfile += this->GetCMakeInstance()->GetCMakeFilesDirectory();
  2156. pfile += "/CMakeRuleHashes.txt";
  2157. this->CheckRuleHashes(pfile, home);
  2158. this->WriteRuleHashes(pfile);
  2159. #endif
  2160. }
  2161. //----------------------------------------------------------------------------
  2162. void cmGlobalGenerator::CheckRuleHashes(std::string const& pfile,
  2163. std::string const& home)
  2164. {
  2165. #if defined(_WIN32) || defined(__CYGWIN__)
  2166. std::ifstream fin(pfile.c_str(), std::ios::in | std::ios::binary);
  2167. #else
  2168. std::ifstream fin(pfile.c_str(), std::ios::in);
  2169. #endif
  2170. if(!fin)
  2171. {
  2172. return;
  2173. }
  2174. std::string line;
  2175. std::string fname;
  2176. while(cmSystemTools::GetLineFromStream(fin, line))
  2177. {
  2178. // Line format is a 32-byte hex string followed by a space
  2179. // followed by a file name (with no escaping).
  2180. // Skip blank and comment lines.
  2181. if(line.size() < 34 || line[0] == '#')
  2182. {
  2183. continue;
  2184. }
  2185. // Get the filename.
  2186. fname = line.substr(33, line.npos);
  2187. // Look for a hash for this file's rule.
  2188. std::map<cmStdString, RuleHash>::const_iterator rhi =
  2189. this->RuleHashes.find(fname);
  2190. if(rhi != this->RuleHashes.end())
  2191. {
  2192. // Compare the rule hash in the file to that we were given.
  2193. if(strncmp(line.c_str(), rhi->second.Data, 32) != 0)
  2194. {
  2195. // The rule has changed. Delete the output so it will be
  2196. // built again.
  2197. fname = cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  2198. cmSystemTools::RemoveFile(fname.c_str());
  2199. }
  2200. }
  2201. else
  2202. {
  2203. // We have no hash for a rule previously listed. This may be a
  2204. // case where a user has turned off a build option and might
  2205. // want to turn it back on later, so do not delete the file.
  2206. // Instead, we keep the rule hash as long as the file exists so
  2207. // that if the feature is turned back on and the rule has
  2208. // changed the file is still rebuilt.
  2209. std::string fpath =
  2210. cmSystemTools::CollapseFullPath(fname.c_str(), home.c_str());
  2211. if(cmSystemTools::FileExists(fpath.c_str()))
  2212. {
  2213. RuleHash hash;
  2214. strncpy(hash.Data, line.c_str(), 32);
  2215. this->RuleHashes[fname] = hash;
  2216. }
  2217. }
  2218. }
  2219. }
  2220. //----------------------------------------------------------------------------
  2221. void cmGlobalGenerator::WriteRuleHashes(std::string const& pfile)
  2222. {
  2223. // Now generate a new persistence file with the current hashes.
  2224. if(this->RuleHashes.empty())
  2225. {
  2226. cmSystemTools::RemoveFile(pfile.c_str());
  2227. }
  2228. else
  2229. {
  2230. cmGeneratedFileStream fout(pfile.c_str());
  2231. fout << "# Hashes of file build rules.\n";
  2232. for(std::map<cmStdString, RuleHash>::const_iterator
  2233. rhi = this->RuleHashes.begin(); rhi != this->RuleHashes.end(); ++rhi)
  2234. {
  2235. fout.write(rhi->second.Data, 32);
  2236. fout << " " << rhi->first << "\n";
  2237. }
  2238. }
  2239. }
  2240. //----------------------------------------------------------------------------
  2241. void cmGlobalGenerator::WriteSummary()
  2242. {
  2243. cmMakefile* mf = this->LocalGenerators[0]->GetMakefile();
  2244. // Record all target directories in a central location.
  2245. std::string fname = mf->GetHomeOutputDirectory();
  2246. fname += cmake::GetCMakeFilesDirectory();
  2247. fname += "/TargetDirectories.txt";
  2248. cmGeneratedFileStream fout(fname.c_str());
  2249. // Generate summary information files for each target.
  2250. std::string dir;
  2251. for(std::map<cmStdString,cmTarget *>::const_iterator ti =
  2252. this->TotalTargets.begin(); ti != this->TotalTargets.end(); ++ti)
  2253. {
  2254. this->WriteSummary(ti->second);
  2255. fout << ti->second->GetSupportDirectory() << "\n";
  2256. }
  2257. }
  2258. //----------------------------------------------------------------------------
  2259. void cmGlobalGenerator::WriteSummary(cmTarget* target)
  2260. {
  2261. // Place the labels file in a per-target support directory.
  2262. std::string dir = target->GetSupportDirectory();
  2263. std::string file = dir;
  2264. file += "/Labels.txt";
  2265. // Check whether labels are enabled for this target.
  2266. if(const char* value = target->GetProperty("LABELS"))
  2267. {
  2268. cmSystemTools::MakeDirectory(dir.c_str());
  2269. cmGeneratedFileStream fout(file.c_str());
  2270. // List the target-wide labels. All sources in the target get
  2271. // these labels.
  2272. std::vector<std::string> labels;
  2273. cmSystemTools::ExpandListArgument(value, labels);
  2274. if(!labels.empty())
  2275. {
  2276. fout << "# Target labels\n";
  2277. for(std::vector<std::string>::const_iterator li = labels.begin();
  2278. li != labels.end(); ++li)
  2279. {
  2280. fout << " " << *li << "\n";
  2281. }
  2282. }
  2283. // List the source files with any per-source labels.
  2284. fout << "# Source files and their labels\n";
  2285. std::vector<cmSourceFile*> const& sources = target->GetSourceFiles();
  2286. for(std::vector<cmSourceFile*>::const_iterator si = sources.begin();
  2287. si != sources.end(); ++si)
  2288. {
  2289. cmSourceFile* sf = *si;
  2290. fout << sf->GetFullPath() << "\n";
  2291. if(const char* svalue = sf->GetProperty("LABELS"))
  2292. {
  2293. labels.clear();
  2294. cmSystemTools::ExpandListArgument(svalue, labels);
  2295. for(std::vector<std::string>::const_iterator li = labels.begin();
  2296. li != labels.end(); ++li)
  2297. {
  2298. fout << " " << *li << "\n";
  2299. }
  2300. }
  2301. }
  2302. }
  2303. else
  2304. {
  2305. cmSystemTools::RemoveFile(file.c_str());
  2306. }
  2307. }
  2308. //----------------------------------------------------------------------------
  2309. // static
  2310. std::string cmGlobalGenerator::EscapeJSON(const std::string& s) {
  2311. std::string result;
  2312. for (std::string::size_type i = 0; i < s.size(); ++i) {
  2313. if (s[i] == '"' || s[i] == '\\') {
  2314. result += '\\';
  2315. }
  2316. result += s[i];
  2317. }
  2318. return result;
  2319. }