cmMakefile.cxx 154 KB

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