cmVisualStudio10TargetGenerator.cxx 175 KB

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