cmMakefile.cxx 158 KB

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