cmVisualStudio10TargetGenerator.cxx 176 KB

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