cmMakefile.cxx 170 KB

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