cmMakefile.cxx 147 KB

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