cmMakefile.cxx 147 KB

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