1
0

cmVisualStudio10TargetGenerator.cxx 171 KB

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