cmVisualStudio10TargetGenerator.cxx 173 KB

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