cmGlobalGenerator.cxx 79 KB

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