cmMakefile.cxx 157 KB

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