cmVisualStudio10TargetGenerator.cxx 177 KB

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