cmVisualStudio10TargetGenerator.cxx 174 KB

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