cmMakefile.cxx 157 KB

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