cmMakefile.cxx 139 KB

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