cmMakefile.cxx 157 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172
  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. if ( def )
  2257. {
  2258. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS,
  2259. def, this);
  2260. }
  2261. else
  2262. {
  2263. vv->VariableAccessed(name,
  2264. cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2265. }
  2266. }
  2267. #endif
  2268. return def;
  2269. }
  2270. const char* cmMakefile::GetSafeDefinition(const std::string& def) const
  2271. {
  2272. const char* ret = this->GetDefinition(def);
  2273. if(!ret)
  2274. {
  2275. return "";
  2276. }
  2277. return ret;
  2278. }
  2279. std::vector<std::string> cmMakefile::GetDefinitions() const
  2280. {
  2281. std::vector<std::string> res = this->StateSnapshot.ClosureKeys();
  2282. std::vector<std::string> cacheKeys = this->GetState()->GetCacheEntryKeys();
  2283. res.insert(res.end(), cacheKeys.begin(), cacheKeys.end());
  2284. std::sort(res.begin(), res.end());
  2285. return res;
  2286. }
  2287. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  2288. {
  2289. return this->ExpandVariablesInString(source, false, false);
  2290. }
  2291. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  2292. bool escapeQuotes,
  2293. bool noEscapes,
  2294. bool atOnly,
  2295. const char* filename,
  2296. long line,
  2297. bool removeEmpty,
  2298. bool replaceAt) const
  2299. {
  2300. bool compareResults = false;
  2301. cmake::MessageType mtype = cmake::LOG;
  2302. std::string errorstr;
  2303. std::string original;
  2304. // Sanity check the @ONLY mode.
  2305. if(atOnly && (!noEscapes || !removeEmpty))
  2306. {
  2307. // This case should never be called. At-only is for
  2308. // configure-file/string which always does no escapes.
  2309. this->IssueMessage(cmake::INTERNAL_ERROR,
  2310. "ExpandVariablesInString @ONLY called "
  2311. "on something with escapes.");
  2312. return source.c_str();
  2313. }
  2314. // Variables used in the WARN case.
  2315. std::string newResult;
  2316. std::string newErrorstr;
  2317. cmake::MessageType newError = cmake::LOG;
  2318. switch(this->GetPolicyStatus(cmPolicies::CMP0053))
  2319. {
  2320. case cmPolicies::WARN:
  2321. {
  2322. // Save the original string for the warning.
  2323. original = source;
  2324. newResult = source;
  2325. compareResults = true;
  2326. // Suppress variable watches to avoid calling hooks twice. Suppress new
  2327. // dereferences since the OLD behavior is still what is actually used.
  2328. this->SuppressWatches = true;
  2329. newError =
  2330. ExpandVariablesInStringNew(newErrorstr, newResult, escapeQuotes,
  2331. noEscapes, atOnly, filename, line,
  2332. removeEmpty, replaceAt);
  2333. this->SuppressWatches = false;
  2334. }
  2335. case cmPolicies::OLD:
  2336. mtype = ExpandVariablesInStringOld(errorstr, source, escapeQuotes,
  2337. noEscapes, atOnly, filename,
  2338. line, removeEmpty, true);
  2339. break;
  2340. case cmPolicies::REQUIRED_IF_USED:
  2341. case cmPolicies::REQUIRED_ALWAYS:
  2342. // Messaging here would be *very* verbose.
  2343. case cmPolicies::NEW:
  2344. mtype = ExpandVariablesInStringNew(errorstr, source, escapeQuotes,
  2345. noEscapes, atOnly, filename,
  2346. line, removeEmpty, replaceAt);
  2347. break;
  2348. }
  2349. // If it's an error in either case, just report the error...
  2350. if(mtype != cmake::LOG)
  2351. {
  2352. if(mtype == cmake::FATAL_ERROR)
  2353. {
  2354. cmSystemTools::SetFatalErrorOccured();
  2355. }
  2356. this->IssueMessage(mtype, errorstr);
  2357. }
  2358. // ...otherwise, see if there's a difference that needs to be warned about.
  2359. else if(compareResults && (newResult != source || newError != mtype))
  2360. {
  2361. std::string msg =
  2362. cmPolicies::GetPolicyWarning(cmPolicies::CMP0053);
  2363. msg += "\n";
  2364. std::string msg_input = original;
  2365. cmSystemTools::ReplaceString(msg_input, "\n", "\n ");
  2366. msg += "For input:\n '";
  2367. msg += msg_input;
  2368. msg += "'\n";
  2369. std::string msg_old = source;
  2370. cmSystemTools::ReplaceString(msg_old, "\n", "\n ");
  2371. msg += "the old evaluation rules produce:\n '";
  2372. msg += msg_old;
  2373. msg += "'\n";
  2374. if(newError == mtype)
  2375. {
  2376. std::string msg_new = newResult;
  2377. cmSystemTools::ReplaceString(msg_new, "\n", "\n ");
  2378. msg += "but the new evaluation rules produce:\n '";
  2379. msg += msg_new;
  2380. msg += "'\n";
  2381. }
  2382. else
  2383. {
  2384. std::string msg_err = newErrorstr;
  2385. cmSystemTools::ReplaceString(msg_err, "\n", "\n ");
  2386. msg += "but the new evaluation rules produce an error:\n ";
  2387. msg += msg_err;
  2388. msg += "\n";
  2389. }
  2390. msg +=
  2391. "Using the old result for compatibility since the policy is not set.";
  2392. this->IssueMessage(cmake::AUTHOR_WARNING, msg);
  2393. }
  2394. return source.c_str();
  2395. }
  2396. cmake::MessageType cmMakefile::ExpandVariablesInStringOld(
  2397. std::string& errorstr,
  2398. std::string& source,
  2399. bool escapeQuotes,
  2400. bool noEscapes,
  2401. bool atOnly,
  2402. const char* filename,
  2403. long line,
  2404. bool removeEmpty,
  2405. bool replaceAt) const
  2406. {
  2407. // Fast path strings without any special characters.
  2408. if ( source.find_first_of("$@\\") == source.npos)
  2409. {
  2410. return cmake::LOG;
  2411. }
  2412. // Special-case the @ONLY mode.
  2413. if(atOnly)
  2414. {
  2415. // Store an original copy of the input.
  2416. std::string input = source;
  2417. // Start with empty output.
  2418. source = "";
  2419. // Look for one @VAR@ at a time.
  2420. const char* in = input.c_str();
  2421. while(this->cmAtVarRegex.find(in))
  2422. {
  2423. // Get the range of the string to replace.
  2424. const char* first = in + this->cmAtVarRegex.start();
  2425. const char* last = in + this->cmAtVarRegex.end();
  2426. // Store the unchanged part of the string now.
  2427. source.append(in, first-in);
  2428. // Lookup the definition of VAR.
  2429. std::string var(first+1, last-first-2);
  2430. if(const char* val = this->GetDefinition(var))
  2431. {
  2432. // Store the value in the output escaping as requested.
  2433. if(escapeQuotes)
  2434. {
  2435. source.append(cmSystemTools::EscapeQuotes(val));
  2436. }
  2437. else
  2438. {
  2439. source.append(val);
  2440. }
  2441. }
  2442. // Continue looking for @VAR@ further along the string.
  2443. in = last;
  2444. }
  2445. // Append the rest of the unchanged part of the string.
  2446. source.append(in);
  2447. return cmake::LOG;
  2448. }
  2449. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2450. // with GetDefinition(), if not found in the map, nothing is expanded.
  2451. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2452. // the current environment variables.
  2453. cmCommandArgumentParserHelper parser;
  2454. parser.SetMakefile(this);
  2455. parser.SetLineFile(line, filename);
  2456. parser.SetEscapeQuotes(escapeQuotes);
  2457. parser.SetNoEscapeMode(noEscapes);
  2458. parser.SetReplaceAtSyntax(replaceAt);
  2459. parser.SetRemoveEmpty(removeEmpty);
  2460. int res = parser.ParseString(source.c_str(), 0);
  2461. const char* emsg = parser.GetError();
  2462. cmake::MessageType mtype = cmake::LOG;
  2463. if ( res && !emsg[0] )
  2464. {
  2465. source = parser.GetResult();
  2466. }
  2467. else
  2468. {
  2469. // Construct the main error message.
  2470. std::ostringstream error;
  2471. error << "Syntax error in cmake code ";
  2472. if(filename && line > 0)
  2473. {
  2474. // This filename and line number may be more specific than the
  2475. // command context because one command invocation can have
  2476. // arguments on multiple lines.
  2477. error << "at\n"
  2478. << " " << filename << ":" << line << "\n";
  2479. }
  2480. error << "when parsing string\n"
  2481. << " " << source << "\n";
  2482. error << emsg;
  2483. // If the parser failed ("res" is false) then this is a real
  2484. // argument parsing error, so the policy applies. Otherwise the
  2485. // parser reported an error message without failing because the
  2486. // helper implementation is unhappy, which has always reported an
  2487. // error.
  2488. mtype = cmake::FATAL_ERROR;
  2489. if(!res)
  2490. {
  2491. // This is a real argument parsing error. Use policy CMP0010 to
  2492. // decide whether it is an error.
  2493. switch(this->GetPolicyStatus(cmPolicies::CMP0010))
  2494. {
  2495. case cmPolicies::WARN:
  2496. error << "\n" << cmPolicies::GetPolicyWarning(cmPolicies::CMP0010);
  2497. case cmPolicies::OLD:
  2498. // OLD behavior is to just warn and continue.
  2499. mtype = cmake::AUTHOR_WARNING;
  2500. break;
  2501. case cmPolicies::REQUIRED_IF_USED:
  2502. case cmPolicies::REQUIRED_ALWAYS:
  2503. error << "\n"
  2504. << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0010);
  2505. case cmPolicies::NEW:
  2506. // NEW behavior is to report the error.
  2507. break;
  2508. }
  2509. }
  2510. errorstr = error.str();
  2511. }
  2512. return mtype;
  2513. }
  2514. typedef enum
  2515. {
  2516. NORMAL,
  2517. ENVIRONMENT,
  2518. CACHE
  2519. } t_domain;
  2520. struct t_lookup
  2521. {
  2522. t_lookup(): domain(NORMAL), loc(0) {}
  2523. t_domain domain;
  2524. size_t loc;
  2525. };
  2526. cmake::MessageType cmMakefile::ExpandVariablesInStringNew(
  2527. std::string& errorstr,
  2528. std::string& source,
  2529. bool escapeQuotes,
  2530. bool noEscapes,
  2531. bool atOnly,
  2532. const char* filename,
  2533. long line,
  2534. bool removeEmpty,
  2535. bool replaceAt) const
  2536. {
  2537. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2538. // with GetDefinition(), if not found in the map, nothing is expanded.
  2539. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2540. // the current environment variables.
  2541. const char* in = source.c_str();
  2542. const char* last = in;
  2543. std::string result;
  2544. result.reserve(source.size());
  2545. std::vector<t_lookup> openstack;
  2546. bool error = false;
  2547. bool done = false;
  2548. cmake::MessageType mtype = cmake::LOG;
  2549. cmState* state = this->GetCMakeInstance()->GetState();
  2550. do
  2551. {
  2552. char inc = *in;
  2553. switch(inc)
  2554. {
  2555. case '}':
  2556. if(!openstack.empty())
  2557. {
  2558. t_lookup var = openstack.back();
  2559. openstack.pop_back();
  2560. result.append(last, in - last);
  2561. std::string const& lookup = result.substr(var.loc);
  2562. const char* value = NULL;
  2563. std::string varresult;
  2564. static const std::string lineVar = "CMAKE_CURRENT_LIST_LINE";
  2565. switch(var.domain)
  2566. {
  2567. case NORMAL:
  2568. if(filename && lookup == lineVar)
  2569. {
  2570. std::ostringstream ostr;
  2571. ostr << line;
  2572. varresult = ostr.str();
  2573. }
  2574. else
  2575. {
  2576. value = this->GetDefinition(lookup);
  2577. }
  2578. break;
  2579. case ENVIRONMENT:
  2580. value = cmSystemTools::GetEnv(lookup.c_str());
  2581. break;
  2582. case CACHE:
  2583. value = state->GetCacheEntryValue(lookup);
  2584. break;
  2585. }
  2586. // Get the string we're meant to append to.
  2587. if(value)
  2588. {
  2589. if(escapeQuotes)
  2590. {
  2591. varresult = cmSystemTools::EscapeQuotes(value);
  2592. }
  2593. else
  2594. {
  2595. varresult = value;
  2596. }
  2597. }
  2598. else if(!removeEmpty)
  2599. {
  2600. // check to see if we need to print a warning
  2601. // if strict mode is on and the variable has
  2602. // not been "cleared"/initialized with a set(foo ) call
  2603. if(this->GetCMakeInstance()->GetWarnUninitialized() &&
  2604. !this->VariableInitialized(lookup))
  2605. {
  2606. if (this->CheckSystemVars ||
  2607. cmSystemTools::IsSubDirectory(filename,
  2608. this->GetHomeDirectory()) ||
  2609. cmSystemTools::IsSubDirectory(filename,
  2610. this->GetHomeOutputDirectory()))
  2611. {
  2612. std::ostringstream msg;
  2613. cmListFileContext lfc;
  2614. cmOutputConverter converter(this->StateSnapshot);
  2615. lfc.FilePath =
  2616. converter.Convert(filename, cmOutputConverter::HOME);
  2617. lfc.Line = line;
  2618. msg << "uninitialized variable \'" << lookup << "\'";
  2619. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
  2620. msg.str(), lfc);
  2621. }
  2622. }
  2623. }
  2624. result.replace(var.loc, result.size() - var.loc, varresult);
  2625. // Start looking from here on out.
  2626. last = in + 1;
  2627. }
  2628. break;
  2629. case '$':
  2630. if(!atOnly)
  2631. {
  2632. t_lookup lookup;
  2633. const char* next = in + 1;
  2634. const char* start = NULL;
  2635. char nextc = *next;
  2636. if(nextc == '{')
  2637. {
  2638. // Looking for a variable.
  2639. start = in + 2;
  2640. lookup.domain = NORMAL;
  2641. }
  2642. else if(nextc == '<')
  2643. {
  2644. }
  2645. else if(!nextc)
  2646. {
  2647. result.append(last, next - last);
  2648. last = next;
  2649. }
  2650. else if(cmHasLiteralPrefix(next, "ENV{"))
  2651. {
  2652. // Looking for an environment variable.
  2653. start = in + 5;
  2654. lookup.domain = ENVIRONMENT;
  2655. }
  2656. else if(cmHasLiteralPrefix(next, "CACHE{"))
  2657. {
  2658. // Looking for a cache variable.
  2659. start = in + 7;
  2660. lookup.domain = CACHE;
  2661. }
  2662. else
  2663. {
  2664. if(this->cmNamedCurly.find(next))
  2665. {
  2666. errorstr = "Syntax $"
  2667. + std::string(next, this->cmNamedCurly.end())
  2668. + "{} is not supported. Only ${}, $ENV{}, "
  2669. "and $CACHE{} are allowed.";
  2670. mtype = cmake::FATAL_ERROR;
  2671. error = true;
  2672. }
  2673. }
  2674. if(start)
  2675. {
  2676. result.append(last, in - last);
  2677. last = start;
  2678. in = start - 1;
  2679. lookup.loc = result.size();
  2680. openstack.push_back(lookup);
  2681. }
  2682. break;
  2683. }
  2684. case '\\':
  2685. if(!noEscapes)
  2686. {
  2687. const char* next = in + 1;
  2688. char nextc = *next;
  2689. if(nextc == 't')
  2690. {
  2691. result.append(last, in - last);
  2692. result.append("\t");
  2693. last = next + 1;
  2694. }
  2695. else if(nextc == 'n')
  2696. {
  2697. result.append(last, in - last);
  2698. result.append("\n");
  2699. last = next + 1;
  2700. }
  2701. else if(nextc == 'r')
  2702. {
  2703. result.append(last, in - last);
  2704. result.append("\r");
  2705. last = next + 1;
  2706. }
  2707. else if(nextc == ';' && openstack.empty())
  2708. {
  2709. // Handled in ExpandListArgument; pass the backslash literally.
  2710. }
  2711. else if (isalnum(nextc) || nextc == '\0')
  2712. {
  2713. errorstr += "Invalid character escape '\\";
  2714. if (nextc)
  2715. {
  2716. errorstr += nextc;
  2717. errorstr += "'.";
  2718. }
  2719. else
  2720. {
  2721. errorstr += "' (at end of input).";
  2722. }
  2723. error = true;
  2724. }
  2725. else
  2726. {
  2727. // Take what we've found so far, skipping the escape character.
  2728. result.append(last, in - last);
  2729. // Start tracking from the next character.
  2730. last = in + 1;
  2731. }
  2732. // Skip the next character since it was escaped, but don't read past
  2733. // the end of the string.
  2734. if(*last)
  2735. {
  2736. ++in;
  2737. }
  2738. }
  2739. break;
  2740. case '\n':
  2741. // Onto the next line.
  2742. ++line;
  2743. break;
  2744. case '\0':
  2745. done = true;
  2746. break;
  2747. case '@':
  2748. if(replaceAt)
  2749. {
  2750. const char* nextAt = strchr(in + 1, '@');
  2751. if(nextAt && nextAt != in + 1 &&
  2752. nextAt == in + 1 + strspn(in + 1,
  2753. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2754. "abcdefghijklmnopqrstuvwxyz"
  2755. "0123456789/_.+-"))
  2756. {
  2757. std::string variable(in + 1, nextAt - in - 1);
  2758. std::string varresult = this->GetSafeDefinition(variable);
  2759. if(escapeQuotes)
  2760. {
  2761. varresult = cmSystemTools::EscapeQuotes(varresult);
  2762. }
  2763. // Skip over the variable.
  2764. result.append(last, in - last);
  2765. result.append(varresult);
  2766. in = nextAt;
  2767. last = in + 1;
  2768. break;
  2769. }
  2770. }
  2771. // Failed to find a valid @ expansion; treat it as literal.
  2772. /* FALLTHROUGH */
  2773. default:
  2774. {
  2775. if(!openstack.empty() &&
  2776. !(isalnum(inc) || inc == '_' ||
  2777. inc == '/' || inc == '.' ||
  2778. inc == '+' || inc == '-'))
  2779. {
  2780. errorstr += "Invalid character (\'";
  2781. errorstr += inc;
  2782. result.append(last, in - last);
  2783. errorstr += "\') in a variable name: "
  2784. "'" + result.substr(openstack.back().loc) + "'";
  2785. mtype = cmake::FATAL_ERROR;
  2786. error = true;
  2787. }
  2788. break;
  2789. }
  2790. }
  2791. // Look at the next character.
  2792. } while(!error && !done && *++in);
  2793. // Check for open variable references yet.
  2794. if(!error && !openstack.empty())
  2795. {
  2796. // There's an open variable reference waiting. Policy CMP0010 flags
  2797. // whether this is an error or not. The new parser now enforces
  2798. // CMP0010 as well.
  2799. errorstr += "There is an unterminated variable reference.";
  2800. error = true;
  2801. }
  2802. if(error)
  2803. {
  2804. std::ostringstream emsg;
  2805. emsg << "Syntax error in cmake code ";
  2806. if(filename)
  2807. {
  2808. // This filename and line number may be more specific than the
  2809. // command context because one command invocation can have
  2810. // arguments on multiple lines.
  2811. emsg << "at\n"
  2812. << " " << filename << ":" << line << "\n";
  2813. }
  2814. emsg << "when parsing string\n"
  2815. << " " << source << "\n";
  2816. emsg << errorstr;
  2817. mtype = cmake::FATAL_ERROR;
  2818. errorstr = emsg.str();
  2819. }
  2820. else
  2821. {
  2822. // Append the rest of the unchanged part of the string.
  2823. result.append(last);
  2824. source = result;
  2825. }
  2826. return mtype;
  2827. }
  2828. void cmMakefile::RemoveVariablesInString(std::string& source,
  2829. bool atOnly) const
  2830. {
  2831. if(!atOnly)
  2832. {
  2833. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  2834. while (var.find(source))
  2835. {
  2836. source.erase(var.start(),var.end() - var.start());
  2837. }
  2838. }
  2839. if(!atOnly)
  2840. {
  2841. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  2842. while (varb.find(source))
  2843. {
  2844. source.erase(varb.start(),varb.end() - varb.start());
  2845. }
  2846. }
  2847. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  2848. while (var2.find(source))
  2849. {
  2850. source.erase(var2.start(),var2.end() - var2.start());
  2851. }
  2852. }
  2853. //----------------------------------------------------------------------------
  2854. std::string
  2855. cmMakefile::GetConfigurations(std::vector<std::string>& configs,
  2856. bool singleConfig) const
  2857. {
  2858. if(this->GetGlobalGenerator()->IsMultiConfig())
  2859. {
  2860. if(const char* configTypes =
  2861. this->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  2862. {
  2863. cmSystemTools::ExpandListArgument(configTypes, configs);
  2864. }
  2865. return "";
  2866. }
  2867. else
  2868. {
  2869. const std::string& buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2870. if(singleConfig && !buildType.empty())
  2871. {
  2872. configs.push_back(buildType);
  2873. }
  2874. return buildType;
  2875. }
  2876. }
  2877. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2878. /**
  2879. * Find a source group whose regular expression matches the filename
  2880. * part of the given source name. Search backward through the list of
  2881. * source groups, and take the first matching group found. This way
  2882. * non-inherited SOURCE_GROUP commands will have precedence over
  2883. * inherited ones.
  2884. */
  2885. cmSourceGroup*
  2886. cmMakefile::FindSourceGroup(const char* source,
  2887. std::vector<cmSourceGroup> &groups) const
  2888. {
  2889. // First search for a group that lists the file explicitly.
  2890. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2891. sg != groups.rend(); ++sg)
  2892. {
  2893. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  2894. if(result)
  2895. {
  2896. return result;
  2897. }
  2898. }
  2899. // Now search for a group whose regex matches the file.
  2900. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2901. sg != groups.rend(); ++sg)
  2902. {
  2903. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  2904. if(result)
  2905. {
  2906. return result;
  2907. }
  2908. }
  2909. // Shouldn't get here, but just in case, return the default group.
  2910. return &groups.front();
  2911. }
  2912. #endif
  2913. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff,
  2914. cmExecutionStatus &status)
  2915. {
  2916. // if there are no blockers get out of here
  2917. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  2918. {
  2919. return false;
  2920. }
  2921. // loop over all function blockers to see if any block this command
  2922. // evaluate in reverse, this is critical for balanced IF statements etc
  2923. std::vector<cmFunctionBlocker*>::reverse_iterator pos;
  2924. for (pos = this->FunctionBlockers.rbegin();
  2925. pos != this->FunctionBlockers.rend(); ++pos)
  2926. {
  2927. if((*pos)->IsFunctionBlocked(lff, *this, status))
  2928. {
  2929. return true;
  2930. }
  2931. }
  2932. return false;
  2933. }
  2934. //----------------------------------------------------------------------------
  2935. void cmMakefile::PushFunctionBlockerBarrier()
  2936. {
  2937. this->FunctionBlockerBarriers.push_back(this->FunctionBlockers.size());
  2938. }
  2939. //----------------------------------------------------------------------------
  2940. void cmMakefile::PopFunctionBlockerBarrier(bool reportError)
  2941. {
  2942. // Remove any extra entries pushed on the barrier.
  2943. FunctionBlockersType::size_type barrier =
  2944. this->FunctionBlockerBarriers.back();
  2945. while(this->FunctionBlockers.size() > barrier)
  2946. {
  2947. cmsys::auto_ptr<cmFunctionBlocker> fb(this->FunctionBlockers.back());
  2948. this->FunctionBlockers.pop_back();
  2949. if(reportError)
  2950. {
  2951. // Report the context in which the unclosed block was opened.
  2952. cmListFileContext const& lfc = fb->GetStartingContext();
  2953. std::ostringstream e;
  2954. e << "A logical block opening on the line\n"
  2955. << " " << lfc << "\n"
  2956. << "is not closed.";
  2957. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  2958. reportError = false;
  2959. }
  2960. }
  2961. // Remove the barrier.
  2962. this->FunctionBlockerBarriers.pop_back();
  2963. }
  2964. //----------------------------------------------------------------------------
  2965. void cmMakefile::PushLoopBlock()
  2966. {
  2967. assert(!this->LoopBlockCounter.empty());
  2968. this->LoopBlockCounter.top()++;
  2969. }
  2970. void cmMakefile::PopLoopBlock()
  2971. {
  2972. assert(!this->LoopBlockCounter.empty());
  2973. assert(this->LoopBlockCounter.top() > 0);
  2974. this->LoopBlockCounter.top()--;
  2975. }
  2976. void cmMakefile::PushLoopBlockBarrier()
  2977. {
  2978. this->LoopBlockCounter.push(0);
  2979. }
  2980. void cmMakefile::PopLoopBlockBarrier()
  2981. {
  2982. assert(!this->LoopBlockCounter.empty());
  2983. assert(this->LoopBlockCounter.top() == 0);
  2984. this->LoopBlockCounter.pop();
  2985. }
  2986. bool cmMakefile::IsLoopBlock() const
  2987. {
  2988. assert(!this->LoopBlockCounter.empty());
  2989. return !this->LoopBlockCounter.empty() && this->LoopBlockCounter.top() > 0;
  2990. }
  2991. std::string cmMakefile::GetExecutionFilePath() const
  2992. {
  2993. assert(this->StateSnapshot.IsValid());
  2994. return this->StateSnapshot.GetExecutionListFile();
  2995. }
  2996. //----------------------------------------------------------------------------
  2997. bool cmMakefile::ExpandArguments(
  2998. std::vector<cmListFileArgument> const& inArgs,
  2999. std::vector<std::string>& outArgs, const char* filename) const
  3000. {
  3001. std::string efp = this->GetExecutionFilePath();
  3002. if (!filename)
  3003. {
  3004. filename = efp.c_str();
  3005. }
  3006. std::vector<cmListFileArgument>::const_iterator i;
  3007. std::string value;
  3008. outArgs.reserve(inArgs.size());
  3009. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  3010. {
  3011. // No expansion in a bracket argument.
  3012. if(i->Delim == cmListFileArgument::Bracket)
  3013. {
  3014. outArgs.push_back(i->Value);
  3015. continue;
  3016. }
  3017. // Expand the variables in the argument.
  3018. value = i->Value;
  3019. this->ExpandVariablesInString(value, false, false, false,
  3020. filename, i->Line, false, false);
  3021. // If the argument is quoted, it should be one argument.
  3022. // Otherwise, it may be a list of arguments.
  3023. if(i->Delim == cmListFileArgument::Quoted)
  3024. {
  3025. outArgs.push_back(value);
  3026. }
  3027. else
  3028. {
  3029. cmSystemTools::ExpandListArgument(value, outArgs);
  3030. }
  3031. }
  3032. return !cmSystemTools::GetFatalErrorOccured();
  3033. }
  3034. //----------------------------------------------------------------------------
  3035. bool cmMakefile::ExpandArguments(
  3036. std::vector<cmListFileArgument> const& inArgs,
  3037. std::vector<cmExpandedCommandArgument>& outArgs, const char* filename) const
  3038. {
  3039. std::string efp = this->GetExecutionFilePath();
  3040. if (!filename)
  3041. {
  3042. filename = efp.c_str();
  3043. }
  3044. std::vector<cmListFileArgument>::const_iterator i;
  3045. std::string value;
  3046. outArgs.reserve(inArgs.size());
  3047. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  3048. {
  3049. // No expansion in a bracket argument.
  3050. if(i->Delim == cmListFileArgument::Bracket)
  3051. {
  3052. outArgs.push_back(cmExpandedCommandArgument(i->Value, true));
  3053. continue;
  3054. }
  3055. // Expand the variables in the argument.
  3056. value = i->Value;
  3057. this->ExpandVariablesInString(value, false, false, false,
  3058. filename, i->Line, false, false);
  3059. // If the argument is quoted, it should be one argument.
  3060. // Otherwise, it may be a list of arguments.
  3061. if(i->Delim == cmListFileArgument::Quoted)
  3062. {
  3063. outArgs.push_back(cmExpandedCommandArgument(value, true));
  3064. }
  3065. else
  3066. {
  3067. std::vector<std::string> stringArgs;
  3068. cmSystemTools::ExpandListArgument(value, stringArgs);
  3069. for(size_t j = 0; j < stringArgs.size(); ++j)
  3070. {
  3071. outArgs.push_back(cmExpandedCommandArgument(stringArgs[j], false));
  3072. }
  3073. }
  3074. }
  3075. return !cmSystemTools::GetFatalErrorOccured();
  3076. }
  3077. //----------------------------------------------------------------------------
  3078. void cmMakefile::AddFunctionBlocker(cmFunctionBlocker* fb)
  3079. {
  3080. if(!this->ExecutionStatusStack.empty())
  3081. {
  3082. // Record the context in which the blocker is created.
  3083. fb->SetStartingContext(this->GetExecutionContext());
  3084. }
  3085. this->FunctionBlockers.push_back(fb);
  3086. }
  3087. cmsys::auto_ptr<cmFunctionBlocker>
  3088. cmMakefile::RemoveFunctionBlocker(cmFunctionBlocker* fb,
  3089. const cmListFileFunction& lff)
  3090. {
  3091. // Find the function blocker stack barrier for the current scope.
  3092. // We only remove a blocker whose index is not less than the barrier.
  3093. FunctionBlockersType::size_type barrier = 0;
  3094. if(!this->FunctionBlockerBarriers.empty())
  3095. {
  3096. barrier = this->FunctionBlockerBarriers.back();
  3097. }
  3098. // Search for the function blocker whose scope this command ends.
  3099. for(FunctionBlockersType::size_type
  3100. i = this->FunctionBlockers.size(); i > barrier; --i)
  3101. {
  3102. std::vector<cmFunctionBlocker*>::iterator pos =
  3103. this->FunctionBlockers.begin() + (i - 1);
  3104. if (*pos == fb)
  3105. {
  3106. // Warn if the arguments do not match, but always remove.
  3107. if(!(*pos)->ShouldRemove(lff, *this))
  3108. {
  3109. cmListFileContext const& lfc = fb->GetStartingContext();
  3110. cmListFileContext closingContext =
  3111. cmListFileContext::FromCommandContext(lff, lfc.FilePath);
  3112. std::ostringstream e;
  3113. e << "A logical block opening on the line\n"
  3114. << " " << lfc << "\n"
  3115. << "closes on the line\n"
  3116. << " " << closingContext << "\n"
  3117. << "with mis-matching arguments.";
  3118. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3119. }
  3120. cmFunctionBlocker* b = *pos;
  3121. this->FunctionBlockers.erase(pos);
  3122. return cmsys::auto_ptr<cmFunctionBlocker>(b);
  3123. }
  3124. }
  3125. return cmsys::auto_ptr<cmFunctionBlocker>();
  3126. }
  3127. const char* cmMakefile::GetHomeDirectory() const
  3128. {
  3129. return this->GetCMakeInstance()->GetHomeDirectory();
  3130. }
  3131. const char* cmMakefile::GetHomeOutputDirectory() const
  3132. {
  3133. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  3134. }
  3135. void cmMakefile::SetScriptModeFile(const char* scriptfile)
  3136. {
  3137. this->AddDefinition("CMAKE_SCRIPT_MODE_FILE", scriptfile);
  3138. }
  3139. void cmMakefile::SetArgcArgv(const std::vector<std::string>& args)
  3140. {
  3141. std::ostringstream strStream;
  3142. strStream << args.size();
  3143. this->AddDefinition("CMAKE_ARGC", strStream.str().c_str());
  3144. //this->MarkVariableAsUsed("CMAKE_ARGC");
  3145. for (unsigned int t = 0; t < args.size(); ++t)
  3146. {
  3147. std::ostringstream tmpStream;
  3148. tmpStream << "CMAKE_ARGV" << t;
  3149. this->AddDefinition(tmpStream.str(), args[t].c_str());
  3150. //this->MarkVariableAsUsed(tmpStream.str().c_str());
  3151. }
  3152. }
  3153. //----------------------------------------------------------------------------
  3154. cmSourceFile* cmMakefile::GetSource(const std::string& sourceName) const
  3155. {
  3156. cmSourceFileLocation sfl(this, sourceName);
  3157. for(std::vector<cmSourceFile*>::const_iterator
  3158. sfi = this->SourceFiles.begin();
  3159. sfi != this->SourceFiles.end(); ++sfi)
  3160. {
  3161. cmSourceFile* sf = *sfi;
  3162. if(sf->Matches(sfl))
  3163. {
  3164. return sf;
  3165. }
  3166. }
  3167. return 0;
  3168. }
  3169. //----------------------------------------------------------------------------
  3170. cmSourceFile* cmMakefile::CreateSource(const std::string& sourceName,
  3171. bool generated)
  3172. {
  3173. cmSourceFile* sf = new cmSourceFile(this, sourceName);
  3174. if(generated)
  3175. {
  3176. sf->SetProperty("GENERATED", "1");
  3177. }
  3178. this->SourceFiles.push_back(sf);
  3179. return sf;
  3180. }
  3181. //----------------------------------------------------------------------------
  3182. cmSourceFile* cmMakefile::GetOrCreateSource(const std::string& sourceName,
  3183. bool generated)
  3184. {
  3185. if(cmSourceFile* esf = this->GetSource(sourceName))
  3186. {
  3187. return esf;
  3188. }
  3189. else
  3190. {
  3191. return this->CreateSource(sourceName, generated);
  3192. }
  3193. }
  3194. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang,
  3195. bool optional)
  3196. {
  3197. this->AddDefinition("CMAKE_CFG_INTDIR",
  3198. this->GetGlobalGenerator()->GetCMakeCFGIntDir());
  3199. this->GetGlobalGenerator()->EnableLanguage(lang, this, optional);
  3200. }
  3201. int cmMakefile::TryCompile(const std::string& srcdir,
  3202. const std::string& bindir,
  3203. const std::string& projectName,
  3204. const std::string& targetName,
  3205. bool fast,
  3206. const std::vector<std::string> *cmakeArgs,
  3207. std::string& output)
  3208. {
  3209. this->IsSourceFileTryCompile = fast;
  3210. // does the binary directory exist ? If not create it...
  3211. if (!cmSystemTools::FileIsDirectory(bindir))
  3212. {
  3213. cmSystemTools::MakeDirectory(bindir.c_str());
  3214. }
  3215. // change to the tests directory and run cmake
  3216. // use the cmake object instead of calling cmake
  3217. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  3218. cmSystemTools::ChangeDirectory(bindir);
  3219. // make sure the same generator is used
  3220. // use this program as the cmake to be run, it should not
  3221. // be run that way but the cmake object requires a vailid path
  3222. cmake cm;
  3223. cm.SetIsInTryCompile(true);
  3224. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  3225. (this->GetGlobalGenerator()->GetName());
  3226. if (!gg)
  3227. {
  3228. cmSystemTools::Error(
  3229. "Internal CMake error, TryCompile bad GlobalGenerator");
  3230. // return to the original directory
  3231. cmSystemTools::ChangeDirectory(cwd);
  3232. this->IsSourceFileTryCompile = false;
  3233. return 1;
  3234. }
  3235. cm.SetGlobalGenerator(gg);
  3236. // do a configure
  3237. cm.SetHomeDirectory(srcdir);
  3238. cm.SetHomeOutputDirectory(bindir);
  3239. cm.SetGeneratorPlatform(this->GetCMakeInstance()->GetGeneratorPlatform());
  3240. cm.SetGeneratorToolset(this->GetCMakeInstance()->GetGeneratorToolset());
  3241. cm.LoadCache();
  3242. if(!gg->IsMultiConfig())
  3243. {
  3244. if(const char* config =
  3245. this->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION"))
  3246. {
  3247. // Tell the single-configuration generator which one to use.
  3248. // Add this before the user-provided CMake arguments in case
  3249. // one of the arguments is -DCMAKE_BUILD_TYPE=...
  3250. cm.AddCacheEntry("CMAKE_BUILD_TYPE", config,
  3251. "Build configuration", cmState::STRING);
  3252. }
  3253. }
  3254. // if cmake args were provided then pass them in
  3255. if (cmakeArgs)
  3256. {
  3257. // FIXME: Workaround to ignore unused CLI variables in try-compile.
  3258. //
  3259. // Ideally we should use SetArgs to honor options like --warn-unused-vars.
  3260. // However, there is a subtle problem when certain arguments are passed to
  3261. // a macro wrapping around try_compile or try_run that does not escape
  3262. // semicolons in its parameters but just passes ${ARGV} or ${ARGN}. In
  3263. // this case a list argument like "-DVAR=a;b" gets split into multiple
  3264. // cmake arguments "-DVAR=a" and "b". Currently SetCacheArgs ignores
  3265. // argument "b" and uses just "-DVAR=a", leading to a subtle bug in that
  3266. // the try_compile or try_run does not get the proper value of VAR. If we
  3267. // call SetArgs here then it would treat "b" as the source directory and
  3268. // cause an error such as "The source directory .../CMakeFiles/CMakeTmp/b
  3269. // does not exist", thus breaking the try_compile or try_run completely.
  3270. //
  3271. // Strictly speaking the bug is in the wrapper macro because the CMake
  3272. // language has always flattened nested lists and the macro should escape
  3273. // the semicolons in its arguments before forwarding them. However, this
  3274. // bug is so subtle that projects typically work anyway, usually because
  3275. // the value VAR=a is sufficient for the try_compile or try_run to get the
  3276. // correct result. Calling SetArgs here would break such projects that
  3277. // previously built. Instead we work around the issue by never reporting
  3278. // unused arguments and ignoring options such as --warn-unused-vars.
  3279. cm.SetWarnUnusedCli(false);
  3280. //cm.SetArgs(*cmakeArgs, true);
  3281. cm.SetCacheArgs(*cmakeArgs);
  3282. }
  3283. // to save time we pass the EnableLanguage info directly
  3284. gg->EnableLanguagesFromGenerator(this->GetGlobalGenerator(), this);
  3285. if(this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS"))
  3286. {
  3287. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  3288. "TRUE", "", cmState::INTERNAL);
  3289. }
  3290. else
  3291. {
  3292. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  3293. "FALSE", "", cmState::INTERNAL);
  3294. }
  3295. if (cm.Configure() != 0)
  3296. {
  3297. cmSystemTools::Error(
  3298. "Internal CMake error, TryCompile configure of cmake failed");
  3299. // return to the original directory
  3300. cmSystemTools::ChangeDirectory(cwd);
  3301. this->IsSourceFileTryCompile = false;
  3302. return 1;
  3303. }
  3304. if (cm.Generate() != 0)
  3305. {
  3306. cmSystemTools::Error(
  3307. "Internal CMake error, TryCompile generation of cmake failed");
  3308. // return to the original directory
  3309. cmSystemTools::ChangeDirectory(cwd);
  3310. this->IsSourceFileTryCompile = false;
  3311. return 1;
  3312. }
  3313. // finally call the generator to actually build the resulting project
  3314. int ret = this->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  3315. projectName,
  3316. targetName,
  3317. fast,
  3318. output,
  3319. this);
  3320. cmSystemTools::ChangeDirectory(cwd);
  3321. this->IsSourceFileTryCompile = false;
  3322. return ret;
  3323. }
  3324. bool cmMakefile::GetIsSourceFileTryCompile() const
  3325. {
  3326. return this->IsSourceFileTryCompile;
  3327. }
  3328. cmake *cmMakefile::GetCMakeInstance() const
  3329. {
  3330. return this->GlobalGenerator->GetCMakeInstance();
  3331. }
  3332. cmGlobalGenerator* cmMakefile::GetGlobalGenerator() const
  3333. {
  3334. return this->GlobalGenerator;
  3335. }
  3336. #ifdef CMAKE_BUILD_WITH_CMAKE
  3337. cmVariableWatch *cmMakefile::GetVariableWatch() const
  3338. {
  3339. if ( this->GetCMakeInstance() &&
  3340. this->GetCMakeInstance()->GetVariableWatch() )
  3341. {
  3342. return this->GetCMakeInstance()->GetVariableWatch();
  3343. }
  3344. return 0;
  3345. }
  3346. #endif
  3347. cmState *cmMakefile::GetState() const
  3348. {
  3349. return this->GetCMakeInstance()->GetState();
  3350. }
  3351. void cmMakefile::DisplayStatus(const char* message, float s) const
  3352. {
  3353. cmake* cm = this->GetCMakeInstance();
  3354. if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE)
  3355. {
  3356. // don't output any STATUS message in FIND_PACKAGE_MODE, since they will
  3357. // directly be fed to the compiler, which will be confused.
  3358. return;
  3359. }
  3360. cm->UpdateProgress(message, s);
  3361. }
  3362. std::string cmMakefile::GetModulesFile(const char* filename) const
  3363. {
  3364. std::string result;
  3365. // We search the module always in CMAKE_ROOT and in CMAKE_MODULE_PATH,
  3366. // and then decide based on the policy setting which one to return.
  3367. // See CMP0017 for more details.
  3368. // The specific problem was that KDE 4.5.0 installs a
  3369. // FindPackageHandleStandardArgs.cmake which doesn't have the new features
  3370. // of FPHSA.cmake introduced in CMake 2.8.3 yet, and by setting
  3371. // CMAKE_MODULE_PATH also e.g. FindZLIB.cmake from cmake included
  3372. // FPHSA.cmake from kdelibs and not from CMake, and tried to use the
  3373. // new features, which were not there in the version from kdelibs, and so
  3374. // failed ("
  3375. std::string moduleInCMakeRoot;
  3376. std::string moduleInCMakeModulePath;
  3377. // Always search in CMAKE_MODULE_PATH:
  3378. const char* cmakeModulePath = this->GetDefinition("CMAKE_MODULE_PATH");
  3379. if(cmakeModulePath)
  3380. {
  3381. std::vector<std::string> modulePath;
  3382. cmSystemTools::ExpandListArgument(cmakeModulePath, modulePath);
  3383. //Look through the possible module directories.
  3384. for(std::vector<std::string>::iterator i = modulePath.begin();
  3385. i != modulePath.end(); ++i)
  3386. {
  3387. std::string itempl = *i;
  3388. cmSystemTools::ConvertToUnixSlashes(itempl);
  3389. itempl += "/";
  3390. itempl += filename;
  3391. if(cmSystemTools::FileExists(itempl.c_str()))
  3392. {
  3393. moduleInCMakeModulePath = itempl;
  3394. break;
  3395. }
  3396. }
  3397. }
  3398. // Always search in the standard modules location.
  3399. const char* cmakeRoot = this->GetDefinition("CMAKE_ROOT");
  3400. if(cmakeRoot)
  3401. {
  3402. moduleInCMakeRoot = cmakeRoot;
  3403. moduleInCMakeRoot += "/Modules/";
  3404. moduleInCMakeRoot += filename;
  3405. cmSystemTools::ConvertToUnixSlashes(moduleInCMakeRoot);
  3406. if(!cmSystemTools::FileExists(moduleInCMakeRoot.c_str()))
  3407. {
  3408. moduleInCMakeRoot = "";
  3409. }
  3410. }
  3411. // Normally, prefer the files found in CMAKE_MODULE_PATH. Only when the file
  3412. // from which we are being called is located itself in CMAKE_ROOT, then
  3413. // prefer results from CMAKE_ROOT depending on the policy setting.
  3414. result = moduleInCMakeModulePath;
  3415. if (result.empty())
  3416. {
  3417. result = moduleInCMakeRoot;
  3418. }
  3419. if (!moduleInCMakeModulePath.empty() && !moduleInCMakeRoot.empty())
  3420. {
  3421. const char* currentFile = this->GetDefinition("CMAKE_CURRENT_LIST_FILE");
  3422. std::string mods = cmakeRoot + std::string("/Modules/");
  3423. if (currentFile && strncmp(currentFile, mods.c_str(), mods.size()) == 0)
  3424. {
  3425. switch (this->GetPolicyStatus(cmPolicies::CMP0017))
  3426. {
  3427. case cmPolicies::WARN:
  3428. {
  3429. std::ostringstream e;
  3430. e << "File " << currentFile << " includes "
  3431. << moduleInCMakeModulePath
  3432. << " (found via CMAKE_MODULE_PATH) which shadows "
  3433. << moduleInCMakeRoot << ". This may cause errors later on .\n"
  3434. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0017);
  3435. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3436. // break; // fall through to OLD behaviour
  3437. }
  3438. case cmPolicies::OLD:
  3439. result = moduleInCMakeModulePath;
  3440. break;
  3441. case cmPolicies::REQUIRED_IF_USED:
  3442. case cmPolicies::REQUIRED_ALWAYS:
  3443. case cmPolicies::NEW:
  3444. result = moduleInCMakeRoot;
  3445. break;
  3446. }
  3447. }
  3448. }
  3449. return result;
  3450. }
  3451. void cmMakefile::ConfigureString(const std::string& input,
  3452. std::string& output, bool atOnly,
  3453. bool escapeQuotes) const
  3454. {
  3455. // Split input to handle one line at a time.
  3456. std::string::const_iterator lineStart = input.begin();
  3457. while(lineStart != input.end())
  3458. {
  3459. // Find the end of this line.
  3460. std::string::const_iterator lineEnd = lineStart;
  3461. while(lineEnd != input.end() && *lineEnd != '\n')
  3462. {
  3463. ++lineEnd;
  3464. }
  3465. // Copy the line.
  3466. std::string line(lineStart, lineEnd);
  3467. // Skip the newline character.
  3468. bool haveNewline = (lineEnd != input.end());
  3469. if(haveNewline)
  3470. {
  3471. ++lineEnd;
  3472. }
  3473. // Replace #cmakedefine instances.
  3474. if(this->cmDefineRegex.find(line))
  3475. {
  3476. const char* def =
  3477. this->GetDefinition(this->cmDefineRegex.match(1));
  3478. if(!cmSystemTools::IsOff(def))
  3479. {
  3480. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  3481. output += line;
  3482. }
  3483. else
  3484. {
  3485. output += "/* #undef ";
  3486. output += this->cmDefineRegex.match(1);
  3487. output += " */";
  3488. }
  3489. }
  3490. else if(this->cmDefine01Regex.find(line))
  3491. {
  3492. const char* def =
  3493. this->GetDefinition(this->cmDefine01Regex.match(1));
  3494. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  3495. output += line;
  3496. if(!cmSystemTools::IsOff(def))
  3497. {
  3498. output += " 1";
  3499. }
  3500. else
  3501. {
  3502. output += " 0";
  3503. }
  3504. }
  3505. else
  3506. {
  3507. output += line;
  3508. }
  3509. if(haveNewline)
  3510. {
  3511. output += "\n";
  3512. }
  3513. // Move to the next line.
  3514. lineStart = lineEnd;
  3515. }
  3516. // Perform variable replacements.
  3517. this->ExpandVariablesInString(output, escapeQuotes, true,
  3518. atOnly, 0, -1, true, true);
  3519. }
  3520. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  3521. bool copyonly, bool atOnly, bool escapeQuotes,
  3522. const cmNewLineStyle& newLine)
  3523. {
  3524. int res = 1;
  3525. if ( !this->CanIWriteThisFile(outfile) )
  3526. {
  3527. cmSystemTools::Error("Attempt to write file: ",
  3528. outfile, " into a source directory.");
  3529. return 0;
  3530. }
  3531. if ( !cmSystemTools::FileExists(infile) )
  3532. {
  3533. cmSystemTools::Error("File ", infile, " does not exist.");
  3534. return 0;
  3535. }
  3536. std::string soutfile = outfile;
  3537. std::string sinfile = infile;
  3538. this->AddCMakeDependFile(sinfile);
  3539. cmSystemTools::ConvertToUnixSlashes(soutfile);
  3540. // Re-generate if non-temporary outputs are missing.
  3541. //when we finalize the configuration we will remove all
  3542. //output files that now don't exist.
  3543. this->AddCMakeOutputFile(soutfile);
  3544. mode_t perm = 0;
  3545. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  3546. std::string::size_type pos = soutfile.rfind('/');
  3547. if(pos != std::string::npos)
  3548. {
  3549. std::string path = soutfile.substr(0, pos);
  3550. cmSystemTools::MakeDirectory(path.c_str());
  3551. }
  3552. if(copyonly)
  3553. {
  3554. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  3555. soutfile.c_str()))
  3556. {
  3557. return 0;
  3558. }
  3559. }
  3560. else
  3561. {
  3562. std::string newLineCharacters;
  3563. std::ios_base::openmode omode = std::ios_base::out | std::ios_base::trunc;
  3564. if (newLine.IsValid())
  3565. {
  3566. newLineCharacters = newLine.GetCharacters();
  3567. omode |= std::ios::binary;
  3568. }
  3569. else
  3570. {
  3571. newLineCharacters = "\n";
  3572. }
  3573. std::string tempOutputFile = soutfile;
  3574. tempOutputFile += ".tmp";
  3575. cmsys::ofstream fout(tempOutputFile.c_str(), omode);
  3576. if(!fout)
  3577. {
  3578. cmSystemTools::Error(
  3579. "Could not open file for write in copy operation ",
  3580. tempOutputFile.c_str());
  3581. cmSystemTools::ReportLastSystemError("");
  3582. return 0;
  3583. }
  3584. cmsys::ifstream fin(sinfile.c_str());
  3585. if(!fin)
  3586. {
  3587. cmSystemTools::Error("Could not open file for read in copy operation ",
  3588. sinfile.c_str());
  3589. return 0;
  3590. }
  3591. cmsys::FStream::BOM bom = cmsys::FStream::ReadBOM(fin);
  3592. if(bom != cmsys::FStream::BOM_None &&
  3593. bom != cmsys::FStream::BOM_UTF8)
  3594. {
  3595. std::ostringstream e;
  3596. e << "File starts with a Byte-Order-Mark that is not UTF-8:\n "
  3597. << sinfile;
  3598. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3599. return 0;
  3600. }
  3601. // rewind to copy BOM to output file
  3602. fin.seekg(0);
  3603. // now copy input to output and expand variables in the
  3604. // input file at the same time
  3605. std::string inLine;
  3606. std::string outLine;
  3607. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  3608. {
  3609. outLine = "";
  3610. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  3611. fout << outLine.c_str() << newLineCharacters;
  3612. }
  3613. // close the files before attempting to copy
  3614. fin.close();
  3615. fout.close();
  3616. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  3617. soutfile.c_str()) )
  3618. {
  3619. res = 0;
  3620. }
  3621. else
  3622. {
  3623. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  3624. }
  3625. cmSystemTools::RemoveFile(tempOutputFile);
  3626. }
  3627. return res;
  3628. }
  3629. void cmMakefile::SetProperty(const std::string& prop, const char* value)
  3630. {
  3631. cmListFileBacktrace lfbt = this->GetBacktrace();
  3632. this->StateSnapshot.GetDirectory().SetProperty(prop, value, lfbt);
  3633. }
  3634. void cmMakefile::AppendProperty(const std::string& prop,
  3635. const char* value,
  3636. bool asString)
  3637. {
  3638. cmListFileBacktrace lfbt = this->GetBacktrace();
  3639. this->StateSnapshot.GetDirectory().AppendProperty(prop, value,
  3640. asString, lfbt);
  3641. }
  3642. const char *cmMakefile::GetProperty(const std::string& prop) const
  3643. {
  3644. return this->StateSnapshot.GetDirectory().GetProperty(prop);
  3645. }
  3646. const char *cmMakefile::GetProperty(const std::string& prop,
  3647. bool chain) const
  3648. {
  3649. return this->StateSnapshot.GetDirectory().GetProperty(prop, chain);
  3650. }
  3651. bool cmMakefile::GetPropertyAsBool(const std::string& prop) const
  3652. {
  3653. return cmSystemTools::IsOn(this->GetProperty(prop));
  3654. }
  3655. std::vector<std::string> cmMakefile::GetPropertyKeys() const
  3656. {
  3657. return this->StateSnapshot.GetDirectory().GetPropertyKeys();
  3658. }
  3659. cmTarget* cmMakefile::FindTarget(const std::string& name,
  3660. bool excludeAliases) const
  3661. {
  3662. if (!excludeAliases)
  3663. {
  3664. std::map<std::string, std::string>::const_iterator i =
  3665. this->AliasTargets.find(name);
  3666. if (i != this->AliasTargets.end())
  3667. {
  3668. cmTargets::iterator ai = this->Targets.find(i->second);
  3669. return &ai->second;
  3670. }
  3671. }
  3672. cmTargets::iterator i = this->Targets.find( name );
  3673. if ( i != this->Targets.end() )
  3674. {
  3675. return &i->second;
  3676. }
  3677. return 0;
  3678. }
  3679. //----------------------------------------------------------------------------
  3680. cmTest* cmMakefile::CreateTest(const std::string& testName)
  3681. {
  3682. cmTest* test = this->GetTest(testName);
  3683. if ( test )
  3684. {
  3685. return test;
  3686. }
  3687. test = new cmTest(this);
  3688. test->SetName(testName);
  3689. this->Tests[testName] = test;
  3690. return test;
  3691. }
  3692. //----------------------------------------------------------------------------
  3693. cmTest* cmMakefile::GetTest(const std::string& testName) const
  3694. {
  3695. std::map<std::string, cmTest*>::const_iterator
  3696. mi = this->Tests.find(testName);
  3697. if(mi != this->Tests.end())
  3698. {
  3699. return mi->second;
  3700. }
  3701. return 0;
  3702. }
  3703. void cmMakefile::AddCMakeDependFilesFromUser()
  3704. {
  3705. std::vector<std::string> deps;
  3706. if(const char* deps_str = this->GetProperty("CMAKE_CONFIGURE_DEPENDS"))
  3707. {
  3708. cmSystemTools::ExpandListArgument(deps_str, deps);
  3709. }
  3710. for(std::vector<std::string>::iterator i = deps.begin();
  3711. i != deps.end(); ++i)
  3712. {
  3713. if(cmSystemTools::FileIsFullPath(i->c_str()))
  3714. {
  3715. this->AddCMakeDependFile(*i);
  3716. }
  3717. else
  3718. {
  3719. std::string f = this->GetCurrentSourceDirectory();
  3720. f += "/";
  3721. f += *i;
  3722. this->AddCMakeDependFile(f);
  3723. }
  3724. }
  3725. }
  3726. std::string cmMakefile::FormatListFileStack() const
  3727. {
  3728. std::vector<std::string> listFiles;
  3729. cmState::Snapshot snp = this->StateSnapshot;
  3730. while (snp.IsValid())
  3731. {
  3732. listFiles.push_back(snp.GetExecutionListFile());
  3733. snp = snp.GetCallStackParent();
  3734. }
  3735. std::reverse(listFiles.begin(), listFiles.end());
  3736. std::ostringstream tmp;
  3737. size_t depth = listFiles.size();
  3738. if (depth > 0)
  3739. {
  3740. std::vector<std::string>::const_iterator it = listFiles.end();
  3741. do
  3742. {
  3743. if (depth != listFiles.size())
  3744. {
  3745. tmp << "\n ";
  3746. }
  3747. --it;
  3748. tmp << "[";
  3749. tmp << depth;
  3750. tmp << "]\t";
  3751. tmp << *it;
  3752. depth--;
  3753. }
  3754. while (it != listFiles.begin());
  3755. }
  3756. return tmp.str();
  3757. }
  3758. void cmMakefile::PushScope()
  3759. {
  3760. std::string commandName;
  3761. long line = 0;
  3762. if (!this->ContextStack.empty())
  3763. {
  3764. commandName = this->ContextStack.back()->Name;
  3765. line = this->ContextStack.back()->Line;
  3766. }
  3767. this->StateSnapshot = this->GetState()->CreateVariableScopeSnapshot(
  3768. this->StateSnapshot,
  3769. commandName,
  3770. line);
  3771. this->PushLoopBlockBarrier();
  3772. #if defined(CMAKE_BUILD_WITH_CMAKE)
  3773. this->GetGlobalGenerator()->GetFileLockPool().PushFunctionScope();
  3774. #endif
  3775. }
  3776. void cmMakefile::PopScope()
  3777. {
  3778. #if defined(CMAKE_BUILD_WITH_CMAKE)
  3779. this->GetGlobalGenerator()->GetFileLockPool().PopFunctionScope();
  3780. #endif
  3781. this->PopLoopBlockBarrier();
  3782. this->CheckForUnusedVariables();
  3783. this->PopSnapshot();
  3784. }
  3785. void cmMakefile::RaiseScope(const std::string& var, const char *varDef)
  3786. {
  3787. if (var.empty())
  3788. {
  3789. return;
  3790. }
  3791. if (!this->StateSnapshot.RaiseScope(var, varDef))
  3792. {
  3793. std::ostringstream m;
  3794. m << "Cannot set \"" << var << "\": current scope has no parent.";
  3795. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  3796. }
  3797. }
  3798. //----------------------------------------------------------------------------
  3799. cmTarget*
  3800. cmMakefile::AddImportedTarget(const std::string& name,
  3801. cmState::TargetType type,
  3802. bool global)
  3803. {
  3804. // Create the target.
  3805. cmsys::auto_ptr<cmTarget> target(new cmTarget);
  3806. target->SetType(type, name);
  3807. target->MarkAsImported(global);
  3808. target->SetMakefile(this);
  3809. // Add to the set of available imported targets.
  3810. this->ImportedTargets[name] = target.get();
  3811. this->GetGlobalGenerator()->IndexTarget(target.get());
  3812. // Transfer ownership to this cmMakefile object.
  3813. this->ImportedTargetsOwned.push_back(target.get());
  3814. return target.release();
  3815. }
  3816. //----------------------------------------------------------------------------
  3817. cmTarget* cmMakefile::FindTargetToUse(const std::string& name,
  3818. bool excludeAliases) const
  3819. {
  3820. // Look for an imported target. These take priority because they
  3821. // are more local in scope and do not have to be globally unique.
  3822. TargetMap::const_iterator
  3823. imported = this->ImportedTargets.find(name);
  3824. if(imported != this->ImportedTargets.end())
  3825. {
  3826. return imported->second;
  3827. }
  3828. // Look for a target built in this directory.
  3829. if(cmTarget* t = this->FindTarget(name, excludeAliases))
  3830. {
  3831. return t;
  3832. }
  3833. // Look for a target built in this project.
  3834. return this->GetGlobalGenerator()->FindTarget(name, excludeAliases);
  3835. }
  3836. //----------------------------------------------------------------------------
  3837. bool cmMakefile::IsAlias(const std::string& name) const
  3838. {
  3839. if (this->AliasTargets.find(name) != this->AliasTargets.end())
  3840. return true;
  3841. return this->GetGlobalGenerator()->IsAlias(name);
  3842. }
  3843. //----------------------------------------------------------------------------
  3844. bool cmMakefile::EnforceUniqueName(std::string const& name, std::string& msg,
  3845. bool isCustom) const
  3846. {
  3847. if(this->IsAlias(name))
  3848. {
  3849. std::ostringstream e;
  3850. e << "cannot create target \"" << name
  3851. << "\" because an alias with the same name already exists.";
  3852. msg = e.str();
  3853. return false;
  3854. }
  3855. if(cmTarget* existing = this->FindTargetToUse(name))
  3856. {
  3857. // The name given conflicts with an existing target. Produce an
  3858. // error in a compatible way.
  3859. if(existing->IsImported())
  3860. {
  3861. // Imported targets were not supported in previous versions.
  3862. // This is new code, so we can make it an error.
  3863. std::ostringstream e;
  3864. e << "cannot create target \"" << name
  3865. << "\" because an imported target with the same name already exists.";
  3866. msg = e.str();
  3867. return false;
  3868. }
  3869. else
  3870. {
  3871. // target names must be globally unique
  3872. switch (this->GetPolicyStatus(cmPolicies::CMP0002))
  3873. {
  3874. case cmPolicies::WARN:
  3875. this->IssueMessage(cmake::AUTHOR_WARNING, cmPolicies::
  3876. GetPolicyWarning(cmPolicies::CMP0002));
  3877. case cmPolicies::OLD:
  3878. return true;
  3879. case cmPolicies::REQUIRED_IF_USED:
  3880. case cmPolicies::REQUIRED_ALWAYS:
  3881. this->IssueMessage(cmake::FATAL_ERROR,
  3882. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0002)
  3883. );
  3884. return true;
  3885. case cmPolicies::NEW:
  3886. break;
  3887. }
  3888. // The conflict is with a non-imported target.
  3889. // Allow this if the user has requested support.
  3890. cmake* cm = this->GetCMakeInstance();
  3891. if(isCustom && existing->GetType() == cmState::UTILITY &&
  3892. this != existing->GetMakefile() &&
  3893. cm->GetState()
  3894. ->GetGlobalPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  3895. {
  3896. return true;
  3897. }
  3898. // Produce an error that tells the user how to work around the
  3899. // problem.
  3900. std::ostringstream e;
  3901. e << "cannot create target \"" << name
  3902. << "\" because another target with the same name already exists. "
  3903. << "The existing target is ";
  3904. switch(existing->GetType())
  3905. {
  3906. case cmState::EXECUTABLE:
  3907. e << "an executable ";
  3908. break;
  3909. case cmState::STATIC_LIBRARY:
  3910. e << "a static library ";
  3911. break;
  3912. case cmState::SHARED_LIBRARY:
  3913. e << "a shared library ";
  3914. break;
  3915. case cmState::MODULE_LIBRARY:
  3916. e << "a module library ";
  3917. break;
  3918. case cmState::UTILITY:
  3919. e << "a custom target ";
  3920. break;
  3921. case cmState::INTERFACE_LIBRARY:
  3922. e << "an interface library ";
  3923. break;
  3924. default: break;
  3925. }
  3926. e << "created in source directory \""
  3927. << existing->GetMakefile()->GetCurrentSourceDirectory() << "\". "
  3928. << "See documentation for policy CMP0002 for more details.";
  3929. msg = e.str();
  3930. return false;
  3931. }
  3932. }
  3933. return true;
  3934. }
  3935. //----------------------------------------------------------------------------
  3936. bool cmMakefile::EnforceUniqueDir(const std::string& srcPath,
  3937. const std::string& binPath) const
  3938. {
  3939. // Make sure the binary directory is unique.
  3940. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  3941. if(gg->BinaryDirectoryIsNew(binPath))
  3942. {
  3943. return true;
  3944. }
  3945. std::ostringstream e;
  3946. switch (this->GetPolicyStatus(cmPolicies::CMP0013))
  3947. {
  3948. case cmPolicies::WARN:
  3949. // Print the warning.
  3950. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0013)
  3951. << "\n"
  3952. << "The binary directory\n"
  3953. << " " << binPath << "\n"
  3954. << "is already used to build a source directory. "
  3955. << "This command uses it to build source directory\n"
  3956. << " " << srcPath << "\n"
  3957. << "which can generate conflicting build files. "
  3958. << "CMake does not support this use case but it used "
  3959. << "to work accidentally and is being allowed for "
  3960. << "compatibility.";
  3961. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3962. case cmPolicies::OLD:
  3963. // OLD behavior does not warn.
  3964. return true;
  3965. case cmPolicies::REQUIRED_IF_USED:
  3966. case cmPolicies::REQUIRED_ALWAYS:
  3967. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0013)
  3968. << "\n";
  3969. case cmPolicies::NEW:
  3970. // NEW behavior prints the error.
  3971. e << "The binary directory\n"
  3972. << " " << binPath << "\n"
  3973. << "is already used to build a source directory. "
  3974. << "It cannot be used to build source directory\n"
  3975. << " " << srcPath << "\n"
  3976. << "Specify a unique binary directory name.";
  3977. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3978. break;
  3979. }
  3980. return false;
  3981. }
  3982. //----------------------------------------------------------------------------
  3983. void cmMakefile::AddQtUiFileWithOptions(cmSourceFile *sf)
  3984. {
  3985. this->QtUiFilesWithOptions.push_back(sf);
  3986. }
  3987. //----------------------------------------------------------------------------
  3988. std::vector<cmSourceFile*> cmMakefile::GetQtUiFilesWithOptions() const
  3989. {
  3990. return this->QtUiFilesWithOptions;
  3991. }
  3992. static std::string const matchVariables[] = {
  3993. "CMAKE_MATCH_0",
  3994. "CMAKE_MATCH_1",
  3995. "CMAKE_MATCH_2",
  3996. "CMAKE_MATCH_3",
  3997. "CMAKE_MATCH_4",
  3998. "CMAKE_MATCH_5",
  3999. "CMAKE_MATCH_6",
  4000. "CMAKE_MATCH_7",
  4001. "CMAKE_MATCH_8",
  4002. "CMAKE_MATCH_9"
  4003. };
  4004. static std::string const nMatchesVariable = "CMAKE_MATCH_COUNT";
  4005. //----------------------------------------------------------------------------
  4006. void cmMakefile::ClearMatches()
  4007. {
  4008. const char* nMatchesStr = this->GetDefinition(nMatchesVariable);
  4009. if (!nMatchesStr)
  4010. {
  4011. return;
  4012. }
  4013. int nMatches = atoi(nMatchesStr);
  4014. for (int i=0; i<=nMatches; i++)
  4015. {
  4016. std::string const& var = matchVariables[i];
  4017. std::string const& s = this->GetSafeDefinition(var);
  4018. if(!s.empty())
  4019. {
  4020. this->AddDefinition(var, "");
  4021. this->MarkVariableAsUsed(var);
  4022. }
  4023. }
  4024. this->AddDefinition(nMatchesVariable, "0");
  4025. this->MarkVariableAsUsed(nMatchesVariable);
  4026. }
  4027. //----------------------------------------------------------------------------
  4028. void cmMakefile::StoreMatches(cmsys::RegularExpression& re)
  4029. {
  4030. char highest = 0;
  4031. for (int i=0; i<10; i++)
  4032. {
  4033. std::string const& m = re.match(i);
  4034. if(!m.empty())
  4035. {
  4036. std::string const& var = matchVariables[i];
  4037. this->AddDefinition(var, m.c_str());
  4038. this->MarkVariableAsUsed(var);
  4039. highest = static_cast<char>('0' + i);
  4040. }
  4041. }
  4042. char nMatches[] = {highest, '\0'};
  4043. this->AddDefinition(nMatchesVariable, nMatches);
  4044. this->MarkVariableAsUsed(nMatchesVariable);
  4045. }
  4046. cmState::Snapshot cmMakefile::GetStateSnapshot() const
  4047. {
  4048. return this->StateSnapshot;
  4049. }
  4050. const char* cmMakefile::GetDefineFlagsCMP0059() const
  4051. {
  4052. return this->DefineFlagsOrig.c_str();
  4053. }
  4054. //----------------------------------------------------------------------------
  4055. cmPolicies::PolicyStatus
  4056. cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id) const
  4057. {
  4058. return this->StateSnapshot.GetPolicy(id);
  4059. }
  4060. //----------------------------------------------------------------------------
  4061. bool cmMakefile::PolicyOptionalWarningEnabled(std::string const& var)
  4062. {
  4063. // Check for an explicit CMAKE_POLICY_WARNING_CMP<NNNN> setting.
  4064. if(!var.empty())
  4065. {
  4066. if(const char* val = this->GetDefinition(var))
  4067. {
  4068. return cmSystemTools::IsOn(val);
  4069. }
  4070. }
  4071. // Enable optional policy warnings with --debug-output, --trace,
  4072. // or --trace-expand.
  4073. cmake* cm = this->GetCMakeInstance();
  4074. return cm->GetDebugOutput() || cm->GetTrace();
  4075. }
  4076. bool cmMakefile::SetPolicy(const char *id,
  4077. cmPolicies::PolicyStatus status)
  4078. {
  4079. cmPolicies::PolicyID pid;
  4080. if (!cmPolicies::GetPolicyID(id, /* out */ pid))
  4081. {
  4082. std::ostringstream e;
  4083. e << "Policy \"" << id << "\" is not known to this version of CMake.";
  4084. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4085. return false;
  4086. }
  4087. return this->SetPolicy(pid,status);
  4088. }
  4089. //----------------------------------------------------------------------------
  4090. bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
  4091. cmPolicies::PolicyStatus status)
  4092. {
  4093. // A REQUIRED_ALWAYS policy may be set only to NEW.
  4094. if(status != cmPolicies::NEW &&
  4095. cmPolicies::GetPolicyStatus(id) ==
  4096. cmPolicies::REQUIRED_ALWAYS)
  4097. {
  4098. std::string msg =
  4099. cmPolicies::GetRequiredAlwaysPolicyError(id);
  4100. this->IssueMessage(cmake::FATAL_ERROR, msg);
  4101. return false;
  4102. }
  4103. this->StateSnapshot.SetPolicy(id, status);
  4104. return true;
  4105. }
  4106. //----------------------------------------------------------------------------
  4107. cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m): Makefile(m)
  4108. {
  4109. this->Makefile->PushPolicy();
  4110. }
  4111. //----------------------------------------------------------------------------
  4112. cmMakefile::PolicyPushPop::~PolicyPushPop()
  4113. {
  4114. this->Makefile->PopPolicy();
  4115. }
  4116. //----------------------------------------------------------------------------
  4117. void cmMakefile::PushPolicy(bool weak, cmPolicies::PolicyMap const& pm)
  4118. {
  4119. this->StateSnapshot.PushPolicy(pm, weak);
  4120. }
  4121. //----------------------------------------------------------------------------
  4122. void cmMakefile::PopPolicy()
  4123. {
  4124. if (!this->StateSnapshot.PopPolicy())
  4125. {
  4126. this->IssueMessage(cmake::FATAL_ERROR,
  4127. "cmake_policy POP without matching PUSH");
  4128. }
  4129. }
  4130. //----------------------------------------------------------------------------
  4131. void cmMakefile::PopSnapshot(bool reportError)
  4132. {
  4133. // cmState::Snapshot manages nested policy scopes within it.
  4134. // Since the scope corresponding to the snapshot is closing,
  4135. // reject any still-open nested policy scopes with an error.
  4136. while (!this->StateSnapshot.CanPopPolicyScope())
  4137. {
  4138. if(reportError)
  4139. {
  4140. this->IssueMessage(cmake::FATAL_ERROR,
  4141. "cmake_policy PUSH without matching POP");
  4142. reportError = false;
  4143. }
  4144. this->PopPolicy();
  4145. }
  4146. this->StateSnapshot = this->GetState()->Pop(this->StateSnapshot);
  4147. assert(this->StateSnapshot.IsValid());
  4148. }
  4149. //----------------------------------------------------------------------------
  4150. bool cmMakefile::SetPolicyVersion(const char *version)
  4151. {
  4152. return cmPolicies::ApplyPolicyVersion(this,version);
  4153. }
  4154. //----------------------------------------------------------------------------
  4155. bool cmMakefile::HasCMP0054AlreadyBeenReported(
  4156. cmListFileContext const& context) const
  4157. {
  4158. return !this->CMP0054ReportedIds.insert(context).second;
  4159. }
  4160. //----------------------------------------------------------------------------
  4161. void cmMakefile::RecordPolicies(cmPolicies::PolicyMap& pm)
  4162. {
  4163. /* Record the setting of every policy. */
  4164. typedef cmPolicies::PolicyID PolicyID;
  4165. for(PolicyID pid = cmPolicies::CMP0000;
  4166. pid != cmPolicies::CMPCOUNT; pid = PolicyID(pid+1))
  4167. {
  4168. pm.Set(pid, this->GetPolicyStatus(pid));
  4169. }
  4170. }
  4171. //----------------------------------------------------------------------------
  4172. bool cmMakefile::IgnoreErrorsCMP0061() const
  4173. {
  4174. bool ignoreErrors = true;
  4175. switch (this->GetPolicyStatus(cmPolicies::CMP0061))
  4176. {
  4177. case cmPolicies::WARN:
  4178. // No warning for this policy!
  4179. case cmPolicies::OLD:
  4180. break;
  4181. case cmPolicies::REQUIRED_IF_USED:
  4182. case cmPolicies::REQUIRED_ALWAYS:
  4183. case cmPolicies::NEW:
  4184. ignoreErrors = false;
  4185. break;
  4186. }
  4187. return ignoreErrors;
  4188. }
  4189. //----------------------------------------------------------------------------
  4190. #define FEATURE_STRING(F) , #F
  4191. static const char * const C_FEATURES[] = {
  4192. 0
  4193. FOR_EACH_C_FEATURE(FEATURE_STRING)
  4194. };
  4195. static const char * const CXX_FEATURES[] = {
  4196. 0
  4197. FOR_EACH_CXX_FEATURE(FEATURE_STRING)
  4198. };
  4199. #undef FEATURE_STRING
  4200. static const char * const C_STANDARDS[] = {
  4201. "90"
  4202. , "99"
  4203. , "11"
  4204. };
  4205. static const char * const CXX_STANDARDS[] = {
  4206. "98"
  4207. , "11"
  4208. , "14"
  4209. };
  4210. //----------------------------------------------------------------------------
  4211. bool cmMakefile::
  4212. AddRequiredTargetFeature(cmTarget *target, const std::string& feature,
  4213. std::string *error) const
  4214. {
  4215. if (cmGeneratorExpression::Find(feature) != std::string::npos)
  4216. {
  4217. target->AppendProperty("COMPILE_FEATURES", feature.c_str());
  4218. return true;
  4219. }
  4220. std::string lang;
  4221. if (!this->CompileFeatureKnown(target, feature, lang, error))
  4222. {
  4223. return false;
  4224. }
  4225. const char* features = this->CompileFeaturesAvailable(lang, error);
  4226. if (!features)
  4227. {
  4228. return false;
  4229. }
  4230. std::vector<std::string> availableFeatures;
  4231. cmSystemTools::ExpandListArgument(features, availableFeatures);
  4232. if (std::find(availableFeatures.begin(),
  4233. availableFeatures.end(),
  4234. feature) == availableFeatures.end())
  4235. {
  4236. std::ostringstream e;
  4237. e << "The compiler feature \"" << feature
  4238. << "\" is not known to " << lang << " compiler\n\""
  4239. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_ID")
  4240. << "\"\nversion "
  4241. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << ".";
  4242. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4243. return false;
  4244. }
  4245. target->AppendProperty("COMPILE_FEATURES", feature.c_str());
  4246. return lang == "C"
  4247. ? this->AddRequiredTargetCFeature(target, feature)
  4248. : this->AddRequiredTargetCxxFeature(target, feature);
  4249. }
  4250. //----------------------------------------------------------------------------
  4251. bool cmMakefile::
  4252. CompileFeatureKnown(cmTarget const* target, const std::string& feature,
  4253. std::string& lang, std::string *error) const
  4254. {
  4255. assert(cmGeneratorExpression::Find(feature) == std::string::npos);
  4256. bool isCFeature = std::find_if(cmArrayBegin(C_FEATURES) + 1,
  4257. cmArrayEnd(C_FEATURES), cmStrCmp(feature))
  4258. != cmArrayEnd(C_FEATURES);
  4259. if (isCFeature)
  4260. {
  4261. lang = "C";
  4262. return true;
  4263. }
  4264. bool isCxxFeature = std::find_if(cmArrayBegin(CXX_FEATURES) + 1,
  4265. cmArrayEnd(CXX_FEATURES), cmStrCmp(feature))
  4266. != cmArrayEnd(CXX_FEATURES);
  4267. if (isCxxFeature)
  4268. {
  4269. lang = "CXX";
  4270. return true;
  4271. }
  4272. std::ostringstream e;
  4273. if (error)
  4274. {
  4275. e << "specified";
  4276. }
  4277. else
  4278. {
  4279. e << "Specified";
  4280. }
  4281. e << " unknown feature \"" << feature << "\" for "
  4282. "target \"" << target->GetName() << "\".";
  4283. if (error)
  4284. {
  4285. *error = e.str();
  4286. }
  4287. else
  4288. {
  4289. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4290. }
  4291. return false;
  4292. }
  4293. //----------------------------------------------------------------------------
  4294. const char* cmMakefile::
  4295. CompileFeaturesAvailable(const std::string& lang, std::string *error) const
  4296. {
  4297. const char* featuresKnown =
  4298. this->GetDefinition("CMAKE_" + lang + "_COMPILE_FEATURES");
  4299. if (!featuresKnown || !*featuresKnown)
  4300. {
  4301. std::ostringstream e;
  4302. if (error)
  4303. {
  4304. e << "no";
  4305. }
  4306. else
  4307. {
  4308. e << "No";
  4309. }
  4310. e << " known features for " << lang << " compiler\n\""
  4311. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_ID")
  4312. << "\"\nversion "
  4313. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << ".";
  4314. if (error)
  4315. {
  4316. *error = e.str();
  4317. }
  4318. else
  4319. {
  4320. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4321. }
  4322. return 0;
  4323. }
  4324. return featuresKnown;
  4325. }
  4326. //----------------------------------------------------------------------------
  4327. bool cmMakefile::HaveStandardAvailable(cmTarget const* target,
  4328. std::string const& lang,
  4329. const std::string& feature) const
  4330. {
  4331. return lang == "C"
  4332. ? this->HaveCStandardAvailable(target, feature)
  4333. : this->HaveCxxStandardAvailable(target, feature);
  4334. }
  4335. //----------------------------------------------------------------------------
  4336. bool cmMakefile::
  4337. HaveCStandardAvailable(cmTarget const* target,
  4338. const std::string& feature) const
  4339. {
  4340. const char* defaultCStandard =
  4341. this->GetDefinition("CMAKE_C_STANDARD_DEFAULT");
  4342. if (!defaultCStandard)
  4343. {
  4344. std::ostringstream e;
  4345. e << "CMAKE_C_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  4346. "not fully configured for this compiler.";
  4347. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  4348. // Return true so the caller does not try to lookup the default standard.
  4349. return true;
  4350. }
  4351. if (std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  4352. cmStrCmp(defaultCStandard)) == cmArrayEnd(C_STANDARDS))
  4353. {
  4354. std::ostringstream e;
  4355. e << "The CMAKE_C_STANDARD_DEFAULT variable contains an "
  4356. "invalid value: \"" << defaultCStandard << "\".";
  4357. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  4358. return false;
  4359. }
  4360. bool needC90 = false;
  4361. bool needC99 = false;
  4362. bool needC11 = false;
  4363. this->CheckNeededCLanguage(feature, needC90, needC99, needC11);
  4364. const char *existingCStandard = target->GetProperty("C_STANDARD");
  4365. if (!existingCStandard)
  4366. {
  4367. existingCStandard = defaultCStandard;
  4368. }
  4369. if (std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  4370. cmStrCmp(existingCStandard)) == cmArrayEnd(C_STANDARDS))
  4371. {
  4372. std::ostringstream e;
  4373. e << "The C_STANDARD property on target \"" << target->GetName()
  4374. << "\" contained an invalid value: \"" << existingCStandard << "\".";
  4375. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4376. return false;
  4377. }
  4378. const char * const *existingCIt = existingCStandard
  4379. ? std::find_if(cmArrayBegin(C_STANDARDS),
  4380. cmArrayEnd(C_STANDARDS),
  4381. cmStrCmp(existingCStandard))
  4382. : cmArrayEnd(C_STANDARDS);
  4383. if (needC11 && existingCStandard && existingCIt <
  4384. std::find_if(cmArrayBegin(C_STANDARDS),
  4385. cmArrayEnd(C_STANDARDS),
  4386. cmStrCmp("11")))
  4387. {
  4388. return false;
  4389. }
  4390. else if(needC99 && existingCStandard && existingCIt <
  4391. std::find_if(cmArrayBegin(C_STANDARDS),
  4392. cmArrayEnd(C_STANDARDS),
  4393. cmStrCmp("99")))
  4394. {
  4395. return false;
  4396. }
  4397. else if(needC90 && existingCStandard && existingCIt <
  4398. std::find_if(cmArrayBegin(C_STANDARDS),
  4399. cmArrayEnd(C_STANDARDS),
  4400. cmStrCmp("90")))
  4401. {
  4402. return false;
  4403. }
  4404. return true;
  4405. }
  4406. //----------------------------------------------------------------------------
  4407. bool cmMakefile::IsLaterStandard(std::string const& lang,
  4408. std::string const& lhs,
  4409. std::string const& rhs)
  4410. {
  4411. if (lang == "C")
  4412. {
  4413. const char * const *rhsIt = std::find_if(cmArrayBegin(C_STANDARDS),
  4414. cmArrayEnd(C_STANDARDS),
  4415. cmStrCmp(rhs));
  4416. return std::find_if(rhsIt, cmArrayEnd(C_STANDARDS),
  4417. cmStrCmp(lhs)) != cmArrayEnd(C_STANDARDS);
  4418. }
  4419. const char * const *rhsIt = std::find_if(cmArrayBegin(CXX_STANDARDS),
  4420. cmArrayEnd(CXX_STANDARDS),
  4421. cmStrCmp(rhs));
  4422. return std::find_if(rhsIt, cmArrayEnd(CXX_STANDARDS),
  4423. cmStrCmp(lhs)) != cmArrayEnd(CXX_STANDARDS);
  4424. }
  4425. //----------------------------------------------------------------------------
  4426. bool cmMakefile::HaveCxxStandardAvailable(cmTarget const* target,
  4427. const std::string& feature) const
  4428. {
  4429. const char* defaultCxxStandard =
  4430. this->GetDefinition("CMAKE_CXX_STANDARD_DEFAULT");
  4431. if (!defaultCxxStandard)
  4432. {
  4433. std::ostringstream e;
  4434. e << "CMAKE_CXX_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  4435. "not fully configured for this compiler.";
  4436. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  4437. // Return true so the caller does not try to lookup the default standard.
  4438. return true;
  4439. }
  4440. if (std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  4441. cmStrCmp(defaultCxxStandard)) == cmArrayEnd(CXX_STANDARDS))
  4442. {
  4443. std::ostringstream e;
  4444. e << "The CMAKE_CXX_STANDARD_DEFAULT variable contains an "
  4445. "invalid value: \"" << defaultCxxStandard << "\".";
  4446. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  4447. return false;
  4448. }
  4449. bool needCxx98 = false;
  4450. bool needCxx11 = false;
  4451. bool needCxx14 = false;
  4452. this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11, needCxx14);
  4453. const char *existingCxxStandard = target->GetProperty("CXX_STANDARD");
  4454. if (!existingCxxStandard)
  4455. {
  4456. existingCxxStandard = defaultCxxStandard;
  4457. }
  4458. if (std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  4459. cmStrCmp(existingCxxStandard)) == cmArrayEnd(CXX_STANDARDS))
  4460. {
  4461. std::ostringstream e;
  4462. e << "The CXX_STANDARD property on target \"" << target->GetName()
  4463. << "\" contained an invalid value: \"" << existingCxxStandard << "\".";
  4464. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4465. return false;
  4466. }
  4467. const char * const *existingCxxIt = existingCxxStandard
  4468. ? std::find_if(cmArrayBegin(CXX_STANDARDS),
  4469. cmArrayEnd(CXX_STANDARDS),
  4470. cmStrCmp(existingCxxStandard))
  4471. : cmArrayEnd(CXX_STANDARDS);
  4472. if (needCxx11 && existingCxxIt < std::find_if(cmArrayBegin(CXX_STANDARDS),
  4473. cmArrayEnd(CXX_STANDARDS),
  4474. cmStrCmp("11")))
  4475. {
  4476. return false;
  4477. }
  4478. else if(needCxx98 && existingCxxIt <
  4479. std::find_if(cmArrayBegin(CXX_STANDARDS),
  4480. cmArrayEnd(CXX_STANDARDS),
  4481. cmStrCmp("98")))
  4482. {
  4483. return false;
  4484. }
  4485. return true;
  4486. }
  4487. //----------------------------------------------------------------------------
  4488. void cmMakefile::CheckNeededCxxLanguage(const std::string& feature,
  4489. bool& needCxx98,
  4490. bool& needCxx11,
  4491. bool& needCxx14) const
  4492. {
  4493. if (const char *propCxx98 =
  4494. this->GetDefinition("CMAKE_CXX98_COMPILE_FEATURES"))
  4495. {
  4496. std::vector<std::string> props;
  4497. cmSystemTools::ExpandListArgument(propCxx98, props);
  4498. needCxx98 = std::find(props.begin(), props.end(), feature) != props.end();
  4499. }
  4500. if (const char *propCxx11 =
  4501. this->GetDefinition("CMAKE_CXX11_COMPILE_FEATURES"))
  4502. {
  4503. std::vector<std::string> props;
  4504. cmSystemTools::ExpandListArgument(propCxx11, props);
  4505. needCxx11 = std::find(props.begin(), props.end(), feature) != props.end();
  4506. }
  4507. if (const char *propCxx14 =
  4508. this->GetDefinition("CMAKE_CXX14_COMPILE_FEATURES"))
  4509. {
  4510. std::vector<std::string> props;
  4511. cmSystemTools::ExpandListArgument(propCxx14, props);
  4512. needCxx14 = std::find(props.begin(), props.end(), feature) != props.end();
  4513. }
  4514. }
  4515. //----------------------------------------------------------------------------
  4516. bool cmMakefile::
  4517. AddRequiredTargetCxxFeature(cmTarget *target,
  4518. const std::string& feature) const
  4519. {
  4520. bool needCxx98 = false;
  4521. bool needCxx11 = false;
  4522. bool needCxx14 = false;
  4523. this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11, needCxx14);
  4524. const char *existingCxxStandard = target->GetProperty("CXX_STANDARD");
  4525. if (existingCxxStandard)
  4526. {
  4527. if (std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  4528. cmStrCmp(existingCxxStandard)) == cmArrayEnd(CXX_STANDARDS))
  4529. {
  4530. std::ostringstream e;
  4531. e << "The CXX_STANDARD property on target \"" << target->GetName()
  4532. << "\" contained an invalid value: \"" << existingCxxStandard << "\".";
  4533. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4534. return false;
  4535. }
  4536. }
  4537. const char * const *existingCxxIt = existingCxxStandard
  4538. ? std::find_if(cmArrayBegin(CXX_STANDARDS),
  4539. cmArrayEnd(CXX_STANDARDS),
  4540. cmStrCmp(existingCxxStandard))
  4541. : cmArrayEnd(CXX_STANDARDS);
  4542. bool setCxx98 = needCxx98 && !existingCxxStandard;
  4543. bool setCxx11 = needCxx11 && !existingCxxStandard;
  4544. bool setCxx14 = needCxx14 && !existingCxxStandard;
  4545. if (needCxx14 && existingCxxStandard && existingCxxIt <
  4546. std::find_if(cmArrayBegin(CXX_STANDARDS),
  4547. cmArrayEnd(CXX_STANDARDS),
  4548. cmStrCmp("14")))
  4549. {
  4550. setCxx14 = true;
  4551. }
  4552. else if (needCxx11 && existingCxxStandard && existingCxxIt <
  4553. std::find_if(cmArrayBegin(CXX_STANDARDS),
  4554. cmArrayEnd(CXX_STANDARDS),
  4555. cmStrCmp("11")))
  4556. {
  4557. setCxx11 = true;
  4558. }
  4559. else if(needCxx98 && existingCxxStandard && existingCxxIt <
  4560. std::find_if(cmArrayBegin(CXX_STANDARDS),
  4561. cmArrayEnd(CXX_STANDARDS),
  4562. cmStrCmp("98")))
  4563. {
  4564. setCxx98 = true;
  4565. }
  4566. if (setCxx14)
  4567. {
  4568. target->SetProperty("CXX_STANDARD", "14");
  4569. }
  4570. else if (setCxx11)
  4571. {
  4572. target->SetProperty("CXX_STANDARD", "11");
  4573. }
  4574. else if (setCxx98)
  4575. {
  4576. target->SetProperty("CXX_STANDARD", "98");
  4577. }
  4578. return true;
  4579. }
  4580. //----------------------------------------------------------------------------
  4581. void cmMakefile::CheckNeededCLanguage(const std::string& feature,
  4582. bool& needC90,
  4583. bool& needC99,
  4584. bool& needC11) const
  4585. {
  4586. if (const char *propC90 =
  4587. this->GetDefinition("CMAKE_C90_COMPILE_FEATURES"))
  4588. {
  4589. std::vector<std::string> props;
  4590. cmSystemTools::ExpandListArgument(propC90, props);
  4591. needC90 = std::find(props.begin(), props.end(), feature) != props.end();
  4592. }
  4593. if (const char *propC99 =
  4594. this->GetDefinition("CMAKE_C99_COMPILE_FEATURES"))
  4595. {
  4596. std::vector<std::string> props;
  4597. cmSystemTools::ExpandListArgument(propC99, props);
  4598. needC99 = std::find(props.begin(), props.end(), feature) != props.end();
  4599. }
  4600. if (const char *propC11 =
  4601. this->GetDefinition("CMAKE_C11_COMPILE_FEATURES"))
  4602. {
  4603. std::vector<std::string> props;
  4604. cmSystemTools::ExpandListArgument(propC11, props);
  4605. needC11 = std::find(props.begin(), props.end(), feature) != props.end();
  4606. }
  4607. }
  4608. //----------------------------------------------------------------------------
  4609. bool cmMakefile::
  4610. AddRequiredTargetCFeature(cmTarget *target, const std::string& feature) const
  4611. {
  4612. bool needC90 = false;
  4613. bool needC99 = false;
  4614. bool needC11 = false;
  4615. this->CheckNeededCLanguage(feature, needC90, needC99, needC11);
  4616. const char *existingCStandard = target->GetProperty("C_STANDARD");
  4617. if (existingCStandard)
  4618. {
  4619. if (std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  4620. cmStrCmp(existingCStandard)) == cmArrayEnd(C_STANDARDS))
  4621. {
  4622. std::ostringstream e;
  4623. e << "The C_STANDARD property on target \"" << target->GetName()
  4624. << "\" contained an invalid value: \"" << existingCStandard << "\".";
  4625. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4626. return false;
  4627. }
  4628. }
  4629. const char * const *existingCIt = existingCStandard
  4630. ? std::find_if(cmArrayBegin(C_STANDARDS),
  4631. cmArrayEnd(C_STANDARDS),
  4632. cmStrCmp(existingCStandard))
  4633. : cmArrayEnd(C_STANDARDS);
  4634. bool setC90 = needC90 && !existingCStandard;
  4635. bool setC99 = needC99 && !existingCStandard;
  4636. bool setC11 = needC11 && !existingCStandard;
  4637. if (needC11 && existingCStandard && existingCIt <
  4638. std::find_if(cmArrayBegin(C_STANDARDS),
  4639. cmArrayEnd(C_STANDARDS),
  4640. cmStrCmp("11")))
  4641. {
  4642. setC11 = true;
  4643. }
  4644. else if(needC99 && existingCStandard && existingCIt <
  4645. std::find_if(cmArrayBegin(C_STANDARDS),
  4646. cmArrayEnd(C_STANDARDS),
  4647. cmStrCmp("99")))
  4648. {
  4649. setC99 = true;
  4650. }
  4651. else if(needC90 && existingCStandard && existingCIt <
  4652. std::find_if(cmArrayBegin(C_STANDARDS),
  4653. cmArrayEnd(C_STANDARDS),
  4654. cmStrCmp("90")))
  4655. {
  4656. setC90 = true;
  4657. }
  4658. if (setC11)
  4659. {
  4660. target->SetProperty("C_STANDARD", "11");
  4661. }
  4662. else if (setC99)
  4663. {
  4664. target->SetProperty("C_STANDARD", "99");
  4665. }
  4666. else if (setC90)
  4667. {
  4668. target->SetProperty("C_STANDARD", "90");
  4669. }
  4670. return true;
  4671. }
  4672. cmMakefile::FunctionPushPop::FunctionPushPop(cmMakefile* mf,
  4673. const std::string& fileName,
  4674. cmPolicies::PolicyMap const& pm)
  4675. : Makefile(mf), ReportError(true)
  4676. {
  4677. this->Makefile->PushFunctionScope(fileName, pm);
  4678. }
  4679. cmMakefile::FunctionPushPop::~FunctionPushPop()
  4680. {
  4681. this->Makefile->PopFunctionScope(this->ReportError);
  4682. }
  4683. cmMakefile::MacroPushPop::MacroPushPop(cmMakefile* mf,
  4684. const std::string& fileName,
  4685. const cmPolicies::PolicyMap& pm)
  4686. : Makefile(mf), ReportError(true)
  4687. {
  4688. this->Makefile->PushMacroScope(fileName, pm);
  4689. }
  4690. cmMakefile::MacroPushPop::~MacroPushPop()
  4691. {
  4692. this->Makefile->PopMacroScope(this->ReportError);
  4693. }
  4694. cmMakefileCall::cmMakefileCall(cmMakefile* mf, const cmCommandContext& lfc,
  4695. cmExecutionStatus& status): Makefile(mf)
  4696. {
  4697. this->Makefile->ContextStack.push_back(&lfc);
  4698. this->Makefile->ExecutionStatusStack.push_back(&status);
  4699. }
  4700. cmMakefileCall::~cmMakefileCall()
  4701. {
  4702. this->Makefile->ExecutionStatusStack.pop_back();
  4703. this->Makefile->ContextStack.pop_back();
  4704. }