cmGlobalGenerator.cxx 90 KB

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