cmMakefile.cxx 139 KB

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