cmMakefile.cxx 168 KB

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