cmVisualStudio10TargetGenerator.cxx 172 KB

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