cmVisualStudio10TargetGenerator.cxx 175 KB

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