cmVisualStudio10TargetGenerator.cxx 171 KB

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