cmVisualStudio10TargetGenerator.cxx 190 KB

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