cmMakefile.cxx 170 KB

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