cmMakefile.cxx 140 KB

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