cmVisualStudio10TargetGenerator.cxx 173 KB

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