cmVisualStudio10TargetGenerator.cxx 193 KB

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