cmVisualStudio10TargetGenerator.cxx 172 KB

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