cmVisualStudio10TargetGenerator.cxx 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897
  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.c_str());
  310. this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  311. ProjectFileExtension.c_str());
  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. case cmGeneratorTarget::SourceKindUnityBatched:
  2014. this->WriteHeaderSource(e1, si.Source);
  2015. break;
  2016. case cmGeneratorTarget::SourceKindIDL:
  2017. tool = "Midl";
  2018. break;
  2019. case cmGeneratorTarget::SourceKindManifest:
  2020. // Handled elsewhere.
  2021. break;
  2022. case cmGeneratorTarget::SourceKindModuleDefinition:
  2023. tool = "None";
  2024. break;
  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 (configDependentDefines) {
  2260. clOptions.AddDefines(
  2261. genexInterpreter.Evaluate(configDefines, "COMPILE_DEFINITIONS"));
  2262. } else {
  2263. clOptions.AddDefines(configDefines);
  2264. }
  2265. std::vector<std::string> includeList;
  2266. if (configDependentIncludes) {
  2267. this->LocalGenerator->AppendIncludeDirectories(
  2268. includeList,
  2269. genexInterpreter.Evaluate(includes, "INCLUDE_DIRECTORIES"), *source);
  2270. } else {
  2271. this->LocalGenerator->AppendIncludeDirectories(includeList, includes,
  2272. *source);
  2273. }
  2274. clOptions.AddIncludes(includeList);
  2275. clOptions.SetConfiguration(config);
  2276. OptionsHelper oh(clOptions, e2);
  2277. oh.PrependInheritedString("AdditionalOptions");
  2278. oh.OutputAdditionalIncludeDirectories(lang);
  2279. oh.OutputFlagMap();
  2280. oh.OutputPreprocessorDefinitions(lang);
  2281. }
  2282. }
  2283. if (this->IsXamlSource(source->GetFullPath())) {
  2284. const std::string& fileName = source->GetFullPath();
  2285. std::string xamlFileName = fileName.substr(0, fileName.find_last_of("."));
  2286. e2.Element("DependentUpon", xamlFileName);
  2287. }
  2288. if (this->ProjectType == csproj) {
  2289. std::string f = source->GetFullPath();
  2290. using CsPropMap = std::map<std::string, std::string>;
  2291. CsPropMap sourceFileTags;
  2292. // set <Link> tag if necessary
  2293. std::string link;
  2294. this->GetCSharpSourceLink(source, link);
  2295. if (!link.empty()) {
  2296. sourceFileTags["Link"] = link;
  2297. }
  2298. this->GetCSharpSourceProperties(&sf, sourceFileTags);
  2299. // write source file specific tags
  2300. if (!sourceFileTags.empty()) {
  2301. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2302. }
  2303. }
  2304. }
  2305. void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild(
  2306. Elem& e2, std::vector<size_t> const& exclude_configs)
  2307. {
  2308. for (size_t ci : exclude_configs) {
  2309. e2.WritePlatformConfigTag("ExcludedFromBuild",
  2310. "'$(Configuration)|$(Platform)'=='" +
  2311. this->Configurations[ci] + "|" +
  2312. this->Platform + "'",
  2313. "true");
  2314. }
  2315. }
  2316. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions(
  2317. Elem& e0)
  2318. {
  2319. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2320. if (ttype > cmStateEnums::GLOBAL_TARGET) {
  2321. return;
  2322. }
  2323. if (this->ProjectType == csproj) {
  2324. return;
  2325. }
  2326. Elem e1(e0, "PropertyGroup");
  2327. e1.Element("_ProjectFileVersion", "10.0.20506.1");
  2328. for (std::string const& config : this->Configurations) {
  2329. const std::string cond = this->CalcCondition(config);
  2330. if (ttype <= cmStateEnums::UTILITY) {
  2331. if (const char* workingDir = this->GeneratorTarget->GetProperty(
  2332. "VS_DEBUGGER_WORKING_DIRECTORY")) {
  2333. std::string genWorkingDir = cmGeneratorExpression::Evaluate(
  2334. workingDir, this->LocalGenerator, config);
  2335. e1.WritePlatformConfigTag("LocalDebuggerWorkingDirectory", cond,
  2336. genWorkingDir);
  2337. }
  2338. if (const char* environment =
  2339. this->GeneratorTarget->GetProperty("VS_DEBUGGER_ENVIRONMENT")) {
  2340. std::string genEnvironment = cmGeneratorExpression::Evaluate(
  2341. environment, this->LocalGenerator, config);
  2342. e1.WritePlatformConfigTag("LocalDebuggerEnvironment", cond,
  2343. genEnvironment);
  2344. }
  2345. if (const char* debuggerCommand =
  2346. this->GeneratorTarget->GetProperty("VS_DEBUGGER_COMMAND")) {
  2347. std::string genDebuggerCommand = cmGeneratorExpression::Evaluate(
  2348. debuggerCommand, this->LocalGenerator, config);
  2349. e1.WritePlatformConfigTag("LocalDebuggerCommand", cond,
  2350. genDebuggerCommand);
  2351. }
  2352. if (const char* commandArguments = this->GeneratorTarget->GetProperty(
  2353. "VS_DEBUGGER_COMMAND_ARGUMENTS")) {
  2354. std::string genCommandArguments = cmGeneratorExpression::Evaluate(
  2355. commandArguments, this->LocalGenerator, config);
  2356. e1.WritePlatformConfigTag("LocalDebuggerCommandArguments", cond,
  2357. genCommandArguments);
  2358. }
  2359. }
  2360. if (ttype >= cmStateEnums::UTILITY) {
  2361. e1.WritePlatformConfigTag(
  2362. "IntDir", cond, "$(Platform)\\$(Configuration)\\$(ProjectName)\\");
  2363. } else {
  2364. std::string intermediateDir = cmStrCat(
  2365. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  2366. config, '/');
  2367. std::string outDir;
  2368. std::string targetNameFull;
  2369. if (ttype == cmStateEnums::OBJECT_LIBRARY) {
  2370. outDir = intermediateDir;
  2371. targetNameFull = cmStrCat(this->GeneratorTarget->GetName(), ".lib");
  2372. } else {
  2373. outDir = this->GeneratorTarget->GetDirectory(config) + "/";
  2374. targetNameFull = this->GeneratorTarget->GetFullName(config);
  2375. }
  2376. ConvertToWindowsSlash(intermediateDir);
  2377. ConvertToWindowsSlash(outDir);
  2378. e1.WritePlatformConfigTag("OutDir", cond, outDir);
  2379. e1.WritePlatformConfigTag("IntDir", cond, intermediateDir);
  2380. if (const char* sdkExecutableDirectories = this->Makefile->GetDefinition(
  2381. "CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES")) {
  2382. e1.WritePlatformConfigTag("ExecutablePath", cond,
  2383. sdkExecutableDirectories);
  2384. }
  2385. if (const char* sdkIncludeDirectories = this->Makefile->GetDefinition(
  2386. "CMAKE_VS_SDK_INCLUDE_DIRECTORIES")) {
  2387. e1.WritePlatformConfigTag("IncludePath", cond, sdkIncludeDirectories);
  2388. }
  2389. if (const char* sdkReferenceDirectories = this->Makefile->GetDefinition(
  2390. "CMAKE_VS_SDK_REFERENCE_DIRECTORIES")) {
  2391. e1.WritePlatformConfigTag("ReferencePath", cond,
  2392. sdkReferenceDirectories);
  2393. }
  2394. if (const char* sdkLibraryDirectories = this->Makefile->GetDefinition(
  2395. "CMAKE_VS_SDK_LIBRARY_DIRECTORIES")) {
  2396. e1.WritePlatformConfigTag("LibraryPath", cond, sdkLibraryDirectories);
  2397. }
  2398. if (const char* sdkLibraryWDirectories = this->Makefile->GetDefinition(
  2399. "CMAKE_VS_SDK_LIBRARY_WINRT_DIRECTORIES")) {
  2400. e1.WritePlatformConfigTag("LibraryWPath", cond,
  2401. sdkLibraryWDirectories);
  2402. }
  2403. if (const char* sdkSourceDirectories =
  2404. this->Makefile->GetDefinition("CMAKE_VS_SDK_SOURCE_DIRECTORIES")) {
  2405. e1.WritePlatformConfigTag("SourcePath", cond, sdkSourceDirectories);
  2406. }
  2407. if (const char* sdkExcludeDirectories = this->Makefile->GetDefinition(
  2408. "CMAKE_VS_SDK_EXCLUDE_DIRECTORIES")) {
  2409. e1.WritePlatformConfigTag("ExcludePath", cond, sdkExcludeDirectories);
  2410. }
  2411. std::string name =
  2412. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  2413. e1.WritePlatformConfigTag("TargetName", cond, name);
  2414. std::string ext =
  2415. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  2416. if (ext.empty()) {
  2417. // An empty TargetExt causes a default extension to be used.
  2418. // A single "." appears to be treated as an empty extension.
  2419. ext = ".";
  2420. }
  2421. e1.WritePlatformConfigTag("TargetExt", cond, ext);
  2422. this->OutputLinkIncremental(e1, config);
  2423. }
  2424. }
  2425. }
  2426. void cmVisualStudio10TargetGenerator::OutputLinkIncremental(
  2427. Elem& e1, std::string const& configName)
  2428. {
  2429. if (!this->MSTools) {
  2430. return;
  2431. }
  2432. if (this->ProjectType == csproj) {
  2433. return;
  2434. }
  2435. // static libraries and things greater than modules do not need
  2436. // to set this option
  2437. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2438. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  2439. return;
  2440. }
  2441. Options& linkOptions = *(this->LinkOptions[configName]);
  2442. const std::string cond = this->CalcCondition(configName);
  2443. if (this->IPOEnabledConfigurations.count(configName) == 0) {
  2444. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  2445. e1.WritePlatformConfigTag("LinkIncremental", cond,
  2446. (incremental ? incremental : "true"));
  2447. }
  2448. linkOptions.RemoveFlag("LinkIncremental");
  2449. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  2450. e1.WritePlatformConfigTag("GenerateManifest", cond,
  2451. (manifest ? manifest : "true"));
  2452. linkOptions.RemoveFlag("GenerateManifest");
  2453. // Some link options belong here. Use them now and remove them so that
  2454. // WriteLinkOptions does not use them.
  2455. static const std::vector<std::string> flags{ "LinkDelaySign",
  2456. "LinkKeyFile" };
  2457. for (const std::string& flag : flags) {
  2458. if (const char* value = linkOptions.GetFlag(flag)) {
  2459. e1.WritePlatformConfigTag(flag, cond, value);
  2460. linkOptions.RemoveFlag(flag);
  2461. }
  2462. }
  2463. }
  2464. std::vector<std::string> cmVisualStudio10TargetGenerator::GetIncludes(
  2465. std::string const& config, std::string const& lang) const
  2466. {
  2467. std::vector<std::string> includes;
  2468. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  2469. lang, config);
  2470. for (std::string& i : includes) {
  2471. ConvertToWindowsSlash(i);
  2472. }
  2473. return includes;
  2474. }
  2475. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  2476. {
  2477. for (std::string const& c : this->Configurations) {
  2478. if (!this->ComputeClOptions(c)) {
  2479. return false;
  2480. }
  2481. }
  2482. return true;
  2483. }
  2484. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  2485. std::string const& configName)
  2486. {
  2487. // much of this was copied from here:
  2488. // copied from cmLocalVisualStudio7Generator.cxx 805
  2489. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  2490. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2491. std::unique_ptr<Options> pOptions;
  2492. switch (this->ProjectType) {
  2493. case vcxproj:
  2494. pOptions = cm::make_unique<Options>(
  2495. this->LocalGenerator, Options::Compiler, gg->GetClFlagTable());
  2496. break;
  2497. case csproj:
  2498. pOptions =
  2499. cm::make_unique<Options>(this->LocalGenerator, Options::CSharpCompiler,
  2500. gg->GetCSharpFlagTable());
  2501. break;
  2502. }
  2503. Options& clOptions = *pOptions;
  2504. std::string flags;
  2505. const std::string& linkLanguage =
  2506. this->GeneratorTarget->GetLinkerLanguage(configName);
  2507. if (linkLanguage.empty()) {
  2508. cmSystemTools::Error(
  2509. "CMake can not determine linker language for target: " + this->Name);
  2510. return false;
  2511. }
  2512. // Choose a language whose flags to use for ClCompile.
  2513. static const char* clLangs[] = { "CXX", "C", "Fortran" };
  2514. std::string langForClCompile;
  2515. if (this->ProjectType == csproj) {
  2516. langForClCompile = "CSharp";
  2517. } else if (cmContains(clLangs, linkLanguage)) {
  2518. langForClCompile = linkLanguage;
  2519. } else {
  2520. std::set<std::string> languages;
  2521. this->GeneratorTarget->GetLanguages(languages, configName);
  2522. for (const char* l : clLangs) {
  2523. if (languages.count(l)) {
  2524. langForClCompile = l;
  2525. break;
  2526. }
  2527. }
  2528. }
  2529. this->LangForClCompile = langForClCompile;
  2530. if (!langForClCompile.empty()) {
  2531. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  2532. langForClCompile, configName);
  2533. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  2534. langForClCompile, configName);
  2535. }
  2536. // set the correct language
  2537. if (linkLanguage == "C") {
  2538. clOptions.AddFlag("CompileAs", "CompileAsC");
  2539. }
  2540. if (linkLanguage == "CXX") {
  2541. clOptions.AddFlag("CompileAs", "CompileAsCpp");
  2542. }
  2543. // Put the IPO enabled configurations into a set.
  2544. if (this->GeneratorTarget->IsIPOEnabled(linkLanguage, configName)) {
  2545. this->IPOEnabledConfigurations.insert(configName);
  2546. }
  2547. // Precompile Headers
  2548. std::string pchHeader =
  2549. this->GeneratorTarget->GetPchHeader(configName, linkLanguage);
  2550. if (this->MSTools && vcxproj == this->ProjectType && pchHeader.empty()) {
  2551. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  2552. }
  2553. // Get preprocessor definitions for this directory.
  2554. std::string defineFlags = this->Makefile->GetDefineFlags();
  2555. if (this->MSTools) {
  2556. if (this->ProjectType == vcxproj) {
  2557. clOptions.FixExceptionHandlingDefault();
  2558. if (this->GlobalGenerator->GetVersion() >=
  2559. cmGlobalVisualStudioGenerator::VS15) {
  2560. // Toolsets that come with VS 2017 may now enable UseFullPaths
  2561. // by default and there is no negative /FC option that projects
  2562. // can use to switch it back. Older toolsets disable this by
  2563. // default anyway so this will not hurt them. If the project
  2564. // is using an explicit /FC option then parsing flags will
  2565. // replace this setting with "true" below.
  2566. clOptions.AddFlag("UseFullPaths", "false");
  2567. }
  2568. clOptions.AddFlag("AssemblerListingLocation", "$(IntDir)");
  2569. }
  2570. }
  2571. // check for managed C++ assembly compiler flag. This overrides any
  2572. // /clr* compiler flags which may be defined in the flags variable(s).
  2573. if (this->ProjectType != csproj) {
  2574. // Warn if /clr was added manually. This should not be done
  2575. // anymore, because cmGeneratorTarget may not be aware that the
  2576. // target uses C++/CLI.
  2577. if (flags.find("/clr") != std::string::npos ||
  2578. defineFlags.find("/clr") != std::string::npos) {
  2579. if (configName == this->Configurations[0]) {
  2580. std::string message = "For the target \"" +
  2581. this->GeneratorTarget->GetName() +
  2582. "\" the /clr compiler flag was added manually. " +
  2583. "Set usage of C++/CLI by setting COMMON_LANGUAGE_RUNTIME "
  2584. "target property.";
  2585. this->Makefile->IssueMessage(MessageType::WARNING, message);
  2586. }
  2587. }
  2588. if (auto* clr =
  2589. this->GeneratorTarget->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  2590. std::string clrString = clr;
  2591. if (!clrString.empty()) {
  2592. clrString = ":" + clrString;
  2593. }
  2594. flags += " /clr" + clrString;
  2595. }
  2596. }
  2597. clOptions.Parse(flags);
  2598. clOptions.Parse(defineFlags);
  2599. std::vector<std::string> targetDefines;
  2600. switch (this->ProjectType) {
  2601. case vcxproj:
  2602. if (!langForClCompile.empty()) {
  2603. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  2604. langForClCompile);
  2605. }
  2606. break;
  2607. case csproj:
  2608. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  2609. "CSharp");
  2610. break;
  2611. }
  2612. clOptions.AddDefines(targetDefines);
  2613. if (this->ProjectType == csproj) {
  2614. clOptions.AppendFlag("DefineConstants", targetDefines);
  2615. }
  2616. // Get includes for this target
  2617. if (!this->LangForClCompile.empty()) {
  2618. clOptions.AddIncludes(
  2619. this->GetIncludes(configName, this->LangForClCompile));
  2620. }
  2621. if (this->MSTools) {
  2622. clOptions.SetVerboseMakefile(
  2623. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  2624. }
  2625. // Add a definition for the configuration name.
  2626. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  2627. clOptions.AddDefine(configDefine);
  2628. if (const std::string* exportMacro =
  2629. this->GeneratorTarget->GetExportMacro()) {
  2630. clOptions.AddDefine(*exportMacro);
  2631. }
  2632. if (this->MSTools) {
  2633. // If we have the VS_WINRT_COMPONENT or CMAKE_VS_WINRT_BY_DEFAULT
  2634. // set then force Compile as WinRT.
  2635. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") ||
  2636. this->Makefile->IsOn("CMAKE_VS_WINRT_BY_DEFAULT")) {
  2637. clOptions.AddFlag("CompileAsWinRT", "true");
  2638. // For WinRT components, add the _WINRT_DLL define to produce a lib
  2639. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2640. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2641. clOptions.AddDefine("_WINRT_DLL");
  2642. }
  2643. } else if (this->GlobalGenerator->TargetsWindowsStore() ||
  2644. this->GlobalGenerator->TargetsWindowsPhone()) {
  2645. if (!clOptions.IsWinRt()) {
  2646. clOptions.AddFlag("CompileAsWinRT", "false");
  2647. }
  2648. }
  2649. if (const char* winRT = clOptions.GetFlag("CompileAsWinRT")) {
  2650. if (cmIsOn(winRT)) {
  2651. this->TargetCompileAsWinRT = true;
  2652. }
  2653. }
  2654. }
  2655. if (this->ProjectType != csproj && clOptions.IsManaged()) {
  2656. this->Managed = true;
  2657. std::string managedType = clOptions.GetFlag("CompileAsManaged");
  2658. if (managedType == "Safe" || managedType == "Pure") {
  2659. // force empty calling convention if safe clr is used
  2660. clOptions.AddFlag("CallingConvention", "");
  2661. }
  2662. // The default values of these flags are incompatible to
  2663. // managed assemblies. We have to force valid values if
  2664. // the target is a managed C++ target.
  2665. clOptions.AddFlag("ExceptionHandling", "Async");
  2666. clOptions.AddFlag("BasicRuntimeChecks", "Default");
  2667. }
  2668. if (this->ProjectType == csproj) {
  2669. // /nowin32manifest overrides /win32manifest: parameter
  2670. if (clOptions.HasFlag("NoWin32Manifest")) {
  2671. clOptions.RemoveFlag("ApplicationManifest");
  2672. }
  2673. }
  2674. if (const char* s = clOptions.GetFlag("SpectreMitigation")) {
  2675. this->SpectreMitigation[configName] = s;
  2676. clOptions.RemoveFlag("SpectreMitigation");
  2677. }
  2678. this->ClOptions[configName] = std::move(pOptions);
  2679. return true;
  2680. }
  2681. void cmVisualStudio10TargetGenerator::WriteClOptions(
  2682. Elem& e1, std::string const& configName)
  2683. {
  2684. Options& clOptions = *(this->ClOptions[configName]);
  2685. if (this->ProjectType == csproj) {
  2686. return;
  2687. }
  2688. Elem e2(e1, "ClCompile");
  2689. OptionsHelper oh(clOptions, e2);
  2690. oh.PrependInheritedString("AdditionalOptions");
  2691. oh.OutputAdditionalIncludeDirectories(this->LangForClCompile);
  2692. oh.OutputFlagMap();
  2693. oh.OutputPreprocessorDefinitions(this->LangForClCompile);
  2694. if (this->NsightTegra) {
  2695. if (const char* processMax =
  2696. this->GeneratorTarget->GetProperty("ANDROID_PROCESS_MAX")) {
  2697. e2.Element("ProcessMax", processMax);
  2698. }
  2699. }
  2700. if (this->MSTools) {
  2701. cmsys::RegularExpression clangToolset("v[0-9]+_clang_.*");
  2702. const char* toolset = this->GlobalGenerator->GetPlatformToolset();
  2703. if (toolset && clangToolset.find(toolset)) {
  2704. e2.Element("ObjectFileName", "$(IntDir)%(filename).obj");
  2705. } else {
  2706. e2.Element("ObjectFileName", "$(IntDir)");
  2707. }
  2708. // If not in debug mode, write the DebugInformationFormat field
  2709. // without value so PDBs don't get generated uselessly. Each tag
  2710. // goes on its own line because Visual Studio corrects it this
  2711. // way when saving the project after CMake generates it.
  2712. if (!clOptions.IsDebug()) {
  2713. Elem e3(e2, "DebugInformationFormat");
  2714. e3.SetHasElements();
  2715. }
  2716. // Specify the compiler program database file if configured.
  2717. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  2718. if (!pdb.empty()) {
  2719. if (this->GlobalGenerator->IsCudaEnabled()) {
  2720. // CUDA does not quote paths with spaces correctly when forwarding
  2721. // this to the host compiler. Use a relative path to avoid spaces.
  2722. // FIXME: We can likely do this even when CUDA is not involved,
  2723. // but for now we will make a minimal change.
  2724. pdb = this->ConvertPath(pdb, true);
  2725. }
  2726. ConvertToWindowsSlash(pdb);
  2727. e2.Element("ProgramDataBaseFileName", pdb);
  2728. }
  2729. // add AdditionalUsingDirectories
  2730. if (this->AdditionalUsingDirectories.count(configName) > 0) {
  2731. std::string dirs;
  2732. for (auto u : this->AdditionalUsingDirectories[configName]) {
  2733. if (!dirs.empty()) {
  2734. dirs.append(";");
  2735. }
  2736. dirs.append(u);
  2737. }
  2738. e2.Element("AdditionalUsingDirectories", dirs);
  2739. }
  2740. }
  2741. }
  2742. bool cmVisualStudio10TargetGenerator::ComputeRcOptions()
  2743. {
  2744. for (std::string const& c : this->Configurations) {
  2745. if (!this->ComputeRcOptions(c)) {
  2746. return false;
  2747. }
  2748. }
  2749. return true;
  2750. }
  2751. bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
  2752. std::string const& configName)
  2753. {
  2754. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2755. auto pOptions = cm::make_unique<Options>(
  2756. this->LocalGenerator, Options::ResourceCompiler, gg->GetRcFlagTable());
  2757. Options& rcOptions = *pOptions;
  2758. std::string CONFIG = cmSystemTools::UpperCase(configName);
  2759. std::string rcConfigFlagsVar = "CMAKE_RC_FLAGS_" + CONFIG;
  2760. std::string flags = this->Makefile->GetSafeDefinition("CMAKE_RC_FLAGS") +
  2761. " " + this->Makefile->GetSafeDefinition(rcConfigFlagsVar);
  2762. rcOptions.Parse(flags);
  2763. // For historical reasons, add the C preprocessor defines to RC.
  2764. Options& clOptions = *(this->ClOptions[configName]);
  2765. rcOptions.AddDefines(clOptions.GetDefines());
  2766. // Get includes for this target
  2767. rcOptions.AddIncludes(this->GetIncludes(configName, "RC"));
  2768. this->RcOptions[configName] = std::move(pOptions);
  2769. return true;
  2770. }
  2771. void cmVisualStudio10TargetGenerator::WriteRCOptions(
  2772. Elem& e1, std::string const& configName)
  2773. {
  2774. if (!this->MSTools) {
  2775. return;
  2776. }
  2777. Elem e2(e1, "ResourceCompile");
  2778. OptionsHelper rcOptions(*(this->RcOptions[configName]), e2);
  2779. rcOptions.OutputPreprocessorDefinitions("RC");
  2780. rcOptions.OutputAdditionalIncludeDirectories("RC");
  2781. rcOptions.PrependInheritedString("AdditionalOptions");
  2782. rcOptions.OutputFlagMap();
  2783. }
  2784. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions()
  2785. {
  2786. if (!this->GlobalGenerator->IsCudaEnabled()) {
  2787. return true;
  2788. }
  2789. for (std::string const& c : this->Configurations) {
  2790. if (!this->ComputeCudaOptions(c)) {
  2791. return false;
  2792. }
  2793. }
  2794. return true;
  2795. }
  2796. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
  2797. std::string const& configName)
  2798. {
  2799. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2800. auto pOptions = cm::make_unique<Options>(
  2801. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  2802. Options& cudaOptions = *pOptions;
  2803. // Get compile flags for CUDA in this directory.
  2804. std::string flags;
  2805. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget, "CUDA",
  2806. configName);
  2807. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, "CUDA",
  2808. configName);
  2809. // Get preprocessor definitions for this directory.
  2810. std::string defineFlags = this->Makefile->GetDefineFlags();
  2811. cudaOptions.Parse(flags);
  2812. cudaOptions.Parse(defineFlags);
  2813. cudaOptions.ParseFinish();
  2814. // If we haven't explicitly enabled GPU debug information
  2815. // explicitly disable it
  2816. if (!cudaOptions.HasFlag("GPUDebugInfo")) {
  2817. cudaOptions.AddFlag("GPUDebugInfo", "false");
  2818. }
  2819. // The extension on object libraries the CUDA gives isn't
  2820. // consistent with how MSVC generates object libraries for C+, so set
  2821. // the default to not have any extension
  2822. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).obj");
  2823. bool notPtx = true;
  2824. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  2825. cudaOptions.AddFlag("GenerateRelocatableDeviceCode", "true");
  2826. } else if (this->GeneratorTarget->GetPropertyAsBool(
  2827. "CUDA_PTX_COMPILATION")) {
  2828. cudaOptions.AddFlag("NvccCompilation", "ptx");
  2829. // We drop the %(Extension) component as CMake expects all PTX files
  2830. // to not have the source file extension at all
  2831. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).ptx");
  2832. notPtx = false;
  2833. }
  2834. if (notPtx &&
  2835. cmSystemTools::VersionCompareGreaterEq(
  2836. "8.0", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  2837. // Explicitly state that we want this file to be treated as a
  2838. // CUDA file no matter what the file extensions is
  2839. // This is only needed for < CUDA 9
  2840. cudaOptions.AppendFlagString("AdditionalOptions", "-x cu");
  2841. }
  2842. // Specify the compiler program database file if configured.
  2843. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  2844. if (!pdb.empty()) {
  2845. // CUDA does not make the directory if it is non-standard.
  2846. std::string const pdbDir = cmSystemTools::GetFilenamePath(pdb);
  2847. cmSystemTools::MakeDirectory(pdbDir);
  2848. if (cmSystemTools::VersionCompareGreaterEq(
  2849. "9.2", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  2850. // CUDA does not have a field for this and does not honor the
  2851. // ProgramDataBaseFileName field in ClCompile. Work around this
  2852. // limitation by creating the directory and passing the flag ourselves.
  2853. pdb = this->ConvertPath(pdb, true);
  2854. ConvertToWindowsSlash(pdb);
  2855. std::string const clFd = "-Xcompiler=\"-Fd\\\"" + pdb + "\\\"\"";
  2856. cudaOptions.AppendFlagString("AdditionalOptions", clFd);
  2857. }
  2858. }
  2859. // CUDA automatically passes the proper '--machine' flag to nvcc
  2860. // for the current architecture, but does not reflect this default
  2861. // in the user-visible IDE settings. Set it explicitly.
  2862. if (this->Platform == "x64") {
  2863. cudaOptions.AddFlag("TargetMachinePlatform", "64");
  2864. }
  2865. // Convert the host compiler options to the toolset's abstractions
  2866. // using a secondary flag table.
  2867. cudaOptions.ClearTables();
  2868. cudaOptions.AddTable(gg->GetCudaHostFlagTable());
  2869. cudaOptions.Reparse("AdditionalCompilerOptions");
  2870. // `CUDA 8.0.targets` places AdditionalCompilerOptions before nvcc!
  2871. // Pass them through -Xcompiler in AdditionalOptions instead.
  2872. if (const char* acoPtr = cudaOptions.GetFlag("AdditionalCompilerOptions")) {
  2873. std::string aco = acoPtr;
  2874. cudaOptions.RemoveFlag("AdditionalCompilerOptions");
  2875. if (!aco.empty()) {
  2876. aco = this->LocalGenerator->EscapeForShell(aco, false);
  2877. cudaOptions.AppendFlagString("AdditionalOptions", "-Xcompiler=" + aco);
  2878. }
  2879. }
  2880. cudaOptions.FixCudaCodeGeneration();
  2881. std::vector<std::string> targetDefines;
  2882. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  2883. "CUDA");
  2884. cudaOptions.AddDefines(targetDefines);
  2885. // Add a definition for the configuration name.
  2886. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  2887. cudaOptions.AddDefine(configDefine);
  2888. if (const std::string* exportMacro =
  2889. this->GeneratorTarget->GetExportMacro()) {
  2890. cudaOptions.AddDefine(*exportMacro);
  2891. }
  2892. // Get includes for this target
  2893. cudaOptions.AddIncludes(this->GetIncludes(configName, "CUDA"));
  2894. cudaOptions.AddFlag("UseHostInclude", "false");
  2895. this->CudaOptions[configName] = std::move(pOptions);
  2896. return true;
  2897. }
  2898. void cmVisualStudio10TargetGenerator::WriteCudaOptions(
  2899. Elem& e1, std::string const& configName)
  2900. {
  2901. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  2902. return;
  2903. }
  2904. Elem e2(e1, "CudaCompile");
  2905. OptionsHelper cudaOptions(*(this->CudaOptions[configName]), e2);
  2906. cudaOptions.OutputAdditionalIncludeDirectories("CUDA");
  2907. cudaOptions.OutputPreprocessorDefinitions("CUDA");
  2908. cudaOptions.PrependInheritedString("AdditionalOptions");
  2909. cudaOptions.OutputFlagMap();
  2910. }
  2911. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions()
  2912. {
  2913. if (!this->GlobalGenerator->IsCudaEnabled()) {
  2914. return true;
  2915. }
  2916. for (std::string const& c : this->Configurations) {
  2917. if (!this->ComputeCudaLinkOptions(c)) {
  2918. return false;
  2919. }
  2920. }
  2921. return true;
  2922. }
  2923. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions(
  2924. std::string const& configName)
  2925. {
  2926. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2927. auto pOptions = cm::make_unique<Options>(
  2928. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  2929. Options& cudaLinkOptions = *pOptions;
  2930. // Determine if we need to do a device link
  2931. const bool doDeviceLinking = requireDeviceLinking(
  2932. *this->GeneratorTarget, *this->LocalGenerator, configName);
  2933. cudaLinkOptions.AddFlag("PerformDeviceLink",
  2934. doDeviceLinking ? "true" : "false");
  2935. // Suppress deprecation warnings for default GPU targets during device link.
  2936. if (cmSystemTools::VersionCompareGreaterEq(
  2937. this->GlobalGenerator->GetPlatformToolsetCudaString(), "8.0")) {
  2938. cudaLinkOptions.AppendFlagString("AdditionalOptions",
  2939. "-Wno-deprecated-gpu-targets");
  2940. }
  2941. // For static libraries that have device linking enabled compute
  2942. // the libraries
  2943. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY &&
  2944. doDeviceLinking) {
  2945. cmComputeLinkInformation* pcli =
  2946. this->GeneratorTarget->GetLinkInformation(configName);
  2947. if (!pcli) {
  2948. cmSystemTools::Error(
  2949. "CMake can not compute cmComputeLinkInformation for target: " +
  2950. this->Name);
  2951. return false;
  2952. }
  2953. // Would like to use:
  2954. // cmLinkLineDeviceComputer computer(this->LocalGenerator,
  2955. // this->LocalGenerator->GetStateSnapshot().GetDirectory());
  2956. // std::string computed_libs = computer.ComputeLinkLibraries(cli,
  2957. // std::string{}); but it outputs in "<libA> <libB>" format instead of
  2958. // "<libA>;<libB>"
  2959. // Note:
  2960. // Any modification of this algorithm should be reflected also in
  2961. // cmLinkLineDeviceComputer
  2962. cmComputeLinkInformation& cli = *pcli;
  2963. std::vector<std::string> libVec;
  2964. const std::string currentBinDir =
  2965. this->LocalGenerator->GetCurrentBinaryDirectory();
  2966. const auto& libs = cli.GetItems();
  2967. for (cmComputeLinkInformation::Item const& l : libs) {
  2968. if (l.Target) {
  2969. auto managedType = l.Target->GetManagedType(configName);
  2970. // Do not allow C# targets to be added to the LIB listing. LIB files
  2971. // are used for linking C++ dependencies. C# libraries do not have lib
  2972. // files. Instead, they compile down to C# reference libraries (DLL
  2973. // files). The
  2974. // `<ProjectReference>` elements added to the vcxproj are enough for
  2975. // the IDE to deduce the DLL file required by other C# projects that
  2976. // need its reference library.
  2977. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  2978. continue;
  2979. }
  2980. const auto type = l.Target->GetType();
  2981. bool skip = false;
  2982. switch (type) {
  2983. case cmStateEnums::SHARED_LIBRARY:
  2984. case cmStateEnums::MODULE_LIBRARY:
  2985. case cmStateEnums::INTERFACE_LIBRARY:
  2986. skip = true;
  2987. break;
  2988. case cmStateEnums::STATIC_LIBRARY:
  2989. skip = l.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
  2990. break;
  2991. default:
  2992. break;
  2993. }
  2994. if (skip) {
  2995. continue;
  2996. }
  2997. }
  2998. if (l.IsPath) {
  2999. std::string path = this->LocalGenerator->MaybeConvertToRelativePath(
  3000. currentBinDir, l.Value);
  3001. ConvertToWindowsSlash(path);
  3002. if (!cmVS10IsTargetsFile(l.Value)) {
  3003. libVec.push_back(path);
  3004. }
  3005. } else {
  3006. libVec.push_back(l.Value);
  3007. }
  3008. }
  3009. cudaLinkOptions.AddFlag("AdditionalDependencies", libVec);
  3010. }
  3011. this->CudaLinkOptions[configName] = std::move(pOptions);
  3012. return true;
  3013. }
  3014. void cmVisualStudio10TargetGenerator::WriteCudaLinkOptions(
  3015. Elem& e1, std::string const& configName)
  3016. {
  3017. if (this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  3018. return;
  3019. }
  3020. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  3021. return;
  3022. }
  3023. Elem e2(e1, "CudaLink");
  3024. OptionsHelper cudaLinkOptions(*(this->CudaLinkOptions[configName]), e2);
  3025. cudaLinkOptions.OutputFlagMap();
  3026. }
  3027. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions()
  3028. {
  3029. if (!this->GlobalGenerator->IsMasmEnabled()) {
  3030. return true;
  3031. }
  3032. for (std::string const& c : this->Configurations) {
  3033. if (!this->ComputeMasmOptions(c)) {
  3034. return false;
  3035. }
  3036. }
  3037. return true;
  3038. }
  3039. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions(
  3040. std::string const& configName)
  3041. {
  3042. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3043. auto pOptions = cm::make_unique<Options>(
  3044. this->LocalGenerator, Options::MasmCompiler, gg->GetMasmFlagTable());
  3045. Options& masmOptions = *pOptions;
  3046. std::string flags;
  3047. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3048. "ASM_MASM", configName);
  3049. masmOptions.Parse(flags);
  3050. // Get includes for this target
  3051. masmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MASM"));
  3052. this->MasmOptions[configName] = std::move(pOptions);
  3053. return true;
  3054. }
  3055. void cmVisualStudio10TargetGenerator::WriteMasmOptions(
  3056. Elem& e1, std::string const& configName)
  3057. {
  3058. if (!this->MSTools || !this->GlobalGenerator->IsMasmEnabled()) {
  3059. return;
  3060. }
  3061. Elem e2(e1, "MASM");
  3062. // Preprocessor definitions and includes are shared with clOptions.
  3063. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3064. clOptions.OutputPreprocessorDefinitions("ASM_MASM");
  3065. OptionsHelper masmOptions(*(this->MasmOptions[configName]), e2);
  3066. masmOptions.OutputAdditionalIncludeDirectories("ASM_MASM");
  3067. masmOptions.PrependInheritedString("AdditionalOptions");
  3068. masmOptions.OutputFlagMap();
  3069. }
  3070. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions()
  3071. {
  3072. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3073. return true;
  3074. }
  3075. for (std::string const& c : this->Configurations) {
  3076. if (!this->ComputeNasmOptions(c)) {
  3077. return false;
  3078. }
  3079. }
  3080. return true;
  3081. }
  3082. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions(
  3083. std::string const& configName)
  3084. {
  3085. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3086. auto pOptions = cm::make_unique<Options>(
  3087. this->LocalGenerator, Options::NasmCompiler, gg->GetNasmFlagTable());
  3088. Options& nasmOptions = *pOptions;
  3089. std::string flags;
  3090. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3091. "ASM_NASM", configName);
  3092. flags += " -f";
  3093. flags += this->Makefile->GetSafeDefinition("CMAKE_ASM_NASM_OBJECT_FORMAT");
  3094. nasmOptions.Parse(flags);
  3095. // Get includes for this target
  3096. nasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_NASM"));
  3097. this->NasmOptions[configName] = std::move(pOptions);
  3098. return true;
  3099. }
  3100. void cmVisualStudio10TargetGenerator::WriteNasmOptions(
  3101. Elem& e1, std::string const& configName)
  3102. {
  3103. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3104. return;
  3105. }
  3106. Elem e2(e1, "NASM");
  3107. std::vector<std::string> includes =
  3108. this->GetIncludes(configName, "ASM_NASM");
  3109. OptionsHelper nasmOptions(*(this->NasmOptions[configName]), e2);
  3110. nasmOptions.OutputAdditionalIncludeDirectories("ASM_NASM");
  3111. nasmOptions.OutputFlagMap();
  3112. nasmOptions.PrependInheritedString("AdditionalOptions");
  3113. nasmOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3114. // Preprocessor definitions and includes are shared with clOptions.
  3115. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3116. clOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3117. }
  3118. void cmVisualStudio10TargetGenerator::WriteLibOptions(
  3119. Elem& e1, std::string const& config)
  3120. {
  3121. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3122. this->GeneratorTarget->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  3123. return;
  3124. }
  3125. const std::string& linkLanguage =
  3126. this->GeneratorTarget->GetLinkClosure(config)->LinkerLanguage;
  3127. std::string libflags;
  3128. this->LocalGenerator->GetStaticLibraryFlags(
  3129. libflags, cmSystemTools::UpperCase(config), linkLanguage,
  3130. this->GeneratorTarget);
  3131. if (!libflags.empty()) {
  3132. Elem e2(e1, "Lib");
  3133. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3134. cmVS10GeneratorOptions libOptions(this->LocalGenerator,
  3135. cmVisualStudioGeneratorOptions::Linker,
  3136. gg->GetLibFlagTable(), this);
  3137. libOptions.Parse(libflags);
  3138. OptionsHelper oh(libOptions, e2);
  3139. oh.PrependInheritedString("AdditionalOptions");
  3140. oh.OutputFlagMap();
  3141. }
  3142. // We cannot generate metadata for static libraries. WindowsPhone
  3143. // and WindowsStore tools look at GenerateWindowsMetadata in the
  3144. // Link tool options even for static libraries.
  3145. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3146. this->GlobalGenerator->TargetsWindowsStore()) {
  3147. Elem e2(e1, "Link");
  3148. e2.Element("GenerateWindowsMetadata", "false");
  3149. }
  3150. }
  3151. void cmVisualStudio10TargetGenerator::WriteManifestOptions(
  3152. Elem& e1, std::string const& config)
  3153. {
  3154. if (this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE &&
  3155. this->GeneratorTarget->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3156. this->GeneratorTarget->GetType() != cmStateEnums::MODULE_LIBRARY) {
  3157. return;
  3158. }
  3159. std::vector<cmSourceFile const*> manifest_srcs;
  3160. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  3161. const char* dpiAware = this->GeneratorTarget->GetProperty("VS_DPI_AWARE");
  3162. if (!manifest_srcs.empty() || dpiAware) {
  3163. Elem e2(e1, "Manifest");
  3164. if (!manifest_srcs.empty()) {
  3165. std::ostringstream oss;
  3166. for (cmSourceFile const* mi : manifest_srcs) {
  3167. std::string m = this->ConvertPath(mi->GetFullPath(), false);
  3168. ConvertToWindowsSlash(m);
  3169. oss << m << ";";
  3170. }
  3171. e2.Element("AdditionalManifestFiles", oss.str());
  3172. }
  3173. if (dpiAware) {
  3174. if (!strcmp(dpiAware, "PerMonitor")) {
  3175. e2.Element("EnableDpiAwareness", "PerMonitorHighDPIAware");
  3176. } else if (cmIsOn(dpiAware)) {
  3177. e2.Element("EnableDpiAwareness", "true");
  3178. } else if (cmIsOff(dpiAware)) {
  3179. e2.Element("EnableDpiAwareness", "false");
  3180. } else {
  3181. cmSystemTools::Error("Bad parameter for VS_DPI_AWARE: " +
  3182. std::string(dpiAware));
  3183. }
  3184. }
  3185. }
  3186. }
  3187. void cmVisualStudio10TargetGenerator::WriteAntBuildOptions(
  3188. Elem& e1, std::string const& configName)
  3189. {
  3190. // Look through the sources for AndroidManifest.xml and use
  3191. // its location as the root source directory.
  3192. std::string rootDir = this->LocalGenerator->GetCurrentSourceDirectory();
  3193. {
  3194. std::vector<cmSourceFile const*> extraSources;
  3195. this->GeneratorTarget->GetExtraSources(extraSources, "");
  3196. for (cmSourceFile const* si : extraSources) {
  3197. if ("androidmanifest.xml" ==
  3198. cmSystemTools::LowerCase(si->GetLocation().GetName())) {
  3199. rootDir = si->GetLocation().GetDirectory();
  3200. break;
  3201. }
  3202. }
  3203. }
  3204. // Tell MSBuild to launch Ant.
  3205. Elem e2(e1, "AntBuild");
  3206. {
  3207. std::string antBuildPath = rootDir;
  3208. ConvertToWindowsSlash(antBuildPath);
  3209. e2.Element("AntBuildPath", antBuildPath);
  3210. }
  3211. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_SKIP_ANT_STEP")) {
  3212. e2.Element("SkipAntStep", "true");
  3213. }
  3214. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_PROGUARD")) {
  3215. e2.Element("EnableProGuard", "true");
  3216. }
  3217. if (const char* proGuardConfigLocation =
  3218. this->GeneratorTarget->GetProperty("ANDROID_PROGUARD_CONFIG_PATH")) {
  3219. e2.Element("ProGuardConfigLocation", proGuardConfigLocation);
  3220. }
  3221. if (const char* securePropertiesLocation =
  3222. this->GeneratorTarget->GetProperty("ANDROID_SECURE_PROPS_PATH")) {
  3223. e2.Element("SecurePropertiesLocation", securePropertiesLocation);
  3224. }
  3225. if (const char* nativeLibDirectoriesExpression =
  3226. this->GeneratorTarget->GetProperty("ANDROID_NATIVE_LIB_DIRECTORIES")) {
  3227. std::string nativeLibDirs = cmGeneratorExpression::Evaluate(
  3228. nativeLibDirectoriesExpression, this->LocalGenerator, configName);
  3229. e2.Element("NativeLibDirectories", nativeLibDirs);
  3230. }
  3231. if (const char* nativeLibDependenciesExpression =
  3232. this->GeneratorTarget->GetProperty(
  3233. "ANDROID_NATIVE_LIB_DEPENDENCIES")) {
  3234. std::string nativeLibDeps = cmGeneratorExpression::Evaluate(
  3235. nativeLibDependenciesExpression, this->LocalGenerator, configName);
  3236. e2.Element("NativeLibDependencies", nativeLibDeps);
  3237. }
  3238. if (const char* javaSourceDir =
  3239. this->GeneratorTarget->GetProperty("ANDROID_JAVA_SOURCE_DIR")) {
  3240. e2.Element("JavaSourceDir", javaSourceDir);
  3241. }
  3242. if (const char* jarDirectoriesExpression =
  3243. this->GeneratorTarget->GetProperty("ANDROID_JAR_DIRECTORIES")) {
  3244. std::string jarDirectories = cmGeneratorExpression::Evaluate(
  3245. jarDirectoriesExpression, this->LocalGenerator, configName);
  3246. e2.Element("JarDirectories", jarDirectories);
  3247. }
  3248. if (const char* jarDeps =
  3249. this->GeneratorTarget->GetProperty("ANDROID_JAR_DEPENDENCIES")) {
  3250. e2.Element("JarDependencies", jarDeps);
  3251. }
  3252. if (const char* assetsDirectories =
  3253. this->GeneratorTarget->GetProperty("ANDROID_ASSETS_DIRECTORIES")) {
  3254. e2.Element("AssetsDirectories", assetsDirectories);
  3255. }
  3256. {
  3257. std::string manifest_xml = rootDir + "/AndroidManifest.xml";
  3258. ConvertToWindowsSlash(manifest_xml);
  3259. e2.Element("AndroidManifestLocation", manifest_xml);
  3260. }
  3261. if (const char* antAdditionalOptions =
  3262. this->GeneratorTarget->GetProperty("ANDROID_ANT_ADDITIONAL_OPTIONS")) {
  3263. e2.Element("AdditionalOptions",
  3264. std::string(antAdditionalOptions) + " %(AdditionalOptions)");
  3265. }
  3266. }
  3267. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  3268. {
  3269. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  3270. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3271. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3272. for (std::string const& c : this->Configurations) {
  3273. if (!this->ComputeLinkOptions(c)) {
  3274. return false;
  3275. }
  3276. }
  3277. }
  3278. return true;
  3279. }
  3280. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
  3281. std::string const& config)
  3282. {
  3283. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3284. auto pOptions = cm::make_unique<Options>(
  3285. this->LocalGenerator, Options::Linker, gg->GetLinkFlagTable(), this);
  3286. Options& linkOptions = *pOptions;
  3287. cmGeneratorTarget::LinkClosure const* linkClosure =
  3288. this->GeneratorTarget->GetLinkClosure(config);
  3289. const std::string& linkLanguage = linkClosure->LinkerLanguage;
  3290. if (linkLanguage.empty()) {
  3291. cmSystemTools::Error(
  3292. "CMake can not determine linker language for target: " + this->Name);
  3293. return false;
  3294. }
  3295. std::string CONFIG = cmSystemTools::UpperCase(config);
  3296. const char* linkType = "SHARED";
  3297. if (this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3298. linkType = "MODULE";
  3299. }
  3300. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3301. linkType = "EXE";
  3302. }
  3303. std::string flags;
  3304. std::string linkFlagVarBase = cmStrCat("CMAKE_", linkType, "_LINKER_FLAGS");
  3305. flags += " ";
  3306. flags += this->Makefile->GetRequiredDefinition(linkFlagVarBase);
  3307. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  3308. flags += " ";
  3309. flags += this->Makefile->GetRequiredDefinition(linkFlagVar);
  3310. const char* targetLinkFlags =
  3311. this->GeneratorTarget->GetProperty("LINK_FLAGS");
  3312. if (targetLinkFlags) {
  3313. flags += " ";
  3314. flags += targetLinkFlags;
  3315. }
  3316. std::string flagsProp = cmStrCat("LINK_FLAGS_", CONFIG);
  3317. if (const char* flagsConfig =
  3318. this->GeneratorTarget->GetProperty(flagsProp)) {
  3319. flags += " ";
  3320. flags += flagsConfig;
  3321. }
  3322. std::vector<std::string> opts;
  3323. this->GeneratorTarget->GetLinkOptions(opts, config, linkLanguage);
  3324. // LINK_OPTIONS are escaped.
  3325. this->LocalGenerator->AppendCompileOptions(flags, opts);
  3326. cmComputeLinkInformation* pcli =
  3327. this->GeneratorTarget->GetLinkInformation(config);
  3328. if (!pcli) {
  3329. cmSystemTools::Error(
  3330. "CMake can not compute cmComputeLinkInformation for target: " +
  3331. this->Name);
  3332. return false;
  3333. }
  3334. cmComputeLinkInformation& cli = *pcli;
  3335. std::vector<std::string> libVec;
  3336. std::vector<std::string> vsTargetVec;
  3337. this->AddLibraries(cli, libVec, vsTargetVec, config);
  3338. if (cmContains(linkClosure->Languages, "CUDA") &&
  3339. this->CudaOptions[config] != nullptr) {
  3340. switch (this->CudaOptions[config]->GetCudaRuntime()) {
  3341. case cmVisualStudioGeneratorOptions::CudaRuntimeStatic:
  3342. libVec.push_back("cudadevrt.lib");
  3343. libVec.push_back("cudart_static.lib");
  3344. break;
  3345. case cmVisualStudioGeneratorOptions::CudaRuntimeShared:
  3346. libVec.push_back("cudadevrt.lib");
  3347. libVec.push_back("cudart.lib");
  3348. break;
  3349. case cmVisualStudioGeneratorOptions::CudaRuntimeNone:
  3350. break;
  3351. }
  3352. }
  3353. std::string standardLibsVar =
  3354. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  3355. std::string const& libs = this->Makefile->GetSafeDefinition(standardLibsVar);
  3356. cmSystemTools::ParseWindowsCommandLine(libs.c_str(), libVec);
  3357. linkOptions.AddFlag("AdditionalDependencies", libVec);
  3358. // Populate TargetsFileAndConfigsVec
  3359. for (std::string const& ti : vsTargetVec) {
  3360. this->AddTargetsFileAndConfigPair(ti, config);
  3361. }
  3362. std::vector<std::string> const& ldirs = cli.GetDirectories();
  3363. std::vector<std::string> linkDirs;
  3364. for (std::string const& d : ldirs) {
  3365. // first just full path
  3366. linkDirs.push_back(d);
  3367. // next path with configuration type Debug, Release, etc
  3368. linkDirs.push_back(d + "/$(Configuration)");
  3369. }
  3370. linkDirs.push_back("%(AdditionalLibraryDirectories)");
  3371. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs);
  3372. cmGeneratorTarget::Names targetNames;
  3373. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3374. targetNames = this->GeneratorTarget->GetExecutableNames(config);
  3375. } else {
  3376. targetNames = this->GeneratorTarget->GetLibraryNames(config);
  3377. }
  3378. if (this->MSTools) {
  3379. if (this->GeneratorTarget->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  3380. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3381. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3382. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3383. if (this->ClOptions[config]->UsingUnicode()) {
  3384. linkOptions.AddFlag("EntryPointSymbol", "wWinMainCRTStartup");
  3385. } else {
  3386. linkOptions.AddFlag("EntryPointSymbol", "WinMainCRTStartup");
  3387. }
  3388. }
  3389. } else {
  3390. linkOptions.AddFlag("SubSystem", "Windows");
  3391. }
  3392. } else {
  3393. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3394. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3395. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3396. if (this->ClOptions[config]->UsingUnicode()) {
  3397. linkOptions.AddFlag("EntryPointSymbol", "mainWCRTStartup");
  3398. } else {
  3399. linkOptions.AddFlag("EntryPointSymbol", "mainACRTStartup");
  3400. }
  3401. }
  3402. } else {
  3403. linkOptions.AddFlag("SubSystem", "Console");
  3404. };
  3405. }
  3406. if (const char* stackVal = this->Makefile->GetDefinition(
  3407. "CMAKE_" + linkLanguage + "_STACK_SIZE")) {
  3408. linkOptions.AddFlag("StackReserveSize", stackVal);
  3409. }
  3410. linkOptions.AddFlag("GenerateDebugInformation", "false");
  3411. std::string pdb = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config),
  3412. '/', targetNames.PDB);
  3413. std::string imLib =
  3414. cmStrCat(this->GeneratorTarget->GetDirectory(
  3415. config, cmStateEnums::ImportLibraryArtifact),
  3416. '/', targetNames.ImportLibrary);
  3417. linkOptions.AddFlag("ImportLibrary", imLib);
  3418. linkOptions.AddFlag("ProgramDataBaseFile", pdb);
  3419. // A Windows Runtime component uses internal .NET metadata,
  3420. // so does not have an import library.
  3421. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") &&
  3422. this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE) {
  3423. linkOptions.AddFlag("GenerateWindowsMetadata", "true");
  3424. } else if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3425. this->GlobalGenerator->TargetsWindowsStore()) {
  3426. // WindowsPhone and WindowsStore components are in an app container
  3427. // and produce WindowsMetadata. If we are not producing a WINRT
  3428. // component, then do not generate the metadata here.
  3429. linkOptions.AddFlag("GenerateWindowsMetadata", "false");
  3430. }
  3431. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  3432. this->GlobalGenerator->GetSystemVersion() == "8.0") {
  3433. // WindowsPhone 8.0 does not have ole32.
  3434. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries", "ole32.lib");
  3435. }
  3436. } else if (this->NsightTegra) {
  3437. linkOptions.AddFlag("SoName", targetNames.SharedObject);
  3438. }
  3439. linkOptions.Parse(flags);
  3440. linkOptions.FixManifestUACFlags();
  3441. if (this->MSTools) {
  3442. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  3443. this->GeneratorTarget->GetModuleDefinitionInfo(config);
  3444. if (mdi && !mdi->DefFile.empty()) {
  3445. linkOptions.AddFlag("ModuleDefinitionFile", mdi->DefFile);
  3446. }
  3447. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries",
  3448. "%(IgnoreSpecificDefaultLibraries)");
  3449. }
  3450. // VS 2015 without all updates has a v140 toolset whose
  3451. // GenerateDebugInformation expects No/Debug instead of false/true.
  3452. if (gg->GetPlatformToolsetNeedsDebugEnum()) {
  3453. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  3454. if (strcmp(debug, "false") == 0) {
  3455. linkOptions.AddFlag("GenerateDebugInformation", "No");
  3456. } else if (strcmp(debug, "true") == 0) {
  3457. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  3458. }
  3459. }
  3460. }
  3461. // Managed code cannot be linked with /DEBUG:FASTLINK
  3462. if (this->Managed) {
  3463. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  3464. if (strcmp(debug, "DebugFastLink") == 0) {
  3465. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  3466. }
  3467. }
  3468. }
  3469. this->LinkOptions[config] = std::move(pOptions);
  3470. return true;
  3471. }
  3472. bool cmVisualStudio10TargetGenerator::ComputeLibOptions()
  3473. {
  3474. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3475. for (std::string const& c : this->Configurations) {
  3476. if (!this->ComputeLibOptions(c)) {
  3477. return false;
  3478. }
  3479. }
  3480. }
  3481. return true;
  3482. }
  3483. bool cmVisualStudio10TargetGenerator::ComputeLibOptions(
  3484. std::string const& config)
  3485. {
  3486. cmComputeLinkInformation* pcli =
  3487. this->GeneratorTarget->GetLinkInformation(config);
  3488. if (!pcli) {
  3489. cmSystemTools::Error(
  3490. "CMake can not compute cmComputeLinkInformation for target: " +
  3491. this->Name);
  3492. return false;
  3493. }
  3494. cmComputeLinkInformation& cli = *pcli;
  3495. using ItemVector = cmComputeLinkInformation::ItemVector;
  3496. const ItemVector& libs = cli.GetItems();
  3497. std::string currentBinDir =
  3498. this->LocalGenerator->GetCurrentBinaryDirectory();
  3499. for (cmComputeLinkInformation::Item const& l : libs) {
  3500. if (l.IsPath && cmVS10IsTargetsFile(l.Value)) {
  3501. std::string path = this->LocalGenerator->MaybeConvertToRelativePath(
  3502. currentBinDir, l.Value);
  3503. ConvertToWindowsSlash(path);
  3504. this->AddTargetsFileAndConfigPair(path, config);
  3505. }
  3506. }
  3507. return true;
  3508. }
  3509. void cmVisualStudio10TargetGenerator::WriteLinkOptions(
  3510. Elem& e1, std::string const& config)
  3511. {
  3512. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3513. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  3514. return;
  3515. }
  3516. if (this->ProjectType == csproj) {
  3517. return;
  3518. }
  3519. {
  3520. Elem e2(e1, "Link");
  3521. OptionsHelper linkOptions(*(this->LinkOptions[config]), e2);
  3522. linkOptions.PrependInheritedString("AdditionalOptions");
  3523. linkOptions.OutputFlagMap();
  3524. }
  3525. if (!this->GlobalGenerator->NeedLinkLibraryDependencies(
  3526. this->GeneratorTarget)) {
  3527. Elem e2(e1, "ProjectReference");
  3528. e2.Element("LinkLibraryDependencies", "false");
  3529. }
  3530. }
  3531. void cmVisualStudio10TargetGenerator::AddLibraries(
  3532. const cmComputeLinkInformation& cli, std::vector<std::string>& libVec,
  3533. std::vector<std::string>& vsTargetVec, const std::string& config)
  3534. {
  3535. using ItemVector = cmComputeLinkInformation::ItemVector;
  3536. ItemVector const& libs = cli.GetItems();
  3537. std::string currentBinDir =
  3538. this->LocalGenerator->GetCurrentBinaryDirectory();
  3539. for (cmComputeLinkInformation::Item const& l : libs) {
  3540. if (l.Target) {
  3541. auto managedType = l.Target->GetManagedType(config);
  3542. if (managedType != cmGeneratorTarget::ManagedType::Native &&
  3543. this->GeneratorTarget->GetManagedType(config) !=
  3544. cmGeneratorTarget::ManagedType::Native &&
  3545. l.Target->IsImported()) {
  3546. auto location = l.Target->GetFullPath(config);
  3547. if (!location.empty()) {
  3548. ConvertToWindowsSlash(location);
  3549. switch (this->ProjectType) {
  3550. case csproj:
  3551. // If the target we want to "link" to is an imported managed
  3552. // target and this is a C# project, we add a hint reference. This
  3553. // reference is written to project file in
  3554. // WriteDotNetReferences().
  3555. this->DotNetHintReferences[config].push_back(
  3556. DotNetHintReference(l.Target->GetName(), location));
  3557. break;
  3558. case vcxproj:
  3559. // Add path of assembly to list of using-directories, so the
  3560. // managed assembly can be used by '#using <assembly.dll>' in
  3561. // code.
  3562. this->AdditionalUsingDirectories[config].insert(
  3563. cmSystemTools::GetFilenamePath(location));
  3564. break;
  3565. }
  3566. }
  3567. }
  3568. // Do not allow C# targets to be added to the LIB listing. LIB files are
  3569. // used for linking C++ dependencies. C# libraries do not have lib files.
  3570. // Instead, they compile down to C# reference libraries (DLL files). The
  3571. // `<ProjectReference>` elements added to the vcxproj are enough for the
  3572. // IDE to deduce the DLL file required by other C# projects that need its
  3573. // reference library.
  3574. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  3575. continue;
  3576. }
  3577. }
  3578. if (l.IsPath) {
  3579. std::string path = this->LocalGenerator->MaybeConvertToRelativePath(
  3580. currentBinDir, l.Value);
  3581. ConvertToWindowsSlash(path);
  3582. if (cmVS10IsTargetsFile(l.Value)) {
  3583. vsTargetVec.push_back(path);
  3584. } else {
  3585. libVec.push_back(path);
  3586. }
  3587. } else if (!l.Target ||
  3588. l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  3589. libVec.push_back(l.Value);
  3590. }
  3591. }
  3592. }
  3593. void cmVisualStudio10TargetGenerator::AddTargetsFileAndConfigPair(
  3594. std::string const& targetsFile, std::string const& config)
  3595. {
  3596. for (TargetsFileAndConfigs& i : this->TargetsFileAndConfigsVec) {
  3597. if (cmSystemTools::ComparePath(targetsFile, i.File)) {
  3598. if (!cmContains(i.Configs, config)) {
  3599. i.Configs.push_back(config);
  3600. }
  3601. return;
  3602. }
  3603. }
  3604. TargetsFileAndConfigs entry;
  3605. entry.File = targetsFile;
  3606. entry.Configs.push_back(config);
  3607. this->TargetsFileAndConfigsVec.push_back(entry);
  3608. }
  3609. void cmVisualStudio10TargetGenerator::WriteMidlOptions(
  3610. Elem& e1, std::string const& configName)
  3611. {
  3612. if (!this->MSTools) {
  3613. return;
  3614. }
  3615. if (this->ProjectType == csproj) {
  3616. return;
  3617. }
  3618. // This processes *any* of the .idl files specified in the project's file
  3619. // list (and passed as the item metadata %(Filename) expressing the rule
  3620. // input filename) into output files at the per-config *build* dir
  3621. // ($(IntDir)) each.
  3622. //
  3623. // IOW, this MIDL section is intended to provide a fully generic syntax
  3624. // content suitable for most cases (read: if you get errors, then it's quite
  3625. // probable that the error is on your side of the .idl setup).
  3626. //
  3627. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  3628. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  3629. // project's file list, otherwise the compiler-side processing won't pick it
  3630. // up (for non-directory form, it ends up looking in project binary dir
  3631. // only). Perhaps there's something to be done to make this more automatic
  3632. // on the CMake side?
  3633. std::vector<std::string> const includes =
  3634. this->GetIncludes(configName, "MIDL");
  3635. std::ostringstream oss;
  3636. for (std::string const& i : includes) {
  3637. oss << i << ";";
  3638. }
  3639. oss << "%(AdditionalIncludeDirectories)";
  3640. Elem e2(e1, "Midl");
  3641. e2.Element("AdditionalIncludeDirectories", oss.str());
  3642. e2.Element("OutputDirectory", "$(ProjectDir)/$(IntDir)");
  3643. e2.Element("HeaderFileName", "%(Filename).h");
  3644. e2.Element("TypeLibraryName", "%(Filename).tlb");
  3645. e2.Element("InterfaceIdentifierFileName", "%(Filename)_i.c");
  3646. e2.Element("ProxyFileName", "%(Filename)_p.c");
  3647. }
  3648. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups(Elem& e0)
  3649. {
  3650. if (this->ProjectType == csproj) {
  3651. return;
  3652. }
  3653. for (const std::string& c : this->Configurations) {
  3654. Elem e1(e0, "ItemDefinitionGroup");
  3655. e1.Attribute("Condition", this->CalcCondition(c));
  3656. // output cl compile flags <ClCompile></ClCompile>
  3657. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  3658. this->WriteClOptions(e1, c);
  3659. // output rc compile flags <ResourceCompile></ResourceCompile>
  3660. this->WriteRCOptions(e1, c);
  3661. this->WriteCudaOptions(e1, c);
  3662. this->WriteMasmOptions(e1, c);
  3663. this->WriteNasmOptions(e1, c);
  3664. }
  3665. // output midl flags <Midl></Midl>
  3666. this->WriteMidlOptions(e1, c);
  3667. // write events
  3668. if (this->ProjectType != csproj) {
  3669. this->WriteEvents(e1, c);
  3670. }
  3671. // output link flags <Link></Link>
  3672. this->WriteLinkOptions(e1, c);
  3673. this->WriteCudaLinkOptions(e1, c);
  3674. // output lib flags <Lib></Lib>
  3675. this->WriteLibOptions(e1, c);
  3676. // output manifest flags <Manifest></Manifest>
  3677. this->WriteManifestOptions(e1, c);
  3678. if (this->NsightTegra &&
  3679. this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE &&
  3680. this->GeneratorTarget->GetPropertyAsBool("ANDROID_GUI")) {
  3681. this->WriteAntBuildOptions(e1, c);
  3682. }
  3683. }
  3684. }
  3685. void cmVisualStudio10TargetGenerator::WriteEvents(
  3686. Elem& e1, std::string const& configName)
  3687. {
  3688. bool addedPrelink = false;
  3689. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  3690. this->GeneratorTarget->GetModuleDefinitionInfo(configName);
  3691. if (mdi && mdi->DefFileGenerated) {
  3692. addedPrelink = true;
  3693. std::vector<cmCustomCommand> commands =
  3694. this->GeneratorTarget->GetPreLinkCommands();
  3695. this->GlobalGenerator->AddSymbolExportCommand(this->GeneratorTarget,
  3696. commands, configName);
  3697. this->WriteEvent(e1, "PreLinkEvent", commands, configName);
  3698. }
  3699. if (!addedPrelink) {
  3700. this->WriteEvent(e1, "PreLinkEvent",
  3701. this->GeneratorTarget->GetPreLinkCommands(), configName);
  3702. }
  3703. this->WriteEvent(e1, "PreBuildEvent",
  3704. this->GeneratorTarget->GetPreBuildCommands(), configName);
  3705. this->WriteEvent(e1, "PostBuildEvent",
  3706. this->GeneratorTarget->GetPostBuildCommands(), configName);
  3707. }
  3708. void cmVisualStudio10TargetGenerator::WriteEvent(
  3709. Elem& e1, const std::string& name,
  3710. std::vector<cmCustomCommand> const& commands, std::string const& configName)
  3711. {
  3712. if (commands.empty()) {
  3713. return;
  3714. }
  3715. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  3716. std::string script;
  3717. const char* pre = "";
  3718. std::string comment;
  3719. for (cmCustomCommand const& cc : commands) {
  3720. cmCustomCommandGenerator ccg(cc, configName, lg);
  3721. if (!ccg.HasOnlyEmptyCommandLines()) {
  3722. comment += pre;
  3723. comment += lg->ConstructComment(ccg);
  3724. script += pre;
  3725. pre = "\n";
  3726. script += lg->ConstructScript(ccg);
  3727. }
  3728. }
  3729. comment = cmVS10EscapeComment(comment);
  3730. if (this->ProjectType != csproj) {
  3731. Elem e2(e1, name);
  3732. e2.Element("Message", comment);
  3733. e2.Element("Command", script);
  3734. } else {
  3735. std::string strippedComment = comment;
  3736. strippedComment.erase(
  3737. std::remove(strippedComment.begin(), strippedComment.end(), '\t'),
  3738. strippedComment.end());
  3739. std::ostringstream oss;
  3740. if (!comment.empty() && !strippedComment.empty()) {
  3741. oss << "echo " << comment << "\n";
  3742. }
  3743. oss << script << "\n";
  3744. e1.Element(name, oss.str());
  3745. }
  3746. }
  3747. void cmVisualStudio10TargetGenerator::WriteProjectReferences(Elem& e0)
  3748. {
  3749. cmGlobalGenerator::TargetDependSet const& unordered =
  3750. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  3751. using OrderedTargetDependSet =
  3752. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  3753. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  3754. Elem e1(e0, "ItemGroup");
  3755. e1.SetHasElements();
  3756. for (cmGeneratorTarget const* dt : depends) {
  3757. if (dt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3758. continue;
  3759. }
  3760. // skip fortran targets as they can not be processed by MSBuild
  3761. // the only reference will be in the .sln file
  3762. if (this->GlobalGenerator->TargetIsFortranOnly(dt)) {
  3763. continue;
  3764. }
  3765. cmLocalGenerator* lg = dt->GetLocalGenerator();
  3766. std::string name = dt->GetName();
  3767. std::string path;
  3768. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  3769. if (p) {
  3770. path = p;
  3771. } else {
  3772. path = cmStrCat(lg->GetCurrentBinaryDirectory(), '/', dt->GetName(),
  3773. computeProjectFileExtension(dt));
  3774. }
  3775. ConvertToWindowsSlash(path);
  3776. Elem e2(e1, "ProjectReference");
  3777. e2.Attribute("Include", path);
  3778. e2.Element("Project", "{" + this->GlobalGenerator->GetGUID(name) + "}");
  3779. e2.Element("Name", name);
  3780. this->WriteDotNetReferenceCustomTags(e2, name);
  3781. // Don't reference targets that don't produce any output.
  3782. if (dt->GetManagedType("") == cmGeneratorTarget::ManagedType::Undefined) {
  3783. e2.Element("ReferenceOutputAssembly", "false");
  3784. e2.Element("CopyToOutputDirectory", "Never");
  3785. }
  3786. }
  3787. }
  3788. void cmVisualStudio10TargetGenerator::WritePlatformExtensions(Elem& e1)
  3789. {
  3790. // This only applies to Windows 10 apps
  3791. if (this->GlobalGenerator->TargetsWindowsStore() &&
  3792. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  3793. const char* desktopExtensionsVersion =
  3794. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  3795. if (desktopExtensionsVersion) {
  3796. this->WriteSinglePlatformExtension(e1, "WindowsDesktop",
  3797. desktopExtensionsVersion);
  3798. }
  3799. const char* mobileExtensionsVersion =
  3800. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  3801. if (mobileExtensionsVersion) {
  3802. this->WriteSinglePlatformExtension(e1, "WindowsMobile",
  3803. mobileExtensionsVersion);
  3804. }
  3805. }
  3806. }
  3807. void cmVisualStudio10TargetGenerator::WriteSinglePlatformExtension(
  3808. Elem& e1, std::string const& extension, std::string const& version)
  3809. {
  3810. const std::string s = "$([Microsoft.Build.Utilities.ToolLocationHelper]"
  3811. "::GetPlatformExtensionSDKLocation(`" +
  3812. extension + ", Version=" + version +
  3813. "`, $(TargetPlatformIdentifier), $(TargetPlatformVersion), null, "
  3814. "$(ExtensionSDKDirectoryRoot), null))"
  3815. "\\DesignTime\\CommonConfiguration\\Neutral\\" +
  3816. extension + ".props";
  3817. Elem e2(e1, "Import");
  3818. e2.Attribute("Project", s);
  3819. e2.Attribute("Condition", "exists('" + s + "')");
  3820. }
  3821. void cmVisualStudio10TargetGenerator::WriteSDKReferences(Elem& e0)
  3822. {
  3823. std::vector<std::string> sdkReferences;
  3824. std::unique_ptr<Elem> spe1;
  3825. if (const char* vsSDKReferences =
  3826. this->GeneratorTarget->GetProperty("VS_SDK_REFERENCES")) {
  3827. cmExpandList(vsSDKReferences, sdkReferences);
  3828. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  3829. for (std::string const& ri : sdkReferences) {
  3830. Elem(*spe1, "SDKReference").Attribute("Include", ri);
  3831. }
  3832. }
  3833. // This only applies to Windows 10 apps
  3834. if (this->GlobalGenerator->TargetsWindowsStore() &&
  3835. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  3836. const char* desktopExtensionsVersion =
  3837. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  3838. const char* mobileExtensionsVersion =
  3839. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  3840. const char* iotExtensionsVersion =
  3841. this->GeneratorTarget->GetProperty("VS_IOT_EXTENSIONS_VERSION");
  3842. if (desktopExtensionsVersion || mobileExtensionsVersion ||
  3843. iotExtensionsVersion) {
  3844. if (!spe1) {
  3845. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  3846. }
  3847. if (desktopExtensionsVersion) {
  3848. this->WriteSingleSDKReference(*spe1, "WindowsDesktop",
  3849. desktopExtensionsVersion);
  3850. }
  3851. if (mobileExtensionsVersion) {
  3852. this->WriteSingleSDKReference(*spe1, "WindowsMobile",
  3853. mobileExtensionsVersion);
  3854. }
  3855. if (iotExtensionsVersion) {
  3856. this->WriteSingleSDKReference(*spe1, "WindowsIoT",
  3857. iotExtensionsVersion);
  3858. }
  3859. }
  3860. }
  3861. }
  3862. void cmVisualStudio10TargetGenerator::WriteSingleSDKReference(
  3863. Elem& e1, std::string const& extension, std::string const& version)
  3864. {
  3865. Elem(e1, "SDKReference")
  3866. .Attribute("Include", extension + ", Version=" + version);
  3867. }
  3868. void cmVisualStudio10TargetGenerator::WriteWinRTPackageCertificateKeyFile(
  3869. Elem& e0)
  3870. {
  3871. if ((this->GlobalGenerator->TargetsWindowsStore() ||
  3872. this->GlobalGenerator->TargetsWindowsPhone()) &&
  3873. (cmStateEnums::EXECUTABLE == this->GeneratorTarget->GetType())) {
  3874. std::string pfxFile;
  3875. std::vector<cmSourceFile const*> certificates;
  3876. this->GeneratorTarget->GetCertificates(certificates, "");
  3877. for (cmSourceFile const* si : certificates) {
  3878. pfxFile = this->ConvertPath(si->GetFullPath(), false);
  3879. ConvertToWindowsSlash(pfxFile);
  3880. break;
  3881. }
  3882. if (this->IsMissingFiles &&
  3883. !(this->GlobalGenerator->TargetsWindowsPhone() &&
  3884. this->GlobalGenerator->GetSystemVersion() == "8.0")) {
  3885. // Move the manifest to a project directory to avoid clashes
  3886. std::string artifactDir =
  3887. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  3888. ConvertToWindowsSlash(artifactDir);
  3889. Elem e1(e0, "PropertyGroup");
  3890. e1.Element("AppxPackageArtifactsDir", artifactDir + "\\");
  3891. std::string resourcePriFile =
  3892. this->DefaultArtifactDir + "/resources.pri";
  3893. ConvertToWindowsSlash(resourcePriFile);
  3894. e1.Element("ProjectPriFullPath", resourcePriFile);
  3895. // If we are missing files and we don't have a certificate and
  3896. // aren't targeting WP8.0, add a default certificate
  3897. if (pfxFile.empty()) {
  3898. std::string templateFolder =
  3899. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  3900. pfxFile = this->DefaultArtifactDir + "/Windows_TemporaryKey.pfx";
  3901. cmSystemTools::CopyAFile(templateFolder + "/Windows_TemporaryKey.pfx",
  3902. pfxFile, false);
  3903. ConvertToWindowsSlash(pfxFile);
  3904. this->AddedFiles.push_back(pfxFile);
  3905. this->AddedDefaultCertificate = true;
  3906. }
  3907. e1.Element("PackageCertificateKeyFile", pfxFile);
  3908. std::string thumb = cmSystemTools::ComputeCertificateThumbprint(pfxFile);
  3909. if (!thumb.empty()) {
  3910. e1.Element("PackageCertificateThumbprint", thumb);
  3911. }
  3912. } else if (!pfxFile.empty()) {
  3913. Elem e1(e0, "PropertyGroup");
  3914. e1.Element("PackageCertificateKeyFile", pfxFile);
  3915. std::string thumb = cmSystemTools::ComputeCertificateThumbprint(pfxFile);
  3916. if (!thumb.empty()) {
  3917. e1.Element("PackageCertificateThumbprint", thumb);
  3918. }
  3919. }
  3920. }
  3921. }
  3922. bool cmVisualStudio10TargetGenerator::IsResxHeader(
  3923. const std::string& headerFile)
  3924. {
  3925. std::set<std::string> expectedResxHeaders;
  3926. this->GeneratorTarget->GetExpectedResxHeaders(expectedResxHeaders, "");
  3927. return expectedResxHeaders.count(headerFile) > 0;
  3928. }
  3929. bool cmVisualStudio10TargetGenerator::IsXamlHeader(
  3930. const std::string& headerFile)
  3931. {
  3932. std::set<std::string> expectedXamlHeaders;
  3933. this->GeneratorTarget->GetExpectedXamlHeaders(expectedXamlHeaders, "");
  3934. return expectedXamlHeaders.count(headerFile) > 0;
  3935. }
  3936. bool cmVisualStudio10TargetGenerator::IsXamlSource(
  3937. const std::string& sourceFile)
  3938. {
  3939. std::set<std::string> expectedXamlSources;
  3940. this->GeneratorTarget->GetExpectedXamlSources(expectedXamlSources, "");
  3941. return expectedXamlSources.count(sourceFile) > 0;
  3942. }
  3943. void cmVisualStudio10TargetGenerator::WriteApplicationTypeSettings(Elem& e1)
  3944. {
  3945. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3946. bool isAppContainer = false;
  3947. bool const isWindowsPhone = this->GlobalGenerator->TargetsWindowsPhone();
  3948. bool const isWindowsStore = this->GlobalGenerator->TargetsWindowsStore();
  3949. std::string const& rev = this->GlobalGenerator->GetApplicationTypeRevision();
  3950. if (isWindowsPhone || isWindowsStore) {
  3951. e1.Element("ApplicationType",
  3952. (isWindowsPhone ? "Windows Phone" : "Windows Store"));
  3953. e1.Element("DefaultLanguage", "en-US");
  3954. if (rev == "10.0") {
  3955. e1.Element("ApplicationTypeRevision", rev);
  3956. // Visual Studio 14.0 is necessary for building 10.0 apps
  3957. e1.Element("MinimumVisualStudioVersion", "14.0");
  3958. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  3959. isAppContainer = true;
  3960. }
  3961. } else if (rev == "8.1") {
  3962. e1.Element("ApplicationTypeRevision", rev);
  3963. // Visual Studio 12.0 is necessary for building 8.1 apps
  3964. e1.Element("MinimumVisualStudioVersion", "12.0");
  3965. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  3966. isAppContainer = true;
  3967. }
  3968. } else if (rev == "8.0") {
  3969. e1.Element("ApplicationTypeRevision", rev);
  3970. // Visual Studio 11.0 is necessary for building 8.0 apps
  3971. e1.Element("MinimumVisualStudioVersion", "11.0");
  3972. if (isWindowsStore &&
  3973. this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  3974. isAppContainer = true;
  3975. } else if (isWindowsPhone &&
  3976. this->GeneratorTarget->GetType() ==
  3977. cmStateEnums::EXECUTABLE) {
  3978. e1.Element("XapOutputs", "true");
  3979. e1.Element("XapFilename",
  3980. this->Name + "_$(Configuration)_$(Platform).xap");
  3981. }
  3982. }
  3983. }
  3984. if (isAppContainer) {
  3985. e1.Element("AppContainerApplication", "true");
  3986. } else if (this->Platform == "ARM64") {
  3987. e1.Element("WindowsSDKDesktopARM64Support", "true");
  3988. } else if (this->Platform == "ARM") {
  3989. e1.Element("WindowsSDKDesktopARMSupport", "true");
  3990. }
  3991. std::string const& targetPlatformVersion =
  3992. gg->GetWindowsTargetPlatformVersion();
  3993. if (!targetPlatformVersion.empty()) {
  3994. e1.Element("WindowsTargetPlatformVersion", targetPlatformVersion);
  3995. }
  3996. const char* targetPlatformMinVersion = this->GeneratorTarget->GetProperty(
  3997. "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION");
  3998. if (targetPlatformMinVersion) {
  3999. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformMinVersion);
  4000. } else if (isWindowsStore && rev == "10.0") {
  4001. // If the min version is not set, then use the TargetPlatformVersion
  4002. if (!targetPlatformVersion.empty()) {
  4003. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformVersion);
  4004. }
  4005. }
  4006. // Added IoT Startup Task support
  4007. if (this->GeneratorTarget->GetPropertyAsBool("VS_IOT_STARTUP_TASK")) {
  4008. e1.Element("ContainsStartupTask", "true");
  4009. }
  4010. }
  4011. void cmVisualStudio10TargetGenerator::VerifyNecessaryFiles()
  4012. {
  4013. // For Windows and Windows Phone executables, we will assume that if a
  4014. // manifest is not present that we need to add all the necessary files
  4015. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4016. std::vector<cmSourceFile const*> manifestSources;
  4017. this->GeneratorTarget->GetAppManifest(manifestSources, "");
  4018. {
  4019. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4020. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4021. if (v == "8.0") {
  4022. // Look through the sources for WMAppManifest.xml
  4023. std::vector<cmSourceFile const*> extraSources;
  4024. this->GeneratorTarget->GetExtraSources(extraSources, "");
  4025. bool foundManifest = false;
  4026. for (cmSourceFile const* si : extraSources) {
  4027. // Need to do a lowercase comparison on the filename
  4028. if ("wmappmanifest.xml" ==
  4029. cmSystemTools::LowerCase(si->GetLocation().GetName())) {
  4030. foundManifest = true;
  4031. break;
  4032. }
  4033. }
  4034. if (!foundManifest) {
  4035. this->IsMissingFiles = true;
  4036. }
  4037. } else if (v == "8.1") {
  4038. if (manifestSources.empty()) {
  4039. this->IsMissingFiles = true;
  4040. }
  4041. }
  4042. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4043. if (manifestSources.empty()) {
  4044. if (v == "8.0") {
  4045. this->IsMissingFiles = true;
  4046. } else if (v == "8.1" || cmHasLiteralPrefix(v, "10.0")) {
  4047. this->IsMissingFiles = true;
  4048. }
  4049. }
  4050. }
  4051. }
  4052. }
  4053. }
  4054. void cmVisualStudio10TargetGenerator::WriteMissingFiles(Elem& e1)
  4055. {
  4056. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4057. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4058. if (v == "8.0") {
  4059. this->WriteMissingFilesWP80(e1);
  4060. } else if (v == "8.1") {
  4061. this->WriteMissingFilesWP81(e1);
  4062. }
  4063. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4064. if (v == "8.0") {
  4065. this->WriteMissingFilesWS80(e1);
  4066. } else if (v == "8.1") {
  4067. this->WriteMissingFilesWS81(e1);
  4068. } else if (cmHasLiteralPrefix(v, "10.0")) {
  4069. this->WriteMissingFilesWS10_0(e1);
  4070. }
  4071. }
  4072. }
  4073. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP80(Elem& e1)
  4074. {
  4075. std::string templateFolder =
  4076. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4077. // For WP80, the manifest needs to be in the same folder as the project
  4078. // this can cause an overwrite problem if projects aren't organized in
  4079. // folders
  4080. std::string manifestFile =
  4081. this->LocalGenerator->GetCurrentBinaryDirectory() + "/WMAppManifest.xml";
  4082. std::string artifactDir =
  4083. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4084. ConvertToWindowsSlash(artifactDir);
  4085. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4086. std::string targetNameXML =
  4087. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4088. cmGeneratedFileStream fout(manifestFile);
  4089. fout.SetCopyIfDifferent(true);
  4090. /* clang-format off */
  4091. fout <<
  4092. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4093. "<Deployment"
  4094. " xmlns=\"http://schemas.microsoft.com/windowsphone/2012/deployment\""
  4095. " AppPlatformVersion=\"8.0\">\n"
  4096. "\t<DefaultLanguage xmlns=\"\" code=\"en-US\"/>\n"
  4097. "\t<App xmlns=\"\" ProductID=\"{" << this->GUID << "}\""
  4098. " Title=\"CMake Test Program\" RuntimeType=\"Modern Native\""
  4099. " Version=\"1.0.0.0\" Genre=\"apps.normal\" Author=\"CMake\""
  4100. " Description=\"Default CMake App\" Publisher=\"CMake\""
  4101. " PublisherID=\"{" << this->GUID << "}\">\n"
  4102. "\t\t<IconPath IsRelative=\"true\" IsResource=\"false\">"
  4103. << artifactDirXML << "\\ApplicationIcon.png</IconPath>\n"
  4104. "\t\t<Capabilities/>\n"
  4105. "\t\t<Tasks>\n"
  4106. "\t\t\t<DefaultTask Name=\"_default\""
  4107. " ImagePath=\"" << targetNameXML << ".exe\" ImageParams=\"\" />\n"
  4108. "\t\t</Tasks>\n"
  4109. "\t\t<Tokens>\n"
  4110. "\t\t\t<PrimaryToken TokenID=\"" << targetNameXML << "Token\""
  4111. " TaskName=\"_default\">\n"
  4112. "\t\t\t\t<TemplateFlip>\n"
  4113. "\t\t\t\t\t<SmallImageURI IsRelative=\"true\" IsResource=\"false\">"
  4114. << artifactDirXML << "\\SmallLogo.png</SmallImageURI>\n"
  4115. "\t\t\t\t\t<Count>0</Count>\n"
  4116. "\t\t\t\t\t<BackgroundImageURI IsRelative=\"true\" IsResource=\"false\">"
  4117. << artifactDirXML << "\\Logo.png</BackgroundImageURI>\n"
  4118. "\t\t\t\t</TemplateFlip>\n"
  4119. "\t\t\t</PrimaryToken>\n"
  4120. "\t\t</Tokens>\n"
  4121. "\t\t<ScreenResolutions>\n"
  4122. "\t\t\t<ScreenResolution Name=\"ID_RESOLUTION_WVGA\" />\n"
  4123. "\t\t</ScreenResolutions>\n"
  4124. "\t</App>\n"
  4125. "</Deployment>\n";
  4126. /* clang-format on */
  4127. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4128. ConvertToWindowsSlash(sourceFile);
  4129. {
  4130. Elem e2(e1, "Xml");
  4131. e2.Attribute("Include", sourceFile);
  4132. e2.Element("SubType", "Designer");
  4133. }
  4134. this->AddedFiles.push_back(sourceFile);
  4135. std::string smallLogo = this->DefaultArtifactDir + "/SmallLogo.png";
  4136. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo.png", smallLogo,
  4137. false);
  4138. ConvertToWindowsSlash(smallLogo);
  4139. Elem(e1, "Image").Attribute("Include", smallLogo);
  4140. this->AddedFiles.push_back(smallLogo);
  4141. std::string logo = this->DefaultArtifactDir + "/Logo.png";
  4142. cmSystemTools::CopyAFile(templateFolder + "/Logo.png", logo, false);
  4143. ConvertToWindowsSlash(logo);
  4144. Elem(e1, "Image").Attribute("Include", logo);
  4145. this->AddedFiles.push_back(logo);
  4146. std::string applicationIcon =
  4147. this->DefaultArtifactDir + "/ApplicationIcon.png";
  4148. cmSystemTools::CopyAFile(templateFolder + "/ApplicationIcon.png",
  4149. applicationIcon, false);
  4150. ConvertToWindowsSlash(applicationIcon);
  4151. Elem(e1, "Image").Attribute("Include", applicationIcon);
  4152. this->AddedFiles.push_back(applicationIcon);
  4153. }
  4154. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP81(Elem& e1)
  4155. {
  4156. std::string manifestFile =
  4157. this->DefaultArtifactDir + "/package.appxManifest";
  4158. std::string artifactDir =
  4159. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4160. ConvertToWindowsSlash(artifactDir);
  4161. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4162. std::string targetNameXML =
  4163. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4164. cmGeneratedFileStream fout(manifestFile);
  4165. fout.SetCopyIfDifferent(true);
  4166. /* clang-format off */
  4167. fout <<
  4168. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4169. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4170. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\""
  4171. " xmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\">\n"
  4172. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4173. " Version=\"1.0.0.0\" />\n"
  4174. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID << "\""
  4175. " PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4176. "\t<Properties>\n"
  4177. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4178. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4179. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4180. "\t</Properties>\n"
  4181. "\t<Prerequisites>\n"
  4182. "\t\t<OSMinVersion>6.3.1</OSMinVersion>\n"
  4183. "\t\t<OSMaxVersionTested>6.3.1</OSMaxVersionTested>\n"
  4184. "\t</Prerequisites>\n"
  4185. "\t<Resources>\n"
  4186. "\t\t<Resource Language=\"x-generate\" />\n"
  4187. "\t</Resources>\n"
  4188. "\t<Applications>\n"
  4189. "\t\t<Application Id=\"App\""
  4190. " Executable=\"" << targetNameXML << ".exe\""
  4191. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4192. "\t\t\t<m2:VisualElements\n"
  4193. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4194. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4195. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4196. "\t\t\t\tForegroundText=\"light\"\n"
  4197. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4198. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4199. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4200. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4201. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4202. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4203. "\t\t\t\t</m2:DefaultTile>\n"
  4204. "\t\t\t\t<m2:SplashScreen"
  4205. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4206. "\t\t\t</m2:VisualElements>\n"
  4207. "\t\t</Application>\n"
  4208. "\t</Applications>\n"
  4209. "</Package>\n";
  4210. /* clang-format on */
  4211. this->WriteCommonMissingFiles(e1, manifestFile);
  4212. }
  4213. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS80(Elem& e1)
  4214. {
  4215. std::string manifestFile =
  4216. this->DefaultArtifactDir + "/package.appxManifest";
  4217. std::string artifactDir =
  4218. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4219. ConvertToWindowsSlash(artifactDir);
  4220. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4221. std::string targetNameXML =
  4222. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4223. cmGeneratedFileStream fout(manifestFile);
  4224. fout.SetCopyIfDifferent(true);
  4225. /* clang-format off */
  4226. fout <<
  4227. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4228. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\">\n"
  4229. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4230. " Version=\"1.0.0.0\" />\n"
  4231. "\t<Properties>\n"
  4232. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4233. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4234. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4235. "\t</Properties>\n"
  4236. "\t<Prerequisites>\n"
  4237. "\t\t<OSMinVersion>6.2.1</OSMinVersion>\n"
  4238. "\t\t<OSMaxVersionTested>6.2.1</OSMaxVersionTested>\n"
  4239. "\t</Prerequisites>\n"
  4240. "\t<Resources>\n"
  4241. "\t\t<Resource Language=\"x-generate\" />\n"
  4242. "\t</Resources>\n"
  4243. "\t<Applications>\n"
  4244. "\t\t<Application Id=\"App\""
  4245. " Executable=\"" << targetNameXML << ".exe\""
  4246. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4247. "\t\t\t<VisualElements"
  4248. " DisplayName=\"" << targetNameXML << "\""
  4249. " Description=\"" << targetNameXML << "\""
  4250. " BackgroundColor=\"#336699\" ForegroundText=\"light\""
  4251. " Logo=\"" << artifactDirXML << "\\Logo.png\""
  4252. " SmallLogo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4253. "\t\t\t\t<DefaultTile ShowName=\"allLogos\""
  4254. " ShortName=\"" << targetNameXML << "\" />\n"
  4255. "\t\t\t\t<SplashScreen"
  4256. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4257. "\t\t\t</VisualElements>\n"
  4258. "\t\t</Application>\n"
  4259. "\t</Applications>\n"
  4260. "</Package>\n";
  4261. /* clang-format on */
  4262. this->WriteCommonMissingFiles(e1, manifestFile);
  4263. }
  4264. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS81(Elem& e1)
  4265. {
  4266. std::string manifestFile =
  4267. this->DefaultArtifactDir + "/package.appxManifest";
  4268. std::string artifactDir =
  4269. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4270. ConvertToWindowsSlash(artifactDir);
  4271. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4272. std::string targetNameXML =
  4273. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4274. cmGeneratedFileStream fout(manifestFile);
  4275. fout.SetCopyIfDifferent(true);
  4276. /* clang-format off */
  4277. fout <<
  4278. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4279. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4280. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\">\n"
  4281. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4282. " Version=\"1.0.0.0\" />\n"
  4283. "\t<Properties>\n"
  4284. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4285. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4286. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4287. "\t</Properties>\n"
  4288. "\t<Prerequisites>\n"
  4289. "\t\t<OSMinVersion>6.3</OSMinVersion>\n"
  4290. "\t\t<OSMaxVersionTested>6.3</OSMaxVersionTested>\n"
  4291. "\t</Prerequisites>\n"
  4292. "\t<Resources>\n"
  4293. "\t\t<Resource Language=\"x-generate\" />\n"
  4294. "\t</Resources>\n"
  4295. "\t<Applications>\n"
  4296. "\t\t<Application Id=\"App\""
  4297. " Executable=\"" << targetNameXML << ".exe\""
  4298. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4299. "\t\t\t<m2:VisualElements\n"
  4300. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4301. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4302. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4303. "\t\t\t\tForegroundText=\"light\"\n"
  4304. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4305. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4306. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4307. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4308. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4309. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4310. "\t\t\t\t</m2:DefaultTile>\n"
  4311. "\t\t\t\t<m2:SplashScreen"
  4312. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4313. "\t\t\t</m2:VisualElements>\n"
  4314. "\t\t</Application>\n"
  4315. "\t</Applications>\n"
  4316. "</Package>\n";
  4317. /* clang-format on */
  4318. this->WriteCommonMissingFiles(e1, manifestFile);
  4319. }
  4320. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS10_0(Elem& e1)
  4321. {
  4322. std::string manifestFile =
  4323. this->DefaultArtifactDir + "/package.appxManifest";
  4324. std::string artifactDir =
  4325. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4326. ConvertToWindowsSlash(artifactDir);
  4327. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4328. std::string targetNameXML =
  4329. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4330. cmGeneratedFileStream fout(manifestFile);
  4331. fout.SetCopyIfDifferent(true);
  4332. /* clang-format off */
  4333. fout <<
  4334. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4335. "<Package\n\t"
  4336. "xmlns=\"http://schemas.microsoft.com/appx/manifest/foundation/windows10\""
  4337. "\txmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\"\n"
  4338. "\txmlns:uap=\"http://schemas.microsoft.com/appx/manifest/uap/windows10\""
  4339. "\n\tIgnorableNamespaces=\"uap mp\">\n\n"
  4340. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4341. " Version=\"1.0.0.0\" />\n"
  4342. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID <<
  4343. "\" PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4344. "\t<Properties>\n"
  4345. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4346. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4347. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4348. "\t</Properties>\n"
  4349. "\t<Dependencies>\n"
  4350. "\t\t<TargetDeviceFamily Name=\"Windows.Universal\" "
  4351. "MinVersion=\"10.0.0.0\" MaxVersionTested=\"10.0.0.0\" />\n"
  4352. "\t</Dependencies>\n"
  4353. "\t<Resources>\n"
  4354. "\t\t<Resource Language=\"x-generate\" />\n"
  4355. "\t</Resources>\n"
  4356. "\t<Applications>\n"
  4357. "\t\t<Application Id=\"App\""
  4358. " Executable=\"" << targetNameXML << ".exe\""
  4359. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4360. "\t\t\t<uap:VisualElements\n"
  4361. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4362. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4363. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4364. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4365. "\t\t\t\tSquare44x44Logo=\"" << artifactDirXML <<
  4366. "\\SmallLogo44x44.png\">\n"
  4367. "\t\t\t\t<uap:SplashScreen"
  4368. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4369. "\t\t\t</uap:VisualElements>\n"
  4370. "\t\t</Application>\n"
  4371. "\t</Applications>\n"
  4372. "</Package>\n";
  4373. /* clang-format on */
  4374. this->WriteCommonMissingFiles(e1, manifestFile);
  4375. }
  4376. void cmVisualStudio10TargetGenerator::WriteCommonMissingFiles(
  4377. Elem& e1, const std::string& manifestFile)
  4378. {
  4379. std::string templateFolder =
  4380. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4381. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4382. ConvertToWindowsSlash(sourceFile);
  4383. {
  4384. Elem e2(e1, "AppxManifest");
  4385. e2.Attribute("Include", sourceFile);
  4386. e2.Element("SubType", "Designer");
  4387. }
  4388. this->AddedFiles.push_back(sourceFile);
  4389. std::string smallLogo = this->DefaultArtifactDir + "/SmallLogo.png";
  4390. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo.png", smallLogo,
  4391. false);
  4392. ConvertToWindowsSlash(smallLogo);
  4393. Elem(e1, "Image").Attribute("Include", smallLogo);
  4394. this->AddedFiles.push_back(smallLogo);
  4395. std::string smallLogo44 = this->DefaultArtifactDir + "/SmallLogo44x44.png";
  4396. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo44x44.png", smallLogo44,
  4397. false);
  4398. ConvertToWindowsSlash(smallLogo44);
  4399. Elem(e1, "Image").Attribute("Include", smallLogo44);
  4400. this->AddedFiles.push_back(smallLogo44);
  4401. std::string logo = this->DefaultArtifactDir + "/Logo.png";
  4402. cmSystemTools::CopyAFile(templateFolder + "/Logo.png", logo, false);
  4403. ConvertToWindowsSlash(logo);
  4404. Elem(e1, "Image").Attribute("Include", logo);
  4405. this->AddedFiles.push_back(logo);
  4406. std::string storeLogo = this->DefaultArtifactDir + "/StoreLogo.png";
  4407. cmSystemTools::CopyAFile(templateFolder + "/StoreLogo.png", storeLogo,
  4408. false);
  4409. ConvertToWindowsSlash(storeLogo);
  4410. Elem(e1, "Image").Attribute("Include", storeLogo);
  4411. this->AddedFiles.push_back(storeLogo);
  4412. std::string splashScreen = this->DefaultArtifactDir + "/SplashScreen.png";
  4413. cmSystemTools::CopyAFile(templateFolder + "/SplashScreen.png", splashScreen,
  4414. false);
  4415. ConvertToWindowsSlash(splashScreen);
  4416. Elem(e1, "Image").Attribute("Include", splashScreen);
  4417. this->AddedFiles.push_back(splashScreen);
  4418. if (this->AddedDefaultCertificate) {
  4419. // This file has already been added to the build so don't copy it
  4420. std::string keyFile =
  4421. this->DefaultArtifactDir + "/Windows_TemporaryKey.pfx";
  4422. ConvertToWindowsSlash(keyFile);
  4423. Elem(e1, "None").Attribute("Include", keyFile);
  4424. }
  4425. }
  4426. bool cmVisualStudio10TargetGenerator::ForceOld(const std::string& source) const
  4427. {
  4428. HANDLE h =
  4429. CreateFileW(cmSystemTools::ConvertToWindowsExtendedPath(source).c_str(),
  4430. FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING,
  4431. FILE_FLAG_BACKUP_SEMANTICS, 0);
  4432. if (!h) {
  4433. return false;
  4434. }
  4435. FILETIME const ftime_20010101 = { 3365781504u, 29389701u };
  4436. if (!SetFileTime(h, &ftime_20010101, &ftime_20010101, &ftime_20010101)) {
  4437. CloseHandle(h);
  4438. return false;
  4439. }
  4440. CloseHandle(h);
  4441. return true;
  4442. }
  4443. void cmVisualStudio10TargetGenerator::GetCSharpSourceProperties(
  4444. cmSourceFile const* sf, std::map<std::string, std::string>& tags)
  4445. {
  4446. if (this->ProjectType == csproj) {
  4447. const cmPropertyMap& props = sf->GetProperties();
  4448. for (const std::string& p : props.GetKeys()) {
  4449. static const std::string propNamePrefix = "VS_CSHARP_";
  4450. if (p.find(propNamePrefix) == 0) {
  4451. std::string tagName = p.substr(propNamePrefix.length());
  4452. if (!tagName.empty()) {
  4453. const std::string val = props.GetPropertyValue(p);
  4454. if (!val.empty()) {
  4455. tags[tagName] = val;
  4456. } else {
  4457. tags.erase(tagName);
  4458. }
  4459. }
  4460. }
  4461. }
  4462. }
  4463. }
  4464. void cmVisualStudio10TargetGenerator::WriteCSharpSourceProperties(
  4465. Elem& e2, const std::map<std::string, std::string>& tags)
  4466. {
  4467. for (const auto& i : tags) {
  4468. e2.Element(i.first, i.second);
  4469. }
  4470. }
  4471. void cmVisualStudio10TargetGenerator::GetCSharpSourceLink(
  4472. cmSourceFile const* sf, std::string& link)
  4473. {
  4474. std::string const& sourceFilePath = sf->GetFullPath();
  4475. std::string const& binaryDir = LocalGenerator->GetCurrentBinaryDirectory();
  4476. if (!cmSystemTools::IsSubDirectory(sourceFilePath, binaryDir)) {
  4477. const std::string& stripFromPath =
  4478. this->Makefile->GetCurrentSourceDirectory();
  4479. if (sourceFilePath.find(stripFromPath) == 0) {
  4480. if (const char* l = sf->GetProperty("VS_CSHARP_Link")) {
  4481. link = l;
  4482. } else {
  4483. link = sourceFilePath.substr(stripFromPath.length() + 1);
  4484. }
  4485. ConvertToWindowsSlash(link);
  4486. }
  4487. }
  4488. }
  4489. std::string cmVisualStudio10TargetGenerator::GetCMakeFilePath(
  4490. const char* relativeFilePath) const
  4491. {
  4492. // Always search in the standard modules location.
  4493. std::string path =
  4494. cmStrCat(cmSystemTools::GetCMakeRoot(), '/', relativeFilePath);
  4495. ConvertToWindowsSlash(path);
  4496. return path;
  4497. }