cmVisualStudio10TargetGenerator.cxx 181 KB

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