cmMakefile.cxx 158 KB

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