cmVisualStudio10TargetGenerator.cxx 182 KB

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