cmMakefile.cxx 167 KB

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