cmVisualStudio10TargetGenerator.cxx 182 KB

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