cmVisualStudio10TargetGenerator.cxx 177 KB

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