cmVisualStudio10TargetGenerator.cxx 202 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmVisualStudio10TargetGenerator.h"
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iterator>
  8. #include <set>
  9. #include <sstream>
  10. #include <cm/filesystem>
  11. #include <cm/memory>
  12. #include <cm/optional>
  13. #include <cm/string_view>
  14. #include <cm/vector>
  15. #include <cmext/algorithm>
  16. #include "windows.h"
  17. #include "cmsys/FStream.hxx"
  18. #include "cmsys/RegularExpression.hxx"
  19. #include "cmComputeLinkInformation.h"
  20. #include "cmCustomCommand.h"
  21. #include "cmCustomCommandGenerator.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmGeneratorExpression.h"
  24. #include "cmGeneratorTarget.h"
  25. #include "cmGlobalGenerator.h"
  26. #include "cmGlobalVisualStudio10Generator.h"
  27. #include "cmGlobalVisualStudio7Generator.h"
  28. #include "cmGlobalVisualStudioGenerator.h"
  29. #include "cmLinkLineDeviceComputer.h"
  30. #include "cmList.h"
  31. #include "cmListFileCache.h"
  32. #include "cmLocalGenerator.h"
  33. #include "cmLocalVisualStudio10Generator.h"
  34. #include "cmLocalVisualStudio7Generator.h"
  35. #include "cmLocalVisualStudioGenerator.h"
  36. #include "cmMakefile.h"
  37. #include "cmMessageType.h"
  38. #include "cmPropertyMap.h"
  39. #include "cmSourceFile.h"
  40. #include "cmSourceFileLocation.h"
  41. #include "cmSourceFileLocationKind.h"
  42. #include "cmSourceGroup.h"
  43. #include "cmStateTypes.h"
  44. #include "cmStringAlgorithms.h"
  45. #include "cmSystemTools.h"
  46. #include "cmTarget.h"
  47. #include "cmValue.h"
  48. #include "cmVisualStudioGeneratorOptions.h"
  49. const std::string kBuildSystemSources = "Buildsystem Input Files";
  50. struct cmIDEFlagTable;
  51. static void ConvertToWindowsSlash(std::string& s);
  52. static std::string cmVS10EscapeXML(std::string arg)
  53. {
  54. cmSystemTools::ReplaceString(arg, "&", "&amp;");
  55. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  56. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  57. return arg;
  58. }
  59. static std::string cmVS10EscapeAttr(std::string arg)
  60. {
  61. cmSystemTools::ReplaceString(arg, "&", "&amp;");
  62. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  63. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  64. cmSystemTools::ReplaceString(arg, "\"", "&quot;");
  65. cmSystemTools::ReplaceString(arg, "\n", "&#10;");
  66. return arg;
  67. }
  68. struct cmVisualStudio10TargetGenerator::Elem
  69. {
  70. std::ostream& S;
  71. const int Indent;
  72. bool HasElements = false;
  73. bool HasContent = false;
  74. std::string Tag;
  75. Elem(std::ostream& s, std::string tag)
  76. : S(s)
  77. , Indent(0)
  78. , Tag(std::move(tag))
  79. {
  80. this->StartElement();
  81. }
  82. Elem(const Elem&) = delete;
  83. Elem(Elem& par, cm::string_view tag)
  84. : S(par.S)
  85. , Indent(par.Indent + 1)
  86. , Tag(std::string(tag))
  87. {
  88. par.SetHasElements();
  89. this->StartElement();
  90. }
  91. void SetHasElements()
  92. {
  93. if (!HasElements) {
  94. this->S << ">";
  95. HasElements = true;
  96. }
  97. }
  98. std::ostream& WriteString(const char* line);
  99. void StartElement() { this->WriteString("<") << this->Tag; }
  100. void Element(cm::string_view tag, std::string val)
  101. {
  102. Elem(*this, tag).Content(std::move(val));
  103. }
  104. Elem& Attribute(const char* an, std::string av)
  105. {
  106. this->S << " " << an << "=\"" << cmVS10EscapeAttr(std::move(av)) << "\"";
  107. return *this;
  108. }
  109. void Content(std::string val)
  110. {
  111. if (!this->HasContent) {
  112. this->S << ">";
  113. this->HasContent = true;
  114. }
  115. this->S << cmVS10EscapeXML(std::move(val));
  116. }
  117. ~Elem()
  118. {
  119. // Do not emit element which has not been started
  120. if (Tag.empty()) {
  121. return;
  122. }
  123. if (HasElements) {
  124. this->WriteString("</") << this->Tag << ">";
  125. } else if (HasContent) {
  126. this->S << "</" << this->Tag << ">";
  127. } else {
  128. this->S << " />";
  129. }
  130. }
  131. void WritePlatformConfigTag(const std::string& tag, const std::string& cond,
  132. const std::string& content);
  133. };
  134. class cmVS10GeneratorOptions : public cmVisualStudioGeneratorOptions
  135. {
  136. public:
  137. using Elem = cmVisualStudio10TargetGenerator::Elem;
  138. cmVS10GeneratorOptions(cmLocalVisualStudioGenerator* lg, Tool tool,
  139. cmVS7FlagTable const* table,
  140. cmVisualStudio10TargetGenerator* g = nullptr)
  141. : cmVisualStudioGeneratorOptions(lg, tool, table)
  142. , TargetGenerator(g)
  143. {
  144. }
  145. void OutputFlag(std::ostream& /*fout*/, int /*indent*/,
  146. const std::string& tag, const std::string& content) override
  147. {
  148. if (!this->GetConfiguration().empty()) {
  149. // if there are configuration specific flags, then
  150. // use the configuration specific tag for PreprocessorDefinitions
  151. const std::string cond =
  152. this->TargetGenerator->CalcCondition(this->GetConfiguration());
  153. this->Parent->WritePlatformConfigTag(tag, cond, content);
  154. } else {
  155. this->Parent->Element(tag, content);
  156. }
  157. }
  158. private:
  159. cmVisualStudio10TargetGenerator* const TargetGenerator;
  160. Elem* Parent = nullptr;
  161. friend cmVisualStudio10TargetGenerator::OptionsHelper;
  162. };
  163. struct cmVisualStudio10TargetGenerator::OptionsHelper
  164. {
  165. cmVS10GeneratorOptions& O;
  166. OptionsHelper(cmVS10GeneratorOptions& o, Elem& e)
  167. : O(o)
  168. {
  169. O.Parent = &e;
  170. }
  171. ~OptionsHelper() { O.Parent = nullptr; }
  172. void OutputPreprocessorDefinitions(const std::string& lang)
  173. {
  174. O.OutputPreprocessorDefinitions(O.Parent->S, O.Parent->Indent + 1, lang);
  175. }
  176. void OutputAdditionalIncludeDirectories(const std::string& lang)
  177. {
  178. O.OutputAdditionalIncludeDirectories(O.Parent->S, O.Parent->Indent + 1,
  179. lang);
  180. }
  181. void OutputFlagMap() { O.OutputFlagMap(O.Parent->S, O.Parent->Indent + 1); }
  182. void PrependInheritedString(std::string const& key)
  183. {
  184. O.PrependInheritedString(key);
  185. }
  186. };
  187. static std::string cmVS10EscapeComment(std::string const& comment)
  188. {
  189. // MSBuild takes the CDATA of a <Message></Message> element and just
  190. // does "echo $CDATA" with no escapes. We must encode the string.
  191. // http://technet.microsoft.com/en-us/library/cc772462%28WS.10%29.aspx
  192. std::string echoable;
  193. for (char c : comment) {
  194. switch (c) {
  195. case '\r':
  196. break;
  197. case '\n':
  198. echoable += '\t';
  199. break;
  200. case '"': /* no break */
  201. case '|': /* no break */
  202. case '&': /* no break */
  203. case '<': /* no break */
  204. case '>': /* no break */
  205. case '^':
  206. echoable += '^'; /* no break */
  207. CM_FALLTHROUGH;
  208. default:
  209. echoable += c;
  210. break;
  211. }
  212. }
  213. return echoable;
  214. }
  215. static bool cmVS10IsTargetsFile(std::string const& path)
  216. {
  217. std::string const ext = cmSystemTools::GetFilenameLastExtension(path);
  218. return cmSystemTools::Strucmp(ext.c_str(), ".targets") == 0;
  219. }
  220. static VsProjectType computeProjectType(cmGeneratorTarget const* t)
  221. {
  222. if (t->IsCSharpOnly()) {
  223. return VsProjectType::csproj;
  224. }
  225. return VsProjectType::vcxproj;
  226. }
  227. static std::string computeProjectFileExtension(VsProjectType projectType)
  228. {
  229. switch (projectType) {
  230. case VsProjectType::csproj:
  231. return ".csproj";
  232. case VsProjectType::proj:
  233. return ".proj";
  234. default:
  235. return ".vcxproj";
  236. }
  237. }
  238. static std::string computeProjectFileExtension(cmGeneratorTarget const* t)
  239. {
  240. return computeProjectFileExtension(computeProjectType(t));
  241. }
  242. cmVisualStudio10TargetGenerator::cmVisualStudio10TargetGenerator(
  243. cmGeneratorTarget* target, cmGlobalVisualStudio10Generator* gg)
  244. : GeneratorTarget(target)
  245. , Makefile(target->Target->GetMakefile())
  246. , Platform(gg->GetPlatformName())
  247. , Name(target->GetName())
  248. , GUID(gg->GetGUID(this->Name))
  249. , GlobalGenerator(gg)
  250. , LocalGenerator(
  251. (cmLocalVisualStudio10Generator*)target->GetLocalGenerator())
  252. {
  253. this->Configurations =
  254. this->Makefile->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  255. this->NsightTegra = gg->IsNsightTegra();
  256. this->Android = gg->TargetsAndroid();
  257. for (unsigned int& version : this->NsightTegraVersion) {
  258. version = 0;
  259. }
  260. sscanf(gg->GetNsightTegraVersion().c_str(), "%u.%u.%u.%u",
  261. &this->NsightTegraVersion[0], &this->NsightTegraVersion[1],
  262. &this->NsightTegraVersion[2], &this->NsightTegraVersion[3]);
  263. this->MSTools = !this->NsightTegra && !this->Android;
  264. this->Managed = false;
  265. this->TargetCompileAsWinRT = false;
  266. this->IsMissingFiles = false;
  267. this->DefaultArtifactDir =
  268. this->LocalGenerator->GetCurrentBinaryDirectory() + "/" +
  269. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  270. this->InSourceBuild = (this->Makefile->GetCurrentSourceDirectory() ==
  271. this->Makefile->GetCurrentBinaryDirectory());
  272. this->ClassifyAllConfigSources();
  273. }
  274. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator() = default;
  275. std::string cmVisualStudio10TargetGenerator::CalcCondition(
  276. const std::string& config) const
  277. {
  278. std::ostringstream oss;
  279. oss << "'$(Configuration)|$(Platform)'=='";
  280. oss << config << "|" << this->Platform;
  281. oss << "'";
  282. // handle special case for 32 bit C# targets
  283. if (this->ProjectType == VsProjectType::csproj &&
  284. this->Platform == "Win32") {
  285. oss << " Or ";
  286. oss << "'$(Configuration)|$(Platform)'=='";
  287. oss << config << "|x86";
  288. oss << "'";
  289. }
  290. return oss.str();
  291. }
  292. void cmVisualStudio10TargetGenerator::Elem::WritePlatformConfigTag(
  293. const std::string& tag, const std::string& cond, const std::string& content)
  294. {
  295. Elem(*this, tag).Attribute("Condition", cond).Content(content);
  296. }
  297. std::ostream& cmVisualStudio10TargetGenerator::Elem::WriteString(
  298. const char* line)
  299. {
  300. this->S << '\n';
  301. this->S.fill(' ');
  302. this->S.width(this->Indent * 2);
  303. // write an empty string to get the fill level indent to print
  304. this->S << "";
  305. this->S << line;
  306. return this->S;
  307. }
  308. #define VS10_CXX_DEFAULT_PROPS "$(VCTargetsPath)\\Microsoft.Cpp.Default.props"
  309. #define VS10_CXX_PROPS "$(VCTargetsPath)\\Microsoft.Cpp.props"
  310. #define VS10_CXX_USER_PROPS \
  311. "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props"
  312. #define VS10_CXX_TARGETS "$(VCTargetsPath)\\Microsoft.Cpp.targets"
  313. #define VS10_CSharp_DEFAULT_PROPS \
  314. "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props"
  315. // This does not seem to exist by default, it's just provided for consistency
  316. // in case users want to have default custom props for C# targets
  317. #define VS10_CSharp_USER_PROPS \
  318. "$(UserRootDir)\\Microsoft.CSharp.$(Platform).user.props"
  319. #define VS10_CSharp_TARGETS "$(MSBuildToolsPath)\\Microsoft.CSharp.targets"
  320. #define VS10_CSharp_NETCF_TARGETS \
  321. "$(MSBuildExtensionsPath)\\Microsoft\\$(TargetFrameworkIdentifier)\\" \
  322. "$(TargetFrameworkTargetsVersion)\\Microsoft.$(TargetFrameworkIdentifier)" \
  323. ".CSharp.targets"
  324. void cmVisualStudio10TargetGenerator::Generate()
  325. {
  326. for (std::string const& config : this->Configurations) {
  327. this->GeneratorTarget->CheckCxxModuleStatus(config);
  328. }
  329. if (this->GeneratorTarget->HaveCxx20ModuleSources() &&
  330. !this->GlobalGenerator->SupportsCxxModuleDyndep()) {
  331. this->Makefile->IssueMessage(
  332. MessageType::FATAL_ERROR,
  333. cmStrCat("The \"", this->GeneratorTarget->GetName(),
  334. "\" target contains C++ module sources which are not supported "
  335. "by the generator"));
  336. }
  337. this->ProjectType = computeProjectType(this->GeneratorTarget);
  338. this->Managed = this->ProjectType == VsProjectType::csproj;
  339. const std::string ProjectFileExtension =
  340. computeProjectFileExtension(this->ProjectType);
  341. if (this->ProjectType == VsProjectType::csproj &&
  342. this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  343. std::string message = "The C# target \"" +
  344. this->GeneratorTarget->GetName() +
  345. "\" is of type STATIC_LIBRARY. This is discouraged (and may be "
  346. "disabled in future). Make it a SHARED library instead.";
  347. this->Makefile->IssueMessage(MessageType::DEPRECATION_WARNING, message);
  348. }
  349. if (this->Android &&
  350. this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE &&
  351. !this->GeneratorTarget->Target->IsAndroidGuiExecutable()) {
  352. this->GlobalGenerator->AddAndroidExecutableWarning(this->Name);
  353. }
  354. // Tell the global generator the name of the project file
  355. this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME",
  356. this->Name);
  357. this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  358. ProjectFileExtension);
  359. this->DotNetHintReferences.clear();
  360. this->AdditionalUsingDirectories.clear();
  361. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  362. if (!this->ComputeClOptions()) {
  363. return;
  364. }
  365. if (!this->ComputeRcOptions()) {
  366. return;
  367. }
  368. if (!this->ComputeCudaOptions()) {
  369. return;
  370. }
  371. if (!this->ComputeCudaLinkOptions()) {
  372. return;
  373. }
  374. if (!this->ComputeMarmasmOptions()) {
  375. return;
  376. }
  377. if (!this->ComputeMasmOptions()) {
  378. return;
  379. }
  380. if (!this->ComputeNasmOptions()) {
  381. return;
  382. }
  383. if (!this->ComputeLinkOptions()) {
  384. return;
  385. }
  386. if (!this->ComputeLibOptions()) {
  387. return;
  388. }
  389. }
  390. std::string path =
  391. cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
  392. this->Name, ProjectFileExtension);
  393. cmGeneratedFileStream BuildFileStream(path);
  394. const std::string& PathToProjectFile = path;
  395. BuildFileStream.SetCopyIfDifferent(true);
  396. // Write the encoding header into the file
  397. char magic[] = { char(0xEF), char(0xBB), char(0xBF) };
  398. BuildFileStream.write(magic, 3);
  399. if (this->ProjectType == VsProjectType::csproj &&
  400. this->GeneratorTarget->IsDotNetSdkTarget() &&
  401. this->GlobalGenerator->GetVersion() >=
  402. cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  403. this->WriteSdkStyleProjectFile(BuildFileStream);
  404. } else {
  405. this->WriteClassicMsBuildProjectFile(BuildFileStream);
  406. }
  407. if (BuildFileStream.Close()) {
  408. this->GlobalGenerator->FileReplacedDuringGenerate(PathToProjectFile);
  409. }
  410. // The groups are stored in a separate file for VS 10
  411. this->WriteGroups();
  412. // Update cache with project-specific entries.
  413. this->UpdateCache();
  414. }
  415. void cmVisualStudio10TargetGenerator::WriteClassicMsBuildProjectFile(
  416. cmGeneratedFileStream& BuildFileStream)
  417. {
  418. BuildFileStream << R"(<?xml version="1.0" encoding=")"
  419. << this->GlobalGenerator->Encoding() << "\"?>";
  420. {
  421. Elem e0(BuildFileStream, "Project");
  422. e0.Attribute("DefaultTargets", "Build");
  423. const char* toolsVersion = this->GlobalGenerator->GetToolsVersion();
  424. if (this->GlobalGenerator->GetVersion() ==
  425. cmGlobalVisualStudioGenerator::VSVersion::VS12 &&
  426. this->GlobalGenerator->TargetsWindowsCE()) {
  427. toolsVersion = "4.0";
  428. }
  429. e0.Attribute("ToolsVersion", toolsVersion);
  430. e0.Attribute("xmlns",
  431. "http://schemas.microsoft.com/developer/msbuild/2003");
  432. if (this->NsightTegra) {
  433. Elem e1(e0, "PropertyGroup");
  434. e1.Attribute("Label", "NsightTegraProject");
  435. const unsigned int nsightTegraMajorVersion = this->NsightTegraVersion[0];
  436. const unsigned int nsightTegraMinorVersion = this->NsightTegraVersion[1];
  437. if (nsightTegraMajorVersion >= 2) {
  438. if (nsightTegraMajorVersion > 3 ||
  439. (nsightTegraMajorVersion == 3 && nsightTegraMinorVersion >= 1)) {
  440. e1.Element("NsightTegraProjectRevisionNumber", "11");
  441. } else {
  442. // Nsight Tegra 2.0 uses project revision 9.
  443. e1.Element("NsightTegraProjectRevisionNumber", "9");
  444. }
  445. // Tell newer versions to upgrade silently when loading.
  446. e1.Element("NsightTegraUpgradeOnceWithoutPrompt", "true");
  447. } else {
  448. // Require Nsight Tegra 1.6 for JCompile support.
  449. e1.Element("NsightTegraProjectRevisionNumber", "7");
  450. }
  451. }
  452. if (const char* hostArch =
  453. this->GlobalGenerator->GetPlatformToolsetHostArchitecture()) {
  454. Elem e1(e0, "PropertyGroup");
  455. e1.Element("PreferredToolArchitecture", hostArch);
  456. }
  457. // The ALL_BUILD, PACKAGE, and ZERO_CHECK projects transitively include
  458. // Microsoft.Common.CurrentVersion.targets which triggers Target
  459. // ResolveNugetPackageAssets when SDK-style targets are in the project.
  460. // However, these projects have no nuget packages to reference and the
  461. // build fails.
  462. // Setting ResolveNugetPackages to false skips this target and the build
  463. // succeeds.
  464. cm::string_view targetName{ this->GeneratorTarget->GetName() };
  465. if (targetName == "ALL_BUILD" || targetName == "PACKAGE" ||
  466. targetName == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  467. Elem e1(e0, "PropertyGroup");
  468. e1.Element("ResolveNugetPackages", "false");
  469. }
  470. if (this->ProjectType != VsProjectType::csproj) {
  471. this->WriteProjectConfigurations(e0);
  472. }
  473. {
  474. Elem e1(e0, "PropertyGroup");
  475. this->WriteCommonPropertyGroupGlobals(e1);
  476. if ((this->MSTools || this->Android) &&
  477. this->GeneratorTarget->IsInBuildSystem()) {
  478. this->WriteApplicationTypeSettings(e1);
  479. this->VerifyNecessaryFiles();
  480. }
  481. cmValue vsProjectName =
  482. this->GeneratorTarget->GetProperty("VS_SCC_PROJECTNAME");
  483. cmValue vsLocalPath =
  484. this->GeneratorTarget->GetProperty("VS_SCC_LOCALPATH");
  485. cmValue vsProvider =
  486. this->GeneratorTarget->GetProperty("VS_SCC_PROVIDER");
  487. if (vsProjectName && vsLocalPath && vsProvider) {
  488. e1.Element("SccProjectName", *vsProjectName);
  489. e1.Element("SccLocalPath", *vsLocalPath);
  490. e1.Element("SccProvider", *vsProvider);
  491. cmValue vsAuxPath =
  492. this->GeneratorTarget->GetProperty("VS_SCC_AUXPATH");
  493. if (vsAuxPath) {
  494. e1.Element("SccAuxPath", *vsAuxPath);
  495. }
  496. }
  497. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT")) {
  498. e1.Element("WinMDAssembly", "true");
  499. }
  500. e1.Element("Platform", this->Platform);
  501. cmValue projLabel = this->GeneratorTarget->GetProperty("PROJECT_LABEL");
  502. e1.Element("ProjectName", projLabel ? *projLabel : this->Name);
  503. {
  504. cm::optional<std::string> targetFramework;
  505. cm::optional<std::string> targetFrameworkVersion;
  506. cm::optional<std::string> targetFrameworkIdentifier;
  507. cm::optional<std::string> targetFrameworkTargetsVersion;
  508. if (cmValue tf =
  509. this->GeneratorTarget->GetProperty("DOTNET_TARGET_FRAMEWORK")) {
  510. targetFramework = *tf;
  511. } else if (cmValue vstfVer = this->GeneratorTarget->GetProperty(
  512. "VS_DOTNET_TARGET_FRAMEWORK_VERSION")) {
  513. // FIXME: Someday, add a deprecation warning for VS_* property.
  514. targetFrameworkVersion = *vstfVer;
  515. } else if (cmValue tfVer = this->GeneratorTarget->GetProperty(
  516. "DOTNET_TARGET_FRAMEWORK_VERSION")) {
  517. targetFrameworkVersion = *tfVer;
  518. } else if (this->ProjectType == VsProjectType::csproj) {
  519. targetFrameworkVersion =
  520. this->GlobalGenerator->GetTargetFrameworkVersion();
  521. }
  522. if (this->ProjectType == VsProjectType::vcxproj &&
  523. this->GlobalGenerator->TargetsWindowsCE()) {
  524. e1.Element("EnableRedirectPlatform", "true");
  525. e1.Element("RedirectPlatformValue", this->Platform);
  526. }
  527. if (this->ProjectType == VsProjectType::csproj) {
  528. if (this->GlobalGenerator->TargetsWindowsCE()) {
  529. // FIXME: These target VS_TARGET_FRAMEWORK* target properties
  530. // are undocumented settings only ever supported for WinCE.
  531. // We need a better way to control these in general.
  532. if (cmValue tfId = this->GeneratorTarget->GetProperty(
  533. "VS_TARGET_FRAMEWORK_IDENTIFIER")) {
  534. targetFrameworkIdentifier = *tfId;
  535. }
  536. if (cmValue tfTargetsVer = this->GeneratorTarget->GetProperty(
  537. "VS_TARGET_FRAMEWORKS_TARGET_VERSION")) {
  538. targetFrameworkTargetsVersion = *tfTargetsVer;
  539. }
  540. }
  541. if (!targetFrameworkIdentifier) {
  542. targetFrameworkIdentifier =
  543. this->GlobalGenerator->GetTargetFrameworkIdentifier();
  544. }
  545. if (!targetFrameworkTargetsVersion) {
  546. targetFrameworkTargetsVersion =
  547. this->GlobalGenerator->GetTargetFrameworkTargetsVersion();
  548. }
  549. }
  550. if (targetFramework) {
  551. if (targetFramework->find(';') != std::string::npos) {
  552. e1.Element("TargetFrameworks", *targetFramework);
  553. } else {
  554. e1.Element("TargetFramework", *targetFramework);
  555. }
  556. }
  557. if (targetFrameworkVersion) {
  558. e1.Element("TargetFrameworkVersion", *targetFrameworkVersion);
  559. }
  560. if (targetFrameworkIdentifier) {
  561. e1.Element("TargetFrameworkIdentifier", *targetFrameworkIdentifier);
  562. }
  563. if (targetFrameworkTargetsVersion) {
  564. e1.Element("TargetFrameworkTargetsVersion",
  565. *targetFrameworkTargetsVersion);
  566. }
  567. if (!this->GlobalGenerator->GetPlatformToolsetCudaCustomDirString()
  568. .empty()) {
  569. e1.Element(
  570. "CudaToolkitCustomDir",
  571. this->GlobalGenerator->GetPlatformToolsetCudaCustomDirString() +
  572. this->GlobalGenerator->GetPlatformToolsetCudaNvccSubdirString());
  573. }
  574. }
  575. // Disable the project upgrade prompt that is displayed the first time a
  576. // project using an older toolset version is opened in a newer version of
  577. // the IDE (respected by VS 2013 and above).
  578. if (this->GlobalGenerator->GetVersion() >=
  579. cmGlobalVisualStudioGenerator::VSVersion::VS12) {
  580. e1.Element("VCProjectUpgraderObjectName", "NoUpgrade");
  581. }
  582. if (const char* vcTargetsPath =
  583. this->GlobalGenerator->GetCustomVCTargetsPath()) {
  584. e1.Element("VCTargetsPath", vcTargetsPath);
  585. }
  586. if (this->Managed) {
  587. if (this->LocalGenerator->GetVersion() >=
  588. cmGlobalVisualStudioGenerator::VSVersion::VS17) {
  589. e1.Element("ManagedAssembly", "true");
  590. }
  591. std::string outputType;
  592. switch (this->GeneratorTarget->GetType()) {
  593. case cmStateEnums::OBJECT_LIBRARY:
  594. case cmStateEnums::STATIC_LIBRARY:
  595. case cmStateEnums::SHARED_LIBRARY:
  596. outputType = "Library";
  597. break;
  598. case cmStateEnums::MODULE_LIBRARY:
  599. outputType = "Module";
  600. break;
  601. case cmStateEnums::EXECUTABLE: {
  602. auto const win32 =
  603. this->GeneratorTarget->GetSafeProperty("WIN32_EXECUTABLE");
  604. if (win32.find("$<") != std::string::npos) {
  605. this->Makefile->IssueMessage(
  606. MessageType::FATAL_ERROR,
  607. cmStrCat(
  608. "Target \"", this->GeneratorTarget->GetName(),
  609. "\" has a generator expression in its WIN32_EXECUTABLE "
  610. "property. This is not supported on managed executables."));
  611. return;
  612. }
  613. if (cmIsOn(win32)) {
  614. outputType = "WinExe";
  615. } else {
  616. outputType = "Exe";
  617. }
  618. } break;
  619. case cmStateEnums::UTILITY:
  620. case cmStateEnums::INTERFACE_LIBRARY:
  621. case cmStateEnums::GLOBAL_TARGET:
  622. outputType = "Utility";
  623. break;
  624. case cmStateEnums::UNKNOWN_LIBRARY:
  625. break;
  626. }
  627. e1.Element("OutputType", outputType);
  628. e1.Element("AppDesignerFolder", "Properties");
  629. }
  630. }
  631. cmValue startupObject =
  632. this->GeneratorTarget->GetProperty("VS_DOTNET_STARTUP_OBJECT");
  633. if (startupObject && this->Managed) {
  634. Elem e1(e0, "PropertyGroup");
  635. e1.Element("StartupObject", *startupObject);
  636. }
  637. switch (this->ProjectType) {
  638. case VsProjectType::vcxproj: {
  639. Elem(e0, "Import").Attribute("Project", VS10_CXX_DEFAULT_PROPS);
  640. std::string const& props =
  641. this->GlobalGenerator->GetPlatformToolsetVersionProps();
  642. if (!props.empty()) {
  643. Elem(e0, "Import").Attribute("Project", props);
  644. }
  645. } break;
  646. case VsProjectType::csproj:
  647. Elem(e0, "Import")
  648. .Attribute("Project", VS10_CSharp_DEFAULT_PROPS)
  649. .Attribute("Condition", "Exists('" VS10_CSharp_DEFAULT_PROPS "')");
  650. break;
  651. default:
  652. break;
  653. }
  654. this->WriteProjectConfigurationValues(e0);
  655. if (this->ProjectType == VsProjectType::vcxproj) {
  656. Elem(e0, "Import").Attribute("Project", VS10_CXX_PROPS);
  657. }
  658. {
  659. Elem e1(e0, "ImportGroup");
  660. e1.Attribute("Label", "ExtensionSettings");
  661. e1.SetHasElements();
  662. if (this->GlobalGenerator->IsCudaEnabled()) {
  663. auto customDir =
  664. this->GlobalGenerator->GetPlatformToolsetCudaCustomDirString();
  665. std::string cudaPath = customDir.empty()
  666. ? "$(VCTargetsPath)\\BuildCustomizations\\"
  667. : customDir +
  668. this->GlobalGenerator
  669. ->GetPlatformToolsetCudaVSIntegrationSubdirString() +
  670. R"(extras\visual_studio_integration\MSBuildExtensions\)";
  671. Elem(e1, "Import")
  672. .Attribute("Project",
  673. std::move(cudaPath) + "CUDA " +
  674. this->GlobalGenerator->GetPlatformToolsetCuda() +
  675. ".props");
  676. }
  677. if (this->GlobalGenerator->IsMarmasmEnabled()) {
  678. Elem(e1, "Import")
  679. .Attribute("Project",
  680. "$(VCTargetsPath)\\BuildCustomizations\\marmasm.props");
  681. }
  682. if (this->GlobalGenerator->IsMasmEnabled()) {
  683. Elem(e1, "Import")
  684. .Attribute("Project",
  685. "$(VCTargetsPath)\\BuildCustomizations\\masm.props");
  686. }
  687. if (this->GlobalGenerator->IsNasmEnabled()) {
  688. // Always search in the standard modules location.
  689. std::string propsTemplate =
  690. GetCMakeFilePath("Templates/MSBuild/nasm.props.in");
  691. std::string propsLocal =
  692. cmStrCat(this->DefaultArtifactDir, "\\nasm.props");
  693. ConvertToWindowsSlash(propsLocal);
  694. this->Makefile->ConfigureFile(propsTemplate, propsLocal, false, true,
  695. true);
  696. Elem(e1, "Import").Attribute("Project", propsLocal);
  697. }
  698. }
  699. {
  700. Elem e1(e0, "ImportGroup");
  701. e1.Attribute("Label", "PropertySheets");
  702. std::string props;
  703. switch (this->ProjectType) {
  704. case VsProjectType::vcxproj:
  705. props = VS10_CXX_USER_PROPS;
  706. break;
  707. case VsProjectType::csproj:
  708. props = VS10_CSharp_USER_PROPS;
  709. break;
  710. default:
  711. break;
  712. }
  713. if (cmValue p = this->GeneratorTarget->GetProperty("VS_USER_PROPS")) {
  714. props = *p;
  715. }
  716. if (!props.empty()) {
  717. ConvertToWindowsSlash(props);
  718. Elem(e1, "Import")
  719. .Attribute("Project", props)
  720. .Attribute("Condition", "exists('" + props + "')")
  721. .Attribute("Label", "LocalAppDataPlatform");
  722. }
  723. this->WritePlatformExtensions(e1);
  724. }
  725. this->WriteDotNetDocumentationFile(e0);
  726. Elem(e0, "PropertyGroup").Attribute("Label", "UserMacros");
  727. this->WriteWinRTPackageCertificateKeyFile(e0);
  728. this->WritePathAndIncrementalLinkOptions(e0);
  729. this->WriteCEDebugProjectConfigurationValues(e0);
  730. this->WriteItemDefinitionGroups(e0);
  731. this->WriteCustomCommands(e0);
  732. this->WriteAllSources(e0);
  733. this->WriteDotNetReferences(e0);
  734. this->WritePackageReferences(e0);
  735. this->WriteImports(e0);
  736. this->WriteEmbeddedResourceGroup(e0);
  737. this->WriteXamlFilesGroup(e0);
  738. this->WriteWinRTReferences(e0);
  739. this->WriteProjectReferences(e0);
  740. this->WriteSDKReferences(e0);
  741. switch (this->ProjectType) {
  742. case VsProjectType::vcxproj:
  743. Elem(e0, "Import").Attribute("Project", VS10_CXX_TARGETS);
  744. break;
  745. case VsProjectType::csproj:
  746. if (this->GlobalGenerator->TargetsWindowsCE()) {
  747. Elem(e0, "Import").Attribute("Project", VS10_CSharp_NETCF_TARGETS);
  748. } else {
  749. Elem(e0, "Import").Attribute("Project", VS10_CSharp_TARGETS);
  750. }
  751. break;
  752. default:
  753. break;
  754. }
  755. this->WriteTargetSpecificReferences(e0);
  756. {
  757. Elem e1(e0, "ImportGroup");
  758. e1.Attribute("Label", "ExtensionTargets");
  759. e1.SetHasElements();
  760. this->WriteTargetsFileReferences(e1);
  761. if (this->GlobalGenerator->IsCudaEnabled()) {
  762. auto customDir =
  763. this->GlobalGenerator->GetPlatformToolsetCudaCustomDirString();
  764. std::string cudaPath = customDir.empty()
  765. ? "$(VCTargetsPath)\\BuildCustomizations\\"
  766. : customDir +
  767. this->GlobalGenerator
  768. ->GetPlatformToolsetCudaVSIntegrationSubdirString() +
  769. R"(extras\visual_studio_integration\MSBuildExtensions\)";
  770. Elem(e1, "Import")
  771. .Attribute("Project",
  772. std::move(cudaPath) + "CUDA " +
  773. this->GlobalGenerator->GetPlatformToolsetCuda() +
  774. ".targets");
  775. }
  776. if (this->GlobalGenerator->IsMarmasmEnabled()) {
  777. Elem(e1, "Import")
  778. .Attribute("Project",
  779. "$(VCTargetsPath)\\BuildCustomizations\\marmasm.targets");
  780. }
  781. if (this->GlobalGenerator->IsMasmEnabled()) {
  782. Elem(e1, "Import")
  783. .Attribute("Project",
  784. "$(VCTargetsPath)\\BuildCustomizations\\masm.targets");
  785. }
  786. if (this->GlobalGenerator->IsNasmEnabled()) {
  787. std::string nasmTargets =
  788. GetCMakeFilePath("Templates/MSBuild/nasm.targets");
  789. Elem(e1, "Import").Attribute("Project", nasmTargets);
  790. }
  791. }
  792. if (this->ProjectType == VsProjectType::vcxproj &&
  793. this->HaveCustomCommandDepfile) {
  794. std::string depfileTargets =
  795. GetCMakeFilePath("Templates/MSBuild/CustomBuildDepFile.targets");
  796. Elem(e0, "Import").Attribute("Project", depfileTargets);
  797. }
  798. if (this->ProjectType == VsProjectType::csproj) {
  799. for (std::string const& c : this->Configurations) {
  800. Elem e1(e0, "PropertyGroup");
  801. e1.Attribute("Condition", "'$(Configuration)' == '" + c + "'");
  802. e1.SetHasElements();
  803. this->WriteEvents(e1, c);
  804. }
  805. // make sure custom commands are executed before build (if necessary)
  806. {
  807. Elem e1(e0, "PropertyGroup");
  808. std::ostringstream oss;
  809. oss << "\n";
  810. for (std::string const& i : this->CSharpCustomCommandNames) {
  811. oss << " " << i << ";\n";
  812. }
  813. oss << " "
  814. << "$(BuildDependsOn)\n";
  815. e1.Element("BuildDependsOn", oss.str());
  816. }
  817. }
  818. }
  819. }
  820. void cmVisualStudio10TargetGenerator::WriteSdkStyleProjectFile(
  821. cmGeneratedFileStream& BuildFileStream)
  822. {
  823. if (this->ProjectType != VsProjectType::csproj ||
  824. !this->GeneratorTarget->IsDotNetSdkTarget()) {
  825. std::string message = "The target \"" + this->GeneratorTarget->GetName() +
  826. "\" is not eligible for .Net SDK style project.";
  827. this->Makefile->IssueMessage(MessageType::INTERNAL_ERROR, message);
  828. return;
  829. }
  830. if (this->HasCustomCommands()) {
  831. std::string message = "The target \"" + this->GeneratorTarget->GetName() +
  832. "\" does not currently support add_custom_command as the Visual Studio "
  833. "generators have not yet learned how to generate custom commands in "
  834. ".Net SDK-style projects.";
  835. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, message);
  836. return;
  837. }
  838. Elem e0(BuildFileStream, "Project");
  839. e0.Attribute("Sdk", *this->GeneratorTarget->GetProperty("DOTNET_SDK"));
  840. {
  841. Elem e1(e0, "PropertyGroup");
  842. this->WriteCommonPropertyGroupGlobals(e1);
  843. e1.Element("EnableDefaultItems", "false");
  844. // Disable the project upgrade prompt that is displayed the first time a
  845. // project using an older toolset version is opened in a newer version
  846. // of the IDE.
  847. e1.Element("VCProjectUpgraderObjectName", "NoUpgrade");
  848. e1.Element("ManagedAssembly", "true");
  849. cmValue targetFramework =
  850. this->GeneratorTarget->GetProperty("DOTNET_TARGET_FRAMEWORK");
  851. if (targetFramework) {
  852. if (targetFramework->find(';') != std::string::npos) {
  853. e1.Element("TargetFrameworks", *targetFramework);
  854. } else {
  855. e1.Element("TargetFramework", *targetFramework);
  856. e1.Element("AppendTargetFrameworkToOutputPath", "false");
  857. }
  858. } else {
  859. e1.Element("TargetFramework", "net5.0");
  860. e1.Element("AppendTargetFrameworkToOutputPath", "false");
  861. }
  862. std::string outputType;
  863. switch (this->GeneratorTarget->GetType()) {
  864. case cmStateEnums::OBJECT_LIBRARY:
  865. case cmStateEnums::STATIC_LIBRARY:
  866. case cmStateEnums::MODULE_LIBRARY:
  867. this->Makefile->IssueMessage(
  868. MessageType::FATAL_ERROR,
  869. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  870. "\" is of a type not supported for managed binaries."));
  871. return;
  872. case cmStateEnums::SHARED_LIBRARY:
  873. outputType = "Library";
  874. break;
  875. case cmStateEnums::EXECUTABLE: {
  876. auto const win32 =
  877. this->GeneratorTarget->GetSafeProperty("WIN32_EXECUTABLE");
  878. if (win32.find("$<") != std::string::npos) {
  879. this->Makefile->IssueMessage(
  880. MessageType::FATAL_ERROR,
  881. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  882. "\" has a generator expression in its WIN32_EXECUTABLE "
  883. "property. This is not supported on managed "
  884. "executables."));
  885. return;
  886. }
  887. if (cmIsOn(win32)) {
  888. outputType = "WinExe";
  889. } else {
  890. outputType = "Exe";
  891. }
  892. } break;
  893. case cmStateEnums::UTILITY:
  894. case cmStateEnums::INTERFACE_LIBRARY:
  895. case cmStateEnums::GLOBAL_TARGET:
  896. outputType = "Utility";
  897. break;
  898. case cmStateEnums::UNKNOWN_LIBRARY:
  899. break;
  900. }
  901. e1.Element("OutputType", outputType);
  902. cmValue startupObject =
  903. this->GeneratorTarget->GetProperty("VS_DOTNET_STARTUP_OBJECT");
  904. if (startupObject) {
  905. e1.Element("StartupObject", *startupObject);
  906. }
  907. }
  908. for (const std::string& config : this->Configurations) {
  909. Elem e1(e0, "PropertyGroup");
  910. e1.Attribute("Condition", "'$(Configuration)' == '" + config + "'");
  911. e1.SetHasElements();
  912. this->WriteEvents(e1, config);
  913. std::string outDir = this->GeneratorTarget->GetDirectory(config) + "/";
  914. ConvertToWindowsSlash(outDir);
  915. e1.Element("OutputPath", outDir);
  916. Options& o = *(this->ClOptions[config]);
  917. OptionsHelper oh(o, e1);
  918. oh.OutputFlagMap();
  919. }
  920. this->WriteDotNetDocumentationFile(e0);
  921. this->WriteAllSources(e0);
  922. this->WriteEmbeddedResourceGroup(e0);
  923. this->WriteXamlFilesGroup(e0);
  924. this->WriteDotNetReferences(e0);
  925. this->WritePackageReferences(e0);
  926. this->WriteProjectReferences(e0);
  927. }
  928. void cmVisualStudio10TargetGenerator::WriteCommonPropertyGroupGlobals(Elem& e1)
  929. {
  930. e1.Attribute("Label", "Globals");
  931. e1.Element("ProjectGuid", "{" + this->GUID + "}");
  932. cmValue vsProjectTypes =
  933. this->GeneratorTarget->GetProperty("VS_GLOBAL_PROJECT_TYPES");
  934. if (vsProjectTypes) {
  935. const char* tagName = "ProjectTypes";
  936. if (this->ProjectType == VsProjectType::csproj) {
  937. tagName = "ProjectTypeGuids";
  938. }
  939. e1.Element(tagName, *vsProjectTypes);
  940. }
  941. cmValue vsGlobalKeyword =
  942. this->GeneratorTarget->GetProperty("VS_GLOBAL_KEYWORD");
  943. if (!vsGlobalKeyword) {
  944. if (this->GlobalGenerator->TargetsAndroid()) {
  945. e1.Element("Keyword", "Android");
  946. } else {
  947. e1.Element("Keyword", "Win32Proj");
  948. }
  949. } else {
  950. e1.Element("Keyword", *vsGlobalKeyword);
  951. }
  952. cmValue vsGlobalRootNamespace =
  953. this->GeneratorTarget->GetProperty("VS_GLOBAL_ROOTNAMESPACE");
  954. if (vsGlobalRootNamespace) {
  955. e1.Element("RootNamespace", *vsGlobalRootNamespace);
  956. }
  957. std::vector<std::string> keys = this->GeneratorTarget->GetPropertyKeys();
  958. for (std::string const& keyIt : keys) {
  959. static const cm::string_view prefix = "VS_GLOBAL_";
  960. if (!cmHasPrefix(keyIt, prefix)) {
  961. continue;
  962. }
  963. cm::string_view globalKey = cm::string_view(keyIt).substr(prefix.length());
  964. // Skip invalid or separately-handled properties.
  965. if (globalKey.empty() || globalKey == "PROJECT_TYPES" ||
  966. globalKey == "ROOTNAMESPACE" || globalKey == "KEYWORD") {
  967. continue;
  968. }
  969. cmValue value = this->GeneratorTarget->GetProperty(keyIt);
  970. if (!value) {
  971. continue;
  972. }
  973. e1.Element(globalKey, *value);
  974. }
  975. }
  976. bool cmVisualStudio10TargetGenerator::HasCustomCommands() const
  977. {
  978. if (!this->GeneratorTarget->GetPreBuildCommands().empty() ||
  979. !this->GeneratorTarget->GetPreLinkCommands().empty() ||
  980. !this->GeneratorTarget->GetPostBuildCommands().empty()) {
  981. return true;
  982. }
  983. auto const& config_sources = this->GeneratorTarget->GetAllConfigSources();
  984. return std::any_of(config_sources.begin(), config_sources.end(),
  985. [](cmGeneratorTarget::AllConfigSource const& si) {
  986. return si.Source->GetCustomCommand();
  987. });
  988. }
  989. void cmVisualStudio10TargetGenerator::WritePackageReferences(Elem& e0)
  990. {
  991. std::vector<std::string> packageReferences =
  992. this->GeneratorTarget->GetPackageReferences();
  993. if (!packageReferences.empty()) {
  994. Elem e1(e0, "ItemGroup");
  995. for (std::string const& ri : packageReferences) {
  996. size_t versionIndex = ri.find_last_of('_');
  997. if (versionIndex != std::string::npos) {
  998. Elem e2(e1, "PackageReference");
  999. e2.Attribute("Include", ri.substr(0, versionIndex));
  1000. e2.Attribute("Version", ri.substr(versionIndex + 1));
  1001. }
  1002. }
  1003. }
  1004. }
  1005. void cmVisualStudio10TargetGenerator::WriteDotNetReferences(Elem& e0)
  1006. {
  1007. std::vector<std::string> references;
  1008. if (cmValue vsDotNetReferences =
  1009. this->GeneratorTarget->GetProperty("VS_DOTNET_REFERENCES")) {
  1010. cmExpandList(*vsDotNetReferences, references);
  1011. }
  1012. cmPropertyMap const& props = this->GeneratorTarget->Target->GetProperties();
  1013. for (auto const& i : props.GetList()) {
  1014. static const cm::string_view vsDnRef = "VS_DOTNET_REFERENCE_";
  1015. if (cmHasPrefix(i.first, vsDnRef)) {
  1016. std::string path = i.second;
  1017. if (!cmsys::SystemTools::FileIsFullPath(path)) {
  1018. path = this->Makefile->GetCurrentSourceDirectory() + "/" + path;
  1019. }
  1020. ConvertToWindowsSlash(path);
  1021. this->DotNetHintReferences[""].emplace_back(
  1022. DotNetHintReference(i.first.substr(vsDnRef.length()), path));
  1023. }
  1024. }
  1025. if (!references.empty() || !this->DotNetHintReferences.empty()) {
  1026. Elem e1(e0, "ItemGroup");
  1027. for (std::string const& ri : references) {
  1028. // if the entry from VS_DOTNET_REFERENCES is an existing file, generate
  1029. // a new hint-reference and name it from the filename
  1030. if (cmsys::SystemTools::FileExists(ri, true)) {
  1031. std::string name =
  1032. cmsys::SystemTools::GetFilenameWithoutLastExtension(ri);
  1033. std::string path = ri;
  1034. ConvertToWindowsSlash(path);
  1035. this->DotNetHintReferences[""].emplace_back(
  1036. DotNetHintReference(name, path));
  1037. } else {
  1038. this->WriteDotNetReference(e1, ri, "", "");
  1039. }
  1040. }
  1041. for (const auto& h : this->DotNetHintReferences) {
  1042. // DotNetHintReferences is also populated from AddLibraries().
  1043. // The configuration specific hint references are added there.
  1044. for (const auto& i : h.second) {
  1045. this->WriteDotNetReference(e1, i.first, i.second, h.first);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. void cmVisualStudio10TargetGenerator::WriteDotNetReference(
  1051. Elem& e1, std::string const& ref, std::string const& hint,
  1052. std::string const& config)
  1053. {
  1054. Elem e2(e1, "Reference");
  1055. // If 'config' is not empty, the reference is only added for the given
  1056. // configuration. This is used when referencing imported managed assemblies.
  1057. // See also cmVisualStudio10TargetGenerator::AddLibraries().
  1058. if (!config.empty()) {
  1059. e2.Attribute("Condition", this->CalcCondition(config));
  1060. }
  1061. e2.Attribute("Include", ref);
  1062. e2.Element("CopyLocalSatelliteAssemblies", "true");
  1063. e2.Element("ReferenceOutputAssembly", "true");
  1064. if (!hint.empty()) {
  1065. const char* privateReference = "True";
  1066. if (cmValue value = this->GeneratorTarget->GetProperty(
  1067. "VS_DOTNET_REFERENCES_COPY_LOCAL")) {
  1068. if (cmIsOff(*value)) {
  1069. privateReference = "False";
  1070. }
  1071. }
  1072. e2.Element("Private", privateReference);
  1073. e2.Element("HintPath", hint);
  1074. }
  1075. this->WriteDotNetReferenceCustomTags(e2, ref);
  1076. }
  1077. void cmVisualStudio10TargetGenerator::WriteImports(Elem& e0)
  1078. {
  1079. cmValue imports =
  1080. this->GeneratorTarget->Target->GetProperty("VS_PROJECT_IMPORT");
  1081. if (imports) {
  1082. cmList argsSplit{ *imports };
  1083. for (auto& path : argsSplit) {
  1084. if (!cmsys::SystemTools::FileIsFullPath(path)) {
  1085. path = this->Makefile->GetCurrentSourceDirectory() + "/" + path;
  1086. }
  1087. ConvertToWindowsSlash(path);
  1088. Elem e1(e0, "Import");
  1089. e1.Attribute("Project", path);
  1090. }
  1091. }
  1092. }
  1093. void cmVisualStudio10TargetGenerator::WriteDotNetReferenceCustomTags(
  1094. Elem& e2, std::string const& ref)
  1095. {
  1096. static const std::string refpropPrefix = "VS_DOTNET_REFERENCEPROP_";
  1097. static const std::string refpropInfix = "_TAG_";
  1098. const std::string refPropFullPrefix = refpropPrefix + ref + refpropInfix;
  1099. using CustomTags = std::map<std::string, std::string>;
  1100. CustomTags tags;
  1101. cmPropertyMap const& props = this->GeneratorTarget->Target->GetProperties();
  1102. for (const auto& i : props.GetList()) {
  1103. if (cmHasPrefix(i.first, refPropFullPrefix) && !i.second.empty()) {
  1104. tags[i.first.substr(refPropFullPrefix.length())] = i.second;
  1105. }
  1106. }
  1107. for (auto const& tag : tags) {
  1108. e2.Element(tag.first, tag.second);
  1109. }
  1110. }
  1111. void cmVisualStudio10TargetGenerator::WriteDotNetDocumentationFile(Elem& e0)
  1112. {
  1113. std::string const& documentationFile =
  1114. this->GeneratorTarget->GetSafeProperty("VS_DOTNET_DOCUMENTATION_FILE");
  1115. if (this->ProjectType == VsProjectType::csproj &&
  1116. !documentationFile.empty()) {
  1117. Elem e1(e0, "PropertyGroup");
  1118. Elem e2(e1, "DocumentationFile");
  1119. e2.Content(documentationFile);
  1120. }
  1121. }
  1122. void cmVisualStudio10TargetGenerator::WriteEmbeddedResourceGroup(Elem& e0)
  1123. {
  1124. if (!this->ResxObjs.empty()) {
  1125. Elem e1(e0, "ItemGroup");
  1126. std::string srcDir = this->Makefile->GetCurrentSourceDirectory();
  1127. ConvertToWindowsSlash(srcDir);
  1128. for (cmSourceFile const* oi : this->ResxObjs) {
  1129. std::string obj = oi->GetFullPath();
  1130. ConvertToWindowsSlash(obj);
  1131. bool useRelativePath = false;
  1132. if (this->ProjectType == VsProjectType::csproj && this->InSourceBuild) {
  1133. // If we do an in-source build and the resource file is in a
  1134. // subdirectory
  1135. // of the .csproj file, we have to use relative pathnames, otherwise
  1136. // visual studio does not show the file in the IDE. Sorry.
  1137. if (cmHasPrefix(obj, srcDir)) {
  1138. obj = this->ConvertPath(obj, true);
  1139. ConvertToWindowsSlash(obj);
  1140. useRelativePath = true;
  1141. }
  1142. }
  1143. Elem e2(e1, "EmbeddedResource");
  1144. e2.Attribute("Include", obj);
  1145. if (this->ProjectType != VsProjectType::csproj) {
  1146. std::string hFileName = obj.substr(0, obj.find_last_of('.')) + ".h";
  1147. e2.Element("DependentUpon", hFileName);
  1148. for (std::string const& c : this->Configurations) {
  1149. std::string s;
  1150. if (this->GeneratorTarget->GetProperty("VS_GLOBAL_ROOTNAMESPACE") ||
  1151. // Handle variant of VS_GLOBAL_<variable> for RootNamespace.
  1152. this->GeneratorTarget->GetProperty("VS_GLOBAL_RootNamespace")) {
  1153. s = "$(RootNamespace).";
  1154. }
  1155. s += "%(Filename).resources";
  1156. e2.WritePlatformConfigTag("LogicalName", this->CalcCondition(c), s);
  1157. }
  1158. } else {
  1159. std::string binDir = this->Makefile->GetCurrentBinaryDirectory();
  1160. ConvertToWindowsSlash(binDir);
  1161. // If the resource was NOT added using a relative path (which should
  1162. // be the default), we have to provide a link here
  1163. if (!useRelativePath) {
  1164. std::string link = this->GetCSharpSourceLink(oi);
  1165. if (link.empty()) {
  1166. link = cmsys::SystemTools::GetFilenameName(obj);
  1167. }
  1168. e2.Element("Link", link);
  1169. }
  1170. // Determine if this is a generated resource from a .Designer.cs file
  1171. std::string designerResource =
  1172. cmSystemTools::GetFilenamePath(oi->GetFullPath()) + "/" +
  1173. cmSystemTools::GetFilenameWithoutLastExtension(oi->GetFullPath()) +
  1174. ".Designer.cs";
  1175. if (cmsys::SystemTools::FileExists(designerResource)) {
  1176. std::string generator = "PublicResXFileCodeGenerator";
  1177. if (cmValue g = oi->GetProperty("VS_RESOURCE_GENERATOR")) {
  1178. generator = *g;
  1179. }
  1180. if (!generator.empty()) {
  1181. e2.Element("Generator", generator);
  1182. if (cmHasPrefix(designerResource, srcDir)) {
  1183. designerResource.erase(0, srcDir.length());
  1184. } else if (cmHasPrefix(designerResource, binDir)) {
  1185. designerResource.erase(0, binDir.length());
  1186. } else {
  1187. designerResource =
  1188. cmsys::SystemTools::GetFilenameName(designerResource);
  1189. }
  1190. ConvertToWindowsSlash(designerResource);
  1191. e2.Element("LastGenOutput", designerResource);
  1192. }
  1193. }
  1194. const cmPropertyMap& props = oi->GetProperties();
  1195. for (const std::string& p : props.GetKeys()) {
  1196. static const cm::string_view propNamePrefix = "VS_CSHARP_";
  1197. if (cmHasPrefix(p, propNamePrefix)) {
  1198. cm::string_view tagName =
  1199. cm::string_view(p).substr(propNamePrefix.length());
  1200. if (!tagName.empty()) {
  1201. cmValue value = props.GetPropertyValue(p);
  1202. if (cmNonempty(value)) {
  1203. e2.Element(tagName, *value);
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. }
  1212. void cmVisualStudio10TargetGenerator::WriteXamlFilesGroup(Elem& e0)
  1213. {
  1214. if (!this->XamlObjs.empty()) {
  1215. Elem e1(e0, "ItemGroup");
  1216. for (cmSourceFile const* oi : this->XamlObjs) {
  1217. std::string obj = oi->GetFullPath();
  1218. std::string xamlType;
  1219. cmValue xamlTypeProperty = oi->GetProperty("VS_XAML_TYPE");
  1220. if (xamlTypeProperty) {
  1221. xamlType = *xamlTypeProperty;
  1222. } else {
  1223. xamlType = "Page";
  1224. }
  1225. Elem e2(e1, xamlType);
  1226. this->WriteSource(e2, oi);
  1227. e2.SetHasElements();
  1228. e2.Element("SubType", "Designer");
  1229. }
  1230. }
  1231. }
  1232. void cmVisualStudio10TargetGenerator::WriteTargetSpecificReferences(Elem& e0)
  1233. {
  1234. if (this->MSTools) {
  1235. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  1236. this->GlobalGenerator->GetSystemVersion() == "8.0") {
  1237. Elem(e0, "Import")
  1238. .Attribute("Project",
  1239. "$(MSBuildExtensionsPath)\\Microsoft\\WindowsPhone\\v"
  1240. "$(TargetPlatformVersion)\\Microsoft.Cpp.WindowsPhone."
  1241. "$(TargetPlatformVersion).targets");
  1242. }
  1243. }
  1244. }
  1245. void cmVisualStudio10TargetGenerator::WriteTargetsFileReferences(Elem& e1)
  1246. {
  1247. for (TargetsFileAndConfigs const& tac : this->TargetsFileAndConfigsVec) {
  1248. std::ostringstream oss;
  1249. oss << "Exists('" << tac.File << "')";
  1250. if (!tac.Configs.empty()) {
  1251. oss << " And (";
  1252. for (size_t j = 0; j < tac.Configs.size(); ++j) {
  1253. if (j > 0) {
  1254. oss << " Or ";
  1255. }
  1256. oss << "'$(Configuration)'=='" << tac.Configs[j] << "'";
  1257. }
  1258. oss << ")";
  1259. }
  1260. Elem(e1, "Import")
  1261. .Attribute("Project", tac.File)
  1262. .Attribute("Condition", oss.str());
  1263. }
  1264. }
  1265. void cmVisualStudio10TargetGenerator::WriteWinRTReferences(Elem& e0)
  1266. {
  1267. std::vector<std::string> references;
  1268. if (cmValue vsWinRTReferences =
  1269. this->GeneratorTarget->GetProperty("VS_WINRT_REFERENCES")) {
  1270. cmExpandList(*vsWinRTReferences, references);
  1271. }
  1272. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  1273. this->GlobalGenerator->GetSystemVersion() == "8.0" &&
  1274. references.empty()) {
  1275. references.push_back("platform.winmd");
  1276. }
  1277. if (!references.empty()) {
  1278. Elem e1(e0, "ItemGroup");
  1279. for (std::string const& ri : references) {
  1280. Elem e2(e1, "Reference");
  1281. e2.Attribute("Include", ri);
  1282. e2.Element("IsWinMDFile", "true");
  1283. }
  1284. }
  1285. }
  1286. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  1287. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations(Elem& e0)
  1288. {
  1289. Elem e1(e0, "ItemGroup");
  1290. e1.Attribute("Label", "ProjectConfigurations");
  1291. for (std::string const& c : this->Configurations) {
  1292. Elem e2(e1, "ProjectConfiguration");
  1293. e2.Attribute("Include", c + "|" + this->Platform);
  1294. e2.Element("Configuration", c);
  1295. e2.Element("Platform", this->Platform);
  1296. }
  1297. }
  1298. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues(Elem& e0)
  1299. {
  1300. for (std::string const& c : this->Configurations) {
  1301. Elem e1(e0, "PropertyGroup");
  1302. e1.Attribute("Condition", this->CalcCondition(c));
  1303. e1.Attribute("Label", "Configuration");
  1304. if (this->ProjectType != VsProjectType::csproj) {
  1305. std::string configType;
  1306. if (cmValue vsConfigurationType =
  1307. this->GeneratorTarget->GetProperty("VS_CONFIGURATION_TYPE")) {
  1308. configType = cmGeneratorExpression::Evaluate(*vsConfigurationType,
  1309. this->LocalGenerator, c);
  1310. } else {
  1311. switch (this->GeneratorTarget->GetType()) {
  1312. case cmStateEnums::SHARED_LIBRARY:
  1313. case cmStateEnums::MODULE_LIBRARY:
  1314. configType = "DynamicLibrary";
  1315. break;
  1316. case cmStateEnums::OBJECT_LIBRARY:
  1317. case cmStateEnums::STATIC_LIBRARY:
  1318. configType = "StaticLibrary";
  1319. break;
  1320. case cmStateEnums::EXECUTABLE:
  1321. if (this->NsightTegra &&
  1322. !this->GeneratorTarget->Target->IsAndroidGuiExecutable()) {
  1323. // Android executables are .so too.
  1324. configType = "DynamicLibrary";
  1325. } else if (this->Android) {
  1326. configType = "DynamicLibrary";
  1327. } else {
  1328. configType = "Application";
  1329. }
  1330. break;
  1331. case cmStateEnums::UTILITY:
  1332. case cmStateEnums::INTERFACE_LIBRARY:
  1333. case cmStateEnums::GLOBAL_TARGET:
  1334. if (this->NsightTegra) {
  1335. // Tegra-Android platform does not understand "Utility".
  1336. configType = "StaticLibrary";
  1337. } else {
  1338. configType = "Utility";
  1339. }
  1340. break;
  1341. case cmStateEnums::UNKNOWN_LIBRARY:
  1342. break;
  1343. }
  1344. }
  1345. e1.Element("ConfigurationType", configType);
  1346. }
  1347. if (this->MSTools) {
  1348. if (!this->Managed) {
  1349. this->WriteMSToolConfigurationValues(e1, c);
  1350. } else {
  1351. this->WriteMSToolConfigurationValuesManaged(e1, c);
  1352. }
  1353. } else if (this->NsightTegra) {
  1354. this->WriteNsightTegraConfigurationValues(e1, c);
  1355. } else if (this->Android) {
  1356. this->WriteAndroidConfigurationValues(e1, c);
  1357. }
  1358. }
  1359. }
  1360. void cmVisualStudio10TargetGenerator::WriteCEDebugProjectConfigurationValues(
  1361. Elem& e0)
  1362. {
  1363. if (!this->GlobalGenerator->TargetsWindowsCE()) {
  1364. return;
  1365. }
  1366. cmValue additionalFiles =
  1367. this->GeneratorTarget->GetProperty("DEPLOYMENT_ADDITIONAL_FILES");
  1368. cmValue remoteDirectory =
  1369. this->GeneratorTarget->GetProperty("DEPLOYMENT_REMOTE_DIRECTORY");
  1370. if (!(additionalFiles || remoteDirectory)) {
  1371. return;
  1372. }
  1373. for (std::string const& c : this->Configurations) {
  1374. Elem e1(e0, "PropertyGroup");
  1375. e1.Attribute("Condition", this->CalcCondition(c));
  1376. if (remoteDirectory) {
  1377. e1.Element("RemoteDirectory", *remoteDirectory);
  1378. }
  1379. if (additionalFiles) {
  1380. e1.Element("CEAdditionalFiles", *additionalFiles);
  1381. }
  1382. }
  1383. }
  1384. void cmVisualStudio10TargetGenerator::WriteMSToolConfigurationValues(
  1385. Elem& e1, std::string const& config)
  1386. {
  1387. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  1388. cmValue mfcFlag = this->Makefile->GetDefinition("CMAKE_MFC_FLAG");
  1389. if (mfcFlag) {
  1390. std::string const mfcFlagValue =
  1391. cmGeneratorExpression::Evaluate(*mfcFlag, this->LocalGenerator, config);
  1392. std::string useOfMfcValue = "false";
  1393. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  1394. if (mfcFlagValue == "1") {
  1395. useOfMfcValue = "Static";
  1396. } else if (mfcFlagValue == "2") {
  1397. useOfMfcValue = "Dynamic";
  1398. }
  1399. }
  1400. e1.Element("UseOfMfc", useOfMfcValue);
  1401. }
  1402. if ((this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY &&
  1403. this->ClOptions[config]->UsingUnicode()) ||
  1404. this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") ||
  1405. this->GlobalGenerator->TargetsWindowsPhone() ||
  1406. this->GlobalGenerator->TargetsWindowsStore() ||
  1407. this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_EXTENSIONS")) {
  1408. e1.Element("CharacterSet", "Unicode");
  1409. } else if (this->GeneratorTarget->GetType() <=
  1410. cmStateEnums::OBJECT_LIBRARY &&
  1411. this->ClOptions[config]->UsingSBCS()) {
  1412. e1.Element("CharacterSet", "NotSet");
  1413. } else {
  1414. e1.Element("CharacterSet", "MultiByte");
  1415. }
  1416. if (cmValue projectToolsetOverride =
  1417. this->GeneratorTarget->GetProperty("VS_PLATFORM_TOOLSET")) {
  1418. e1.Element("PlatformToolset", *projectToolsetOverride);
  1419. } else if (const char* toolset = gg->GetPlatformToolset()) {
  1420. e1.Element("PlatformToolset", toolset);
  1421. }
  1422. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") ||
  1423. this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_EXTENSIONS")) {
  1424. e1.Element("WindowsAppContainer", "true");
  1425. }
  1426. if (this->IPOEnabledConfigurations.count(config) > 0) {
  1427. e1.Element("WholeProgramOptimization", "true");
  1428. }
  1429. if (this->ASanEnabledConfigurations.find(config) !=
  1430. this->ASanEnabledConfigurations.end()) {
  1431. e1.Element("EnableAsan", "true");
  1432. }
  1433. if (this->FuzzerEnabledConfigurations.find(config) !=
  1434. this->FuzzerEnabledConfigurations.end()) {
  1435. e1.Element("EnableFuzzer", "true");
  1436. }
  1437. {
  1438. auto s = this->SpectreMitigation.find(config);
  1439. if (s != this->SpectreMitigation.end()) {
  1440. e1.Element("SpectreMitigation", s->second);
  1441. }
  1442. }
  1443. }
  1444. void cmVisualStudio10TargetGenerator::WriteMSToolConfigurationValuesManaged(
  1445. Elem& e1, std::string const& config)
  1446. {
  1447. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  1448. return;
  1449. }
  1450. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  1451. Options& o = *(this->ClOptions[config]);
  1452. if (o.IsDebug()) {
  1453. e1.Element("DebugSymbols", "true");
  1454. e1.Element("DefineDebug", "true");
  1455. }
  1456. std::string outDir = this->GeneratorTarget->GetDirectory(config) + "/";
  1457. ConvertToWindowsSlash(outDir);
  1458. e1.Element("OutputPath", outDir);
  1459. if (o.HasFlag("Platform")) {
  1460. e1.Element("PlatformTarget", o.GetFlag("Platform"));
  1461. o.RemoveFlag("Platform");
  1462. }
  1463. if (cmValue projectToolsetOverride =
  1464. this->GeneratorTarget->GetProperty("VS_PLATFORM_TOOLSET")) {
  1465. e1.Element("PlatformToolset", *projectToolsetOverride);
  1466. } else if (const char* toolset = gg->GetPlatformToolset()) {
  1467. e1.Element("PlatformToolset", toolset);
  1468. }
  1469. std::string postfixName =
  1470. cmStrCat(cmSystemTools::UpperCase(config), "_POSTFIX");
  1471. std::string assemblyName = this->GeneratorTarget->GetOutputName(
  1472. config, cmStateEnums::RuntimeBinaryArtifact);
  1473. if (cmValue postfix = this->GeneratorTarget->GetProperty(postfixName)) {
  1474. assemblyName += *postfix;
  1475. }
  1476. e1.Element("AssemblyName", assemblyName);
  1477. if (cmStateEnums::EXECUTABLE == this->GeneratorTarget->GetType()) {
  1478. e1.Element("StartAction", "Program");
  1479. e1.Element("StartProgram", outDir + assemblyName + ".exe");
  1480. }
  1481. OptionsHelper oh(o, e1);
  1482. oh.OutputFlagMap();
  1483. }
  1484. //----------------------------------------------------------------------------
  1485. void cmVisualStudio10TargetGenerator::WriteNsightTegraConfigurationValues(
  1486. Elem& e1, std::string const&)
  1487. {
  1488. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  1489. const char* toolset = gg->GetPlatformToolset();
  1490. e1.Element("NdkToolchainVersion", toolset ? toolset : "Default");
  1491. if (cmValue minApi = this->GeneratorTarget->GetProperty("ANDROID_API_MIN")) {
  1492. e1.Element("AndroidMinAPI", "android-" + *minApi);
  1493. }
  1494. if (cmValue api = this->GeneratorTarget->GetProperty("ANDROID_API")) {
  1495. e1.Element("AndroidTargetAPI", "android-" + *api);
  1496. }
  1497. if (cmValue cpuArch = this->GeneratorTarget->GetProperty("ANDROID_ARCH")) {
  1498. e1.Element("AndroidArch", *cpuArch);
  1499. }
  1500. if (cmValue stlType =
  1501. this->GeneratorTarget->GetProperty("ANDROID_STL_TYPE")) {
  1502. e1.Element("AndroidStlType", *stlType);
  1503. }
  1504. }
  1505. void cmVisualStudio10TargetGenerator::WriteAndroidConfigurationValues(
  1506. Elem& e1, std::string const&)
  1507. {
  1508. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  1509. if (cmValue projectToolsetOverride =
  1510. this->GeneratorTarget->GetProperty("VS_PLATFORM_TOOLSET")) {
  1511. e1.Element("PlatformToolset", *projectToolsetOverride);
  1512. } else if (const char* toolset = gg->GetPlatformToolset()) {
  1513. e1.Element("PlatformToolset", toolset);
  1514. }
  1515. if (cmValue stlType =
  1516. this->GeneratorTarget->GetProperty("ANDROID_STL_TYPE")) {
  1517. if (*stlType != "none") {
  1518. e1.Element("UseOfStl", *stlType);
  1519. }
  1520. }
  1521. std::string const& apiLevel = gg->GetSystemVersion();
  1522. if (!apiLevel.empty()) {
  1523. e1.Element("AndroidAPILevel", cmStrCat("android-", apiLevel));
  1524. }
  1525. }
  1526. void cmVisualStudio10TargetGenerator::WriteCustomCommands(Elem& e0)
  1527. {
  1528. this->CSharpCustomCommandNames.clear();
  1529. cmSourceFile const* srcCMakeLists =
  1530. this->LocalGenerator->CreateVCProjBuildRule();
  1531. for (cmGeneratorTarget::AllConfigSource const& si :
  1532. this->GeneratorTarget->GetAllConfigSources()) {
  1533. if (si.Source == srcCMakeLists) {
  1534. // Skip explicit reference to CMakeLists.txt source.
  1535. continue;
  1536. }
  1537. this->WriteCustomCommand(e0, si.Source);
  1538. }
  1539. // Add CMakeLists.txt file with rule to re-run CMake for user convenience.
  1540. if (this->GeneratorTarget->GetType() != cmStateEnums::GLOBAL_TARGET &&
  1541. this->GeneratorTarget->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  1542. if (srcCMakeLists) {
  1543. // Write directly rather than through WriteCustomCommand because
  1544. // we do not want the de-duplication and it has no dependencies.
  1545. if (cmCustomCommand const* command = srcCMakeLists->GetCustomCommand()) {
  1546. this->WriteCustomRule(e0, srcCMakeLists, *command);
  1547. }
  1548. }
  1549. }
  1550. }
  1551. void cmVisualStudio10TargetGenerator::WriteCustomCommand(
  1552. Elem& e0, cmSourceFile const* sf)
  1553. {
  1554. if (this->LocalGenerator->GetSourcesVisited(this->GeneratorTarget)
  1555. .insert(sf)
  1556. .second) {
  1557. if (std::vector<cmSourceFile*> const* depends =
  1558. this->GeneratorTarget->GetSourceDepends(sf)) {
  1559. for (cmSourceFile const* di : *depends) {
  1560. this->WriteCustomCommand(e0, di);
  1561. }
  1562. }
  1563. if (cmCustomCommand const* command = sf->GetCustomCommand()) {
  1564. // C# projects write their <Target> within WriteCustomRule()
  1565. this->WriteCustomRule(e0, sf, *command);
  1566. }
  1567. }
  1568. }
  1569. void cmVisualStudio10TargetGenerator::WriteCustomRule(
  1570. Elem& e0, cmSourceFile const* source, cmCustomCommand const& command)
  1571. {
  1572. std::string sourcePath = source->GetFullPath();
  1573. // VS 10 will always rebuild a custom command attached to a .rule
  1574. // file that doesn't exist so create the file explicitly.
  1575. if (source->GetPropertyAsBool("__CMAKE_RULE")) {
  1576. if (!cmSystemTools::FileExists(sourcePath)) {
  1577. // Make sure the path exists for the file
  1578. std::string path = cmSystemTools::GetFilenamePath(sourcePath);
  1579. cmSystemTools::MakeDirectory(path);
  1580. cmsys::ofstream fout(sourcePath.c_str());
  1581. if (fout) {
  1582. fout << "# generated from CMake\n";
  1583. fout.flush();
  1584. fout.close();
  1585. // Force given file to have a very old timestamp, thus
  1586. // preventing dependent rebuilds.
  1587. this->ForceOld(sourcePath);
  1588. } else {
  1589. std::string error =
  1590. cmStrCat("Could not create file: [", sourcePath, "] ");
  1591. cmSystemTools::Error(error + cmSystemTools::GetLastSystemError());
  1592. }
  1593. }
  1594. }
  1595. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1596. std::unique_ptr<Elem> spe1;
  1597. std::unique_ptr<Elem> spe2;
  1598. if (this->ProjectType != VsProjectType::csproj) {
  1599. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  1600. spe2 = cm::make_unique<Elem>(*spe1, "CustomBuild");
  1601. this->WriteSource(*spe2, source);
  1602. spe2->SetHasElements();
  1603. if (command.GetStdPipesUTF8()) {
  1604. this->WriteStdOutEncodingUtf8(*spe2);
  1605. }
  1606. } else {
  1607. Elem e1(e0, "ItemGroup");
  1608. Elem e2(e1, "None");
  1609. this->WriteSource(e2, source);
  1610. e2.SetHasElements();
  1611. }
  1612. for (std::string const& c : this->Configurations) {
  1613. cmCustomCommandGenerator ccg(command, c, lg, true);
  1614. std::string comment = lg->ConstructComment(ccg);
  1615. comment = cmVS10EscapeComment(comment);
  1616. std::string script = lg->ConstructScript(ccg);
  1617. bool symbolic = false;
  1618. // input files for custom command
  1619. std::stringstream additional_inputs;
  1620. {
  1621. const char* sep = "";
  1622. if (this->ProjectType == VsProjectType::csproj) {
  1623. // csproj files do not attach the command to a specific file
  1624. // so the primary input must be listed explicitly.
  1625. additional_inputs << source->GetFullPath();
  1626. sep = ";";
  1627. }
  1628. // Avoid listing an input more than once.
  1629. std::set<std::string> unique_inputs;
  1630. // The source is either implicit an input or has been added above.
  1631. unique_inputs.insert(source->GetFullPath());
  1632. for (std::string const& d : ccg.GetDepends()) {
  1633. std::string dep;
  1634. if (lg->GetRealDependency(d, c, dep)) {
  1635. if (!unique_inputs.insert(dep).second) {
  1636. // already listed
  1637. continue;
  1638. }
  1639. ConvertToWindowsSlash(dep);
  1640. additional_inputs << sep << dep;
  1641. sep = ";";
  1642. if (!symbolic) {
  1643. if (cmSourceFile* sf = this->Makefile->GetSource(
  1644. dep, cmSourceFileLocationKind::Known)) {
  1645. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1646. }
  1647. }
  1648. }
  1649. }
  1650. if (this->ProjectType != VsProjectType::csproj) {
  1651. additional_inputs << sep << "%(AdditionalInputs)";
  1652. }
  1653. }
  1654. // output files for custom command
  1655. std::stringstream outputs;
  1656. {
  1657. const char* sep = "";
  1658. for (std::string const& o : ccg.GetOutputs()) {
  1659. std::string out = o;
  1660. ConvertToWindowsSlash(out);
  1661. outputs << sep << out;
  1662. sep = ";";
  1663. if (!symbolic) {
  1664. if (cmSourceFile* sf = this->Makefile->GetSource(
  1665. o, cmSourceFileLocationKind::Known)) {
  1666. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1667. }
  1668. }
  1669. }
  1670. }
  1671. script += lg->FinishConstructScript(this->ProjectType);
  1672. if (this->ProjectType == VsProjectType::csproj) {
  1673. std::string name = "CustomCommand_" + c + "_" +
  1674. cmSystemTools::ComputeStringMD5(sourcePath);
  1675. this->WriteCustomRuleCSharp(e0, c, name, script, additional_inputs.str(),
  1676. outputs.str(), comment, ccg);
  1677. } else {
  1678. BuildInParallel buildInParallel = BuildInParallel::No;
  1679. if (command.GetCMP0147Status() == cmPolicies::NEW &&
  1680. !command.GetUsesTerminal() &&
  1681. !(command.HasMainDependency() && source->GetIsGenerated())) {
  1682. buildInParallel = BuildInParallel::Yes;
  1683. }
  1684. this->WriteCustomRuleCpp(*spe2, c, script, additional_inputs.str(),
  1685. outputs.str(), comment, ccg, symbolic,
  1686. buildInParallel);
  1687. }
  1688. }
  1689. }
  1690. void cmVisualStudio10TargetGenerator::WriteCustomRuleCpp(
  1691. Elem& e2, std::string const& config, std::string const& script,
  1692. std::string const& additional_inputs, std::string const& outputs,
  1693. std::string const& comment, cmCustomCommandGenerator const& ccg,
  1694. bool symbolic, BuildInParallel buildInParallel)
  1695. {
  1696. const std::string cond = this->CalcCondition(config);
  1697. if (buildInParallel == BuildInParallel::Yes &&
  1698. this->GlobalGenerator->IsBuildInParallelSupported()) {
  1699. e2.WritePlatformConfigTag("BuildInParallel", cond, "true");
  1700. }
  1701. e2.WritePlatformConfigTag("Message", cond, comment);
  1702. e2.WritePlatformConfigTag("Command", cond, script);
  1703. e2.WritePlatformConfigTag("AdditionalInputs", cond, additional_inputs);
  1704. e2.WritePlatformConfigTag("Outputs", cond, outputs);
  1705. // Turn off linking of custom command outputs.
  1706. e2.WritePlatformConfigTag("LinkObjects", cond, "false");
  1707. if (symbolic &&
  1708. this->LocalGenerator->GetVersion() >=
  1709. cmGlobalVisualStudioGenerator::VSVersion::VS16) {
  1710. // VS >= 16.4 warn if outputs are not created, but one of our
  1711. // outputs is marked SYMBOLIC and not expected to be created.
  1712. e2.WritePlatformConfigTag("VerifyInputsAndOutputsExist", cond, "false");
  1713. }
  1714. std::string depfile = ccg.GetFullDepfile();
  1715. if (!depfile.empty()) {
  1716. this->HaveCustomCommandDepfile = true;
  1717. std::string internal_depfile = ccg.GetInternalDepfile();
  1718. ConvertToWindowsSlash(internal_depfile);
  1719. e2.WritePlatformConfigTag("DepFileAdditionalInputsFile", cond,
  1720. internal_depfile);
  1721. }
  1722. }
  1723. void cmVisualStudio10TargetGenerator::WriteCustomRuleCSharp(
  1724. Elem& e0, std::string const& config, std::string const& name,
  1725. std::string const& script, std::string const& inputs,
  1726. std::string const& outputs, std::string const& comment,
  1727. cmCustomCommandGenerator const& ccg)
  1728. {
  1729. if (!ccg.GetFullDepfile().empty()) {
  1730. this->Makefile->IssueMessage(
  1731. MessageType::FATAL_ERROR,
  1732. cmStrCat("CSharp target \"", this->GeneratorTarget->GetName(),
  1733. "\" does not support add_custom_command DEPFILE."));
  1734. }
  1735. this->CSharpCustomCommandNames.insert(name);
  1736. Elem e1(e0, "Target");
  1737. e1.Attribute("Condition", this->CalcCondition(config));
  1738. e1.S << "\n Name=\"" << name << "\"";
  1739. e1.S << "\n Inputs=\"" << cmVS10EscapeAttr(inputs) << "\"";
  1740. e1.S << "\n Outputs=\"" << cmVS10EscapeAttr(outputs) << "\"";
  1741. if (!comment.empty()) {
  1742. Elem(e1, "Exec").Attribute("Command", "echo " + comment);
  1743. }
  1744. Elem(e1, "Exec").Attribute("Command", script);
  1745. }
  1746. std::string cmVisualStudio10TargetGenerator::ConvertPath(
  1747. std::string const& path, bool forceRelative)
  1748. {
  1749. return forceRelative
  1750. ? cmSystemTools::RelativePath(
  1751. this->LocalGenerator->GetCurrentBinaryDirectory(), path)
  1752. : path;
  1753. }
  1754. static void ConvertToWindowsSlash(std::string& s)
  1755. {
  1756. // first convert all of the slashes
  1757. for (auto& ch : s) {
  1758. if (ch == '/') {
  1759. ch = '\\';
  1760. }
  1761. }
  1762. }
  1763. void cmVisualStudio10TargetGenerator::WriteGroups()
  1764. {
  1765. if (this->ProjectType == VsProjectType::csproj) {
  1766. return;
  1767. }
  1768. // collect up group information
  1769. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  1770. std::vector<cmGeneratorTarget::AllConfigSource> const& sources =
  1771. this->GeneratorTarget->GetAllConfigSources();
  1772. std::set<cmSourceGroup const*> groupsUsed;
  1773. for (cmGeneratorTarget::AllConfigSource const& si : sources) {
  1774. std::string const& source = si.Source->GetFullPath();
  1775. cmSourceGroup* sourceGroup =
  1776. this->Makefile->FindSourceGroup(source, sourceGroups);
  1777. groupsUsed.insert(sourceGroup);
  1778. }
  1779. if (cmSourceFile const* srcCMakeLists =
  1780. this->LocalGenerator->CreateVCProjBuildRule()) {
  1781. std::string const& source = srcCMakeLists->GetFullPath();
  1782. cmSourceGroup* sourceGroup =
  1783. this->Makefile->FindSourceGroup(source, sourceGroups);
  1784. groupsUsed.insert(sourceGroup);
  1785. }
  1786. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  1787. // Write out group file
  1788. std::string path = cmStrCat(
  1789. this->LocalGenerator->GetCurrentBinaryDirectory(), '/', this->Name,
  1790. computeProjectFileExtension(this->GeneratorTarget), ".filters");
  1791. cmGeneratedFileStream fout(path);
  1792. fout.SetCopyIfDifferent(true);
  1793. char magic[] = { char(0xEF), char(0xBB), char(0xBF) };
  1794. fout.write(magic, 3);
  1795. fout << R"(<?xml version="1.0" encoding=")"
  1796. << this->GlobalGenerator->Encoding() << "\"?>";
  1797. {
  1798. Elem e0(fout, "Project");
  1799. e0.Attribute("ToolsVersion", this->GlobalGenerator->GetToolsVersion());
  1800. e0.Attribute("xmlns",
  1801. "http://schemas.microsoft.com/developer/msbuild/2003");
  1802. for (auto const& ti : this->Tools) {
  1803. if ((this->GeneratorTarget->GetName() ==
  1804. CMAKE_CHECK_BUILD_SYSTEM_TARGET) &&
  1805. (ti.first == "None")) {
  1806. this->WriteBuildSystemSources(e0, ti.first, ti.second);
  1807. } else {
  1808. this->WriteGroupSources(e0, ti.first, ti.second, sourceGroups);
  1809. }
  1810. }
  1811. // Added files are images and the manifest.
  1812. if (!this->AddedFiles.empty()) {
  1813. Elem e1(e0, "ItemGroup");
  1814. e1.SetHasElements();
  1815. for (std::string const& oi : this->AddedFiles) {
  1816. std::string fileName =
  1817. cmSystemTools::LowerCase(cmSystemTools::GetFilenameName(oi));
  1818. if (fileName == "wmappmanifest.xml") {
  1819. Elem e2(e1, "XML");
  1820. e2.Attribute("Include", oi);
  1821. e2.Element("Filter", "Resource Files");
  1822. } else if (cmSystemTools::GetFilenameExtension(fileName) ==
  1823. ".appxmanifest") {
  1824. Elem e2(e1, "AppxManifest");
  1825. e2.Attribute("Include", oi);
  1826. e2.Element("Filter", "Resource Files");
  1827. } else if (cmSystemTools::GetFilenameExtension(fileName) == ".pfx") {
  1828. Elem e2(e1, "None");
  1829. e2.Attribute("Include", oi);
  1830. e2.Element("Filter", "Resource Files");
  1831. } else {
  1832. Elem e2(e1, "Image");
  1833. e2.Attribute("Include", oi);
  1834. e2.Element("Filter", "Resource Files");
  1835. }
  1836. }
  1837. }
  1838. if (!this->ResxObjs.empty()) {
  1839. Elem e1(e0, "ItemGroup");
  1840. for (cmSourceFile const* oi : this->ResxObjs) {
  1841. std::string obj = oi->GetFullPath();
  1842. ConvertToWindowsSlash(obj);
  1843. Elem e2(e1, "EmbeddedResource");
  1844. e2.Attribute("Include", obj);
  1845. e2.Element("Filter", "Resource Files");
  1846. }
  1847. }
  1848. {
  1849. Elem e1(e0, "ItemGroup");
  1850. e1.SetHasElements();
  1851. std::vector<cmSourceGroup const*> groupsVec(groupsUsed.begin(),
  1852. groupsUsed.end());
  1853. std::sort(groupsVec.begin(), groupsVec.end(),
  1854. [](cmSourceGroup const* l, cmSourceGroup const* r) {
  1855. return l->GetFullName() < r->GetFullName();
  1856. });
  1857. for (cmSourceGroup const* sg : groupsVec) {
  1858. std::string const& name = sg->GetFullName();
  1859. if (!name.empty()) {
  1860. std::string guidName = "SG_Filter_" + name;
  1861. std::string guid = this->GlobalGenerator->GetGUID(guidName);
  1862. Elem e2(e1, "Filter");
  1863. e2.Attribute("Include", name);
  1864. e2.Element("UniqueIdentifier", "{" + guid + "}");
  1865. }
  1866. }
  1867. if (!this->ResxObjs.empty() || !this->AddedFiles.empty()) {
  1868. std::string guidName = "SG_Filter_Resource Files";
  1869. std::string guid = this->GlobalGenerator->GetGUID(guidName);
  1870. Elem e2(e1, "Filter");
  1871. e2.Attribute("Include", "Resource Files");
  1872. e2.Element("UniqueIdentifier", "{" + guid + "}");
  1873. e2.Element("Extensions",
  1874. "rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;"
  1875. "gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms");
  1876. }
  1877. if (this->GeneratorTarget->GetName() ==
  1878. CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  1879. for (const std::string& filter : this->BuildSystemSourcesFilters) {
  1880. std::string guidName = "SG_Filter_";
  1881. guidName += filter;
  1882. std::string guid = this->GlobalGenerator->GetGUID(guidName);
  1883. Elem e2(e1, "Filter");
  1884. e2.Attribute("Include", filter);
  1885. e2.Element("UniqueIdentifier", "{" + guid + "}");
  1886. }
  1887. }
  1888. }
  1889. }
  1890. fout << '\n';
  1891. if (fout.Close()) {
  1892. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  1893. }
  1894. }
  1895. // Add to groupsUsed empty source groups that have non-empty children.
  1896. void cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  1897. std::set<cmSourceGroup const*>& groupsUsed,
  1898. const std::vector<cmSourceGroup>& allGroups)
  1899. {
  1900. for (cmSourceGroup const& current : allGroups) {
  1901. std::vector<cmSourceGroup> const& children = current.GetGroupChildren();
  1902. if (children.empty()) {
  1903. continue; // the group is really empty
  1904. }
  1905. this->AddMissingSourceGroups(groupsUsed, children);
  1906. if (groupsUsed.count(&current) > 0) {
  1907. continue; // group has already been added to set
  1908. }
  1909. // check if it least one of the group's descendants is not empty
  1910. // (at least one child must already have been added)
  1911. auto child_it = children.begin();
  1912. while (child_it != children.end()) {
  1913. if (groupsUsed.count(&(*child_it)) > 0) {
  1914. break; // found a child that was already added => add current group too
  1915. }
  1916. child_it++;
  1917. }
  1918. if (child_it == children.end()) {
  1919. continue; // no descendants have source files => ignore this group
  1920. }
  1921. groupsUsed.insert(&current);
  1922. }
  1923. }
  1924. void cmVisualStudio10TargetGenerator::WriteGroupSources(
  1925. Elem& e0, std::string const& name, ToolSources const& sources,
  1926. std::vector<cmSourceGroup>& sourceGroups)
  1927. {
  1928. Elem e1(e0, "ItemGroup");
  1929. e1.SetHasElements();
  1930. for (ToolSource const& s : sources) {
  1931. cmSourceFile const* sf = s.SourceFile;
  1932. std::string const& source = sf->GetFullPath();
  1933. cmSourceGroup* sourceGroup =
  1934. this->Makefile->FindSourceGroup(source, sourceGroups);
  1935. std::string const& filter = sourceGroup->GetFullName();
  1936. std::string path = this->ConvertPath(source, s.RelativePath);
  1937. ConvertToWindowsSlash(path);
  1938. Elem e2(e1, name);
  1939. e2.Attribute("Include", path);
  1940. if (!filter.empty()) {
  1941. e2.Element("Filter", filter);
  1942. }
  1943. }
  1944. }
  1945. void cmVisualStudio10TargetGenerator::WriteBuildSystemSources(
  1946. Elem& e0, std::string const& name, ToolSources const& sources)
  1947. {
  1948. const std::string srcDir = this->Makefile->GetCurrentSourceDirectory();
  1949. const std::string::size_type srcDirLength = srcDir.length();
  1950. Elem e1(e0, "ItemGroup");
  1951. e1.SetHasElements();
  1952. for (ToolSource const& s : sources) {
  1953. cmSourceFile const* sf = s.SourceFile;
  1954. std::string const& source = sf->GetFullPath();
  1955. cm::filesystem::path sourcePath(source);
  1956. bool isInSrcDir = cmHasPrefix(source, srcDir);
  1957. std::string filter = kBuildSystemSources;
  1958. if (isInSrcDir) {
  1959. std::string parentPath = sourcePath.parent_path().string();
  1960. if (srcDir != parentPath) {
  1961. filter += parentPath.substr(srcDirLength);
  1962. }
  1963. ConvertToWindowsSlash(filter);
  1964. this->BuildSystemSourcesFilters.insert(filter);
  1965. }
  1966. std::string path = this->ConvertPath(source, s.RelativePath);
  1967. ConvertToWindowsSlash(path);
  1968. Elem e2(e1, name);
  1969. e2.Attribute("Include", path);
  1970. e2.Element("Filter", filter);
  1971. }
  1972. }
  1973. void cmVisualStudio10TargetGenerator::WriteHeaderSource(
  1974. Elem& e1, cmSourceFile const* sf, ConfigToSettings const& toolSettings)
  1975. {
  1976. std::string const& fileName = sf->GetFullPath();
  1977. Elem e2(e1, "ClInclude");
  1978. this->WriteSource(e2, sf);
  1979. if (this->IsResxHeader(fileName)) {
  1980. e2.Element("FileType", "CppForm");
  1981. } else if (this->IsXamlHeader(fileName)) {
  1982. e2.Element("DependentUpon",
  1983. fileName.substr(0, fileName.find_last_of('.')));
  1984. }
  1985. this->FinishWritingSource(e2, toolSettings);
  1986. }
  1987. void cmVisualStudio10TargetGenerator::ParseSettingsProperty(
  1988. const std::string& settingsPropertyValue, ConfigToSettings& toolSettings)
  1989. {
  1990. if (!settingsPropertyValue.empty()) {
  1991. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  1992. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  1993. ge.Parse(settingsPropertyValue);
  1994. for (const std::string& config : this->Configurations) {
  1995. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  1996. cmList settings{ evaluated };
  1997. for (const auto& setting : settings) {
  1998. const std::string::size_type assignment = setting.find('=');
  1999. if (assignment != std::string::npos) {
  2000. const std::string propName = setting.substr(0, assignment);
  2001. const std::string propValue = setting.substr(assignment + 1);
  2002. if (!propValue.empty()) {
  2003. toolSettings[config][propName] = propValue;
  2004. }
  2005. }
  2006. }
  2007. }
  2008. }
  2009. }
  2010. bool cmVisualStudio10TargetGenerator::PropertyIsSameInAllConfigs(
  2011. const ConfigToSettings& toolSettings, const std::string& propName)
  2012. {
  2013. std::string firstPropValue;
  2014. for (const auto& configToSettings : toolSettings) {
  2015. const std::unordered_map<std::string, std::string>& settings =
  2016. configToSettings.second;
  2017. if (firstPropValue.empty()) {
  2018. if (settings.find(propName) != settings.end()) {
  2019. firstPropValue = settings.find(propName)->second;
  2020. }
  2021. }
  2022. if (settings.find(propName) == settings.end()) {
  2023. return false;
  2024. }
  2025. if (settings.find(propName)->second != firstPropValue) {
  2026. return false;
  2027. }
  2028. }
  2029. return true;
  2030. }
  2031. void cmVisualStudio10TargetGenerator::WriteExtraSource(
  2032. Elem& e1, cmSourceFile const* sf, ConfigToSettings& toolSettings)
  2033. {
  2034. bool toolHasSettings = false;
  2035. const char* tool = "None";
  2036. std::string settingsGenerator;
  2037. std::string settingsLastGenOutput;
  2038. std::string sourceLink;
  2039. std::string subType;
  2040. std::string copyToOutDir;
  2041. std::string includeInVsix;
  2042. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  2043. if (this->ProjectType == VsProjectType::csproj && !this->InSourceBuild) {
  2044. toolHasSettings = true;
  2045. }
  2046. if (ext == "hlsl") {
  2047. tool = "FXCompile";
  2048. // Figure out the type of shader compiler to use.
  2049. if (cmValue st = sf->GetProperty("VS_SHADER_TYPE")) {
  2050. for (const std::string& config : this->Configurations) {
  2051. toolSettings[config]["ShaderType"] = *st;
  2052. }
  2053. }
  2054. // Figure out which entry point to use if any
  2055. if (cmValue se = sf->GetProperty("VS_SHADER_ENTRYPOINT")) {
  2056. for (const std::string& config : this->Configurations) {
  2057. toolSettings[config]["EntryPointName"] = *se;
  2058. }
  2059. }
  2060. // Figure out which shader model to use if any
  2061. if (cmValue sm = sf->GetProperty("VS_SHADER_MODEL")) {
  2062. for (const std::string& config : this->Configurations) {
  2063. toolSettings[config]["ShaderModel"] = *sm;
  2064. }
  2065. }
  2066. // Figure out which output header file to use if any
  2067. if (cmValue ohf = sf->GetProperty("VS_SHADER_OUTPUT_HEADER_FILE")) {
  2068. for (const std::string& config : this->Configurations) {
  2069. toolSettings[config]["HeaderFileOutput"] = *ohf;
  2070. }
  2071. }
  2072. // Figure out which variable name to use if any
  2073. if (cmValue vn = sf->GetProperty("VS_SHADER_VARIABLE_NAME")) {
  2074. for (const std::string& config : this->Configurations) {
  2075. toolSettings[config]["VariableName"] = *vn;
  2076. }
  2077. }
  2078. // Figure out if there's any additional flags to use
  2079. if (cmValue saf = sf->GetProperty("VS_SHADER_FLAGS")) {
  2080. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2081. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*saf);
  2082. for (const std::string& config : this->Configurations) {
  2083. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2084. if (!evaluated.empty()) {
  2085. toolSettings[config]["AdditionalOptions"] = evaluated;
  2086. }
  2087. }
  2088. }
  2089. // Figure out if debug information should be generated
  2090. if (cmValue sed = sf->GetProperty("VS_SHADER_ENABLE_DEBUG")) {
  2091. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2092. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*sed);
  2093. for (const std::string& config : this->Configurations) {
  2094. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2095. if (!evaluated.empty()) {
  2096. toolSettings[config]["EnableDebuggingInformation"] =
  2097. cmIsOn(evaluated) ? "true" : "false";
  2098. }
  2099. }
  2100. }
  2101. // Figure out if optimizations should be disabled
  2102. if (cmValue sdo = sf->GetProperty("VS_SHADER_DISABLE_OPTIMIZATIONS")) {
  2103. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2104. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*sdo);
  2105. for (const std::string& config : this->Configurations) {
  2106. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2107. if (!evaluated.empty()) {
  2108. toolSettings[config]["DisableOptimizations"] =
  2109. cmIsOn(evaluated) ? "true" : "false";
  2110. }
  2111. }
  2112. }
  2113. if (cmValue sofn = sf->GetProperty("VS_SHADER_OBJECT_FILE_NAME")) {
  2114. for (const std::string& config : this->Configurations) {
  2115. toolSettings[config]["ObjectFileOutput"] = *sofn;
  2116. }
  2117. }
  2118. } else if (ext == "jpg" || ext == "png") {
  2119. tool = "Image";
  2120. } else if (ext == "resw") {
  2121. tool = "PRIResource";
  2122. } else if (ext == "xml") {
  2123. tool = "XML";
  2124. } else if (ext == "natvis") {
  2125. tool = "Natvis";
  2126. } else if (ext == "settings") {
  2127. settingsLastGenOutput =
  2128. cmsys::SystemTools::GetFilenameName(sf->GetFullPath());
  2129. std::size_t pos = settingsLastGenOutput.find(".settings");
  2130. settingsLastGenOutput.replace(pos, 9, ".Designer.cs");
  2131. settingsGenerator = "SettingsSingleFileGenerator";
  2132. toolHasSettings = true;
  2133. } else if (ext == "vsixmanifest") {
  2134. subType = "Designer";
  2135. }
  2136. if (cmValue c = sf->GetProperty("VS_COPY_TO_OUT_DIR")) {
  2137. tool = "Content";
  2138. copyToOutDir = *c;
  2139. toolHasSettings = true;
  2140. }
  2141. if (sf->GetPropertyAsBool("VS_INCLUDE_IN_VSIX")) {
  2142. includeInVsix = "True";
  2143. tool = "Content";
  2144. toolHasSettings = true;
  2145. }
  2146. // Collect VS_CSHARP_* property values (if some are set)
  2147. std::map<std::string, std::string> sourceFileTags;
  2148. this->GetCSharpSourceProperties(sf, sourceFileTags);
  2149. if (this->NsightTegra) {
  2150. // Nsight Tegra needs specific file types to check up-to-dateness.
  2151. std::string name = cmSystemTools::LowerCase(sf->GetLocation().GetName());
  2152. if (name == "androidmanifest.xml" || name == "build.xml" ||
  2153. name == "proguard.cfg" || name == "proguard-project.txt" ||
  2154. ext == "properties") {
  2155. tool = "AndroidBuild";
  2156. } else if (ext == "java") {
  2157. tool = "JCompile";
  2158. } else if (ext == "asm" || ext == "s") {
  2159. tool = "ClCompile";
  2160. }
  2161. }
  2162. cmValue toolOverride = sf->GetProperty("VS_TOOL_OVERRIDE");
  2163. if (cmNonempty(toolOverride)) {
  2164. tool = toolOverride->c_str();
  2165. }
  2166. std::string deployContent;
  2167. std::string deployLocation;
  2168. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  2169. this->GlobalGenerator->TargetsWindowsStore()) {
  2170. cmValue content = sf->GetProperty("VS_DEPLOYMENT_CONTENT");
  2171. if (cmNonempty(content)) {
  2172. toolHasSettings = true;
  2173. deployContent = *content;
  2174. cmValue location = sf->GetProperty("VS_DEPLOYMENT_LOCATION");
  2175. if (cmNonempty(location)) {
  2176. deployLocation = *location;
  2177. }
  2178. }
  2179. }
  2180. if (ParsedToolTargetSettings.find(tool) == ParsedToolTargetSettings.end()) {
  2181. cmValue toolTargetProperty = this->GeneratorTarget->Target->GetProperty(
  2182. "VS_SOURCE_SETTINGS_" + std::string(tool));
  2183. ConfigToSettings toolTargetSettings;
  2184. if (toolTargetProperty) {
  2185. ParseSettingsProperty(*toolTargetProperty, toolTargetSettings);
  2186. }
  2187. ParsedToolTargetSettings[tool] = toolTargetSettings;
  2188. }
  2189. for (const auto& configToSetting : ParsedToolTargetSettings[tool]) {
  2190. for (const auto& setting : configToSetting.second) {
  2191. toolSettings[configToSetting.first][setting.first] = setting.second;
  2192. }
  2193. }
  2194. if (!toolSettings.empty()) {
  2195. toolHasSettings = true;
  2196. }
  2197. Elem e2(e1, tool);
  2198. this->WriteSource(e2, sf);
  2199. if (toolHasSettings) {
  2200. e2.SetHasElements();
  2201. this->FinishWritingSource(e2, toolSettings);
  2202. if (!deployContent.empty()) {
  2203. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2204. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2205. ge.Parse(deployContent);
  2206. // Deployment location cannot be set on a configuration basis
  2207. if (!deployLocation.empty()) {
  2208. e2.Element("Link", deployLocation + "\\%(FileName)%(Extension)");
  2209. }
  2210. for (auto& config : this->Configurations) {
  2211. if (cge->Evaluate(this->LocalGenerator, config) == "1") {
  2212. e2.WritePlatformConfigTag("DeploymentContent",
  2213. "'$(Configuration)|$(Platform)'=='" +
  2214. config + "|" + this->Platform + "'",
  2215. "true");
  2216. } else {
  2217. e2.WritePlatformConfigTag("ExcludedFromBuild",
  2218. "'$(Configuration)|$(Platform)'=='" +
  2219. config + "|" + this->Platform + "'",
  2220. "true");
  2221. }
  2222. }
  2223. }
  2224. if (!settingsGenerator.empty()) {
  2225. e2.Element("Generator", settingsGenerator);
  2226. }
  2227. if (!settingsLastGenOutput.empty()) {
  2228. e2.Element("LastGenOutput", settingsLastGenOutput);
  2229. }
  2230. if (!subType.empty()) {
  2231. e2.Element("SubType", subType);
  2232. }
  2233. if (!copyToOutDir.empty()) {
  2234. e2.Element("CopyToOutputDirectory", copyToOutDir);
  2235. }
  2236. if (!includeInVsix.empty()) {
  2237. e2.Element("IncludeInVSIX", includeInVsix);
  2238. }
  2239. // write source file specific tags
  2240. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2241. }
  2242. }
  2243. void cmVisualStudio10TargetGenerator::WriteSource(Elem& e2,
  2244. cmSourceFile const* sf)
  2245. {
  2246. // Visual Studio tools append relative paths to the current dir, as in:
  2247. //
  2248. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  2249. //
  2250. // and fail if this exceeds the maximum allowed path length. Our path
  2251. // conversion uses full paths when possible to allow deeper trees.
  2252. // However, CUDA 8.0 msbuild rules fail on absolute paths so for CUDA
  2253. // we must use relative paths.
  2254. bool forceRelative = sf->GetLanguage() == "CUDA";
  2255. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), forceRelative);
  2256. ConvertToWindowsSlash(sourceFile);
  2257. e2.Attribute("Include", sourceFile);
  2258. if (this->ProjectType == VsProjectType::csproj && !this->InSourceBuild) {
  2259. // For out of source projects we have to provide a link (if not specified
  2260. // via property) for every source file (besides .cs files) otherwise they
  2261. // will not be visible in VS at all.
  2262. // First we check if the file is in a source group, then we check if the
  2263. // file path is relative to current source- or binary-dir, otherwise it is
  2264. // added with the plain filename without any path. This means the file will
  2265. // show up at root-level of the csproj (where CMakeLists.txt etc. are).
  2266. std::string link = this->GetCSharpSourceLink(sf);
  2267. if (link.empty()) {
  2268. link = cmsys::SystemTools::GetFilenameName(sf->GetFullPath());
  2269. }
  2270. e2.Element("Link", link);
  2271. }
  2272. ToolSource toolSource = { sf, forceRelative };
  2273. this->Tools[e2.Tag].push_back(toolSource);
  2274. }
  2275. void cmVisualStudio10TargetGenerator::WriteAllSources(Elem& e0)
  2276. {
  2277. if (this->GeneratorTarget->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2278. return;
  2279. }
  2280. const bool haveUnityBuild =
  2281. this->GeneratorTarget->GetPropertyAsBool("UNITY_BUILD");
  2282. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  2283. Elem e1(e0, "PropertyGroup");
  2284. e1.Element("EnableUnitySupport", "true");
  2285. }
  2286. Elem e1(e0, "ItemGroup");
  2287. e1.SetHasElements();
  2288. std::vector<size_t> all_configs;
  2289. for (size_t ci = 0; ci < this->Configurations.size(); ++ci) {
  2290. all_configs.push_back(ci);
  2291. }
  2292. std::vector<cmGeneratorTarget::AllConfigSource> const& sources =
  2293. this->GeneratorTarget->GetAllConfigSources();
  2294. cmSourceFile const* srcCMakeLists =
  2295. this->LocalGenerator->CreateVCProjBuildRule();
  2296. for (cmGeneratorTarget::AllConfigSource const& si : sources) {
  2297. if (si.Source == srcCMakeLists) {
  2298. // Skip explicit reference to CMakeLists.txt source.
  2299. continue;
  2300. }
  2301. ConfigToSettings toolSettings;
  2302. for (const auto& config : this->Configurations) {
  2303. toolSettings[config];
  2304. }
  2305. if (cmValue p = si.Source->GetProperty("VS_SETTINGS")) {
  2306. ParseSettingsProperty(*p, toolSettings);
  2307. }
  2308. const char* tool = nullptr;
  2309. switch (si.Kind) {
  2310. case cmGeneratorTarget::SourceKindAppManifest:
  2311. tool = "AppxManifest";
  2312. break;
  2313. case cmGeneratorTarget::SourceKindCertificate:
  2314. tool = "None";
  2315. break;
  2316. case cmGeneratorTarget::SourceKindCustomCommand:
  2317. // Handled elsewhere.
  2318. break;
  2319. case cmGeneratorTarget::SourceKindExternalObject:
  2320. tool = "Object";
  2321. if (this->LocalGenerator->GetVersion() <
  2322. cmGlobalVisualStudioGenerator::VSVersion::VS11) {
  2323. // For VS == 10 we cannot use LinkObjects to avoid linking custom
  2324. // command outputs. If an object file is generated in this target,
  2325. // then vs10 will use it in the build, and we have to list it as
  2326. // None instead of Object.
  2327. std::vector<cmSourceFile*> const* d =
  2328. this->GeneratorTarget->GetSourceDepends(si.Source);
  2329. if (d && !d->empty()) {
  2330. tool = "None";
  2331. }
  2332. }
  2333. break;
  2334. case cmGeneratorTarget::SourceKindExtra:
  2335. this->WriteExtraSource(e1, si.Source, toolSettings);
  2336. break;
  2337. case cmGeneratorTarget::SourceKindHeader:
  2338. this->WriteHeaderSource(e1, si.Source, toolSettings);
  2339. break;
  2340. case cmGeneratorTarget::SourceKindIDL:
  2341. tool = "Midl";
  2342. break;
  2343. case cmGeneratorTarget::SourceKindManifest:
  2344. // Handled elsewhere.
  2345. break;
  2346. case cmGeneratorTarget::SourceKindModuleDefinition:
  2347. tool = "None";
  2348. break;
  2349. case cmGeneratorTarget::SourceKindUnityBatched:
  2350. case cmGeneratorTarget::SourceKindObjectSource: {
  2351. const std::string& lang = si.Source->GetLanguage();
  2352. if (lang == "C" || lang == "CXX") {
  2353. tool = "ClCompile";
  2354. } else if (lang == "ASM_MARMASM" &&
  2355. this->GlobalGenerator->IsMarmasmEnabled()) {
  2356. tool = "MARMASM";
  2357. } else if (lang == "ASM_MASM" &&
  2358. this->GlobalGenerator->IsMasmEnabled()) {
  2359. tool = "MASM";
  2360. } else if (lang == "ASM_NASM" &&
  2361. this->GlobalGenerator->IsNasmEnabled()) {
  2362. tool = "NASM";
  2363. } else if (lang == "RC") {
  2364. tool = "ResourceCompile";
  2365. } else if (lang == "CSharp") {
  2366. tool = "Compile";
  2367. } else if (lang == "CUDA" && this->GlobalGenerator->IsCudaEnabled()) {
  2368. tool = "CudaCompile";
  2369. } else {
  2370. tool = "None";
  2371. }
  2372. } break;
  2373. case cmGeneratorTarget::SourceKindResx:
  2374. this->ResxObjs.push_back(si.Source);
  2375. break;
  2376. case cmGeneratorTarget::SourceKindXaml:
  2377. this->XamlObjs.push_back(si.Source);
  2378. break;
  2379. }
  2380. if (tool) {
  2381. // Compute set of configurations to exclude, if any.
  2382. std::vector<size_t> const& include_configs = si.Configs;
  2383. std::vector<size_t> exclude_configs;
  2384. std::set_difference(all_configs.begin(), all_configs.end(),
  2385. include_configs.begin(), include_configs.end(),
  2386. std::back_inserter(exclude_configs));
  2387. Elem e2(e1, tool);
  2388. bool isCSharp = (si.Source->GetLanguage() == "CSharp");
  2389. if (isCSharp && !exclude_configs.empty()) {
  2390. std::stringstream conditions;
  2391. bool firstConditionSet{ false };
  2392. for (const auto& ci : include_configs) {
  2393. if (firstConditionSet) {
  2394. conditions << " Or ";
  2395. }
  2396. conditions << "('$(Configuration)|$(Platform)'=='" +
  2397. this->Configurations[ci] + "|" + this->Platform + "')";
  2398. firstConditionSet = true;
  2399. }
  2400. e2.Attribute("Condition", conditions.str());
  2401. }
  2402. this->WriteSource(e2, si.Source);
  2403. bool useNativeUnityBuild = false;
  2404. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  2405. // Magic value taken from cmGlobalVisualStudioVersionedGenerator.cxx
  2406. static const std::string vs15 = "141";
  2407. std::string toolset =
  2408. this->GlobalGenerator->GetPlatformToolsetString();
  2409. cmSystemTools::ReplaceString(toolset, "v", "");
  2410. if (toolset.empty() ||
  2411. cmSystemTools::VersionCompareGreaterEq(toolset, vs15)) {
  2412. useNativeUnityBuild = true;
  2413. }
  2414. }
  2415. if (haveUnityBuild && strcmp(tool, "ClCompile") == 0 &&
  2416. si.Source->GetProperty("UNITY_SOURCE_FILE")) {
  2417. if (useNativeUnityBuild) {
  2418. e2.Attribute(
  2419. "IncludeInUnityFile",
  2420. si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")
  2421. ? "false"
  2422. : "true");
  2423. e2.Attribute("CustomUnityFile", "true");
  2424. std::string unityDir = cmSystemTools::GetFilenamePath(
  2425. *si.Source->GetProperty("UNITY_SOURCE_FILE"));
  2426. e2.Attribute("UnityFilesDirectory", unityDir);
  2427. } else {
  2428. // Visual Studio versions prior to 2017 15.8 do not know about unity
  2429. // builds, thus we exclude the files already part of unity sources.
  2430. if (!si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")) {
  2431. exclude_configs = all_configs;
  2432. }
  2433. }
  2434. }
  2435. if (si.Kind == cmGeneratorTarget::SourceKindObjectSource ||
  2436. si.Kind == cmGeneratorTarget::SourceKindUnityBatched) {
  2437. this->OutputSourceSpecificFlags(e2, si.Source);
  2438. }
  2439. if (si.Source->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  2440. e2.Element("PrecompiledHeader", "NotUsing");
  2441. }
  2442. if (!isCSharp && !exclude_configs.empty()) {
  2443. this->WriteExcludeFromBuild(e2, exclude_configs);
  2444. }
  2445. this->FinishWritingSource(e2, toolSettings);
  2446. }
  2447. }
  2448. if (this->IsMissingFiles) {
  2449. this->WriteMissingFiles(e1);
  2450. }
  2451. }
  2452. void cmVisualStudio10TargetGenerator::FinishWritingSource(
  2453. Elem& e2, ConfigToSettings const& toolSettings)
  2454. {
  2455. std::vector<std::string> writtenSettings;
  2456. for (const auto& configSettings : toolSettings) {
  2457. for (const auto& setting : configSettings.second) {
  2458. if (std::find(writtenSettings.begin(), writtenSettings.end(),
  2459. setting.first) != writtenSettings.end()) {
  2460. continue;
  2461. }
  2462. if (PropertyIsSameInAllConfigs(toolSettings, setting.first)) {
  2463. e2.Element(setting.first, setting.second);
  2464. writtenSettings.push_back(setting.first);
  2465. } else {
  2466. e2.WritePlatformConfigTag(setting.first,
  2467. "'$(Configuration)|$(Platform)'=='" +
  2468. configSettings.first + "|" +
  2469. this->Platform + "'",
  2470. setting.second);
  2471. }
  2472. }
  2473. }
  2474. }
  2475. void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  2476. Elem& e2, cmSourceFile const* source)
  2477. {
  2478. cmSourceFile const& sf = *source;
  2479. std::string objectName;
  2480. if (this->GeneratorTarget->HasExplicitObjectName(&sf)) {
  2481. objectName = this->GeneratorTarget->GetObjectName(&sf);
  2482. }
  2483. std::string flags;
  2484. bool configDependentFlags = false;
  2485. std::string options;
  2486. bool configDependentOptions = false;
  2487. std::string defines;
  2488. bool configDependentDefines = false;
  2489. std::string includes;
  2490. bool configDependentIncludes = false;
  2491. if (cmValue cflags = sf.GetProperty("COMPILE_FLAGS")) {
  2492. configDependentFlags =
  2493. cmGeneratorExpression::Find(*cflags) != std::string::npos;
  2494. flags += *cflags;
  2495. }
  2496. if (cmValue coptions = sf.GetProperty("COMPILE_OPTIONS")) {
  2497. configDependentOptions =
  2498. cmGeneratorExpression::Find(*coptions) != std::string::npos;
  2499. options += *coptions;
  2500. }
  2501. if (cmValue cdefs = sf.GetProperty("COMPILE_DEFINITIONS")) {
  2502. configDependentDefines =
  2503. cmGeneratorExpression::Find(*cdefs) != std::string::npos;
  2504. defines += *cdefs;
  2505. }
  2506. if (cmValue cincludes = sf.GetProperty("INCLUDE_DIRECTORIES")) {
  2507. configDependentIncludes =
  2508. cmGeneratorExpression::Find(*cincludes) != std::string::npos;
  2509. includes += *cincludes;
  2510. }
  2511. // Force language if the file extension does not match.
  2512. // Note that MSVC treats the upper-case '.C' extension as C and not C++.
  2513. std::string const ext = sf.GetExtension();
  2514. std::string const extLang = ext == "C"
  2515. ? "C"
  2516. : this->GlobalGenerator->GetLanguageFromExtension(ext.c_str());
  2517. std::string lang = this->LocalGenerator->GetSourceFileLanguage(sf);
  2518. const char* compileAs = nullptr;
  2519. if (lang != extLang) {
  2520. if (lang == "CXX") {
  2521. // force a C++ file type
  2522. compileAs = "CompileAsCpp";
  2523. } else if (lang == "C") {
  2524. // force to c
  2525. compileAs = "CompileAsC";
  2526. }
  2527. }
  2528. bool noWinRT = this->TargetCompileAsWinRT && lang == "C";
  2529. // for the first time we need a new line if there is something
  2530. // produced here.
  2531. if (!objectName.empty()) {
  2532. if (lang == "CUDA") {
  2533. e2.Element("CompileOut", "$(IntDir)/" + objectName);
  2534. } else {
  2535. e2.Element("ObjectFileName", "$(IntDir)/" + objectName);
  2536. }
  2537. }
  2538. if (lang == "ASM_NASM") {
  2539. if (cmValue objectDeps = sf.GetProperty("OBJECT_DEPENDS")) {
  2540. cmList depends{ *objectDeps };
  2541. for (auto& d : depends) {
  2542. ConvertToWindowsSlash(d);
  2543. }
  2544. e2.Element("AdditionalDependencies", depends.join(";"));
  2545. }
  2546. }
  2547. for (std::string const& config : this->Configurations) {
  2548. this->GeneratorTarget->NeedCxxModuleSupport(lang, config);
  2549. std::string configUpper = cmSystemTools::UpperCase(config);
  2550. std::string configDefines = defines;
  2551. std::string defPropName = cmStrCat("COMPILE_DEFINITIONS_", configUpper);
  2552. if (cmValue ccdefs = sf.GetProperty(defPropName)) {
  2553. if (!configDefines.empty()) {
  2554. configDefines += ";";
  2555. }
  2556. configDependentDefines |=
  2557. cmGeneratorExpression::Find(*ccdefs) != std::string::npos;
  2558. configDefines += *ccdefs;
  2559. }
  2560. bool const shouldScanForModules = lang == "CXX"_s &&
  2561. this->GeneratorTarget->NeedDyndepForSource(lang, config, source);
  2562. auto const* fs =
  2563. this->GeneratorTarget->GetFileSetForSource(config, source);
  2564. const char* compileAsPerConfig = compileAs;
  2565. if (fs &&
  2566. (fs->GetType() == "CXX_MODULES"_s ||
  2567. fs->GetType() == "CXX_MODULE_HEADER_UNITS"_s)) {
  2568. if (lang == "CXX"_s) {
  2569. if (fs->GetType() == "CXX_MODULES"_s) {
  2570. compileAsPerConfig = "CompileAsCppModule";
  2571. } else {
  2572. compileAsPerConfig = "CompileAsHeaderUnit";
  2573. }
  2574. } else {
  2575. this->Makefile->IssueMessage(
  2576. MessageType::FATAL_ERROR,
  2577. cmStrCat(
  2578. "Target \"", this->GeneratorTarget->Target->GetName(),
  2579. "\" contains the source\n ", source->GetFullPath(),
  2580. "\nin a file set of type \"", fs->GetType(),
  2581. R"(" but the source is not classified as a "CXX" source.)"));
  2582. }
  2583. }
  2584. // We have pch state in the following situation:
  2585. // 1. We have SKIP_PRECOMPILE_HEADERS == true
  2586. // 2. We are creating the pre-compiled header
  2587. // 3. We are a different language than the linker language AND pch is
  2588. // enabled.
  2589. std::string const& linkLanguage =
  2590. this->GeneratorTarget->GetLinkerLanguage(config);
  2591. std::string const& pchSource =
  2592. this->GeneratorTarget->GetPchSource(config, lang);
  2593. const bool skipPCH =
  2594. pchSource.empty() || sf.GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS");
  2595. const bool makePCH = (sf.GetFullPath() == pchSource);
  2596. const bool useSharedPCH = !skipPCH && (lang == linkLanguage);
  2597. const bool useDifferentLangPCH = !skipPCH && (lang != linkLanguage);
  2598. const bool useNoPCH = skipPCH && (lang != linkLanguage) &&
  2599. !this->GeneratorTarget->GetPchHeader(config, linkLanguage).empty();
  2600. const bool needsPCHFlags =
  2601. (makePCH || useSharedPCH || useDifferentLangPCH || useNoPCH);
  2602. // if we have flags or defines for this config then
  2603. // use them
  2604. if (!flags.empty() || !options.empty() || !configDefines.empty() ||
  2605. !includes.empty() || compileAsPerConfig || noWinRT ||
  2606. !options.empty() || needsPCHFlags) {
  2607. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2608. cmIDEFlagTable const* flagtable = nullptr;
  2609. const std::string& srclang = source->GetLanguage();
  2610. if (srclang == "C" || srclang == "CXX") {
  2611. flagtable = gg->GetClFlagTable();
  2612. } else if (srclang == "ASM_MARMASM" &&
  2613. this->GlobalGenerator->IsMarmasmEnabled()) {
  2614. flagtable = gg->GetMarmasmFlagTable();
  2615. } else if (srclang == "ASM_MASM" &&
  2616. this->GlobalGenerator->IsMasmEnabled()) {
  2617. flagtable = gg->GetMasmFlagTable();
  2618. } else if (lang == "ASM_NASM" &&
  2619. this->GlobalGenerator->IsNasmEnabled()) {
  2620. flagtable = gg->GetNasmFlagTable();
  2621. } else if (srclang == "RC") {
  2622. flagtable = gg->GetRcFlagTable();
  2623. } else if (srclang == "CSharp") {
  2624. flagtable = gg->GetCSharpFlagTable();
  2625. }
  2626. cmGeneratorExpressionInterpreter genexInterpreter(
  2627. this->LocalGenerator, config, this->GeneratorTarget, lang);
  2628. cmVS10GeneratorOptions clOptions(
  2629. this->LocalGenerator, cmVisualStudioGeneratorOptions::Compiler,
  2630. flagtable, this);
  2631. if (compileAsPerConfig) {
  2632. clOptions.AddFlag("CompileAs", compileAsPerConfig);
  2633. }
  2634. if (shouldScanForModules) {
  2635. clOptions.AddFlag("ScanSourceforModuleDependencies", "true");
  2636. } else {
  2637. clOptions.AddFlag("ScanSourceforModuleDependencies", "false");
  2638. }
  2639. if (noWinRT) {
  2640. clOptions.AddFlag("CompileAsWinRT", "false");
  2641. }
  2642. if (configDependentFlags) {
  2643. clOptions.Parse(genexInterpreter.Evaluate(flags, "COMPILE_FLAGS"));
  2644. } else {
  2645. clOptions.Parse(flags);
  2646. }
  2647. if (needsPCHFlags) {
  2648. // Add precompile headers compile options.
  2649. if (makePCH) {
  2650. clOptions.AddFlag("PrecompiledHeader", "Create");
  2651. std::string pchHeader =
  2652. this->GeneratorTarget->GetPchHeader(config, lang);
  2653. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2654. std::string pchFile =
  2655. this->GeneratorTarget->GetPchFile(config, lang);
  2656. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  2657. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2658. } else if (useNoPCH) {
  2659. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  2660. } else if (useSharedPCH) {
  2661. std::string pchHeader =
  2662. this->GeneratorTarget->GetPchHeader(config, lang);
  2663. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2664. } else if (useDifferentLangPCH) {
  2665. clOptions.AddFlag("PrecompiledHeader", "Use");
  2666. std::string pchHeader =
  2667. this->GeneratorTarget->GetPchHeader(config, lang);
  2668. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2669. std::string pchFile =
  2670. this->GeneratorTarget->GetPchFile(config, lang);
  2671. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  2672. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2673. }
  2674. }
  2675. if (!options.empty()) {
  2676. std::string expandedOptions;
  2677. if (configDependentOptions) {
  2678. this->LocalGenerator->AppendCompileOptions(
  2679. expandedOptions,
  2680. genexInterpreter.Evaluate(options, "COMPILE_OPTIONS"));
  2681. } else {
  2682. this->LocalGenerator->AppendCompileOptions(expandedOptions, options);
  2683. }
  2684. clOptions.Parse(expandedOptions);
  2685. }
  2686. if (clOptions.HasFlag("DisableSpecificWarnings")) {
  2687. clOptions.AppendFlag("DisableSpecificWarnings",
  2688. "%(DisableSpecificWarnings)");
  2689. }
  2690. if (clOptions.HasFlag("ForcedIncludeFiles")) {
  2691. clOptions.AppendFlag("ForcedIncludeFiles", "%(ForcedIncludeFiles)");
  2692. }
  2693. if (configDependentDefines) {
  2694. clOptions.AddDefines(
  2695. genexInterpreter.Evaluate(configDefines, "COMPILE_DEFINITIONS"));
  2696. } else {
  2697. clOptions.AddDefines(configDefines);
  2698. }
  2699. std::vector<std::string> includeList;
  2700. if (configDependentIncludes) {
  2701. this->LocalGenerator->AppendIncludeDirectories(
  2702. includeList,
  2703. genexInterpreter.Evaluate(includes, "INCLUDE_DIRECTORIES"), *source);
  2704. } else {
  2705. this->LocalGenerator->AppendIncludeDirectories(includeList, includes,
  2706. *source);
  2707. }
  2708. clOptions.AddIncludes(includeList);
  2709. clOptions.SetConfiguration(config);
  2710. OptionsHelper oh(clOptions, e2);
  2711. oh.PrependInheritedString("AdditionalOptions");
  2712. oh.OutputAdditionalIncludeDirectories(lang);
  2713. oh.OutputFlagMap();
  2714. oh.OutputPreprocessorDefinitions(lang);
  2715. }
  2716. }
  2717. if (this->IsXamlSource(source->GetFullPath())) {
  2718. const std::string& fileName = source->GetFullPath();
  2719. e2.Element("DependentUpon",
  2720. fileName.substr(0, fileName.find_last_of('.')));
  2721. }
  2722. if (this->ProjectType == VsProjectType::csproj) {
  2723. using CsPropMap = std::map<std::string, std::string>;
  2724. CsPropMap sourceFileTags;
  2725. this->GetCSharpSourceProperties(&sf, sourceFileTags);
  2726. // write source file specific tags
  2727. if (!sourceFileTags.empty()) {
  2728. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2729. }
  2730. }
  2731. }
  2732. void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild(
  2733. Elem& e2, std::vector<size_t> const& exclude_configs)
  2734. {
  2735. for (size_t ci : exclude_configs) {
  2736. e2.WritePlatformConfigTag("ExcludedFromBuild",
  2737. "'$(Configuration)|$(Platform)'=='" +
  2738. this->Configurations[ci] + "|" +
  2739. this->Platform + "'",
  2740. "true");
  2741. }
  2742. }
  2743. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions(
  2744. Elem& e0)
  2745. {
  2746. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2747. if (ttype > cmStateEnums::INTERFACE_LIBRARY) {
  2748. return;
  2749. }
  2750. if (this->ProjectType == VsProjectType::csproj) {
  2751. return;
  2752. }
  2753. Elem e1(e0, "PropertyGroup");
  2754. e1.Element("_ProjectFileVersion", "10.0.20506.1");
  2755. for (std::string const& config : this->Configurations) {
  2756. const std::string cond = this->CalcCondition(config);
  2757. if (ttype >= cmStateEnums::UTILITY) {
  2758. e1.WritePlatformConfigTag(
  2759. "IntDir", cond, R"($(Platform)\$(Configuration)\$(ProjectName)\)");
  2760. } else {
  2761. std::string intermediateDir = cmStrCat(
  2762. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  2763. config, '/');
  2764. std::string outDir;
  2765. std::string targetNameFull;
  2766. if (ttype == cmStateEnums::OBJECT_LIBRARY) {
  2767. outDir = intermediateDir;
  2768. targetNameFull = cmStrCat(this->GeneratorTarget->GetName(), ".lib");
  2769. } else {
  2770. outDir = this->GeneratorTarget->GetDirectory(config) + "/";
  2771. targetNameFull = this->GeneratorTarget->GetFullName(config);
  2772. }
  2773. ConvertToWindowsSlash(intermediateDir);
  2774. ConvertToWindowsSlash(outDir);
  2775. e1.WritePlatformConfigTag("OutDir", cond, outDir);
  2776. e1.WritePlatformConfigTag("IntDir", cond, intermediateDir);
  2777. if (cmValue sdkExecutableDirectories = this->Makefile->GetDefinition(
  2778. "CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES")) {
  2779. e1.WritePlatformConfigTag("ExecutablePath", cond,
  2780. *sdkExecutableDirectories);
  2781. }
  2782. if (cmValue sdkIncludeDirectories = this->Makefile->GetDefinition(
  2783. "CMAKE_VS_SDK_INCLUDE_DIRECTORIES")) {
  2784. e1.WritePlatformConfigTag("IncludePath", cond, *sdkIncludeDirectories);
  2785. }
  2786. if (cmValue sdkReferenceDirectories = this->Makefile->GetDefinition(
  2787. "CMAKE_VS_SDK_REFERENCE_DIRECTORIES")) {
  2788. e1.WritePlatformConfigTag("ReferencePath", cond,
  2789. *sdkReferenceDirectories);
  2790. }
  2791. if (cmValue sdkLibraryDirectories = this->Makefile->GetDefinition(
  2792. "CMAKE_VS_SDK_LIBRARY_DIRECTORIES")) {
  2793. e1.WritePlatformConfigTag("LibraryPath", cond, *sdkLibraryDirectories);
  2794. }
  2795. if (cmValue sdkLibraryWDirectories = this->Makefile->GetDefinition(
  2796. "CMAKE_VS_SDK_LIBRARY_WINRT_DIRECTORIES")) {
  2797. e1.WritePlatformConfigTag("LibraryWPath", cond,
  2798. *sdkLibraryWDirectories);
  2799. }
  2800. if (cmValue sdkSourceDirectories =
  2801. this->Makefile->GetDefinition("CMAKE_VS_SDK_SOURCE_DIRECTORIES")) {
  2802. e1.WritePlatformConfigTag("SourcePath", cond, *sdkSourceDirectories);
  2803. }
  2804. if (cmValue sdkExcludeDirectories = this->Makefile->GetDefinition(
  2805. "CMAKE_VS_SDK_EXCLUDE_DIRECTORIES")) {
  2806. e1.WritePlatformConfigTag("ExcludePath", cond, *sdkExcludeDirectories);
  2807. }
  2808. std::string name =
  2809. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  2810. e1.WritePlatformConfigTag("TargetName", cond, name);
  2811. std::string ext =
  2812. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  2813. if (ext.empty()) {
  2814. // An empty TargetExt causes a default extension to be used.
  2815. // A single "." appears to be treated as an empty extension.
  2816. ext = ".";
  2817. }
  2818. e1.WritePlatformConfigTag("TargetExt", cond, ext);
  2819. this->OutputLinkIncremental(e1, config);
  2820. }
  2821. if (ttype <= cmStateEnums::UTILITY) {
  2822. if (cmValue workingDir = this->GeneratorTarget->GetProperty(
  2823. "VS_DEBUGGER_WORKING_DIRECTORY")) {
  2824. std::string genWorkingDir = cmGeneratorExpression::Evaluate(
  2825. *workingDir, this->LocalGenerator, config);
  2826. e1.WritePlatformConfigTag("LocalDebuggerWorkingDirectory", cond,
  2827. genWorkingDir);
  2828. }
  2829. if (cmValue environment =
  2830. this->GeneratorTarget->GetProperty("VS_DEBUGGER_ENVIRONMENT")) {
  2831. std::string genEnvironment = cmGeneratorExpression::Evaluate(
  2832. *environment, this->LocalGenerator, config);
  2833. e1.WritePlatformConfigTag("LocalDebuggerEnvironment", cond,
  2834. genEnvironment);
  2835. }
  2836. if (cmValue debuggerCommand =
  2837. this->GeneratorTarget->GetProperty("VS_DEBUGGER_COMMAND")) {
  2838. std::string genDebuggerCommand = cmGeneratorExpression::Evaluate(
  2839. *debuggerCommand, this->LocalGenerator, config);
  2840. e1.WritePlatformConfigTag("LocalDebuggerCommand", cond,
  2841. genDebuggerCommand);
  2842. }
  2843. if (cmValue commandArguments = this->GeneratorTarget->GetProperty(
  2844. "VS_DEBUGGER_COMMAND_ARGUMENTS")) {
  2845. std::string genCommandArguments = cmGeneratorExpression::Evaluate(
  2846. *commandArguments, this->LocalGenerator, config);
  2847. e1.WritePlatformConfigTag("LocalDebuggerCommandArguments", cond,
  2848. genCommandArguments);
  2849. }
  2850. }
  2851. }
  2852. }
  2853. void cmVisualStudio10TargetGenerator::OutputLinkIncremental(
  2854. Elem& e1, std::string const& configName)
  2855. {
  2856. if (!this->MSTools) {
  2857. return;
  2858. }
  2859. if (this->ProjectType == VsProjectType::csproj) {
  2860. return;
  2861. }
  2862. // static libraries and things greater than modules do not need
  2863. // to set this option
  2864. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2865. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  2866. return;
  2867. }
  2868. Options& linkOptions = *(this->LinkOptions[configName]);
  2869. const std::string cond = this->CalcCondition(configName);
  2870. if (this->IPOEnabledConfigurations.count(configName) == 0) {
  2871. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  2872. e1.WritePlatformConfigTag("LinkIncremental", cond,
  2873. (incremental ? incremental : "true"));
  2874. }
  2875. linkOptions.RemoveFlag("LinkIncremental");
  2876. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  2877. e1.WritePlatformConfigTag("GenerateManifest", cond,
  2878. (manifest ? manifest : "true"));
  2879. linkOptions.RemoveFlag("GenerateManifest");
  2880. // Some link options belong here. Use them now and remove them so that
  2881. // WriteLinkOptions does not use them.
  2882. static const std::vector<std::string> flags{ "LinkDelaySign",
  2883. "LinkKeyFile" };
  2884. for (const std::string& flag : flags) {
  2885. if (const char* value = linkOptions.GetFlag(flag)) {
  2886. e1.WritePlatformConfigTag(flag, cond, value);
  2887. linkOptions.RemoveFlag(flag);
  2888. }
  2889. }
  2890. }
  2891. std::vector<std::string> cmVisualStudio10TargetGenerator::GetIncludes(
  2892. std::string const& config, std::string const& lang) const
  2893. {
  2894. std::vector<std::string> includes;
  2895. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  2896. lang, config);
  2897. for (std::string& i : includes) {
  2898. ConvertToWindowsSlash(i);
  2899. }
  2900. return includes;
  2901. }
  2902. std::string cmVisualStudio10TargetGenerator::GetTargetOutputName() const
  2903. {
  2904. std::string config;
  2905. if (!this->Configurations.empty()) {
  2906. config = this->Configurations[0];
  2907. }
  2908. const auto& nameComponents =
  2909. this->GeneratorTarget->GetFullNameComponents(config);
  2910. return nameComponents.prefix + nameComponents.base;
  2911. }
  2912. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  2913. {
  2914. return std::all_of(
  2915. this->Configurations.begin(), this->Configurations.end(),
  2916. [this](std::string const& c) { return this->ComputeClOptions(c); });
  2917. }
  2918. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  2919. std::string const& configName)
  2920. {
  2921. // much of this was copied from here:
  2922. // copied from cmLocalVisualStudio7Generator.cxx 805
  2923. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  2924. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2925. std::unique_ptr<Options> pOptions;
  2926. switch (this->ProjectType) {
  2927. case VsProjectType::vcxproj:
  2928. pOptions = cm::make_unique<Options>(
  2929. this->LocalGenerator, Options::Compiler, gg->GetClFlagTable());
  2930. break;
  2931. case VsProjectType::csproj:
  2932. pOptions =
  2933. cm::make_unique<Options>(this->LocalGenerator, Options::CSharpCompiler,
  2934. gg->GetCSharpFlagTable());
  2935. break;
  2936. default:
  2937. break;
  2938. }
  2939. Options& clOptions = *pOptions;
  2940. std::string flags;
  2941. const std::string& linkLanguage =
  2942. this->GeneratorTarget->GetLinkerLanguage(configName);
  2943. if (linkLanguage.empty()) {
  2944. cmSystemTools::Error(
  2945. "CMake can not determine linker language for target: " + this->Name);
  2946. return false;
  2947. }
  2948. // Choose a language whose flags to use for ClCompile.
  2949. static const char* clLangs[] = { "CXX", "C", "Fortran" };
  2950. std::string langForClCompile;
  2951. if (this->ProjectType == VsProjectType::csproj) {
  2952. langForClCompile = "CSharp";
  2953. } else if (cm::contains(clLangs, linkLanguage)) {
  2954. langForClCompile = linkLanguage;
  2955. } else {
  2956. std::set<std::string> languages;
  2957. this->GeneratorTarget->GetLanguages(languages, configName);
  2958. for (const char* l : clLangs) {
  2959. if (languages.count(l)) {
  2960. langForClCompile = l;
  2961. break;
  2962. }
  2963. }
  2964. }
  2965. this->LangForClCompile = langForClCompile;
  2966. if (!langForClCompile.empty()) {
  2967. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  2968. cmBuildStep::Compile,
  2969. langForClCompile, configName);
  2970. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  2971. langForClCompile, configName);
  2972. }
  2973. // Put the IPO enabled configurations into a set.
  2974. if (this->GeneratorTarget->IsIPOEnabled(linkLanguage, configName)) {
  2975. this->IPOEnabledConfigurations.insert(configName);
  2976. }
  2977. // Check if ASan is enabled.
  2978. if (flags.find("/fsanitize=address") != std::string::npos ||
  2979. flags.find("-fsanitize=address") != std::string::npos) {
  2980. this->ASanEnabledConfigurations.insert(configName);
  2981. }
  2982. // Check if (lib)Fuzzer is enabled.
  2983. if (flags.find("/fsanitize=fuzzer") != std::string::npos ||
  2984. flags.find("-fsanitize=fuzzer") != std::string::npos) {
  2985. this->FuzzerEnabledConfigurations.insert(configName);
  2986. }
  2987. // Precompile Headers
  2988. std::string pchHeader =
  2989. this->GeneratorTarget->GetPchHeader(configName, linkLanguage);
  2990. if (this->MSTools && VsProjectType::vcxproj == this->ProjectType &&
  2991. pchHeader.empty()) {
  2992. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  2993. } else if (this->MSTools && VsProjectType::vcxproj == this->ProjectType &&
  2994. !pchHeader.empty()) {
  2995. clOptions.AddFlag("PrecompiledHeader", "Use");
  2996. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2997. std::string pchFile =
  2998. this->GeneratorTarget->GetPchFile(configName, linkLanguage);
  2999. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  3000. }
  3001. // Get preprocessor definitions for this directory.
  3002. std::string defineFlags = this->Makefile->GetDefineFlags();
  3003. if (this->MSTools) {
  3004. if (this->ProjectType == VsProjectType::vcxproj) {
  3005. clOptions.FixExceptionHandlingDefault();
  3006. if (this->GlobalGenerator->GetVersion() >=
  3007. cmGlobalVisualStudioGenerator::VSVersion::VS15) {
  3008. // Toolsets that come with VS 2017 may now enable UseFullPaths
  3009. // by default and there is no negative /FC option that projects
  3010. // can use to switch it back. Older toolsets disable this by
  3011. // default anyway so this will not hurt them. If the project
  3012. // is using an explicit /FC option then parsing flags will
  3013. // replace this setting with "true" below.
  3014. clOptions.AddFlag("UseFullPaths", "false");
  3015. }
  3016. clOptions.AddFlag("AssemblerListingLocation", "$(IntDir)");
  3017. }
  3018. }
  3019. // check for managed C++ assembly compiler flag. This overrides any
  3020. // /clr* compiler flags which may be defined in the flags variable(s).
  3021. if (this->ProjectType != VsProjectType::csproj) {
  3022. // Warn if /clr was added manually. This should not be done
  3023. // anymore, because cmGeneratorTarget may not be aware that the
  3024. // target uses C++/CLI.
  3025. if (flags.find("/clr") != std::string::npos ||
  3026. flags.find("-clr") != std::string::npos ||
  3027. defineFlags.find("/clr") != std::string::npos ||
  3028. defineFlags.find("-clr") != std::string::npos) {
  3029. if (configName == this->Configurations[0]) {
  3030. std::string message = "For the target \"" +
  3031. this->GeneratorTarget->GetName() +
  3032. "\" the /clr compiler flag was added manually. " +
  3033. "Set usage of C++/CLI by setting COMMON_LANGUAGE_RUNTIME "
  3034. "target property.";
  3035. this->Makefile->IssueMessage(MessageType::WARNING, message);
  3036. }
  3037. }
  3038. if (cmValue clr =
  3039. this->GeneratorTarget->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  3040. std::string clrString = *clr;
  3041. if (!clrString.empty()) {
  3042. clrString = ":" + clrString;
  3043. }
  3044. flags += " /clr" + clrString;
  3045. }
  3046. }
  3047. // Get includes for this target
  3048. if (!this->LangForClCompile.empty()) {
  3049. auto includeList = this->GetIncludes(configName, this->LangForClCompile);
  3050. auto sysIncludeFlag = this->Makefile->GetDefinition(
  3051. cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", this->LangForClCompile));
  3052. if (sysIncludeFlag) {
  3053. bool gotOneSys = false;
  3054. for (auto i : includeList) {
  3055. cmSystemTools::ConvertToUnixSlashes(i);
  3056. if (this->GeneratorTarget->IsSystemIncludeDirectory(
  3057. i, configName, this->LangForClCompile)) {
  3058. auto flag = cmTrimWhitespace(*sysIncludeFlag);
  3059. if (this->MSTools) {
  3060. cmSystemTools::ReplaceString(flag, "-external:I", "/external:I");
  3061. }
  3062. clOptions.AppendFlagString("AdditionalOptions",
  3063. cmStrCat(flag, " \"", i, '"'));
  3064. gotOneSys = true;
  3065. } else {
  3066. clOptions.AddInclude(i);
  3067. }
  3068. }
  3069. if (gotOneSys) {
  3070. if (auto sysIncludeFlagWarning = this->Makefile->GetDefinition(
  3071. cmStrCat("_CMAKE_INCLUDE_SYSTEM_FLAG_", this->LangForClCompile,
  3072. "_WARNING"))) {
  3073. flags = cmStrCat(flags, ' ', *sysIncludeFlagWarning);
  3074. }
  3075. }
  3076. } else {
  3077. clOptions.AddIncludes(includeList);
  3078. }
  3079. }
  3080. clOptions.Parse(flags);
  3081. clOptions.Parse(defineFlags);
  3082. std::vector<std::string> targetDefines;
  3083. switch (this->ProjectType) {
  3084. case VsProjectType::vcxproj:
  3085. if (!langForClCompile.empty()) {
  3086. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3087. langForClCompile);
  3088. }
  3089. break;
  3090. case VsProjectType::csproj:
  3091. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3092. "CSharp");
  3093. cm::erase_if(targetDefines, [](std::string const& def) {
  3094. return def.find('=') != std::string::npos;
  3095. });
  3096. break;
  3097. default:
  3098. break;
  3099. }
  3100. clOptions.AddDefines(targetDefines);
  3101. if (this->ProjectType == VsProjectType::csproj) {
  3102. clOptions.AppendFlag("DefineConstants", targetDefines);
  3103. }
  3104. if (this->MSTools) {
  3105. clOptions.SetVerboseMakefile(
  3106. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  3107. }
  3108. // Add C-specific flags expressible in a ClCompile meant for C++.
  3109. if (langForClCompile == "CXX") {
  3110. std::set<std::string> languages;
  3111. this->GeneratorTarget->GetLanguages(languages, configName);
  3112. if (languages.count("C")) {
  3113. std::string flagsC;
  3114. this->LocalGenerator->AddLanguageFlags(
  3115. flagsC, this->GeneratorTarget, cmBuildStep::Compile, "C", configName);
  3116. this->LocalGenerator->AddCompileOptions(flagsC, this->GeneratorTarget,
  3117. "C", configName);
  3118. Options optC(this->LocalGenerator, Options::Compiler,
  3119. gg->GetClFlagTable());
  3120. optC.Parse(flagsC);
  3121. if (const char* stdC = optC.GetFlag("LanguageStandard_C")) {
  3122. clOptions.AddFlag("LanguageStandard_C", stdC);
  3123. }
  3124. }
  3125. }
  3126. // Add a definition for the configuration name.
  3127. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  3128. clOptions.AddDefine(configDefine);
  3129. if (const std::string* exportMacro =
  3130. this->GeneratorTarget->GetExportMacro()) {
  3131. clOptions.AddDefine(*exportMacro);
  3132. }
  3133. if (this->MSTools) {
  3134. // If we have the VS_WINRT_COMPONENT set then force Compile as WinRT
  3135. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT")) {
  3136. clOptions.AddFlag("CompileAsWinRT", "true");
  3137. // For WinRT components, add the _WINRT_DLL define to produce a lib
  3138. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3139. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3140. clOptions.AddDefine("_WINRT_DLL");
  3141. }
  3142. } else if (this->GlobalGenerator->TargetsWindowsStore() ||
  3143. this->GlobalGenerator->TargetsWindowsPhone() ||
  3144. this->Makefile->IsOn("CMAKE_VS_WINRT_BY_DEFAULT")) {
  3145. if (!clOptions.IsWinRt()) {
  3146. clOptions.AddFlag("CompileAsWinRT", "false");
  3147. }
  3148. }
  3149. if (const char* winRT = clOptions.GetFlag("CompileAsWinRT")) {
  3150. if (cmIsOn(winRT)) {
  3151. this->TargetCompileAsWinRT = true;
  3152. }
  3153. }
  3154. }
  3155. if (this->ProjectType != VsProjectType::csproj &&
  3156. (clOptions.IsManaged() || clOptions.HasFlag("CLRSupport"))) {
  3157. this->Managed = true;
  3158. std::string managedType = clOptions.HasFlag("CompileAsManaged")
  3159. ? clOptions.GetFlag("CompileAsManaged")
  3160. : "Mixed";
  3161. if (managedType == "Safe" || managedType == "Pure") {
  3162. // force empty calling convention if safe clr is used
  3163. clOptions.AddFlag("CallingConvention", "");
  3164. }
  3165. // The default values of these flags are incompatible to
  3166. // managed assemblies. We have to force valid values if
  3167. // the target is a managed C++ target.
  3168. clOptions.AddFlag("ExceptionHandling", "Async");
  3169. clOptions.AddFlag("BasicRuntimeChecks", "Default");
  3170. }
  3171. if (this->ProjectType == VsProjectType::csproj) {
  3172. // /nowin32manifest overrides /win32manifest: parameter
  3173. if (clOptions.HasFlag("NoWin32Manifest")) {
  3174. clOptions.RemoveFlag("ApplicationManifest");
  3175. }
  3176. }
  3177. if (const char* s = clOptions.GetFlag("SpectreMitigation")) {
  3178. this->SpectreMitigation[configName] = s;
  3179. clOptions.RemoveFlag("SpectreMitigation");
  3180. }
  3181. // Remove any target-wide -TC or -TP flag added by the project.
  3182. // Such flags are unnecessary and break our model of language selection.
  3183. if (langForClCompile == "C" || langForClCompile == "CXX") {
  3184. clOptions.RemoveFlag("CompileAs");
  3185. }
  3186. this->ClOptions[configName] = std::move(pOptions);
  3187. return true;
  3188. }
  3189. void cmVisualStudio10TargetGenerator::WriteClOptions(
  3190. Elem& e1, std::string const& configName)
  3191. {
  3192. Options& clOptions = *(this->ClOptions[configName]);
  3193. if (this->ProjectType == VsProjectType::csproj) {
  3194. return;
  3195. }
  3196. Elem e2(e1, "ClCompile");
  3197. OptionsHelper oh(clOptions, e2);
  3198. oh.PrependInheritedString("AdditionalOptions");
  3199. oh.OutputAdditionalIncludeDirectories(this->LangForClCompile);
  3200. oh.OutputFlagMap();
  3201. oh.OutputPreprocessorDefinitions(this->LangForClCompile);
  3202. if (this->NsightTegra) {
  3203. if (cmValue processMax =
  3204. this->GeneratorTarget->GetProperty("ANDROID_PROCESS_MAX")) {
  3205. e2.Element("ProcessMax", *processMax);
  3206. }
  3207. }
  3208. if (this->Android) {
  3209. e2.Element("ObjectFileName", "$(IntDir)%(filename).o");
  3210. } else if (this->MSTools) {
  3211. cmsys::RegularExpression clangToolset("v[0-9]+_clang_.*");
  3212. const char* toolset = this->GlobalGenerator->GetPlatformToolset();
  3213. cmValue noCompileBatching =
  3214. this->GeneratorTarget->GetProperty("VS_NO_COMPILE_BATCHING");
  3215. if (noCompileBatching.IsOn() || (toolset && clangToolset.find(toolset))) {
  3216. e2.Element("ObjectFileName", "$(IntDir)%(filename).obj");
  3217. } else {
  3218. e2.Element("ObjectFileName", "$(IntDir)");
  3219. }
  3220. // If not in debug mode, write the DebugInformationFormat field
  3221. // without value so PDBs don't get generated uselessly. Each tag
  3222. // goes on its own line because Visual Studio corrects it this
  3223. // way when saving the project after CMake generates it.
  3224. if (!clOptions.IsDebug()) {
  3225. Elem e3(e2, "DebugInformationFormat");
  3226. e3.SetHasElements();
  3227. }
  3228. // Specify the compiler program database file if configured.
  3229. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  3230. if (!pdb.empty()) {
  3231. if (this->GlobalGenerator->IsCudaEnabled()) {
  3232. // CUDA does not quote paths with spaces correctly when forwarding
  3233. // this to the host compiler. Use a relative path to avoid spaces.
  3234. // FIXME: We can likely do this even when CUDA is not involved,
  3235. // but for now we will make a minimal change.
  3236. pdb = this->ConvertPath(pdb, true);
  3237. }
  3238. ConvertToWindowsSlash(pdb);
  3239. e2.Element("ProgramDataBaseFileName", pdb);
  3240. }
  3241. // add AdditionalUsingDirectories
  3242. if (this->AdditionalUsingDirectories.count(configName) > 0) {
  3243. std::string dirs;
  3244. for (auto const& u : this->AdditionalUsingDirectories[configName]) {
  3245. if (!dirs.empty()) {
  3246. dirs.append(";");
  3247. }
  3248. dirs.append(u);
  3249. }
  3250. e2.Element("AdditionalUsingDirectories", dirs);
  3251. }
  3252. }
  3253. }
  3254. bool cmVisualStudio10TargetGenerator::ComputeRcOptions()
  3255. {
  3256. return std::all_of(
  3257. this->Configurations.begin(), this->Configurations.end(),
  3258. [this](std::string const& c) { return this->ComputeRcOptions(c); });
  3259. }
  3260. bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
  3261. std::string const& configName)
  3262. {
  3263. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3264. auto pOptions = cm::make_unique<Options>(
  3265. this->LocalGenerator, Options::ResourceCompiler, gg->GetRcFlagTable());
  3266. Options& rcOptions = *pOptions;
  3267. std::string CONFIG = cmSystemTools::UpperCase(configName);
  3268. std::string rcConfigFlagsVar = "CMAKE_RC_FLAGS_" + CONFIG;
  3269. std::string flags = this->Makefile->GetSafeDefinition("CMAKE_RC_FLAGS") +
  3270. " " + this->Makefile->GetSafeDefinition(rcConfigFlagsVar);
  3271. rcOptions.Parse(flags);
  3272. // For historical reasons, add the C preprocessor defines to RC.
  3273. Options& clOptions = *(this->ClOptions[configName]);
  3274. rcOptions.AddDefines(clOptions.GetDefines());
  3275. // Get includes for this target
  3276. rcOptions.AddIncludes(this->GetIncludes(configName, "RC"));
  3277. this->RcOptions[configName] = std::move(pOptions);
  3278. return true;
  3279. }
  3280. void cmVisualStudio10TargetGenerator::WriteRCOptions(
  3281. Elem& e1, std::string const& configName)
  3282. {
  3283. if (!this->MSTools) {
  3284. return;
  3285. }
  3286. Elem e2(e1, "ResourceCompile");
  3287. OptionsHelper rcOptions(*(this->RcOptions[configName]), e2);
  3288. rcOptions.OutputPreprocessorDefinitions("RC");
  3289. rcOptions.OutputAdditionalIncludeDirectories("RC");
  3290. rcOptions.PrependInheritedString("AdditionalOptions");
  3291. rcOptions.OutputFlagMap();
  3292. }
  3293. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions()
  3294. {
  3295. if (!this->GlobalGenerator->IsCudaEnabled()) {
  3296. return true;
  3297. }
  3298. return std::all_of(this->Configurations.begin(), this->Configurations.end(),
  3299. [this](std::string const& c) {
  3300. return !this->GeneratorTarget->IsLanguageUsed("CUDA",
  3301. c) ||
  3302. this->ComputeCudaOptions(c);
  3303. });
  3304. }
  3305. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
  3306. std::string const& configName)
  3307. {
  3308. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3309. auto pOptions = cm::make_unique<Options>(
  3310. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  3311. Options& cudaOptions = *pOptions;
  3312. auto cudaVersion = this->GlobalGenerator->GetPlatformToolsetCudaString();
  3313. // Get compile flags for CUDA in this directory.
  3314. std::string flags;
  3315. this->LocalGenerator->AddLanguageFlags(
  3316. flags, this->GeneratorTarget, cmBuildStep::Compile, "CUDA", configName);
  3317. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, "CUDA",
  3318. configName);
  3319. // Get preprocessor definitions for this directory.
  3320. std::string defineFlags = this->Makefile->GetDefineFlags();
  3321. cudaOptions.Parse(flags);
  3322. cudaOptions.Parse(defineFlags);
  3323. cudaOptions.ParseFinish();
  3324. // If we haven't explicitly enabled GPU debug information
  3325. // explicitly disable it
  3326. if (!cudaOptions.HasFlag("GPUDebugInfo")) {
  3327. cudaOptions.AddFlag("GPUDebugInfo", "false");
  3328. }
  3329. // The extension on object libraries the CUDA gives isn't
  3330. // consistent with how MSVC generates object libraries for C+, so set
  3331. // the default to not have any extension
  3332. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).obj");
  3333. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  3334. cudaOptions.AddFlag("GenerateRelocatableDeviceCode", "true");
  3335. }
  3336. bool notPtxLike = true;
  3337. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) {
  3338. cudaOptions.AddFlag("NvccCompilation", "ptx");
  3339. // We drop the %(Extension) component as CMake expects all PTX files
  3340. // to not have the source file extension at all
  3341. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).ptx");
  3342. notPtxLike = false;
  3343. if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  3344. cudaVersion, "9.0") &&
  3345. cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, cudaVersion,
  3346. "11.5")) {
  3347. // The DriverApi flag before 11.5 ( verified back to 9.0 ) which controls
  3348. // PTX compilation doesn't propagate user defines causing
  3349. // target_compile_definitions to behave differently for VS +
  3350. // PTX compared to other generators so we patch the rules
  3351. // to normalize behavior
  3352. cudaOptions.AddFlag("DriverApiCommandLineTemplate",
  3353. "%(BaseCommandLineTemplate) [CompileOut] [FastMath] "
  3354. "[Defines] \"%(FullPath)\"");
  3355. }
  3356. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3357. "CUDA_CUBIN_COMPILATION")) {
  3358. cudaOptions.AddFlag("NvccCompilation", "cubin");
  3359. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).cubin");
  3360. notPtxLike = false;
  3361. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3362. "CUDA_FATBIN_COMPILATION")) {
  3363. cudaOptions.AddFlag("NvccCompilation", "fatbin");
  3364. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).fatbin");
  3365. notPtxLike = false;
  3366. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3367. "CUDA_OPTIX_COMPILATION")) {
  3368. cudaOptions.AddFlag("NvccCompilation", "optix-ir");
  3369. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).optixir");
  3370. notPtxLike = false;
  3371. }
  3372. if (notPtxLike &&
  3373. cmSystemTools::VersionCompareGreaterEq(
  3374. "8.0", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  3375. // Explicitly state that we want this file to be treated as a
  3376. // CUDA file no matter what the file extensions is
  3377. // This is only needed for < CUDA 9
  3378. cudaOptions.AppendFlagString("AdditionalOptions", "-x cu");
  3379. }
  3380. // Specify the compiler program database file if configured.
  3381. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  3382. if (!pdb.empty()) {
  3383. // CUDA does not make the directory if it is non-standard.
  3384. std::string const pdbDir = cmSystemTools::GetFilenamePath(pdb);
  3385. cmSystemTools::MakeDirectory(pdbDir);
  3386. if (cmSystemTools::VersionCompareGreaterEq(
  3387. "9.2", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  3388. // CUDA does not have a field for this and does not honor the
  3389. // ProgramDataBaseFileName field in ClCompile. Work around this
  3390. // limitation by creating the directory and passing the flag ourselves.
  3391. pdb = this->ConvertPath(pdb, true);
  3392. ConvertToWindowsSlash(pdb);
  3393. std::string const clFd = R"(-Xcompiler="-Fd\")" + pdb + R"(\"")";
  3394. cudaOptions.AppendFlagString("AdditionalOptions", clFd);
  3395. }
  3396. }
  3397. // CUDA automatically passes the proper '--machine' flag to nvcc
  3398. // for the current architecture, but does not reflect this default
  3399. // in the user-visible IDE settings. Set it explicitly.
  3400. if (this->Platform == "x64") {
  3401. cudaOptions.AddFlag("TargetMachinePlatform", "64");
  3402. }
  3403. // Convert the host compiler options to the toolset's abstractions
  3404. // using a secondary flag table.
  3405. cudaOptions.ClearTables();
  3406. cudaOptions.AddTable(gg->GetCudaHostFlagTable());
  3407. cudaOptions.Reparse("AdditionalCompilerOptions");
  3408. // `CUDA 8.0.targets` places AdditionalCompilerOptions before nvcc!
  3409. // Pass them through -Xcompiler in AdditionalOptions instead.
  3410. if (const char* acoPtr = cudaOptions.GetFlag("AdditionalCompilerOptions")) {
  3411. std::string aco = acoPtr;
  3412. cudaOptions.RemoveFlag("AdditionalCompilerOptions");
  3413. if (!aco.empty()) {
  3414. aco = this->LocalGenerator->EscapeForShell(aco, false);
  3415. cudaOptions.AppendFlagString("AdditionalOptions", "-Xcompiler=" + aco);
  3416. }
  3417. }
  3418. cudaOptions.FixCudaCodeGeneration();
  3419. std::vector<std::string> targetDefines;
  3420. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3421. "CUDA");
  3422. cudaOptions.AddDefines(targetDefines);
  3423. // Add a definition for the configuration name.
  3424. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  3425. cudaOptions.AddDefine(configDefine);
  3426. if (const std::string* exportMacro =
  3427. this->GeneratorTarget->GetExportMacro()) {
  3428. cudaOptions.AddDefine(*exportMacro);
  3429. }
  3430. // Get includes for this target
  3431. cudaOptions.AddIncludes(this->GetIncludes(configName, "CUDA"));
  3432. cudaOptions.AddFlag("UseHostInclude", "false");
  3433. // Add runtime library selection flag.
  3434. std::string const& cudaRuntime =
  3435. this->GeneratorTarget->GetRuntimeLinkLibrary("CUDA", configName);
  3436. if (cudaRuntime == "STATIC") {
  3437. cudaOptions.AddFlag("CudaRuntime", "Static");
  3438. } else if (cudaRuntime == "SHARED") {
  3439. cudaOptions.AddFlag("CudaRuntime", "Shared");
  3440. } else if (cudaRuntime == "NONE") {
  3441. cudaOptions.AddFlag("CudaRuntime", "None");
  3442. }
  3443. this->CudaOptions[configName] = std::move(pOptions);
  3444. return true;
  3445. }
  3446. void cmVisualStudio10TargetGenerator::WriteCudaOptions(
  3447. Elem& e1, std::string const& configName)
  3448. {
  3449. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled() ||
  3450. !this->GeneratorTarget->IsLanguageUsed("CUDA", configName)) {
  3451. return;
  3452. }
  3453. Elem e2(e1, "CudaCompile");
  3454. OptionsHelper cudaOptions(*(this->CudaOptions[configName]), e2);
  3455. cudaOptions.OutputAdditionalIncludeDirectories("CUDA");
  3456. cudaOptions.OutputPreprocessorDefinitions("CUDA");
  3457. cudaOptions.PrependInheritedString("AdditionalOptions");
  3458. cudaOptions.OutputFlagMap();
  3459. }
  3460. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions()
  3461. {
  3462. if (!this->GlobalGenerator->IsCudaEnabled()) {
  3463. return true;
  3464. }
  3465. return std::all_of(
  3466. this->Configurations.begin(), this->Configurations.end(),
  3467. [this](std::string const& c) { return this->ComputeCudaLinkOptions(c); });
  3468. }
  3469. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions(
  3470. std::string const& configName)
  3471. {
  3472. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3473. auto pOptions = cm::make_unique<Options>(
  3474. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  3475. Options& cudaLinkOptions = *pOptions;
  3476. cmGeneratorTarget::DeviceLinkSetter setter(*this->GeneratorTarget);
  3477. // Determine if we need to do a device link
  3478. const bool doDeviceLinking = requireDeviceLinking(
  3479. *this->GeneratorTarget, *this->LocalGenerator, configName);
  3480. cudaLinkOptions.AddFlag("PerformDeviceLink",
  3481. doDeviceLinking ? "true" : "false");
  3482. // Add extra flags for device linking
  3483. cudaLinkOptions.AppendFlagString(
  3484. "AdditionalOptions",
  3485. this->Makefile->GetSafeDefinition("_CMAKE_CUDA_EXTRA_FLAGS"));
  3486. cudaLinkOptions.AppendFlagString(
  3487. "AdditionalOptions",
  3488. this->Makefile->GetSafeDefinition("_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS"));
  3489. std::vector<std::string> linkOpts;
  3490. std::string linkFlags;
  3491. this->GeneratorTarget->GetLinkOptions(linkOpts, configName, "CUDA");
  3492. // LINK_OPTIONS are escaped.
  3493. this->LocalGenerator->AppendCompileOptions(linkFlags, linkOpts);
  3494. cmComputeLinkInformation* pcli =
  3495. this->GeneratorTarget->GetLinkInformation(configName);
  3496. if (doDeviceLinking && pcli) {
  3497. cmLinkLineDeviceComputer computer(
  3498. this->LocalGenerator,
  3499. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  3500. std::string ignored_;
  3501. this->LocalGenerator->GetDeviceLinkFlags(computer, configName, ignored_,
  3502. linkFlags, ignored_, ignored_,
  3503. this->GeneratorTarget);
  3504. this->LocalGenerator->AddLanguageFlagsForLinking(
  3505. linkFlags, this->GeneratorTarget, "CUDA", configName);
  3506. }
  3507. cudaLinkOptions.AppendFlagString("AdditionalOptions", linkFlags);
  3508. // For static libraries that have device linking enabled compute
  3509. // the libraries
  3510. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY &&
  3511. doDeviceLinking) {
  3512. cmComputeLinkInformation& cli = *pcli;
  3513. cmLinkLineDeviceComputer computer(
  3514. this->LocalGenerator,
  3515. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  3516. std::vector<BT<std::string>> btLibVec;
  3517. computer.ComputeLinkLibraries(cli, std::string{}, btLibVec);
  3518. std::vector<std::string> libVec;
  3519. for (auto const& item : btLibVec) {
  3520. libVec.emplace_back(item.Value);
  3521. }
  3522. cudaLinkOptions.AddFlag("AdditionalDependencies", libVec);
  3523. }
  3524. this->CudaLinkOptions[configName] = std::move(pOptions);
  3525. return true;
  3526. }
  3527. void cmVisualStudio10TargetGenerator::WriteCudaLinkOptions(
  3528. Elem& e1, std::string const& configName)
  3529. {
  3530. if (this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  3531. return;
  3532. }
  3533. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  3534. return;
  3535. }
  3536. Elem e2(e1, "CudaLink");
  3537. OptionsHelper cudaLinkOptions(*(this->CudaLinkOptions[configName]), e2);
  3538. cudaLinkOptions.OutputFlagMap();
  3539. }
  3540. bool cmVisualStudio10TargetGenerator::ComputeMarmasmOptions()
  3541. {
  3542. if (!this->GlobalGenerator->IsMarmasmEnabled()) {
  3543. return true;
  3544. }
  3545. return std::all_of(
  3546. this->Configurations.begin(), this->Configurations.end(),
  3547. [this](std::string const& c) { return this->ComputeMarmasmOptions(c); });
  3548. }
  3549. bool cmVisualStudio10TargetGenerator::ComputeMarmasmOptions(
  3550. std::string const& configName)
  3551. {
  3552. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3553. auto pOptions = cm::make_unique<Options>(
  3554. this->LocalGenerator, Options::MarmasmCompiler, gg->GetMarmasmFlagTable());
  3555. Options& marmasmOptions = *pOptions;
  3556. std::string flags;
  3557. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3558. cmBuildStep::Compile, "ASM_MARMASM",
  3559. configName);
  3560. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3561. "ASM_MARMASM", configName);
  3562. marmasmOptions.Parse(flags);
  3563. // Get includes for this target
  3564. marmasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MARMASM"));
  3565. this->MarmasmOptions[configName] = std::move(pOptions);
  3566. return true;
  3567. }
  3568. void cmVisualStudio10TargetGenerator::WriteMarmasmOptions(
  3569. Elem& e1, std::string const& configName)
  3570. {
  3571. if (!this->MSTools || !this->GlobalGenerator->IsMarmasmEnabled()) {
  3572. return;
  3573. }
  3574. Elem e2(e1, "MARMASM");
  3575. // Preprocessor definitions and includes are shared with clOptions.
  3576. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3577. clOptions.OutputPreprocessorDefinitions("ASM_MARMASM");
  3578. OptionsHelper marmasmOptions(*(this->MarmasmOptions[configName]), e2);
  3579. marmasmOptions.OutputAdditionalIncludeDirectories("ASM_MARMASM");
  3580. marmasmOptions.PrependInheritedString("AdditionalOptions");
  3581. marmasmOptions.OutputFlagMap();
  3582. }
  3583. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions()
  3584. {
  3585. if (!this->GlobalGenerator->IsMasmEnabled()) {
  3586. return true;
  3587. }
  3588. return std::all_of(
  3589. this->Configurations.begin(), this->Configurations.end(),
  3590. [this](std::string const& c) { return this->ComputeMasmOptions(c); });
  3591. }
  3592. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions(
  3593. std::string const& configName)
  3594. {
  3595. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3596. auto pOptions = cm::make_unique<Options>(
  3597. this->LocalGenerator, Options::MasmCompiler, gg->GetMasmFlagTable());
  3598. Options& masmOptions = *pOptions;
  3599. // MSBuild enables debug information by default.
  3600. // Disable it explicitly unless a flag parsed below re-enables it.
  3601. masmOptions.AddFlag("GenerateDebugInformation", "false");
  3602. std::string flags;
  3603. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3604. cmBuildStep::Compile, "ASM_MASM",
  3605. configName);
  3606. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3607. "ASM_MASM", configName);
  3608. masmOptions.Parse(flags);
  3609. // Get includes for this target
  3610. masmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MASM"));
  3611. this->MasmOptions[configName] = std::move(pOptions);
  3612. return true;
  3613. }
  3614. void cmVisualStudio10TargetGenerator::WriteMasmOptions(
  3615. Elem& e1, std::string const& configName)
  3616. {
  3617. if (!this->MSTools || !this->GlobalGenerator->IsMasmEnabled()) {
  3618. return;
  3619. }
  3620. Elem e2(e1, "MASM");
  3621. // Preprocessor definitions and includes are shared with clOptions.
  3622. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3623. clOptions.OutputPreprocessorDefinitions("ASM_MASM");
  3624. OptionsHelper masmOptions(*(this->MasmOptions[configName]), e2);
  3625. masmOptions.OutputAdditionalIncludeDirectories("ASM_MASM");
  3626. masmOptions.PrependInheritedString("AdditionalOptions");
  3627. masmOptions.OutputFlagMap();
  3628. }
  3629. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions()
  3630. {
  3631. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3632. return true;
  3633. }
  3634. return std::all_of(
  3635. this->Configurations.begin(), this->Configurations.end(),
  3636. [this](std::string const& c) { return this->ComputeNasmOptions(c); });
  3637. }
  3638. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions(
  3639. std::string const& configName)
  3640. {
  3641. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3642. auto pOptions = cm::make_unique<Options>(
  3643. this->LocalGenerator, Options::NasmCompiler, gg->GetNasmFlagTable());
  3644. Options& nasmOptions = *pOptions;
  3645. std::string flags;
  3646. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3647. cmBuildStep::Compile, "ASM_NASM",
  3648. configName);
  3649. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3650. "ASM_NASM", configName);
  3651. flags += " -f";
  3652. flags += this->Makefile->GetSafeDefinition("CMAKE_ASM_NASM_OBJECT_FORMAT");
  3653. nasmOptions.Parse(flags);
  3654. // Get includes for this target
  3655. nasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_NASM"));
  3656. this->NasmOptions[configName] = std::move(pOptions);
  3657. return true;
  3658. }
  3659. void cmVisualStudio10TargetGenerator::WriteNasmOptions(
  3660. Elem& e1, std::string const& configName)
  3661. {
  3662. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3663. return;
  3664. }
  3665. Elem e2(e1, "NASM");
  3666. OptionsHelper nasmOptions(*(this->NasmOptions[configName]), e2);
  3667. nasmOptions.OutputAdditionalIncludeDirectories("ASM_NASM");
  3668. nasmOptions.OutputFlagMap();
  3669. nasmOptions.PrependInheritedString("AdditionalOptions");
  3670. nasmOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3671. // Preprocessor definitions and includes are shared with clOptions.
  3672. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3673. clOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3674. }
  3675. void cmVisualStudio10TargetGenerator::WriteLibOptions(
  3676. Elem& e1, std::string const& config)
  3677. {
  3678. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3679. this->GeneratorTarget->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  3680. return;
  3681. }
  3682. const std::string& linkLanguage =
  3683. this->GeneratorTarget->GetLinkClosure(config)->LinkerLanguage;
  3684. std::string libflags;
  3685. this->LocalGenerator->GetStaticLibraryFlags(libflags, config, linkLanguage,
  3686. this->GeneratorTarget);
  3687. if (!libflags.empty()) {
  3688. Elem e2(e1, "Lib");
  3689. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3690. cmVS10GeneratorOptions libOptions(this->LocalGenerator,
  3691. cmVisualStudioGeneratorOptions::Linker,
  3692. gg->GetLibFlagTable(), this);
  3693. libOptions.Parse(libflags);
  3694. OptionsHelper oh(libOptions, e2);
  3695. oh.PrependInheritedString("AdditionalOptions");
  3696. oh.OutputFlagMap();
  3697. }
  3698. // We cannot generate metadata for static libraries. WindowsPhone
  3699. // and WindowsStore tools look at GenerateWindowsMetadata in the
  3700. // Link tool options even for static libraries.
  3701. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3702. this->GlobalGenerator->TargetsWindowsStore()) {
  3703. Elem e2(e1, "Link");
  3704. e2.Element("GenerateWindowsMetadata", "false");
  3705. }
  3706. }
  3707. void cmVisualStudio10TargetGenerator::WriteManifestOptions(
  3708. Elem& e1, std::string const& config)
  3709. {
  3710. if (this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE &&
  3711. this->GeneratorTarget->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3712. this->GeneratorTarget->GetType() != cmStateEnums::MODULE_LIBRARY) {
  3713. return;
  3714. }
  3715. std::vector<cmSourceFile const*> manifest_srcs;
  3716. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  3717. cmValue dpiAware = this->GeneratorTarget->GetProperty("VS_DPI_AWARE");
  3718. if (!manifest_srcs.empty() || dpiAware) {
  3719. Elem e2(e1, "Manifest");
  3720. if (!manifest_srcs.empty()) {
  3721. std::ostringstream oss;
  3722. for (cmSourceFile const* mi : manifest_srcs) {
  3723. std::string m = this->ConvertPath(mi->GetFullPath(), false);
  3724. ConvertToWindowsSlash(m);
  3725. oss << m << ";";
  3726. }
  3727. e2.Element("AdditionalManifestFiles", oss.str());
  3728. }
  3729. if (dpiAware) {
  3730. if (*dpiAware == "PerMonitor") {
  3731. e2.Element("EnableDpiAwareness", "PerMonitorHighDPIAware");
  3732. } else if (cmIsOn(*dpiAware)) {
  3733. e2.Element("EnableDpiAwareness", "true");
  3734. } else if (cmIsOff(*dpiAware)) {
  3735. e2.Element("EnableDpiAwareness", "false");
  3736. } else {
  3737. cmSystemTools::Error("Bad parameter for VS_DPI_AWARE: " + *dpiAware);
  3738. }
  3739. }
  3740. }
  3741. }
  3742. void cmVisualStudio10TargetGenerator::WriteAntBuildOptions(
  3743. Elem& e1, std::string const& configName)
  3744. {
  3745. // Look through the sources for AndroidManifest.xml and use
  3746. // its location as the root source directory.
  3747. std::string rootDir = this->LocalGenerator->GetCurrentSourceDirectory();
  3748. {
  3749. for (cmGeneratorTarget::AllConfigSource const& source :
  3750. this->GeneratorTarget->GetAllConfigSources()) {
  3751. if (source.Kind == cmGeneratorTarget::SourceKindExtra &&
  3752. "androidmanifest.xml" ==
  3753. cmSystemTools::LowerCase(source.Source->GetLocation().GetName())) {
  3754. rootDir = source.Source->GetLocation().GetDirectory();
  3755. break;
  3756. }
  3757. }
  3758. }
  3759. // Tell MSBuild to launch Ant.
  3760. Elem e2(e1, "AntBuild");
  3761. {
  3762. std::string antBuildPath = rootDir;
  3763. ConvertToWindowsSlash(antBuildPath);
  3764. e2.Element("AntBuildPath", antBuildPath);
  3765. }
  3766. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_SKIP_ANT_STEP")) {
  3767. e2.Element("SkipAntStep", "true");
  3768. }
  3769. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_PROGUARD")) {
  3770. e2.Element("EnableProGuard", "true");
  3771. }
  3772. if (cmValue proGuardConfigLocation =
  3773. this->GeneratorTarget->GetProperty("ANDROID_PROGUARD_CONFIG_PATH")) {
  3774. e2.Element("ProGuardConfigLocation", *proGuardConfigLocation);
  3775. }
  3776. if (cmValue securePropertiesLocation =
  3777. this->GeneratorTarget->GetProperty("ANDROID_SECURE_PROPS_PATH")) {
  3778. e2.Element("SecurePropertiesLocation", *securePropertiesLocation);
  3779. }
  3780. if (cmValue nativeLibDirectoriesExpression =
  3781. this->GeneratorTarget->GetProperty("ANDROID_NATIVE_LIB_DIRECTORIES")) {
  3782. std::string nativeLibDirs = cmGeneratorExpression::Evaluate(
  3783. *nativeLibDirectoriesExpression, this->LocalGenerator, configName);
  3784. e2.Element("NativeLibDirectories", nativeLibDirs);
  3785. }
  3786. if (cmValue nativeLibDependenciesExpression =
  3787. this->GeneratorTarget->GetProperty(
  3788. "ANDROID_NATIVE_LIB_DEPENDENCIES")) {
  3789. std::string nativeLibDeps = cmGeneratorExpression::Evaluate(
  3790. *nativeLibDependenciesExpression, this->LocalGenerator, configName);
  3791. e2.Element("NativeLibDependencies", nativeLibDeps);
  3792. }
  3793. if (cmValue javaSourceDir =
  3794. this->GeneratorTarget->GetProperty("ANDROID_JAVA_SOURCE_DIR")) {
  3795. e2.Element("JavaSourceDir", *javaSourceDir);
  3796. }
  3797. if (cmValue jarDirectoriesExpression =
  3798. this->GeneratorTarget->GetProperty("ANDROID_JAR_DIRECTORIES")) {
  3799. std::string jarDirectories = cmGeneratorExpression::Evaluate(
  3800. *jarDirectoriesExpression, this->LocalGenerator, configName);
  3801. e2.Element("JarDirectories", jarDirectories);
  3802. }
  3803. if (cmValue jarDeps =
  3804. this->GeneratorTarget->GetProperty("ANDROID_JAR_DEPENDENCIES")) {
  3805. e2.Element("JarDependencies", *jarDeps);
  3806. }
  3807. if (cmValue assetsDirectories =
  3808. this->GeneratorTarget->GetProperty("ANDROID_ASSETS_DIRECTORIES")) {
  3809. e2.Element("AssetsDirectories", *assetsDirectories);
  3810. }
  3811. {
  3812. std::string manifest_xml = rootDir + "/AndroidManifest.xml";
  3813. ConvertToWindowsSlash(manifest_xml);
  3814. e2.Element("AndroidManifestLocation", manifest_xml);
  3815. }
  3816. if (cmValue antAdditionalOptions =
  3817. this->GeneratorTarget->GetProperty("ANDROID_ANT_ADDITIONAL_OPTIONS")) {
  3818. e2.Element("AdditionalOptions",
  3819. *antAdditionalOptions + " %(AdditionalOptions)");
  3820. }
  3821. }
  3822. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  3823. {
  3824. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  3825. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3826. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3827. for (std::string const& c : this->Configurations) {
  3828. if (!this->ComputeLinkOptions(c)) {
  3829. return false;
  3830. }
  3831. }
  3832. }
  3833. return true;
  3834. }
  3835. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
  3836. std::string const& config)
  3837. {
  3838. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3839. auto pOptions = cm::make_unique<Options>(
  3840. this->LocalGenerator, Options::Linker, gg->GetLinkFlagTable(), this);
  3841. Options& linkOptions = *pOptions;
  3842. cmGeneratorTarget::LinkClosure const* linkClosure =
  3843. this->GeneratorTarget->GetLinkClosure(config);
  3844. const std::string& linkLanguage = linkClosure->LinkerLanguage;
  3845. if (linkLanguage.empty()) {
  3846. cmSystemTools::Error(
  3847. "CMake can not determine linker language for target: " + this->Name);
  3848. return false;
  3849. }
  3850. std::string CONFIG = cmSystemTools::UpperCase(config);
  3851. const char* linkType = "SHARED";
  3852. if (this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3853. linkType = "MODULE";
  3854. }
  3855. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3856. linkType = "EXE";
  3857. }
  3858. std::string flags;
  3859. std::string linkFlagVarBase = cmStrCat("CMAKE_", linkType, "_LINKER_FLAGS");
  3860. flags += " ";
  3861. flags += this->Makefile->GetRequiredDefinition(linkFlagVarBase);
  3862. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  3863. flags += " ";
  3864. flags += this->Makefile->GetRequiredDefinition(linkFlagVar);
  3865. cmValue targetLinkFlags = this->GeneratorTarget->GetProperty("LINK_FLAGS");
  3866. if (targetLinkFlags) {
  3867. flags += " ";
  3868. flags += *targetLinkFlags;
  3869. }
  3870. std::string flagsProp = cmStrCat("LINK_FLAGS_", CONFIG);
  3871. if (cmValue flagsConfig = this->GeneratorTarget->GetProperty(flagsProp)) {
  3872. flags += " ";
  3873. flags += *flagsConfig;
  3874. }
  3875. std::vector<std::string> opts;
  3876. this->GeneratorTarget->GetLinkOptions(opts, config, linkLanguage);
  3877. // LINK_OPTIONS are escaped.
  3878. this->LocalGenerator->AppendCompileOptions(flags, opts);
  3879. cmComputeLinkInformation* pcli =
  3880. this->GeneratorTarget->GetLinkInformation(config);
  3881. if (!pcli) {
  3882. cmSystemTools::Error(
  3883. "CMake can not compute cmComputeLinkInformation for target: " +
  3884. this->Name);
  3885. return false;
  3886. }
  3887. cmComputeLinkInformation& cli = *pcli;
  3888. std::vector<std::string> libVec;
  3889. std::vector<std::string> vsTargetVec;
  3890. this->AddLibraries(cli, libVec, vsTargetVec, config);
  3891. std::string standardLibsVar =
  3892. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  3893. std::string const& libs = this->Makefile->GetSafeDefinition(standardLibsVar);
  3894. cmSystemTools::ParseWindowsCommandLine(libs.c_str(), libVec);
  3895. linkOptions.AddFlag("AdditionalDependencies", libVec);
  3896. // Populate TargetsFileAndConfigsVec
  3897. for (std::string const& ti : vsTargetVec) {
  3898. this->AddTargetsFileAndConfigPair(ti, config);
  3899. }
  3900. std::vector<std::string> const& ldirs = cli.GetDirectories();
  3901. std::vector<std::string> linkDirs;
  3902. for (std::string const& d : ldirs) {
  3903. // first just full path
  3904. linkDirs.push_back(d);
  3905. // next path with configuration type Debug, Release, etc
  3906. linkDirs.push_back(d + "/$(Configuration)");
  3907. }
  3908. linkDirs.push_back("%(AdditionalLibraryDirectories)");
  3909. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs);
  3910. cmGeneratorTarget::Names targetNames;
  3911. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3912. targetNames = this->GeneratorTarget->GetExecutableNames(config);
  3913. } else {
  3914. targetNames = this->GeneratorTarget->GetLibraryNames(config);
  3915. }
  3916. if (this->MSTools) {
  3917. if (this->GeneratorTarget->IsWin32Executable(config)) {
  3918. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3919. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3920. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3921. if (this->ClOptions[config]->UsingUnicode()) {
  3922. linkOptions.AddFlag("EntryPointSymbol", "wWinMainCRTStartup");
  3923. } else {
  3924. linkOptions.AddFlag("EntryPointSymbol", "WinMainCRTStartup");
  3925. }
  3926. }
  3927. } else {
  3928. linkOptions.AddFlag("SubSystem", "Windows");
  3929. }
  3930. } else {
  3931. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3932. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3933. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3934. if (this->ClOptions[config]->UsingUnicode()) {
  3935. linkOptions.AddFlag("EntryPointSymbol", "mainWCRTStartup");
  3936. } else {
  3937. linkOptions.AddFlag("EntryPointSymbol", "mainACRTStartup");
  3938. }
  3939. }
  3940. } else {
  3941. linkOptions.AddFlag("SubSystem", "Console");
  3942. };
  3943. }
  3944. if (cmValue stackVal = this->Makefile->GetDefinition(
  3945. "CMAKE_" + linkLanguage + "_STACK_SIZE")) {
  3946. linkOptions.AddFlag("StackReserveSize", *stackVal);
  3947. }
  3948. linkOptions.AddFlag("GenerateDebugInformation", "false");
  3949. std::string pdb = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config),
  3950. '/', targetNames.PDB);
  3951. if (!targetNames.ImportLibrary.empty()) {
  3952. std::string imLib =
  3953. cmStrCat(this->GeneratorTarget->GetDirectory(
  3954. config, cmStateEnums::ImportLibraryArtifact),
  3955. '/', targetNames.ImportLibrary);
  3956. linkOptions.AddFlag("ImportLibrary", imLib);
  3957. }
  3958. linkOptions.AddFlag("ProgramDataBaseFile", pdb);
  3959. // A Windows Runtime component uses internal .NET metadata,
  3960. // so does not have an import library.
  3961. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") &&
  3962. this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE) {
  3963. linkOptions.AddFlag("GenerateWindowsMetadata", "true");
  3964. } else if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3965. this->GlobalGenerator->TargetsWindowsStore()) {
  3966. // WindowsPhone and WindowsStore components are in an app container
  3967. // and produce WindowsMetadata. If we are not producing a WINRT
  3968. // component, then do not generate the metadata here.
  3969. linkOptions.AddFlag("GenerateWindowsMetadata", "false");
  3970. }
  3971. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  3972. this->GlobalGenerator->GetSystemVersion() == "8.0") {
  3973. // WindowsPhone 8.0 does not have ole32.
  3974. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries", "ole32.lib");
  3975. }
  3976. } else if (this->NsightTegra) {
  3977. linkOptions.AddFlag("SoName", targetNames.SharedObject);
  3978. }
  3979. linkOptions.Parse(flags);
  3980. linkOptions.FixManifestUACFlags();
  3981. if (this->MSTools) {
  3982. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  3983. this->GeneratorTarget->GetModuleDefinitionInfo(config);
  3984. if (mdi && !mdi->DefFile.empty()) {
  3985. linkOptions.AddFlag("ModuleDefinitionFile", mdi->DefFile);
  3986. }
  3987. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries",
  3988. "%(IgnoreSpecificDefaultLibraries)");
  3989. }
  3990. // VS 2015 without all updates has a v140 toolset whose
  3991. // GenerateDebugInformation expects No/Debug instead of false/true.
  3992. if (gg->GetPlatformToolsetNeedsDebugEnum()) {
  3993. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  3994. if (strcmp(debug, "false") == 0) {
  3995. linkOptions.AddFlag("GenerateDebugInformation", "No");
  3996. } else if (strcmp(debug, "true") == 0) {
  3997. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  3998. }
  3999. }
  4000. }
  4001. // Managed code cannot be linked with /DEBUG:FASTLINK
  4002. if (this->Managed) {
  4003. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  4004. if (strcmp(debug, "DebugFastLink") == 0) {
  4005. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  4006. }
  4007. }
  4008. }
  4009. this->LinkOptions[config] = std::move(pOptions);
  4010. return true;
  4011. }
  4012. bool cmVisualStudio10TargetGenerator::ComputeLibOptions()
  4013. {
  4014. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4015. for (std::string const& c : this->Configurations) {
  4016. if (!this->ComputeLibOptions(c)) {
  4017. return false;
  4018. }
  4019. }
  4020. }
  4021. return true;
  4022. }
  4023. bool cmVisualStudio10TargetGenerator::ComputeLibOptions(
  4024. std::string const& config)
  4025. {
  4026. cmComputeLinkInformation* pcli =
  4027. this->GeneratorTarget->GetLinkInformation(config);
  4028. if (!pcli) {
  4029. cmSystemTools::Error(
  4030. "CMake can not compute cmComputeLinkInformation for target: " +
  4031. this->Name);
  4032. return false;
  4033. }
  4034. cmComputeLinkInformation& cli = *pcli;
  4035. using ItemVector = cmComputeLinkInformation::ItemVector;
  4036. const ItemVector& libs = cli.GetItems();
  4037. for (cmComputeLinkInformation::Item const& l : libs) {
  4038. if (l.IsPath == cmComputeLinkInformation::ItemIsPath::Yes &&
  4039. cmVS10IsTargetsFile(l.Value.Value)) {
  4040. std::string path =
  4041. this->LocalGenerator->MaybeRelativeToCurBinDir(l.Value.Value);
  4042. ConvertToWindowsSlash(path);
  4043. this->AddTargetsFileAndConfigPair(path, config);
  4044. }
  4045. }
  4046. return true;
  4047. }
  4048. void cmVisualStudio10TargetGenerator::WriteLinkOptions(
  4049. Elem& e1, std::string const& config)
  4050. {
  4051. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4052. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  4053. return;
  4054. }
  4055. if (this->ProjectType == VsProjectType::csproj) {
  4056. return;
  4057. }
  4058. {
  4059. Elem e2(e1, "Link");
  4060. OptionsHelper linkOptions(*(this->LinkOptions[config]), e2);
  4061. linkOptions.PrependInheritedString("AdditionalOptions");
  4062. linkOptions.OutputFlagMap();
  4063. }
  4064. if (!this->GlobalGenerator->NeedLinkLibraryDependencies(
  4065. this->GeneratorTarget)) {
  4066. Elem e2(e1, "ProjectReference");
  4067. e2.Element("LinkLibraryDependencies", "false");
  4068. }
  4069. }
  4070. void cmVisualStudio10TargetGenerator::AddLibraries(
  4071. const cmComputeLinkInformation& cli, std::vector<std::string>& libVec,
  4072. std::vector<std::string>& vsTargetVec, const std::string& config)
  4073. {
  4074. using ItemVector = cmComputeLinkInformation::ItemVector;
  4075. ItemVector const& libs = cli.GetItems();
  4076. for (cmComputeLinkInformation::Item const& l : libs) {
  4077. if (l.Target) {
  4078. auto managedType = l.Target->GetManagedType(config);
  4079. if (managedType != cmGeneratorTarget::ManagedType::Native &&
  4080. this->GeneratorTarget->GetManagedType(config) !=
  4081. cmGeneratorTarget::ManagedType::Native &&
  4082. l.Target->IsImported() &&
  4083. l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4084. auto location = l.Target->GetFullPath(config);
  4085. if (!location.empty()) {
  4086. ConvertToWindowsSlash(location);
  4087. switch (this->ProjectType) {
  4088. case VsProjectType::csproj:
  4089. // If the target we want to "link" to is an imported managed
  4090. // target and this is a C# project, we add a hint reference. This
  4091. // reference is written to project file in
  4092. // WriteDotNetReferences().
  4093. this->DotNetHintReferences[config].push_back(
  4094. DotNetHintReference(l.Target->GetName(), location));
  4095. break;
  4096. case VsProjectType::vcxproj:
  4097. // Add path of assembly to list of using-directories, so the
  4098. // managed assembly can be used by '#using <assembly.dll>' in
  4099. // code.
  4100. this->AdditionalUsingDirectories[config].insert(
  4101. cmSystemTools::GetFilenamePath(location));
  4102. break;
  4103. default:
  4104. // In .proj files, we wouldn't be referencing libraries.
  4105. break;
  4106. }
  4107. }
  4108. }
  4109. // Do not allow C# targets to be added to the LIB listing. LIB files are
  4110. // used for linking C++ dependencies. C# libraries do not have lib files.
  4111. // Instead, they compile down to C# reference libraries (DLL files). The
  4112. // `<ProjectReference>` elements added to the vcxproj are enough for the
  4113. // IDE to deduce the DLL file required by other C# projects that need its
  4114. // reference library.
  4115. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  4116. continue;
  4117. }
  4118. }
  4119. if (l.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  4120. std::string path =
  4121. this->LocalGenerator->MaybeRelativeToCurBinDir(l.Value.Value);
  4122. ConvertToWindowsSlash(path);
  4123. if (cmVS10IsTargetsFile(l.Value.Value)) {
  4124. vsTargetVec.push_back(path);
  4125. } else {
  4126. libVec.push_back(l.HasFeature() ? l.GetFormattedItem(path).Value
  4127. : path);
  4128. }
  4129. } else if (!l.Target ||
  4130. l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4131. libVec.push_back(l.Value.Value);
  4132. }
  4133. }
  4134. }
  4135. void cmVisualStudio10TargetGenerator::AddTargetsFileAndConfigPair(
  4136. std::string const& targetsFile, std::string const& config)
  4137. {
  4138. for (TargetsFileAndConfigs& i : this->TargetsFileAndConfigsVec) {
  4139. if (cmSystemTools::ComparePath(targetsFile, i.File)) {
  4140. if (!cm::contains(i.Configs, config)) {
  4141. i.Configs.push_back(config);
  4142. }
  4143. return;
  4144. }
  4145. }
  4146. TargetsFileAndConfigs entry;
  4147. entry.File = targetsFile;
  4148. entry.Configs.push_back(config);
  4149. this->TargetsFileAndConfigsVec.push_back(entry);
  4150. }
  4151. void cmVisualStudio10TargetGenerator::WriteMidlOptions(
  4152. Elem& e1, std::string const& configName)
  4153. {
  4154. if (!this->MSTools) {
  4155. return;
  4156. }
  4157. if (this->ProjectType == VsProjectType::csproj) {
  4158. return;
  4159. }
  4160. if (this->GeneratorTarget->GetType() > cmStateEnums::UTILITY) {
  4161. return;
  4162. }
  4163. // This processes *any* of the .idl files specified in the project's file
  4164. // list (and passed as the item metadata %(Filename) expressing the rule
  4165. // input filename) into output files at the per-config *build* dir
  4166. // ($(IntDir)) each.
  4167. //
  4168. // IOW, this MIDL section is intended to provide a fully generic syntax
  4169. // content suitable for most cases (read: if you get errors, then it's quite
  4170. // probable that the error is on your side of the .idl setup).
  4171. //
  4172. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  4173. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  4174. // project's file list, otherwise the compiler-side processing won't pick it
  4175. // up (for non-directory form, it ends up looking in project binary dir
  4176. // only). Perhaps there's something to be done to make this more automatic
  4177. // on the CMake side?
  4178. std::vector<std::string> const includes =
  4179. this->GetIncludes(configName, "MIDL");
  4180. std::ostringstream oss;
  4181. for (std::string const& i : includes) {
  4182. oss << i << ";";
  4183. }
  4184. oss << "%(AdditionalIncludeDirectories)";
  4185. Elem e2(e1, "Midl");
  4186. e2.Element("AdditionalIncludeDirectories", oss.str());
  4187. e2.Element("OutputDirectory", "$(ProjectDir)/$(IntDir)");
  4188. e2.Element("HeaderFileName", "%(Filename).h");
  4189. e2.Element("TypeLibraryName", "%(Filename).tlb");
  4190. e2.Element("InterfaceIdentifierFileName", "%(Filename)_i.c");
  4191. e2.Element("ProxyFileName", "%(Filename)_p.c");
  4192. }
  4193. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups(Elem& e0)
  4194. {
  4195. if (this->ProjectType == VsProjectType::csproj) {
  4196. return;
  4197. }
  4198. for (const std::string& c : this->Configurations) {
  4199. Elem e1(e0, "ItemDefinitionGroup");
  4200. e1.Attribute("Condition", this->CalcCondition(c));
  4201. // output cl compile flags <ClCompile></ClCompile>
  4202. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  4203. this->WriteClOptions(e1, c);
  4204. // output rc compile flags <ResourceCompile></ResourceCompile>
  4205. this->WriteRCOptions(e1, c);
  4206. this->WriteCudaOptions(e1, c);
  4207. this->WriteMarmasmOptions(e1, c);
  4208. this->WriteMasmOptions(e1, c);
  4209. this->WriteNasmOptions(e1, c);
  4210. }
  4211. // output midl flags <Midl></Midl>
  4212. this->WriteMidlOptions(e1, c);
  4213. // write events
  4214. if (this->ProjectType != VsProjectType::csproj) {
  4215. this->WriteEvents(e1, c);
  4216. }
  4217. // output link flags <Link></Link>
  4218. this->WriteLinkOptions(e1, c);
  4219. this->WriteCudaLinkOptions(e1, c);
  4220. // output lib flags <Lib></Lib>
  4221. this->WriteLibOptions(e1, c);
  4222. // output manifest flags <Manifest></Manifest>
  4223. this->WriteManifestOptions(e1, c);
  4224. if (this->NsightTegra &&
  4225. this->GeneratorTarget->Target->IsAndroidGuiExecutable()) {
  4226. this->WriteAntBuildOptions(e1, c);
  4227. }
  4228. }
  4229. }
  4230. void cmVisualStudio10TargetGenerator::WriteEvents(
  4231. Elem& e1, std::string const& configName)
  4232. {
  4233. bool addedPrelink = false;
  4234. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  4235. this->GeneratorTarget->GetModuleDefinitionInfo(configName);
  4236. if (mdi && mdi->DefFileGenerated) {
  4237. addedPrelink = true;
  4238. std::vector<cmCustomCommand> commands =
  4239. this->GeneratorTarget->GetPreLinkCommands();
  4240. this->GlobalGenerator->AddSymbolExportCommand(this->GeneratorTarget,
  4241. commands, configName);
  4242. this->WriteEvent(e1, "PreLinkEvent", commands, configName);
  4243. }
  4244. if (!addedPrelink) {
  4245. this->WriteEvent(e1, "PreLinkEvent",
  4246. this->GeneratorTarget->GetPreLinkCommands(), configName);
  4247. }
  4248. this->WriteEvent(e1, "PreBuildEvent",
  4249. this->GeneratorTarget->GetPreBuildCommands(), configName);
  4250. this->WriteEvent(e1, "PostBuildEvent",
  4251. this->GeneratorTarget->GetPostBuildCommands(), configName);
  4252. }
  4253. void cmVisualStudio10TargetGenerator::WriteEvent(
  4254. Elem& e1, const std::string& name,
  4255. std::vector<cmCustomCommand> const& commands, std::string const& configName)
  4256. {
  4257. if (commands.empty()) {
  4258. return;
  4259. }
  4260. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  4261. std::string script;
  4262. const char* pre = "";
  4263. std::string comment;
  4264. bool stdPipesUTF8 = false;
  4265. for (cmCustomCommand const& cc : commands) {
  4266. cmCustomCommandGenerator ccg(cc, configName, lg);
  4267. if (!ccg.HasOnlyEmptyCommandLines()) {
  4268. comment += pre;
  4269. comment += lg->ConstructComment(ccg);
  4270. script += pre;
  4271. pre = "\n";
  4272. script += lg->ConstructScript(ccg);
  4273. stdPipesUTF8 = stdPipesUTF8 || cc.GetStdPipesUTF8();
  4274. }
  4275. }
  4276. if (!script.empty()) {
  4277. script += lg->FinishConstructScript(this->ProjectType);
  4278. }
  4279. comment = cmVS10EscapeComment(comment);
  4280. if (this->ProjectType != VsProjectType::csproj) {
  4281. Elem e2(e1, name);
  4282. if (stdPipesUTF8) {
  4283. this->WriteStdOutEncodingUtf8(e2);
  4284. }
  4285. e2.Element("Message", comment);
  4286. e2.Element("Command", script);
  4287. } else {
  4288. std::string strippedComment = comment;
  4289. strippedComment.erase(
  4290. std::remove(strippedComment.begin(), strippedComment.end(), '\t'),
  4291. strippedComment.end());
  4292. std::ostringstream oss;
  4293. if (!comment.empty() && !strippedComment.empty()) {
  4294. oss << "echo " << comment << "\n";
  4295. }
  4296. oss << script << "\n";
  4297. e1.Element(name, oss.str());
  4298. }
  4299. }
  4300. void cmVisualStudio10TargetGenerator::WriteProjectReferences(Elem& e0)
  4301. {
  4302. cmGlobalGenerator::TargetDependSet const& unordered =
  4303. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  4304. using OrderedTargetDependSet =
  4305. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  4306. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  4307. Elem e1(e0, "ItemGroup");
  4308. e1.SetHasElements();
  4309. for (cmGeneratorTarget const* dt : depends) {
  4310. if (!dt->IsInBuildSystem()) {
  4311. continue;
  4312. }
  4313. // skip fortran targets as they can not be processed by MSBuild
  4314. // the only reference will be in the .sln file
  4315. if (this->GlobalGenerator->TargetIsFortranOnly(dt)) {
  4316. continue;
  4317. }
  4318. cmLocalGenerator* lg = dt->GetLocalGenerator();
  4319. std::string name = dt->GetName();
  4320. std::string path;
  4321. if (cmValue p = dt->GetProperty("EXTERNAL_MSPROJECT")) {
  4322. path = *p;
  4323. } else {
  4324. path = cmStrCat(lg->GetCurrentBinaryDirectory(), '/', dt->GetName(),
  4325. computeProjectFileExtension(dt));
  4326. }
  4327. ConvertToWindowsSlash(path);
  4328. Elem e2(e1, "ProjectReference");
  4329. e2.Attribute("Include", path);
  4330. e2.Element("Project", "{" + this->GlobalGenerator->GetGUID(name) + "}");
  4331. e2.Element("Name", name);
  4332. this->WriteDotNetReferenceCustomTags(e2, name);
  4333. if (dt->IsCSharpOnly() || cmHasLiteralSuffix(path, "csproj")) {
  4334. e2.Element("SkipGetTargetFrameworkProperties", "true");
  4335. }
  4336. // Don't reference targets that don't produce any output.
  4337. else if (this->Configurations.empty() ||
  4338. dt->GetManagedType(this->Configurations[0]) ==
  4339. cmGeneratorTarget::ManagedType::Undefined) {
  4340. e2.Element("ReferenceOutputAssembly", "false");
  4341. e2.Element("CopyToOutputDirectory", "Never");
  4342. }
  4343. }
  4344. }
  4345. void cmVisualStudio10TargetGenerator::WritePlatformExtensions(Elem& e1)
  4346. {
  4347. // This only applies to Windows 10 apps
  4348. if (this->GlobalGenerator->TargetsWindowsStore() &&
  4349. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  4350. cmValue desktopExtensionsVersion =
  4351. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  4352. if (desktopExtensionsVersion) {
  4353. this->WriteSinglePlatformExtension(e1, "WindowsDesktop",
  4354. *desktopExtensionsVersion);
  4355. }
  4356. cmValue mobileExtensionsVersion =
  4357. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  4358. if (mobileExtensionsVersion) {
  4359. this->WriteSinglePlatformExtension(e1, "WindowsMobile",
  4360. *mobileExtensionsVersion);
  4361. }
  4362. }
  4363. }
  4364. void cmVisualStudio10TargetGenerator::WriteSinglePlatformExtension(
  4365. Elem& e1, std::string const& extension, std::string const& version)
  4366. {
  4367. const std::string s = "$([Microsoft.Build.Utilities.ToolLocationHelper]"
  4368. "::GetPlatformExtensionSDKLocation(`" +
  4369. extension + ", Version=" + version +
  4370. "`, $(TargetPlatformIdentifier), $(TargetPlatformVersion), null, "
  4371. "$(ExtensionSDKDirectoryRoot), null))"
  4372. "\\DesignTime\\CommonConfiguration\\Neutral\\" +
  4373. extension + ".props";
  4374. Elem e2(e1, "Import");
  4375. e2.Attribute("Project", s);
  4376. e2.Attribute("Condition", "exists('" + s + "')");
  4377. }
  4378. void cmVisualStudio10TargetGenerator::WriteSDKReferences(Elem& e0)
  4379. {
  4380. std::vector<std::string> sdkReferences;
  4381. std::unique_ptr<Elem> spe1;
  4382. if (cmValue vsSDKReferences =
  4383. this->GeneratorTarget->GetProperty("VS_SDK_REFERENCES")) {
  4384. cmExpandList(*vsSDKReferences, sdkReferences);
  4385. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  4386. for (std::string const& ri : sdkReferences) {
  4387. Elem(*spe1, "SDKReference").Attribute("Include", ri);
  4388. }
  4389. }
  4390. // This only applies to Windows 10 apps
  4391. if (this->GlobalGenerator->TargetsWindowsStore() &&
  4392. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  4393. cmValue desktopExtensionsVersion =
  4394. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  4395. cmValue mobileExtensionsVersion =
  4396. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  4397. cmValue iotExtensionsVersion =
  4398. this->GeneratorTarget->GetProperty("VS_IOT_EXTENSIONS_VERSION");
  4399. if (desktopExtensionsVersion || mobileExtensionsVersion ||
  4400. iotExtensionsVersion) {
  4401. if (!spe1) {
  4402. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  4403. }
  4404. if (desktopExtensionsVersion) {
  4405. this->WriteSingleSDKReference(*spe1, "WindowsDesktop",
  4406. *desktopExtensionsVersion);
  4407. }
  4408. if (mobileExtensionsVersion) {
  4409. this->WriteSingleSDKReference(*spe1, "WindowsMobile",
  4410. *mobileExtensionsVersion);
  4411. }
  4412. if (iotExtensionsVersion) {
  4413. this->WriteSingleSDKReference(*spe1, "WindowsIoT",
  4414. *iotExtensionsVersion);
  4415. }
  4416. }
  4417. }
  4418. }
  4419. void cmVisualStudio10TargetGenerator::WriteSingleSDKReference(
  4420. Elem& e1, std::string const& extension, std::string const& version)
  4421. {
  4422. Elem(e1, "SDKReference")
  4423. .Attribute("Include", extension + ", Version=" + version);
  4424. }
  4425. void cmVisualStudio10TargetGenerator::WriteWinRTPackageCertificateKeyFile(
  4426. Elem& e0)
  4427. {
  4428. if ((this->GlobalGenerator->TargetsWindowsStore() ||
  4429. this->GlobalGenerator->TargetsWindowsPhone()) &&
  4430. (cmStateEnums::EXECUTABLE == this->GeneratorTarget->GetType())) {
  4431. std::string pfxFile;
  4432. for (cmGeneratorTarget::AllConfigSource const& source :
  4433. this->GeneratorTarget->GetAllConfigSources()) {
  4434. if (source.Kind == cmGeneratorTarget::SourceKindCertificate) {
  4435. pfxFile = this->ConvertPath(source.Source->GetFullPath(), false);
  4436. ConvertToWindowsSlash(pfxFile);
  4437. break;
  4438. }
  4439. }
  4440. if (this->IsMissingFiles &&
  4441. !(this->GlobalGenerator->TargetsWindowsPhone() &&
  4442. this->GlobalGenerator->GetSystemVersion() == "8.0")) {
  4443. // Move the manifest to a project directory to avoid clashes
  4444. std::string artifactDir =
  4445. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4446. ConvertToWindowsSlash(artifactDir);
  4447. Elem e1(e0, "PropertyGroup");
  4448. e1.Element("AppxPackageArtifactsDir", artifactDir + "\\");
  4449. std::string resourcePriFile =
  4450. this->DefaultArtifactDir + "/resources.pri";
  4451. ConvertToWindowsSlash(resourcePriFile);
  4452. e1.Element("ProjectPriFullPath", resourcePriFile);
  4453. // If we are missing files and we don't have a certificate and
  4454. // aren't targeting WP8.0, add a default certificate
  4455. if (pfxFile.empty()) {
  4456. std::string templateFolder =
  4457. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4458. pfxFile = this->DefaultArtifactDir + "/Windows_TemporaryKey.pfx";
  4459. cmSystemTools::CopyAFile(templateFolder + "/Windows_TemporaryKey.pfx",
  4460. pfxFile, false);
  4461. ConvertToWindowsSlash(pfxFile);
  4462. this->AddedFiles.push_back(pfxFile);
  4463. this->AddedDefaultCertificate = true;
  4464. }
  4465. e1.Element("PackageCertificateKeyFile", pfxFile);
  4466. std::string thumb = cmSystemTools::ComputeCertificateThumbprint(pfxFile);
  4467. if (!thumb.empty()) {
  4468. e1.Element("PackageCertificateThumbprint", thumb);
  4469. }
  4470. } else if (!pfxFile.empty()) {
  4471. Elem e1(e0, "PropertyGroup");
  4472. e1.Element("PackageCertificateKeyFile", pfxFile);
  4473. std::string thumb = cmSystemTools::ComputeCertificateThumbprint(pfxFile);
  4474. if (!thumb.empty()) {
  4475. e1.Element("PackageCertificateThumbprint", thumb);
  4476. }
  4477. }
  4478. }
  4479. }
  4480. void cmVisualStudio10TargetGenerator::ClassifyAllConfigSources()
  4481. {
  4482. for (cmGeneratorTarget::AllConfigSource const& source :
  4483. this->GeneratorTarget->GetAllConfigSources()) {
  4484. this->ClassifyAllConfigSource(source);
  4485. }
  4486. }
  4487. void cmVisualStudio10TargetGenerator::ClassifyAllConfigSource(
  4488. cmGeneratorTarget::AllConfigSource const& acs)
  4489. {
  4490. switch (acs.Kind) {
  4491. case cmGeneratorTarget::SourceKindResx: {
  4492. // Build and save the name of the corresponding .h file
  4493. // This relationship will be used later when building the project files.
  4494. // Both names would have been auto generated from Visual Studio
  4495. // where the user supplied the file name and Visual Studio
  4496. // appended the suffix.
  4497. std::string resx = acs.Source->ResolveFullPath();
  4498. std::string hFileName = resx.substr(0, resx.find_last_of('.')) + ".h";
  4499. this->ExpectedResxHeaders.insert(hFileName);
  4500. } break;
  4501. case cmGeneratorTarget::SourceKindXaml: {
  4502. // Build and save the name of the corresponding .h and .cpp file
  4503. // This relationship will be used later when building the project files.
  4504. // Both names would have been auto generated from Visual Studio
  4505. // where the user supplied the file name and Visual Studio
  4506. // appended the suffix.
  4507. std::string xaml = acs.Source->ResolveFullPath();
  4508. std::string hFileName = xaml + ".h";
  4509. std::string cppFileName = xaml + ".cpp";
  4510. this->ExpectedXamlHeaders.insert(hFileName);
  4511. this->ExpectedXamlSources.insert(cppFileName);
  4512. } break;
  4513. default:
  4514. break;
  4515. }
  4516. }
  4517. bool cmVisualStudio10TargetGenerator::IsResxHeader(
  4518. const std::string& headerFile)
  4519. {
  4520. return this->ExpectedResxHeaders.count(headerFile) > 0;
  4521. }
  4522. bool cmVisualStudio10TargetGenerator::IsXamlHeader(
  4523. const std::string& headerFile)
  4524. {
  4525. return this->ExpectedXamlHeaders.count(headerFile) > 0;
  4526. }
  4527. bool cmVisualStudio10TargetGenerator::IsXamlSource(
  4528. const std::string& sourceFile)
  4529. {
  4530. return this->ExpectedXamlSources.count(sourceFile) > 0;
  4531. }
  4532. void cmVisualStudio10TargetGenerator::WriteApplicationTypeSettings(Elem& e1)
  4533. {
  4534. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  4535. bool isAppContainer = false;
  4536. bool const isWindowsPhone = this->GlobalGenerator->TargetsWindowsPhone();
  4537. bool const isWindowsStore = this->GlobalGenerator->TargetsWindowsStore();
  4538. bool const isAndroid = this->GlobalGenerator->TargetsAndroid();
  4539. std::string const& rev = this->GlobalGenerator->GetApplicationTypeRevision();
  4540. if (isWindowsPhone || isWindowsStore) {
  4541. e1.Element("ApplicationType",
  4542. (isWindowsPhone ? "Windows Phone" : "Windows Store"));
  4543. e1.Element("DefaultLanguage", "en-US");
  4544. if (rev == "10.0") {
  4545. e1.Element("ApplicationTypeRevision", rev);
  4546. // Visual Studio 14.0 is necessary for building 10.0 apps
  4547. e1.Element("MinimumVisualStudioVersion", "14.0");
  4548. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4549. isAppContainer = true;
  4550. }
  4551. } else if (rev == "8.1") {
  4552. e1.Element("ApplicationTypeRevision", rev);
  4553. // Visual Studio 12.0 is necessary for building 8.1 apps
  4554. e1.Element("MinimumVisualStudioVersion", "12.0");
  4555. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4556. isAppContainer = true;
  4557. }
  4558. } else if (rev == "8.0") {
  4559. e1.Element("ApplicationTypeRevision", rev);
  4560. // Visual Studio 11.0 is necessary for building 8.0 apps
  4561. e1.Element("MinimumVisualStudioVersion", "11.0");
  4562. if (isWindowsStore &&
  4563. this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4564. isAppContainer = true;
  4565. } else if (isWindowsPhone &&
  4566. this->GeneratorTarget->GetType() ==
  4567. cmStateEnums::EXECUTABLE) {
  4568. e1.Element("XapOutputs", "true");
  4569. e1.Element("XapFilename",
  4570. this->Name + "_$(Configuration)_$(Platform).xap");
  4571. }
  4572. }
  4573. } else if (isAndroid) {
  4574. e1.Element("ApplicationType", "Android");
  4575. e1.Element("ApplicationTypeRevision",
  4576. gg->GetAndroidApplicationTypeRevision());
  4577. }
  4578. if (isAppContainer) {
  4579. e1.Element("AppContainerApplication", "true");
  4580. } else if (!isAndroid) {
  4581. if (this->Platform == "ARM64") {
  4582. e1.Element("WindowsSDKDesktopARM64Support", "true");
  4583. } else if (this->Platform == "ARM") {
  4584. e1.Element("WindowsSDKDesktopARMSupport", "true");
  4585. }
  4586. }
  4587. std::string const& targetPlatformVersion =
  4588. gg->GetWindowsTargetPlatformVersion();
  4589. if (!targetPlatformVersion.empty()) {
  4590. e1.Element("WindowsTargetPlatformVersion", targetPlatformVersion);
  4591. }
  4592. cmValue targetPlatformMinVersion = this->GeneratorTarget->GetProperty(
  4593. "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION");
  4594. if (targetPlatformMinVersion) {
  4595. e1.Element("WindowsTargetPlatformMinVersion", *targetPlatformMinVersion);
  4596. } else if (isWindowsStore && rev == "10.0") {
  4597. // If the min version is not set, then use the TargetPlatformVersion
  4598. if (!targetPlatformVersion.empty()) {
  4599. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformVersion);
  4600. }
  4601. }
  4602. // Added IoT Startup Task support
  4603. if (this->GeneratorTarget->GetPropertyAsBool("VS_IOT_STARTUP_TASK")) {
  4604. e1.Element("ContainsStartupTask", "true");
  4605. }
  4606. }
  4607. void cmVisualStudio10TargetGenerator::VerifyNecessaryFiles()
  4608. {
  4609. // For Windows and Windows Phone executables, we will assume that if a
  4610. // manifest is not present that we need to add all the necessary files
  4611. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4612. std::vector<cmGeneratorTarget::AllConfigSource> manifestSources =
  4613. this->GeneratorTarget->GetAllConfigSources(
  4614. cmGeneratorTarget::SourceKindAppManifest);
  4615. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4616. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4617. if (v == "8.0") {
  4618. // Look through the sources for WMAppManifest.xml
  4619. bool foundManifest = false;
  4620. for (cmGeneratorTarget::AllConfigSource const& source :
  4621. this->GeneratorTarget->GetAllConfigSources()) {
  4622. if (source.Kind == cmGeneratorTarget::SourceKindExtra &&
  4623. "wmappmanifest.xml" ==
  4624. cmSystemTools::LowerCase(
  4625. source.Source->GetLocation().GetName())) {
  4626. foundManifest = true;
  4627. break;
  4628. }
  4629. }
  4630. if (!foundManifest) {
  4631. this->IsMissingFiles = true;
  4632. }
  4633. } else if (v == "8.1") {
  4634. if (manifestSources.empty()) {
  4635. this->IsMissingFiles = true;
  4636. }
  4637. }
  4638. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4639. if (manifestSources.empty()) {
  4640. if (v == "8.0") {
  4641. this->IsMissingFiles = true;
  4642. } else if (v == "8.1" || cmHasLiteralPrefix(v, "10.0")) {
  4643. this->IsMissingFiles = true;
  4644. }
  4645. }
  4646. }
  4647. }
  4648. }
  4649. void cmVisualStudio10TargetGenerator::WriteMissingFiles(Elem& e1)
  4650. {
  4651. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4652. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4653. if (v == "8.0") {
  4654. this->WriteMissingFilesWP80(e1);
  4655. } else if (v == "8.1") {
  4656. this->WriteMissingFilesWP81(e1);
  4657. }
  4658. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4659. if (v == "8.0") {
  4660. this->WriteMissingFilesWS80(e1);
  4661. } else if (v == "8.1") {
  4662. this->WriteMissingFilesWS81(e1);
  4663. } else if (cmHasLiteralPrefix(v, "10.0")) {
  4664. this->WriteMissingFilesWS10_0(e1);
  4665. }
  4666. }
  4667. }
  4668. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP80(Elem& e1)
  4669. {
  4670. std::string templateFolder =
  4671. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4672. // For WP80, the manifest needs to be in the same folder as the project
  4673. // this can cause an overwrite problem if projects aren't organized in
  4674. // folders
  4675. std::string manifestFile =
  4676. this->LocalGenerator->GetCurrentBinaryDirectory() + "/WMAppManifest.xml";
  4677. std::string artifactDir =
  4678. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4679. ConvertToWindowsSlash(artifactDir);
  4680. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4681. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4682. cmGeneratedFileStream fout(manifestFile);
  4683. fout.SetCopyIfDifferent(true);
  4684. /* clang-format off */
  4685. fout <<
  4686. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4687. "<Deployment"
  4688. " xmlns=\"http://schemas.microsoft.com/windowsphone/2012/deployment\""
  4689. " AppPlatformVersion=\"8.0\">\n"
  4690. "\t<DefaultLanguage xmlns=\"\" code=\"en-US\"/>\n"
  4691. "\t<App xmlns=\"\" ProductID=\"{" << this->GUID << "}\""
  4692. " Title=\"CMake Test Program\" RuntimeType=\"Modern Native\""
  4693. " Version=\"1.0.0.0\" Genre=\"apps.normal\" Author=\"CMake\""
  4694. " Description=\"Default CMake App\" Publisher=\"CMake\""
  4695. " PublisherID=\"{" << this->GUID << "}\">\n"
  4696. "\t\t<IconPath IsRelative=\"true\" IsResource=\"false\">"
  4697. << artifactDirXML << "\\ApplicationIcon.png</IconPath>\n"
  4698. "\t\t<Capabilities/>\n"
  4699. "\t\t<Tasks>\n"
  4700. "\t\t\t<DefaultTask Name=\"_default\""
  4701. " ImagePath=\"" << targetNameXML << ".exe\" ImageParams=\"\" />\n"
  4702. "\t\t</Tasks>\n"
  4703. "\t\t<Tokens>\n"
  4704. "\t\t\t<PrimaryToken TokenID=\"" << targetNameXML << "Token\""
  4705. " TaskName=\"_default\">\n"
  4706. "\t\t\t\t<TemplateFlip>\n"
  4707. "\t\t\t\t\t<SmallImageURI IsRelative=\"true\" IsResource=\"false\">"
  4708. << artifactDirXML << "\\SmallLogo.png</SmallImageURI>\n"
  4709. "\t\t\t\t\t<Count>0</Count>\n"
  4710. "\t\t\t\t\t<BackgroundImageURI IsRelative=\"true\" IsResource=\"false\">"
  4711. << artifactDirXML << "\\Logo.png</BackgroundImageURI>\n"
  4712. "\t\t\t\t</TemplateFlip>\n"
  4713. "\t\t\t</PrimaryToken>\n"
  4714. "\t\t</Tokens>\n"
  4715. "\t\t<ScreenResolutions>\n"
  4716. "\t\t\t<ScreenResolution Name=\"ID_RESOLUTION_WVGA\" />\n"
  4717. "\t\t</ScreenResolutions>\n"
  4718. "\t</App>\n"
  4719. "</Deployment>\n";
  4720. /* clang-format on */
  4721. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4722. ConvertToWindowsSlash(sourceFile);
  4723. {
  4724. Elem e2(e1, "Xml");
  4725. e2.Attribute("Include", sourceFile);
  4726. e2.Element("SubType", "Designer");
  4727. }
  4728. this->AddedFiles.push_back(sourceFile);
  4729. std::string smallLogo = this->DefaultArtifactDir + "/SmallLogo.png";
  4730. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo.png", smallLogo,
  4731. false);
  4732. ConvertToWindowsSlash(smallLogo);
  4733. Elem(e1, "Image").Attribute("Include", smallLogo);
  4734. this->AddedFiles.push_back(smallLogo);
  4735. std::string logo = this->DefaultArtifactDir + "/Logo.png";
  4736. cmSystemTools::CopyAFile(templateFolder + "/Logo.png", logo, false);
  4737. ConvertToWindowsSlash(logo);
  4738. Elem(e1, "Image").Attribute("Include", logo);
  4739. this->AddedFiles.push_back(logo);
  4740. std::string applicationIcon =
  4741. this->DefaultArtifactDir + "/ApplicationIcon.png";
  4742. cmSystemTools::CopyAFile(templateFolder + "/ApplicationIcon.png",
  4743. applicationIcon, false);
  4744. ConvertToWindowsSlash(applicationIcon);
  4745. Elem(e1, "Image").Attribute("Include", applicationIcon);
  4746. this->AddedFiles.push_back(applicationIcon);
  4747. }
  4748. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP81(Elem& e1)
  4749. {
  4750. std::string manifestFile =
  4751. this->DefaultArtifactDir + "/package.appxManifest";
  4752. std::string artifactDir =
  4753. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4754. ConvertToWindowsSlash(artifactDir);
  4755. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4756. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4757. cmGeneratedFileStream fout(manifestFile);
  4758. fout.SetCopyIfDifferent(true);
  4759. /* clang-format off */
  4760. fout <<
  4761. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4762. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4763. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\""
  4764. " xmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\">\n"
  4765. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4766. " Version=\"1.0.0.0\" />\n"
  4767. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID << "\""
  4768. " PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4769. "\t<Properties>\n"
  4770. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4771. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4772. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4773. "\t</Properties>\n"
  4774. "\t<Prerequisites>\n"
  4775. "\t\t<OSMinVersion>6.3.1</OSMinVersion>\n"
  4776. "\t\t<OSMaxVersionTested>6.3.1</OSMaxVersionTested>\n"
  4777. "\t</Prerequisites>\n"
  4778. "\t<Resources>\n"
  4779. "\t\t<Resource Language=\"x-generate\" />\n"
  4780. "\t</Resources>\n"
  4781. "\t<Applications>\n"
  4782. "\t\t<Application Id=\"App\""
  4783. " Executable=\"" << targetNameXML << ".exe\""
  4784. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4785. "\t\t\t<m2:VisualElements\n"
  4786. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4787. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4788. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4789. "\t\t\t\tForegroundText=\"light\"\n"
  4790. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4791. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4792. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4793. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4794. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4795. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4796. "\t\t\t\t</m2:DefaultTile>\n"
  4797. "\t\t\t\t<m2:SplashScreen"
  4798. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4799. "\t\t\t</m2:VisualElements>\n"
  4800. "\t\t</Application>\n"
  4801. "\t</Applications>\n"
  4802. "</Package>\n";
  4803. /* clang-format on */
  4804. this->WriteCommonMissingFiles(e1, manifestFile);
  4805. }
  4806. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS80(Elem& e1)
  4807. {
  4808. std::string manifestFile =
  4809. this->DefaultArtifactDir + "/package.appxManifest";
  4810. std::string artifactDir =
  4811. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4812. ConvertToWindowsSlash(artifactDir);
  4813. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4814. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4815. cmGeneratedFileStream fout(manifestFile);
  4816. fout.SetCopyIfDifferent(true);
  4817. /* clang-format off */
  4818. fout <<
  4819. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4820. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\">\n"
  4821. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4822. " Version=\"1.0.0.0\" />\n"
  4823. "\t<Properties>\n"
  4824. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4825. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4826. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4827. "\t</Properties>\n"
  4828. "\t<Prerequisites>\n"
  4829. "\t\t<OSMinVersion>6.2.1</OSMinVersion>\n"
  4830. "\t\t<OSMaxVersionTested>6.2.1</OSMaxVersionTested>\n"
  4831. "\t</Prerequisites>\n"
  4832. "\t<Resources>\n"
  4833. "\t\t<Resource Language=\"x-generate\" />\n"
  4834. "\t</Resources>\n"
  4835. "\t<Applications>\n"
  4836. "\t\t<Application Id=\"App\""
  4837. " Executable=\"" << targetNameXML << ".exe\""
  4838. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4839. "\t\t\t<VisualElements"
  4840. " DisplayName=\"" << targetNameXML << "\""
  4841. " Description=\"" << targetNameXML << "\""
  4842. " BackgroundColor=\"#336699\" ForegroundText=\"light\""
  4843. " Logo=\"" << artifactDirXML << "\\Logo.png\""
  4844. " SmallLogo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4845. "\t\t\t\t<DefaultTile ShowName=\"allLogos\""
  4846. " ShortName=\"" << targetNameXML << "\" />\n"
  4847. "\t\t\t\t<SplashScreen"
  4848. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4849. "\t\t\t</VisualElements>\n"
  4850. "\t\t</Application>\n"
  4851. "\t</Applications>\n"
  4852. "</Package>\n";
  4853. /* clang-format on */
  4854. this->WriteCommonMissingFiles(e1, manifestFile);
  4855. }
  4856. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS81(Elem& e1)
  4857. {
  4858. std::string manifestFile =
  4859. this->DefaultArtifactDir + "/package.appxManifest";
  4860. std::string artifactDir =
  4861. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4862. ConvertToWindowsSlash(artifactDir);
  4863. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4864. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4865. cmGeneratedFileStream fout(manifestFile);
  4866. fout.SetCopyIfDifferent(true);
  4867. /* clang-format off */
  4868. fout <<
  4869. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4870. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4871. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\">\n"
  4872. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4873. " Version=\"1.0.0.0\" />\n"
  4874. "\t<Properties>\n"
  4875. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4876. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4877. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4878. "\t</Properties>\n"
  4879. "\t<Prerequisites>\n"
  4880. "\t\t<OSMinVersion>6.3</OSMinVersion>\n"
  4881. "\t\t<OSMaxVersionTested>6.3</OSMaxVersionTested>\n"
  4882. "\t</Prerequisites>\n"
  4883. "\t<Resources>\n"
  4884. "\t\t<Resource Language=\"x-generate\" />\n"
  4885. "\t</Resources>\n"
  4886. "\t<Applications>\n"
  4887. "\t\t<Application Id=\"App\""
  4888. " Executable=\"" << targetNameXML << ".exe\""
  4889. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4890. "\t\t\t<m2:VisualElements\n"
  4891. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4892. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4893. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4894. "\t\t\t\tForegroundText=\"light\"\n"
  4895. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4896. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4897. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4898. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4899. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4900. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4901. "\t\t\t\t</m2:DefaultTile>\n"
  4902. "\t\t\t\t<m2:SplashScreen"
  4903. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4904. "\t\t\t</m2:VisualElements>\n"
  4905. "\t\t</Application>\n"
  4906. "\t</Applications>\n"
  4907. "</Package>\n";
  4908. /* clang-format on */
  4909. this->WriteCommonMissingFiles(e1, manifestFile);
  4910. }
  4911. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS10_0(Elem& e1)
  4912. {
  4913. std::string manifestFile =
  4914. this->DefaultArtifactDir + "/package.appxManifest";
  4915. std::string artifactDir =
  4916. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4917. ConvertToWindowsSlash(artifactDir);
  4918. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4919. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4920. cmGeneratedFileStream fout(manifestFile);
  4921. fout.SetCopyIfDifferent(true);
  4922. /* clang-format off */
  4923. fout <<
  4924. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4925. "<Package\n\t"
  4926. "xmlns=\"http://schemas.microsoft.com/appx/manifest/foundation/windows10\""
  4927. "\txmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\"\n"
  4928. "\txmlns:uap=\"http://schemas.microsoft.com/appx/manifest/uap/windows10\""
  4929. "\n\tIgnorableNamespaces=\"uap mp\">\n\n"
  4930. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4931. " Version=\"1.0.0.0\" />\n"
  4932. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID <<
  4933. "\" PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4934. "\t<Properties>\n"
  4935. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4936. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4937. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4938. "\t</Properties>\n"
  4939. "\t<Dependencies>\n"
  4940. "\t\t<TargetDeviceFamily Name=\"Windows.Universal\" "
  4941. "MinVersion=\"10.0.0.0\" MaxVersionTested=\"10.0.0.0\" />\n"
  4942. "\t</Dependencies>\n"
  4943. "\t<Resources>\n"
  4944. "\t\t<Resource Language=\"x-generate\" />\n"
  4945. "\t</Resources>\n"
  4946. "\t<Applications>\n"
  4947. "\t\t<Application Id=\"App\""
  4948. " Executable=\"" << targetNameXML << ".exe\""
  4949. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4950. "\t\t\t<uap:VisualElements\n"
  4951. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4952. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4953. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4954. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4955. "\t\t\t\tSquare44x44Logo=\"" << artifactDirXML <<
  4956. "\\SmallLogo44x44.png\">\n"
  4957. "\t\t\t\t<uap:SplashScreen"
  4958. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4959. "\t\t\t</uap:VisualElements>\n"
  4960. "\t\t</Application>\n"
  4961. "\t</Applications>\n"
  4962. "</Package>\n";
  4963. /* clang-format on */
  4964. this->WriteCommonMissingFiles(e1, manifestFile);
  4965. }
  4966. void cmVisualStudio10TargetGenerator::WriteCommonMissingFiles(
  4967. Elem& e1, const std::string& manifestFile)
  4968. {
  4969. std::string templateFolder =
  4970. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4971. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4972. ConvertToWindowsSlash(sourceFile);
  4973. {
  4974. Elem e2(e1, "AppxManifest");
  4975. e2.Attribute("Include", sourceFile);
  4976. e2.Element("SubType", "Designer");
  4977. }
  4978. this->AddedFiles.push_back(sourceFile);
  4979. std::string smallLogo = this->DefaultArtifactDir + "/SmallLogo.png";
  4980. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo.png", smallLogo,
  4981. false);
  4982. ConvertToWindowsSlash(smallLogo);
  4983. Elem(e1, "Image").Attribute("Include", smallLogo);
  4984. this->AddedFiles.push_back(smallLogo);
  4985. std::string smallLogo44 = this->DefaultArtifactDir + "/SmallLogo44x44.png";
  4986. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo44x44.png", smallLogo44,
  4987. false);
  4988. ConvertToWindowsSlash(smallLogo44);
  4989. Elem(e1, "Image").Attribute("Include", smallLogo44);
  4990. this->AddedFiles.push_back(smallLogo44);
  4991. std::string logo = this->DefaultArtifactDir + "/Logo.png";
  4992. cmSystemTools::CopyAFile(templateFolder + "/Logo.png", logo, false);
  4993. ConvertToWindowsSlash(logo);
  4994. Elem(e1, "Image").Attribute("Include", logo);
  4995. this->AddedFiles.push_back(logo);
  4996. std::string storeLogo = this->DefaultArtifactDir + "/StoreLogo.png";
  4997. cmSystemTools::CopyAFile(templateFolder + "/StoreLogo.png", storeLogo,
  4998. false);
  4999. ConvertToWindowsSlash(storeLogo);
  5000. Elem(e1, "Image").Attribute("Include", storeLogo);
  5001. this->AddedFiles.push_back(storeLogo);
  5002. std::string splashScreen = this->DefaultArtifactDir + "/SplashScreen.png";
  5003. cmSystemTools::CopyAFile(templateFolder + "/SplashScreen.png", splashScreen,
  5004. false);
  5005. ConvertToWindowsSlash(splashScreen);
  5006. Elem(e1, "Image").Attribute("Include", splashScreen);
  5007. this->AddedFiles.push_back(splashScreen);
  5008. if (this->AddedDefaultCertificate) {
  5009. // This file has already been added to the build so don't copy it
  5010. std::string keyFile =
  5011. this->DefaultArtifactDir + "/Windows_TemporaryKey.pfx";
  5012. ConvertToWindowsSlash(keyFile);
  5013. Elem(e1, "None").Attribute("Include", keyFile);
  5014. }
  5015. }
  5016. bool cmVisualStudio10TargetGenerator::ForceOld(const std::string& source) const
  5017. {
  5018. HANDLE h =
  5019. CreateFileW(cmSystemTools::ConvertToWindowsExtendedPath(source).c_str(),
  5020. FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING,
  5021. FILE_FLAG_BACKUP_SEMANTICS, 0);
  5022. if (!h) {
  5023. return false;
  5024. }
  5025. FILETIME const ftime_20010101 = { 3365781504u, 29389701u };
  5026. if (!SetFileTime(h, &ftime_20010101, &ftime_20010101, &ftime_20010101)) {
  5027. CloseHandle(h);
  5028. return false;
  5029. }
  5030. CloseHandle(h);
  5031. return true;
  5032. }
  5033. void cmVisualStudio10TargetGenerator::GetCSharpSourceProperties(
  5034. cmSourceFile const* sf, std::map<std::string, std::string>& tags)
  5035. {
  5036. if (this->ProjectType == VsProjectType::csproj) {
  5037. const cmPropertyMap& props = sf->GetProperties();
  5038. for (const std::string& p : props.GetKeys()) {
  5039. static const cm::string_view propNamePrefix = "VS_CSHARP_";
  5040. if (cmHasPrefix(p, propNamePrefix)) {
  5041. std::string tagName = p.substr(propNamePrefix.length());
  5042. if (!tagName.empty()) {
  5043. cmValue val = props.GetPropertyValue(p);
  5044. if (cmNonempty(val)) {
  5045. tags[tagName] = *val;
  5046. } else {
  5047. tags.erase(tagName);
  5048. }
  5049. }
  5050. }
  5051. }
  5052. }
  5053. }
  5054. void cmVisualStudio10TargetGenerator::WriteCSharpSourceProperties(
  5055. Elem& e2, const std::map<std::string, std::string>& tags)
  5056. {
  5057. for (const auto& i : tags) {
  5058. e2.Element(i.first, i.second);
  5059. }
  5060. }
  5061. std::string cmVisualStudio10TargetGenerator::GetCSharpSourceLink(
  5062. cmSourceFile const* source)
  5063. {
  5064. // For out of source files, we first check if a matching source group
  5065. // for this file exists, otherwise we check if the path relative to current
  5066. // source- or binary-dir is used within the link and return that.
  5067. // In case of .cs files we can't do that automatically for files in the
  5068. // binary directory, because this leads to compilation errors.
  5069. std::string link;
  5070. std::string sourceGroupedFile;
  5071. std::string const& fullFileName = source->GetFullPath();
  5072. std::string const& srcDir = this->Makefile->GetCurrentSourceDirectory();
  5073. std::string const& binDir = this->Makefile->GetCurrentBinaryDirectory();
  5074. // unfortunately we have to copy the source groups, because
  5075. // FindSourceGroup uses a regex which is modifying the group
  5076. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  5077. cmSourceGroup* sourceGroup =
  5078. this->Makefile->FindSourceGroup(fullFileName, sourceGroups);
  5079. if (sourceGroup && !sourceGroup->GetFullName().empty()) {
  5080. sourceGroupedFile = sourceGroup->GetFullName() + "/" +
  5081. cmsys::SystemTools::GetFilenameName(fullFileName);
  5082. cmsys::SystemTools::ConvertToUnixSlashes(sourceGroupedFile);
  5083. }
  5084. if (!sourceGroupedFile.empty() &&
  5085. cmHasSuffix(fullFileName, sourceGroupedFile)) {
  5086. link = sourceGroupedFile;
  5087. } else if (cmHasPrefix(fullFileName, srcDir)) {
  5088. link = fullFileName.substr(srcDir.length() + 1);
  5089. } else if (!cmHasSuffix(fullFileName, ".cs") &&
  5090. cmHasPrefix(fullFileName, binDir)) {
  5091. link = fullFileName.substr(binDir.length() + 1);
  5092. } else if (cmValue l = source->GetProperty("VS_CSHARP_Link")) {
  5093. link = *l;
  5094. }
  5095. ConvertToWindowsSlash(link);
  5096. return link;
  5097. }
  5098. std::string cmVisualStudio10TargetGenerator::GetCMakeFilePath(
  5099. const char* relativeFilePath) const
  5100. {
  5101. // Always search in the standard modules location.
  5102. std::string path =
  5103. cmStrCat(cmSystemTools::GetCMakeRoot(), '/', relativeFilePath);
  5104. ConvertToWindowsSlash(path);
  5105. return path;
  5106. }
  5107. void cmVisualStudio10TargetGenerator::WriteStdOutEncodingUtf8(Elem& e1)
  5108. {
  5109. if (this->GlobalGenerator->IsUtf8EncodingSupported()) {
  5110. e1.Element("UseUtf8Encoding", "Always");
  5111. } else if (this->GlobalGenerator->IsStdOutEncodingSupported()) {
  5112. e1.Element("StdOutEncoding", "UTF-8");
  5113. }
  5114. }
  5115. void cmVisualStudio10TargetGenerator::UpdateCache()
  5116. {
  5117. std::vector<std::string> packageReferences;
  5118. if (this->GeneratorTarget->HasPackageReferences()) {
  5119. // Store a cache entry that later determines, if a package restore is
  5120. // required.
  5121. this->GeneratorTarget->Makefile->AddCacheDefinition(
  5122. this->GeneratorTarget->GetName() + "_REQUIRES_VS_PACKAGE_RESTORE", "ON",
  5123. "Value Computed by CMake", cmStateEnums::STATIC);
  5124. } else {
  5125. // If there are any dependencies that require package restore, inherit the
  5126. // cache variable.
  5127. cmGlobalGenerator::TargetDependSet const& unordered =
  5128. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  5129. using OrderedTargetDependSet =
  5130. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  5131. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  5132. for (cmGeneratorTarget const* dt : depends) {
  5133. if (dt->HasPackageReferences()) {
  5134. this->GeneratorTarget->Makefile->AddCacheDefinition(
  5135. this->GeneratorTarget->GetName() + "_REQUIRES_VS_PACKAGE_RESTORE",
  5136. "ON", "Value Computed by CMake", cmStateEnums::STATIC);
  5137. }
  5138. }
  5139. }
  5140. }