cmMakefile.cxx 158 KB

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