cmVisualStudio10TargetGenerator.cxx 205 KB

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