cmVisualStudio10TargetGenerator.cxx 205 KB

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