cmMakefile.cxx 146 KB

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