cmMakefile.cxx 161 KB

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