cmMakefile.cxx 135 KB

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