cmMakefile.cxx 168 KB

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