cmVisualStudio10TargetGenerator.cxx 186 KB

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