cmVisualStudio10TargetGenerator.cxx 193 KB

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