cmMakefile.cxx 158 KB

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