cmGlobalGenerator.cxx 101 KB

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