cmMakefile.cxx 158 KB

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