cmVisualStudio10TargetGenerator.cxx 194 KB

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