cmVisualStudio10TargetGenerator.cxx 202 KB

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