cmVisualStudio10TargetGenerator.cxx 200 KB

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