cmMakefile.cxx 157 KB

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