cmVisualStudio10TargetGenerator.cxx 172 KB

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