cmVisualStudio10TargetGenerator.cxx 172 KB

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