cmMakefile.cxx 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658
  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. #include "cmMakefile.h"
  11. #include "cmVersion.h"
  12. #include "cmCommand.h"
  13. #include "cmSourceFile.h"
  14. #include "cmSourceFileLocation.h"
  15. #include "cmSystemTools.h"
  16. #include "cmGlobalGenerator.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmCommands.h"
  19. #include "cmCacheManager.h"
  20. #include "cmFunctionBlocker.h"
  21. #include "cmListFileCache.h"
  22. #include "cmDocumentGeneratorExpressions.h"
  23. #include "cmCommandArgumentParserHelper.h"
  24. #include "cmDocumentCompileDefinitions.h"
  25. #include "cmGeneratorExpression.h"
  26. #include "cmTest.h"
  27. #ifdef CMAKE_BUILD_WITH_CMAKE
  28. # include "cmVariableWatch.h"
  29. #endif
  30. #include "cmInstallGenerator.h"
  31. #include "cmTestGenerator.h"
  32. #include "cmDefinitions.h"
  33. #include "cmake.h"
  34. #include <stdlib.h> // required for atoi
  35. #include <cmsys/RegularExpression.hxx>
  36. #include <cmsys/auto_ptr.hxx>
  37. #include <stack>
  38. #include <ctype.h> // for isspace
  39. #include <assert.h>
  40. class cmMakefile::Internals
  41. {
  42. public:
  43. std::stack<cmDefinitions, std::list<cmDefinitions> > VarStack;
  44. std::stack<std::set<cmStdString> > VarInitStack;
  45. std::stack<std::set<cmStdString> > VarUsageStack;
  46. bool IsSourceFileTryCompile;
  47. };
  48. // default is not to be building executables
  49. cmMakefile::cmMakefile(): Internal(new Internals)
  50. {
  51. const cmDefinitions& defs = cmDefinitions();
  52. const std::set<cmStdString> globalKeys = defs.LocalKeys();
  53. this->Internal->VarStack.push(defs);
  54. this->Internal->VarInitStack.push(globalKeys);
  55. this->Internal->VarUsageStack.push(globalKeys);
  56. this->Internal->IsSourceFileTryCompile = false;
  57. // Initialize these first since AddDefaultDefinitions calls AddDefinition
  58. this->WarnUnused = false;
  59. this->CheckSystemVars = false;
  60. // Setup the default include file regular expression (match everything).
  61. this->IncludeFileRegularExpression = "^.*$";
  62. // Setup the default include complaint regular expression (match nothing).
  63. this->ComplainFileRegularExpression = "^$";
  64. // Source and header file extensions that we can handle
  65. // Set up a list of source and header extensions
  66. // these are used to find files when the extension
  67. // is not given
  68. // The "c" extension MUST precede the "C" extension.
  69. this->SourceFileExtensions.push_back( "c" );
  70. this->SourceFileExtensions.push_back( "C" );
  71. this->SourceFileExtensions.push_back( "c++" );
  72. this->SourceFileExtensions.push_back( "cc" );
  73. this->SourceFileExtensions.push_back( "cpp" );
  74. this->SourceFileExtensions.push_back( "cxx" );
  75. this->SourceFileExtensions.push_back( "m" );
  76. this->SourceFileExtensions.push_back( "M" );
  77. this->SourceFileExtensions.push_back( "mm" );
  78. this->HeaderFileExtensions.push_back( "h" );
  79. this->HeaderFileExtensions.push_back( "hh" );
  80. this->HeaderFileExtensions.push_back( "h++" );
  81. this->HeaderFileExtensions.push_back( "hm" );
  82. this->HeaderFileExtensions.push_back( "hpp" );
  83. this->HeaderFileExtensions.push_back( "hxx" );
  84. this->HeaderFileExtensions.push_back( "in" );
  85. this->HeaderFileExtensions.push_back( "txx" );
  86. this->DefineFlags = " ";
  87. this->LocalGenerator = 0;
  88. this->AddDefaultDefinitions();
  89. this->Initialize();
  90. this->PreOrder = false;
  91. this->GeneratingBuildSystem = false;
  92. }
  93. cmMakefile::cmMakefile(const cmMakefile& mf): Internal(new Internals)
  94. {
  95. this->Internal->VarStack.push(mf.Internal->VarStack.top().Closure());
  96. this->Internal->VarInitStack.push(mf.Internal->VarInitStack.top());
  97. this->Internal->VarUsageStack.push(mf.Internal->VarUsageStack.top());
  98. this->Prefix = mf.Prefix;
  99. this->AuxSourceDirectories = mf.AuxSourceDirectories;
  100. this->cmStartDirectory = mf.cmStartDirectory;
  101. this->StartOutputDirectory = mf.StartOutputDirectory;
  102. this->cmHomeDirectory = mf.cmHomeDirectory;
  103. this->HomeOutputDirectory = mf.HomeOutputDirectory;
  104. this->cmCurrentListFile = mf.cmCurrentListFile;
  105. this->ProjectName = mf.ProjectName;
  106. this->Targets = mf.Targets;
  107. this->SourceFiles = mf.SourceFiles;
  108. this->Tests = mf.Tests;
  109. this->LinkDirectories = mf.LinkDirectories;
  110. this->SystemIncludeDirectories = mf.SystemIncludeDirectories;
  111. this->ListFiles = mf.ListFiles;
  112. this->OutputFiles = mf.OutputFiles;
  113. this->LinkLibraries = mf.LinkLibraries;
  114. this->InstallGenerators = mf.InstallGenerators;
  115. this->TestGenerators = mf.TestGenerators;
  116. this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
  117. this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
  118. this->SourceFileExtensions = mf.SourceFileExtensions;
  119. this->HeaderFileExtensions = mf.HeaderFileExtensions;
  120. this->DefineFlags = mf.DefineFlags;
  121. this->DefineFlagsOrig = mf.DefineFlagsOrig;
  122. #if defined(CMAKE_BUILD_WITH_CMAKE)
  123. this->SourceGroups = mf.SourceGroups;
  124. #endif
  125. this->LocalGenerator = mf.LocalGenerator;
  126. this->FunctionBlockers = mf.FunctionBlockers;
  127. this->MacrosMap = mf.MacrosMap;
  128. this->SubDirectoryOrder = mf.SubDirectoryOrder;
  129. this->Properties = mf.Properties;
  130. this->PreOrder = mf.PreOrder;
  131. this->WarnUnused = mf.WarnUnused;
  132. this->Initialize();
  133. this->CheckSystemVars = mf.CheckSystemVars;
  134. this->ListFileStack = mf.ListFileStack;
  135. this->OutputToSource = mf.OutputToSource;
  136. }
  137. //----------------------------------------------------------------------------
  138. void cmMakefile::Initialize()
  139. {
  140. this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  141. this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  142. this->cmAtVarRegex.compile("(@[A-Za-z_0-9/.+-]+@)");
  143. // Enter a policy level for this directory.
  144. this->PushPolicy();
  145. // Protect the directory-level policies.
  146. this->PushPolicyBarrier();
  147. // By default the check is not done. It is enabled by
  148. // cmListFileCache in the top level if necessary.
  149. this->CheckCMP0000 = false;
  150. }
  151. unsigned int cmMakefile::GetCacheMajorVersion()
  152. {
  153. return this->GetCacheManager()->GetCacheMajorVersion();
  154. }
  155. unsigned int cmMakefile::GetCacheMinorVersion()
  156. {
  157. return this->GetCacheManager()->GetCacheMinorVersion();
  158. }
  159. bool cmMakefile::NeedCacheCompatibility(int major, int minor)
  160. {
  161. return this->GetCacheManager()->NeedCacheCompatibility(major, minor);
  162. }
  163. cmMakefile::~cmMakefile()
  164. {
  165. for(std::vector<cmInstallGenerator*>::iterator
  166. i = this->InstallGenerators.begin();
  167. i != this->InstallGenerators.end(); ++i)
  168. {
  169. delete *i;
  170. }
  171. for(std::vector<cmTestGenerator*>::iterator
  172. i = this->TestGenerators.begin();
  173. i != this->TestGenerators.end(); ++i)
  174. {
  175. delete *i;
  176. }
  177. for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
  178. i != this->SourceFiles.end(); ++i)
  179. {
  180. delete *i;
  181. }
  182. for(std::map<cmStdString, cmTest*>::iterator i = this->Tests.begin();
  183. i != this->Tests.end(); ++i)
  184. {
  185. delete i->second;
  186. }
  187. for(std::vector<cmTarget*>::iterator
  188. i = this->ImportedTargetsOwned.begin();
  189. i != this->ImportedTargetsOwned.end(); ++i)
  190. {
  191. delete *i;
  192. }
  193. for(unsigned int i=0; i < this->FinalPassCommands.size(); i++)
  194. {
  195. delete this->FinalPassCommands[i];
  196. }
  197. std::vector<cmFunctionBlocker*>::iterator pos;
  198. for (pos = this->FunctionBlockers.begin();
  199. pos != this->FunctionBlockers.end(); ++pos)
  200. {
  201. cmFunctionBlocker* b = *pos;
  202. delete b;
  203. }
  204. this->FunctionBlockers.clear();
  205. if (this->PolicyStack.size() != 1)
  206. {
  207. cmSystemTools::Error("Internal CMake Error, Policy Stack has not been"
  208. " popped properly");
  209. }
  210. }
  211. void cmMakefile::PrintStringVector(const char* s,
  212. const std::vector<std::string>& v) const
  213. {
  214. std::cout << s << ": ( \n";
  215. for(std::vector<std::string>::const_iterator i = v.begin();
  216. i != v.end(); ++i)
  217. {
  218. std::cout << (*i).c_str() << " ";
  219. }
  220. std::cout << " )\n";
  221. }
  222. void cmMakefile
  223. ::PrintStringVector(const char* s,
  224. const std::vector<std::pair<cmStdString, bool> >& v) const
  225. {
  226. std::cout << s << ": ( \n";
  227. for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
  228. = v.begin(); i != v.end(); ++i)
  229. {
  230. std::cout << i->first.c_str() << " " << i->second;
  231. }
  232. std::cout << " )\n";
  233. }
  234. // call print on all the classes in the makefile
  235. void cmMakefile::Print()
  236. {
  237. // print the class lists
  238. std::cout << "classes:\n";
  239. std::cout << " this->Targets: ";
  240. for (cmTargets::iterator l = this->Targets.begin();
  241. l != this->Targets.end(); l++)
  242. {
  243. std::cout << l->first << std::endl;
  244. }
  245. std::cout << " this->StartOutputDirectory; " <<
  246. this->StartOutputDirectory.c_str() << std::endl;
  247. std::cout << " this->HomeOutputDirectory; " <<
  248. this->HomeOutputDirectory.c_str() << std::endl;
  249. std::cout << " this->cmStartDirectory; " <<
  250. this->cmStartDirectory.c_str() << std::endl;
  251. std::cout << " this->cmHomeDirectory; " <<
  252. this->cmHomeDirectory.c_str() << std::endl;
  253. std::cout << " this->ProjectName; "
  254. << this->ProjectName.c_str() << std::endl;
  255. this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
  256. #if defined(CMAKE_BUILD_WITH_CMAKE)
  257. for( std::vector<cmSourceGroup>::const_iterator i =
  258. this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
  259. {
  260. std::cout << "Source Group: " << i->GetName() << std::endl;
  261. }
  262. #endif
  263. }
  264. bool cmMakefile::CommandExists(const char* name) const
  265. {
  266. return this->GetCMakeInstance()->CommandExists(name);
  267. }
  268. //----------------------------------------------------------------------------
  269. void cmMakefile::IssueMessage(cmake::MessageType t,
  270. std::string const& text) const
  271. {
  272. // Collect context information.
  273. cmListFileBacktrace backtrace;
  274. if(!this->CallStack.empty())
  275. {
  276. if((t == cmake::FATAL_ERROR) || (t == cmake::INTERNAL_ERROR))
  277. {
  278. this->CallStack.back().Status->SetNestedError(true);
  279. }
  280. this->GetBacktrace(backtrace);
  281. }
  282. else
  283. {
  284. cmListFileContext lfc;
  285. if(this->ListFileStack.empty())
  286. {
  287. // We are not processing the project. Add the directory-level context.
  288. lfc.FilePath = this->GetCurrentDirectory();
  289. lfc.FilePath += "/CMakeLists.txt";
  290. }
  291. else
  292. {
  293. // We are processing the project but are not currently executing a
  294. // command. Add whatever context information we have.
  295. lfc.FilePath = this->ListFileStack.back();
  296. }
  297. lfc.Line = 0;
  298. if(!this->GetCMakeInstance()->GetIsInTryCompile())
  299. {
  300. lfc.FilePath = this->LocalGenerator->Convert(lfc.FilePath.c_str(),
  301. cmLocalGenerator::HOME);
  302. }
  303. backtrace.push_back(lfc);
  304. }
  305. // Issue the message.
  306. this->GetCMakeInstance()->IssueMessage(t, text, backtrace);
  307. }
  308. //----------------------------------------------------------------------------
  309. bool cmMakefile::GetBacktrace(cmListFileBacktrace& backtrace) const
  310. {
  311. if(this->CallStack.empty())
  312. {
  313. return false;
  314. }
  315. for(CallStackType::const_reverse_iterator i = this->CallStack.rbegin();
  316. i != this->CallStack.rend(); ++i)
  317. {
  318. cmListFileContext lfc = *(*i).Context;
  319. lfc.FilePath = this->LocalGenerator->Convert(lfc.FilePath.c_str(),
  320. cmLocalGenerator::HOME);
  321. backtrace.push_back(lfc);
  322. }
  323. return true;
  324. }
  325. //----------------------------------------------------------------------------
  326. void cmMakefile::PrintCommandTrace(const cmListFileFunction& lff)
  327. {
  328. cmOStringStream msg;
  329. msg << lff.FilePath << "(" << lff.Line << "): ";
  330. msg << lff.Name << "(";
  331. for(std::vector<cmListFileArgument>::const_iterator i =
  332. lff.Arguments.begin(); i != lff.Arguments.end(); ++i)
  333. {
  334. msg << i->Value;
  335. msg << " ";
  336. }
  337. msg << ")";
  338. cmSystemTools::Message(msg.str().c_str());
  339. }
  340. //----------------------------------------------------------------------------
  341. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
  342. cmExecutionStatus &status)
  343. {
  344. bool result = true;
  345. // quick return if blocked
  346. if(this->IsFunctionBlocked(lff,status))
  347. {
  348. // No error.
  349. return result;
  350. }
  351. std::string name = lff.Name;
  352. // Place this call on the call stack.
  353. cmMakefileCall stack_manager(this, lff, status);
  354. static_cast<void>(stack_manager);
  355. // Lookup the command prototype.
  356. if(cmCommand* proto = this->GetCMakeInstance()->GetCommand(name.c_str()))
  357. {
  358. // Clone the prototype.
  359. cmsys::auto_ptr<cmCommand> pcmd(proto->Clone());
  360. pcmd->SetMakefile(this);
  361. // Decide whether to invoke the command.
  362. if(pcmd->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  363. (this->GetCMakeInstance()->GetWorkingMode() != cmake::SCRIPT_MODE
  364. || pcmd->IsScriptable()))
  365. {
  366. // if trace is enabled, print out invoke information
  367. if(this->GetCMakeInstance()->GetTrace())
  368. {
  369. this->PrintCommandTrace(lff);
  370. }
  371. // Try invoking the command.
  372. if(!pcmd->InvokeInitialPass(lff.Arguments,status) ||
  373. status.GetNestedError())
  374. {
  375. if(!status.GetNestedError())
  376. {
  377. // The command invocation requested that we report an error.
  378. this->IssueMessage(cmake::FATAL_ERROR, pcmd->GetError());
  379. }
  380. result = false;
  381. if ( this->GetCMakeInstance()->GetWorkingMode() != cmake::NORMAL_MODE)
  382. {
  383. cmSystemTools::SetFatalErrorOccured();
  384. }
  385. }
  386. else if(pcmd->HasFinalPass())
  387. {
  388. // use the command
  389. this->FinalPassCommands.push_back(pcmd.release());
  390. }
  391. }
  392. else if ( this->GetCMakeInstance()->GetWorkingMode() == cmake::SCRIPT_MODE
  393. && !pcmd->IsScriptable() )
  394. {
  395. std::string error = "Command ";
  396. error += pcmd->GetName();
  397. error += "() is not scriptable";
  398. this->IssueMessage(cmake::FATAL_ERROR, error);
  399. result = false;
  400. cmSystemTools::SetFatalErrorOccured();
  401. }
  402. }
  403. else
  404. {
  405. if(!cmSystemTools::GetFatalErrorOccured())
  406. {
  407. std::string error = "Unknown CMake command \"";
  408. error += lff.Name;
  409. error += "\".";
  410. this->IssueMessage(cmake::FATAL_ERROR, error);
  411. result = false;
  412. cmSystemTools::SetFatalErrorOccured();
  413. }
  414. }
  415. return result;
  416. }
  417. //----------------------------------------------------------------------------
  418. class cmMakefile::IncludeScope
  419. {
  420. public:
  421. IncludeScope(cmMakefile* mf, const char* fname, bool noPolicyScope);
  422. ~IncludeScope();
  423. void Quiet() { this->ReportError = false; }
  424. private:
  425. cmMakefile* Makefile;
  426. const char* File;
  427. bool NoPolicyScope;
  428. bool CheckCMP0011;
  429. bool ReportError;
  430. void EnforceCMP0011();
  431. };
  432. //----------------------------------------------------------------------------
  433. cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf, const char* fname,
  434. bool noPolicyScope):
  435. Makefile(mf), File(fname), NoPolicyScope(noPolicyScope),
  436. CheckCMP0011(false), ReportError(true)
  437. {
  438. if(!this->NoPolicyScope)
  439. {
  440. // Check CMP0011 to determine the policy scope type.
  441. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011))
  442. {
  443. case cmPolicies::WARN:
  444. // We need to push a scope to detect whether the script sets
  445. // any policies that would affect the includer and therefore
  446. // requires a warning. We use a weak scope to simulate OLD
  447. // behavior by allowing policy changes to affect the includer.
  448. this->Makefile->PushPolicy(true);
  449. this->CheckCMP0011 = true;
  450. break;
  451. case cmPolicies::OLD:
  452. // OLD behavior is to not push a scope at all.
  453. this->NoPolicyScope = true;
  454. break;
  455. case cmPolicies::REQUIRED_IF_USED:
  456. case cmPolicies::REQUIRED_ALWAYS:
  457. // We should never make this policy required, but we handle it
  458. // here just in case.
  459. this->CheckCMP0011 = true;
  460. case cmPolicies::NEW:
  461. // NEW behavior is to push a (strong) scope.
  462. this->Makefile->PushPolicy();
  463. break;
  464. }
  465. }
  466. // The included file cannot pop our policy scope.
  467. this->Makefile->PushPolicyBarrier();
  468. }
  469. //----------------------------------------------------------------------------
  470. cmMakefile::IncludeScope::~IncludeScope()
  471. {
  472. // Enforce matching policy scopes inside the included file.
  473. this->Makefile->PopPolicyBarrier(this->ReportError);
  474. if(!this->NoPolicyScope)
  475. {
  476. // If we need to enforce policy CMP0011 then the top entry is the
  477. // one we pushed above. If the entry is empty, then the included
  478. // script did not set any policies that might affect the includer so
  479. // we do not need to enforce the policy.
  480. if(this->CheckCMP0011 && this->Makefile->PolicyStack.back().empty())
  481. {
  482. this->CheckCMP0011 = false;
  483. }
  484. // Pop the scope we pushed for the script.
  485. this->Makefile->PopPolicy();
  486. // We enforce the policy after the script's policy stack entry has
  487. // been removed.
  488. if(this->CheckCMP0011)
  489. {
  490. this->EnforceCMP0011();
  491. }
  492. }
  493. }
  494. //----------------------------------------------------------------------------
  495. void cmMakefile::IncludeScope::EnforceCMP0011()
  496. {
  497. // We check the setting of this policy again because the included
  498. // script might actually set this policy for its includer.
  499. cmPolicies* policies = this->Makefile->GetPolicies();
  500. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011))
  501. {
  502. case cmPolicies::WARN:
  503. // Warn because the user did not set this policy.
  504. {
  505. cmOStringStream w;
  506. w << policies->GetPolicyWarning(cmPolicies::CMP0011) << "\n"
  507. << "The included script\n " << this->File << "\n"
  508. << "affects policy settings. "
  509. << "CMake is implying the NO_POLICY_SCOPE option for compatibility, "
  510. << "so the effects are applied to the including context.";
  511. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  512. }
  513. break;
  514. case cmPolicies::REQUIRED_IF_USED:
  515. case cmPolicies::REQUIRED_ALWAYS:
  516. {
  517. cmOStringStream e;
  518. e << policies->GetRequiredPolicyError(cmPolicies::CMP0011) << "\n"
  519. << "The included script\n " << this->File << "\n"
  520. << "affects policy settings, so it requires this policy to be set.";
  521. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  522. }
  523. break;
  524. case cmPolicies::OLD:
  525. case cmPolicies::NEW:
  526. // The script set this policy. We assume the purpose of the
  527. // script is to initialize policies for its includer, and since
  528. // the policy is now set for later scripts, we do not warn.
  529. break;
  530. }
  531. }
  532. //----------------------------------------------------------------------------
  533. // Parse the given CMakeLists.txt file executing all commands
  534. //
  535. bool cmMakefile::ReadListFile(const char* filename_in,
  536. const char *external_in,
  537. std::string* fullPath,
  538. bool noPolicyScope)
  539. {
  540. std::string currentParentFile
  541. = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  542. std::string currentFile
  543. = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
  544. this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);
  545. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  546. const char* external = 0;
  547. std::string external_abs;
  548. const char* filename = filename_in;
  549. std::string filename_abs;
  550. if (external_in)
  551. {
  552. external_abs =
  553. cmSystemTools::CollapseFullPath(external_in,
  554. this->cmStartDirectory.c_str());
  555. external = external_abs.c_str();
  556. if (filename_in)
  557. {
  558. filename_abs =
  559. cmSystemTools::CollapseFullPath(filename_in,
  560. this->cmStartDirectory.c_str());
  561. filename = filename_abs.c_str();
  562. }
  563. }
  564. // keep track of the current file being read
  565. if (filename)
  566. {
  567. if(this->cmCurrentListFile != filename)
  568. {
  569. this->cmCurrentListFile = filename;
  570. }
  571. }
  572. // Now read the input file
  573. const char *filenametoread= filename;
  574. if( external)
  575. {
  576. filenametoread= external;
  577. }
  578. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread);
  579. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  580. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  581. cmSystemTools::GetFilenamePath(filenametoread).c_str());
  582. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  583. // try to see if the list file is the top most
  584. // list file for a project, and if it is, then it
  585. // must have a project command. If there is not
  586. // one, then cmake will provide one via the
  587. // cmListFileCache class.
  588. bool requireProjectCommand = false;
  589. if(!external && this->cmStartDirectory == this->cmHomeDirectory)
  590. {
  591. if(cmSystemTools::LowerCase(
  592. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  593. {
  594. requireProjectCommand = true;
  595. }
  596. }
  597. // push the listfile onto the stack
  598. this->ListFileStack.push_back(filenametoread);
  599. if(fullPath!=0)
  600. {
  601. *fullPath=filenametoread;
  602. }
  603. cmListFile cacheFile;
  604. if( !cacheFile.ParseFile(filenametoread, requireProjectCommand, this) )
  605. {
  606. // pop the listfile off the stack
  607. this->ListFileStack.pop_back();
  608. if(fullPath!=0)
  609. {
  610. *fullPath = "";
  611. }
  612. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  613. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  614. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  615. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  616. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  617. cmSystemTools::GetFilenamePath(currentFile).c_str());
  618. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  619. return false;
  620. }
  621. // add this list file to the list of dependencies
  622. this->ListFiles.push_back( filenametoread);
  623. // Enforce balanced blocks (if/endif, function/endfunction, etc.).
  624. {
  625. LexicalPushPop lexScope(this);
  626. IncludeScope incScope(this, filenametoread, noPolicyScope);
  627. // Run the parsed commands.
  628. const size_t numberFunctions = cacheFile.Functions.size();
  629. for(size_t i =0; i < numberFunctions; ++i)
  630. {
  631. cmExecutionStatus status;
  632. this->ExecuteCommand(cacheFile.Functions[i],status);
  633. if(cmSystemTools::GetFatalErrorOccured())
  634. {
  635. // Exit early due to error.
  636. lexScope.Quiet();
  637. incScope.Quiet();
  638. break;
  639. }
  640. if(status.GetReturnInvoked())
  641. {
  642. // Exit early due to return command.
  643. break;
  644. }
  645. }
  646. }
  647. // If this is the directory-level CMakeLists.txt file then perform
  648. // some extra checks.
  649. if(this->ListFileStack.size() == 1)
  650. {
  651. this->EnforceDirectoryLevelRules();
  652. }
  653. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  654. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  655. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  656. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  657. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  658. cmSystemTools::GetFilenamePath(currentFile).c_str());
  659. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  660. // pop the listfile off the stack
  661. this->ListFileStack.pop_back();
  662. // Check for unused variables
  663. this->CheckForUnusedVariables();
  664. return true;
  665. }
  666. //----------------------------------------------------------------------------
  667. void cmMakefile::EnforceDirectoryLevelRules()
  668. {
  669. // Diagnose a violation of CMP0000 if necessary.
  670. if(this->CheckCMP0000)
  671. {
  672. cmOStringStream msg;
  673. msg << "No cmake_minimum_required command is present. "
  674. << "A line of code such as\n"
  675. << " cmake_minimum_required(VERSION "
  676. << cmVersion::GetMajorVersion() << "."
  677. << cmVersion::GetMinorVersion()
  678. << ")\n"
  679. << "should be added at the top of the file. "
  680. << "The version specified may be lower if you wish to "
  681. << "support older CMake versions for this project. "
  682. << "For more information run "
  683. << "\"cmake --help-policy CMP0000\".";
  684. switch (this->GetPolicyStatus(cmPolicies::CMP0000))
  685. {
  686. case cmPolicies::WARN:
  687. // Warn because the user did not provide a mimimum required
  688. // version.
  689. this->IssueMessage(cmake::AUTHOR_WARNING, msg.str().c_str());
  690. case cmPolicies::OLD:
  691. // OLD behavior is to use policy version 2.4 set in
  692. // cmListFileCache.
  693. break;
  694. case cmPolicies::REQUIRED_IF_USED:
  695. case cmPolicies::REQUIRED_ALWAYS:
  696. case cmPolicies::NEW:
  697. // NEW behavior is to issue an error.
  698. this->IssueMessage(cmake::FATAL_ERROR, msg.str().c_str());
  699. cmSystemTools::SetFatalErrorOccured();
  700. return;
  701. }
  702. }
  703. }
  704. void cmMakefile::AddCommand(cmCommand* wg)
  705. {
  706. this->GetCMakeInstance()->AddCommand(wg);
  707. }
  708. // Set the make file
  709. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  710. {
  711. this->LocalGenerator = lg;
  712. // the source groups need to access the global generator
  713. // so don't create them until the lg is set
  714. #if defined(CMAKE_BUILD_WITH_CMAKE)
  715. this->AddSourceGroup("", "^.*$");
  716. this->AddSourceGroup
  717. ("Source Files",
  718. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|f|f90|for|fpp"
  719. "|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  720. this->AddSourceGroup("Header Files", CM_HEADER_REGEX);
  721. this->AddSourceGroup("CMake Rules", "\\.rule$");
  722. this->AddSourceGroup("Resources", "\\.plist$");
  723. this->AddSourceGroup("Object Files", "\\.(lo|o|obj)$");
  724. #endif
  725. this->WarnUnused = this->GetCMakeInstance()->GetWarnUnused();
  726. this->CheckSystemVars = this->GetCMakeInstance()->GetCheckSystemVars();
  727. }
  728. bool cmMakefile::NeedBackwardsCompatibility(unsigned int major,
  729. unsigned int minor,
  730. unsigned int patch)
  731. {
  732. if(this->LocalGenerator)
  733. {
  734. return
  735. this->LocalGenerator->NeedBackwardsCompatibility(major, minor, patch);
  736. }
  737. else
  738. {
  739. return false;
  740. }
  741. }
  742. namespace
  743. {
  744. struct file_exists
  745. {
  746. bool operator()(const std::string& path) const
  747. {
  748. return cmSystemTools::FileExists(path.c_str());
  749. }
  750. };
  751. }
  752. void cmMakefile::FinalPass()
  753. {
  754. // do all the variable expansions here
  755. this->ExpandVariablesCMP0019();
  756. // give all the commands a chance to do something
  757. // after the file has been parsed before generation
  758. for(std::vector<cmCommand*>::iterator i = this->FinalPassCommands.begin();
  759. i != this->FinalPassCommands.end(); ++i)
  760. {
  761. (*i)->FinalPass();
  762. }
  763. //go through all configured files and see which ones still exist.
  764. //we don't want cmake to re-run if a configured file is created and deleted
  765. //during processing as that would make it a transient file that can't
  766. //influence the build process
  767. //remove_if will move all items that don't have a valid file name to the
  768. //back of the vector
  769. std::vector<std::string>::iterator new_end = std::remove_if(
  770. this->OutputFiles.begin(),
  771. this->OutputFiles.end(),
  772. file_exists() );
  773. //we just have to erase all items at the back
  774. this->OutputFiles.erase(new_end, this->OutputFiles.end() );
  775. }
  776. // Generate the output file
  777. void cmMakefile::ConfigureFinalPass()
  778. {
  779. this->FinalPass();
  780. const char* oldValue
  781. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  782. if (oldValue && atof(oldValue) <= 1.2)
  783. {
  784. cmSystemTools::Error("You have requested backwards compatibility "
  785. "with CMake version 1.2 or earlier. This version "
  786. "of CMake only supports backwards compatibility "
  787. "with CMake 1.4 or later. For compatibility with "
  788. "1.2 or earlier please use CMake 2.0");
  789. }
  790. for (cmTargets::iterator l = this->Targets.begin();
  791. l != this->Targets.end(); l++)
  792. {
  793. l->second.FinishConfigure();
  794. }
  795. }
  796. //----------------------------------------------------------------------------
  797. void
  798. cmMakefile::AddCustomCommandToTarget(const char* target,
  799. const std::vector<std::string>& depends,
  800. const cmCustomCommandLines& commandLines,
  801. cmTarget::CustomCommandType type,
  802. const char* comment,
  803. const char* workingDir,
  804. bool escapeOldStyle)
  805. {
  806. // Find the target to which to add the custom command.
  807. cmTargets::iterator ti = this->Targets.find(target);
  808. if(ti != this->Targets.end())
  809. {
  810. if(ti->second.GetType() == cmTarget::OBJECT_LIBRARY)
  811. {
  812. cmOStringStream e;
  813. e << "Target \"" << target << "\" is an OBJECT library "
  814. "that may not have PRE_BUILD, PRE_LINK, or POST_BUILD commands.";
  815. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  816. return;
  817. }
  818. // Add the command to the appropriate build step for the target.
  819. std::vector<std::string> no_output;
  820. cmCustomCommand cc(this, no_output, depends,
  821. commandLines, comment, workingDir);
  822. cc.SetEscapeOldStyle(escapeOldStyle);
  823. cc.SetEscapeAllowMakeVars(true);
  824. switch(type)
  825. {
  826. case cmTarget::PRE_BUILD:
  827. ti->second.GetPreBuildCommands().push_back(cc);
  828. break;
  829. case cmTarget::PRE_LINK:
  830. ti->second.GetPreLinkCommands().push_back(cc);
  831. break;
  832. case cmTarget::POST_BUILD:
  833. ti->second.GetPostBuildCommands().push_back(cc);
  834. break;
  835. }
  836. }
  837. }
  838. //----------------------------------------------------------------------------
  839. cmSourceFile*
  840. cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
  841. const std::vector<std::string>& depends,
  842. const char* main_dependency,
  843. const cmCustomCommandLines& commandLines,
  844. const char* comment,
  845. const char* workingDir,
  846. bool replace,
  847. bool escapeOldStyle)
  848. {
  849. // Make sure there is at least one output.
  850. if(outputs.empty())
  851. {
  852. cmSystemTools::Error("Attempt to add a custom rule with no output!");
  853. return 0;
  854. }
  855. // Validate custom commands. TODO: More strict?
  856. for(cmCustomCommandLines::const_iterator i=commandLines.begin();
  857. i != commandLines.end(); ++i)
  858. {
  859. cmCustomCommandLine const& cl = *i;
  860. if(!cl.empty() && !cl[0].empty() && cl[0][0] == '"')
  861. {
  862. cmOStringStream e;
  863. e << "COMMAND may not contain literal quotes:\n " << cl[0] << "\n";
  864. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  865. return 0;
  866. }
  867. }
  868. // Choose a source file on which to store the custom command.
  869. cmSourceFile* file = 0;
  870. if(main_dependency && main_dependency[0])
  871. {
  872. // The main dependency was specified. Use it unless a different
  873. // custom command already used it.
  874. file = this->GetSource(main_dependency);
  875. if(file && file->GetCustomCommand() && !replace)
  876. {
  877. // The main dependency already has a custom command.
  878. if(commandLines == file->GetCustomCommand()->GetCommandLines())
  879. {
  880. // The existing custom command is identical. Silently ignore
  881. // the duplicate.
  882. return file;
  883. }
  884. else
  885. {
  886. // The existing custom command is different. We need to
  887. // generate a rule file for this new command.
  888. file = 0;
  889. }
  890. }
  891. else
  892. {
  893. // The main dependency does not have a custom command or we are
  894. // allowed to replace it. Use it to store the command.
  895. file = this->GetOrCreateSource(main_dependency);
  896. }
  897. }
  898. // Generate a rule file if the main dependency is not available.
  899. if(!file)
  900. {
  901. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  902. // Construct a rule file associated with the first output produced.
  903. std::string outName = gg->GenerateRuleFile(outputs[0]);
  904. // Check if the rule file already exists.
  905. file = this->GetSource(outName.c_str());
  906. if(file && file->GetCustomCommand() && !replace)
  907. {
  908. // The rule file already exists.
  909. if(commandLines != file->GetCustomCommand()->GetCommandLines())
  910. {
  911. cmSystemTools::Error("Attempt to add a custom rule to output \"",
  912. outName.c_str(),
  913. "\" which already has a custom rule.");
  914. }
  915. return file;
  916. }
  917. // Create a cmSourceFile for the rule file.
  918. file = this->GetOrCreateSource(outName.c_str(), true);
  919. file->SetProperty("__CMAKE_RULE", "1");
  920. }
  921. // Always create the output sources and mark them generated.
  922. for(std::vector<std::string>::const_iterator o = outputs.begin();
  923. o != outputs.end(); ++o)
  924. {
  925. if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
  926. {
  927. out->SetProperty("GENERATED", "1");
  928. }
  929. }
  930. // Construct a complete list of dependencies.
  931. std::vector<std::string> depends2(depends);
  932. if(main_dependency && main_dependency[0])
  933. {
  934. depends2.push_back(main_dependency);
  935. }
  936. // Attach the custom command to the file.
  937. if(file)
  938. {
  939. cmCustomCommand* cc =
  940. new cmCustomCommand(this, outputs, depends2, commandLines,
  941. comment, workingDir);
  942. cc->SetEscapeOldStyle(escapeOldStyle);
  943. cc->SetEscapeAllowMakeVars(true);
  944. file->SetCustomCommand(cc);
  945. this->UpdateOutputToSourceMap(outputs, file);
  946. }
  947. return file;
  948. }
  949. //----------------------------------------------------------------------------
  950. void
  951. cmMakefile::UpdateOutputToSourceMap(std::vector<std::string> const& outputs,
  952. cmSourceFile* source)
  953. {
  954. for(std::vector<std::string>::const_iterator o = outputs.begin();
  955. o != outputs.end(); ++o)
  956. {
  957. this->UpdateOutputToSourceMap(*o, source);
  958. }
  959. }
  960. //----------------------------------------------------------------------------
  961. void
  962. cmMakefile::UpdateOutputToSourceMap(std::string const& output,
  963. cmSourceFile* source)
  964. {
  965. this->OutputToSource[output] = source;
  966. }
  967. //----------------------------------------------------------------------------
  968. cmSourceFile*
  969. cmMakefile::AddCustomCommandToOutput(const char* output,
  970. const std::vector<std::string>& depends,
  971. const char* main_dependency,
  972. const cmCustomCommandLines& commandLines,
  973. const char* comment,
  974. const char* workingDir,
  975. bool replace,
  976. bool escapeOldStyle)
  977. {
  978. std::vector<std::string> outputs;
  979. outputs.push_back(output);
  980. return this->AddCustomCommandToOutput(outputs, depends, main_dependency,
  981. commandLines, comment, workingDir,
  982. replace, escapeOldStyle);
  983. }
  984. //----------------------------------------------------------------------------
  985. void
  986. cmMakefile::AddCustomCommandOldStyle(const char* target,
  987. const std::vector<std::string>& outputs,
  988. const std::vector<std::string>& depends,
  989. const char* source,
  990. const cmCustomCommandLines& commandLines,
  991. const char* comment)
  992. {
  993. // Translate the old-style signature to one of the new-style
  994. // signatures.
  995. if(strcmp(source, target) == 0)
  996. {
  997. // In the old-style signature if the source and target were the
  998. // same then it added a post-build rule to the target. Preserve
  999. // this behavior.
  1000. this->AddCustomCommandToTarget(target, depends, commandLines,
  1001. cmTarget::POST_BUILD, comment, 0);
  1002. return;
  1003. }
  1004. // Each output must get its own copy of this rule.
  1005. cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
  1006. "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
  1007. "hm|hpp|hxx|in|txx|inl)$");
  1008. for(std::vector<std::string>::const_iterator oi = outputs.begin();
  1009. oi != outputs.end(); ++oi)
  1010. {
  1011. // Get the name of this output.
  1012. const char* output = oi->c_str();
  1013. cmSourceFile* sf;
  1014. // Choose whether to use a main dependency.
  1015. if(sourceFiles.find(source))
  1016. {
  1017. // The source looks like a real file. Use it as the main dependency.
  1018. sf = this->AddCustomCommandToOutput(output, depends, source,
  1019. commandLines, comment, 0);
  1020. }
  1021. else
  1022. {
  1023. // The source may not be a real file. Do not use a main dependency.
  1024. const char* no_main_dependency = 0;
  1025. std::vector<std::string> depends2 = depends;
  1026. depends2.push_back(source);
  1027. sf = this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
  1028. commandLines, comment, 0);
  1029. }
  1030. // If the rule was added to the source (and not a .rule file),
  1031. // then add the source to the target to make sure the rule is
  1032. // included.
  1033. if(sf && !sf->GetPropertyAsBool("__CMAKE_RULE"))
  1034. {
  1035. if (this->Targets.find(target) != this->Targets.end())
  1036. {
  1037. this->Targets[target].AddSourceFile(sf);
  1038. }
  1039. else
  1040. {
  1041. cmSystemTools::Error("Attempt to add a custom rule to a target "
  1042. "that does not exist yet for target ", target);
  1043. return;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. //----------------------------------------------------------------------------
  1049. void cmMakefile::AddUtilityCommand(const char* utilityName,
  1050. bool excludeFromAll,
  1051. const std::vector<std::string>& depends,
  1052. const char* workingDirectory,
  1053. const char* command,
  1054. const char* arg1,
  1055. const char* arg2,
  1056. const char* arg3,
  1057. const char* arg4)
  1058. {
  1059. // Construct the command line for the custom command.
  1060. cmCustomCommandLine commandLine;
  1061. commandLine.push_back(command);
  1062. if(arg1)
  1063. {
  1064. commandLine.push_back(arg1);
  1065. }
  1066. if(arg2)
  1067. {
  1068. commandLine.push_back(arg2);
  1069. }
  1070. if(arg3)
  1071. {
  1072. commandLine.push_back(arg3);
  1073. }
  1074. if(arg4)
  1075. {
  1076. commandLine.push_back(arg4);
  1077. }
  1078. cmCustomCommandLines commandLines;
  1079. commandLines.push_back(commandLine);
  1080. // Call the real signature of this method.
  1081. this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
  1082. depends, commandLines);
  1083. }
  1084. //----------------------------------------------------------------------------
  1085. cmTarget*
  1086. cmMakefile::AddUtilityCommand(const char* utilityName,
  1087. bool excludeFromAll,
  1088. const char* workingDirectory,
  1089. const std::vector<std::string>& depends,
  1090. const cmCustomCommandLines& commandLines,
  1091. bool escapeOldStyle, const char* comment)
  1092. {
  1093. // Create a target instance for this utility.
  1094. cmTarget* target = this->AddNewTarget(cmTarget::UTILITY, utilityName);
  1095. if (excludeFromAll)
  1096. {
  1097. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1098. }
  1099. if(!comment)
  1100. {
  1101. // Use an empty comment to avoid generation of default comment.
  1102. comment = "";
  1103. }
  1104. // Store the custom command in the target.
  1105. std::string force = this->GetStartOutputDirectory();
  1106. force += cmake::GetCMakeFilesDirectory();
  1107. force += "/";
  1108. force += utilityName;
  1109. const char* no_main_dependency = 0;
  1110. bool no_replace = false;
  1111. this->AddCustomCommandToOutput(force.c_str(), depends,
  1112. no_main_dependency,
  1113. commandLines, comment,
  1114. workingDirectory, no_replace,
  1115. escapeOldStyle);
  1116. cmSourceFile* sf = target->AddSource(force.c_str());
  1117. // The output is not actually created so mark it symbolic.
  1118. if(sf)
  1119. {
  1120. sf->SetProperty("SYMBOLIC", "1");
  1121. }
  1122. else
  1123. {
  1124. cmSystemTools::Error("Could not get source file entry for ",
  1125. force.c_str());
  1126. }
  1127. return target;
  1128. }
  1129. void cmMakefile::AddDefineFlag(const char* flag)
  1130. {
  1131. if (!flag)
  1132. {
  1133. return;
  1134. }
  1135. // Update the string used for the old DEFINITIONS property.
  1136. this->AddDefineFlag(flag, this->DefineFlagsOrig);
  1137. // If this is really a definition, update COMPILE_DEFINITIONS.
  1138. if(this->ParseDefineFlag(flag, false))
  1139. {
  1140. return;
  1141. }
  1142. // Add this flag that does not look like a definition.
  1143. this->AddDefineFlag(flag, this->DefineFlags);
  1144. }
  1145. void cmMakefile::AddDefineFlag(const char* flag, std::string& dflags)
  1146. {
  1147. // remove any \n\r
  1148. std::string ret = flag;
  1149. std::string::size_type pos = 0;
  1150. while((pos = ret.find('\n', pos)) != std::string::npos)
  1151. {
  1152. ret[pos] = ' ';
  1153. pos++;
  1154. }
  1155. pos = 0;
  1156. while((pos = ret.find('\r', pos)) != std::string::npos)
  1157. {
  1158. ret[pos] = ' ';
  1159. pos++;
  1160. }
  1161. dflags += " ";
  1162. dflags += ret;
  1163. }
  1164. void cmMakefile::RemoveDefineFlag(const char* flag)
  1165. {
  1166. // Check the length of the flag to remove.
  1167. std::string::size_type len = strlen(flag);
  1168. if(len < 1)
  1169. {
  1170. return;
  1171. }
  1172. // Update the string used for the old DEFINITIONS property.
  1173. this->RemoveDefineFlag(flag, len, this->DefineFlagsOrig);
  1174. // If this is really a definition, update COMPILE_DEFINITIONS.
  1175. if(this->ParseDefineFlag(flag, true))
  1176. {
  1177. return;
  1178. }
  1179. // Remove this flag that does not look like a definition.
  1180. this->RemoveDefineFlag(flag, len, this->DefineFlags);
  1181. }
  1182. void cmMakefile::RemoveDefineFlag(const char* flag,
  1183. std::string::size_type len,
  1184. std::string& dflags)
  1185. {
  1186. // Remove all instances of the flag that are surrounded by
  1187. // whitespace or the beginning/end of the string.
  1188. for(std::string::size_type lpos = dflags.find(flag, 0);
  1189. lpos != std::string::npos; lpos = dflags.find(flag, lpos))
  1190. {
  1191. std::string::size_type rpos = lpos + len;
  1192. if((lpos <= 0 || isspace(dflags[lpos-1])) &&
  1193. (rpos >= dflags.size() || isspace(dflags[rpos])))
  1194. {
  1195. dflags.erase(lpos, len);
  1196. }
  1197. else
  1198. {
  1199. ++lpos;
  1200. }
  1201. }
  1202. }
  1203. void cmMakefile::AddCompileOption(const char* option)
  1204. {
  1205. this->AppendProperty("COMPILE_OPTIONS", option);
  1206. }
  1207. bool cmMakefile::ParseDefineFlag(std::string const& def, bool remove)
  1208. {
  1209. // Create a regular expression to match valid definitions.
  1210. static cmsys::RegularExpression
  1211. valid("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=.*)?$");
  1212. // Make sure the definition matches.
  1213. if(!valid.find(def.c_str()))
  1214. {
  1215. return false;
  1216. }
  1217. // VS6 IDE does not support definition values with spaces in
  1218. // combination with '"', '$', or ';'.
  1219. if((strcmp(this->LocalGenerator->GetGlobalGenerator()->GetName(),
  1220. "Visual Studio 6") == 0) &&
  1221. (def.find(" ") != def.npos && def.find_first_of("\"$;") != def.npos))
  1222. {
  1223. return false;
  1224. }
  1225. // Definitions with non-trivial values require a policy check.
  1226. static cmsys::RegularExpression
  1227. trivial("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=[A-Za-z0-9_.]+)?$");
  1228. if(!trivial.find(def.c_str()))
  1229. {
  1230. // This definition has a non-trivial value.
  1231. switch(this->GetPolicyStatus(cmPolicies::CMP0005))
  1232. {
  1233. case cmPolicies::WARN:
  1234. this->IssueMessage(
  1235. cmake::AUTHOR_WARNING,
  1236. this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0005)
  1237. );
  1238. case cmPolicies::OLD:
  1239. // OLD behavior is to not escape the value. We should not
  1240. // convert the definition to use the property.
  1241. return false;
  1242. case cmPolicies::REQUIRED_IF_USED:
  1243. case cmPolicies::REQUIRED_ALWAYS:
  1244. this->IssueMessage(
  1245. cmake::FATAL_ERROR,
  1246. this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0005)
  1247. );
  1248. return false;
  1249. case cmPolicies::NEW:
  1250. // NEW behavior is to escape the value. Proceed to convert it
  1251. // to an entry in the property.
  1252. break;
  1253. }
  1254. }
  1255. // Get the definition part after the flag.
  1256. const char* define = def.c_str() + 2;
  1257. if(remove)
  1258. {
  1259. if(const char* cdefs = this->GetProperty("COMPILE_DEFINITIONS"))
  1260. {
  1261. // Expand the list.
  1262. std::vector<std::string> defs;
  1263. cmSystemTools::ExpandListArgument(cdefs, defs);
  1264. // Recompose the list without the definition.
  1265. std::string ndefs;
  1266. const char* sep = "";
  1267. for(std::vector<std::string>::const_iterator di = defs.begin();
  1268. di != defs.end(); ++di)
  1269. {
  1270. if(*di != define)
  1271. {
  1272. ndefs += sep;
  1273. sep = ";";
  1274. ndefs += *di;
  1275. }
  1276. }
  1277. // Store the new list.
  1278. this->SetProperty("COMPILE_DEFINITIONS", ndefs.c_str());
  1279. }
  1280. }
  1281. else
  1282. {
  1283. // Append the definition to the directory property.
  1284. this->AppendProperty("COMPILE_DEFINITIONS", define);
  1285. }
  1286. return true;
  1287. }
  1288. void cmMakefile::AddLinkLibrary(const char* lib,
  1289. cmTarget::LinkLibraryType llt)
  1290. {
  1291. cmTarget::LibraryID tmp;
  1292. tmp.first = lib;
  1293. tmp.second = llt;
  1294. this->LinkLibraries.push_back(tmp);
  1295. }
  1296. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  1297. const char* lib,
  1298. cmTarget::LinkLibraryType llt)
  1299. {
  1300. cmTargets::iterator i = this->Targets.find(target);
  1301. if ( i != this->Targets.end())
  1302. {
  1303. cmTarget* tgt =
  1304. this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0,lib);
  1305. if(tgt)
  1306. {
  1307. // CMake versions below 2.4 allowed linking to modules.
  1308. bool allowModules = this->NeedBackwardsCompatibility(2,2);
  1309. // if it is not a static or shared library then you can not link to it
  1310. if(!((tgt->GetType() == cmTarget::STATIC_LIBRARY) ||
  1311. (tgt->GetType() == cmTarget::SHARED_LIBRARY) ||
  1312. tgt->IsExecutableWithExports()))
  1313. {
  1314. cmOStringStream e;
  1315. e << "Target \"" << lib << "\" of type "
  1316. << cmTarget::GetTargetTypeName(tgt->GetType())
  1317. << " may not be linked into another target. "
  1318. << "One may link only to STATIC or SHARED libraries, or "
  1319. << "to executables with the ENABLE_EXPORTS property set.";
  1320. // in older versions of cmake linking to modules was allowed
  1321. if( tgt->GetType() == cmTarget::MODULE_LIBRARY )
  1322. {
  1323. e << "\n"
  1324. << "If you are developing a new project, re-organize it to avoid "
  1325. << "linking to modules. "
  1326. << "If you are just trying to build an existing project, "
  1327. << "set CMAKE_BACKWARDS_COMPATIBILITY to 2.2 or lower to allow "
  1328. << "linking to modules.";
  1329. }
  1330. // if no modules are allowed then this is always an error
  1331. if(!allowModules ||
  1332. // if we allow modules but the type is not a module then it is
  1333. // still an error
  1334. (allowModules && tgt->GetType() != cmTarget::MODULE_LIBRARY))
  1335. {
  1336. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1337. }
  1338. }
  1339. }
  1340. i->second.AddLinkLibrary( *this, target, lib, llt );
  1341. }
  1342. else
  1343. {
  1344. cmOStringStream e;
  1345. e << "Attempt to add link library \""
  1346. << lib << "\" to target \""
  1347. << target << "\" which is not built in this directory.";
  1348. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1349. }
  1350. }
  1351. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  1352. const char* d)
  1353. {
  1354. cmTargets::iterator i = this->Targets.find(target);
  1355. if ( i != this->Targets.end())
  1356. {
  1357. if(this->IsAlias(target))
  1358. {
  1359. cmOStringStream e;
  1360. e << "ALIAS target \"" << target << "\" "
  1361. << "may not be linked into another target.";
  1362. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1363. return;
  1364. }
  1365. i->second.AddLinkDirectory( d );
  1366. }
  1367. else
  1368. {
  1369. cmSystemTools::Error
  1370. ("Attempt to add link directories to non-existent target: ",
  1371. target, " for directory ", d);
  1372. }
  1373. }
  1374. void cmMakefile::AddLinkLibrary(const char* lib)
  1375. {
  1376. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  1377. }
  1378. void cmMakefile::AddLinkDirectory(const char* dir)
  1379. {
  1380. // Don't add a link directory that is already present. Yes, this
  1381. // linear search results in n^2 behavior, but n won't be getting
  1382. // much bigger than 20. We cannot use a set because of order
  1383. // dependency of the link search path.
  1384. if(!dir)
  1385. {
  1386. return;
  1387. }
  1388. // remove trailing slashes
  1389. if(dir[strlen(dir)-1] == '/')
  1390. {
  1391. std::string newdir = dir;
  1392. newdir = newdir.substr(0, newdir.size()-1);
  1393. if(std::find(this->LinkDirectories.begin(),
  1394. this->LinkDirectories.end(),
  1395. newdir.c_str()) == this->LinkDirectories.end())
  1396. {
  1397. this->LinkDirectories.push_back(newdir);
  1398. }
  1399. }
  1400. else
  1401. {
  1402. if(std::find(this->LinkDirectories.begin(),
  1403. this->LinkDirectories.end(), dir)
  1404. == this->LinkDirectories.end())
  1405. {
  1406. this->LinkDirectories.push_back(dir);
  1407. }
  1408. }
  1409. }
  1410. void cmMakefile::InitializeFromParent()
  1411. {
  1412. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  1413. // Initialize definitions with the closure of the parent scope.
  1414. this->Internal->VarStack.top() = parent->Internal->VarStack.top().Closure();
  1415. const std::vector<cmValueWithOrigin> parentIncludes =
  1416. parent->GetIncludeDirectoriesEntries();
  1417. this->IncludeDirectoriesEntries.insert(this->IncludeDirectoriesEntries.end(),
  1418. parentIncludes.begin(),
  1419. parentIncludes.end());
  1420. const std::vector<cmValueWithOrigin> parentOptions =
  1421. parent->GetCompileOptionsEntries();
  1422. this->CompileOptionsEntries.insert(this->CompileOptionsEntries.end(),
  1423. parentOptions.begin(),
  1424. parentOptions.end());
  1425. const std::vector<cmValueWithOrigin> parentDefines =
  1426. parent->GetCompileDefinitionsEntries();
  1427. this->CompileDefinitionsEntries.insert(this->CompileDefinitionsEntries.end(),
  1428. parentDefines.begin(),
  1429. parentDefines.end());
  1430. this->SystemIncludeDirectories = parent->SystemIncludeDirectories;
  1431. // define flags
  1432. this->DefineFlags = parent->DefineFlags;
  1433. this->DefineFlagsOrig = parent->DefineFlagsOrig;
  1434. // Include transform property. There is no per-config version.
  1435. {
  1436. const char* prop = "IMPLICIT_DEPENDS_INCLUDE_TRANSFORM";
  1437. this->SetProperty(prop, parent->GetProperty(prop));
  1438. }
  1439. // compile definitions property and per-config versions
  1440. {
  1441. this->SetProperty("COMPILE_DEFINITIONS",
  1442. parent->GetProperty("COMPILE_DEFINITIONS"));
  1443. std::vector<std::string> configs;
  1444. this->GetConfigurations(configs);
  1445. for(std::vector<std::string>::const_iterator ci = configs.begin();
  1446. ci != configs.end(); ++ci)
  1447. {
  1448. std::string defPropName = "COMPILE_DEFINITIONS_";
  1449. defPropName += cmSystemTools::UpperCase(*ci);
  1450. this->SetProperty(defPropName.c_str(),
  1451. parent->GetProperty(defPropName.c_str()));
  1452. }
  1453. }
  1454. // link libraries
  1455. this->LinkLibraries = parent->LinkLibraries;
  1456. // link directories
  1457. this->LinkDirectories = parent->LinkDirectories;
  1458. // the initial project name
  1459. this->ProjectName = parent->ProjectName;
  1460. // Copy include regular expressions.
  1461. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  1462. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  1463. // Imported targets.
  1464. this->ImportedTargets = parent->ImportedTargets;
  1465. }
  1466. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  1467. {
  1468. // copy our variables from the child makefile
  1469. lg2->GetMakefile()->InitializeFromParent();
  1470. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  1471. if (this->GetCMakeInstance()->GetDebugOutput())
  1472. {
  1473. std::string msg=" Entering ";
  1474. msg += lg2->GetMakefile()->GetCurrentDirectory();
  1475. cmSystemTools::Message(msg.c_str());
  1476. }
  1477. // finally configure the subdir
  1478. lg2->Configure();
  1479. if (this->GetCMakeInstance()->GetDebugOutput())
  1480. {
  1481. std::string msg=" Returning to ";
  1482. msg += this->GetCurrentDirectory();
  1483. cmSystemTools::Message(msg.c_str());
  1484. }
  1485. }
  1486. void cmMakefile::AddSubDirectory(const char* sub,
  1487. bool excludeFromAll, bool preorder)
  1488. {
  1489. // the source path must be made full if it isn't already
  1490. std::string srcPath = sub;
  1491. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  1492. {
  1493. srcPath = this->GetCurrentDirectory();
  1494. srcPath += "/";
  1495. srcPath += sub;
  1496. }
  1497. // binary path must be made full if it isn't already
  1498. std::string binPath = sub;
  1499. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  1500. {
  1501. binPath = this->GetCurrentOutputDirectory();
  1502. binPath += "/";
  1503. binPath += sub;
  1504. }
  1505. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  1506. excludeFromAll, preorder, false);
  1507. }
  1508. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  1509. bool excludeFromAll, bool preorder,
  1510. bool immediate)
  1511. {
  1512. // Make sure the binary directory is unique.
  1513. if(!this->EnforceUniqueDir(srcPath, binPath))
  1514. {
  1515. return;
  1516. }
  1517. // create a new local generator and set its parent
  1518. cmLocalGenerator *lg2 =
  1519. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  1520. lg2->SetParent(this->LocalGenerator);
  1521. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  1522. // set the subdirs start dirs
  1523. lg2->GetMakefile()->SetStartDirectory(srcPath);
  1524. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  1525. if(excludeFromAll)
  1526. {
  1527. lg2->GetMakefile()->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1528. }
  1529. lg2->GetMakefile()->SetPreOrder(preorder);
  1530. if (immediate)
  1531. {
  1532. this->ConfigureSubDirectory(lg2);
  1533. }
  1534. }
  1535. //----------------------------------------------------------------------------
  1536. void cmMakefile::AddIncludeDirectories(const std::vector<std::string> &incs,
  1537. bool before)
  1538. {
  1539. if (incs.empty())
  1540. {
  1541. return;
  1542. }
  1543. std::string incString;
  1544. std::string sep;
  1545. for(std::vector<std::string>::const_iterator li = incs.begin();
  1546. li != incs.end(); ++li)
  1547. {
  1548. incString += sep + *li;
  1549. sep = ";";
  1550. }
  1551. std::vector<cmValueWithOrigin>::iterator position =
  1552. before ? this->IncludeDirectoriesEntries.begin()
  1553. : this->IncludeDirectoriesEntries.end();
  1554. cmListFileBacktrace lfbt;
  1555. this->GetBacktrace(lfbt);
  1556. cmValueWithOrigin entry(incString, lfbt);
  1557. this->IncludeDirectoriesEntries.insert(position, entry);
  1558. // Property on each target:
  1559. for (cmTargets::iterator l = this->Targets.begin();
  1560. l != this->Targets.end(); ++l)
  1561. {
  1562. cmTarget &t = l->second;
  1563. t.InsertInclude(entry, before);
  1564. }
  1565. }
  1566. //----------------------------------------------------------------------------
  1567. void
  1568. cmMakefile::AddSystemIncludeDirectories(const std::set<cmStdString> &incs)
  1569. {
  1570. for(std::set<cmStdString>::const_iterator li = incs.begin();
  1571. li != incs.end(); ++li)
  1572. {
  1573. this->SystemIncludeDirectories.insert(*li);
  1574. }
  1575. for (cmTargets::iterator l = this->Targets.begin();
  1576. l != this->Targets.end(); ++l)
  1577. {
  1578. cmTarget &t = l->second;
  1579. t.AddSystemIncludeDirectories(incs);
  1580. }
  1581. }
  1582. void cmMakefile::AddDefinition(const char* name, const char* value)
  1583. {
  1584. if (!value )
  1585. {
  1586. return;
  1587. }
  1588. #ifdef CMAKE_STRICT
  1589. if (this->GetCMakeInstance())
  1590. {
  1591. this->GetCMakeInstance()->
  1592. RecordPropertyAccess(name,cmProperty::VARIABLE);
  1593. }
  1594. #endif
  1595. this->Internal->VarStack.top().Set(name, value);
  1596. if (this->Internal->VarUsageStack.size() &&
  1597. this->VariableInitialized(name))
  1598. {
  1599. this->CheckForUnused("changing definition", name);
  1600. this->Internal->VarUsageStack.top().erase(name);
  1601. }
  1602. this->Internal->VarInitStack.top().insert(name);
  1603. #ifdef CMAKE_BUILD_WITH_CMAKE
  1604. cmVariableWatch* vv = this->GetVariableWatch();
  1605. if ( vv )
  1606. {
  1607. vv->VariableAccessed(name,
  1608. cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1609. value,
  1610. this);
  1611. }
  1612. #endif
  1613. }
  1614. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  1615. const char* doc,
  1616. cmCacheManager::CacheEntryType type,
  1617. bool force)
  1618. {
  1619. const char* val = value;
  1620. cmCacheManager::CacheIterator it =
  1621. this->GetCacheManager()->GetCacheIterator(name);
  1622. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1623. it.Initialized())
  1624. {
  1625. // if this is not a force, then use the value from the cache
  1626. // if it is a force, then use the value being passed in
  1627. if(!force)
  1628. {
  1629. val = it.GetValue();
  1630. }
  1631. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  1632. {
  1633. std::vector<std::string>::size_type cc;
  1634. std::vector<std::string> files;
  1635. std::string nvalue = "";
  1636. cmSystemTools::ExpandListArgument(val, files);
  1637. for ( cc = 0; cc < files.size(); cc ++ )
  1638. {
  1639. if(!cmSystemTools::IsOff(files[cc].c_str()))
  1640. {
  1641. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  1642. }
  1643. if ( cc > 0 )
  1644. {
  1645. nvalue += ";";
  1646. }
  1647. nvalue += files[cc];
  1648. }
  1649. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1650. val = it.GetValue();
  1651. }
  1652. }
  1653. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  1654. // if there was a definition then remove it
  1655. this->Internal->VarStack.top().Set(name, 0);
  1656. }
  1657. void cmMakefile::AddDefinition(const char* name, bool value)
  1658. {
  1659. this->Internal->VarStack.top().Set(name, value? "ON" : "OFF");
  1660. if (this->Internal->VarUsageStack.size() &&
  1661. this->VariableInitialized(name))
  1662. {
  1663. this->CheckForUnused("changing definition", name);
  1664. this->Internal->VarUsageStack.top().erase(name);
  1665. }
  1666. this->Internal->VarInitStack.top().insert(name);
  1667. #ifdef CMAKE_BUILD_WITH_CMAKE
  1668. cmVariableWatch* vv = this->GetVariableWatch();
  1669. if ( vv )
  1670. {
  1671. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1672. value?"ON":"OFF", this);
  1673. }
  1674. #endif
  1675. }
  1676. void cmMakefile::CheckForUnusedVariables() const
  1677. {
  1678. if (!this->WarnUnused)
  1679. {
  1680. return;
  1681. }
  1682. const cmDefinitions& defs = this->Internal->VarStack.top();
  1683. const std::set<cmStdString>& locals = defs.LocalKeys();
  1684. std::set<cmStdString>::const_iterator it = locals.begin();
  1685. for (; it != locals.end(); ++it)
  1686. {
  1687. this->CheckForUnused("out of scope", it->c_str());
  1688. }
  1689. }
  1690. void cmMakefile::MarkVariableAsUsed(const char* var)
  1691. {
  1692. this->Internal->VarUsageStack.top().insert(var);
  1693. }
  1694. bool cmMakefile::VariableInitialized(const char* var) const
  1695. {
  1696. if(this->Internal->VarInitStack.top().find(var) !=
  1697. this->Internal->VarInitStack.top().end())
  1698. {
  1699. return true;
  1700. }
  1701. return false;
  1702. }
  1703. bool cmMakefile::VariableUsed(const char* var) const
  1704. {
  1705. if(this->Internal->VarUsageStack.top().find(var) !=
  1706. this->Internal->VarUsageStack.top().end())
  1707. {
  1708. return true;
  1709. }
  1710. return false;
  1711. }
  1712. void cmMakefile::CheckForUnused(const char* reason, const char* name) const
  1713. {
  1714. if (this->WarnUnused && !this->VariableUsed(name))
  1715. {
  1716. cmStdString path;
  1717. cmListFileBacktrace bt;
  1718. if (this->CallStack.size())
  1719. {
  1720. const cmListFileContext* file = this->CallStack.back().Context;
  1721. bt.push_back(*file);
  1722. path = file->FilePath.c_str();
  1723. }
  1724. else
  1725. {
  1726. path = this->GetStartDirectory();
  1727. path += "/CMakeLists.txt";
  1728. cmListFileContext lfc;
  1729. lfc.FilePath = path;
  1730. lfc.Line = 0;
  1731. bt.push_back(lfc);
  1732. }
  1733. if (this->CheckSystemVars ||
  1734. cmSystemTools::IsSubDirectory(path.c_str(),
  1735. this->GetHomeDirectory()) ||
  1736. (cmSystemTools::IsSubDirectory(path.c_str(),
  1737. this->GetHomeOutputDirectory()) &&
  1738. !cmSystemTools::IsSubDirectory(path.c_str(),
  1739. cmake::GetCMakeFilesDirectory())))
  1740. {
  1741. cmOStringStream msg;
  1742. msg << "unused variable (" << reason << ") \'" << name << "\'";
  1743. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
  1744. msg.str().c_str(),
  1745. bt);
  1746. }
  1747. }
  1748. }
  1749. void cmMakefile::RemoveDefinition(const char* name)
  1750. {
  1751. this->Internal->VarStack.top().Set(name, 0);
  1752. if (this->Internal->VarUsageStack.size() &&
  1753. this->VariableInitialized(name))
  1754. {
  1755. this->CheckForUnused("unsetting", name);
  1756. this->Internal->VarUsageStack.top().erase(name);
  1757. }
  1758. this->Internal->VarInitStack.top().insert(name);
  1759. #ifdef CMAKE_BUILD_WITH_CMAKE
  1760. cmVariableWatch* vv = this->GetVariableWatch();
  1761. if ( vv )
  1762. {
  1763. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS,
  1764. 0, this);
  1765. }
  1766. #endif
  1767. }
  1768. void cmMakefile::RemoveCacheDefinition(const char* name)
  1769. {
  1770. this->GetCacheManager()->RemoveCacheEntry(name);
  1771. }
  1772. void cmMakefile::SetProjectName(const char* p)
  1773. {
  1774. this->ProjectName = p;
  1775. }
  1776. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  1777. {
  1778. // for these targets do not add anything
  1779. switch(target.GetType())
  1780. {
  1781. case cmTarget::UTILITY:
  1782. case cmTarget::GLOBAL_TARGET:
  1783. return;
  1784. default:;
  1785. }
  1786. std::vector<std::string>::iterator j;
  1787. for(j = this->LinkDirectories.begin();
  1788. j != this->LinkDirectories.end(); ++j)
  1789. {
  1790. target.AddLinkDirectory(j->c_str());
  1791. }
  1792. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1793. }
  1794. void cmMakefile::AddAlias(const char* lname, cmTarget *tgt)
  1795. {
  1796. this->AliasTargets[lname] = tgt;
  1797. this->LocalGenerator->GetGlobalGenerator()->AddAlias(lname, tgt);
  1798. }
  1799. cmTarget* cmMakefile::AddLibrary(const char* lname, cmTarget::TargetType type,
  1800. const std::vector<std::string> &srcs,
  1801. bool excludeFromAll)
  1802. {
  1803. // wrong type ? default to STATIC
  1804. if ( (type != cmTarget::STATIC_LIBRARY)
  1805. && (type != cmTarget::SHARED_LIBRARY)
  1806. && (type != cmTarget::MODULE_LIBRARY)
  1807. && (type != cmTarget::OBJECT_LIBRARY))
  1808. {
  1809. this->IssueMessage(cmake::INTERNAL_ERROR,
  1810. "cmMakefile::AddLibrary given invalid target type.");
  1811. type = cmTarget::STATIC_LIBRARY;
  1812. }
  1813. cmTarget* target = this->AddNewTarget(type, lname);
  1814. // Clear its dependencies. Otherwise, dependencies might persist
  1815. // over changes in CMakeLists.txt, making the information stale and
  1816. // hence useless.
  1817. target->ClearDependencyInformation( *this, lname );
  1818. if(excludeFromAll)
  1819. {
  1820. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1821. }
  1822. target->AddSources(srcs);
  1823. this->AddGlobalLinkInformation(lname, *target);
  1824. return target;
  1825. }
  1826. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1827. const std::vector<std::string> &srcs,
  1828. bool excludeFromAll)
  1829. {
  1830. cmTarget* target = this->AddNewTarget(cmTarget::EXECUTABLE, exeName);
  1831. if(excludeFromAll)
  1832. {
  1833. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1834. }
  1835. target->AddSources(srcs);
  1836. this->AddGlobalLinkInformation(exeName, *target);
  1837. return target;
  1838. }
  1839. //----------------------------------------------------------------------------
  1840. cmTarget*
  1841. cmMakefile::AddNewTarget(cmTarget::TargetType type, const char* name)
  1842. {
  1843. cmTargets::iterator it =
  1844. this->Targets.insert(cmTargets::value_type(name, cmTarget())).first;
  1845. cmTarget& target = it->second;
  1846. target.SetType(type, name);
  1847. target.SetMakefile(this);
  1848. this->LocalGenerator->GetGlobalGenerator()->AddTarget(&it->second);
  1849. return &it->second;
  1850. }
  1851. cmSourceFile *cmMakefile::LinearGetSourceFileWithOutput(const char *cname)
  1852. {
  1853. std::string name = cname;
  1854. std::string out;
  1855. // look through all the source files that have custom commands
  1856. // and see if the custom command has the passed source file as an output
  1857. for(std::vector<cmSourceFile*>::const_iterator i =
  1858. this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
  1859. {
  1860. // does this source file have a custom command?
  1861. if ((*i)->GetCustomCommand())
  1862. {
  1863. // is the output of the custom command match the source files name
  1864. const std::vector<std::string>& outputs =
  1865. (*i)->GetCustomCommand()->GetOutputs();
  1866. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1867. o != outputs.end(); ++o)
  1868. {
  1869. out = *o;
  1870. std::string::size_type pos = out.rfind(name);
  1871. // If the output matches exactly
  1872. if (pos != out.npos &&
  1873. pos == out.size() - name.size() &&
  1874. (pos ==0 || out[pos-1] == '/'))
  1875. {
  1876. return *i;
  1877. }
  1878. }
  1879. }
  1880. }
  1881. // otherwise return NULL
  1882. return 0;
  1883. }
  1884. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1885. {
  1886. std::string name = cname;
  1887. // If the queried path is not absolute we use the backward compatible
  1888. // linear-time search for an output with a matching suffix.
  1889. if(!cmSystemTools::FileIsFullPath(cname))
  1890. {
  1891. return LinearGetSourceFileWithOutput(cname);
  1892. }
  1893. // Otherwise we use an efficient lookup map.
  1894. OutputToSourceMap::iterator o = this->OutputToSource.find(name);
  1895. if (o != this->OutputToSource.end())
  1896. {
  1897. return (*o).second;
  1898. }
  1899. return 0;
  1900. }
  1901. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1902. cmSourceGroup* cmMakefile::GetSourceGroup(const std::vector<std::string>&name)
  1903. {
  1904. cmSourceGroup* sg = 0;
  1905. // first look for source group starting with the same as the one we wants
  1906. for (std::vector<cmSourceGroup>::iterator sgIt = this->SourceGroups.begin();
  1907. sgIt != this->SourceGroups.end(); ++sgIt)
  1908. {
  1909. std::string sgName = sgIt->GetName();
  1910. if(sgName == name[0])
  1911. {
  1912. sg = &(*sgIt);
  1913. break;
  1914. }
  1915. }
  1916. if(sg != 0)
  1917. {
  1918. // iterate through its children to find match source group
  1919. for(unsigned int i=1; i<name.size(); ++i)
  1920. {
  1921. sg = sg->lookupChild(name[i].c_str());
  1922. if(sg == 0)
  1923. {
  1924. break;
  1925. }
  1926. }
  1927. }
  1928. return sg;
  1929. }
  1930. void cmMakefile::AddSourceGroup(const char* name,
  1931. const char* regex)
  1932. {
  1933. if (name)
  1934. {
  1935. std::vector<std::string> nameVector;
  1936. nameVector.push_back(name);
  1937. AddSourceGroup(nameVector, regex);
  1938. }
  1939. }
  1940. void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
  1941. const char* regex)
  1942. {
  1943. cmSourceGroup* sg = 0;
  1944. std::vector<std::string> currentName;
  1945. int i = 0;
  1946. const int lastElement = static_cast<int>(name.size()-1);
  1947. for(i=lastElement; i>=0; --i)
  1948. {
  1949. currentName.assign(name.begin(), name.begin()+i+1);
  1950. sg = this->GetSourceGroup(currentName);
  1951. if(sg != 0)
  1952. {
  1953. break;
  1954. }
  1955. }
  1956. // i now contains the index of the last found component
  1957. if(i==lastElement)
  1958. {
  1959. // group already exists, replace its regular expression
  1960. if ( regex )
  1961. {
  1962. // We only want to set the regular expression. If there are already
  1963. // source files in the group, we don't want to remove them.
  1964. sg->SetGroupRegex(regex);
  1965. }
  1966. return;
  1967. }
  1968. else if(i==-1)
  1969. {
  1970. // group does not exist nor belong to any existing group
  1971. // add its first component
  1972. this->SourceGroups.push_back(cmSourceGroup(name[0].c_str(), regex));
  1973. sg = this->GetSourceGroup(currentName);
  1974. i = 0; // last component found
  1975. }
  1976. // build the whole source group path
  1977. const char* fullname = sg->GetFullName();
  1978. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  1979. if(strlen(fullname))
  1980. {
  1981. std::string guidName = "SG_Filter_";
  1982. guidName += fullname;
  1983. gg->CreateGUID(guidName.c_str());
  1984. }
  1985. for(++i; i<=lastElement; ++i)
  1986. {
  1987. sg->AddChild(cmSourceGroup(name[i].c_str(), 0, sg->GetFullName()));
  1988. sg = sg->lookupChild(name[i].c_str());
  1989. fullname = sg->GetFullName();
  1990. if(strlen(fullname))
  1991. {
  1992. std::string guidName = "SG_Filter_";
  1993. guidName += fullname;
  1994. gg->CreateGUID(guidName.c_str());
  1995. }
  1996. }
  1997. sg->SetGroupRegex(regex);
  1998. }
  1999. #endif
  2000. void cmMakefile::AddExtraDirectory(const char* dir)
  2001. {
  2002. this->AuxSourceDirectories.push_back(dir);
  2003. }
  2004. static bool mightExpandVariablesCMP0019(const char* s)
  2005. {
  2006. return s && *s && strstr(s,"${") && strchr(s,'}');
  2007. }
  2008. void cmMakefile::ExpandVariablesCMP0019()
  2009. {
  2010. // Drop this ancient compatibility behavior with a policy.
  2011. cmPolicies::PolicyStatus pol = this->GetPolicyStatus(cmPolicies::CMP0019);
  2012. if(pol != cmPolicies::OLD && pol != cmPolicies::WARN)
  2013. {
  2014. return;
  2015. }
  2016. cmOStringStream w;
  2017. const char *includeDirs = this->GetProperty("INCLUDE_DIRECTORIES");
  2018. if(mightExpandVariablesCMP0019(includeDirs))
  2019. {
  2020. std::string dirs = includeDirs;
  2021. this->ExpandVariablesInString(dirs, true, true);
  2022. if(pol == cmPolicies::WARN && dirs != includeDirs)
  2023. {
  2024. w << "Evaluated directory INCLUDE_DIRECTORIES\n"
  2025. << " " << includeDirs << "\n"
  2026. << "as\n"
  2027. << " " << dirs << "\n";
  2028. }
  2029. this->SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  2030. }
  2031. // Also for each target's INCLUDE_DIRECTORIES property:
  2032. for (cmTargets::iterator l = this->Targets.begin();
  2033. l != this->Targets.end(); ++l)
  2034. {
  2035. cmTarget &t = l->second;
  2036. includeDirs = t.GetProperty("INCLUDE_DIRECTORIES");
  2037. if(mightExpandVariablesCMP0019(includeDirs))
  2038. {
  2039. std::string dirs = includeDirs;
  2040. this->ExpandVariablesInString(dirs, true, true);
  2041. if(pol == cmPolicies::WARN && dirs != includeDirs)
  2042. {
  2043. w << "Evaluated target " << t.GetName() << " INCLUDE_DIRECTORIES\n"
  2044. << " " << includeDirs << "\n"
  2045. << "as\n"
  2046. << " " << dirs << "\n";
  2047. }
  2048. t.SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  2049. }
  2050. }
  2051. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  2052. d != this->LinkDirectories.end(); ++d)
  2053. {
  2054. if(mightExpandVariablesCMP0019(d->c_str()))
  2055. {
  2056. std::string orig = *d;
  2057. this->ExpandVariablesInString(*d, true, true);
  2058. if(pol == cmPolicies::WARN && *d != orig)
  2059. {
  2060. w << "Evaluated link directory\n"
  2061. << " " << orig << "\n"
  2062. << "as\n"
  2063. << " " << *d << "\n";
  2064. }
  2065. }
  2066. }
  2067. for(cmTarget::LinkLibraryVectorType::iterator l =
  2068. this->LinkLibraries.begin();
  2069. l != this->LinkLibraries.end(); ++l)
  2070. {
  2071. if(mightExpandVariablesCMP0019(l->first.c_str()))
  2072. {
  2073. std::string orig = l->first;
  2074. this->ExpandVariablesInString(l->first, true, true);
  2075. if(pol == cmPolicies::WARN && l->first != orig)
  2076. {
  2077. w << "Evaluated link library\n"
  2078. << " " << orig << "\n"
  2079. << "as\n"
  2080. << " " << l->first << "\n";
  2081. }
  2082. }
  2083. }
  2084. if(!w.str().empty())
  2085. {
  2086. cmOStringStream m;
  2087. m << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0019)
  2088. << "\n"
  2089. << "The following variable evaluations were encountered:\n"
  2090. << w.str();
  2091. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  2092. }
  2093. }
  2094. bool cmMakefile::IsOn(const char* name) const
  2095. {
  2096. const char* value = this->GetDefinition(name);
  2097. return cmSystemTools::IsOn(value);
  2098. }
  2099. bool cmMakefile::IsSet(const char* name) const
  2100. {
  2101. const char* value = this->GetDefinition(name);
  2102. if ( !value )
  2103. {
  2104. return false;
  2105. }
  2106. if ( ! *value )
  2107. {
  2108. return false;
  2109. }
  2110. if ( cmSystemTools::IsNOTFOUND(value) )
  2111. {
  2112. return false;
  2113. }
  2114. return true;
  2115. }
  2116. bool cmMakefile::PlatformIs64Bit() const
  2117. {
  2118. if(const char* sizeof_dptr = this->GetDefinition("CMAKE_SIZEOF_VOID_P"))
  2119. {
  2120. return atoi(sizeof_dptr) == 8;
  2121. }
  2122. return false;
  2123. }
  2124. const char* cmMakefile::GetSONameFlag(const char* language) const
  2125. {
  2126. std::string name = "CMAKE_SHARED_LIBRARY_SONAME";
  2127. if(language)
  2128. {
  2129. name += "_";
  2130. name += language;
  2131. }
  2132. name += "_FLAG";
  2133. return GetDefinition(name.c_str());
  2134. }
  2135. bool cmMakefile::CanIWriteThisFile(const char* fileName)
  2136. {
  2137. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  2138. {
  2139. return true;
  2140. }
  2141. // If we are doing an in-source build, then the test will always fail
  2142. if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
  2143. this->GetHomeOutputDirectory()) )
  2144. {
  2145. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  2146. {
  2147. return false;
  2148. }
  2149. return true;
  2150. }
  2151. // Check if this is a subdirectory of the source tree but not a
  2152. // subdirectory of the build tree
  2153. if ( cmSystemTools::IsSubDirectory(fileName,
  2154. this->GetHomeDirectory()) &&
  2155. !cmSystemTools::IsSubDirectory(fileName,
  2156. this->GetHomeOutputDirectory()) )
  2157. {
  2158. return false;
  2159. }
  2160. return true;
  2161. }
  2162. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  2163. {
  2164. const char* ret = this->GetDefinition(name);
  2165. if(!ret)
  2166. {
  2167. cmSystemTools::Error("Error required internal CMake variable not "
  2168. "set, cmake may be not be built correctly.\n",
  2169. "Missing variable is:\n",
  2170. name);
  2171. return "";
  2172. }
  2173. return ret;
  2174. }
  2175. bool cmMakefile::IsDefinitionSet(const char* name) const
  2176. {
  2177. const char* def = this->Internal->VarStack.top().Get(name);
  2178. this->Internal->VarUsageStack.top().insert(name);
  2179. if(!def)
  2180. {
  2181. def = this->GetCacheManager()->GetCacheValue(name);
  2182. }
  2183. #ifdef CMAKE_BUILD_WITH_CMAKE
  2184. if(cmVariableWatch* vv = this->GetVariableWatch())
  2185. {
  2186. if(!def)
  2187. {
  2188. vv->VariableAccessed
  2189. (name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS,
  2190. def, this);
  2191. }
  2192. }
  2193. #endif
  2194. return def?true:false;
  2195. }
  2196. const char* cmMakefile::GetDefinition(const char* name) const
  2197. {
  2198. #ifdef CMAKE_STRICT
  2199. if (this->GetCMakeInstance())
  2200. {
  2201. this->GetCMakeInstance()->
  2202. RecordPropertyAccess(name,cmProperty::VARIABLE);
  2203. }
  2204. #endif
  2205. if (this->WarnUnused)
  2206. {
  2207. this->Internal->VarUsageStack.top().insert(name);
  2208. }
  2209. const char* def = this->Internal->VarStack.top().Get(name);
  2210. if(!def)
  2211. {
  2212. def = this->GetCacheManager()->GetCacheValue(name);
  2213. }
  2214. #ifdef CMAKE_BUILD_WITH_CMAKE
  2215. cmVariableWatch* vv = this->GetVariableWatch();
  2216. if ( vv )
  2217. {
  2218. if ( def )
  2219. {
  2220. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS,
  2221. def, this);
  2222. }
  2223. else
  2224. {
  2225. // are unknown access allowed
  2226. const char* allow = this->Internal->VarStack.top()
  2227. .Get("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  2228. if(cmSystemTools::IsOn(allow))
  2229. {
  2230. vv->VariableAccessed(name,
  2231. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2232. }
  2233. else
  2234. {
  2235. vv->VariableAccessed(name,
  2236. cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2237. }
  2238. }
  2239. }
  2240. #endif
  2241. return def;
  2242. }
  2243. const char* cmMakefile::GetSafeDefinition(const char* def) const
  2244. {
  2245. const char* ret = this->GetDefinition(def);
  2246. if(!ret)
  2247. {
  2248. return "";
  2249. }
  2250. return ret;
  2251. }
  2252. std::vector<std::string> cmMakefile
  2253. ::GetDefinitions(int cacheonly /* = 0 */) const
  2254. {
  2255. std::set<cmStdString> definitions;
  2256. if ( !cacheonly )
  2257. {
  2258. definitions = this->Internal->VarStack.top().ClosureKeys();
  2259. }
  2260. cmCacheManager::CacheIterator cit =
  2261. this->GetCacheManager()->GetCacheIterator();
  2262. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2263. {
  2264. definitions.insert(cit.GetName());
  2265. }
  2266. std::vector<std::string> res;
  2267. std::set<cmStdString>::iterator fit;
  2268. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  2269. {
  2270. res.push_back(*fit);
  2271. }
  2272. return res;
  2273. }
  2274. const char *cmMakefile::ExpandVariablesInString(std::string& source)
  2275. {
  2276. return this->ExpandVariablesInString(source, false, false);
  2277. }
  2278. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  2279. bool escapeQuotes,
  2280. bool noEscapes,
  2281. bool atOnly,
  2282. const char* filename,
  2283. long line,
  2284. bool removeEmpty,
  2285. bool replaceAt)
  2286. {
  2287. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  2288. {
  2289. return source.c_str();
  2290. }
  2291. // Special-case the @ONLY mode.
  2292. if(atOnly)
  2293. {
  2294. if(!noEscapes || !removeEmpty || !replaceAt)
  2295. {
  2296. // This case should never be called. At-only is for
  2297. // configure-file/string which always does no escapes.
  2298. this->IssueMessage(cmake::INTERNAL_ERROR,
  2299. "ExpandVariablesInString @ONLY called "
  2300. "on something with escapes.");
  2301. }
  2302. // Store an original copy of the input.
  2303. std::string input = source;
  2304. // Start with empty output.
  2305. source = "";
  2306. // Look for one @VAR@ at a time.
  2307. const char* in = input.c_str();
  2308. while(this->cmAtVarRegex.find(in))
  2309. {
  2310. // Get the range of the string to replace.
  2311. const char* first = in + this->cmAtVarRegex.start();
  2312. const char* last = in + this->cmAtVarRegex.end();
  2313. // Store the unchanged part of the string now.
  2314. source.append(in, first-in);
  2315. // Lookup the definition of VAR.
  2316. std::string var(first+1, last-first-2);
  2317. if(const char* val = this->GetDefinition(var.c_str()))
  2318. {
  2319. // Store the value in the output escaping as requested.
  2320. if(escapeQuotes)
  2321. {
  2322. source.append(cmSystemTools::EscapeQuotes(val));
  2323. }
  2324. else
  2325. {
  2326. source.append(val);
  2327. }
  2328. }
  2329. // Continue looking for @VAR@ further along the string.
  2330. in = last;
  2331. }
  2332. // Append the rest of the unchanged part of the string.
  2333. source.append(in);
  2334. return source.c_str();
  2335. }
  2336. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2337. // with GetDefinition(), if not found in the map, nothing is expanded.
  2338. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2339. // the current environment variables.
  2340. cmCommandArgumentParserHelper parser;
  2341. parser.SetMakefile(this);
  2342. parser.SetLineFile(line, filename);
  2343. parser.SetEscapeQuotes(escapeQuotes);
  2344. parser.SetNoEscapeMode(noEscapes);
  2345. parser.SetReplaceAtSyntax(replaceAt);
  2346. parser.SetRemoveEmpty(removeEmpty);
  2347. int res = parser.ParseString(source.c_str(), 0);
  2348. const char* emsg = parser.GetError();
  2349. if ( res && !emsg[0] )
  2350. {
  2351. source = parser.GetResult();
  2352. }
  2353. else
  2354. {
  2355. // Construct the main error message.
  2356. cmOStringStream error;
  2357. error << "Syntax error in cmake code ";
  2358. if(filename && line > 0)
  2359. {
  2360. // This filename and line number may be more specific than the
  2361. // command context because one command invocation can have
  2362. // arguments on multiple lines.
  2363. error << "at\n"
  2364. << " " << filename << ":" << line << "\n";
  2365. }
  2366. error << "when parsing string\n"
  2367. << " " << source.c_str() << "\n";
  2368. error << emsg;
  2369. // If the parser failed ("res" is false) then this is a real
  2370. // argument parsing error, so the policy applies. Otherwise the
  2371. // parser reported an error message without failing because the
  2372. // helper implementation is unhappy, which has always reported an
  2373. // error.
  2374. cmake::MessageType mtype = cmake::FATAL_ERROR;
  2375. if(!res)
  2376. {
  2377. // This is a real argument parsing error. Use policy CMP0010 to
  2378. // decide whether it is an error.
  2379. switch(this->GetPolicyStatus(cmPolicies::CMP0010))
  2380. {
  2381. case cmPolicies::WARN:
  2382. error << "\n"
  2383. << (this->GetPolicies()
  2384. ->GetPolicyWarning(cmPolicies::CMP0010));
  2385. case cmPolicies::OLD:
  2386. // OLD behavior is to just warn and continue.
  2387. mtype = cmake::AUTHOR_WARNING;
  2388. break;
  2389. case cmPolicies::REQUIRED_IF_USED:
  2390. case cmPolicies::REQUIRED_ALWAYS:
  2391. error << "\n"
  2392. << (this->GetPolicies()
  2393. ->GetRequiredPolicyError(cmPolicies::CMP0010));
  2394. case cmPolicies::NEW:
  2395. // NEW behavior is to report the error.
  2396. cmSystemTools::SetFatalErrorOccured();
  2397. break;
  2398. }
  2399. }
  2400. this->IssueMessage(mtype, error.str());
  2401. }
  2402. return source.c_str();
  2403. }
  2404. void cmMakefile::RemoveVariablesInString(std::string& source,
  2405. bool atOnly) const
  2406. {
  2407. if(!atOnly)
  2408. {
  2409. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  2410. while (var.find(source))
  2411. {
  2412. source.erase(var.start(),var.end() - var.start());
  2413. }
  2414. }
  2415. if(!atOnly)
  2416. {
  2417. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  2418. while (varb.find(source))
  2419. {
  2420. source.erase(varb.start(),varb.end() - varb.start());
  2421. }
  2422. }
  2423. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  2424. while (var2.find(source))
  2425. {
  2426. source.erase(var2.start(),var2.end() - var2.start());
  2427. }
  2428. }
  2429. /**
  2430. * Add the default definitions to the makefile. These values must not
  2431. * be dependent on anything that isn't known when this cmMakefile instance
  2432. * is constructed.
  2433. */
  2434. void cmMakefile::AddDefaultDefinitions()
  2435. {
  2436. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  2437. With CMake must separate between target and host platform. In most cases
  2438. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  2439. additional set of variables for the host system is required ->
  2440. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  2441. WIN32, UNIX and APPLE are now set in the platform files in
  2442. Modules/Platforms/.
  2443. To keep cmake scripts (-P) and custom language and compiler modules
  2444. working, these variables are still also set here in this place, but they
  2445. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  2446. files are executed. */
  2447. #if defined(_WIN32)
  2448. this->AddDefinition("WIN32", "1");
  2449. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  2450. #else
  2451. this->AddDefinition("UNIX", "1");
  2452. this->AddDefinition("CMAKE_HOST_UNIX", "1");
  2453. #endif
  2454. #if defined(__CYGWIN__)
  2455. if(cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32")))
  2456. {
  2457. this->AddDefinition("WIN32", "1");
  2458. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  2459. }
  2460. #endif
  2461. #if defined(__APPLE__)
  2462. this->AddDefinition("APPLE", "1");
  2463. this->AddDefinition("CMAKE_HOST_APPLE", "1");
  2464. #endif
  2465. char temp[1024];
  2466. sprintf(temp, "%d", cmVersion::GetMinorVersion());
  2467. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  2468. sprintf(temp, "%d", cmVersion::GetMajorVersion());
  2469. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  2470. sprintf(temp, "%d", cmVersion::GetPatchVersion());
  2471. this->AddDefinition("CMAKE_PATCH_VERSION", temp);
  2472. sprintf(temp, "%d", cmVersion::GetTweakVersion());
  2473. this->AddDefinition("CMAKE_TWEAK_VERSION", temp);
  2474. this->AddDefinition("CMAKE_VERSION", cmVersion::GetCMakeVersion());
  2475. this->AddDefinition("CMAKE_FILES_DIRECTORY",
  2476. cmake::GetCMakeFilesDirectory());
  2477. }
  2478. //----------------------------------------------------------------------------
  2479. const char*
  2480. cmMakefile::GetConfigurations(std::vector<std::string>& configs,
  2481. bool single) const
  2482. {
  2483. if(this->LocalGenerator->GetGlobalGenerator()->IsMultiConfig())
  2484. {
  2485. if(const char* configTypes =
  2486. this->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  2487. {
  2488. cmSystemTools::ExpandListArgument(configTypes, configs);
  2489. }
  2490. return 0;
  2491. }
  2492. else
  2493. {
  2494. const char* buildType = this->GetDefinition("CMAKE_BUILD_TYPE");
  2495. if(single && buildType && *buildType)
  2496. {
  2497. configs.push_back(buildType);
  2498. }
  2499. return buildType;
  2500. }
  2501. }
  2502. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2503. /**
  2504. * Find a source group whose regular expression matches the filename
  2505. * part of the given source name. Search backward through the list of
  2506. * source groups, and take the first matching group found. This way
  2507. * non-inherited SOURCE_GROUP commands will have precedence over
  2508. * inherited ones.
  2509. */
  2510. cmSourceGroup&
  2511. cmMakefile::FindSourceGroup(const char* source,
  2512. std::vector<cmSourceGroup> &groups)
  2513. {
  2514. // First search for a group that lists the file explicitly.
  2515. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2516. sg != groups.rend(); ++sg)
  2517. {
  2518. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  2519. if(result)
  2520. {
  2521. return *result;
  2522. }
  2523. }
  2524. // Now search for a group whose regex matches the file.
  2525. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2526. sg != groups.rend(); ++sg)
  2527. {
  2528. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  2529. if(result)
  2530. {
  2531. return *result;
  2532. }
  2533. }
  2534. // Shouldn't get here, but just in case, return the default group.
  2535. return groups.front();
  2536. }
  2537. #endif
  2538. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff,
  2539. cmExecutionStatus &status)
  2540. {
  2541. // if there are no blockers get out of here
  2542. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  2543. {
  2544. return false;
  2545. }
  2546. // loop over all function blockers to see if any block this command
  2547. // evaluate in reverse, this is critical for balanced IF statements etc
  2548. std::vector<cmFunctionBlocker*>::reverse_iterator pos;
  2549. for (pos = this->FunctionBlockers.rbegin();
  2550. pos != this->FunctionBlockers.rend(); ++pos)
  2551. {
  2552. if((*pos)->IsFunctionBlocked(lff, *this, status))
  2553. {
  2554. return true;
  2555. }
  2556. }
  2557. return false;
  2558. }
  2559. //----------------------------------------------------------------------------
  2560. void cmMakefile::PushFunctionBlockerBarrier()
  2561. {
  2562. this->FunctionBlockerBarriers.push_back(this->FunctionBlockers.size());
  2563. }
  2564. //----------------------------------------------------------------------------
  2565. void cmMakefile::PopFunctionBlockerBarrier(bool reportError)
  2566. {
  2567. // Remove any extra entries pushed on the barrier.
  2568. FunctionBlockersType::size_type barrier =
  2569. this->FunctionBlockerBarriers.back();
  2570. while(this->FunctionBlockers.size() > barrier)
  2571. {
  2572. cmsys::auto_ptr<cmFunctionBlocker> fb(this->FunctionBlockers.back());
  2573. this->FunctionBlockers.pop_back();
  2574. if(reportError)
  2575. {
  2576. // Report the context in which the unclosed block was opened.
  2577. cmListFileContext const& lfc = fb->GetStartingContext();
  2578. cmOStringStream e;
  2579. e << "A logical block opening on the line\n"
  2580. << " " << lfc << "\n"
  2581. << "is not closed.";
  2582. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  2583. reportError = false;
  2584. }
  2585. }
  2586. // Remove the barrier.
  2587. this->FunctionBlockerBarriers.pop_back();
  2588. }
  2589. bool cmMakefile::ExpandArguments(
  2590. std::vector<cmListFileArgument> const& inArgs,
  2591. std::vector<std::string>& outArgs)
  2592. {
  2593. std::vector<cmListFileArgument>::const_iterator i;
  2594. std::string value;
  2595. outArgs.reserve(inArgs.size());
  2596. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  2597. {
  2598. // Expand the variables in the argument.
  2599. value = i->Value;
  2600. this->ExpandVariablesInString(value, false, false, false,
  2601. i->FilePath, i->Line,
  2602. false, true);
  2603. // If the argument is quoted, it should be one argument.
  2604. // Otherwise, it may be a list of arguments.
  2605. if(i->Delim == cmListFileArgument::Quoted)
  2606. {
  2607. outArgs.push_back(value);
  2608. }
  2609. else
  2610. {
  2611. cmSystemTools::ExpandListArgument(value, outArgs);
  2612. }
  2613. }
  2614. return !cmSystemTools::GetFatalErrorOccured();
  2615. }
  2616. //----------------------------------------------------------------------------
  2617. void cmMakefile::AddFunctionBlocker(cmFunctionBlocker* fb)
  2618. {
  2619. if(!this->CallStack.empty())
  2620. {
  2621. // Record the context in which the blocker is created.
  2622. fb->SetStartingContext(*(this->CallStack.back().Context));
  2623. }
  2624. this->FunctionBlockers.push_back(fb);
  2625. }
  2626. cmsys::auto_ptr<cmFunctionBlocker>
  2627. cmMakefile::RemoveFunctionBlocker(cmFunctionBlocker* fb,
  2628. const cmListFileFunction& lff)
  2629. {
  2630. // Find the function blocker stack barrier for the current scope.
  2631. // We only remove a blocker whose index is not less than the barrier.
  2632. FunctionBlockersType::size_type barrier = 0;
  2633. if(!this->FunctionBlockerBarriers.empty())
  2634. {
  2635. barrier = this->FunctionBlockerBarriers.back();
  2636. }
  2637. // Search for the function blocker whose scope this command ends.
  2638. for(FunctionBlockersType::size_type
  2639. i = this->FunctionBlockers.size(); i > barrier; --i)
  2640. {
  2641. std::vector<cmFunctionBlocker*>::iterator pos =
  2642. this->FunctionBlockers.begin() + (i - 1);
  2643. if (*pos == fb)
  2644. {
  2645. // Warn if the arguments do not match, but always remove.
  2646. if(!(*pos)->ShouldRemove(lff, *this))
  2647. {
  2648. cmListFileContext const& lfc = fb->GetStartingContext();
  2649. cmOStringStream e;
  2650. e << "A logical block opening on the line\n"
  2651. << " " << lfc << "\n"
  2652. << "closes on the line\n"
  2653. << " " << lff << "\n"
  2654. << "with mis-matching arguments.";
  2655. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2656. }
  2657. cmFunctionBlocker* b = *pos;
  2658. this->FunctionBlockers.erase(pos);
  2659. return cmsys::auto_ptr<cmFunctionBlocker>(b);
  2660. }
  2661. }
  2662. return cmsys::auto_ptr<cmFunctionBlocker>();
  2663. }
  2664. //----------------------------------------------------------------------------
  2665. cmMakefile::LexicalPushPop::LexicalPushPop(cmMakefile* mf):
  2666. Makefile(mf), ReportError(true)
  2667. {
  2668. this->Makefile->PushFunctionBlockerBarrier();
  2669. }
  2670. //----------------------------------------------------------------------------
  2671. cmMakefile::LexicalPushPop::~LexicalPushPop()
  2672. {
  2673. this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
  2674. }
  2675. void cmMakefile::SetHomeDirectory(const char* dir)
  2676. {
  2677. this->cmHomeDirectory = dir;
  2678. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  2679. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  2680. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  2681. {
  2682. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  2683. }
  2684. }
  2685. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  2686. {
  2687. this->HomeOutputDirectory = lib;
  2688. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  2689. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  2690. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  2691. {
  2692. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
  2693. this->GetHomeOutputDirectory());
  2694. }
  2695. }
  2696. void cmMakefile::SetScriptModeFile(const char* scriptfile)
  2697. {
  2698. this->AddDefinition("CMAKE_SCRIPT_MODE_FILE", scriptfile);
  2699. }
  2700. void cmMakefile::SetArgcArgv(const std::vector<std::string>& args)
  2701. {
  2702. cmOStringStream strStream;
  2703. strStream << args.size();
  2704. this->AddDefinition("CMAKE_ARGC", strStream.str().c_str());
  2705. //this->MarkVariableAsUsed("CMAKE_ARGC");
  2706. for (unsigned int t = 0; t < args.size(); ++t)
  2707. {
  2708. cmOStringStream tmpStream;
  2709. tmpStream << "CMAKE_ARGV" << t;
  2710. this->AddDefinition(tmpStream.str().c_str(), args[t].c_str());
  2711. //this->MarkVariableAsUsed(tmpStream.str().c_str());
  2712. }
  2713. }
  2714. //----------------------------------------------------------------------------
  2715. cmSourceFile* cmMakefile::GetSource(const char* sourceName)
  2716. {
  2717. cmSourceFileLocation sfl(this, sourceName);
  2718. for(std::vector<cmSourceFile*>::const_iterator
  2719. sfi = this->SourceFiles.begin();
  2720. sfi != this->SourceFiles.end(); ++sfi)
  2721. {
  2722. cmSourceFile* sf = *sfi;
  2723. if(sf->Matches(sfl))
  2724. {
  2725. return sf;
  2726. }
  2727. }
  2728. return 0;
  2729. }
  2730. //----------------------------------------------------------------------------
  2731. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  2732. bool generated)
  2733. {
  2734. if(cmSourceFile* esf = this->GetSource(sourceName))
  2735. {
  2736. return esf;
  2737. }
  2738. else
  2739. {
  2740. cmSourceFile* sf = new cmSourceFile(this, sourceName);
  2741. if(generated)
  2742. {
  2743. sf->SetProperty("GENERATED", "1");
  2744. }
  2745. this->SourceFiles.push_back(sf);
  2746. return sf;
  2747. }
  2748. }
  2749. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang,
  2750. bool optional)
  2751. {
  2752. this->AddDefinition("CMAKE_CFG_INTDIR",
  2753. this->LocalGenerator->GetGlobalGenerator()
  2754. ->GetCMakeCFGIntDir());
  2755. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this,
  2756. optional);
  2757. }
  2758. void cmMakefile::ExpandSourceListArguments(
  2759. std::vector<std::string> const& arguments,
  2760. std::vector<std::string>& newargs, unsigned int /* start */)
  2761. {
  2762. // now expand the args
  2763. unsigned int i;
  2764. for(i = 0; i < arguments.size(); ++i)
  2765. {
  2766. // List expansion will have been done already.
  2767. newargs.push_back(arguments[i]);
  2768. }
  2769. }
  2770. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  2771. const char *projectName, const char *targetName,
  2772. bool fast,
  2773. const std::vector<std::string> *cmakeArgs,
  2774. std::string *output)
  2775. {
  2776. this->Internal->IsSourceFileTryCompile = fast;
  2777. // does the binary directory exist ? If not create it...
  2778. if (!cmSystemTools::FileIsDirectory(bindir))
  2779. {
  2780. cmSystemTools::MakeDirectory(bindir);
  2781. }
  2782. // change to the tests directory and run cmake
  2783. // use the cmake object instead of calling cmake
  2784. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2785. cmSystemTools::ChangeDirectory(bindir);
  2786. // make sure the same generator is used
  2787. // use this program as the cmake to be run, it should not
  2788. // be run that way but the cmake object requires a vailid path
  2789. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  2790. cmake cm;
  2791. cm.SetIsInTryCompile(true);
  2792. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  2793. (this->LocalGenerator->GetGlobalGenerator()->GetName());
  2794. if (!gg)
  2795. {
  2796. cmSystemTools::Error(
  2797. "Internal CMake error, TryCompile bad GlobalGenerator");
  2798. // return to the original directory
  2799. cmSystemTools::ChangeDirectory(cwd.c_str());
  2800. this->Internal->IsSourceFileTryCompile = false;
  2801. return 1;
  2802. }
  2803. cm.SetGlobalGenerator(gg);
  2804. // do a configure
  2805. cm.SetHomeDirectory(srcdir);
  2806. cm.SetHomeOutputDirectory(bindir);
  2807. cm.SetStartDirectory(srcdir);
  2808. cm.SetStartOutputDirectory(bindir);
  2809. cm.SetCMakeCommand(cmakeCommand.c_str());
  2810. cm.SetGeneratorToolset(this->GetCMakeInstance()->GetGeneratorToolset());
  2811. cm.LoadCache();
  2812. if(!gg->IsMultiConfig())
  2813. {
  2814. if(const char* config =
  2815. this->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION"))
  2816. {
  2817. // Tell the single-configuration generator which one to use.
  2818. // Add this before the user-provided CMake arguments in case
  2819. // one of the arguments is -DCMAKE_BUILD_TYPE=...
  2820. cm.AddCacheEntry("CMAKE_BUILD_TYPE", config,
  2821. "Build configuration", cmCacheManager::STRING);
  2822. }
  2823. }
  2824. // if cmake args were provided then pass them in
  2825. if (cmakeArgs)
  2826. {
  2827. // FIXME: Workaround to ignore unused CLI variables in try-compile.
  2828. //
  2829. // Ideally we should use SetArgs to honor options like --warn-unused-vars.
  2830. // However, there is a subtle problem when certain arguments are passed to
  2831. // a macro wrapping around try_compile or try_run that does not escape
  2832. // semicolons in its parameters but just passes ${ARGV} or ${ARGN}. In
  2833. // this case a list argument like "-DVAR=a;b" gets split into multiple
  2834. // cmake arguments "-DVAR=a" and "b". Currently SetCacheArgs ignores
  2835. // argument "b" and uses just "-DVAR=a", leading to a subtle bug in that
  2836. // the try_compile or try_run does not get the proper value of VAR. If we
  2837. // call SetArgs here then it would treat "b" as the source directory and
  2838. // cause an error such as "The source directory .../CMakeFiles/CMakeTmp/b
  2839. // does not exist", thus breaking the try_compile or try_run completely.
  2840. //
  2841. // Strictly speaking the bug is in the wrapper macro because the CMake
  2842. // language has always flattened nested lists and the macro should escape
  2843. // the semicolons in its arguments before forwarding them. However, this
  2844. // bug is so subtle that projects typically work anyway, usually because
  2845. // the value VAR=a is sufficient for the try_compile or try_run to get the
  2846. // correct result. Calling SetArgs here would break such projects that
  2847. // previously built. Instead we work around the issue by never reporting
  2848. // unused arguments and ignoring options such as --warn-unused-vars.
  2849. cm.SetWarnUnusedCli(false);
  2850. //cm.SetArgs(*cmakeArgs, true);
  2851. cm.SetCacheArgs(*cmakeArgs);
  2852. }
  2853. // to save time we pass the EnableLanguage info directly
  2854. gg->EnableLanguagesFromGenerator
  2855. (this->LocalGenerator->GetGlobalGenerator(), this);
  2856. if(this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS"))
  2857. {
  2858. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  2859. "TRUE", "", cmCacheManager::INTERNAL);
  2860. }
  2861. else
  2862. {
  2863. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  2864. "FALSE", "", cmCacheManager::INTERNAL);
  2865. }
  2866. if (cm.Configure() != 0)
  2867. {
  2868. cmSystemTools::Error(
  2869. "Internal CMake error, TryCompile configure of cmake failed");
  2870. // return to the original directory
  2871. cmSystemTools::ChangeDirectory(cwd.c_str());
  2872. this->Internal->IsSourceFileTryCompile = false;
  2873. return 1;
  2874. }
  2875. if (cm.Generate() != 0)
  2876. {
  2877. cmSystemTools::Error(
  2878. "Internal CMake error, TryCompile generation of cmake failed");
  2879. // return to the original directory
  2880. cmSystemTools::ChangeDirectory(cwd.c_str());
  2881. this->Internal->IsSourceFileTryCompile = false;
  2882. return 1;
  2883. }
  2884. // finally call the generator to actually build the resulting project
  2885. int ret =
  2886. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2887. projectName,
  2888. targetName,
  2889. fast,
  2890. output,
  2891. this);
  2892. cmSystemTools::ChangeDirectory(cwd.c_str());
  2893. this->Internal->IsSourceFileTryCompile = false;
  2894. return ret;
  2895. }
  2896. bool cmMakefile::GetIsSourceFileTryCompile() const
  2897. {
  2898. return this->Internal->IsSourceFileTryCompile;
  2899. }
  2900. cmake *cmMakefile::GetCMakeInstance() const
  2901. {
  2902. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2903. {
  2904. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2905. }
  2906. return 0;
  2907. }
  2908. #ifdef CMAKE_BUILD_WITH_CMAKE
  2909. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2910. {
  2911. if ( this->GetCMakeInstance() &&
  2912. this->GetCMakeInstance()->GetVariableWatch() )
  2913. {
  2914. return this->GetCMakeInstance()->GetVariableWatch();
  2915. }
  2916. return 0;
  2917. }
  2918. #endif
  2919. void cmMakefile::AddMacro(const char* name, const char* signature)
  2920. {
  2921. if ( !name || !signature )
  2922. {
  2923. return;
  2924. }
  2925. this->MacrosMap[name] = signature;
  2926. }
  2927. void cmMakefile::GetListOfMacros(std::string& macros)
  2928. {
  2929. StringStringMap::iterator it;
  2930. macros = "";
  2931. int cc = 0;
  2932. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2933. {
  2934. if ( cc > 0 )
  2935. {
  2936. macros += ";";
  2937. }
  2938. macros += it->first;
  2939. cc ++;
  2940. }
  2941. }
  2942. cmCacheManager *cmMakefile::GetCacheManager() const
  2943. {
  2944. return this->GetCMakeInstance()->GetCacheManager();
  2945. }
  2946. void cmMakefile::DisplayStatus(const char* message, float s)
  2947. {
  2948. cmake* cm = this->GetLocalGenerator()->GetGlobalGenerator()
  2949. ->GetCMakeInstance();
  2950. if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE)
  2951. {
  2952. // don't output any STATUS message in FIND_PACKAGE_MODE, since they will
  2953. // directly be fed to the compiler, which will be confused.
  2954. return;
  2955. }
  2956. cm->UpdateProgress(message, s);
  2957. }
  2958. std::string cmMakefile::GetModulesFile(const char* filename)
  2959. {
  2960. std::string result;
  2961. // We search the module always in CMAKE_ROOT and in CMAKE_MODULE_PATH,
  2962. // and then decide based on the policy setting which one to return.
  2963. // See CMP0017 for more details.
  2964. // The specific problem was that KDE 4.5.0 installs a
  2965. // FindPackageHandleStandardArgs.cmake which doesn't have the new features
  2966. // of FPHSA.cmake introduced in CMake 2.8.3 yet, and by setting
  2967. // CMAKE_MODULE_PATH also e.g. FindZLIB.cmake from cmake included
  2968. // FPHSA.cmake from kdelibs and not from CMake, and tried to use the
  2969. // new features, which were not there in the version from kdelibs, and so
  2970. // failed ("
  2971. std::string moduleInCMakeRoot;
  2972. std::string moduleInCMakeModulePath;
  2973. // Always search in CMAKE_MODULE_PATH:
  2974. const char* cmakeModulePath = this->GetDefinition("CMAKE_MODULE_PATH");
  2975. if(cmakeModulePath)
  2976. {
  2977. std::vector<std::string> modulePath;
  2978. cmSystemTools::ExpandListArgument(cmakeModulePath, modulePath);
  2979. //Look through the possible module directories.
  2980. for(std::vector<std::string>::iterator i = modulePath.begin();
  2981. i != modulePath.end(); ++i)
  2982. {
  2983. std::string itempl = *i;
  2984. cmSystemTools::ConvertToUnixSlashes(itempl);
  2985. itempl += "/";
  2986. itempl += filename;
  2987. if(cmSystemTools::FileExists(itempl.c_str()))
  2988. {
  2989. moduleInCMakeModulePath = itempl;
  2990. break;
  2991. }
  2992. }
  2993. }
  2994. // Always search in the standard modules location.
  2995. const char* cmakeRoot = this->GetDefinition("CMAKE_ROOT");
  2996. if(cmakeRoot)
  2997. {
  2998. moduleInCMakeRoot = cmakeRoot;
  2999. moduleInCMakeRoot += "/Modules/";
  3000. moduleInCMakeRoot += filename;
  3001. cmSystemTools::ConvertToUnixSlashes(moduleInCMakeRoot);
  3002. if(!cmSystemTools::FileExists(moduleInCMakeRoot.c_str()))
  3003. {
  3004. moduleInCMakeRoot = "";
  3005. }
  3006. }
  3007. // Normally, prefer the files found in CMAKE_MODULE_PATH. Only when the file
  3008. // from which we are being called is located itself in CMAKE_ROOT, then
  3009. // prefer results from CMAKE_ROOT depending on the policy setting.
  3010. result = moduleInCMakeModulePath;
  3011. if (result.size() == 0)
  3012. {
  3013. result = moduleInCMakeRoot;
  3014. }
  3015. if ((moduleInCMakeModulePath.size()>0) && (moduleInCMakeRoot.size()>0))
  3016. {
  3017. const char* currentFile = this->GetDefinition("CMAKE_CURRENT_LIST_FILE");
  3018. if (currentFile && (strstr(currentFile, cmakeRoot) == currentFile))
  3019. {
  3020. switch (this->GetPolicyStatus(cmPolicies::CMP0017))
  3021. {
  3022. case cmPolicies::WARN:
  3023. {
  3024. cmOStringStream e;
  3025. e << "File " << currentFile << " includes "
  3026. << moduleInCMakeModulePath
  3027. << " (found via CMAKE_MODULE_PATH) which shadows "
  3028. << moduleInCMakeRoot << ". This may cause errors later on .\n"
  3029. << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0017);
  3030. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3031. // break; // fall through to OLD behaviour
  3032. }
  3033. case cmPolicies::OLD:
  3034. result = moduleInCMakeModulePath;
  3035. break;
  3036. case cmPolicies::REQUIRED_IF_USED:
  3037. case cmPolicies::REQUIRED_ALWAYS:
  3038. case cmPolicies::NEW:
  3039. default:
  3040. result = moduleInCMakeRoot;
  3041. break;
  3042. }
  3043. }
  3044. }
  3045. return result;
  3046. }
  3047. void cmMakefile::ConfigureString(const std::string& input,
  3048. std::string& output, bool atOnly,
  3049. bool escapeQuotes)
  3050. {
  3051. // Split input to handle one line at a time.
  3052. std::string::const_iterator lineStart = input.begin();
  3053. while(lineStart != input.end())
  3054. {
  3055. // Find the end of this line.
  3056. std::string::const_iterator lineEnd = lineStart;
  3057. while(lineEnd != input.end() && *lineEnd != '\n')
  3058. {
  3059. ++lineEnd;
  3060. }
  3061. // Copy the line.
  3062. std::string line(lineStart, lineEnd);
  3063. // Skip the newline character.
  3064. bool haveNewline = (lineEnd != input.end());
  3065. if(haveNewline)
  3066. {
  3067. ++lineEnd;
  3068. }
  3069. // Replace #cmakedefine instances.
  3070. if(this->cmDefineRegex.find(line))
  3071. {
  3072. const char* def =
  3073. this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  3074. if(!cmSystemTools::IsOff(def))
  3075. {
  3076. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  3077. output += line;
  3078. }
  3079. else
  3080. {
  3081. output += "/* #undef ";
  3082. output += this->cmDefineRegex.match(1);
  3083. output += " */";
  3084. }
  3085. }
  3086. else if(this->cmDefine01Regex.find(line))
  3087. {
  3088. const char* def =
  3089. this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  3090. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  3091. output += line;
  3092. if(!cmSystemTools::IsOff(def))
  3093. {
  3094. output += " 1";
  3095. }
  3096. else
  3097. {
  3098. output += " 0";
  3099. }
  3100. }
  3101. else
  3102. {
  3103. output += line;
  3104. }
  3105. if(haveNewline)
  3106. {
  3107. output += "\n";
  3108. }
  3109. // Move to the next line.
  3110. lineStart = lineEnd;
  3111. }
  3112. // Perform variable replacements.
  3113. this->ExpandVariablesInString(output, escapeQuotes, true,
  3114. atOnly, 0, -1, true);
  3115. }
  3116. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  3117. bool copyonly, bool atOnly, bool escapeQuotes,
  3118. const cmNewLineStyle& newLine)
  3119. {
  3120. int res = 1;
  3121. if ( !this->CanIWriteThisFile(outfile) )
  3122. {
  3123. cmSystemTools::Error("Attempt to write file: ",
  3124. outfile, " into a source directory.");
  3125. return 0;
  3126. }
  3127. if ( !cmSystemTools::FileExists(infile) )
  3128. {
  3129. cmSystemTools::Error("File ", infile, " does not exist.");
  3130. return 0;
  3131. }
  3132. std::string soutfile = outfile;
  3133. std::string sinfile = infile;
  3134. this->AddCMakeDependFile(sinfile);
  3135. cmSystemTools::ConvertToUnixSlashes(soutfile);
  3136. // Re-generate if non-temporary outputs are missing.
  3137. //when we finalize the configuration we will remove all
  3138. //output files that now don't exist.
  3139. this->AddCMakeOutputFile(soutfile);
  3140. mode_t perm = 0;
  3141. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  3142. std::string::size_type pos = soutfile.rfind('/');
  3143. if(pos != std::string::npos)
  3144. {
  3145. std::string path = soutfile.substr(0, pos);
  3146. cmSystemTools::MakeDirectory(path.c_str());
  3147. }
  3148. if(copyonly)
  3149. {
  3150. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  3151. soutfile.c_str()))
  3152. {
  3153. return 0;
  3154. }
  3155. }
  3156. else
  3157. {
  3158. std::string newLineCharacters;
  3159. std::ios_base::openmode omode = std::ios_base::out | std::ios_base::trunc;
  3160. if (newLine.IsValid())
  3161. {
  3162. newLineCharacters = newLine.GetCharacters();
  3163. omode |= std::ios::binary;
  3164. }
  3165. else
  3166. {
  3167. newLineCharacters = "\n";
  3168. }
  3169. std::string tempOutputFile = soutfile;
  3170. tempOutputFile += ".tmp";
  3171. std::ofstream fout(tempOutputFile.c_str(), omode);
  3172. if(!fout)
  3173. {
  3174. cmSystemTools::Error(
  3175. "Could not open file for write in copy operation ",
  3176. tempOutputFile.c_str());
  3177. cmSystemTools::ReportLastSystemError("");
  3178. return 0;
  3179. }
  3180. std::ifstream fin(sinfile.c_str());
  3181. if(!fin)
  3182. {
  3183. cmSystemTools::Error("Could not open file for read in copy operation ",
  3184. sinfile.c_str());
  3185. return 0;
  3186. }
  3187. // now copy input to output and expand variables in the
  3188. // input file at the same time
  3189. std::string inLine;
  3190. std::string outLine;
  3191. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  3192. {
  3193. outLine = "";
  3194. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  3195. fout << outLine.c_str() << newLineCharacters;
  3196. }
  3197. // close the files before attempting to copy
  3198. fin.close();
  3199. fout.close();
  3200. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  3201. soutfile.c_str()) )
  3202. {
  3203. res = 0;
  3204. }
  3205. else
  3206. {
  3207. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  3208. }
  3209. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  3210. }
  3211. return res;
  3212. }
  3213. void cmMakefile::SetProperty(const char* prop, const char* value)
  3214. {
  3215. if (!prop)
  3216. {
  3217. return;
  3218. }
  3219. // handle special props
  3220. std::string propname = prop;
  3221. if ( propname == "LINK_DIRECTORIES" )
  3222. {
  3223. std::vector<std::string> varArgsExpanded;
  3224. if(value)
  3225. {
  3226. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  3227. }
  3228. this->SetLinkDirectories(varArgsExpanded);
  3229. return;
  3230. }
  3231. if (propname == "INCLUDE_DIRECTORIES")
  3232. {
  3233. this->IncludeDirectoriesEntries.clear();
  3234. if (!value)
  3235. {
  3236. return;
  3237. }
  3238. cmListFileBacktrace lfbt;
  3239. this->GetBacktrace(lfbt);
  3240. this->IncludeDirectoriesEntries.push_back(
  3241. cmValueWithOrigin(value, lfbt));
  3242. return;
  3243. }
  3244. if (propname == "COMPILE_OPTIONS")
  3245. {
  3246. this->CompileOptionsEntries.clear();
  3247. if (!value)
  3248. {
  3249. return;
  3250. }
  3251. cmListFileBacktrace lfbt;
  3252. this->GetBacktrace(lfbt);
  3253. this->CompileOptionsEntries.push_back(cmValueWithOrigin(value, lfbt));
  3254. return;
  3255. }
  3256. if (propname == "COMPILE_DEFINITIONS")
  3257. {
  3258. this->CompileDefinitionsEntries.clear();
  3259. if (!value)
  3260. {
  3261. return;
  3262. }
  3263. cmListFileBacktrace lfbt;
  3264. this->GetBacktrace(lfbt);
  3265. cmValueWithOrigin entry(value, lfbt);
  3266. this->CompileDefinitionsEntries.push_back(entry);
  3267. return;
  3268. }
  3269. if ( propname == "INCLUDE_REGULAR_EXPRESSION" )
  3270. {
  3271. this->SetIncludeRegularExpression(value);
  3272. return;
  3273. }
  3274. if ( propname == "ADDITIONAL_MAKE_CLEAN_FILES" )
  3275. {
  3276. // This property is not inherrited
  3277. if ( strcmp(this->GetCurrentDirectory(),
  3278. this->GetStartDirectory()) != 0 )
  3279. {
  3280. return;
  3281. }
  3282. }
  3283. this->Properties.SetProperty(prop,value,cmProperty::DIRECTORY);
  3284. }
  3285. void cmMakefile::AppendProperty(const char* prop, const char* value,
  3286. bool asString)
  3287. {
  3288. if (!prop)
  3289. {
  3290. return;
  3291. }
  3292. // handle special props
  3293. std::string propname = prop;
  3294. if (propname == "INCLUDE_DIRECTORIES")
  3295. {
  3296. cmListFileBacktrace lfbt;
  3297. this->GetBacktrace(lfbt);
  3298. this->IncludeDirectoriesEntries.push_back(
  3299. cmValueWithOrigin(value, lfbt));
  3300. return;
  3301. }
  3302. if (propname == "COMPILE_OPTIONS")
  3303. {
  3304. cmListFileBacktrace lfbt;
  3305. this->GetBacktrace(lfbt);
  3306. this->CompileOptionsEntries.push_back(
  3307. cmValueWithOrigin(value, lfbt));
  3308. return;
  3309. }
  3310. if (propname == "COMPILE_DEFINITIONS")
  3311. {
  3312. cmListFileBacktrace lfbt;
  3313. this->GetBacktrace(lfbt);
  3314. this->CompileDefinitionsEntries.push_back(
  3315. cmValueWithOrigin(value, lfbt));
  3316. return;
  3317. }
  3318. if ( propname == "LINK_DIRECTORIES" )
  3319. {
  3320. std::vector<std::string> varArgsExpanded;
  3321. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  3322. for(std::vector<std::string>::const_iterator vi = varArgsExpanded.begin();
  3323. vi != varArgsExpanded.end(); ++vi)
  3324. {
  3325. this->AddLinkDirectory(vi->c_str());
  3326. }
  3327. return;
  3328. }
  3329. this->Properties.AppendProperty(prop,value,cmProperty::DIRECTORY,asString);
  3330. }
  3331. const char *cmMakefile::GetPropertyOrDefinition(const char* prop)
  3332. {
  3333. const char *ret = this->GetProperty(prop, cmProperty::DIRECTORY);
  3334. if (!ret)
  3335. {
  3336. ret = this->GetDefinition(prop);
  3337. }
  3338. return ret;
  3339. }
  3340. const char *cmMakefile::GetProperty(const char* prop)
  3341. {
  3342. return this->GetProperty(prop, cmProperty::DIRECTORY);
  3343. }
  3344. const char *cmMakefile::GetProperty(const char* prop,
  3345. cmProperty::ScopeType scope)
  3346. {
  3347. if(!prop)
  3348. {
  3349. return 0;
  3350. }
  3351. // watch for specific properties
  3352. static std::string output;
  3353. output = "";
  3354. if (!strcmp("PARENT_DIRECTORY",prop))
  3355. {
  3356. if(cmLocalGenerator* plg = this->LocalGenerator->GetParent())
  3357. {
  3358. output = plg->GetMakefile()->GetStartDirectory();
  3359. }
  3360. return output.c_str();
  3361. }
  3362. else if (!strcmp("INCLUDE_REGULAR_EXPRESSION",prop) )
  3363. {
  3364. output = this->GetIncludeRegularExpression();
  3365. return output.c_str();
  3366. }
  3367. else if (!strcmp("LISTFILE_STACK",prop))
  3368. {
  3369. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  3370. i != this->ListFileStack.end(); ++i)
  3371. {
  3372. if (i != this->ListFileStack.begin())
  3373. {
  3374. output += ";";
  3375. }
  3376. output += *i;
  3377. }
  3378. return output.c_str();
  3379. }
  3380. else if (!strcmp("VARIABLES",prop) || !strcmp("CACHE_VARIABLES",prop))
  3381. {
  3382. int cacheonly = 0;
  3383. if ( !strcmp("CACHE_VARIABLES",prop) )
  3384. {
  3385. cacheonly = 1;
  3386. }
  3387. std::vector<std::string> vars = this->GetDefinitions(cacheonly);
  3388. for (unsigned int cc = 0; cc < vars.size(); cc ++ )
  3389. {
  3390. if ( cc > 0 )
  3391. {
  3392. output += ";";
  3393. }
  3394. output += vars[cc];
  3395. }
  3396. return output.c_str();
  3397. }
  3398. else if (!strcmp("MACROS",prop))
  3399. {
  3400. this->GetListOfMacros(output);
  3401. return output.c_str();
  3402. }
  3403. else if (!strcmp("DEFINITIONS",prop))
  3404. {
  3405. output += this->DefineFlagsOrig;
  3406. return output.c_str();
  3407. }
  3408. else if (!strcmp("LINK_DIRECTORIES",prop))
  3409. {
  3410. cmOStringStream str;
  3411. for (std::vector<std::string>::const_iterator
  3412. it = this->GetLinkDirectories().begin();
  3413. it != this->GetLinkDirectories().end();
  3414. ++ it )
  3415. {
  3416. if ( it != this->GetLinkDirectories().begin())
  3417. {
  3418. str << ";";
  3419. }
  3420. str << it->c_str();
  3421. }
  3422. output = str.str();
  3423. return output.c_str();
  3424. }
  3425. else if (!strcmp("INCLUDE_DIRECTORIES",prop))
  3426. {
  3427. std::string sep;
  3428. for (std::vector<cmValueWithOrigin>::const_iterator
  3429. it = this->IncludeDirectoriesEntries.begin(),
  3430. end = this->IncludeDirectoriesEntries.end();
  3431. it != end; ++it)
  3432. {
  3433. output += sep;
  3434. output += it->Value;
  3435. sep = ";";
  3436. }
  3437. return output.c_str();
  3438. }
  3439. else if (!strcmp("COMPILE_OPTIONS",prop))
  3440. {
  3441. std::string sep;
  3442. for (std::vector<cmValueWithOrigin>::const_iterator
  3443. it = this->CompileOptionsEntries.begin(),
  3444. end = this->CompileOptionsEntries.end();
  3445. it != end; ++it)
  3446. {
  3447. output += sep;
  3448. output += it->Value;
  3449. sep = ";";
  3450. }
  3451. return output.c_str();
  3452. }
  3453. else if (!strcmp("COMPILE_DEFINITIONS",prop))
  3454. {
  3455. std::string sep;
  3456. for (std::vector<cmValueWithOrigin>::const_iterator
  3457. it = this->CompileDefinitionsEntries.begin(),
  3458. end = this->CompileDefinitionsEntries.end();
  3459. it != end; ++it)
  3460. {
  3461. output += sep;
  3462. output += it->Value;
  3463. sep = ";";
  3464. }
  3465. return output.c_str();
  3466. }
  3467. bool chain = false;
  3468. const char *retVal =
  3469. this->Properties.GetPropertyValue(prop, scope, chain);
  3470. if (chain)
  3471. {
  3472. if(this->LocalGenerator->GetParent())
  3473. {
  3474. return this->LocalGenerator->GetParent()->GetMakefile()->
  3475. GetProperty(prop, scope);
  3476. }
  3477. return this->GetCMakeInstance()->GetProperty(prop,scope);
  3478. }
  3479. return retVal;
  3480. }
  3481. bool cmMakefile::GetPropertyAsBool(const char* prop)
  3482. {
  3483. return cmSystemTools::IsOn(this->GetProperty(prop));
  3484. }
  3485. //----------------------------------------------------------------------------
  3486. const char* cmMakefile::GetFeature(const char* feature, const char* config)
  3487. {
  3488. // TODO: Define accumulation policy for features (prepend, append, replace).
  3489. // Currently we always replace.
  3490. if(config && *config)
  3491. {
  3492. std::string featureConfig = feature;
  3493. featureConfig += "_";
  3494. featureConfig += cmSystemTools::UpperCase(config);
  3495. if(const char* value = this->GetProperty(featureConfig.c_str()))
  3496. {
  3497. return value;
  3498. }
  3499. }
  3500. if(const char* value = this->GetProperty(feature))
  3501. {
  3502. return value;
  3503. }
  3504. if(cmLocalGenerator* parent = this->LocalGenerator->GetParent())
  3505. {
  3506. return parent->GetMakefile()->GetFeature(feature, config);
  3507. }
  3508. return 0;
  3509. }
  3510. cmTarget* cmMakefile::FindTarget(const char* name, bool excludeAliases)
  3511. {
  3512. if (!excludeAliases)
  3513. {
  3514. std::map<std::string, cmTarget*>::iterator i
  3515. = this->AliasTargets.find(name);
  3516. if (i != this->AliasTargets.end())
  3517. {
  3518. return i->second;
  3519. }
  3520. }
  3521. cmTargets& tgts = this->GetTargets();
  3522. cmTargets::iterator i = tgts.find ( name );
  3523. if ( i != tgts.end() )
  3524. {
  3525. return &i->second;
  3526. }
  3527. return 0;
  3528. }
  3529. //----------------------------------------------------------------------------
  3530. cmTest* cmMakefile::CreateTest(const char* testName)
  3531. {
  3532. if ( !testName )
  3533. {
  3534. return 0;
  3535. }
  3536. cmTest* test = this->GetTest(testName);
  3537. if ( test )
  3538. {
  3539. return test;
  3540. }
  3541. test = new cmTest(this);
  3542. test->SetName(testName);
  3543. this->Tests[testName] = test;
  3544. return test;
  3545. }
  3546. //----------------------------------------------------------------------------
  3547. cmTest* cmMakefile::GetTest(const char* testName) const
  3548. {
  3549. if(testName)
  3550. {
  3551. std::map<cmStdString, cmTest*>::const_iterator
  3552. mi = this->Tests.find(testName);
  3553. if(mi != this->Tests.end())
  3554. {
  3555. return mi->second;
  3556. }
  3557. }
  3558. return 0;
  3559. }
  3560. std::string cmMakefile::GetListFileStack()
  3561. {
  3562. cmOStringStream tmp;
  3563. size_t depth = this->ListFileStack.size();
  3564. if (depth > 0)
  3565. {
  3566. std::deque<cmStdString>::iterator it = this->ListFileStack.end();
  3567. do
  3568. {
  3569. if (depth != this->ListFileStack.size())
  3570. {
  3571. tmp << "\n ";
  3572. }
  3573. --it;
  3574. tmp << "[";
  3575. tmp << depth;
  3576. tmp << "]\t";
  3577. tmp << *it;
  3578. depth--;
  3579. }
  3580. while (it != this->ListFileStack.begin());
  3581. }
  3582. return tmp.str();
  3583. }
  3584. void cmMakefile::PushScope()
  3585. {
  3586. cmDefinitions* parent = &this->Internal->VarStack.top();
  3587. const std::set<cmStdString>& init = this->Internal->VarInitStack.top();
  3588. const std::set<cmStdString>& usage = this->Internal->VarUsageStack.top();
  3589. this->Internal->VarStack.push(cmDefinitions(parent));
  3590. this->Internal->VarInitStack.push(init);
  3591. this->Internal->VarUsageStack.push(usage);
  3592. }
  3593. void cmMakefile::PopScope()
  3594. {
  3595. cmDefinitions* current = &this->Internal->VarStack.top();
  3596. std::set<cmStdString> init = this->Internal->VarInitStack.top();
  3597. std::set<cmStdString> usage = this->Internal->VarUsageStack.top();
  3598. const std::set<cmStdString>& locals = current->LocalKeys();
  3599. // Remove initialization and usage information for variables in the local
  3600. // scope.
  3601. std::set<cmStdString>::const_iterator it = locals.begin();
  3602. for (; it != locals.end(); ++it)
  3603. {
  3604. init.erase(*it);
  3605. if (!this->VariableUsed(it->c_str()))
  3606. {
  3607. this->CheckForUnused("out of scope", it->c_str());
  3608. }
  3609. else
  3610. {
  3611. usage.erase(*it);
  3612. }
  3613. }
  3614. this->Internal->VarStack.pop();
  3615. this->Internal->VarInitStack.pop();
  3616. this->Internal->VarUsageStack.pop();
  3617. // Push initialization and usage up to the parent scope.
  3618. it = init.begin();
  3619. for (; it != init.end(); ++it)
  3620. {
  3621. this->Internal->VarInitStack.top().insert(*it);
  3622. }
  3623. it = usage.begin();
  3624. for (; it != usage.end(); ++it)
  3625. {
  3626. this->Internal->VarUsageStack.top().insert(*it);
  3627. }
  3628. }
  3629. void cmMakefile::RaiseScope(const char *var, const char *varDef)
  3630. {
  3631. if (!var || !strlen(var))
  3632. {
  3633. return;
  3634. }
  3635. cmDefinitions& cur = this->Internal->VarStack.top();
  3636. if(cmDefinitions* up = cur.GetParent())
  3637. {
  3638. // First localize the definition in the current scope.
  3639. cur.Get(var);
  3640. // Now update the definition in the parent scope.
  3641. up->Set(var, varDef);
  3642. }
  3643. else if(cmLocalGenerator* plg = this->LocalGenerator->GetParent())
  3644. {
  3645. // Update the definition in the parent directory top scope. This
  3646. // directory's scope was initialized by the closure of the parent
  3647. // scope, so we do not need to localize the definition first.
  3648. cmMakefile* parent = plg->GetMakefile();
  3649. if (varDef)
  3650. {
  3651. parent->AddDefinition(var, varDef);
  3652. }
  3653. else
  3654. {
  3655. parent->RemoveDefinition(var);
  3656. }
  3657. }
  3658. else
  3659. {
  3660. cmOStringStream m;
  3661. m << "Cannot set \"" << var << "\": current scope has no parent.";
  3662. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  3663. }
  3664. }
  3665. // define properties
  3666. void cmMakefile::DefineProperties(cmake *cm)
  3667. {
  3668. cm->DefineProperty
  3669. ("ADDITIONAL_MAKE_CLEAN_FILES", cmProperty::DIRECTORY,
  3670. "Additional files to clean during the make clean stage.",
  3671. "A list of files that will be cleaned as a part of the "
  3672. "\"make clean\" stage. ");
  3673. cm->DefineProperty
  3674. ("CLEAN_NO_CUSTOM", cmProperty::DIRECTORY,
  3675. "Should the output of custom commands be left.",
  3676. "If this is true then the outputs of custom commands for this "
  3677. "directory will not be removed during the \"make clean\" stage. ");
  3678. cm->DefineProperty
  3679. ("LISTFILE_STACK", cmProperty::DIRECTORY,
  3680. "The current stack of listfiles being processed.",
  3681. "This property is mainly useful when trying to debug errors "
  3682. "in your CMake scripts. It returns a list of what list files "
  3683. "are currently being processed, in order. So if one listfile "
  3684. "does an INCLUDE command then that is effectively pushing "
  3685. "the included listfile onto the stack.", false);
  3686. cm->DefineProperty
  3687. ("TEST_INCLUDE_FILE", cmProperty::DIRECTORY,
  3688. "A cmake file that will be included when ctest is run.",
  3689. "If you specify TEST_INCLUDE_FILE, that file will be "
  3690. "included and processed when ctest is run on the directory.");
  3691. cm->DefineProperty
  3692. ("COMPILE_DEFINITIONS", cmProperty::DIRECTORY,
  3693. "Preprocessor definitions for compiling a directory's sources.",
  3694. "The COMPILE_DEFINITIONS property may be set to a "
  3695. "semicolon-separated list of preprocessor "
  3696. "definitions using the syntax VAR or VAR=value. Function-style "
  3697. "definitions are not supported. CMake will automatically escape "
  3698. "the value correctly for the native build system (note that CMake "
  3699. "language syntax may require escapes to specify some values). "
  3700. "This property may be set on a per-configuration basis using the name "
  3701. "COMPILE_DEFINITIONS_<CONFIG> where <CONFIG> is an upper-case name "
  3702. "(ex. \"COMPILE_DEFINITIONS_DEBUG\"). "
  3703. "This property will be initialized in each directory by its value "
  3704. "in the directory's parent.\n"
  3705. "CMake will automatically drop some definitions that "
  3706. "are not supported by the native build tool. "
  3707. "The VS6 IDE does not support definition values with spaces "
  3708. "(but NMake does).\n"
  3709. CM_DOCUMENT_COMPILE_DEFINITIONS_DISCLAIMER);
  3710. cm->DefineProperty
  3711. ("COMPILE_DEFINITIONS_<CONFIG>", cmProperty::DIRECTORY,
  3712. "Per-configuration preprocessor definitions in a directory.",
  3713. "This is the configuration-specific version of COMPILE_DEFINITIONS. "
  3714. "This property will be initialized in each directory by its value "
  3715. "in the directory's parent.\n");
  3716. cm->DefineProperty
  3717. ("IMPLICIT_DEPENDS_INCLUDE_TRANSFORM", cmProperty::DIRECTORY,
  3718. "Specify #include line transforms for dependencies in a directory.",
  3719. "This property specifies rules to transform macro-like #include lines "
  3720. "during implicit dependency scanning of C and C++ source files. "
  3721. "The list of rules must be semicolon-separated with each entry of "
  3722. "the form \"A_MACRO(%)=value-with-%\" (the % must be literal). "
  3723. "During dependency scanning occurrences of A_MACRO(...) on #include "
  3724. "lines will be replaced by the value given with the macro argument "
  3725. "substituted for '%'. For example, the entry\n"
  3726. " MYDIR(%)=<mydir/%>\n"
  3727. "will convert lines of the form\n"
  3728. " #include MYDIR(myheader.h)\n"
  3729. "to\n"
  3730. " #include <mydir/myheader.h>\n"
  3731. "allowing the dependency to be followed.\n"
  3732. "This property applies to sources in all targets within a directory. "
  3733. "The property value is initialized in each directory by its value "
  3734. "in the directory's parent.");
  3735. cm->DefineProperty
  3736. ("EXCLUDE_FROM_ALL", cmProperty::DIRECTORY,
  3737. "Exclude the directory from the all target of its parent.",
  3738. "A property on a directory that indicates if its targets are excluded "
  3739. "from the default build target. If it is not, then with a Makefile "
  3740. "for example typing make will cause the targets to be built. "
  3741. "The same concept applies to the default build of other generators.",
  3742. false);
  3743. cm->DefineProperty
  3744. ("PARENT_DIRECTORY", cmProperty::DIRECTORY,
  3745. "Source directory that added current subdirectory.",
  3746. "This read-only property specifies the source directory that "
  3747. "added the current source directory as a subdirectory of the build. "
  3748. "In the top-level directory the value is the empty-string.", false);
  3749. cm->DefineProperty
  3750. ("INCLUDE_REGULAR_EXPRESSION", cmProperty::DIRECTORY,
  3751. "Include file scanning regular expression.",
  3752. "This read-only property specifies the regular expression used "
  3753. "during dependency scanning to match include files that should "
  3754. "be followed. See the include_regular_expression command.", false);
  3755. cm->DefineProperty
  3756. ("INTERPROCEDURAL_OPTIMIZATION", cmProperty::DIRECTORY,
  3757. "Enable interprocedural optimization for targets in a directory.",
  3758. "If set to true, enables interprocedural optimizations "
  3759. "if they are known to be supported by the compiler.");
  3760. cm->DefineProperty
  3761. ("INTERPROCEDURAL_OPTIMIZATION_<CONFIG>", cmProperty::DIRECTORY,
  3762. "Per-configuration interprocedural optimization for a directory.",
  3763. "This is a per-configuration version of INTERPROCEDURAL_OPTIMIZATION. "
  3764. "If set, this property overrides the generic property "
  3765. "for the named configuration.");
  3766. cm->DefineProperty
  3767. ("VARIABLES", cmProperty::DIRECTORY,
  3768. "List of variables defined in the current directory.",
  3769. "This read-only property specifies the list of CMake variables "
  3770. "currently defined. "
  3771. "It is intended for debugging purposes.", false);
  3772. cm->DefineProperty
  3773. ("CACHE_VARIABLES", cmProperty::DIRECTORY,
  3774. "List of cache variables available in the current directory.",
  3775. "This read-only property specifies the list of CMake cache "
  3776. "variables currently defined. "
  3777. "It is intended for debugging purposes.", false);
  3778. cm->DefineProperty
  3779. ("MACROS", cmProperty::DIRECTORY,
  3780. "List of macro commands available in the current directory.",
  3781. "This read-only property specifies the list of CMake macros "
  3782. "currently defined. "
  3783. "It is intended for debugging purposes. "
  3784. "See the macro command.", false);
  3785. cm->DefineProperty
  3786. ("DEFINITIONS", cmProperty::DIRECTORY,
  3787. "For CMake 2.4 compatibility only. Use COMPILE_DEFINITIONS instead.",
  3788. "This read-only property specifies the list of flags given so far "
  3789. "to the add_definitions command. "
  3790. "It is intended for debugging purposes. "
  3791. "Use the COMPILE_DEFINITIONS instead.", false);
  3792. cm->DefineProperty
  3793. ("INCLUDE_DIRECTORIES", cmProperty::DIRECTORY,
  3794. "List of preprocessor include file search directories.",
  3795. "This property specifies the list of directories given "
  3796. "so far to the include_directories command. "
  3797. "This property exists on directories and targets. "
  3798. "In addition to accepting values from the include_directories "
  3799. "command, values may be set directly on any directory or any "
  3800. "target using the set_property command. "
  3801. "A target gets its initial value for this property from the value "
  3802. "of the directory property. "
  3803. "A directory gets its initial value from its parent directory if "
  3804. "it has one. "
  3805. "Both directory and target property values are adjusted by calls "
  3806. "to the include_directories command."
  3807. "\n"
  3808. "The target property values are used by the generators to set "
  3809. "the include paths for the compiler. "
  3810. "See also the include_directories command.");
  3811. cm->DefineProperty
  3812. ("COMPILE_OPTIONS", cmProperty::DIRECTORY,
  3813. "List of options to pass to the compiler.",
  3814. "This property specifies the list of directories given "
  3815. "so far for this property. "
  3816. "This property exists on directories and targets."
  3817. "\n"
  3818. "The target property values are used by the generators to set "
  3819. "the options for the compiler.\n"
  3820. "Contents of COMPILE_OPTIONS may use \"generator expressions\" with "
  3821. "the syntax \"$<...>\". "
  3822. CM_DOCUMENT_COMMAND_GENERATOR_EXPRESSIONS);
  3823. cm->DefineProperty
  3824. ("LINK_DIRECTORIES", cmProperty::DIRECTORY,
  3825. "List of linker search directories.",
  3826. "This read-only property specifies the list of directories given "
  3827. "so far to the link_directories command. "
  3828. "It is intended for debugging purposes.", false);
  3829. cm->DefineProperty
  3830. ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
  3831. "Specify a launcher for compile rules.",
  3832. "See the global property of the same name for details. "
  3833. "This overrides the global property for a directory.",
  3834. true);
  3835. cm->DefineProperty
  3836. ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
  3837. "Specify a launcher for link rules.",
  3838. "See the global property of the same name for details. "
  3839. "This overrides the global property for a directory.",
  3840. true);
  3841. cm->DefineProperty
  3842. ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
  3843. "Specify a launcher for custom rules.",
  3844. "See the global property of the same name for details. "
  3845. "This overrides the global property for a directory.",
  3846. true);
  3847. cm->DefineProperty
  3848. ("VS_GLOBAL_SECTION_PRE_<section>", cmProperty::DIRECTORY,
  3849. "Specify a preSolution global section in Visual Studio.",
  3850. "Setting a property like this generates an entry of the following form "
  3851. "in the solution file:\n"
  3852. " GlobalSection(<section>) = preSolution\n"
  3853. " <contents based on property value>\n"
  3854. " EndGlobalSection\n"
  3855. "The property must be set to a semicolon-separated list of key=value "
  3856. "pairs. Each such pair will be transformed into an entry in the solution "
  3857. "global section. Whitespace around key and value is ignored. List "
  3858. "elements which do not contain an equal sign are skipped."
  3859. "\n"
  3860. "This property only works for Visual Studio 7 and above; it is ignored "
  3861. "on other generators. The property only applies when set on a directory "
  3862. "whose CMakeLists.txt contains a project() command.");
  3863. cm->DefineProperty
  3864. ("VS_GLOBAL_SECTION_POST_<section>", cmProperty::DIRECTORY,
  3865. "Specify a postSolution global section in Visual Studio.",
  3866. "Setting a property like this generates an entry of the following form "
  3867. "in the solution file:\n"
  3868. " GlobalSection(<section>) = postSolution\n"
  3869. " <contents based on property value>\n"
  3870. " EndGlobalSection\n"
  3871. "The property must be set to a semicolon-separated list of key=value "
  3872. "pairs. Each such pair will be transformed into an entry in the solution "
  3873. "global section. Whitespace around key and value is ignored. List "
  3874. "elements which do not contain an equal sign are skipped."
  3875. "\n"
  3876. "This property only works for Visual Studio 7 and above; it is ignored "
  3877. "on other generators. The property only applies when set on a directory "
  3878. "whose CMakeLists.txt contains a project() command."
  3879. "\n"
  3880. "Note that CMake generates postSolution sections ExtensibilityGlobals "
  3881. "and ExtensibilityAddIns by default. If you set the corresponding "
  3882. "property, it will override the default section. For example, setting "
  3883. "VS_GLOBAL_SECTION_POST_ExtensibilityGlobals will override the default "
  3884. "contents of the ExtensibilityGlobals section, while keeping "
  3885. "ExtensibilityAddIns on its default.");
  3886. }
  3887. //----------------------------------------------------------------------------
  3888. cmTarget*
  3889. cmMakefile::AddImportedTarget(const char* name, cmTarget::TargetType type,
  3890. bool global)
  3891. {
  3892. // Create the target.
  3893. cmsys::auto_ptr<cmTarget> target(new cmTarget);
  3894. target->SetType(type, name);
  3895. target->SetMakefile(this);
  3896. target->MarkAsImported();
  3897. // Add to the set of available imported targets.
  3898. this->ImportedTargets[name] = target.get();
  3899. if(global)
  3900. {
  3901. this->LocalGenerator->GetGlobalGenerator()->AddTarget(target.get());
  3902. }
  3903. // Transfer ownership to this cmMakefile object.
  3904. this->ImportedTargetsOwned.push_back(target.get());
  3905. return target.release();
  3906. }
  3907. //----------------------------------------------------------------------------
  3908. cmTarget* cmMakefile::FindTargetToUse(const char* name, bool excludeAliases)
  3909. {
  3910. // Look for an imported target. These take priority because they
  3911. // are more local in scope and do not have to be globally unique.
  3912. std::map<cmStdString, cmTarget*>::const_iterator
  3913. imported = this->ImportedTargets.find(name);
  3914. if(imported != this->ImportedTargets.end())
  3915. {
  3916. return imported->second;
  3917. }
  3918. // Look for a target built in this directory.
  3919. if(cmTarget* t = this->FindTarget(name, excludeAliases))
  3920. {
  3921. return t;
  3922. }
  3923. // Look for a target built in this project.
  3924. return this->LocalGenerator->GetGlobalGenerator()->FindTarget(0, name,
  3925. excludeAliases);
  3926. }
  3927. //----------------------------------------------------------------------------
  3928. bool cmMakefile::IsAlias(const char *name)
  3929. {
  3930. if (this->AliasTargets.find(name) != this->AliasTargets.end())
  3931. return true;
  3932. return this->GetLocalGenerator()->GetGlobalGenerator()->IsAlias(name);
  3933. }
  3934. //----------------------------------------------------------------------------
  3935. cmGeneratorTarget* cmMakefile::FindGeneratorTargetToUse(const char* name)
  3936. {
  3937. cmTarget *t = this->FindTargetToUse(name);
  3938. return this->LocalGenerator->GetGlobalGenerator()->GetGeneratorTarget(t);
  3939. }
  3940. //----------------------------------------------------------------------------
  3941. bool cmMakefile::EnforceUniqueName(std::string const& name, std::string& msg,
  3942. bool isCustom)
  3943. {
  3944. if(this->IsAlias(name.c_str()))
  3945. {
  3946. cmOStringStream e;
  3947. e << "cannot create target \"" << name
  3948. << "\" because an alias with the same name already exists.";
  3949. msg = e.str();
  3950. return false;
  3951. }
  3952. if(cmTarget* existing = this->FindTargetToUse(name.c_str()))
  3953. {
  3954. // The name given conflicts with an existing target. Produce an
  3955. // error in a compatible way.
  3956. if(existing->IsImported())
  3957. {
  3958. // Imported targets were not supported in previous versions.
  3959. // This is new code, so we can make it an error.
  3960. cmOStringStream e;
  3961. e << "cannot create target \"" << name
  3962. << "\" because an imported target with the same name already exists.";
  3963. msg = e.str();
  3964. return false;
  3965. }
  3966. else
  3967. {
  3968. // target names must be globally unique
  3969. switch (this->GetPolicyStatus(cmPolicies::CMP0002))
  3970. {
  3971. case cmPolicies::WARN:
  3972. this->IssueMessage(cmake::AUTHOR_WARNING, this->GetPolicies()->
  3973. GetPolicyWarning(cmPolicies::CMP0002));
  3974. case cmPolicies::OLD:
  3975. return true;
  3976. case cmPolicies::REQUIRED_IF_USED:
  3977. case cmPolicies::REQUIRED_ALWAYS:
  3978. this->IssueMessage(cmake::FATAL_ERROR,
  3979. this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0002)
  3980. );
  3981. return true;
  3982. case cmPolicies::NEW:
  3983. break;
  3984. }
  3985. // The conflict is with a non-imported target.
  3986. // Allow this if the user has requested support.
  3987. cmake* cm =
  3988. this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  3989. if(isCustom && existing->GetType() == cmTarget::UTILITY &&
  3990. this != existing->GetMakefile() &&
  3991. cm->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  3992. {
  3993. return true;
  3994. }
  3995. // Produce an error that tells the user how to work around the
  3996. // problem.
  3997. cmOStringStream e;
  3998. e << "cannot create target \"" << name
  3999. << "\" because another target with the same name already exists. "
  4000. << "The existing target is ";
  4001. switch(existing->GetType())
  4002. {
  4003. case cmTarget::EXECUTABLE:
  4004. e << "an executable ";
  4005. break;
  4006. case cmTarget::STATIC_LIBRARY:
  4007. e << "a static library ";
  4008. break;
  4009. case cmTarget::SHARED_LIBRARY:
  4010. e << "a shared library ";
  4011. break;
  4012. case cmTarget::MODULE_LIBRARY:
  4013. e << "a module library ";
  4014. break;
  4015. case cmTarget::UTILITY:
  4016. e << "a custom target ";
  4017. break;
  4018. default: break;
  4019. }
  4020. e << "created in source directory \""
  4021. << existing->GetMakefile()->GetCurrentDirectory() << "\". "
  4022. << "See documentation for policy CMP0002 for more details.";
  4023. msg = e.str();
  4024. return false;
  4025. }
  4026. }
  4027. return true;
  4028. }
  4029. //----------------------------------------------------------------------------
  4030. bool cmMakefile::EnforceUniqueDir(const char* srcPath, const char* binPath)
  4031. {
  4032. // Make sure the binary directory is unique.
  4033. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  4034. if(gg->BinaryDirectoryIsNew(binPath))
  4035. {
  4036. return true;
  4037. }
  4038. cmOStringStream e;
  4039. switch (this->GetPolicyStatus(cmPolicies::CMP0013))
  4040. {
  4041. case cmPolicies::WARN:
  4042. // Print the warning.
  4043. e << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0013)
  4044. << "\n"
  4045. << "The binary directory\n"
  4046. << " " << binPath << "\n"
  4047. << "is already used to build a source directory. "
  4048. << "This command uses it to build source directory\n"
  4049. << " " << srcPath << "\n"
  4050. << "which can generate conflicting build files. "
  4051. << "CMake does not support this use case but it used "
  4052. << "to work accidentally and is being allowed for "
  4053. << "compatibility.";
  4054. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  4055. case cmPolicies::OLD:
  4056. // OLD behavior does not warn.
  4057. return true;
  4058. case cmPolicies::REQUIRED_IF_USED:
  4059. case cmPolicies::REQUIRED_ALWAYS:
  4060. e << this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0013)
  4061. << "\n";
  4062. case cmPolicies::NEW:
  4063. // NEW behavior prints the error.
  4064. e << "The binary directory\n"
  4065. << " " << binPath << "\n"
  4066. << "is already used to build a source directory. "
  4067. << "It cannot be used to build source directory\n"
  4068. << " " << srcPath << "\n"
  4069. << "Specify a unique binary directory name.";
  4070. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4071. break;
  4072. }
  4073. return false;
  4074. }
  4075. //----------------------------------------------------------------------------
  4076. cmPolicies::PolicyStatus
  4077. cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id)
  4078. {
  4079. // Get the current setting of the policy.
  4080. cmPolicies::PolicyStatus cur = this->GetPolicyStatusInternal(id);
  4081. // If the policy is required to be set to NEW but is not, ignore the
  4082. // current setting and tell the caller.
  4083. if(cur != cmPolicies::NEW)
  4084. {
  4085. if(cur == cmPolicies::REQUIRED_ALWAYS ||
  4086. cur == cmPolicies::REQUIRED_IF_USED)
  4087. {
  4088. return cur;
  4089. }
  4090. cmPolicies::PolicyStatus def = this->GetPolicies()->GetPolicyStatus(id);
  4091. if(def == cmPolicies::REQUIRED_ALWAYS ||
  4092. def == cmPolicies::REQUIRED_IF_USED)
  4093. {
  4094. return def;
  4095. }
  4096. }
  4097. // The current setting is okay.
  4098. return cur;
  4099. }
  4100. //----------------------------------------------------------------------------
  4101. cmPolicies::PolicyStatus
  4102. cmMakefile::GetPolicyStatusInternal(cmPolicies::PolicyID id)
  4103. {
  4104. // Is the policy set in our stack?
  4105. for(PolicyStackType::reverse_iterator psi = this->PolicyStack.rbegin();
  4106. psi != this->PolicyStack.rend(); ++psi)
  4107. {
  4108. PolicyStackEntry::const_iterator pse = psi->find(id);
  4109. if(pse != psi->end())
  4110. {
  4111. return pse->second;
  4112. }
  4113. }
  4114. // If we have a parent directory, recurse up to it.
  4115. if(this->LocalGenerator->GetParent())
  4116. {
  4117. cmMakefile* parent = this->LocalGenerator->GetParent()->GetMakefile();
  4118. return parent->GetPolicyStatusInternal(id);
  4119. }
  4120. // The policy is not set. Use the default for this CMake version.
  4121. return this->GetPolicies()->GetPolicyStatus(id);
  4122. }
  4123. bool cmMakefile::SetPolicy(const char *id,
  4124. cmPolicies::PolicyStatus status)
  4125. {
  4126. cmPolicies::PolicyID pid;
  4127. if (!this->GetPolicies()->GetPolicyID(id, /* out */ pid))
  4128. {
  4129. cmOStringStream e;
  4130. e << "Policy \"" << id << "\" is not known to this version of CMake.";
  4131. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4132. return false;
  4133. }
  4134. return this->SetPolicy(pid,status);
  4135. }
  4136. //----------------------------------------------------------------------------
  4137. bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
  4138. cmPolicies::PolicyStatus status)
  4139. {
  4140. // A REQUIRED_ALWAYS policy may be set only to NEW.
  4141. if(status != cmPolicies::NEW &&
  4142. this->GetPolicies()->GetPolicyStatus(id) ==
  4143. cmPolicies::REQUIRED_ALWAYS)
  4144. {
  4145. std::string msg =
  4146. this->GetPolicies()->GetRequiredAlwaysPolicyError(id);
  4147. this->IssueMessage(cmake::FATAL_ERROR, msg.c_str());
  4148. return false;
  4149. }
  4150. // Update the policy stack from the top to the top-most strong entry.
  4151. bool previous_was_weak = true;
  4152. for(PolicyStackType::reverse_iterator psi = this->PolicyStack.rbegin();
  4153. previous_was_weak && psi != this->PolicyStack.rend(); ++psi)
  4154. {
  4155. (*psi)[id] = status;
  4156. previous_was_weak = psi->Weak;
  4157. }
  4158. // Special hook for presenting compatibility variable as soon as
  4159. // the user requests it.
  4160. if(id == cmPolicies::CMP0001 &&
  4161. (status == cmPolicies::WARN || status == cmPolicies::OLD))
  4162. {
  4163. if(!(this->GetCacheManager()
  4164. ->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY")))
  4165. {
  4166. // Set it to 2.4 because that is the last version where the
  4167. // variable had meaning.
  4168. this->AddCacheDefinition
  4169. ("CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  4170. "For backwards compatibility, what version of CMake "
  4171. "commands and "
  4172. "syntax should this version of CMake try to support.",
  4173. cmCacheManager::STRING);
  4174. }
  4175. }
  4176. return true;
  4177. }
  4178. //----------------------------------------------------------------------------
  4179. cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m, bool weak,
  4180. cmPolicies::PolicyMap const& pm):
  4181. Makefile(m), ReportError(true)
  4182. {
  4183. this->Makefile->PushPolicy(weak, pm);
  4184. this->Makefile->PushPolicyBarrier();
  4185. }
  4186. //----------------------------------------------------------------------------
  4187. cmMakefile::PolicyPushPop::~PolicyPushPop()
  4188. {
  4189. this->Makefile->PopPolicyBarrier(this->ReportError);
  4190. this->Makefile->PopPolicy();
  4191. }
  4192. //----------------------------------------------------------------------------
  4193. void cmMakefile::PushPolicy(bool weak, cmPolicies::PolicyMap const& pm)
  4194. {
  4195. // Allocate a new stack entry.
  4196. this->PolicyStack.push_back(PolicyStackEntry(pm, weak));
  4197. }
  4198. //----------------------------------------------------------------------------
  4199. void cmMakefile::PopPolicy()
  4200. {
  4201. if(this->PolicyStack.size() > this->PolicyBarriers.back())
  4202. {
  4203. this->PolicyStack.pop_back();
  4204. }
  4205. else
  4206. {
  4207. this->IssueMessage(cmake::FATAL_ERROR,
  4208. "cmake_policy POP without matching PUSH");
  4209. }
  4210. }
  4211. //----------------------------------------------------------------------------
  4212. void cmMakefile::PushPolicyBarrier()
  4213. {
  4214. this->PolicyBarriers.push_back(this->PolicyStack.size());
  4215. }
  4216. //----------------------------------------------------------------------------
  4217. void cmMakefile::PopPolicyBarrier(bool reportError)
  4218. {
  4219. // Remove any extra entries pushed on the barrier.
  4220. PolicyStackType::size_type barrier = this->PolicyBarriers.back();
  4221. while(this->PolicyStack.size() > barrier)
  4222. {
  4223. if(reportError)
  4224. {
  4225. this->IssueMessage(cmake::FATAL_ERROR,
  4226. "cmake_policy PUSH without matching POP");
  4227. reportError = false;
  4228. }
  4229. this->PopPolicy();
  4230. }
  4231. // Remove the barrier.
  4232. this->PolicyBarriers.pop_back();
  4233. }
  4234. bool cmMakefile::SetPolicyVersion(const char *version)
  4235. {
  4236. return this->GetCMakeInstance()->GetPolicies()->
  4237. ApplyPolicyVersion(this,version);
  4238. }
  4239. cmPolicies *cmMakefile::GetPolicies()
  4240. {
  4241. if (!this->GetCMakeInstance())
  4242. {
  4243. return 0;
  4244. }
  4245. return this->GetCMakeInstance()->GetPolicies();
  4246. }
  4247. //----------------------------------------------------------------------------
  4248. void cmMakefile::RecordPolicies(cmPolicies::PolicyMap& pm)
  4249. {
  4250. /* Record the setting of every policy. */
  4251. typedef cmPolicies::PolicyID PolicyID;
  4252. for(PolicyID pid = cmPolicies::CMP0000;
  4253. pid != cmPolicies::CMPCOUNT; pid = PolicyID(pid+1))
  4254. {
  4255. pm[pid] = this->GetPolicyStatus(pid);
  4256. }
  4257. }