cmVisualStudio10TargetGenerator.cxx 173 KB

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