cmMakefile.cxx 137 KB

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