cmVisualStudio10TargetGenerator.cxx 174 KB

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