cmVisualStudio10TargetGenerator.cxx 200 KB

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