cmMakefile.cxx 146 KB

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