cmMakefile.cxx 133 KB

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