cmMakefile.cxx 147 KB

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