cmVisualStudio10TargetGenerator.cxx 203 KB

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