cmMakefile.cxx 158 KB

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