cmMakefile.cxx 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517
  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<cmStdString> > VarInitStack;
  44. std::stack<std::set<cmStdString> > 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<cmStdString> 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<cmStdString, 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).c_str() << " ";
  218. }
  219. std::cout << " )\n";
  220. }
  221. void cmMakefile
  222. ::PrintStringVector(const char* s,
  223. const std::vector<std::pair<cmStdString, bool> >& v) const
  224. {
  225. std::cout << s << ": ( \n";
  226. for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
  227. = v.begin(); i != v.end(); ++i)
  228. {
  229. std::cout << i->first.c_str() << " " << 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.c_str() << std::endl;
  246. std::cout << " this->HomeOutputDirectory; " <<
  247. this->HomeOutputDirectory.c_str() << std::endl;
  248. std::cout << " this->cmStartDirectory; " <<
  249. this->cmStartDirectory.c_str() << std::endl;
  250. std::cout << " this->cmHomeDirectory; " <<
  251. this->cmHomeDirectory.c_str() << std::endl;
  252. std::cout << " this->ProjectName; "
  253. << this->ProjectName.c_str() << 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.c_str(),
  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.c_str(),
  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.c_str()))
  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().c_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().c_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 char* 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. switch(this->GetPolicyStatus(cmPolicies::CMP0040))
  813. {
  814. case cmPolicies::WARN:
  815. issueMessage = true;
  816. case cmPolicies::OLD:
  817. break;
  818. case cmPolicies::NEW:
  819. case cmPolicies::REQUIRED_IF_USED:
  820. case cmPolicies::REQUIRED_ALWAYS:
  821. issueMessage = true;
  822. messageType = cmake::FATAL_ERROR;
  823. }
  824. if(issueMessage)
  825. {
  826. cmOStringStream e;
  827. e << (this->GetPolicies()
  828. ->GetPolicyWarning(cmPolicies::CMP0040)) << "\n";
  829. e << "The target name \"" << target << "\" is unknown in this context.";
  830. IssueMessage(messageType, e.str().c_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 char* 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 && main_dependency[0])
  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.c_str());
  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.c_str(), 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->c_str(), 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 && main_dependency[0])
  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 char* output,
  1014. const std::vector<std::string>& depends,
  1015. const char* 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 char* target,
  1031. const std::vector<std::string>& outputs,
  1032. const std::vector<std::string>& depends,
  1033. const char* 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(strcmp(source, target) == 0)
  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. const char* no_main_dependency = 0;
  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].AddSourceFile(sf);
  1082. }
  1083. else
  1084. {
  1085. cmSystemTools::Error("Attempt to add a custom rule to a target "
  1086. "that does not exist yet for target ", target);
  1087. return;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. //----------------------------------------------------------------------------
  1093. void cmMakefile::AddUtilityCommand(const char* utilityName,
  1094. bool excludeFromAll,
  1095. const std::vector<std::string>& depends,
  1096. const char* workingDirectory,
  1097. const char* command,
  1098. const char* arg1,
  1099. const char* arg2,
  1100. const char* arg3,
  1101. const char* arg4)
  1102. {
  1103. // Construct the command line for the custom command.
  1104. cmCustomCommandLine commandLine;
  1105. commandLine.push_back(command);
  1106. if(arg1)
  1107. {
  1108. commandLine.push_back(arg1);
  1109. }
  1110. if(arg2)
  1111. {
  1112. commandLine.push_back(arg2);
  1113. }
  1114. if(arg3)
  1115. {
  1116. commandLine.push_back(arg3);
  1117. }
  1118. if(arg4)
  1119. {
  1120. commandLine.push_back(arg4);
  1121. }
  1122. cmCustomCommandLines commandLines;
  1123. commandLines.push_back(commandLine);
  1124. // Call the real signature of this method.
  1125. this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
  1126. depends, commandLines);
  1127. }
  1128. //----------------------------------------------------------------------------
  1129. cmTarget*
  1130. cmMakefile::AddUtilityCommand(const char* utilityName,
  1131. bool excludeFromAll,
  1132. const char* workingDirectory,
  1133. const std::vector<std::string>& depends,
  1134. const cmCustomCommandLines& commandLines,
  1135. bool escapeOldStyle, const char* comment)
  1136. {
  1137. // Create a target instance for this utility.
  1138. cmTarget* target = this->AddNewTarget(cmTarget::UTILITY, utilityName);
  1139. if (excludeFromAll)
  1140. {
  1141. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1142. }
  1143. if(!comment)
  1144. {
  1145. // Use an empty comment to avoid generation of default comment.
  1146. comment = "";
  1147. }
  1148. // Store the custom command in the target.
  1149. std::string force = this->GetStartOutputDirectory();
  1150. force += cmake::GetCMakeFilesDirectory();
  1151. force += "/";
  1152. force += utilityName;
  1153. const char* no_main_dependency = 0;
  1154. bool no_replace = false;
  1155. this->AddCustomCommandToOutput(force.c_str(), depends,
  1156. no_main_dependency,
  1157. commandLines, comment,
  1158. workingDirectory, no_replace,
  1159. escapeOldStyle);
  1160. cmSourceFile* sf = target->AddSource(force.c_str());
  1161. // The output is not actually created so mark it symbolic.
  1162. if(sf)
  1163. {
  1164. sf->SetProperty("SYMBOLIC", "1");
  1165. }
  1166. else
  1167. {
  1168. cmSystemTools::Error("Could not get source file entry for ",
  1169. force.c_str());
  1170. }
  1171. return target;
  1172. }
  1173. void cmMakefile::AddDefineFlag(const char* flag)
  1174. {
  1175. if (!flag)
  1176. {
  1177. return;
  1178. }
  1179. // Update the string used for the old DEFINITIONS property.
  1180. this->AddDefineFlag(flag, this->DefineFlagsOrig);
  1181. // If this is really a definition, update COMPILE_DEFINITIONS.
  1182. if(this->ParseDefineFlag(flag, false))
  1183. {
  1184. return;
  1185. }
  1186. // Add this flag that does not look like a definition.
  1187. this->AddDefineFlag(flag, this->DefineFlags);
  1188. }
  1189. void cmMakefile::AddDefineFlag(const char* flag, std::string& dflags)
  1190. {
  1191. // remove any \n\r
  1192. std::string ret = flag;
  1193. std::string::size_type pos = 0;
  1194. while((pos = ret.find('\n', pos)) != std::string::npos)
  1195. {
  1196. ret[pos] = ' ';
  1197. pos++;
  1198. }
  1199. pos = 0;
  1200. while((pos = ret.find('\r', pos)) != std::string::npos)
  1201. {
  1202. ret[pos] = ' ';
  1203. pos++;
  1204. }
  1205. dflags += " ";
  1206. dflags += ret;
  1207. }
  1208. void cmMakefile::RemoveDefineFlag(const char* flag)
  1209. {
  1210. // Check the length of the flag to remove.
  1211. std::string::size_type len = strlen(flag);
  1212. if(len < 1)
  1213. {
  1214. return;
  1215. }
  1216. // Update the string used for the old DEFINITIONS property.
  1217. this->RemoveDefineFlag(flag, len, this->DefineFlagsOrig);
  1218. // If this is really a definition, update COMPILE_DEFINITIONS.
  1219. if(this->ParseDefineFlag(flag, true))
  1220. {
  1221. return;
  1222. }
  1223. // Remove this flag that does not look like a definition.
  1224. this->RemoveDefineFlag(flag, len, this->DefineFlags);
  1225. }
  1226. void cmMakefile::RemoveDefineFlag(const char* flag,
  1227. std::string::size_type len,
  1228. std::string& dflags)
  1229. {
  1230. // Remove all instances of the flag that are surrounded by
  1231. // whitespace or the beginning/end of the string.
  1232. for(std::string::size_type lpos = dflags.find(flag, 0);
  1233. lpos != std::string::npos; lpos = dflags.find(flag, lpos))
  1234. {
  1235. std::string::size_type rpos = lpos + len;
  1236. if((lpos <= 0 || isspace(dflags[lpos-1])) &&
  1237. (rpos >= dflags.size() || isspace(dflags[rpos])))
  1238. {
  1239. dflags.erase(lpos, len);
  1240. }
  1241. else
  1242. {
  1243. ++lpos;
  1244. }
  1245. }
  1246. }
  1247. void cmMakefile::AddCompileOption(const char* option)
  1248. {
  1249. this->AppendProperty("COMPILE_OPTIONS", option);
  1250. }
  1251. bool cmMakefile::ParseDefineFlag(std::string const& def, bool remove)
  1252. {
  1253. // Create a regular expression to match valid definitions.
  1254. static cmsys::RegularExpression
  1255. valid("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=.*)?$");
  1256. // Make sure the definition matches.
  1257. if(!valid.find(def.c_str()))
  1258. {
  1259. return false;
  1260. }
  1261. // VS6 IDE does not support definition values with spaces in
  1262. // combination with '"', '$', or ';'.
  1263. if((strcmp(this->LocalGenerator->GetGlobalGenerator()->GetName(),
  1264. "Visual Studio 6") == 0) &&
  1265. (def.find(" ") != def.npos && def.find_first_of("\"$;") != def.npos))
  1266. {
  1267. return false;
  1268. }
  1269. // Definitions with non-trivial values require a policy check.
  1270. static cmsys::RegularExpression
  1271. trivial("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=[A-Za-z0-9_.]+)?$");
  1272. if(!trivial.find(def.c_str()))
  1273. {
  1274. // This definition has a non-trivial value.
  1275. switch(this->GetPolicyStatus(cmPolicies::CMP0005))
  1276. {
  1277. case cmPolicies::WARN:
  1278. this->IssueMessage(
  1279. cmake::AUTHOR_WARNING,
  1280. this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0005)
  1281. );
  1282. case cmPolicies::OLD:
  1283. // OLD behavior is to not escape the value. We should not
  1284. // convert the definition to use the property.
  1285. return false;
  1286. case cmPolicies::REQUIRED_IF_USED:
  1287. case cmPolicies::REQUIRED_ALWAYS:
  1288. this->IssueMessage(
  1289. cmake::FATAL_ERROR,
  1290. this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0005)
  1291. );
  1292. return false;
  1293. case cmPolicies::NEW:
  1294. // NEW behavior is to escape the value. Proceed to convert it
  1295. // to an entry in the property.
  1296. break;
  1297. }
  1298. }
  1299. // Get the definition part after the flag.
  1300. const char* define = def.c_str() + 2;
  1301. if(remove)
  1302. {
  1303. if(const char* cdefs = this->GetProperty("COMPILE_DEFINITIONS"))
  1304. {
  1305. // Expand the list.
  1306. std::vector<std::string> defs;
  1307. cmSystemTools::ExpandListArgument(cdefs, defs);
  1308. // Recompose the list without the definition.
  1309. std::string ndefs;
  1310. const char* sep = "";
  1311. for(std::vector<std::string>::const_iterator di = defs.begin();
  1312. di != defs.end(); ++di)
  1313. {
  1314. if(*di != define)
  1315. {
  1316. ndefs += sep;
  1317. sep = ";";
  1318. ndefs += *di;
  1319. }
  1320. }
  1321. // Store the new list.
  1322. this->SetProperty("COMPILE_DEFINITIONS", ndefs.c_str());
  1323. }
  1324. }
  1325. else
  1326. {
  1327. // Append the definition to the directory property.
  1328. this->AppendProperty("COMPILE_DEFINITIONS", define);
  1329. }
  1330. return true;
  1331. }
  1332. void cmMakefile::AddLinkLibrary(const char* lib,
  1333. cmTarget::LinkLibraryType llt)
  1334. {
  1335. cmTarget::LibraryID tmp;
  1336. tmp.first = lib;
  1337. tmp.second = llt;
  1338. this->LinkLibraries.push_back(tmp);
  1339. }
  1340. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  1341. const char* lib,
  1342. cmTarget::LinkLibraryType llt)
  1343. {
  1344. cmTargets::iterator i = this->Targets.find(target);
  1345. if ( i != this->Targets.end())
  1346. {
  1347. cmTarget* tgt =
  1348. this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0,lib);
  1349. if(tgt)
  1350. {
  1351. // if it is not a static or shared library then you can not link to it
  1352. if(!((tgt->GetType() == cmTarget::STATIC_LIBRARY) ||
  1353. (tgt->GetType() == cmTarget::SHARED_LIBRARY) ||
  1354. (tgt->GetType() == cmTarget::INTERFACE_LIBRARY) ||
  1355. tgt->IsExecutableWithExports()))
  1356. {
  1357. cmOStringStream e;
  1358. e << "Target \"" << lib << "\" of type "
  1359. << cmTarget::GetTargetTypeName(tgt->GetType())
  1360. << " may not be linked into another target. "
  1361. << "One may link only to STATIC or SHARED libraries, or "
  1362. << "to executables with the ENABLE_EXPORTS property set.";
  1363. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1364. }
  1365. }
  1366. i->second.AddLinkLibrary( *this, target, lib, llt );
  1367. }
  1368. else
  1369. {
  1370. cmOStringStream e;
  1371. e << "Attempt to add link library \""
  1372. << lib << "\" to target \""
  1373. << target << "\" which is not built in this directory.";
  1374. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1375. }
  1376. }
  1377. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  1378. const char* d)
  1379. {
  1380. cmTargets::iterator i = this->Targets.find(target);
  1381. if ( i != this->Targets.end())
  1382. {
  1383. if(this->IsAlias(target))
  1384. {
  1385. cmOStringStream e;
  1386. e << "ALIAS target \"" << target << "\" "
  1387. << "may not be linked into another target.";
  1388. this->IssueMessage(cmake::FATAL_ERROR, e.str().c_str());
  1389. return;
  1390. }
  1391. i->second.AddLinkDirectory( d );
  1392. }
  1393. else
  1394. {
  1395. cmSystemTools::Error
  1396. ("Attempt to add link directories to non-existent target: ",
  1397. target, " for directory ", d);
  1398. }
  1399. }
  1400. void cmMakefile::AddLinkLibrary(const char* lib)
  1401. {
  1402. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  1403. }
  1404. void cmMakefile::AddLinkDirectory(const char* dir)
  1405. {
  1406. // Don't add a link directory that is already present. Yes, this
  1407. // linear search results in n^2 behavior, but n won't be getting
  1408. // much bigger than 20. We cannot use a set because of order
  1409. // dependency of the link search path.
  1410. if(!dir)
  1411. {
  1412. return;
  1413. }
  1414. // remove trailing slashes
  1415. if(dir[strlen(dir)-1] == '/')
  1416. {
  1417. std::string newdir = dir;
  1418. newdir = newdir.substr(0, newdir.size()-1);
  1419. if(std::find(this->LinkDirectories.begin(),
  1420. this->LinkDirectories.end(),
  1421. newdir.c_str()) == this->LinkDirectories.end())
  1422. {
  1423. this->LinkDirectories.push_back(newdir);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. if(std::find(this->LinkDirectories.begin(),
  1429. this->LinkDirectories.end(), dir)
  1430. == this->LinkDirectories.end())
  1431. {
  1432. this->LinkDirectories.push_back(dir);
  1433. }
  1434. }
  1435. }
  1436. void cmMakefile::InitializeFromParent()
  1437. {
  1438. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  1439. // Initialize definitions with the closure of the parent scope.
  1440. this->Internal->VarStack.top() = parent->Internal->VarStack.top().Closure();
  1441. const std::vector<cmValueWithOrigin>& parentIncludes =
  1442. parent->GetIncludeDirectoriesEntries();
  1443. this->IncludeDirectoriesEntries.insert(this->IncludeDirectoriesEntries.end(),
  1444. parentIncludes.begin(),
  1445. parentIncludes.end());
  1446. const std::vector<cmValueWithOrigin>& parentOptions =
  1447. parent->GetCompileOptionsEntries();
  1448. this->CompileOptionsEntries.insert(this->CompileOptionsEntries.end(),
  1449. parentOptions.begin(),
  1450. parentOptions.end());
  1451. const std::vector<cmValueWithOrigin>& parentDefines =
  1452. parent->GetCompileDefinitionsEntries();
  1453. this->CompileDefinitionsEntries.insert(this->CompileDefinitionsEntries.end(),
  1454. parentDefines.begin(),
  1455. parentDefines.end());
  1456. this->SystemIncludeDirectories = parent->SystemIncludeDirectories;
  1457. // define flags
  1458. this->DefineFlags = parent->DefineFlags;
  1459. this->DefineFlagsOrig = parent->DefineFlagsOrig;
  1460. // Include transform property. There is no per-config version.
  1461. {
  1462. const char* prop = "IMPLICIT_DEPENDS_INCLUDE_TRANSFORM";
  1463. this->SetProperty(prop, parent->GetProperty(prop));
  1464. }
  1465. // compile definitions property and per-config versions
  1466. cmPolicies::PolicyStatus polSt = this->GetPolicyStatus(cmPolicies::CMP0043);
  1467. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD)
  1468. {
  1469. this->SetProperty("COMPILE_DEFINITIONS",
  1470. parent->GetProperty("COMPILE_DEFINITIONS"));
  1471. std::vector<std::string> configs;
  1472. this->GetConfigurations(configs);
  1473. for(std::vector<std::string>::const_iterator ci = configs.begin();
  1474. ci != configs.end(); ++ci)
  1475. {
  1476. std::string defPropName = "COMPILE_DEFINITIONS_";
  1477. defPropName += cmSystemTools::UpperCase(*ci);
  1478. const char* prop = parent->GetProperty(defPropName.c_str());
  1479. this->SetProperty(defPropName.c_str(), prop);
  1480. }
  1481. }
  1482. // link libraries
  1483. this->LinkLibraries = parent->LinkLibraries;
  1484. // link directories
  1485. this->LinkDirectories = parent->LinkDirectories;
  1486. // the initial project name
  1487. this->ProjectName = parent->ProjectName;
  1488. // Copy include regular expressions.
  1489. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  1490. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  1491. // Imported targets.
  1492. this->ImportedTargets = parent->ImportedTargets;
  1493. }
  1494. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  1495. {
  1496. // copy our variables from the child makefile
  1497. lg2->GetMakefile()->InitializeFromParent();
  1498. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  1499. if (this->GetCMakeInstance()->GetDebugOutput())
  1500. {
  1501. std::string msg=" Entering ";
  1502. msg += lg2->GetMakefile()->GetCurrentDirectory();
  1503. cmSystemTools::Message(msg.c_str());
  1504. }
  1505. // finally configure the subdir
  1506. lg2->Configure();
  1507. if (this->GetCMakeInstance()->GetDebugOutput())
  1508. {
  1509. std::string msg=" Returning to ";
  1510. msg += this->GetCurrentDirectory();
  1511. cmSystemTools::Message(msg.c_str());
  1512. }
  1513. }
  1514. void cmMakefile::AddSubDirectory(const char* sub,
  1515. bool excludeFromAll, bool preorder)
  1516. {
  1517. // the source path must be made full if it isn't already
  1518. std::string srcPath = sub;
  1519. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  1520. {
  1521. srcPath = this->GetCurrentDirectory();
  1522. srcPath += "/";
  1523. srcPath += sub;
  1524. }
  1525. // binary path must be made full if it isn't already
  1526. std::string binPath = sub;
  1527. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  1528. {
  1529. binPath = this->GetCurrentOutputDirectory();
  1530. binPath += "/";
  1531. binPath += sub;
  1532. }
  1533. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  1534. excludeFromAll, preorder, false);
  1535. }
  1536. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  1537. bool excludeFromAll, bool preorder,
  1538. bool immediate)
  1539. {
  1540. // Make sure the binary directory is unique.
  1541. if(!this->EnforceUniqueDir(srcPath, binPath))
  1542. {
  1543. return;
  1544. }
  1545. // create a new local generator and set its parent
  1546. cmLocalGenerator *lg2 =
  1547. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  1548. lg2->SetParent(this->LocalGenerator);
  1549. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  1550. // set the subdirs start dirs
  1551. lg2->GetMakefile()->SetStartDirectory(srcPath);
  1552. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  1553. if(excludeFromAll)
  1554. {
  1555. lg2->GetMakefile()->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1556. }
  1557. lg2->GetMakefile()->SetPreOrder(preorder);
  1558. if (immediate)
  1559. {
  1560. this->ConfigureSubDirectory(lg2);
  1561. }
  1562. }
  1563. //----------------------------------------------------------------------------
  1564. void cmMakefile::AddIncludeDirectories(const std::vector<std::string> &incs,
  1565. bool before)
  1566. {
  1567. if (incs.empty())
  1568. {
  1569. return;
  1570. }
  1571. std::string incString;
  1572. std::string sep;
  1573. for(std::vector<std::string>::const_iterator li = incs.begin();
  1574. li != incs.end(); ++li)
  1575. {
  1576. incString += sep + *li;
  1577. sep = ";";
  1578. }
  1579. std::vector<cmValueWithOrigin>::iterator position =
  1580. before ? this->IncludeDirectoriesEntries.begin()
  1581. : this->IncludeDirectoriesEntries.end();
  1582. cmListFileBacktrace lfbt;
  1583. this->GetBacktrace(lfbt);
  1584. cmValueWithOrigin entry(incString, lfbt);
  1585. this->IncludeDirectoriesEntries.insert(position, entry);
  1586. // Property on each target:
  1587. for (cmTargets::iterator l = this->Targets.begin();
  1588. l != this->Targets.end(); ++l)
  1589. {
  1590. cmTarget &t = l->second;
  1591. t.InsertInclude(entry, before);
  1592. }
  1593. }
  1594. //----------------------------------------------------------------------------
  1595. void
  1596. cmMakefile::AddSystemIncludeDirectories(const std::set<cmStdString> &incs)
  1597. {
  1598. for(std::set<cmStdString>::const_iterator li = incs.begin();
  1599. li != incs.end(); ++li)
  1600. {
  1601. this->SystemIncludeDirectories.insert(*li);
  1602. }
  1603. for (cmTargets::iterator l = this->Targets.begin();
  1604. l != this->Targets.end(); ++l)
  1605. {
  1606. cmTarget &t = l->second;
  1607. t.AddSystemIncludeDirectories(incs);
  1608. }
  1609. }
  1610. void cmMakefile::AddDefinition(const char* name, const char* value)
  1611. {
  1612. if (!value )
  1613. {
  1614. return;
  1615. }
  1616. this->Internal->VarStack.top().Set(name, value);
  1617. if (this->Internal->VarUsageStack.size() &&
  1618. this->VariableInitialized(name))
  1619. {
  1620. this->CheckForUnused("changing definition", name);
  1621. this->Internal->VarUsageStack.top().erase(name);
  1622. }
  1623. this->Internal->VarInitStack.top().insert(name);
  1624. #ifdef CMAKE_BUILD_WITH_CMAKE
  1625. cmVariableWatch* vv = this->GetVariableWatch();
  1626. if ( vv )
  1627. {
  1628. vv->VariableAccessed(name,
  1629. cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1630. value,
  1631. this);
  1632. }
  1633. #endif
  1634. }
  1635. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  1636. const char* doc,
  1637. cmCacheManager::CacheEntryType type,
  1638. bool force)
  1639. {
  1640. const char* val = value;
  1641. cmCacheManager::CacheIterator it =
  1642. this->GetCacheManager()->GetCacheIterator(name);
  1643. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1644. it.Initialized())
  1645. {
  1646. // if this is not a force, then use the value from the cache
  1647. // if it is a force, then use the value being passed in
  1648. if(!force)
  1649. {
  1650. val = it.GetValue();
  1651. }
  1652. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  1653. {
  1654. std::vector<std::string>::size_type cc;
  1655. std::vector<std::string> files;
  1656. std::string nvalue = "";
  1657. cmSystemTools::ExpandListArgument(val, files);
  1658. for ( cc = 0; cc < files.size(); cc ++ )
  1659. {
  1660. if(!cmSystemTools::IsOff(files[cc].c_str()))
  1661. {
  1662. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  1663. }
  1664. if ( cc > 0 )
  1665. {
  1666. nvalue += ";";
  1667. }
  1668. nvalue += files[cc];
  1669. }
  1670. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1671. val = it.GetValue();
  1672. }
  1673. }
  1674. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  1675. // if there was a definition then remove it
  1676. this->Internal->VarStack.top().Set(name, 0);
  1677. }
  1678. void cmMakefile::AddDefinition(const char* name, bool value)
  1679. {
  1680. this->Internal->VarStack.top().Set(name, value? "ON" : "OFF");
  1681. if (this->Internal->VarUsageStack.size() &&
  1682. this->VariableInitialized(name))
  1683. {
  1684. this->CheckForUnused("changing definition", name);
  1685. this->Internal->VarUsageStack.top().erase(name);
  1686. }
  1687. this->Internal->VarInitStack.top().insert(name);
  1688. #ifdef CMAKE_BUILD_WITH_CMAKE
  1689. cmVariableWatch* vv = this->GetVariableWatch();
  1690. if ( vv )
  1691. {
  1692. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1693. value?"ON":"OFF", this);
  1694. }
  1695. #endif
  1696. }
  1697. void cmMakefile::CheckForUnusedVariables() const
  1698. {
  1699. if (!this->WarnUnused)
  1700. {
  1701. return;
  1702. }
  1703. const cmDefinitions& defs = this->Internal->VarStack.top();
  1704. const std::set<cmStdString>& locals = defs.LocalKeys();
  1705. std::set<cmStdString>::const_iterator it = locals.begin();
  1706. for (; it != locals.end(); ++it)
  1707. {
  1708. this->CheckForUnused("out of scope", it->c_str());
  1709. }
  1710. }
  1711. void cmMakefile::MarkVariableAsUsed(const char* var)
  1712. {
  1713. this->Internal->VarUsageStack.top().insert(var);
  1714. }
  1715. bool cmMakefile::VariableInitialized(const char* var) const
  1716. {
  1717. if(this->Internal->VarInitStack.top().find(var) !=
  1718. this->Internal->VarInitStack.top().end())
  1719. {
  1720. return true;
  1721. }
  1722. return false;
  1723. }
  1724. bool cmMakefile::VariableUsed(const char* var) const
  1725. {
  1726. if(this->Internal->VarUsageStack.top().find(var) !=
  1727. this->Internal->VarUsageStack.top().end())
  1728. {
  1729. return true;
  1730. }
  1731. return false;
  1732. }
  1733. void cmMakefile::CheckForUnused(const char* reason, const char* name) const
  1734. {
  1735. if (this->WarnUnused && !this->VariableUsed(name))
  1736. {
  1737. cmStdString path;
  1738. cmListFileBacktrace bt;
  1739. if (this->CallStack.size())
  1740. {
  1741. const cmListFileContext* file = this->CallStack.back().Context;
  1742. bt.push_back(*file);
  1743. path = file->FilePath.c_str();
  1744. }
  1745. else
  1746. {
  1747. path = this->GetStartDirectory();
  1748. path += "/CMakeLists.txt";
  1749. cmListFileContext lfc;
  1750. lfc.FilePath = path;
  1751. lfc.Line = 0;
  1752. bt.push_back(lfc);
  1753. }
  1754. if (this->CheckSystemVars ||
  1755. cmSystemTools::IsSubDirectory(path.c_str(),
  1756. this->GetHomeDirectory()) ||
  1757. (cmSystemTools::IsSubDirectory(path.c_str(),
  1758. this->GetHomeOutputDirectory()) &&
  1759. !cmSystemTools::IsSubDirectory(path.c_str(),
  1760. cmake::GetCMakeFilesDirectory())))
  1761. {
  1762. cmOStringStream msg;
  1763. msg << "unused variable (" << reason << ") \'" << name << "\'";
  1764. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
  1765. msg.str().c_str(),
  1766. bt);
  1767. }
  1768. }
  1769. }
  1770. void cmMakefile::RemoveDefinition(const char* name)
  1771. {
  1772. this->Internal->VarStack.top().Set(name, 0);
  1773. if (this->Internal->VarUsageStack.size() &&
  1774. this->VariableInitialized(name))
  1775. {
  1776. this->CheckForUnused("unsetting", name);
  1777. this->Internal->VarUsageStack.top().erase(name);
  1778. }
  1779. this->Internal->VarInitStack.top().insert(name);
  1780. #ifdef CMAKE_BUILD_WITH_CMAKE
  1781. cmVariableWatch* vv = this->GetVariableWatch();
  1782. if ( vv )
  1783. {
  1784. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS,
  1785. 0, this);
  1786. }
  1787. #endif
  1788. }
  1789. void cmMakefile::RemoveCacheDefinition(const char* name)
  1790. {
  1791. this->GetCacheManager()->RemoveCacheEntry(name);
  1792. }
  1793. void cmMakefile::SetProjectName(const char* p)
  1794. {
  1795. this->ProjectName = p;
  1796. }
  1797. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  1798. {
  1799. // for these targets do not add anything
  1800. switch(target.GetType())
  1801. {
  1802. case cmTarget::UTILITY:
  1803. case cmTarget::GLOBAL_TARGET:
  1804. case cmTarget::INTERFACE_LIBRARY:
  1805. return;
  1806. default:;
  1807. }
  1808. std::vector<std::string>::iterator j;
  1809. for(j = this->LinkDirectories.begin();
  1810. j != this->LinkDirectories.end(); ++j)
  1811. {
  1812. target.AddLinkDirectory(j->c_str());
  1813. }
  1814. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1815. }
  1816. void cmMakefile::AddAlias(const char* lname, cmTarget *tgt)
  1817. {
  1818. this->AliasTargets[lname] = tgt;
  1819. this->LocalGenerator->GetGlobalGenerator()->AddAlias(lname, tgt);
  1820. }
  1821. cmTarget* cmMakefile::AddLibrary(const char* lname, 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 char* 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 char *cname) const
  1876. {
  1877. std::string name = cname;
  1878. std::string out;
  1879. // look through all the source files that have custom commands
  1880. // and see if the custom command has the passed source file as an output
  1881. for(std::vector<cmSourceFile*>::const_iterator i =
  1882. this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
  1883. {
  1884. // does this source file have a custom command?
  1885. if ((*i)->GetCustomCommand())
  1886. {
  1887. // is the output of the custom command match the source files name
  1888. const std::vector<std::string>& outputs =
  1889. (*i)->GetCustomCommand()->GetOutputs();
  1890. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1891. o != outputs.end(); ++o)
  1892. {
  1893. out = *o;
  1894. std::string::size_type pos = out.rfind(name);
  1895. // If the output matches exactly
  1896. if (pos != out.npos &&
  1897. pos == out.size() - name.size() &&
  1898. (pos ==0 || out[pos-1] == '/'))
  1899. {
  1900. return *i;
  1901. }
  1902. }
  1903. }
  1904. }
  1905. // otherwise return NULL
  1906. return 0;
  1907. }
  1908. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname) const
  1909. {
  1910. std::string name = cname;
  1911. // If the queried path is not absolute we use the backward compatible
  1912. // linear-time search for an output with a matching suffix.
  1913. if(!cmSystemTools::FileIsFullPath(cname))
  1914. {
  1915. return LinearGetSourceFileWithOutput(cname);
  1916. }
  1917. // Otherwise we use an efficient lookup map.
  1918. OutputToSourceMap::const_iterator o = this->OutputToSource.find(name);
  1919. if (o != this->OutputToSource.end())
  1920. {
  1921. return (*o).second;
  1922. }
  1923. return 0;
  1924. }
  1925. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1926. cmSourceGroup*
  1927. cmMakefile::GetSourceGroup(const std::vector<std::string>&name) const
  1928. {
  1929. cmSourceGroup* sg = 0;
  1930. // first look for source group starting with the same as the one we wants
  1931. for (std::vector<cmSourceGroup>::const_iterator
  1932. sgIt = this->SourceGroups.begin();
  1933. sgIt != this->SourceGroups.end(); ++sgIt)
  1934. {
  1935. std::string sgName = sgIt->GetName();
  1936. if(sgName == name[0])
  1937. {
  1938. sg = const_cast<cmSourceGroup*>(&(*sgIt));
  1939. break;
  1940. }
  1941. }
  1942. if(sg != 0)
  1943. {
  1944. // iterate through its children to find match source group
  1945. for(unsigned int i=1; i<name.size(); ++i)
  1946. {
  1947. sg = sg->LookupChild(name[i].c_str());
  1948. if(sg == 0)
  1949. {
  1950. break;
  1951. }
  1952. }
  1953. }
  1954. return sg;
  1955. }
  1956. void cmMakefile::AddSourceGroup(const char* name,
  1957. const char* regex)
  1958. {
  1959. if (name)
  1960. {
  1961. std::vector<std::string> nameVector;
  1962. nameVector.push_back(name);
  1963. AddSourceGroup(nameVector, regex);
  1964. }
  1965. }
  1966. void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
  1967. const char* regex)
  1968. {
  1969. cmSourceGroup* sg = 0;
  1970. std::vector<std::string> currentName;
  1971. int i = 0;
  1972. const int lastElement = static_cast<int>(name.size()-1);
  1973. for(i=lastElement; i>=0; --i)
  1974. {
  1975. currentName.assign(name.begin(), name.begin()+i+1);
  1976. sg = this->GetSourceGroup(currentName);
  1977. if(sg != 0)
  1978. {
  1979. break;
  1980. }
  1981. }
  1982. // i now contains the index of the last found component
  1983. if(i==lastElement)
  1984. {
  1985. // group already exists, replace its regular expression
  1986. if ( regex )
  1987. {
  1988. // We only want to set the regular expression. If there are already
  1989. // source files in the group, we don't want to remove them.
  1990. sg->SetGroupRegex(regex);
  1991. }
  1992. return;
  1993. }
  1994. else if(i==-1)
  1995. {
  1996. // group does not exist nor belong to any existing group
  1997. // add its first component
  1998. this->SourceGroups.push_back(cmSourceGroup(name[0].c_str(), regex));
  1999. sg = this->GetSourceGroup(currentName);
  2000. i = 0; // last component found
  2001. }
  2002. // build the whole source group path
  2003. const char* fullname = sg->GetFullName();
  2004. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  2005. if(strlen(fullname))
  2006. {
  2007. std::string guidName = "SG_Filter_";
  2008. guidName += fullname;
  2009. gg->CreateGUID(guidName.c_str());
  2010. }
  2011. for(++i; i<=lastElement; ++i)
  2012. {
  2013. sg->AddChild(cmSourceGroup(name[i].c_str(), 0, sg->GetFullName()));
  2014. sg = sg->LookupChild(name[i].c_str());
  2015. fullname = sg->GetFullName();
  2016. if(strlen(fullname))
  2017. {
  2018. std::string guidName = "SG_Filter_";
  2019. guidName += fullname;
  2020. gg->CreateGUID(guidName.c_str());
  2021. }
  2022. }
  2023. sg->SetGroupRegex(regex);
  2024. }
  2025. #endif
  2026. void cmMakefile::AddExtraDirectory(const char* dir)
  2027. {
  2028. this->AuxSourceDirectories.push_back(dir);
  2029. }
  2030. static bool mightExpandVariablesCMP0019(const char* s)
  2031. {
  2032. return s && *s && strstr(s,"${") && strchr(s,'}');
  2033. }
  2034. void cmMakefile::ExpandVariablesCMP0019()
  2035. {
  2036. // Drop this ancient compatibility behavior with a policy.
  2037. cmPolicies::PolicyStatus pol = this->GetPolicyStatus(cmPolicies::CMP0019);
  2038. if(pol != cmPolicies::OLD && pol != cmPolicies::WARN)
  2039. {
  2040. return;
  2041. }
  2042. cmOStringStream w;
  2043. const char *includeDirs = this->GetProperty("INCLUDE_DIRECTORIES");
  2044. if(mightExpandVariablesCMP0019(includeDirs))
  2045. {
  2046. std::string dirs = includeDirs;
  2047. this->ExpandVariablesInString(dirs, true, true);
  2048. if(pol == cmPolicies::WARN && dirs != includeDirs)
  2049. {
  2050. w << "Evaluated directory INCLUDE_DIRECTORIES\n"
  2051. << " " << includeDirs << "\n"
  2052. << "as\n"
  2053. << " " << dirs << "\n";
  2054. }
  2055. this->SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  2056. }
  2057. // Also for each target's INCLUDE_DIRECTORIES property:
  2058. for (cmTargets::iterator l = this->Targets.begin();
  2059. l != this->Targets.end(); ++l)
  2060. {
  2061. cmTarget &t = l->second;
  2062. if (t.GetType() == cmTarget::INTERFACE_LIBRARY)
  2063. {
  2064. continue;
  2065. }
  2066. includeDirs = t.GetProperty("INCLUDE_DIRECTORIES");
  2067. if(mightExpandVariablesCMP0019(includeDirs))
  2068. {
  2069. std::string dirs = includeDirs;
  2070. this->ExpandVariablesInString(dirs, true, true);
  2071. if(pol == cmPolicies::WARN && dirs != includeDirs)
  2072. {
  2073. w << "Evaluated target " << t.GetName() << " INCLUDE_DIRECTORIES\n"
  2074. << " " << includeDirs << "\n"
  2075. << "as\n"
  2076. << " " << dirs << "\n";
  2077. }
  2078. t.SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  2079. }
  2080. }
  2081. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  2082. d != this->LinkDirectories.end(); ++d)
  2083. {
  2084. if(mightExpandVariablesCMP0019(d->c_str()))
  2085. {
  2086. std::string orig = *d;
  2087. this->ExpandVariablesInString(*d, true, true);
  2088. if(pol == cmPolicies::WARN && *d != orig)
  2089. {
  2090. w << "Evaluated link directory\n"
  2091. << " " << orig << "\n"
  2092. << "as\n"
  2093. << " " << *d << "\n";
  2094. }
  2095. }
  2096. }
  2097. for(cmTarget::LinkLibraryVectorType::iterator l =
  2098. this->LinkLibraries.begin();
  2099. l != this->LinkLibraries.end(); ++l)
  2100. {
  2101. if(mightExpandVariablesCMP0019(l->first.c_str()))
  2102. {
  2103. std::string orig = l->first;
  2104. this->ExpandVariablesInString(l->first, true, true);
  2105. if(pol == cmPolicies::WARN && l->first != orig)
  2106. {
  2107. w << "Evaluated link library\n"
  2108. << " " << orig << "\n"
  2109. << "as\n"
  2110. << " " << l->first << "\n";
  2111. }
  2112. }
  2113. }
  2114. if(!w.str().empty())
  2115. {
  2116. cmOStringStream m;
  2117. m << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0019)
  2118. << "\n"
  2119. << "The following variable evaluations were encountered:\n"
  2120. << w.str();
  2121. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  2122. }
  2123. }
  2124. bool cmMakefile::IsOn(const char* name) const
  2125. {
  2126. const char* value = this->GetDefinition(name);
  2127. return cmSystemTools::IsOn(value);
  2128. }
  2129. bool cmMakefile::IsSet(const char* name) const
  2130. {
  2131. const char* value = this->GetDefinition(name);
  2132. if ( !value )
  2133. {
  2134. return false;
  2135. }
  2136. if ( ! *value )
  2137. {
  2138. return false;
  2139. }
  2140. if ( cmSystemTools::IsNOTFOUND(value) )
  2141. {
  2142. return false;
  2143. }
  2144. return true;
  2145. }
  2146. bool cmMakefile::PlatformIs64Bit() const
  2147. {
  2148. if(const char* sizeof_dptr = this->GetDefinition("CMAKE_SIZEOF_VOID_P"))
  2149. {
  2150. return atoi(sizeof_dptr) == 8;
  2151. }
  2152. return false;
  2153. }
  2154. const char* cmMakefile::GetSONameFlag(const char* language) const
  2155. {
  2156. std::string name = "CMAKE_SHARED_LIBRARY_SONAME";
  2157. if(language)
  2158. {
  2159. name += "_";
  2160. name += language;
  2161. }
  2162. name += "_FLAG";
  2163. return GetDefinition(name.c_str());
  2164. }
  2165. bool cmMakefile::CanIWriteThisFile(const char* fileName) const
  2166. {
  2167. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  2168. {
  2169. return true;
  2170. }
  2171. // If we are doing an in-source build, then the test will always fail
  2172. if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
  2173. this->GetHomeOutputDirectory()) )
  2174. {
  2175. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  2176. {
  2177. return false;
  2178. }
  2179. return true;
  2180. }
  2181. // Check if this is a subdirectory of the source tree but not a
  2182. // subdirectory of the build tree
  2183. if ( cmSystemTools::IsSubDirectory(fileName,
  2184. this->GetHomeDirectory()) &&
  2185. !cmSystemTools::IsSubDirectory(fileName,
  2186. this->GetHomeOutputDirectory()) )
  2187. {
  2188. return false;
  2189. }
  2190. return true;
  2191. }
  2192. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  2193. {
  2194. const char* ret = this->GetDefinition(name);
  2195. if(!ret)
  2196. {
  2197. cmSystemTools::Error("Error required internal CMake variable not "
  2198. "set, cmake may be not be built correctly.\n",
  2199. "Missing variable is:\n",
  2200. name);
  2201. return "";
  2202. }
  2203. return ret;
  2204. }
  2205. bool cmMakefile::IsDefinitionSet(const char* name) const
  2206. {
  2207. const char* def = this->Internal->VarStack.top().Get(name);
  2208. this->Internal->VarUsageStack.top().insert(name);
  2209. if(!def)
  2210. {
  2211. def = this->GetCacheManager()->GetCacheValue(name);
  2212. }
  2213. #ifdef CMAKE_BUILD_WITH_CMAKE
  2214. if(cmVariableWatch* vv = this->GetVariableWatch())
  2215. {
  2216. if(!def)
  2217. {
  2218. vv->VariableAccessed
  2219. (name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS,
  2220. def, this);
  2221. }
  2222. }
  2223. #endif
  2224. return def?true:false;
  2225. }
  2226. const char* cmMakefile::GetDefinition(const char* name) const
  2227. {
  2228. if (this->WarnUnused)
  2229. {
  2230. this->Internal->VarUsageStack.top().insert(name);
  2231. }
  2232. const char* def = this->Internal->VarStack.top().Get(name);
  2233. if(!def)
  2234. {
  2235. def = this->GetCacheManager()->GetCacheValue(name);
  2236. }
  2237. #ifdef CMAKE_BUILD_WITH_CMAKE
  2238. cmVariableWatch* vv = this->GetVariableWatch();
  2239. if ( vv )
  2240. {
  2241. if ( def )
  2242. {
  2243. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS,
  2244. def, this);
  2245. }
  2246. else
  2247. {
  2248. // are unknown access allowed
  2249. const char* allow = this->Internal->VarStack.top()
  2250. .Get("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  2251. if(cmSystemTools::IsOn(allow))
  2252. {
  2253. vv->VariableAccessed(name,
  2254. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2255. }
  2256. else
  2257. {
  2258. vv->VariableAccessed(name,
  2259. cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  2260. }
  2261. }
  2262. }
  2263. #endif
  2264. return def;
  2265. }
  2266. const char* cmMakefile::GetSafeDefinition(const char* def) const
  2267. {
  2268. const char* ret = this->GetDefinition(def);
  2269. if(!ret)
  2270. {
  2271. return "";
  2272. }
  2273. return ret;
  2274. }
  2275. std::vector<std::string> cmMakefile
  2276. ::GetDefinitions(int cacheonly /* = 0 */) const
  2277. {
  2278. std::set<cmStdString> definitions;
  2279. if ( !cacheonly )
  2280. {
  2281. definitions = this->Internal->VarStack.top().ClosureKeys();
  2282. }
  2283. cmCacheManager::CacheIterator cit =
  2284. this->GetCacheManager()->GetCacheIterator();
  2285. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2286. {
  2287. definitions.insert(cit.GetName());
  2288. }
  2289. std::vector<std::string> res;
  2290. std::set<cmStdString>::iterator fit;
  2291. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  2292. {
  2293. res.push_back(*fit);
  2294. }
  2295. return res;
  2296. }
  2297. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  2298. {
  2299. return this->ExpandVariablesInString(source, false, false);
  2300. }
  2301. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  2302. bool escapeQuotes,
  2303. bool noEscapes,
  2304. bool atOnly,
  2305. const char* filename,
  2306. long line,
  2307. bool removeEmpty,
  2308. bool replaceAt) const
  2309. {
  2310. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  2311. {
  2312. return source.c_str();
  2313. }
  2314. // Special-case the @ONLY mode.
  2315. if(atOnly)
  2316. {
  2317. if(!noEscapes || !removeEmpty || !replaceAt)
  2318. {
  2319. // This case should never be called. At-only is for
  2320. // configure-file/string which always does no escapes.
  2321. this->IssueMessage(cmake::INTERNAL_ERROR,
  2322. "ExpandVariablesInString @ONLY called "
  2323. "on something with escapes.");
  2324. }
  2325. // Store an original copy of the input.
  2326. std::string input = source;
  2327. // Start with empty output.
  2328. source = "";
  2329. // Look for one @VAR@ at a time.
  2330. const char* in = input.c_str();
  2331. while(this->cmAtVarRegex.find(in))
  2332. {
  2333. // Get the range of the string to replace.
  2334. const char* first = in + this->cmAtVarRegex.start();
  2335. const char* last = in + this->cmAtVarRegex.end();
  2336. // Store the unchanged part of the string now.
  2337. source.append(in, first-in);
  2338. // Lookup the definition of VAR.
  2339. std::string var(first+1, last-first-2);
  2340. if(const char* val = this->GetDefinition(var.c_str()))
  2341. {
  2342. // Store the value in the output escaping as requested.
  2343. if(escapeQuotes)
  2344. {
  2345. source.append(cmSystemTools::EscapeQuotes(val));
  2346. }
  2347. else
  2348. {
  2349. source.append(val);
  2350. }
  2351. }
  2352. // Continue looking for @VAR@ further along the string.
  2353. in = last;
  2354. }
  2355. // Append the rest of the unchanged part of the string.
  2356. source.append(in);
  2357. return source.c_str();
  2358. }
  2359. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2360. // with GetDefinition(), if not found in the map, nothing is expanded.
  2361. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2362. // the current environment variables.
  2363. cmCommandArgumentParserHelper parser;
  2364. parser.SetMakefile(this);
  2365. parser.SetLineFile(line, filename);
  2366. parser.SetEscapeQuotes(escapeQuotes);
  2367. parser.SetNoEscapeMode(noEscapes);
  2368. parser.SetReplaceAtSyntax(replaceAt);
  2369. parser.SetRemoveEmpty(removeEmpty);
  2370. int res = parser.ParseString(source.c_str(), 0);
  2371. const char* emsg = parser.GetError();
  2372. if ( res && !emsg[0] )
  2373. {
  2374. source = parser.GetResult();
  2375. }
  2376. else
  2377. {
  2378. // Construct the main error message.
  2379. cmOStringStream error;
  2380. error << "Syntax error in cmake code ";
  2381. if(filename && line > 0)
  2382. {
  2383. // This filename and line number may be more specific than the
  2384. // command context because one command invocation can have
  2385. // arguments on multiple lines.
  2386. error << "at\n"
  2387. << " " << filename << ":" << line << "\n";
  2388. }
  2389. error << "when parsing string\n"
  2390. << " " << source.c_str() << "\n";
  2391. error << emsg;
  2392. // If the parser failed ("res" is false) then this is a real
  2393. // argument parsing error, so the policy applies. Otherwise the
  2394. // parser reported an error message without failing because the
  2395. // helper implementation is unhappy, which has always reported an
  2396. // error.
  2397. cmake::MessageType mtype = cmake::FATAL_ERROR;
  2398. if(!res)
  2399. {
  2400. // This is a real argument parsing error. Use policy CMP0010 to
  2401. // decide whether it is an error.
  2402. switch(this->GetPolicyStatus(cmPolicies::CMP0010))
  2403. {
  2404. case cmPolicies::WARN:
  2405. error << "\n"
  2406. << (this->GetPolicies()
  2407. ->GetPolicyWarning(cmPolicies::CMP0010));
  2408. case cmPolicies::OLD:
  2409. // OLD behavior is to just warn and continue.
  2410. mtype = cmake::AUTHOR_WARNING;
  2411. break;
  2412. case cmPolicies::REQUIRED_IF_USED:
  2413. case cmPolicies::REQUIRED_ALWAYS:
  2414. error << "\n"
  2415. << (this->GetPolicies()
  2416. ->GetRequiredPolicyError(cmPolicies::CMP0010));
  2417. case cmPolicies::NEW:
  2418. // NEW behavior is to report the error.
  2419. cmSystemTools::SetFatalErrorOccured();
  2420. break;
  2421. }
  2422. }
  2423. this->IssueMessage(mtype, error.str());
  2424. }
  2425. return source.c_str();
  2426. }
  2427. void cmMakefile::RemoveVariablesInString(std::string& source,
  2428. bool atOnly) const
  2429. {
  2430. if(!atOnly)
  2431. {
  2432. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  2433. while (var.find(source))
  2434. {
  2435. source.erase(var.start(),var.end() - var.start());
  2436. }
  2437. }
  2438. if(!atOnly)
  2439. {
  2440. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  2441. while (varb.find(source))
  2442. {
  2443. source.erase(varb.start(),varb.end() - varb.start());
  2444. }
  2445. }
  2446. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  2447. while (var2.find(source))
  2448. {
  2449. source.erase(var2.start(),var2.end() - var2.start());
  2450. }
  2451. }
  2452. /**
  2453. * Add the default definitions to the makefile. These values must not
  2454. * be dependent on anything that isn't known when this cmMakefile instance
  2455. * is constructed.
  2456. */
  2457. void cmMakefile::AddDefaultDefinitions()
  2458. {
  2459. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  2460. With CMake must separate between target and host platform. In most cases
  2461. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  2462. additional set of variables for the host system is required ->
  2463. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  2464. WIN32, UNIX and APPLE are now set in the platform files in
  2465. Modules/Platforms/.
  2466. To keep cmake scripts (-P) and custom language and compiler modules
  2467. working, these variables are still also set here in this place, but they
  2468. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  2469. files are executed. */
  2470. #if defined(_WIN32)
  2471. this->AddDefinition("WIN32", "1");
  2472. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  2473. #else
  2474. this->AddDefinition("UNIX", "1");
  2475. this->AddDefinition("CMAKE_HOST_UNIX", "1");
  2476. #endif
  2477. #if defined(__CYGWIN__)
  2478. if(cmSystemTools::IsOn(cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32")))
  2479. {
  2480. this->AddDefinition("WIN32", "1");
  2481. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  2482. }
  2483. #endif
  2484. #if defined(__APPLE__)
  2485. this->AddDefinition("APPLE", "1");
  2486. this->AddDefinition("CMAKE_HOST_APPLE", "1");
  2487. #endif
  2488. char temp[1024];
  2489. sprintf(temp, "%d", cmVersion::GetMinorVersion());
  2490. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  2491. sprintf(temp, "%d", cmVersion::GetMajorVersion());
  2492. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  2493. sprintf(temp, "%d", cmVersion::GetPatchVersion());
  2494. this->AddDefinition("CMAKE_PATCH_VERSION", temp);
  2495. sprintf(temp, "%d", cmVersion::GetTweakVersion());
  2496. this->AddDefinition("CMAKE_TWEAK_VERSION", temp);
  2497. this->AddDefinition("CMAKE_VERSION", cmVersion::GetCMakeVersion());
  2498. this->AddDefinition("CMAKE_FILES_DIRECTORY",
  2499. cmake::GetCMakeFilesDirectory());
  2500. }
  2501. //----------------------------------------------------------------------------
  2502. const char*
  2503. cmMakefile::GetConfigurations(std::vector<std::string>& configs,
  2504. bool single) const
  2505. {
  2506. if(this->LocalGenerator->GetGlobalGenerator()->IsMultiConfig())
  2507. {
  2508. if(const char* configTypes =
  2509. this->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  2510. {
  2511. cmSystemTools::ExpandListArgument(configTypes, configs);
  2512. }
  2513. return 0;
  2514. }
  2515. else
  2516. {
  2517. const char* buildType = this->GetDefinition("CMAKE_BUILD_TYPE");
  2518. if(single && buildType && *buildType)
  2519. {
  2520. configs.push_back(buildType);
  2521. }
  2522. return buildType;
  2523. }
  2524. }
  2525. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2526. /**
  2527. * Find a source group whose regular expression matches the filename
  2528. * part of the given source name. Search backward through the list of
  2529. * source groups, and take the first matching group found. This way
  2530. * non-inherited SOURCE_GROUP commands will have precedence over
  2531. * inherited ones.
  2532. */
  2533. cmSourceGroup*
  2534. cmMakefile::FindSourceGroup(const char* source,
  2535. std::vector<cmSourceGroup> &groups) const
  2536. {
  2537. // First search for a group that lists the file explicitly.
  2538. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2539. sg != groups.rend(); ++sg)
  2540. {
  2541. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  2542. if(result)
  2543. {
  2544. return result;
  2545. }
  2546. }
  2547. // Now search for a group whose regex matches the file.
  2548. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2549. sg != groups.rend(); ++sg)
  2550. {
  2551. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  2552. if(result)
  2553. {
  2554. return result;
  2555. }
  2556. }
  2557. // Shouldn't get here, but just in case, return the default group.
  2558. return &groups.front();
  2559. }
  2560. #endif
  2561. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff,
  2562. cmExecutionStatus &status)
  2563. {
  2564. // if there are no blockers get out of here
  2565. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  2566. {
  2567. return false;
  2568. }
  2569. // loop over all function blockers to see if any block this command
  2570. // evaluate in reverse, this is critical for balanced IF statements etc
  2571. std::vector<cmFunctionBlocker*>::reverse_iterator pos;
  2572. for (pos = this->FunctionBlockers.rbegin();
  2573. pos != this->FunctionBlockers.rend(); ++pos)
  2574. {
  2575. if((*pos)->IsFunctionBlocked(lff, *this, status))
  2576. {
  2577. return true;
  2578. }
  2579. }
  2580. return false;
  2581. }
  2582. //----------------------------------------------------------------------------
  2583. void cmMakefile::PushFunctionBlockerBarrier()
  2584. {
  2585. this->FunctionBlockerBarriers.push_back(this->FunctionBlockers.size());
  2586. }
  2587. //----------------------------------------------------------------------------
  2588. void cmMakefile::PopFunctionBlockerBarrier(bool reportError)
  2589. {
  2590. // Remove any extra entries pushed on the barrier.
  2591. FunctionBlockersType::size_type barrier =
  2592. this->FunctionBlockerBarriers.back();
  2593. while(this->FunctionBlockers.size() > barrier)
  2594. {
  2595. cmsys::auto_ptr<cmFunctionBlocker> fb(this->FunctionBlockers.back());
  2596. this->FunctionBlockers.pop_back();
  2597. if(reportError)
  2598. {
  2599. // Report the context in which the unclosed block was opened.
  2600. cmListFileContext const& lfc = fb->GetStartingContext();
  2601. cmOStringStream e;
  2602. e << "A logical block opening on the line\n"
  2603. << " " << lfc << "\n"
  2604. << "is not closed.";
  2605. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  2606. reportError = false;
  2607. }
  2608. }
  2609. // Remove the barrier.
  2610. this->FunctionBlockerBarriers.pop_back();
  2611. }
  2612. bool cmMakefile::ExpandArguments(
  2613. std::vector<cmListFileArgument> const& inArgs,
  2614. std::vector<std::string>& outArgs) const
  2615. {
  2616. std::vector<cmListFileArgument>::const_iterator i;
  2617. std::string value;
  2618. outArgs.reserve(inArgs.size());
  2619. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  2620. {
  2621. // No expansion in a bracket argument.
  2622. if(i->Delim == cmListFileArgument::Bracket)
  2623. {
  2624. outArgs.push_back(i->Value);
  2625. continue;
  2626. }
  2627. // Expand the variables in the argument.
  2628. value = i->Value;
  2629. this->ExpandVariablesInString(value, false, false, false,
  2630. i->FilePath, i->Line,
  2631. false, true);
  2632. // If the argument is quoted, it should be one argument.
  2633. // Otherwise, it may be a list of arguments.
  2634. if(i->Delim == cmListFileArgument::Quoted)
  2635. {
  2636. outArgs.push_back(value);
  2637. }
  2638. else
  2639. {
  2640. cmSystemTools::ExpandListArgument(value, outArgs);
  2641. }
  2642. }
  2643. return !cmSystemTools::GetFatalErrorOccured();
  2644. }
  2645. //----------------------------------------------------------------------------
  2646. void cmMakefile::AddFunctionBlocker(cmFunctionBlocker* fb)
  2647. {
  2648. if(!this->CallStack.empty())
  2649. {
  2650. // Record the context in which the blocker is created.
  2651. fb->SetStartingContext(*(this->CallStack.back().Context));
  2652. }
  2653. this->FunctionBlockers.push_back(fb);
  2654. }
  2655. cmsys::auto_ptr<cmFunctionBlocker>
  2656. cmMakefile::RemoveFunctionBlocker(cmFunctionBlocker* fb,
  2657. const cmListFileFunction& lff)
  2658. {
  2659. // Find the function blocker stack barrier for the current scope.
  2660. // We only remove a blocker whose index is not less than the barrier.
  2661. FunctionBlockersType::size_type barrier = 0;
  2662. if(!this->FunctionBlockerBarriers.empty())
  2663. {
  2664. barrier = this->FunctionBlockerBarriers.back();
  2665. }
  2666. // Search for the function blocker whose scope this command ends.
  2667. for(FunctionBlockersType::size_type
  2668. i = this->FunctionBlockers.size(); i > barrier; --i)
  2669. {
  2670. std::vector<cmFunctionBlocker*>::iterator pos =
  2671. this->FunctionBlockers.begin() + (i - 1);
  2672. if (*pos == fb)
  2673. {
  2674. // Warn if the arguments do not match, but always remove.
  2675. if(!(*pos)->ShouldRemove(lff, *this))
  2676. {
  2677. cmListFileContext const& lfc = fb->GetStartingContext();
  2678. cmOStringStream e;
  2679. e << "A logical block opening on the line\n"
  2680. << " " << lfc << "\n"
  2681. << "closes on the line\n"
  2682. << " " << lff << "\n"
  2683. << "with mis-matching arguments.";
  2684. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2685. }
  2686. cmFunctionBlocker* b = *pos;
  2687. this->FunctionBlockers.erase(pos);
  2688. return cmsys::auto_ptr<cmFunctionBlocker>(b);
  2689. }
  2690. }
  2691. return cmsys::auto_ptr<cmFunctionBlocker>();
  2692. }
  2693. //----------------------------------------------------------------------------
  2694. cmMakefile::LexicalPushPop::LexicalPushPop(cmMakefile* mf):
  2695. Makefile(mf), ReportError(true)
  2696. {
  2697. this->Makefile->PushFunctionBlockerBarrier();
  2698. }
  2699. //----------------------------------------------------------------------------
  2700. cmMakefile::LexicalPushPop::~LexicalPushPop()
  2701. {
  2702. this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
  2703. }
  2704. void cmMakefile::SetHomeDirectory(const char* dir)
  2705. {
  2706. this->cmHomeDirectory = dir;
  2707. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  2708. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  2709. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  2710. {
  2711. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  2712. }
  2713. }
  2714. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  2715. {
  2716. this->HomeOutputDirectory = lib;
  2717. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  2718. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  2719. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  2720. {
  2721. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
  2722. this->GetHomeOutputDirectory());
  2723. }
  2724. }
  2725. void cmMakefile::SetScriptModeFile(const char* scriptfile)
  2726. {
  2727. this->AddDefinition("CMAKE_SCRIPT_MODE_FILE", scriptfile);
  2728. }
  2729. void cmMakefile::SetArgcArgv(const std::vector<std::string>& args)
  2730. {
  2731. cmOStringStream strStream;
  2732. strStream << args.size();
  2733. this->AddDefinition("CMAKE_ARGC", strStream.str().c_str());
  2734. //this->MarkVariableAsUsed("CMAKE_ARGC");
  2735. for (unsigned int t = 0; t < args.size(); ++t)
  2736. {
  2737. cmOStringStream tmpStream;
  2738. tmpStream << "CMAKE_ARGV" << t;
  2739. this->AddDefinition(tmpStream.str().c_str(), args[t].c_str());
  2740. //this->MarkVariableAsUsed(tmpStream.str().c_str());
  2741. }
  2742. }
  2743. //----------------------------------------------------------------------------
  2744. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  2745. {
  2746. cmSourceFileLocation sfl(this, sourceName);
  2747. for(std::vector<cmSourceFile*>::const_iterator
  2748. sfi = this->SourceFiles.begin();
  2749. sfi != this->SourceFiles.end(); ++sfi)
  2750. {
  2751. cmSourceFile* sf = *sfi;
  2752. if(sf->Matches(sfl))
  2753. {
  2754. return sf;
  2755. }
  2756. }
  2757. return 0;
  2758. }
  2759. //----------------------------------------------------------------------------
  2760. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  2761. bool generated)
  2762. {
  2763. if(cmSourceFile* esf = this->GetSource(sourceName))
  2764. {
  2765. return esf;
  2766. }
  2767. else
  2768. {
  2769. cmSourceFile* sf = new cmSourceFile(this, sourceName);
  2770. if(generated)
  2771. {
  2772. sf->SetProperty("GENERATED", "1");
  2773. }
  2774. this->SourceFiles.push_back(sf);
  2775. return sf;
  2776. }
  2777. }
  2778. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang,
  2779. bool optional)
  2780. {
  2781. this->AddDefinition("CMAKE_CFG_INTDIR",
  2782. this->LocalGenerator->GetGlobalGenerator()
  2783. ->GetCMakeCFGIntDir());
  2784. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this,
  2785. optional);
  2786. }
  2787. void cmMakefile::ExpandSourceListArguments(
  2788. std::vector<std::string> const& arguments,
  2789. std::vector<std::string>& newargs, unsigned int /* start */) const
  2790. {
  2791. // now expand the args
  2792. unsigned int i;
  2793. for(i = 0; i < arguments.size(); ++i)
  2794. {
  2795. // List expansion will have been done already.
  2796. newargs.push_back(arguments[i]);
  2797. }
  2798. }
  2799. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  2800. const char *projectName, const char *targetName,
  2801. bool fast,
  2802. const std::vector<std::string> *cmakeArgs,
  2803. std::string *output)
  2804. {
  2805. this->Internal->IsSourceFileTryCompile = fast;
  2806. // does the binary directory exist ? If not create it...
  2807. if (!cmSystemTools::FileIsDirectory(bindir))
  2808. {
  2809. cmSystemTools::MakeDirectory(bindir);
  2810. }
  2811. // change to the tests directory and run cmake
  2812. // use the cmake object instead of calling cmake
  2813. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2814. cmSystemTools::ChangeDirectory(bindir);
  2815. // make sure the same generator is used
  2816. // use this program as the cmake to be run, it should not
  2817. // be run that way but the cmake object requires a vailid path
  2818. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  2819. cmake cm;
  2820. cm.SetIsInTryCompile(true);
  2821. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  2822. (this->LocalGenerator->GetGlobalGenerator()->GetName());
  2823. if (!gg)
  2824. {
  2825. cmSystemTools::Error(
  2826. "Internal CMake error, TryCompile bad GlobalGenerator");
  2827. // return to the original directory
  2828. cmSystemTools::ChangeDirectory(cwd.c_str());
  2829. this->Internal->IsSourceFileTryCompile = false;
  2830. return 1;
  2831. }
  2832. cm.SetGlobalGenerator(gg);
  2833. // do a configure
  2834. cm.SetHomeDirectory(srcdir);
  2835. cm.SetHomeOutputDirectory(bindir);
  2836. cm.SetStartDirectory(srcdir);
  2837. cm.SetStartOutputDirectory(bindir);
  2838. cm.SetGeneratorToolset(this->GetCMakeInstance()->GetGeneratorToolset());
  2839. cm.LoadCache();
  2840. if(!gg->IsMultiConfig())
  2841. {
  2842. if(const char* config =
  2843. this->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION"))
  2844. {
  2845. // Tell the single-configuration generator which one to use.
  2846. // Add this before the user-provided CMake arguments in case
  2847. // one of the arguments is -DCMAKE_BUILD_TYPE=...
  2848. cm.AddCacheEntry("CMAKE_BUILD_TYPE", config,
  2849. "Build configuration", cmCacheManager::STRING);
  2850. }
  2851. }
  2852. // if cmake args were provided then pass them in
  2853. if (cmakeArgs)
  2854. {
  2855. // FIXME: Workaround to ignore unused CLI variables in try-compile.
  2856. //
  2857. // Ideally we should use SetArgs to honor options like --warn-unused-vars.
  2858. // However, there is a subtle problem when certain arguments are passed to
  2859. // a macro wrapping around try_compile or try_run that does not escape
  2860. // semicolons in its parameters but just passes ${ARGV} or ${ARGN}. In
  2861. // this case a list argument like "-DVAR=a;b" gets split into multiple
  2862. // cmake arguments "-DVAR=a" and "b". Currently SetCacheArgs ignores
  2863. // argument "b" and uses just "-DVAR=a", leading to a subtle bug in that
  2864. // the try_compile or try_run does not get the proper value of VAR. If we
  2865. // call SetArgs here then it would treat "b" as the source directory and
  2866. // cause an error such as "The source directory .../CMakeFiles/CMakeTmp/b
  2867. // does not exist", thus breaking the try_compile or try_run completely.
  2868. //
  2869. // Strictly speaking the bug is in the wrapper macro because the CMake
  2870. // language has always flattened nested lists and the macro should escape
  2871. // the semicolons in its arguments before forwarding them. However, this
  2872. // bug is so subtle that projects typically work anyway, usually because
  2873. // the value VAR=a is sufficient for the try_compile or try_run to get the
  2874. // correct result. Calling SetArgs here would break such projects that
  2875. // previously built. Instead we work around the issue by never reporting
  2876. // unused arguments and ignoring options such as --warn-unused-vars.
  2877. cm.SetWarnUnusedCli(false);
  2878. //cm.SetArgs(*cmakeArgs, true);
  2879. cm.SetCacheArgs(*cmakeArgs);
  2880. }
  2881. // to save time we pass the EnableLanguage info directly
  2882. gg->EnableLanguagesFromGenerator
  2883. (this->LocalGenerator->GetGlobalGenerator(), this);
  2884. if(this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS"))
  2885. {
  2886. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  2887. "TRUE", "", cmCacheManager::INTERNAL);
  2888. }
  2889. else
  2890. {
  2891. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS",
  2892. "FALSE", "", cmCacheManager::INTERNAL);
  2893. }
  2894. if (cm.Configure() != 0)
  2895. {
  2896. cmSystemTools::Error(
  2897. "Internal CMake error, TryCompile configure of cmake failed");
  2898. // return to the original directory
  2899. cmSystemTools::ChangeDirectory(cwd.c_str());
  2900. this->Internal->IsSourceFileTryCompile = false;
  2901. return 1;
  2902. }
  2903. if (cm.Generate() != 0)
  2904. {
  2905. cmSystemTools::Error(
  2906. "Internal CMake error, TryCompile generation of cmake failed");
  2907. // return to the original directory
  2908. cmSystemTools::ChangeDirectory(cwd.c_str());
  2909. this->Internal->IsSourceFileTryCompile = false;
  2910. return 1;
  2911. }
  2912. // finally call the generator to actually build the resulting project
  2913. int ret =
  2914. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2915. projectName,
  2916. targetName,
  2917. fast,
  2918. output,
  2919. this);
  2920. cmSystemTools::ChangeDirectory(cwd.c_str());
  2921. this->Internal->IsSourceFileTryCompile = false;
  2922. return ret;
  2923. }
  2924. bool cmMakefile::GetIsSourceFileTryCompile() const
  2925. {
  2926. return this->Internal->IsSourceFileTryCompile;
  2927. }
  2928. cmake *cmMakefile::GetCMakeInstance() const
  2929. {
  2930. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2931. {
  2932. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2933. }
  2934. return 0;
  2935. }
  2936. #ifdef CMAKE_BUILD_WITH_CMAKE
  2937. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2938. {
  2939. if ( this->GetCMakeInstance() &&
  2940. this->GetCMakeInstance()->GetVariableWatch() )
  2941. {
  2942. return this->GetCMakeInstance()->GetVariableWatch();
  2943. }
  2944. return 0;
  2945. }
  2946. #endif
  2947. void cmMakefile::AddMacro(const char* name, const char* signature)
  2948. {
  2949. if ( !name || !signature )
  2950. {
  2951. return;
  2952. }
  2953. this->MacrosMap[name] = signature;
  2954. }
  2955. void cmMakefile::GetListOfMacros(std::string& macros) const
  2956. {
  2957. StringStringMap::const_iterator it;
  2958. macros = "";
  2959. int cc = 0;
  2960. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2961. {
  2962. if ( cc > 0 )
  2963. {
  2964. macros += ";";
  2965. }
  2966. macros += it->first;
  2967. cc ++;
  2968. }
  2969. }
  2970. cmCacheManager *cmMakefile::GetCacheManager() const
  2971. {
  2972. return this->GetCMakeInstance()->GetCacheManager();
  2973. }
  2974. void cmMakefile::DisplayStatus(const char* message, float s) const
  2975. {
  2976. cmake* cm = this->GetLocalGenerator()->GetGlobalGenerator()
  2977. ->GetCMakeInstance();
  2978. if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE)
  2979. {
  2980. // don't output any STATUS message in FIND_PACKAGE_MODE, since they will
  2981. // directly be fed to the compiler, which will be confused.
  2982. return;
  2983. }
  2984. cm->UpdateProgress(message, s);
  2985. }
  2986. std::string cmMakefile::GetModulesFile(const char* filename) const
  2987. {
  2988. std::string result;
  2989. // We search the module always in CMAKE_ROOT and in CMAKE_MODULE_PATH,
  2990. // and then decide based on the policy setting which one to return.
  2991. // See CMP0017 for more details.
  2992. // The specific problem was that KDE 4.5.0 installs a
  2993. // FindPackageHandleStandardArgs.cmake which doesn't have the new features
  2994. // of FPHSA.cmake introduced in CMake 2.8.3 yet, and by setting
  2995. // CMAKE_MODULE_PATH also e.g. FindZLIB.cmake from cmake included
  2996. // FPHSA.cmake from kdelibs and not from CMake, and tried to use the
  2997. // new features, which were not there in the version from kdelibs, and so
  2998. // failed ("
  2999. std::string moduleInCMakeRoot;
  3000. std::string moduleInCMakeModulePath;
  3001. // Always search in CMAKE_MODULE_PATH:
  3002. const char* cmakeModulePath = this->GetDefinition("CMAKE_MODULE_PATH");
  3003. if(cmakeModulePath)
  3004. {
  3005. std::vector<std::string> modulePath;
  3006. cmSystemTools::ExpandListArgument(cmakeModulePath, modulePath);
  3007. //Look through the possible module directories.
  3008. for(std::vector<std::string>::iterator i = modulePath.begin();
  3009. i != modulePath.end(); ++i)
  3010. {
  3011. std::string itempl = *i;
  3012. cmSystemTools::ConvertToUnixSlashes(itempl);
  3013. itempl += "/";
  3014. itempl += filename;
  3015. if(cmSystemTools::FileExists(itempl.c_str()))
  3016. {
  3017. moduleInCMakeModulePath = itempl;
  3018. break;
  3019. }
  3020. }
  3021. }
  3022. // Always search in the standard modules location.
  3023. const char* cmakeRoot = this->GetDefinition("CMAKE_ROOT");
  3024. if(cmakeRoot)
  3025. {
  3026. moduleInCMakeRoot = cmakeRoot;
  3027. moduleInCMakeRoot += "/Modules/";
  3028. moduleInCMakeRoot += filename;
  3029. cmSystemTools::ConvertToUnixSlashes(moduleInCMakeRoot);
  3030. if(!cmSystemTools::FileExists(moduleInCMakeRoot.c_str()))
  3031. {
  3032. moduleInCMakeRoot = "";
  3033. }
  3034. }
  3035. // Normally, prefer the files found in CMAKE_MODULE_PATH. Only when the file
  3036. // from which we are being called is located itself in CMAKE_ROOT, then
  3037. // prefer results from CMAKE_ROOT depending on the policy setting.
  3038. result = moduleInCMakeModulePath;
  3039. if (result.size() == 0)
  3040. {
  3041. result = moduleInCMakeRoot;
  3042. }
  3043. if ((moduleInCMakeModulePath.size()>0) && (moduleInCMakeRoot.size()>0))
  3044. {
  3045. const char* currentFile = this->GetDefinition("CMAKE_CURRENT_LIST_FILE");
  3046. if (currentFile && (strstr(currentFile, cmakeRoot) == currentFile))
  3047. {
  3048. switch (this->GetPolicyStatus(cmPolicies::CMP0017))
  3049. {
  3050. case cmPolicies::WARN:
  3051. {
  3052. cmOStringStream e;
  3053. e << "File " << currentFile << " includes "
  3054. << moduleInCMakeModulePath
  3055. << " (found via CMAKE_MODULE_PATH) which shadows "
  3056. << moduleInCMakeRoot << ". This may cause errors later on .\n"
  3057. << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0017);
  3058. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3059. // break; // fall through to OLD behaviour
  3060. }
  3061. case cmPolicies::OLD:
  3062. result = moduleInCMakeModulePath;
  3063. break;
  3064. case cmPolicies::REQUIRED_IF_USED:
  3065. case cmPolicies::REQUIRED_ALWAYS:
  3066. case cmPolicies::NEW:
  3067. default:
  3068. result = moduleInCMakeRoot;
  3069. break;
  3070. }
  3071. }
  3072. }
  3073. return result;
  3074. }
  3075. void cmMakefile::ConfigureString(const std::string& input,
  3076. std::string& output, bool atOnly,
  3077. bool escapeQuotes) const
  3078. {
  3079. // Split input to handle one line at a time.
  3080. std::string::const_iterator lineStart = input.begin();
  3081. while(lineStart != input.end())
  3082. {
  3083. // Find the end of this line.
  3084. std::string::const_iterator lineEnd = lineStart;
  3085. while(lineEnd != input.end() && *lineEnd != '\n')
  3086. {
  3087. ++lineEnd;
  3088. }
  3089. // Copy the line.
  3090. std::string line(lineStart, lineEnd);
  3091. // Skip the newline character.
  3092. bool haveNewline = (lineEnd != input.end());
  3093. if(haveNewline)
  3094. {
  3095. ++lineEnd;
  3096. }
  3097. // Replace #cmakedefine instances.
  3098. if(this->cmDefineRegex.find(line))
  3099. {
  3100. const char* def =
  3101. this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  3102. if(!cmSystemTools::IsOff(def))
  3103. {
  3104. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  3105. output += line;
  3106. }
  3107. else
  3108. {
  3109. output += "/* #undef ";
  3110. output += this->cmDefineRegex.match(1);
  3111. output += " */";
  3112. }
  3113. }
  3114. else if(this->cmDefine01Regex.find(line))
  3115. {
  3116. const char* def =
  3117. this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  3118. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  3119. output += line;
  3120. if(!cmSystemTools::IsOff(def))
  3121. {
  3122. output += " 1";
  3123. }
  3124. else
  3125. {
  3126. output += " 0";
  3127. }
  3128. }
  3129. else
  3130. {
  3131. output += line;
  3132. }
  3133. if(haveNewline)
  3134. {
  3135. output += "\n";
  3136. }
  3137. // Move to the next line.
  3138. lineStart = lineEnd;
  3139. }
  3140. // Perform variable replacements.
  3141. this->ExpandVariablesInString(output, escapeQuotes, true,
  3142. atOnly, 0, -1, true);
  3143. }
  3144. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  3145. bool copyonly, bool atOnly, bool escapeQuotes,
  3146. const cmNewLineStyle& newLine)
  3147. {
  3148. int res = 1;
  3149. if ( !this->CanIWriteThisFile(outfile) )
  3150. {
  3151. cmSystemTools::Error("Attempt to write file: ",
  3152. outfile, " into a source directory.");
  3153. return 0;
  3154. }
  3155. if ( !cmSystemTools::FileExists(infile) )
  3156. {
  3157. cmSystemTools::Error("File ", infile, " does not exist.");
  3158. return 0;
  3159. }
  3160. std::string soutfile = outfile;
  3161. std::string sinfile = infile;
  3162. this->AddCMakeDependFile(sinfile);
  3163. cmSystemTools::ConvertToUnixSlashes(soutfile);
  3164. // Re-generate if non-temporary outputs are missing.
  3165. //when we finalize the configuration we will remove all
  3166. //output files that now don't exist.
  3167. this->AddCMakeOutputFile(soutfile);
  3168. mode_t perm = 0;
  3169. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  3170. std::string::size_type pos = soutfile.rfind('/');
  3171. if(pos != std::string::npos)
  3172. {
  3173. std::string path = soutfile.substr(0, pos);
  3174. cmSystemTools::MakeDirectory(path.c_str());
  3175. }
  3176. if(copyonly)
  3177. {
  3178. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  3179. soutfile.c_str()))
  3180. {
  3181. return 0;
  3182. }
  3183. }
  3184. else
  3185. {
  3186. std::string newLineCharacters;
  3187. std::ios_base::openmode omode = std::ios_base::out | std::ios_base::trunc;
  3188. if (newLine.IsValid())
  3189. {
  3190. newLineCharacters = newLine.GetCharacters();
  3191. omode |= std::ios::binary;
  3192. }
  3193. else
  3194. {
  3195. newLineCharacters = "\n";
  3196. }
  3197. std::string tempOutputFile = soutfile;
  3198. tempOutputFile += ".tmp";
  3199. cmsys::ofstream fout(tempOutputFile.c_str(), omode);
  3200. if(!fout)
  3201. {
  3202. cmSystemTools::Error(
  3203. "Could not open file for write in copy operation ",
  3204. tempOutputFile.c_str());
  3205. cmSystemTools::ReportLastSystemError("");
  3206. return 0;
  3207. }
  3208. cmsys::ifstream fin(sinfile.c_str());
  3209. if(!fin)
  3210. {
  3211. cmSystemTools::Error("Could not open file for read in copy operation ",
  3212. sinfile.c_str());
  3213. return 0;
  3214. }
  3215. // now copy input to output and expand variables in the
  3216. // input file at the same time
  3217. std::string inLine;
  3218. std::string outLine;
  3219. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  3220. {
  3221. outLine = "";
  3222. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  3223. fout << outLine.c_str() << newLineCharacters;
  3224. }
  3225. // close the files before attempting to copy
  3226. fin.close();
  3227. fout.close();
  3228. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  3229. soutfile.c_str()) )
  3230. {
  3231. res = 0;
  3232. }
  3233. else
  3234. {
  3235. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  3236. }
  3237. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  3238. }
  3239. return res;
  3240. }
  3241. void cmMakefile::SetProperty(const char* prop, const char* value)
  3242. {
  3243. if (!prop)
  3244. {
  3245. return;
  3246. }
  3247. // handle special props
  3248. std::string propname = prop;
  3249. if ( propname == "LINK_DIRECTORIES" )
  3250. {
  3251. std::vector<std::string> varArgsExpanded;
  3252. if(value)
  3253. {
  3254. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  3255. }
  3256. this->SetLinkDirectories(varArgsExpanded);
  3257. return;
  3258. }
  3259. if (propname == "INCLUDE_DIRECTORIES")
  3260. {
  3261. this->IncludeDirectoriesEntries.clear();
  3262. if (!value)
  3263. {
  3264. return;
  3265. }
  3266. cmListFileBacktrace lfbt;
  3267. this->GetBacktrace(lfbt);
  3268. this->IncludeDirectoriesEntries.push_back(
  3269. cmValueWithOrigin(value, lfbt));
  3270. return;
  3271. }
  3272. if (propname == "COMPILE_OPTIONS")
  3273. {
  3274. this->CompileOptionsEntries.clear();
  3275. if (!value)
  3276. {
  3277. return;
  3278. }
  3279. cmListFileBacktrace lfbt;
  3280. this->GetBacktrace(lfbt);
  3281. this->CompileOptionsEntries.push_back(cmValueWithOrigin(value, lfbt));
  3282. return;
  3283. }
  3284. if (propname == "COMPILE_DEFINITIONS")
  3285. {
  3286. this->CompileDefinitionsEntries.clear();
  3287. if (!value)
  3288. {
  3289. return;
  3290. }
  3291. cmListFileBacktrace lfbt;
  3292. this->GetBacktrace(lfbt);
  3293. cmValueWithOrigin entry(value, lfbt);
  3294. this->CompileDefinitionsEntries.push_back(entry);
  3295. return;
  3296. }
  3297. if ( propname == "INCLUDE_REGULAR_EXPRESSION" )
  3298. {
  3299. this->SetIncludeRegularExpression(value);
  3300. return;
  3301. }
  3302. if ( propname == "ADDITIONAL_MAKE_CLEAN_FILES" )
  3303. {
  3304. // This property is not inherrited
  3305. if ( strcmp(this->GetCurrentDirectory(),
  3306. this->GetStartDirectory()) != 0 )
  3307. {
  3308. return;
  3309. }
  3310. }
  3311. this->Properties.SetProperty(prop,value,cmProperty::DIRECTORY);
  3312. }
  3313. void cmMakefile::AppendProperty(const char* prop, const char* value,
  3314. bool asString)
  3315. {
  3316. if (!prop)
  3317. {
  3318. return;
  3319. }
  3320. // handle special props
  3321. std::string propname = prop;
  3322. if (propname == "INCLUDE_DIRECTORIES")
  3323. {
  3324. cmListFileBacktrace lfbt;
  3325. this->GetBacktrace(lfbt);
  3326. this->IncludeDirectoriesEntries.push_back(
  3327. cmValueWithOrigin(value, lfbt));
  3328. return;
  3329. }
  3330. if (propname == "COMPILE_OPTIONS")
  3331. {
  3332. cmListFileBacktrace lfbt;
  3333. this->GetBacktrace(lfbt);
  3334. this->CompileOptionsEntries.push_back(
  3335. cmValueWithOrigin(value, lfbt));
  3336. return;
  3337. }
  3338. if (propname == "COMPILE_DEFINITIONS")
  3339. {
  3340. cmListFileBacktrace lfbt;
  3341. this->GetBacktrace(lfbt);
  3342. this->CompileDefinitionsEntries.push_back(
  3343. cmValueWithOrigin(value, lfbt));
  3344. return;
  3345. }
  3346. if ( propname == "LINK_DIRECTORIES" )
  3347. {
  3348. std::vector<std::string> varArgsExpanded;
  3349. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  3350. for(std::vector<std::string>::const_iterator vi = varArgsExpanded.begin();
  3351. vi != varArgsExpanded.end(); ++vi)
  3352. {
  3353. this->AddLinkDirectory(vi->c_str());
  3354. }
  3355. return;
  3356. }
  3357. this->Properties.AppendProperty(prop,value,cmProperty::DIRECTORY,asString);
  3358. }
  3359. const char *cmMakefile::GetPropertyOrDefinition(const char* prop) const
  3360. {
  3361. const char *ret = this->GetProperty(prop, cmProperty::DIRECTORY);
  3362. if (!ret)
  3363. {
  3364. ret = this->GetDefinition(prop);
  3365. }
  3366. return ret;
  3367. }
  3368. const char *cmMakefile::GetProperty(const char* prop) const
  3369. {
  3370. return this->GetProperty(prop, cmProperty::DIRECTORY);
  3371. }
  3372. const char *cmMakefile::GetProperty(const char* prop,
  3373. cmProperty::ScopeType scope) const
  3374. {
  3375. if(!prop)
  3376. {
  3377. return 0;
  3378. }
  3379. // watch for specific properties
  3380. static std::string output;
  3381. output = "";
  3382. if (!strcmp("PARENT_DIRECTORY",prop))
  3383. {
  3384. if(cmLocalGenerator* plg = this->LocalGenerator->GetParent())
  3385. {
  3386. output = plg->GetMakefile()->GetStartDirectory();
  3387. }
  3388. return output.c_str();
  3389. }
  3390. else if (!strcmp("INCLUDE_REGULAR_EXPRESSION",prop) )
  3391. {
  3392. output = this->GetIncludeRegularExpression();
  3393. return output.c_str();
  3394. }
  3395. else if (!strcmp("LISTFILE_STACK",prop))
  3396. {
  3397. for (std::deque<cmStdString>::const_iterator
  3398. i = this->ListFileStack.begin();
  3399. i != this->ListFileStack.end(); ++i)
  3400. {
  3401. if (i != this->ListFileStack.begin())
  3402. {
  3403. output += ";";
  3404. }
  3405. output += *i;
  3406. }
  3407. return output.c_str();
  3408. }
  3409. else if (!strcmp("VARIABLES",prop) || !strcmp("CACHE_VARIABLES",prop))
  3410. {
  3411. int cacheonly = 0;
  3412. if ( !strcmp("CACHE_VARIABLES",prop) )
  3413. {
  3414. cacheonly = 1;
  3415. }
  3416. std::vector<std::string> vars = this->GetDefinitions(cacheonly);
  3417. for (unsigned int cc = 0; cc < vars.size(); cc ++ )
  3418. {
  3419. if ( cc > 0 )
  3420. {
  3421. output += ";";
  3422. }
  3423. output += vars[cc];
  3424. }
  3425. return output.c_str();
  3426. }
  3427. else if (!strcmp("MACROS",prop))
  3428. {
  3429. this->GetListOfMacros(output);
  3430. return output.c_str();
  3431. }
  3432. else if (!strcmp("DEFINITIONS",prop))
  3433. {
  3434. output += this->DefineFlagsOrig;
  3435. return output.c_str();
  3436. }
  3437. else if (!strcmp("LINK_DIRECTORIES",prop))
  3438. {
  3439. cmOStringStream str;
  3440. for (std::vector<std::string>::const_iterator
  3441. it = this->GetLinkDirectories().begin();
  3442. it != this->GetLinkDirectories().end();
  3443. ++ it )
  3444. {
  3445. if ( it != this->GetLinkDirectories().begin())
  3446. {
  3447. str << ";";
  3448. }
  3449. str << it->c_str();
  3450. }
  3451. output = str.str();
  3452. return output.c_str();
  3453. }
  3454. else if (!strcmp("INCLUDE_DIRECTORIES",prop))
  3455. {
  3456. std::string sep;
  3457. for (std::vector<cmValueWithOrigin>::const_iterator
  3458. it = this->IncludeDirectoriesEntries.begin(),
  3459. end = this->IncludeDirectoriesEntries.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 (!strcmp("COMPILE_OPTIONS",prop))
  3469. {
  3470. std::string sep;
  3471. for (std::vector<cmValueWithOrigin>::const_iterator
  3472. it = this->CompileOptionsEntries.begin(),
  3473. end = this->CompileOptionsEntries.end();
  3474. it != end; ++it)
  3475. {
  3476. output += sep;
  3477. output += it->Value;
  3478. sep = ";";
  3479. }
  3480. return output.c_str();
  3481. }
  3482. else if (!strcmp("COMPILE_DEFINITIONS",prop))
  3483. {
  3484. std::string sep;
  3485. for (std::vector<cmValueWithOrigin>::const_iterator
  3486. it = this->CompileDefinitionsEntries.begin(),
  3487. end = this->CompileDefinitionsEntries.end();
  3488. it != end; ++it)
  3489. {
  3490. output += sep;
  3491. output += it->Value;
  3492. sep = ";";
  3493. }
  3494. return output.c_str();
  3495. }
  3496. bool chain = false;
  3497. const char *retVal =
  3498. this->Properties.GetPropertyValue(prop, scope, chain);
  3499. if (chain)
  3500. {
  3501. if(this->LocalGenerator->GetParent())
  3502. {
  3503. return this->LocalGenerator->GetParent()->GetMakefile()->
  3504. GetProperty(prop, scope);
  3505. }
  3506. return this->GetCMakeInstance()->GetProperty(prop,scope);
  3507. }
  3508. return retVal;
  3509. }
  3510. bool cmMakefile::GetPropertyAsBool(const char* prop) const
  3511. {
  3512. return cmSystemTools::IsOn(this->GetProperty(prop));
  3513. }
  3514. //----------------------------------------------------------------------------
  3515. const char* cmMakefile::GetFeature(const char* feature, const char* config)
  3516. {
  3517. // TODO: Define accumulation policy for features (prepend, append, replace).
  3518. // Currently we always replace.
  3519. if(config && *config)
  3520. {
  3521. std::string featureConfig = feature;
  3522. featureConfig += "_";
  3523. featureConfig += cmSystemTools::UpperCase(config);
  3524. if(const char* value = this->GetProperty(featureConfig.c_str()))
  3525. {
  3526. return value;
  3527. }
  3528. }
  3529. if(const char* value = this->GetProperty(feature))
  3530. {
  3531. return value;
  3532. }
  3533. if(cmLocalGenerator* parent = this->LocalGenerator->GetParent())
  3534. {
  3535. return parent->GetMakefile()->GetFeature(feature, config);
  3536. }
  3537. return 0;
  3538. }
  3539. cmTarget* cmMakefile::FindTarget(const std::string& name,
  3540. bool excludeAliases) const
  3541. {
  3542. if (!excludeAliases)
  3543. {
  3544. std::map<std::string, cmTarget*>::const_iterator i
  3545. = this->AliasTargets.find(name);
  3546. if (i != this->AliasTargets.end())
  3547. {
  3548. return i->second;
  3549. }
  3550. }
  3551. cmTargets::iterator i = this->Targets.find( name );
  3552. if ( i != this->Targets.end() )
  3553. {
  3554. return &i->second;
  3555. }
  3556. return 0;
  3557. }
  3558. //----------------------------------------------------------------------------
  3559. cmTest* cmMakefile::CreateTest(const char* testName)
  3560. {
  3561. if ( !testName )
  3562. {
  3563. return 0;
  3564. }
  3565. cmTest* test = this->GetTest(testName);
  3566. if ( test )
  3567. {
  3568. return test;
  3569. }
  3570. test = new cmTest(this);
  3571. test->SetName(testName);
  3572. this->Tests[testName] = test;
  3573. return test;
  3574. }
  3575. //----------------------------------------------------------------------------
  3576. cmTest* cmMakefile::GetTest(const char* testName) const
  3577. {
  3578. if(testName)
  3579. {
  3580. std::map<cmStdString, cmTest*>::const_iterator
  3581. mi = this->Tests.find(testName);
  3582. if(mi != this->Tests.end())
  3583. {
  3584. return mi->second;
  3585. }
  3586. }
  3587. return 0;
  3588. }
  3589. void cmMakefile::AddCMakeDependFilesFromUser()
  3590. {
  3591. std::vector<std::string> deps;
  3592. if(const char* deps_str = this->GetProperty("CMAKE_CONFIGURE_DEPENDS"))
  3593. {
  3594. cmSystemTools::ExpandListArgument(deps_str, deps);
  3595. }
  3596. for(std::vector<std::string>::iterator i = deps.begin();
  3597. i != deps.end(); ++i)
  3598. {
  3599. if(cmSystemTools::FileIsFullPath(i->c_str()))
  3600. {
  3601. this->AddCMakeDependFile(*i);
  3602. }
  3603. else
  3604. {
  3605. std::string f = this->GetCurrentDirectory();
  3606. f += "/";
  3607. f += *i;
  3608. this->AddCMakeDependFile(f);
  3609. }
  3610. }
  3611. }
  3612. std::string cmMakefile::GetListFileStack() const
  3613. {
  3614. cmOStringStream tmp;
  3615. size_t depth = this->ListFileStack.size();
  3616. if (depth > 0)
  3617. {
  3618. std::deque<cmStdString>::const_iterator it = this->ListFileStack.end();
  3619. do
  3620. {
  3621. if (depth != this->ListFileStack.size())
  3622. {
  3623. tmp << "\n ";
  3624. }
  3625. --it;
  3626. tmp << "[";
  3627. tmp << depth;
  3628. tmp << "]\t";
  3629. tmp << *it;
  3630. depth--;
  3631. }
  3632. while (it != this->ListFileStack.begin());
  3633. }
  3634. return tmp.str();
  3635. }
  3636. void cmMakefile::PushScope()
  3637. {
  3638. cmDefinitions* parent = &this->Internal->VarStack.top();
  3639. const std::set<cmStdString>& init = this->Internal->VarInitStack.top();
  3640. const std::set<cmStdString>& usage = this->Internal->VarUsageStack.top();
  3641. this->Internal->VarStack.push(cmDefinitions(parent));
  3642. this->Internal->VarInitStack.push(init);
  3643. this->Internal->VarUsageStack.push(usage);
  3644. }
  3645. void cmMakefile::PopScope()
  3646. {
  3647. cmDefinitions* current = &this->Internal->VarStack.top();
  3648. std::set<cmStdString> init = this->Internal->VarInitStack.top();
  3649. std::set<cmStdString> usage = this->Internal->VarUsageStack.top();
  3650. const std::set<cmStdString>& locals = current->LocalKeys();
  3651. // Remove initialization and usage information for variables in the local
  3652. // scope.
  3653. std::set<cmStdString>::const_iterator it = locals.begin();
  3654. for (; it != locals.end(); ++it)
  3655. {
  3656. init.erase(*it);
  3657. if (!this->VariableUsed(it->c_str()))
  3658. {
  3659. this->CheckForUnused("out of scope", it->c_str());
  3660. }
  3661. else
  3662. {
  3663. usage.erase(*it);
  3664. }
  3665. }
  3666. this->Internal->VarStack.pop();
  3667. this->Internal->VarInitStack.pop();
  3668. this->Internal->VarUsageStack.pop();
  3669. // Push initialization and usage up to the parent scope.
  3670. it = init.begin();
  3671. for (; it != init.end(); ++it)
  3672. {
  3673. this->Internal->VarInitStack.top().insert(*it);
  3674. }
  3675. it = usage.begin();
  3676. for (; it != usage.end(); ++it)
  3677. {
  3678. this->Internal->VarUsageStack.top().insert(*it);
  3679. }
  3680. }
  3681. void cmMakefile::RaiseScope(const char *var, const char *varDef)
  3682. {
  3683. if (!var || !strlen(var))
  3684. {
  3685. return;
  3686. }
  3687. cmDefinitions& cur = this->Internal->VarStack.top();
  3688. if(cmDefinitions* up = cur.GetParent())
  3689. {
  3690. // First localize the definition in the current scope.
  3691. cur.Get(var);
  3692. // Now update the definition in the parent scope.
  3693. up->Set(var, varDef);
  3694. }
  3695. else if(cmLocalGenerator* plg = this->LocalGenerator->GetParent())
  3696. {
  3697. // Update the definition in the parent directory top scope. This
  3698. // directory's scope was initialized by the closure of the parent
  3699. // scope, so we do not need to localize the definition first.
  3700. cmMakefile* parent = plg->GetMakefile();
  3701. if (varDef)
  3702. {
  3703. parent->AddDefinition(var, varDef);
  3704. }
  3705. else
  3706. {
  3707. parent->RemoveDefinition(var);
  3708. }
  3709. }
  3710. else
  3711. {
  3712. cmOStringStream m;
  3713. m << "Cannot set \"" << var << "\": current scope has no parent.";
  3714. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  3715. }
  3716. }
  3717. // define properties
  3718. void cmMakefile::DefineProperties(cmake *cm)
  3719. {
  3720. cm->DefineProperty
  3721. ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
  3722. "", "", true);
  3723. cm->DefineProperty
  3724. ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
  3725. "", "", true);
  3726. cm->DefineProperty
  3727. ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
  3728. "", "", true);
  3729. }
  3730. //----------------------------------------------------------------------------
  3731. cmTarget*
  3732. cmMakefile::AddImportedTarget(const char* name, cmTarget::TargetType type,
  3733. bool global)
  3734. {
  3735. // Create the target.
  3736. cmsys::auto_ptr<cmTarget> target(new cmTarget);
  3737. target->SetType(type, name);
  3738. target->MarkAsImported();
  3739. target->SetMakefile(this);
  3740. // Add to the set of available imported targets.
  3741. this->ImportedTargets[name] = target.get();
  3742. if(global)
  3743. {
  3744. this->LocalGenerator->GetGlobalGenerator()->AddTarget(target.get());
  3745. }
  3746. // Transfer ownership to this cmMakefile object.
  3747. this->ImportedTargetsOwned.push_back(target.get());
  3748. return target.release();
  3749. }
  3750. //----------------------------------------------------------------------------
  3751. cmTarget* cmMakefile::FindTargetToUse(const std::string& name,
  3752. bool excludeAliases) const
  3753. {
  3754. // Look for an imported target. These take priority because they
  3755. // are more local in scope and do not have to be globally unique.
  3756. std::map<cmStdString, cmTarget*>::const_iterator
  3757. imported = this->ImportedTargets.find(name);
  3758. if(imported != this->ImportedTargets.end())
  3759. {
  3760. return imported->second;
  3761. }
  3762. // Look for a target built in this directory.
  3763. if(cmTarget* t = this->FindTarget(name, excludeAliases))
  3764. {
  3765. return t;
  3766. }
  3767. // Look for a target built in this project.
  3768. return this->LocalGenerator->GetGlobalGenerator()->FindTarget(0,
  3769. name.c_str(),
  3770. excludeAliases);
  3771. }
  3772. //----------------------------------------------------------------------------
  3773. bool cmMakefile::IsAlias(const std::string& name) const
  3774. {
  3775. if (this->AliasTargets.find(name) != this->AliasTargets.end())
  3776. return true;
  3777. return this->GetLocalGenerator()->GetGlobalGenerator()->IsAlias(
  3778. name.c_str());
  3779. }
  3780. //----------------------------------------------------------------------------
  3781. cmGeneratorTarget*
  3782. cmMakefile::FindGeneratorTargetToUse(const char* name) const
  3783. {
  3784. if (cmTarget *t = this->FindTargetToUse(name))
  3785. {
  3786. return this->LocalGenerator->GetGlobalGenerator()->GetGeneratorTarget(t);
  3787. }
  3788. return 0;
  3789. }
  3790. //----------------------------------------------------------------------------
  3791. bool cmMakefile::EnforceUniqueName(std::string const& name, std::string& msg,
  3792. bool isCustom) const
  3793. {
  3794. if(this->IsAlias(name))
  3795. {
  3796. cmOStringStream e;
  3797. e << "cannot create target \"" << name
  3798. << "\" because an alias with the same name already exists.";
  3799. msg = e.str();
  3800. return false;
  3801. }
  3802. if(cmTarget* existing = this->FindTargetToUse(name))
  3803. {
  3804. // The name given conflicts with an existing target. Produce an
  3805. // error in a compatible way.
  3806. if(existing->IsImported())
  3807. {
  3808. // Imported targets were not supported in previous versions.
  3809. // This is new code, so we can make it an error.
  3810. cmOStringStream e;
  3811. e << "cannot create target \"" << name
  3812. << "\" because an imported target with the same name already exists.";
  3813. msg = e.str();
  3814. return false;
  3815. }
  3816. else
  3817. {
  3818. // target names must be globally unique
  3819. switch (this->GetPolicyStatus(cmPolicies::CMP0002))
  3820. {
  3821. case cmPolicies::WARN:
  3822. this->IssueMessage(cmake::AUTHOR_WARNING, this->GetPolicies()->
  3823. GetPolicyWarning(cmPolicies::CMP0002));
  3824. case cmPolicies::OLD:
  3825. return true;
  3826. case cmPolicies::REQUIRED_IF_USED:
  3827. case cmPolicies::REQUIRED_ALWAYS:
  3828. this->IssueMessage(cmake::FATAL_ERROR,
  3829. this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0002)
  3830. );
  3831. return true;
  3832. case cmPolicies::NEW:
  3833. break;
  3834. }
  3835. // The conflict is with a non-imported target.
  3836. // Allow this if the user has requested support.
  3837. cmake* cm =
  3838. this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  3839. if(isCustom && existing->GetType() == cmTarget::UTILITY &&
  3840. this != existing->GetMakefile() &&
  3841. cm->GetPropertyAsBool("ALLOW_DUPLICATE_CUSTOM_TARGETS"))
  3842. {
  3843. return true;
  3844. }
  3845. // Produce an error that tells the user how to work around the
  3846. // problem.
  3847. cmOStringStream e;
  3848. e << "cannot create target \"" << name
  3849. << "\" because another target with the same name already exists. "
  3850. << "The existing target is ";
  3851. switch(existing->GetType())
  3852. {
  3853. case cmTarget::EXECUTABLE:
  3854. e << "an executable ";
  3855. break;
  3856. case cmTarget::STATIC_LIBRARY:
  3857. e << "a static library ";
  3858. break;
  3859. case cmTarget::SHARED_LIBRARY:
  3860. e << "a shared library ";
  3861. break;
  3862. case cmTarget::MODULE_LIBRARY:
  3863. e << "a module library ";
  3864. break;
  3865. case cmTarget::UTILITY:
  3866. e << "a custom target ";
  3867. break;
  3868. default: break;
  3869. }
  3870. e << "created in source directory \""
  3871. << existing->GetMakefile()->GetCurrentDirectory() << "\". "
  3872. << "See documentation for policy CMP0002 for more details.";
  3873. msg = e.str();
  3874. return false;
  3875. }
  3876. }
  3877. return true;
  3878. }
  3879. //----------------------------------------------------------------------------
  3880. bool cmMakefile::EnforceUniqueDir(const char* srcPath,
  3881. const char* binPath) const
  3882. {
  3883. // Make sure the binary directory is unique.
  3884. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  3885. if(gg->BinaryDirectoryIsNew(binPath))
  3886. {
  3887. return true;
  3888. }
  3889. cmOStringStream e;
  3890. switch (this->GetPolicyStatus(cmPolicies::CMP0013))
  3891. {
  3892. case cmPolicies::WARN:
  3893. // Print the warning.
  3894. e << this->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0013)
  3895. << "\n"
  3896. << "The binary directory\n"
  3897. << " " << binPath << "\n"
  3898. << "is already used to build a source directory. "
  3899. << "This command uses it to build source directory\n"
  3900. << " " << srcPath << "\n"
  3901. << "which can generate conflicting build files. "
  3902. << "CMake does not support this use case but it used "
  3903. << "to work accidentally and is being allowed for "
  3904. << "compatibility.";
  3905. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3906. case cmPolicies::OLD:
  3907. // OLD behavior does not warn.
  3908. return true;
  3909. case cmPolicies::REQUIRED_IF_USED:
  3910. case cmPolicies::REQUIRED_ALWAYS:
  3911. e << this->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0013)
  3912. << "\n";
  3913. case cmPolicies::NEW:
  3914. // NEW behavior prints the error.
  3915. e << "The binary directory\n"
  3916. << " " << binPath << "\n"
  3917. << "is already used to build a source directory. "
  3918. << "It cannot be used to build source directory\n"
  3919. << " " << srcPath << "\n"
  3920. << "Specify a unique binary directory name.";
  3921. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3922. break;
  3923. }
  3924. return false;
  3925. }
  3926. //----------------------------------------------------------------------------
  3927. void cmMakefile::AddQtUiFileWithOptions(cmSourceFile *sf)
  3928. {
  3929. this->QtUiFilesWithOptions.push_back(sf);
  3930. }
  3931. //----------------------------------------------------------------------------
  3932. std::vector<cmSourceFile*> cmMakefile::GetQtUiFilesWithOptions() const
  3933. {
  3934. return this->QtUiFilesWithOptions;
  3935. }
  3936. //----------------------------------------------------------------------------
  3937. cmPolicies::PolicyStatus
  3938. cmMakefile::GetPolicyStatus(cmPolicies::PolicyID id) const
  3939. {
  3940. // Get the current setting of the policy.
  3941. cmPolicies::PolicyStatus cur = this->GetPolicyStatusInternal(id);
  3942. // If the policy is required to be set to NEW but is not, ignore the
  3943. // current setting and tell the caller.
  3944. if(cur != cmPolicies::NEW)
  3945. {
  3946. if(cur == cmPolicies::REQUIRED_ALWAYS ||
  3947. cur == cmPolicies::REQUIRED_IF_USED)
  3948. {
  3949. return cur;
  3950. }
  3951. cmPolicies::PolicyStatus def = this->GetPolicies()->GetPolicyStatus(id);
  3952. if(def == cmPolicies::REQUIRED_ALWAYS ||
  3953. def == cmPolicies::REQUIRED_IF_USED)
  3954. {
  3955. return def;
  3956. }
  3957. }
  3958. // The current setting is okay.
  3959. return cur;
  3960. }
  3961. //----------------------------------------------------------------------------
  3962. cmPolicies::PolicyStatus
  3963. cmMakefile::GetPolicyStatusInternal(cmPolicies::PolicyID id) const
  3964. {
  3965. // Is the policy set in our stack?
  3966. for(PolicyStackType::const_reverse_iterator psi = this->PolicyStack.rbegin();
  3967. psi != this->PolicyStack.rend(); ++psi)
  3968. {
  3969. PolicyStackEntry::const_iterator pse = psi->find(id);
  3970. if(pse != psi->end())
  3971. {
  3972. return pse->second;
  3973. }
  3974. }
  3975. // If we have a parent directory, recurse up to it.
  3976. if(this->LocalGenerator->GetParent())
  3977. {
  3978. cmMakefile* parent = this->LocalGenerator->GetParent()->GetMakefile();
  3979. return parent->GetPolicyStatusInternal(id);
  3980. }
  3981. // The policy is not set. Use the default for this CMake version.
  3982. return this->GetPolicies()->GetPolicyStatus(id);
  3983. }
  3984. //----------------------------------------------------------------------------
  3985. bool cmMakefile::PolicyOptionalWarningEnabled(std::string const& var)
  3986. {
  3987. // Check for an explicit CMAKE_POLICY_WARNING_CMP<NNNN> setting.
  3988. if(!var.empty())
  3989. {
  3990. if(const char* val = this->GetDefinition(var.c_str()))
  3991. {
  3992. return cmSystemTools::IsOn(val);
  3993. }
  3994. }
  3995. // Enable optional policy warnings with --debug-output or --trace.
  3996. cmake* cm = this->GetCMakeInstance();
  3997. return cm->GetDebugOutput() || cm->GetTrace();
  3998. }
  3999. bool cmMakefile::SetPolicy(const char *id,
  4000. cmPolicies::PolicyStatus status)
  4001. {
  4002. cmPolicies::PolicyID pid;
  4003. if (!this->GetPolicies()->GetPolicyID(id, /* out */ pid))
  4004. {
  4005. cmOStringStream e;
  4006. e << "Policy \"" << id << "\" is not known to this version of CMake.";
  4007. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  4008. return false;
  4009. }
  4010. return this->SetPolicy(pid,status);
  4011. }
  4012. //----------------------------------------------------------------------------
  4013. bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
  4014. cmPolicies::PolicyStatus status)
  4015. {
  4016. // A REQUIRED_ALWAYS policy may be set only to NEW.
  4017. if(status != cmPolicies::NEW &&
  4018. this->GetPolicies()->GetPolicyStatus(id) ==
  4019. cmPolicies::REQUIRED_ALWAYS)
  4020. {
  4021. std::string msg =
  4022. this->GetPolicies()->GetRequiredAlwaysPolicyError(id);
  4023. this->IssueMessage(cmake::FATAL_ERROR, msg.c_str());
  4024. return false;
  4025. }
  4026. // Update the policy stack from the top to the top-most strong entry.
  4027. bool previous_was_weak = true;
  4028. for(PolicyStackType::reverse_iterator psi = this->PolicyStack.rbegin();
  4029. previous_was_weak && psi != this->PolicyStack.rend(); ++psi)
  4030. {
  4031. (*psi)[id] = status;
  4032. previous_was_weak = psi->Weak;
  4033. }
  4034. // Special hook for presenting compatibility variable as soon as
  4035. // the user requests it.
  4036. if(id == cmPolicies::CMP0001 &&
  4037. (status == cmPolicies::WARN || status == cmPolicies::OLD))
  4038. {
  4039. if(!(this->GetCacheManager()
  4040. ->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY")))
  4041. {
  4042. // Set it to 2.4 because that is the last version where the
  4043. // variable had meaning.
  4044. this->AddCacheDefinition
  4045. ("CMAKE_BACKWARDS_COMPATIBILITY", "2.4",
  4046. "For backwards compatibility, what version of CMake "
  4047. "commands and "
  4048. "syntax should this version of CMake try to support.",
  4049. cmCacheManager::STRING);
  4050. }
  4051. }
  4052. return true;
  4053. }
  4054. //----------------------------------------------------------------------------
  4055. cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m, bool weak,
  4056. cmPolicies::PolicyMap const& pm):
  4057. Makefile(m), ReportError(true)
  4058. {
  4059. this->Makefile->PushPolicy(weak, pm);
  4060. this->Makefile->PushPolicyBarrier();
  4061. }
  4062. //----------------------------------------------------------------------------
  4063. cmMakefile::PolicyPushPop::~PolicyPushPop()
  4064. {
  4065. this->Makefile->PopPolicyBarrier(this->ReportError);
  4066. this->Makefile->PopPolicy();
  4067. }
  4068. //----------------------------------------------------------------------------
  4069. void cmMakefile::PushPolicy(bool weak, cmPolicies::PolicyMap const& pm)
  4070. {
  4071. // Allocate a new stack entry.
  4072. this->PolicyStack.push_back(PolicyStackEntry(pm, weak));
  4073. }
  4074. //----------------------------------------------------------------------------
  4075. void cmMakefile::PopPolicy()
  4076. {
  4077. if(this->PolicyStack.size() > this->PolicyBarriers.back())
  4078. {
  4079. this->PolicyStack.pop_back();
  4080. }
  4081. else
  4082. {
  4083. this->IssueMessage(cmake::FATAL_ERROR,
  4084. "cmake_policy POP without matching PUSH");
  4085. }
  4086. }
  4087. //----------------------------------------------------------------------------
  4088. void cmMakefile::PushPolicyBarrier()
  4089. {
  4090. this->PolicyBarriers.push_back(this->PolicyStack.size());
  4091. }
  4092. //----------------------------------------------------------------------------
  4093. void cmMakefile::PopPolicyBarrier(bool reportError)
  4094. {
  4095. // Remove any extra entries pushed on the barrier.
  4096. PolicyStackType::size_type barrier = this->PolicyBarriers.back();
  4097. while(this->PolicyStack.size() > barrier)
  4098. {
  4099. if(reportError)
  4100. {
  4101. this->IssueMessage(cmake::FATAL_ERROR,
  4102. "cmake_policy PUSH without matching POP");
  4103. reportError = false;
  4104. }
  4105. this->PopPolicy();
  4106. }
  4107. // Remove the barrier.
  4108. this->PolicyBarriers.pop_back();
  4109. }
  4110. bool cmMakefile::SetPolicyVersion(const char *version)
  4111. {
  4112. return this->GetCMakeInstance()->GetPolicies()->
  4113. ApplyPolicyVersion(this,version);
  4114. }
  4115. cmPolicies *cmMakefile::GetPolicies() const
  4116. {
  4117. if (!this->GetCMakeInstance())
  4118. {
  4119. return 0;
  4120. }
  4121. return this->GetCMakeInstance()->GetPolicies();
  4122. }
  4123. //----------------------------------------------------------------------------
  4124. void cmMakefile::RecordPolicies(cmPolicies::PolicyMap& pm)
  4125. {
  4126. /* Record the setting of every policy. */
  4127. typedef cmPolicies::PolicyID PolicyID;
  4128. for(PolicyID pid = cmPolicies::CMP0000;
  4129. pid != cmPolicies::CMPCOUNT; pid = PolicyID(pid+1))
  4130. {
  4131. pm[pid] = this->GetPolicyStatus(pid);
  4132. }
  4133. }