cmVisualStudio10TargetGenerator.cxx 207 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767
  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. if (this->GeneratorTarget->IsSynthetic()) {
  330. this->GeneratorTarget->Makefile->IssueMessage(
  331. MessageType::FATAL_ERROR,
  332. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  333. "\" contains C++ modules intended for BMI-only compilation. "
  334. "This is not yet supported by the Visual Studio generator."));
  335. return;
  336. }
  337. for (std::string const& config : this->Configurations) {
  338. this->GeneratorTarget->CheckCxxModuleStatus(config);
  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. std::string config;
  2346. if (!this->Configurations.empty()) {
  2347. config = this->Configurations[si.Configs[0]];
  2348. }
  2349. auto const* fs =
  2350. this->GeneratorTarget->GetFileSetForSource(config, si.Source);
  2351. if (tool) {
  2352. // Compute set of configurations to exclude, if any.
  2353. std::vector<size_t> const& include_configs = si.Configs;
  2354. std::vector<size_t> exclude_configs;
  2355. std::set_difference(all_configs.begin(), all_configs.end(),
  2356. include_configs.begin(), include_configs.end(),
  2357. std::back_inserter(exclude_configs));
  2358. Elem e2(e1, tool);
  2359. bool isCSharp = (si.Source->GetLanguage() == "CSharp"_s);
  2360. if (isCSharp && !exclude_configs.empty()) {
  2361. std::stringstream conditions;
  2362. bool firstConditionSet{ false };
  2363. for (const auto& ci : include_configs) {
  2364. if (firstConditionSet) {
  2365. conditions << " Or ";
  2366. }
  2367. conditions << "('$(Configuration)|$(Platform)'=='"
  2368. << this->Configurations[ci] << '|' << this->Platform
  2369. << "')";
  2370. firstConditionSet = true;
  2371. }
  2372. e2.Attribute("Condition", conditions.str());
  2373. }
  2374. this->WriteSource(e2, si.Source);
  2375. bool useNativeUnityBuild = false;
  2376. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  2377. // Magic value taken from cmGlobalVisualStudioVersionedGenerator.cxx
  2378. static const std::string vs15 = "141";
  2379. std::string toolset =
  2380. this->GlobalGenerator->GetPlatformToolsetString();
  2381. cmSystemTools::ReplaceString(toolset, "v", "");
  2382. if (toolset.empty() ||
  2383. cmSystemTools::VersionCompareGreaterEq(toolset, vs15)) {
  2384. useNativeUnityBuild = true;
  2385. }
  2386. }
  2387. if (haveUnityBuild && strcmp(tool, "ClCompile") == 0 &&
  2388. si.Source->GetProperty("UNITY_SOURCE_FILE")) {
  2389. if (useNativeUnityBuild) {
  2390. e2.Attribute(
  2391. "IncludeInUnityFile",
  2392. si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")
  2393. ? "false"
  2394. : "true");
  2395. e2.Attribute("CustomUnityFile", "true");
  2396. std::string unityDir = cmSystemTools::GetFilenamePath(
  2397. *si.Source->GetProperty("UNITY_SOURCE_FILE"));
  2398. e2.Attribute("UnityFilesDirectory", unityDir);
  2399. } else {
  2400. // Visual Studio versions prior to 2017 15.8 do not know about unity
  2401. // builds, thus we exclude the files already part of unity sources.
  2402. if (!si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")) {
  2403. exclude_configs = all_configs;
  2404. }
  2405. }
  2406. }
  2407. if (si.Kind == cmGeneratorTarget::SourceKindObjectSource ||
  2408. si.Kind == cmGeneratorTarget::SourceKindUnityBatched) {
  2409. this->OutputSourceSpecificFlags(e2, si.Source);
  2410. } else if (fs && fs->GetType() == "CXX_MODULES"_s) {
  2411. this->GeneratorTarget->Makefile->IssueMessage(
  2412. MessageType::FATAL_ERROR,
  2413. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  2414. "\" has source file\n ", si.Source->GetFullPath(),
  2415. "\nin a \"FILE_SET TYPE CXX_MODULES\" but it is not "
  2416. "scheduled for compilation."));
  2417. }
  2418. if (si.Source->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  2419. e2.Element("PrecompiledHeader", "NotUsing");
  2420. }
  2421. if (!isCSharp && !exclude_configs.empty()) {
  2422. this->WriteExcludeFromBuild(e2, exclude_configs);
  2423. }
  2424. this->FinishWritingSource(e2, toolSettings);
  2425. } else if (fs && fs->GetType() == "CXX_MODULES"_s) {
  2426. this->GeneratorTarget->Makefile->IssueMessage(
  2427. MessageType::FATAL_ERROR,
  2428. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  2429. "\" has source file\n ", si.Source->GetFullPath(),
  2430. "\nin a \"FILE_SET TYPE CXX_MODULES\" but it is not "
  2431. "scheduled for compilation."));
  2432. }
  2433. }
  2434. if (this->IsMissingFiles) {
  2435. this->WriteMissingFiles(e1);
  2436. }
  2437. }
  2438. void cmVisualStudio10TargetGenerator::FinishWritingSource(
  2439. Elem& e2, ConfigToSettings const& toolSettings)
  2440. {
  2441. std::vector<std::string> writtenSettings;
  2442. for (const auto& configSettings : toolSettings) {
  2443. for (const auto& setting : configSettings.second) {
  2444. if (std::find(writtenSettings.begin(), writtenSettings.end(),
  2445. setting.first) != writtenSettings.end()) {
  2446. continue;
  2447. }
  2448. if (PropertyIsSameInAllConfigs(toolSettings, setting.first)) {
  2449. e2.Element(setting.first, setting.second);
  2450. writtenSettings.push_back(setting.first);
  2451. } else {
  2452. e2.WritePlatformConfigTag(setting.first,
  2453. cmStrCat("'$(Configuration)|$(Platform)'=='",
  2454. configSettings.first, '|',
  2455. this->Platform, '\''),
  2456. setting.second);
  2457. }
  2458. }
  2459. }
  2460. }
  2461. void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  2462. Elem& e2, cmSourceFile const* source)
  2463. {
  2464. cmSourceFile const& sf = *source;
  2465. std::string objectName;
  2466. if (this->GeneratorTarget->HasExplicitObjectName(&sf)) {
  2467. objectName = this->GeneratorTarget->GetObjectName(&sf);
  2468. }
  2469. std::string flags;
  2470. bool configDependentFlags = false;
  2471. std::string options;
  2472. bool configDependentOptions = false;
  2473. std::string defines;
  2474. bool configDependentDefines = false;
  2475. std::string includes;
  2476. bool configDependentIncludes = false;
  2477. if (cmValue cflags = sf.GetProperty("COMPILE_FLAGS")) {
  2478. configDependentFlags =
  2479. cmGeneratorExpression::Find(*cflags) != std::string::npos;
  2480. flags += *cflags;
  2481. }
  2482. if (cmValue coptions = sf.GetProperty("COMPILE_OPTIONS")) {
  2483. configDependentOptions =
  2484. cmGeneratorExpression::Find(*coptions) != std::string::npos;
  2485. options += *coptions;
  2486. }
  2487. if (cmValue cdefs = sf.GetProperty("COMPILE_DEFINITIONS")) {
  2488. configDependentDefines =
  2489. cmGeneratorExpression::Find(*cdefs) != std::string::npos;
  2490. defines += *cdefs;
  2491. }
  2492. if (cmValue cincludes = sf.GetProperty("INCLUDE_DIRECTORIES")) {
  2493. configDependentIncludes =
  2494. cmGeneratorExpression::Find(*cincludes) != std::string::npos;
  2495. includes += *cincludes;
  2496. }
  2497. // Force language if the file extension does not match.
  2498. // Note that MSVC treats the upper-case '.C' extension as C and not C++.
  2499. std::string const ext = sf.GetExtension();
  2500. std::string const extLang = ext == "C"_s
  2501. ? "C"
  2502. : this->GlobalGenerator->GetLanguageFromExtension(ext.c_str());
  2503. std::string lang = this->LocalGenerator->GetSourceFileLanguage(sf);
  2504. const char* compileAs = nullptr;
  2505. if (lang != extLang) {
  2506. if (lang == "CXX"_s) {
  2507. // force a C++ file type
  2508. compileAs = "CompileAsCpp";
  2509. } else if (lang == "C"_s) {
  2510. // force to c
  2511. compileAs = "CompileAsC";
  2512. }
  2513. }
  2514. bool noWinRT = this->TargetCompileAsWinRT && lang == "C"_s;
  2515. // for the first time we need a new line if there is something
  2516. // produced here.
  2517. if (!objectName.empty()) {
  2518. if (lang == "CUDA"_s) {
  2519. e2.Element("CompileOut", cmStrCat("$(IntDir)/", objectName));
  2520. } else {
  2521. e2.Element("ObjectFileName", cmStrCat("$(IntDir)/", objectName));
  2522. }
  2523. }
  2524. if (lang == "ASM_NASM"_s) {
  2525. if (cmValue objectDeps = sf.GetProperty("OBJECT_DEPENDS")) {
  2526. cmList depends{ *objectDeps };
  2527. for (auto& d : depends) {
  2528. ConvertToWindowsSlash(d);
  2529. }
  2530. e2.Element("AdditionalDependencies", depends.join(";"));
  2531. }
  2532. }
  2533. bool isCppModule = false;
  2534. for (std::string const& config : this->Configurations) {
  2535. this->GeneratorTarget->NeedCxxModuleSupport(lang, config);
  2536. std::string configUpper = cmSystemTools::UpperCase(config);
  2537. std::string configDefines = defines;
  2538. std::string defPropName = cmStrCat("COMPILE_DEFINITIONS_", configUpper);
  2539. if (cmValue ccdefs = sf.GetProperty(defPropName)) {
  2540. if (!configDefines.empty()) {
  2541. configDefines += ';';
  2542. }
  2543. configDependentDefines |=
  2544. cmGeneratorExpression::Find(*ccdefs) != std::string::npos;
  2545. configDefines += *ccdefs;
  2546. }
  2547. bool const shouldScanForModules = lang == "CXX"_s &&
  2548. this->GeneratorTarget->NeedDyndepForSource(lang, config, source);
  2549. auto const* fs =
  2550. this->GeneratorTarget->GetFileSetForSource(config, source);
  2551. const char* compileAsPerConfig = compileAs;
  2552. if (fs && fs->GetType() == "CXX_MODULES"_s) {
  2553. if (lang == "CXX"_s) {
  2554. if (fs->GetType() == "CXX_MODULES"_s) {
  2555. isCppModule = true;
  2556. if (shouldScanForModules &&
  2557. this->GlobalGenerator->IsScanDependenciesSupported()) {
  2558. // ScanSourceforModuleDependencies uses 'cl /scanDependencies' and
  2559. // can distinguish module interface units and internal partitions.
  2560. compileAsPerConfig = "CompileAsCpp";
  2561. } else {
  2562. compileAsPerConfig = "CompileAsCppModule";
  2563. }
  2564. } else {
  2565. compileAsPerConfig = "CompileAsHeaderUnit";
  2566. }
  2567. } else {
  2568. this->Makefile->IssueMessage(
  2569. MessageType::FATAL_ERROR,
  2570. cmStrCat(
  2571. "Target \"", this->GeneratorTarget->Target->GetName(),
  2572. "\" contains the source\n ", source->GetFullPath(),
  2573. "\nin a file set of type \"", fs->GetType(),
  2574. R"(" but the source is not classified as a "CXX" source.)"));
  2575. }
  2576. }
  2577. // We have pch state in the following situation:
  2578. // 1. We have SKIP_PRECOMPILE_HEADERS == true
  2579. // 2. We are creating the pre-compiled header
  2580. // 3. We are a different language than the linker language AND pch is
  2581. // enabled.
  2582. std::string const& linkLanguage =
  2583. this->GeneratorTarget->GetLinkerLanguage(config);
  2584. std::string const& pchSource =
  2585. this->GeneratorTarget->GetPchSource(config, lang);
  2586. const bool skipPCH =
  2587. pchSource.empty() || sf.GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS");
  2588. const bool makePCH = (sf.GetFullPath() == pchSource);
  2589. const bool useSharedPCH = !skipPCH && (lang == linkLanguage);
  2590. const bool useDifferentLangPCH = !skipPCH && (lang != linkLanguage);
  2591. const bool useNoPCH = skipPCH && (lang != linkLanguage) &&
  2592. !this->GeneratorTarget->GetPchHeader(config, linkLanguage).empty();
  2593. const bool needsPCHFlags =
  2594. (makePCH || useSharedPCH || useDifferentLangPCH || useNoPCH);
  2595. // if we have flags or defines for this config then
  2596. // use them
  2597. if (!flags.empty() || !options.empty() || !configDefines.empty() ||
  2598. !includes.empty() || compileAsPerConfig || noWinRT ||
  2599. !options.empty() || needsPCHFlags) {
  2600. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2601. cmIDEFlagTable const* flagtable = nullptr;
  2602. const std::string& srclang = source->GetLanguage();
  2603. if (srclang == "C"_s || srclang == "CXX"_s) {
  2604. flagtable = gg->GetClFlagTable();
  2605. } else if (srclang == "ASM_MARMASM"_s &&
  2606. this->GlobalGenerator->IsMarmasmEnabled()) {
  2607. flagtable = gg->GetMarmasmFlagTable();
  2608. } else if (srclang == "ASM_MASM"_s &&
  2609. this->GlobalGenerator->IsMasmEnabled()) {
  2610. flagtable = gg->GetMasmFlagTable();
  2611. } else if (lang == "ASM_NASM"_s &&
  2612. this->GlobalGenerator->IsNasmEnabled()) {
  2613. flagtable = gg->GetNasmFlagTable();
  2614. } else if (srclang == "RC"_s) {
  2615. flagtable = gg->GetRcFlagTable();
  2616. } else if (srclang == "CSharp"_s) {
  2617. flagtable = gg->GetCSharpFlagTable();
  2618. }
  2619. cmGeneratorExpressionInterpreter genexInterpreter(
  2620. this->LocalGenerator, config, this->GeneratorTarget, lang);
  2621. cmVS10GeneratorOptions clOptions(
  2622. this->LocalGenerator, cmVisualStudioGeneratorOptions::Compiler,
  2623. flagtable, this);
  2624. if (compileAsPerConfig) {
  2625. clOptions.AddFlag("CompileAs", compileAsPerConfig);
  2626. }
  2627. if (shouldScanForModules) {
  2628. clOptions.AddFlag("ScanSourceforModuleDependencies", "true");
  2629. } else {
  2630. clOptions.AddFlag("ScanSourceforModuleDependencies", "false");
  2631. }
  2632. if (noWinRT) {
  2633. clOptions.AddFlag("CompileAsWinRT", "false");
  2634. }
  2635. if (configDependentFlags) {
  2636. clOptions.Parse(genexInterpreter.Evaluate(flags, "COMPILE_FLAGS"));
  2637. } else {
  2638. clOptions.Parse(flags);
  2639. }
  2640. if (needsPCHFlags) {
  2641. // Add precompile headers compile options.
  2642. if (makePCH) {
  2643. clOptions.AddFlag("PrecompiledHeader", "Create");
  2644. std::string pchHeader =
  2645. this->GeneratorTarget->GetPchHeader(config, lang);
  2646. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2647. std::string pchFile =
  2648. this->GeneratorTarget->GetPchFile(config, lang);
  2649. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  2650. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2651. } else if (useNoPCH) {
  2652. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  2653. } else if (useSharedPCH) {
  2654. std::string pchHeader =
  2655. this->GeneratorTarget->GetPchHeader(config, lang);
  2656. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2657. } else if (useDifferentLangPCH) {
  2658. clOptions.AddFlag("PrecompiledHeader", "Use");
  2659. std::string pchHeader =
  2660. this->GeneratorTarget->GetPchHeader(config, lang);
  2661. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2662. std::string pchFile =
  2663. this->GeneratorTarget->GetPchFile(config, lang);
  2664. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  2665. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2666. }
  2667. }
  2668. if (!options.empty()) {
  2669. std::string expandedOptions;
  2670. if (configDependentOptions) {
  2671. this->LocalGenerator->AppendCompileOptions(
  2672. expandedOptions,
  2673. genexInterpreter.Evaluate(options, "COMPILE_OPTIONS"));
  2674. } else {
  2675. this->LocalGenerator->AppendCompileOptions(expandedOptions, options);
  2676. }
  2677. clOptions.Parse(expandedOptions);
  2678. }
  2679. if (clOptions.HasFlag("DisableSpecificWarnings")) {
  2680. clOptions.AppendFlag("DisableSpecificWarnings",
  2681. "%(DisableSpecificWarnings)");
  2682. }
  2683. if (clOptions.HasFlag("ForcedIncludeFiles")) {
  2684. clOptions.AppendFlag("ForcedIncludeFiles", "%(ForcedIncludeFiles)");
  2685. }
  2686. if (configDependentDefines) {
  2687. clOptions.AddDefines(
  2688. genexInterpreter.Evaluate(configDefines, "COMPILE_DEFINITIONS"));
  2689. } else {
  2690. clOptions.AddDefines(configDefines);
  2691. }
  2692. std::vector<std::string> includeList;
  2693. if (configDependentIncludes) {
  2694. this->LocalGenerator->AppendIncludeDirectories(
  2695. includeList,
  2696. genexInterpreter.Evaluate(includes, "INCLUDE_DIRECTORIES"), *source);
  2697. } else {
  2698. this->LocalGenerator->AppendIncludeDirectories(includeList, includes,
  2699. *source);
  2700. }
  2701. clOptions.AddIncludes(includeList);
  2702. clOptions.SetConfiguration(config);
  2703. OptionsHelper oh(clOptions, e2);
  2704. oh.PrependInheritedString("AdditionalOptions");
  2705. oh.OutputAdditionalIncludeDirectories(lang);
  2706. oh.OutputFlagMap();
  2707. oh.OutputPreprocessorDefinitions(lang);
  2708. }
  2709. }
  2710. if (isCppModule && !objectName.empty()) {
  2711. std::string baseName = cmStrCat("$(IntDir)/", objectName);
  2712. cmStripSuffixIfExists(baseName, ".obj");
  2713. e2.Element("ModuleOutputFile", cmStrCat(baseName, ".ifc"));
  2714. e2.Element("ModuleDependenciesFile", cmStrCat(baseName, ".module.json"));
  2715. }
  2716. if (this->IsXamlSource(source->GetFullPath())) {
  2717. const std::string& fileName = source->GetFullPath();
  2718. e2.Element("DependentUpon",
  2719. fileName.substr(0, fileName.find_last_of('.')));
  2720. }
  2721. if (this->ProjectType == VsProjectType::csproj) {
  2722. using CsPropMap = std::map<std::string, std::string>;
  2723. CsPropMap sourceFileTags;
  2724. this->GetCSharpSourceProperties(&sf, sourceFileTags);
  2725. // write source file specific tags
  2726. if (!sourceFileTags.empty()) {
  2727. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2728. }
  2729. }
  2730. }
  2731. void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild(
  2732. Elem& e2, std::vector<size_t> const& exclude_configs)
  2733. {
  2734. for (size_t ci : exclude_configs) {
  2735. e2.WritePlatformConfigTag("ExcludedFromBuild",
  2736. cmStrCat("'$(Configuration)|$(Platform)'=='",
  2737. this->Configurations[ci], '|',
  2738. this->Platform, '\''),
  2739. "true");
  2740. }
  2741. }
  2742. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions(
  2743. Elem& e0)
  2744. {
  2745. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2746. if (ttype > cmStateEnums::INTERFACE_LIBRARY) {
  2747. return;
  2748. }
  2749. if (this->ProjectType == VsProjectType::csproj) {
  2750. return;
  2751. }
  2752. Elem e1(e0, "PropertyGroup");
  2753. e1.Element("_ProjectFileVersion", "10.0.20506.1");
  2754. for (std::string const& config : this->Configurations) {
  2755. const std::string cond = this->CalcCondition(config);
  2756. if (ttype >= cmStateEnums::UTILITY) {
  2757. e1.WritePlatformConfigTag(
  2758. "IntDir", cond, R"($(Platform)\$(Configuration)\$(ProjectName)\)");
  2759. } else {
  2760. std::string intermediateDir = cmStrCat(
  2761. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  2762. config, '/');
  2763. std::string outDir;
  2764. std::string targetNameFull;
  2765. if (ttype == cmStateEnums::OBJECT_LIBRARY) {
  2766. outDir = intermediateDir;
  2767. targetNameFull = cmStrCat(this->GeneratorTarget->GetName(), ".lib");
  2768. } else {
  2769. outDir = cmStrCat(this->GeneratorTarget->GetDirectory(config), '/');
  2770. targetNameFull = this->GeneratorTarget->GetFullName(config);
  2771. }
  2772. ConvertToWindowsSlash(intermediateDir);
  2773. ConvertToWindowsSlash(outDir);
  2774. e1.WritePlatformConfigTag("OutDir", cond, outDir);
  2775. e1.WritePlatformConfigTag("IntDir", cond, intermediateDir);
  2776. if (cmValue sdkExecutableDirectories = this->Makefile->GetDefinition(
  2777. "CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES")) {
  2778. e1.WritePlatformConfigTag("ExecutablePath", cond,
  2779. *sdkExecutableDirectories);
  2780. }
  2781. if (cmValue sdkIncludeDirectories = this->Makefile->GetDefinition(
  2782. "CMAKE_VS_SDK_INCLUDE_DIRECTORIES")) {
  2783. e1.WritePlatformConfigTag("IncludePath", cond, *sdkIncludeDirectories);
  2784. }
  2785. if (cmValue sdkReferenceDirectories = this->Makefile->GetDefinition(
  2786. "CMAKE_VS_SDK_REFERENCE_DIRECTORIES")) {
  2787. e1.WritePlatformConfigTag("ReferencePath", cond,
  2788. *sdkReferenceDirectories);
  2789. }
  2790. if (cmValue sdkLibraryDirectories = this->Makefile->GetDefinition(
  2791. "CMAKE_VS_SDK_LIBRARY_DIRECTORIES")) {
  2792. e1.WritePlatformConfigTag("LibraryPath", cond, *sdkLibraryDirectories);
  2793. }
  2794. if (cmValue sdkLibraryWDirectories = this->Makefile->GetDefinition(
  2795. "CMAKE_VS_SDK_LIBRARY_WINRT_DIRECTORIES")) {
  2796. e1.WritePlatformConfigTag("LibraryWPath", cond,
  2797. *sdkLibraryWDirectories);
  2798. }
  2799. if (cmValue sdkSourceDirectories =
  2800. this->Makefile->GetDefinition("CMAKE_VS_SDK_SOURCE_DIRECTORIES")) {
  2801. e1.WritePlatformConfigTag("SourcePath", cond, *sdkSourceDirectories);
  2802. }
  2803. if (cmValue sdkExcludeDirectories = this->Makefile->GetDefinition(
  2804. "CMAKE_VS_SDK_EXCLUDE_DIRECTORIES")) {
  2805. e1.WritePlatformConfigTag("ExcludePath", cond, *sdkExcludeDirectories);
  2806. }
  2807. std::string name =
  2808. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  2809. e1.WritePlatformConfigTag("TargetName", cond, name);
  2810. std::string ext =
  2811. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  2812. if (ext.empty()) {
  2813. // An empty TargetExt causes a default extension to be used.
  2814. // A single "." appears to be treated as an empty extension.
  2815. ext = ".";
  2816. }
  2817. e1.WritePlatformConfigTag("TargetExt", cond, ext);
  2818. this->OutputLinkIncremental(e1, config);
  2819. }
  2820. if (ttype <= cmStateEnums::UTILITY) {
  2821. if (cmValue workingDir = this->GeneratorTarget->GetProperty(
  2822. "VS_DEBUGGER_WORKING_DIRECTORY")) {
  2823. std::string genWorkingDir = cmGeneratorExpression::Evaluate(
  2824. *workingDir, this->LocalGenerator, config);
  2825. e1.WritePlatformConfigTag("LocalDebuggerWorkingDirectory", cond,
  2826. genWorkingDir);
  2827. }
  2828. if (cmValue environment =
  2829. this->GeneratorTarget->GetProperty("VS_DEBUGGER_ENVIRONMENT")) {
  2830. std::string genEnvironment = cmGeneratorExpression::Evaluate(
  2831. *environment, this->LocalGenerator, config);
  2832. e1.WritePlatformConfigTag("LocalDebuggerEnvironment", cond,
  2833. genEnvironment);
  2834. }
  2835. if (cmValue debuggerCommand =
  2836. this->GeneratorTarget->GetProperty("VS_DEBUGGER_COMMAND")) {
  2837. std::string genDebuggerCommand = cmGeneratorExpression::Evaluate(
  2838. *debuggerCommand, this->LocalGenerator, config);
  2839. e1.WritePlatformConfigTag("LocalDebuggerCommand", cond,
  2840. genDebuggerCommand);
  2841. }
  2842. if (cmValue commandArguments = this->GeneratorTarget->GetProperty(
  2843. "VS_DEBUGGER_COMMAND_ARGUMENTS")) {
  2844. std::string genCommandArguments = cmGeneratorExpression::Evaluate(
  2845. *commandArguments, this->LocalGenerator, config);
  2846. e1.WritePlatformConfigTag("LocalDebuggerCommandArguments", cond,
  2847. genCommandArguments);
  2848. }
  2849. }
  2850. }
  2851. }
  2852. void cmVisualStudio10TargetGenerator::WritePublicProjectContentOptions(
  2853. Elem& e0)
  2854. {
  2855. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2856. if (ttype != cmStateEnums::SHARED_LIBRARY) {
  2857. return;
  2858. }
  2859. if (this->ProjectType != VsProjectType::vcxproj) {
  2860. return;
  2861. }
  2862. Elem e1(e0, "PropertyGroup");
  2863. for (std::string const& config : this->Configurations) {
  2864. if (this->GeneratorTarget->HaveCxx20ModuleSources() &&
  2865. this->GeneratorTarget->HaveCxxModuleSupport(config) ==
  2866. cmGeneratorTarget::Cxx20SupportLevel::Supported) {
  2867. const std::string cond = this->CalcCondition(config);
  2868. // For DLL projects, we export all BMIs for now
  2869. e1.WritePlatformConfigTag("AllProjectBMIsArePublic", cond, "true");
  2870. }
  2871. }
  2872. }
  2873. void cmVisualStudio10TargetGenerator::OutputLinkIncremental(
  2874. Elem& e1, std::string const& configName)
  2875. {
  2876. if (!this->MSTools) {
  2877. return;
  2878. }
  2879. if (this->ProjectType == VsProjectType::csproj) {
  2880. return;
  2881. }
  2882. // static libraries and things greater than modules do not need
  2883. // to set this option
  2884. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2885. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  2886. return;
  2887. }
  2888. Options& linkOptions = *(this->LinkOptions[configName]);
  2889. const std::string cond = this->CalcCondition(configName);
  2890. if (this->IPOEnabledConfigurations.count(configName) == 0) {
  2891. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  2892. e1.WritePlatformConfigTag("LinkIncremental", cond,
  2893. (incremental ? incremental : "true"));
  2894. }
  2895. linkOptions.RemoveFlag("LinkIncremental");
  2896. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  2897. e1.WritePlatformConfigTag("GenerateManifest", cond,
  2898. (manifest ? manifest : "true"));
  2899. linkOptions.RemoveFlag("GenerateManifest");
  2900. // Some link options belong here. Use them now and remove them so that
  2901. // WriteLinkOptions does not use them.
  2902. static const std::vector<std::string> flags{ "LinkDelaySign",
  2903. "LinkKeyFile" };
  2904. for (const std::string& flag : flags) {
  2905. if (const char* value = linkOptions.GetFlag(flag)) {
  2906. e1.WritePlatformConfigTag(flag, cond, value);
  2907. linkOptions.RemoveFlag(flag);
  2908. }
  2909. }
  2910. }
  2911. std::vector<std::string> cmVisualStudio10TargetGenerator::GetIncludes(
  2912. std::string const& config, std::string const& lang) const
  2913. {
  2914. std::vector<std::string> includes;
  2915. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  2916. lang, config);
  2917. for (std::string& i : includes) {
  2918. ConvertToWindowsSlash(i);
  2919. }
  2920. return includes;
  2921. }
  2922. std::string cmVisualStudio10TargetGenerator::GetTargetOutputName() const
  2923. {
  2924. std::string config;
  2925. if (!this->Configurations.empty()) {
  2926. config = this->Configurations[0];
  2927. }
  2928. const auto& nameComponents =
  2929. this->GeneratorTarget->GetFullNameComponents(config);
  2930. return cmStrCat(nameComponents.prefix, nameComponents.base);
  2931. }
  2932. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  2933. {
  2934. return std::all_of(
  2935. this->Configurations.begin(), this->Configurations.end(),
  2936. [this](std::string const& c) { return this->ComputeClOptions(c); });
  2937. }
  2938. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  2939. std::string const& configName)
  2940. {
  2941. // much of this was copied from here:
  2942. // copied from cmLocalVisualStudio7Generator.cxx 805
  2943. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  2944. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2945. std::unique_ptr<Options> pOptions;
  2946. switch (this->ProjectType) {
  2947. case VsProjectType::vcxproj:
  2948. pOptions = cm::make_unique<Options>(
  2949. this->LocalGenerator, Options::Compiler, gg->GetClFlagTable());
  2950. break;
  2951. case VsProjectType::csproj:
  2952. pOptions =
  2953. cm::make_unique<Options>(this->LocalGenerator, Options::CSharpCompiler,
  2954. gg->GetCSharpFlagTable());
  2955. break;
  2956. default:
  2957. break;
  2958. }
  2959. Options& clOptions = *pOptions;
  2960. std::string flags;
  2961. const std::string& linkLanguage =
  2962. this->GeneratorTarget->GetLinkerLanguage(configName);
  2963. if (linkLanguage.empty()) {
  2964. cmSystemTools::Error(cmStrCat(
  2965. "CMake can not determine linker language for target: ", this->Name));
  2966. return false;
  2967. }
  2968. // Choose a language whose flags to use for ClCompile.
  2969. static const char* clLangs[] = { "CXX", "C", "Fortran" };
  2970. std::string langForClCompile;
  2971. if (this->ProjectType == VsProjectType::csproj) {
  2972. langForClCompile = "CSharp";
  2973. } else if (cm::contains(clLangs, linkLanguage)) {
  2974. langForClCompile = linkLanguage;
  2975. } else {
  2976. std::set<std::string> languages;
  2977. this->GeneratorTarget->GetLanguages(languages, configName);
  2978. for (const char* l : clLangs) {
  2979. if (languages.count(l)) {
  2980. langForClCompile = l;
  2981. break;
  2982. }
  2983. }
  2984. }
  2985. this->LangForClCompile = langForClCompile;
  2986. if (!langForClCompile.empty()) {
  2987. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  2988. cmBuildStep::Compile,
  2989. langForClCompile, configName);
  2990. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  2991. langForClCompile, configName);
  2992. }
  2993. // Put the IPO enabled configurations into a set.
  2994. if (this->GeneratorTarget->IsIPOEnabled(linkLanguage, configName)) {
  2995. this->IPOEnabledConfigurations.insert(configName);
  2996. }
  2997. // Check if ASan is enabled.
  2998. if (flags.find("/fsanitize=address") != std::string::npos ||
  2999. flags.find("-fsanitize=address") != std::string::npos) {
  3000. this->ASanEnabledConfigurations.insert(configName);
  3001. }
  3002. // Check if (lib)Fuzzer is enabled.
  3003. if (flags.find("/fsanitize=fuzzer") != std::string::npos ||
  3004. flags.find("-fsanitize=fuzzer") != std::string::npos) {
  3005. this->FuzzerEnabledConfigurations.insert(configName);
  3006. }
  3007. // Precompile Headers
  3008. std::string pchHeader =
  3009. this->GeneratorTarget->GetPchHeader(configName, linkLanguage);
  3010. if (this->MSTools && VsProjectType::vcxproj == this->ProjectType &&
  3011. pchHeader.empty()) {
  3012. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  3013. } else if (this->MSTools && VsProjectType::vcxproj == this->ProjectType &&
  3014. !pchHeader.empty()) {
  3015. clOptions.AddFlag("PrecompiledHeader", "Use");
  3016. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  3017. std::string pchFile =
  3018. this->GeneratorTarget->GetPchFile(configName, linkLanguage);
  3019. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  3020. }
  3021. // Get preprocessor definitions for this directory.
  3022. std::string defineFlags = this->Makefile->GetDefineFlags();
  3023. if (this->MSTools) {
  3024. if (this->ProjectType == VsProjectType::vcxproj) {
  3025. clOptions.FixExceptionHandlingDefault();
  3026. if (this->GlobalGenerator->GetVersion() >=
  3027. cmGlobalVisualStudioGenerator::VSVersion::VS15) {
  3028. // Toolsets that come with VS 2017 may now enable UseFullPaths
  3029. // by default and there is no negative /FC option that projects
  3030. // can use to switch it back. Older toolsets disable this by
  3031. // default anyway so this will not hurt them. If the project
  3032. // is using an explicit /FC option then parsing flags will
  3033. // replace this setting with "true" below.
  3034. clOptions.AddFlag("UseFullPaths", "false");
  3035. }
  3036. clOptions.AddFlag("AssemblerListingLocation", "$(IntDir)");
  3037. }
  3038. }
  3039. // check for managed C++ assembly compiler flag. This overrides any
  3040. // /clr* compiler flags which may be defined in the flags variable(s).
  3041. if (this->ProjectType != VsProjectType::csproj) {
  3042. // Warn if /clr was added manually. This should not be done
  3043. // anymore, because cmGeneratorTarget may not be aware that the
  3044. // target uses C++/CLI.
  3045. if (flags.find("/clr") != std::string::npos ||
  3046. flags.find("-clr") != std::string::npos ||
  3047. defineFlags.find("/clr") != std::string::npos ||
  3048. defineFlags.find("-clr") != std::string::npos) {
  3049. if (configName == this->Configurations[0]) {
  3050. std::string message =
  3051. cmStrCat("For the target \"", this->GeneratorTarget->GetName(),
  3052. "\" the /clr compiler flag was added manually. ",
  3053. "Set usage of C++/CLI by setting COMMON_LANGUAGE_RUNTIME "
  3054. "target property.");
  3055. this->Makefile->IssueMessage(MessageType::WARNING, message);
  3056. }
  3057. }
  3058. if (cmValue clr =
  3059. this->GeneratorTarget->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  3060. std::string clrString = *clr;
  3061. if (!clrString.empty()) {
  3062. clrString = cmStrCat(':', clrString);
  3063. }
  3064. flags += cmStrCat(" /clr", clrString);
  3065. }
  3066. }
  3067. // Get includes for this target
  3068. if (!this->LangForClCompile.empty()) {
  3069. auto includeList = this->GetIncludes(configName, this->LangForClCompile);
  3070. auto sysIncludeFlag = this->Makefile->GetDefinition(
  3071. cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", this->LangForClCompile));
  3072. if (sysIncludeFlag) {
  3073. bool gotOneSys = false;
  3074. for (auto i : includeList) {
  3075. cmSystemTools::ConvertToUnixSlashes(i);
  3076. if (this->GeneratorTarget->IsSystemIncludeDirectory(
  3077. i, configName, this->LangForClCompile)) {
  3078. auto flag = cmTrimWhitespace(*sysIncludeFlag);
  3079. if (this->MSTools) {
  3080. cmSystemTools::ReplaceString(flag, "-external:I", "/external:I");
  3081. }
  3082. clOptions.AppendFlagString("AdditionalOptions",
  3083. cmStrCat(flag, " \"", i, '"'));
  3084. gotOneSys = true;
  3085. } else {
  3086. clOptions.AddInclude(i);
  3087. }
  3088. }
  3089. if (gotOneSys) {
  3090. if (auto sysIncludeFlagWarning = this->Makefile->GetDefinition(
  3091. cmStrCat("_CMAKE_INCLUDE_SYSTEM_FLAG_", this->LangForClCompile,
  3092. "_WARNING"))) {
  3093. flags = cmStrCat(flags, ' ', *sysIncludeFlagWarning);
  3094. }
  3095. }
  3096. } else {
  3097. clOptions.AddIncludes(includeList);
  3098. }
  3099. }
  3100. clOptions.Parse(flags);
  3101. clOptions.Parse(defineFlags);
  3102. std::vector<std::string> targetDefines;
  3103. switch (this->ProjectType) {
  3104. case VsProjectType::vcxproj:
  3105. if (!langForClCompile.empty()) {
  3106. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3107. langForClCompile);
  3108. }
  3109. break;
  3110. case VsProjectType::csproj:
  3111. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3112. "CSharp");
  3113. cm::erase_if(targetDefines, [](std::string const& def) {
  3114. return def.find('=') != std::string::npos;
  3115. });
  3116. break;
  3117. default:
  3118. break;
  3119. }
  3120. clOptions.AddDefines(targetDefines);
  3121. if (this->ProjectType == VsProjectType::csproj) {
  3122. clOptions.AppendFlag("DefineConstants", targetDefines);
  3123. }
  3124. if (this->MSTools) {
  3125. clOptions.SetVerboseMakefile(
  3126. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  3127. }
  3128. // Add C-specific flags expressible in a ClCompile meant for C++.
  3129. if (langForClCompile == "CXX"_s) {
  3130. std::set<std::string> languages;
  3131. this->GeneratorTarget->GetLanguages(languages, configName);
  3132. if (languages.count("C")) {
  3133. std::string flagsC;
  3134. this->LocalGenerator->AddLanguageFlags(
  3135. flagsC, this->GeneratorTarget, cmBuildStep::Compile, "C", configName);
  3136. this->LocalGenerator->AddCompileOptions(flagsC, this->GeneratorTarget,
  3137. "C", configName);
  3138. Options optC(this->LocalGenerator, Options::Compiler,
  3139. gg->GetClFlagTable());
  3140. optC.Parse(flagsC);
  3141. if (const char* stdC = optC.GetFlag("LanguageStandard_C")) {
  3142. clOptions.AddFlag("LanguageStandard_C", stdC);
  3143. }
  3144. }
  3145. }
  3146. // Add a definition for the configuration name.
  3147. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  3148. clOptions.AddDefine(configDefine);
  3149. if (const std::string* exportMacro =
  3150. this->GeneratorTarget->GetExportMacro()) {
  3151. clOptions.AddDefine(*exportMacro);
  3152. }
  3153. if (this->MSTools) {
  3154. // If we have the VS_WINRT_COMPONENT set then force Compile as WinRT
  3155. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT")) {
  3156. clOptions.AddFlag("CompileAsWinRT", "true");
  3157. // For WinRT components, add the _WINRT_DLL define to produce a lib
  3158. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3159. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3160. clOptions.AddDefine("_WINRT_DLL");
  3161. }
  3162. } else if (this->GlobalGenerator->TargetsWindowsStore() ||
  3163. this->GlobalGenerator->TargetsWindowsPhone() ||
  3164. this->Makefile->IsOn("CMAKE_VS_WINRT_BY_DEFAULT")) {
  3165. if (!clOptions.IsWinRt()) {
  3166. clOptions.AddFlag("CompileAsWinRT", "false");
  3167. }
  3168. }
  3169. if (const char* winRT = clOptions.GetFlag("CompileAsWinRT")) {
  3170. if (cmIsOn(winRT)) {
  3171. this->TargetCompileAsWinRT = true;
  3172. }
  3173. }
  3174. }
  3175. if (this->ProjectType != VsProjectType::csproj &&
  3176. (clOptions.IsManaged() || clOptions.HasFlag("CLRSupport"))) {
  3177. this->Managed = true;
  3178. std::string managedType = clOptions.HasFlag("CompileAsManaged")
  3179. ? clOptions.GetFlag("CompileAsManaged")
  3180. : "Mixed";
  3181. if (managedType == "Safe"_s || managedType == "Pure"_s) {
  3182. // force empty calling convention if safe clr is used
  3183. clOptions.AddFlag("CallingConvention", "");
  3184. }
  3185. // The default values of these flags are incompatible to
  3186. // managed assemblies. We have to force valid values if
  3187. // the target is a managed C++ target.
  3188. clOptions.AddFlag("ExceptionHandling", "Async");
  3189. clOptions.AddFlag("BasicRuntimeChecks", "Default");
  3190. }
  3191. if (this->ProjectType == VsProjectType::csproj) {
  3192. // /nowin32manifest overrides /win32manifest: parameter
  3193. if (clOptions.HasFlag("NoWin32Manifest")) {
  3194. clOptions.RemoveFlag("ApplicationManifest");
  3195. }
  3196. }
  3197. if (const char* s = clOptions.GetFlag("SpectreMitigation")) {
  3198. this->SpectreMitigation[configName] = s;
  3199. clOptions.RemoveFlag("SpectreMitigation");
  3200. }
  3201. // Remove any target-wide -TC or -TP flag added by the project.
  3202. // Such flags are unnecessary and break our model of language selection.
  3203. if (langForClCompile == "C"_s || langForClCompile == "CXX"_s) {
  3204. clOptions.RemoveFlag("CompileAs");
  3205. }
  3206. this->ClOptions[configName] = std::move(pOptions);
  3207. return true;
  3208. }
  3209. void cmVisualStudio10TargetGenerator::WriteClOptions(
  3210. Elem& e1, std::string const& configName)
  3211. {
  3212. Options& clOptions = *(this->ClOptions[configName]);
  3213. if (this->ProjectType == VsProjectType::csproj) {
  3214. return;
  3215. }
  3216. Elem e2(e1, "ClCompile");
  3217. OptionsHelper oh(clOptions, e2);
  3218. oh.PrependInheritedString("AdditionalOptions");
  3219. oh.OutputAdditionalIncludeDirectories(this->LangForClCompile);
  3220. oh.OutputFlagMap();
  3221. oh.OutputPreprocessorDefinitions(this->LangForClCompile);
  3222. if (this->NsightTegra) {
  3223. if (cmValue processMax =
  3224. this->GeneratorTarget->GetProperty("ANDROID_PROCESS_MAX")) {
  3225. e2.Element("ProcessMax", *processMax);
  3226. }
  3227. }
  3228. if (this->Android) {
  3229. e2.Element("ObjectFileName", "$(IntDir)%(filename).o");
  3230. } else if (this->MSTools) {
  3231. cmsys::RegularExpression clangToolset("v[0-9]+_clang_.*");
  3232. const char* toolset = this->GlobalGenerator->GetPlatformToolset();
  3233. cmValue noCompileBatching =
  3234. this->GeneratorTarget->GetProperty("VS_NO_COMPILE_BATCHING");
  3235. if (noCompileBatching.IsOn() || (toolset && clangToolset.find(toolset))) {
  3236. e2.Element("ObjectFileName", "$(IntDir)%(filename).obj");
  3237. } else {
  3238. e2.Element("ObjectFileName", "$(IntDir)");
  3239. }
  3240. // If not in debug mode, write the DebugInformationFormat field
  3241. // without value so PDBs don't get generated uselessly. Each tag
  3242. // goes on its own line because Visual Studio corrects it this
  3243. // way when saving the project after CMake generates it.
  3244. if (!clOptions.IsDebug()) {
  3245. Elem e3(e2, "DebugInformationFormat");
  3246. e3.SetHasElements();
  3247. }
  3248. // Specify the compiler program database file if configured.
  3249. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  3250. if (!pdb.empty()) {
  3251. if (this->GlobalGenerator->IsCudaEnabled()) {
  3252. // CUDA does not quote paths with spaces correctly when forwarding
  3253. // this to the host compiler. Use a relative path to avoid spaces.
  3254. // FIXME: We can likely do this even when CUDA is not involved,
  3255. // but for now we will make a minimal change.
  3256. pdb = this->ConvertPath(pdb, true);
  3257. }
  3258. ConvertToWindowsSlash(pdb);
  3259. e2.Element("ProgramDataBaseFileName", pdb);
  3260. }
  3261. // add AdditionalUsingDirectories
  3262. if (this->AdditionalUsingDirectories.count(configName) > 0) {
  3263. std::string dirs;
  3264. for (auto const& u : this->AdditionalUsingDirectories[configName]) {
  3265. if (!dirs.empty()) {
  3266. dirs.append(";");
  3267. }
  3268. dirs.append(u);
  3269. }
  3270. e2.Element("AdditionalUsingDirectories", dirs);
  3271. }
  3272. }
  3273. }
  3274. bool cmVisualStudio10TargetGenerator::ComputeRcOptions()
  3275. {
  3276. return std::all_of(
  3277. this->Configurations.begin(), this->Configurations.end(),
  3278. [this](std::string const& c) { return this->ComputeRcOptions(c); });
  3279. }
  3280. bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
  3281. std::string const& configName)
  3282. {
  3283. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3284. auto pOptions = cm::make_unique<Options>(
  3285. this->LocalGenerator, Options::ResourceCompiler, gg->GetRcFlagTable());
  3286. Options& rcOptions = *pOptions;
  3287. std::string CONFIG = cmSystemTools::UpperCase(configName);
  3288. std::string rcConfigFlagsVar = cmStrCat("CMAKE_RC_FLAGS_", CONFIG);
  3289. std::string flags =
  3290. cmStrCat(this->Makefile->GetSafeDefinition("CMAKE_RC_FLAGS"), ' ',
  3291. this->Makefile->GetSafeDefinition(rcConfigFlagsVar));
  3292. rcOptions.Parse(flags);
  3293. // For historical reasons, add the C preprocessor defines to RC.
  3294. Options& clOptions = *(this->ClOptions[configName]);
  3295. rcOptions.AddDefines(clOptions.GetDefines());
  3296. // Get includes for this target
  3297. rcOptions.AddIncludes(this->GetIncludes(configName, "RC"));
  3298. this->RcOptions[configName] = std::move(pOptions);
  3299. return true;
  3300. }
  3301. void cmVisualStudio10TargetGenerator::WriteRCOptions(
  3302. Elem& e1, std::string const& configName)
  3303. {
  3304. if (!this->MSTools) {
  3305. return;
  3306. }
  3307. Elem e2(e1, "ResourceCompile");
  3308. OptionsHelper rcOptions(*(this->RcOptions[configName]), e2);
  3309. rcOptions.OutputPreprocessorDefinitions("RC");
  3310. rcOptions.OutputAdditionalIncludeDirectories("RC");
  3311. rcOptions.PrependInheritedString("AdditionalOptions");
  3312. rcOptions.OutputFlagMap();
  3313. }
  3314. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions()
  3315. {
  3316. if (!this->GlobalGenerator->IsCudaEnabled()) {
  3317. return true;
  3318. }
  3319. return std::all_of(this->Configurations.begin(), this->Configurations.end(),
  3320. [this](std::string const& c) {
  3321. return !this->GeneratorTarget->IsLanguageUsed("CUDA",
  3322. c) ||
  3323. this->ComputeCudaOptions(c);
  3324. });
  3325. }
  3326. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
  3327. std::string const& configName)
  3328. {
  3329. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3330. auto pOptions = cm::make_unique<Options>(
  3331. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  3332. Options& cudaOptions = *pOptions;
  3333. auto cudaVersion = this->GlobalGenerator->GetPlatformToolsetCudaString();
  3334. // Get compile flags for CUDA in this directory.
  3335. std::string flags;
  3336. this->LocalGenerator->AddLanguageFlags(
  3337. flags, this->GeneratorTarget, cmBuildStep::Compile, "CUDA", configName);
  3338. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, "CUDA",
  3339. configName);
  3340. // Get preprocessor definitions for this directory.
  3341. std::string defineFlags = this->Makefile->GetDefineFlags();
  3342. cudaOptions.Parse(flags);
  3343. cudaOptions.Parse(defineFlags);
  3344. cudaOptions.ParseFinish();
  3345. // If we haven't explicitly enabled GPU debug information
  3346. // explicitly disable it
  3347. if (!cudaOptions.HasFlag("GPUDebugInfo")) {
  3348. cudaOptions.AddFlag("GPUDebugInfo", "false");
  3349. }
  3350. // The extension on object libraries the CUDA gives isn't
  3351. // consistent with how MSVC generates object libraries for C+, so set
  3352. // the default to not have any extension
  3353. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).obj");
  3354. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  3355. cudaOptions.AddFlag("GenerateRelocatableDeviceCode", "true");
  3356. }
  3357. bool notPtxLike = true;
  3358. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) {
  3359. cudaOptions.AddFlag("NvccCompilation", "ptx");
  3360. // We drop the %(Extension) component as CMake expects all PTX files
  3361. // to not have the source file extension at all
  3362. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).ptx");
  3363. notPtxLike = false;
  3364. if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  3365. cudaVersion, "9.0") &&
  3366. cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, cudaVersion,
  3367. "11.5")) {
  3368. // The DriverApi flag before 11.5 ( verified back to 9.0 ) which controls
  3369. // PTX compilation doesn't propagate user defines causing
  3370. // target_compile_definitions to behave differently for VS +
  3371. // PTX compared to other generators so we patch the rules
  3372. // to normalize behavior
  3373. cudaOptions.AddFlag("DriverApiCommandLineTemplate",
  3374. "%(BaseCommandLineTemplate) [CompileOut] [FastMath] "
  3375. "[Defines] \"%(FullPath)\"");
  3376. }
  3377. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3378. "CUDA_CUBIN_COMPILATION")) {
  3379. cudaOptions.AddFlag("NvccCompilation", "cubin");
  3380. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).cubin");
  3381. notPtxLike = false;
  3382. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3383. "CUDA_FATBIN_COMPILATION")) {
  3384. cudaOptions.AddFlag("NvccCompilation", "fatbin");
  3385. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).fatbin");
  3386. notPtxLike = false;
  3387. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3388. "CUDA_OPTIX_COMPILATION")) {
  3389. cudaOptions.AddFlag("NvccCompilation", "optix-ir");
  3390. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).optixir");
  3391. notPtxLike = false;
  3392. }
  3393. if (notPtxLike &&
  3394. cmSystemTools::VersionCompareGreaterEq(
  3395. "8.0", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  3396. // Explicitly state that we want this file to be treated as a
  3397. // CUDA file no matter what the file extensions is
  3398. // This is only needed for < CUDA 9
  3399. cudaOptions.AppendFlagString("AdditionalOptions", "-x cu");
  3400. }
  3401. // Specify the compiler program database file if configured.
  3402. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  3403. if (!pdb.empty()) {
  3404. // CUDA does not make the directory if it is non-standard.
  3405. std::string const pdbDir = cmSystemTools::GetFilenamePath(pdb);
  3406. cmSystemTools::MakeDirectory(pdbDir);
  3407. if (cmSystemTools::VersionCompareGreaterEq(
  3408. "9.2", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  3409. // CUDA does not have a field for this and does not honor the
  3410. // ProgramDataBaseFileName field in ClCompile. Work around this
  3411. // limitation by creating the directory and passing the flag ourselves.
  3412. pdb = this->ConvertPath(pdb, true);
  3413. ConvertToWindowsSlash(pdb);
  3414. std::string const clFd = cmStrCat(R"(-Xcompiler="-Fd\")", pdb, R"(\"")");
  3415. cudaOptions.AppendFlagString("AdditionalOptions", clFd);
  3416. }
  3417. }
  3418. // CUDA automatically passes the proper '--machine' flag to nvcc
  3419. // for the current architecture, but does not reflect this default
  3420. // in the user-visible IDE settings. Set it explicitly.
  3421. if (this->Platform == "x64"_s) {
  3422. cudaOptions.AddFlag("TargetMachinePlatform", "64");
  3423. }
  3424. // Convert the host compiler options to the toolset's abstractions
  3425. // using a secondary flag table.
  3426. cudaOptions.ClearTables();
  3427. cudaOptions.AddTable(gg->GetCudaHostFlagTable());
  3428. cudaOptions.Reparse("AdditionalCompilerOptions");
  3429. // `CUDA 8.0.targets` places AdditionalCompilerOptions before nvcc!
  3430. // Pass them through -Xcompiler in AdditionalOptions instead.
  3431. if (const char* acoPtr = cudaOptions.GetFlag("AdditionalCompilerOptions")) {
  3432. std::string aco = acoPtr;
  3433. cudaOptions.RemoveFlag("AdditionalCompilerOptions");
  3434. if (!aco.empty()) {
  3435. aco = this->LocalGenerator->EscapeForShell(aco, false);
  3436. cudaOptions.AppendFlagString("AdditionalOptions",
  3437. cmStrCat("-Xcompiler=", aco));
  3438. }
  3439. }
  3440. cudaOptions.FixCudaCodeGeneration();
  3441. std::vector<std::string> targetDefines;
  3442. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3443. "CUDA");
  3444. cudaOptions.AddDefines(targetDefines);
  3445. // Add a definition for the configuration name.
  3446. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  3447. cudaOptions.AddDefine(configDefine);
  3448. if (const std::string* exportMacro =
  3449. this->GeneratorTarget->GetExportMacro()) {
  3450. cudaOptions.AddDefine(*exportMacro);
  3451. }
  3452. // Get includes for this target
  3453. cudaOptions.AddIncludes(this->GetIncludes(configName, "CUDA"));
  3454. cudaOptions.AddFlag("UseHostInclude", "false");
  3455. // Add runtime library selection flag.
  3456. std::string const& cudaRuntime =
  3457. this->GeneratorTarget->GetRuntimeLinkLibrary("CUDA", configName);
  3458. if (cudaRuntime == "STATIC"_s) {
  3459. cudaOptions.AddFlag("CudaRuntime", "Static");
  3460. } else if (cudaRuntime == "SHARED"_s) {
  3461. cudaOptions.AddFlag("CudaRuntime", "Shared");
  3462. } else if (cudaRuntime == "NONE"_s) {
  3463. cudaOptions.AddFlag("CudaRuntime", "None");
  3464. }
  3465. this->CudaOptions[configName] = std::move(pOptions);
  3466. return true;
  3467. }
  3468. void cmVisualStudio10TargetGenerator::WriteCudaOptions(
  3469. Elem& e1, std::string const& configName)
  3470. {
  3471. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled() ||
  3472. !this->GeneratorTarget->IsLanguageUsed("CUDA", configName)) {
  3473. return;
  3474. }
  3475. Elem e2(e1, "CudaCompile");
  3476. OptionsHelper cudaOptions(*(this->CudaOptions[configName]), e2);
  3477. cudaOptions.OutputAdditionalIncludeDirectories("CUDA");
  3478. cudaOptions.OutputPreprocessorDefinitions("CUDA");
  3479. cudaOptions.PrependInheritedString("AdditionalOptions");
  3480. cudaOptions.OutputFlagMap();
  3481. }
  3482. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions()
  3483. {
  3484. if (!this->GlobalGenerator->IsCudaEnabled()) {
  3485. return true;
  3486. }
  3487. return std::all_of(
  3488. this->Configurations.begin(), this->Configurations.end(),
  3489. [this](std::string const& c) { return this->ComputeCudaLinkOptions(c); });
  3490. }
  3491. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions(
  3492. std::string const& configName)
  3493. {
  3494. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3495. auto pOptions = cm::make_unique<Options>(
  3496. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  3497. Options& cudaLinkOptions = *pOptions;
  3498. cmGeneratorTarget::DeviceLinkSetter setter(*this->GeneratorTarget);
  3499. // Determine if we need to do a device link
  3500. const bool doDeviceLinking = requireDeviceLinking(
  3501. *this->GeneratorTarget, *this->LocalGenerator, configName);
  3502. cudaLinkOptions.AddFlag("PerformDeviceLink",
  3503. doDeviceLinking ? "true" : "false");
  3504. // Add extra flags for device linking
  3505. cudaLinkOptions.AppendFlagString(
  3506. "AdditionalOptions",
  3507. this->Makefile->GetSafeDefinition("_CMAKE_CUDA_EXTRA_FLAGS"));
  3508. cudaLinkOptions.AppendFlagString(
  3509. "AdditionalOptions",
  3510. this->Makefile->GetSafeDefinition("_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS"));
  3511. std::vector<std::string> linkOpts;
  3512. std::string linkFlags;
  3513. this->GeneratorTarget->GetLinkOptions(linkOpts, configName, "CUDA");
  3514. // LINK_OPTIONS are escaped.
  3515. this->LocalGenerator->AppendCompileOptions(linkFlags, linkOpts);
  3516. cmComputeLinkInformation* pcli =
  3517. this->GeneratorTarget->GetLinkInformation(configName);
  3518. if (doDeviceLinking && pcli) {
  3519. cmLinkLineDeviceComputer computer(
  3520. this->LocalGenerator,
  3521. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  3522. std::string ignored_;
  3523. this->LocalGenerator->GetDeviceLinkFlags(computer, configName, ignored_,
  3524. linkFlags, ignored_, ignored_,
  3525. this->GeneratorTarget);
  3526. this->LocalGenerator->AddLanguageFlagsForLinking(
  3527. linkFlags, this->GeneratorTarget, "CUDA", configName);
  3528. }
  3529. cudaLinkOptions.AppendFlagString("AdditionalOptions", linkFlags);
  3530. if (doDeviceLinking) {
  3531. std::vector<std::string> libVec;
  3532. auto const& kinded = this->GeneratorTarget->GetKindedSources(configName);
  3533. // CMake conversion uses full paths when possible to allow deeper trees.
  3534. // However, CUDA 8.0 msbuild rules fail on absolute paths so for CUDA
  3535. // we must use relative paths.
  3536. const bool forceRelative = true;
  3537. for (cmGeneratorTarget::SourceAndKind const& si : kinded.Sources) {
  3538. switch (si.Kind) {
  3539. case cmGeneratorTarget::SourceKindExternalObject: {
  3540. std::string path =
  3541. this->ConvertPath(si.Source.Value->GetFullPath(), forceRelative);
  3542. ConvertToWindowsSlash(path);
  3543. libVec.emplace_back(std::move(path));
  3544. } break;
  3545. default:
  3546. break;
  3547. }
  3548. }
  3549. // For static libraries that have device linking enabled compute
  3550. // the libraries
  3551. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3552. cmComputeLinkInformation& cli = *pcli;
  3553. cmLinkLineDeviceComputer computer(
  3554. this->LocalGenerator,
  3555. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  3556. std::vector<BT<std::string>> btLibVec;
  3557. computer.ComputeLinkLibraries(cli, std::string{}, btLibVec);
  3558. for (auto const& item : btLibVec) {
  3559. libVec.emplace_back(item.Value);
  3560. }
  3561. }
  3562. if (!libVec.empty()) {
  3563. cudaLinkOptions.AddFlag("AdditionalDependencies", libVec);
  3564. }
  3565. }
  3566. this->CudaLinkOptions[configName] = std::move(pOptions);
  3567. return true;
  3568. }
  3569. void cmVisualStudio10TargetGenerator::WriteCudaLinkOptions(
  3570. Elem& e1, std::string const& configName)
  3571. {
  3572. // We need to write link options for OBJECT libraries so that
  3573. // we override the default device link behavior ( enabled ) when
  3574. // building object libraries with ptx/optix-ir/etc
  3575. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  3576. return;
  3577. }
  3578. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  3579. return;
  3580. }
  3581. Elem e2(e1, "CudaLink");
  3582. OptionsHelper cudaLinkOptions(*(this->CudaLinkOptions[configName]), e2);
  3583. cudaLinkOptions.OutputFlagMap();
  3584. }
  3585. bool cmVisualStudio10TargetGenerator::ComputeMarmasmOptions()
  3586. {
  3587. if (!this->GlobalGenerator->IsMarmasmEnabled()) {
  3588. return true;
  3589. }
  3590. return std::all_of(
  3591. this->Configurations.begin(), this->Configurations.end(),
  3592. [this](std::string const& c) { return this->ComputeMarmasmOptions(c); });
  3593. }
  3594. bool cmVisualStudio10TargetGenerator::ComputeMarmasmOptions(
  3595. std::string const& configName)
  3596. {
  3597. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3598. auto pOptions = cm::make_unique<Options>(
  3599. this->LocalGenerator, Options::MarmasmCompiler, gg->GetMarmasmFlagTable());
  3600. Options& marmasmOptions = *pOptions;
  3601. std::string flags;
  3602. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3603. cmBuildStep::Compile, "ASM_MARMASM",
  3604. configName);
  3605. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3606. "ASM_MARMASM", configName);
  3607. marmasmOptions.Parse(flags);
  3608. // Get includes for this target
  3609. marmasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MARMASM"));
  3610. this->MarmasmOptions[configName] = std::move(pOptions);
  3611. return true;
  3612. }
  3613. void cmVisualStudio10TargetGenerator::WriteMarmasmOptions(
  3614. Elem& e1, std::string const& configName)
  3615. {
  3616. if (!this->MSTools || !this->GlobalGenerator->IsMarmasmEnabled()) {
  3617. return;
  3618. }
  3619. Elem e2(e1, "MARMASM");
  3620. // Preprocessor definitions and includes are shared with clOptions.
  3621. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3622. clOptions.OutputPreprocessorDefinitions("ASM_MARMASM");
  3623. OptionsHelper marmasmOptions(*(this->MarmasmOptions[configName]), e2);
  3624. marmasmOptions.OutputAdditionalIncludeDirectories("ASM_MARMASM");
  3625. marmasmOptions.PrependInheritedString("AdditionalOptions");
  3626. marmasmOptions.OutputFlagMap();
  3627. }
  3628. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions()
  3629. {
  3630. if (!this->GlobalGenerator->IsMasmEnabled()) {
  3631. return true;
  3632. }
  3633. return std::all_of(
  3634. this->Configurations.begin(), this->Configurations.end(),
  3635. [this](std::string const& c) { return this->ComputeMasmOptions(c); });
  3636. }
  3637. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions(
  3638. std::string const& configName)
  3639. {
  3640. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3641. auto pOptions = cm::make_unique<Options>(
  3642. this->LocalGenerator, Options::MasmCompiler, gg->GetMasmFlagTable());
  3643. Options& masmOptions = *pOptions;
  3644. // MSBuild enables debug information by default.
  3645. // Disable it explicitly unless a flag parsed below re-enables it.
  3646. masmOptions.AddFlag("GenerateDebugInformation", "false");
  3647. std::string flags;
  3648. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3649. cmBuildStep::Compile, "ASM_MASM",
  3650. configName);
  3651. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3652. "ASM_MASM", configName);
  3653. masmOptions.Parse(flags);
  3654. // Get includes for this target
  3655. masmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MASM"));
  3656. this->MasmOptions[configName] = std::move(pOptions);
  3657. return true;
  3658. }
  3659. void cmVisualStudio10TargetGenerator::WriteMasmOptions(
  3660. Elem& e1, std::string const& configName)
  3661. {
  3662. if (!this->MSTools || !this->GlobalGenerator->IsMasmEnabled()) {
  3663. return;
  3664. }
  3665. Elem e2(e1, "MASM");
  3666. // Preprocessor definitions and includes are shared with clOptions.
  3667. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3668. clOptions.OutputPreprocessorDefinitions("ASM_MASM");
  3669. OptionsHelper masmOptions(*(this->MasmOptions[configName]), e2);
  3670. masmOptions.OutputAdditionalIncludeDirectories("ASM_MASM");
  3671. masmOptions.PrependInheritedString("AdditionalOptions");
  3672. masmOptions.OutputFlagMap();
  3673. }
  3674. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions()
  3675. {
  3676. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3677. return true;
  3678. }
  3679. return std::all_of(
  3680. this->Configurations.begin(), this->Configurations.end(),
  3681. [this](std::string const& c) { return this->ComputeNasmOptions(c); });
  3682. }
  3683. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions(
  3684. std::string const& configName)
  3685. {
  3686. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3687. auto pOptions = cm::make_unique<Options>(
  3688. this->LocalGenerator, Options::NasmCompiler, gg->GetNasmFlagTable());
  3689. Options& nasmOptions = *pOptions;
  3690. std::string flags;
  3691. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3692. cmBuildStep::Compile, "ASM_NASM",
  3693. configName);
  3694. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3695. "ASM_NASM", configName);
  3696. flags += " -f";
  3697. flags += this->Makefile->GetSafeDefinition("CMAKE_ASM_NASM_OBJECT_FORMAT");
  3698. nasmOptions.Parse(flags);
  3699. // Get includes for this target
  3700. nasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_NASM"));
  3701. this->NasmOptions[configName] = std::move(pOptions);
  3702. return true;
  3703. }
  3704. void cmVisualStudio10TargetGenerator::WriteNasmOptions(
  3705. Elem& e1, std::string const& configName)
  3706. {
  3707. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3708. return;
  3709. }
  3710. Elem e2(e1, "NASM");
  3711. OptionsHelper nasmOptions(*(this->NasmOptions[configName]), e2);
  3712. nasmOptions.OutputAdditionalIncludeDirectories("ASM_NASM");
  3713. nasmOptions.OutputFlagMap();
  3714. nasmOptions.PrependInheritedString("AdditionalOptions");
  3715. nasmOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3716. // Preprocessor definitions and includes are shared with clOptions.
  3717. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3718. clOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3719. }
  3720. void cmVisualStudio10TargetGenerator::WriteLibOptions(
  3721. Elem& e1, std::string const& config)
  3722. {
  3723. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3724. this->GeneratorTarget->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  3725. return;
  3726. }
  3727. const std::string& linkLanguage =
  3728. this->GeneratorTarget->GetLinkClosure(config)->LinkerLanguage;
  3729. std::string libflags;
  3730. this->LocalGenerator->GetStaticLibraryFlags(libflags, config, linkLanguage,
  3731. this->GeneratorTarget);
  3732. if (!libflags.empty()) {
  3733. Elem e2(e1, "Lib");
  3734. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3735. cmVS10GeneratorOptions libOptions(this->LocalGenerator,
  3736. cmVisualStudioGeneratorOptions::Linker,
  3737. gg->GetLibFlagTable(), this);
  3738. libOptions.Parse(libflags);
  3739. OptionsHelper oh(libOptions, e2);
  3740. oh.PrependInheritedString("AdditionalOptions");
  3741. oh.OutputFlagMap();
  3742. }
  3743. // We cannot generate metadata for static libraries. WindowsPhone
  3744. // and WindowsStore tools look at GenerateWindowsMetadata in the
  3745. // Link tool options even for static libraries.
  3746. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3747. this->GlobalGenerator->TargetsWindowsStore()) {
  3748. Elem e2(e1, "Link");
  3749. e2.Element("GenerateWindowsMetadata", "false");
  3750. }
  3751. }
  3752. void cmVisualStudio10TargetGenerator::WriteManifestOptions(
  3753. Elem& e1, std::string const& config)
  3754. {
  3755. if (this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE &&
  3756. this->GeneratorTarget->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3757. this->GeneratorTarget->GetType() != cmStateEnums::MODULE_LIBRARY) {
  3758. return;
  3759. }
  3760. std::vector<cmSourceFile const*> manifest_srcs;
  3761. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  3762. cmValue dpiAware = this->GeneratorTarget->GetProperty("VS_DPI_AWARE");
  3763. if (!manifest_srcs.empty() || dpiAware) {
  3764. Elem e2(e1, "Manifest");
  3765. if (!manifest_srcs.empty()) {
  3766. std::ostringstream oss;
  3767. for (cmSourceFile const* mi : manifest_srcs) {
  3768. std::string m = this->ConvertPath(mi->GetFullPath(), false);
  3769. ConvertToWindowsSlash(m);
  3770. oss << m << ";";
  3771. }
  3772. e2.Element("AdditionalManifestFiles", oss.str());
  3773. }
  3774. if (dpiAware) {
  3775. if (*dpiAware == "PerMonitor"_s) {
  3776. e2.Element("EnableDpiAwareness", "PerMonitorHighDPIAware");
  3777. } else if (cmIsOn(*dpiAware)) {
  3778. e2.Element("EnableDpiAwareness", "true");
  3779. } else if (cmIsOff(*dpiAware)) {
  3780. e2.Element("EnableDpiAwareness", "false");
  3781. } else {
  3782. cmSystemTools::Error(
  3783. cmStrCat("Bad parameter for VS_DPI_AWARE: ", *dpiAware));
  3784. }
  3785. }
  3786. }
  3787. }
  3788. void cmVisualStudio10TargetGenerator::WriteAntBuildOptions(
  3789. Elem& e1, std::string const& configName)
  3790. {
  3791. // Look through the sources for AndroidManifest.xml and use
  3792. // its location as the root source directory.
  3793. std::string rootDir = this->LocalGenerator->GetCurrentSourceDirectory();
  3794. {
  3795. for (cmGeneratorTarget::AllConfigSource const& source :
  3796. this->GeneratorTarget->GetAllConfigSources()) {
  3797. if (source.Kind == cmGeneratorTarget::SourceKindExtra &&
  3798. "androidmanifest.xml" ==
  3799. cmSystemTools::LowerCase(source.Source->GetLocation().GetName())) {
  3800. rootDir = source.Source->GetLocation().GetDirectory();
  3801. break;
  3802. }
  3803. }
  3804. }
  3805. // Tell MSBuild to launch Ant.
  3806. Elem e2(e1, "AntBuild");
  3807. {
  3808. std::string antBuildPath = rootDir;
  3809. ConvertToWindowsSlash(antBuildPath);
  3810. e2.Element("AntBuildPath", antBuildPath);
  3811. }
  3812. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_SKIP_ANT_STEP")) {
  3813. e2.Element("SkipAntStep", "true");
  3814. }
  3815. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_PROGUARD")) {
  3816. e2.Element("EnableProGuard", "true");
  3817. }
  3818. if (cmValue proGuardConfigLocation =
  3819. this->GeneratorTarget->GetProperty("ANDROID_PROGUARD_CONFIG_PATH")) {
  3820. e2.Element("ProGuardConfigLocation", *proGuardConfigLocation);
  3821. }
  3822. if (cmValue securePropertiesLocation =
  3823. this->GeneratorTarget->GetProperty("ANDROID_SECURE_PROPS_PATH")) {
  3824. e2.Element("SecurePropertiesLocation", *securePropertiesLocation);
  3825. }
  3826. if (cmValue nativeLibDirectoriesExpression =
  3827. this->GeneratorTarget->GetProperty("ANDROID_NATIVE_LIB_DIRECTORIES")) {
  3828. std::string nativeLibDirs = cmGeneratorExpression::Evaluate(
  3829. *nativeLibDirectoriesExpression, this->LocalGenerator, configName);
  3830. e2.Element("NativeLibDirectories", nativeLibDirs);
  3831. }
  3832. if (cmValue nativeLibDependenciesExpression =
  3833. this->GeneratorTarget->GetProperty(
  3834. "ANDROID_NATIVE_LIB_DEPENDENCIES")) {
  3835. std::string nativeLibDeps = cmGeneratorExpression::Evaluate(
  3836. *nativeLibDependenciesExpression, this->LocalGenerator, configName);
  3837. e2.Element("NativeLibDependencies", nativeLibDeps);
  3838. }
  3839. if (cmValue javaSourceDir =
  3840. this->GeneratorTarget->GetProperty("ANDROID_JAVA_SOURCE_DIR")) {
  3841. e2.Element("JavaSourceDir", *javaSourceDir);
  3842. }
  3843. if (cmValue jarDirectoriesExpression =
  3844. this->GeneratorTarget->GetProperty("ANDROID_JAR_DIRECTORIES")) {
  3845. std::string jarDirectories = cmGeneratorExpression::Evaluate(
  3846. *jarDirectoriesExpression, this->LocalGenerator, configName);
  3847. e2.Element("JarDirectories", jarDirectories);
  3848. }
  3849. if (cmValue jarDeps =
  3850. this->GeneratorTarget->GetProperty("ANDROID_JAR_DEPENDENCIES")) {
  3851. e2.Element("JarDependencies", *jarDeps);
  3852. }
  3853. if (cmValue assetsDirectories =
  3854. this->GeneratorTarget->GetProperty("ANDROID_ASSETS_DIRECTORIES")) {
  3855. e2.Element("AssetsDirectories", *assetsDirectories);
  3856. }
  3857. {
  3858. std::string manifest_xml = cmStrCat(rootDir, "/AndroidManifest.xml");
  3859. ConvertToWindowsSlash(manifest_xml);
  3860. e2.Element("AndroidManifestLocation", manifest_xml);
  3861. }
  3862. if (cmValue antAdditionalOptions =
  3863. this->GeneratorTarget->GetProperty("ANDROID_ANT_ADDITIONAL_OPTIONS")) {
  3864. e2.Element("AdditionalOptions",
  3865. cmStrCat(*antAdditionalOptions, " %(AdditionalOptions)"));
  3866. }
  3867. }
  3868. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  3869. {
  3870. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  3871. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3872. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3873. for (std::string const& c : this->Configurations) {
  3874. if (!this->ComputeLinkOptions(c)) {
  3875. return false;
  3876. }
  3877. }
  3878. }
  3879. return true;
  3880. }
  3881. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
  3882. std::string const& config)
  3883. {
  3884. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3885. auto pOptions = cm::make_unique<Options>(
  3886. this->LocalGenerator, Options::Linker, gg->GetLinkFlagTable(), this);
  3887. Options& linkOptions = *pOptions;
  3888. cmGeneratorTarget::LinkClosure const* linkClosure =
  3889. this->GeneratorTarget->GetLinkClosure(config);
  3890. const std::string& linkLanguage = linkClosure->LinkerLanguage;
  3891. if (linkLanguage.empty()) {
  3892. cmSystemTools::Error(cmStrCat(
  3893. "CMake can not determine linker language for target: ", this->Name));
  3894. return false;
  3895. }
  3896. std::string CONFIG = cmSystemTools::UpperCase(config);
  3897. const char* linkType = "SHARED";
  3898. if (this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3899. linkType = "MODULE";
  3900. }
  3901. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3902. linkType = "EXE";
  3903. }
  3904. std::string flags;
  3905. std::string linkFlagVarBase = cmStrCat("CMAKE_", linkType, "_LINKER_FLAGS");
  3906. flags += ' ';
  3907. flags += this->Makefile->GetRequiredDefinition(linkFlagVarBase);
  3908. std::string linkFlagVar = cmStrCat(linkFlagVarBase, '_', CONFIG);
  3909. flags += ' ';
  3910. flags += this->Makefile->GetRequiredDefinition(linkFlagVar);
  3911. cmValue targetLinkFlags = this->GeneratorTarget->GetProperty("LINK_FLAGS");
  3912. if (targetLinkFlags) {
  3913. flags += ' ';
  3914. flags += *targetLinkFlags;
  3915. }
  3916. std::string flagsProp = cmStrCat("LINK_FLAGS_", CONFIG);
  3917. if (cmValue flagsConfig = this->GeneratorTarget->GetProperty(flagsProp)) {
  3918. flags += ' ';
  3919. flags += *flagsConfig;
  3920. }
  3921. std::vector<std::string> opts;
  3922. this->GeneratorTarget->GetLinkOptions(opts, config, linkLanguage);
  3923. // LINK_OPTIONS are escaped.
  3924. this->LocalGenerator->AppendCompileOptions(flags, opts);
  3925. cmComputeLinkInformation* pcli =
  3926. this->GeneratorTarget->GetLinkInformation(config);
  3927. if (!pcli) {
  3928. cmSystemTools::Error(
  3929. cmStrCat("CMake can not compute cmComputeLinkInformation for target: ",
  3930. this->Name));
  3931. return false;
  3932. }
  3933. cmComputeLinkInformation& cli = *pcli;
  3934. std::vector<std::string> libVec;
  3935. std::vector<std::string> vsTargetVec;
  3936. this->AddLibraries(cli, libVec, vsTargetVec, config);
  3937. std::string standardLibsVar =
  3938. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  3939. std::string const& libs = this->Makefile->GetSafeDefinition(standardLibsVar);
  3940. cmSystemTools::ParseWindowsCommandLine(libs.c_str(), libVec);
  3941. linkOptions.AddFlag("AdditionalDependencies", libVec);
  3942. // Populate TargetsFileAndConfigsVec
  3943. for (std::string const& ti : vsTargetVec) {
  3944. this->AddTargetsFileAndConfigPair(ti, config);
  3945. }
  3946. std::vector<std::string> const& ldirs = cli.GetDirectories();
  3947. std::vector<std::string> linkDirs;
  3948. for (std::string const& d : ldirs) {
  3949. // first just full path
  3950. linkDirs.push_back(d);
  3951. // next path with configuration type Debug, Release, etc
  3952. linkDirs.emplace_back(cmStrCat(d, "/$(Configuration)"));
  3953. }
  3954. linkDirs.push_back("%(AdditionalLibraryDirectories)");
  3955. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs);
  3956. cmGeneratorTarget::Names targetNames;
  3957. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3958. targetNames = this->GeneratorTarget->GetExecutableNames(config);
  3959. } else {
  3960. targetNames = this->GeneratorTarget->GetLibraryNames(config);
  3961. }
  3962. if (this->MSTools) {
  3963. if (this->GeneratorTarget->IsWin32Executable(config)) {
  3964. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3965. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3966. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3967. if (this->ClOptions[config]->UsingUnicode()) {
  3968. linkOptions.AddFlag("EntryPointSymbol", "wWinMainCRTStartup");
  3969. } else {
  3970. linkOptions.AddFlag("EntryPointSymbol", "WinMainCRTStartup");
  3971. }
  3972. }
  3973. } else {
  3974. linkOptions.AddFlag("SubSystem", "Windows");
  3975. }
  3976. } else {
  3977. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3978. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3979. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3980. if (this->ClOptions[config]->UsingUnicode()) {
  3981. linkOptions.AddFlag("EntryPointSymbol", "mainWCRTStartup");
  3982. } else {
  3983. linkOptions.AddFlag("EntryPointSymbol", "mainACRTStartup");
  3984. }
  3985. }
  3986. } else {
  3987. linkOptions.AddFlag("SubSystem", "Console");
  3988. };
  3989. }
  3990. if (cmValue stackVal = this->Makefile->GetDefinition(
  3991. cmStrCat("CMAKE_", linkLanguage, "_STACK_SIZE"))) {
  3992. linkOptions.AddFlag("StackReserveSize", *stackVal);
  3993. }
  3994. linkOptions.AddFlag("GenerateDebugInformation", "false");
  3995. std::string pdb = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config),
  3996. '/', targetNames.PDB);
  3997. if (!targetNames.ImportLibrary.empty()) {
  3998. std::string imLib =
  3999. cmStrCat(this->GeneratorTarget->GetDirectory(
  4000. config, cmStateEnums::ImportLibraryArtifact),
  4001. '/', targetNames.ImportLibrary);
  4002. linkOptions.AddFlag("ImportLibrary", imLib);
  4003. }
  4004. linkOptions.AddFlag("ProgramDataBaseFile", pdb);
  4005. // A Windows Runtime component uses internal .NET metadata,
  4006. // so does not have an import library.
  4007. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") &&
  4008. this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE) {
  4009. linkOptions.AddFlag("GenerateWindowsMetadata", "true");
  4010. } else if (this->GlobalGenerator->TargetsWindowsPhone() ||
  4011. this->GlobalGenerator->TargetsWindowsStore()) {
  4012. // WindowsPhone and WindowsStore components are in an app container
  4013. // and produce WindowsMetadata. If we are not producing a WINRT
  4014. // component, then do not generate the metadata here.
  4015. linkOptions.AddFlag("GenerateWindowsMetadata", "false");
  4016. }
  4017. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  4018. this->GlobalGenerator->GetSystemVersion() == "8.0"_s) {
  4019. // WindowsPhone 8.0 does not have ole32.
  4020. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries", "ole32.lib");
  4021. }
  4022. } else if (this->NsightTegra) {
  4023. linkOptions.AddFlag("SoName", targetNames.SharedObject);
  4024. }
  4025. linkOptions.Parse(flags);
  4026. linkOptions.FixManifestUACFlags();
  4027. if (this->MSTools) {
  4028. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  4029. this->GeneratorTarget->GetModuleDefinitionInfo(config);
  4030. if (mdi && !mdi->DefFile.empty()) {
  4031. linkOptions.AddFlag("ModuleDefinitionFile", mdi->DefFile);
  4032. }
  4033. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries",
  4034. "%(IgnoreSpecificDefaultLibraries)");
  4035. }
  4036. // VS 2015 without all updates has a v140 toolset whose
  4037. // GenerateDebugInformation expects No/Debug instead of false/true.
  4038. if (gg->GetPlatformToolsetNeedsDebugEnum()) {
  4039. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  4040. if (strcmp(debug, "false") == 0) {
  4041. linkOptions.AddFlag("GenerateDebugInformation", "No");
  4042. } else if (strcmp(debug, "true") == 0) {
  4043. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  4044. }
  4045. }
  4046. }
  4047. // Managed code cannot be linked with /DEBUG:FASTLINK
  4048. if (this->Managed) {
  4049. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  4050. if (strcmp(debug, "DebugFastLink") == 0) {
  4051. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  4052. }
  4053. }
  4054. }
  4055. this->LinkOptions[config] = std::move(pOptions);
  4056. return true;
  4057. }
  4058. bool cmVisualStudio10TargetGenerator::ComputeLibOptions()
  4059. {
  4060. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4061. for (std::string const& c : this->Configurations) {
  4062. if (!this->ComputeLibOptions(c)) {
  4063. return false;
  4064. }
  4065. }
  4066. }
  4067. return true;
  4068. }
  4069. bool cmVisualStudio10TargetGenerator::ComputeLibOptions(
  4070. std::string const& config)
  4071. {
  4072. cmComputeLinkInformation* pcli =
  4073. this->GeneratorTarget->GetLinkInformation(config);
  4074. if (!pcli) {
  4075. cmSystemTools::Error(
  4076. cmStrCat("CMake can not compute cmComputeLinkInformation for target: ",
  4077. this->Name));
  4078. return false;
  4079. }
  4080. cmComputeLinkInformation& cli = *pcli;
  4081. using ItemVector = cmComputeLinkInformation::ItemVector;
  4082. const ItemVector& libs = cli.GetItems();
  4083. for (cmComputeLinkInformation::Item const& l : libs) {
  4084. if (l.IsPath == cmComputeLinkInformation::ItemIsPath::Yes &&
  4085. cmVS10IsTargetsFile(l.Value.Value)) {
  4086. std::string path =
  4087. this->LocalGenerator->MaybeRelativeToCurBinDir(l.Value.Value);
  4088. ConvertToWindowsSlash(path);
  4089. this->AddTargetsFileAndConfigPair(path, config);
  4090. }
  4091. }
  4092. return true;
  4093. }
  4094. void cmVisualStudio10TargetGenerator::WriteLinkOptions(
  4095. Elem& e1, std::string const& config)
  4096. {
  4097. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4098. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  4099. return;
  4100. }
  4101. if (this->ProjectType == VsProjectType::csproj) {
  4102. return;
  4103. }
  4104. {
  4105. Elem e2(e1, "Link");
  4106. OptionsHelper linkOptions(*(this->LinkOptions[config]), e2);
  4107. linkOptions.PrependInheritedString("AdditionalOptions");
  4108. linkOptions.OutputFlagMap();
  4109. }
  4110. if (!this->GlobalGenerator->NeedLinkLibraryDependencies(
  4111. this->GeneratorTarget)) {
  4112. Elem e2(e1, "ProjectReference");
  4113. e2.Element("LinkLibraryDependencies", "false");
  4114. }
  4115. }
  4116. void cmVisualStudio10TargetGenerator::AddLibraries(
  4117. const cmComputeLinkInformation& cli, std::vector<std::string>& libVec,
  4118. std::vector<std::string>& vsTargetVec, const std::string& config)
  4119. {
  4120. using ItemVector = cmComputeLinkInformation::ItemVector;
  4121. ItemVector const& libs = cli.GetItems();
  4122. for (cmComputeLinkInformation::Item const& l : libs) {
  4123. if (l.Target) {
  4124. auto managedType = l.Target->GetManagedType(config);
  4125. if (managedType != cmGeneratorTarget::ManagedType::Native &&
  4126. this->GeneratorTarget->GetManagedType(config) !=
  4127. cmGeneratorTarget::ManagedType::Native &&
  4128. l.Target->IsImported() &&
  4129. l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4130. auto location = l.Target->GetFullPath(config);
  4131. if (!location.empty()) {
  4132. ConvertToWindowsSlash(location);
  4133. switch (this->ProjectType) {
  4134. case VsProjectType::csproj:
  4135. // If the target we want to "link" to is an imported managed
  4136. // target and this is a C# project, we add a hint reference. This
  4137. // reference is written to project file in
  4138. // WriteDotNetReferences().
  4139. this->DotNetHintReferences[config].push_back(
  4140. DotNetHintReference(l.Target->GetName(), location));
  4141. break;
  4142. case VsProjectType::vcxproj:
  4143. // Add path of assembly to list of using-directories, so the
  4144. // managed assembly can be used by '#using <assembly.dll>' in
  4145. // code.
  4146. this->AdditionalUsingDirectories[config].insert(
  4147. cmSystemTools::GetFilenamePath(location));
  4148. break;
  4149. default:
  4150. // In .proj files, we wouldn't be referencing libraries.
  4151. break;
  4152. }
  4153. }
  4154. }
  4155. // Do not allow C# targets to be added to the LIB listing. LIB files are
  4156. // used for linking C++ dependencies. C# libraries do not have lib files.
  4157. // Instead, they compile down to C# reference libraries (DLL files). The
  4158. // `<ProjectReference>` elements added to the vcxproj are enough for the
  4159. // IDE to deduce the DLL file required by other C# projects that need its
  4160. // reference library.
  4161. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  4162. continue;
  4163. }
  4164. }
  4165. if (l.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  4166. std::string path =
  4167. this->LocalGenerator->MaybeRelativeToCurBinDir(l.Value.Value);
  4168. ConvertToWindowsSlash(path);
  4169. if (cmVS10IsTargetsFile(l.Value.Value)) {
  4170. vsTargetVec.push_back(path);
  4171. } else {
  4172. libVec.push_back(l.HasFeature() ? l.GetFormattedItem(path).Value
  4173. : path);
  4174. }
  4175. } else if (!l.Target ||
  4176. (l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  4177. l.Target->GetType() != cmStateEnums::OBJECT_LIBRARY)) {
  4178. libVec.push_back(l.Value.Value);
  4179. }
  4180. }
  4181. }
  4182. void cmVisualStudio10TargetGenerator::AddTargetsFileAndConfigPair(
  4183. std::string const& targetsFile, std::string const& config)
  4184. {
  4185. for (TargetsFileAndConfigs& i : this->TargetsFileAndConfigsVec) {
  4186. if (cmSystemTools::ComparePath(targetsFile, i.File)) {
  4187. if (!cm::contains(i.Configs, config)) {
  4188. i.Configs.push_back(config);
  4189. }
  4190. return;
  4191. }
  4192. }
  4193. TargetsFileAndConfigs entry;
  4194. entry.File = targetsFile;
  4195. entry.Configs.push_back(config);
  4196. this->TargetsFileAndConfigsVec.push_back(entry);
  4197. }
  4198. void cmVisualStudio10TargetGenerator::WriteMidlOptions(
  4199. Elem& e1, std::string const& configName)
  4200. {
  4201. if (!this->MSTools) {
  4202. return;
  4203. }
  4204. if (this->ProjectType == VsProjectType::csproj) {
  4205. return;
  4206. }
  4207. if (this->GeneratorTarget->GetType() > cmStateEnums::UTILITY) {
  4208. return;
  4209. }
  4210. // This processes *any* of the .idl files specified in the project's file
  4211. // list (and passed as the item metadata %(Filename) expressing the rule
  4212. // input filename) into output files at the per-config *build* dir
  4213. // ($(IntDir)) each.
  4214. //
  4215. // IOW, this MIDL section is intended to provide a fully generic syntax
  4216. // content suitable for most cases (read: if you get errors, then it's quite
  4217. // probable that the error is on your side of the .idl setup).
  4218. //
  4219. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  4220. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  4221. // project's file list, otherwise the compiler-side processing won't pick it
  4222. // up (for non-directory form, it ends up looking in project binary dir
  4223. // only). Perhaps there's something to be done to make this more automatic
  4224. // on the CMake side?
  4225. std::vector<std::string> const includes =
  4226. this->GetIncludes(configName, "MIDL");
  4227. std::ostringstream oss;
  4228. for (std::string const& i : includes) {
  4229. oss << i << ";";
  4230. }
  4231. oss << "%(AdditionalIncludeDirectories)";
  4232. Elem e2(e1, "Midl");
  4233. e2.Element("AdditionalIncludeDirectories", oss.str());
  4234. e2.Element("OutputDirectory", "$(ProjectDir)/$(IntDir)");
  4235. e2.Element("HeaderFileName", "%(Filename).h");
  4236. e2.Element("TypeLibraryName", "%(Filename).tlb");
  4237. e2.Element("InterfaceIdentifierFileName", "%(Filename)_i.c");
  4238. e2.Element("ProxyFileName", "%(Filename)_p.c");
  4239. }
  4240. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups(Elem& e0)
  4241. {
  4242. if (this->ProjectType == VsProjectType::csproj) {
  4243. return;
  4244. }
  4245. for (const std::string& c : this->Configurations) {
  4246. Elem e1(e0, "ItemDefinitionGroup");
  4247. e1.Attribute("Condition", this->CalcCondition(c));
  4248. // output cl compile flags <ClCompile></ClCompile>
  4249. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  4250. this->WriteClOptions(e1, c);
  4251. // output rc compile flags <ResourceCompile></ResourceCompile>
  4252. this->WriteRCOptions(e1, c);
  4253. this->WriteCudaOptions(e1, c);
  4254. this->WriteMarmasmOptions(e1, c);
  4255. this->WriteMasmOptions(e1, c);
  4256. this->WriteNasmOptions(e1, c);
  4257. }
  4258. // output midl flags <Midl></Midl>
  4259. this->WriteMidlOptions(e1, c);
  4260. // write events
  4261. if (this->ProjectType != VsProjectType::csproj) {
  4262. this->WriteEvents(e1, c);
  4263. }
  4264. // output link flags <Link></Link>
  4265. this->WriteLinkOptions(e1, c);
  4266. this->WriteCudaLinkOptions(e1, c);
  4267. // output lib flags <Lib></Lib>
  4268. this->WriteLibOptions(e1, c);
  4269. // output manifest flags <Manifest></Manifest>
  4270. this->WriteManifestOptions(e1, c);
  4271. if (this->NsightTegra &&
  4272. this->GeneratorTarget->Target->IsAndroidGuiExecutable()) {
  4273. this->WriteAntBuildOptions(e1, c);
  4274. }
  4275. }
  4276. }
  4277. void cmVisualStudio10TargetGenerator::WriteEvents(
  4278. Elem& e1, std::string const& configName)
  4279. {
  4280. bool addedPrelink = false;
  4281. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  4282. this->GeneratorTarget->GetModuleDefinitionInfo(configName);
  4283. if (mdi && mdi->DefFileGenerated) {
  4284. addedPrelink = true;
  4285. std::vector<cmCustomCommand> commands =
  4286. this->GeneratorTarget->GetPreLinkCommands();
  4287. this->GlobalGenerator->AddSymbolExportCommand(this->GeneratorTarget,
  4288. commands, configName);
  4289. this->WriteEvent(e1, "PreLinkEvent", commands, configName);
  4290. }
  4291. if (!addedPrelink) {
  4292. this->WriteEvent(e1, "PreLinkEvent",
  4293. this->GeneratorTarget->GetPreLinkCommands(), configName);
  4294. }
  4295. this->WriteEvent(e1, "PreBuildEvent",
  4296. this->GeneratorTarget->GetPreBuildCommands(), configName);
  4297. this->WriteEvent(e1, "PostBuildEvent",
  4298. this->GeneratorTarget->GetPostBuildCommands(), configName);
  4299. }
  4300. void cmVisualStudio10TargetGenerator::WriteEvent(
  4301. Elem& e1, const std::string& name,
  4302. std::vector<cmCustomCommand> const& commands, std::string const& configName)
  4303. {
  4304. if (commands.empty()) {
  4305. return;
  4306. }
  4307. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  4308. std::string script;
  4309. const char* pre = "";
  4310. std::string comment;
  4311. bool stdPipesUTF8 = false;
  4312. for (cmCustomCommand const& cc : commands) {
  4313. cmCustomCommandGenerator ccg(cc, configName, lg);
  4314. if (!ccg.HasOnlyEmptyCommandLines()) {
  4315. comment += pre;
  4316. comment += lg->ConstructComment(ccg);
  4317. script += pre;
  4318. pre = "\n";
  4319. script += lg->ConstructScript(ccg);
  4320. stdPipesUTF8 = stdPipesUTF8 || cc.GetStdPipesUTF8();
  4321. }
  4322. }
  4323. if (!script.empty()) {
  4324. script += lg->FinishConstructScript(this->ProjectType);
  4325. }
  4326. comment = cmVS10EscapeComment(comment);
  4327. if (this->ProjectType != VsProjectType::csproj) {
  4328. Elem e2(e1, name);
  4329. if (stdPipesUTF8) {
  4330. this->WriteStdOutEncodingUtf8(e2);
  4331. }
  4332. e2.Element("Message", comment);
  4333. e2.Element("Command", script);
  4334. } else {
  4335. std::string strippedComment = comment;
  4336. strippedComment.erase(
  4337. std::remove(strippedComment.begin(), strippedComment.end(), '\t'),
  4338. strippedComment.end());
  4339. std::ostringstream oss;
  4340. if (!comment.empty() && !strippedComment.empty()) {
  4341. oss << "echo " << comment << "\n";
  4342. }
  4343. oss << script << "\n";
  4344. e1.Element(name, oss.str());
  4345. }
  4346. }
  4347. void cmVisualStudio10TargetGenerator::WriteProjectReferences(Elem& e0)
  4348. {
  4349. cmGlobalGenerator::TargetDependSet const& unordered =
  4350. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  4351. using OrderedTargetDependSet =
  4352. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  4353. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  4354. Elem e1(e0, "ItemGroup");
  4355. e1.SetHasElements();
  4356. for (cmGeneratorTarget const* dt : depends) {
  4357. if (!dt->IsInBuildSystem()) {
  4358. continue;
  4359. }
  4360. // skip fortran targets as they can not be processed by MSBuild
  4361. // the only reference will be in the .sln file
  4362. if (this->GlobalGenerator->TargetIsFortranOnly(dt)) {
  4363. continue;
  4364. }
  4365. cmLocalGenerator* lg = dt->GetLocalGenerator();
  4366. std::string name = dt->GetName();
  4367. std::string path;
  4368. if (cmValue p = dt->GetProperty("EXTERNAL_MSPROJECT")) {
  4369. path = *p;
  4370. } else {
  4371. path = cmStrCat(lg->GetCurrentBinaryDirectory(), '/', dt->GetName(),
  4372. computeProjectFileExtension(dt));
  4373. }
  4374. ConvertToWindowsSlash(path);
  4375. Elem e2(e1, "ProjectReference");
  4376. e2.Attribute("Include", path);
  4377. e2.Element("Project",
  4378. cmStrCat('{', this->GlobalGenerator->GetGUID(name), '}'));
  4379. e2.Element("Name", name);
  4380. this->WriteDotNetReferenceCustomTags(e2, name);
  4381. if (dt->IsCSharpOnly() || cmHasLiteralSuffix(path, "csproj")) {
  4382. e2.Element("SkipGetTargetFrameworkProperties", "true");
  4383. }
  4384. // Don't reference targets that don't produce any output.
  4385. else if (this->Configurations.empty() ||
  4386. dt->GetManagedType(this->Configurations[0]) ==
  4387. cmGeneratorTarget::ManagedType::Undefined) {
  4388. e2.Element("ReferenceOutputAssembly", "false");
  4389. e2.Element("CopyToOutputDirectory", "Never");
  4390. }
  4391. }
  4392. }
  4393. void cmVisualStudio10TargetGenerator::WritePlatformExtensions(Elem& e1)
  4394. {
  4395. // This only applies to Windows 10 apps
  4396. if (this->GlobalGenerator->TargetsWindowsStore() &&
  4397. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  4398. cmValue desktopExtensionsVersion =
  4399. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  4400. if (desktopExtensionsVersion) {
  4401. this->WriteSinglePlatformExtension(e1, "WindowsDesktop",
  4402. *desktopExtensionsVersion);
  4403. }
  4404. cmValue mobileExtensionsVersion =
  4405. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  4406. if (mobileExtensionsVersion) {
  4407. this->WriteSinglePlatformExtension(e1, "WindowsMobile",
  4408. *mobileExtensionsVersion);
  4409. }
  4410. }
  4411. }
  4412. void cmVisualStudio10TargetGenerator::WriteSinglePlatformExtension(
  4413. Elem& e1, std::string const& extension, std::string const& version)
  4414. {
  4415. const std::string s =
  4416. cmStrCat("$([Microsoft.Build.Utilities.ToolLocationHelper]"
  4417. "::GetPlatformExtensionSDKLocation(`",
  4418. extension, ", Version=", version,
  4419. "`, $(TargetPlatformIdentifier), $(TargetPlatformVersion), null, "
  4420. "$(ExtensionSDKDirectoryRoot), null))"
  4421. "\\DesignTime\\CommonConfiguration\\Neutral\\",
  4422. extension, ".props");
  4423. Elem e2(e1, "Import");
  4424. e2.Attribute("Project", s);
  4425. e2.Attribute("Condition", cmStrCat("exists('", s, "')"));
  4426. }
  4427. void cmVisualStudio10TargetGenerator::WriteSDKReferences(Elem& e0)
  4428. {
  4429. cmList sdkReferences;
  4430. std::unique_ptr<Elem> spe1;
  4431. if (cmValue vsSDKReferences =
  4432. this->GeneratorTarget->GetProperty("VS_SDK_REFERENCES")) {
  4433. sdkReferences.assign(*vsSDKReferences);
  4434. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  4435. for (auto const& ri : sdkReferences) {
  4436. Elem(*spe1, "SDKReference").Attribute("Include", ri);
  4437. }
  4438. }
  4439. // This only applies to Windows 10 apps
  4440. if (this->GlobalGenerator->TargetsWindowsStore() &&
  4441. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  4442. cmValue desktopExtensionsVersion =
  4443. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  4444. cmValue mobileExtensionsVersion =
  4445. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  4446. cmValue iotExtensionsVersion =
  4447. this->GeneratorTarget->GetProperty("VS_IOT_EXTENSIONS_VERSION");
  4448. if (desktopExtensionsVersion || mobileExtensionsVersion ||
  4449. iotExtensionsVersion) {
  4450. if (!spe1) {
  4451. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  4452. }
  4453. if (desktopExtensionsVersion) {
  4454. this->WriteSingleSDKReference(*spe1, "WindowsDesktop",
  4455. *desktopExtensionsVersion);
  4456. }
  4457. if (mobileExtensionsVersion) {
  4458. this->WriteSingleSDKReference(*spe1, "WindowsMobile",
  4459. *mobileExtensionsVersion);
  4460. }
  4461. if (iotExtensionsVersion) {
  4462. this->WriteSingleSDKReference(*spe1, "WindowsIoT",
  4463. *iotExtensionsVersion);
  4464. }
  4465. }
  4466. }
  4467. }
  4468. void cmVisualStudio10TargetGenerator::WriteSingleSDKReference(
  4469. Elem& e1, std::string const& extension, std::string const& version)
  4470. {
  4471. Elem(e1, "SDKReference")
  4472. .Attribute("Include", cmStrCat(extension, ", Version=", version));
  4473. }
  4474. namespace {
  4475. std::string ComputeCertificateThumbprint(const std::string& source)
  4476. {
  4477. std::string thumbprint;
  4478. CRYPT_INTEGER_BLOB cryptBlob;
  4479. HCERTSTORE certStore = nullptr;
  4480. PCCERT_CONTEXT certContext = nullptr;
  4481. HANDLE certFile = CreateFileW(
  4482. cmsys::Encoding::ToWide(source.c_str()).c_str(), GENERIC_READ,
  4483. FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  4484. if (certFile != INVALID_HANDLE_VALUE && certFile != nullptr) {
  4485. DWORD fileSize = GetFileSize(certFile, nullptr);
  4486. if (fileSize != INVALID_FILE_SIZE) {
  4487. auto certData = cm::make_unique<BYTE[]>(fileSize);
  4488. if (certData != nullptr) {
  4489. DWORD dwRead = 0;
  4490. if (ReadFile(certFile, certData.get(), fileSize, &dwRead, nullptr)) {
  4491. cryptBlob.cbData = fileSize;
  4492. cryptBlob.pbData = certData.get();
  4493. // Verify that this is a valid cert
  4494. if (PFXIsPFXBlob(&cryptBlob)) {
  4495. // Open the certificate as a store
  4496. certStore =
  4497. PFXImportCertStore(&cryptBlob, nullptr, CRYPT_EXPORTABLE);
  4498. if (certStore != nullptr) {
  4499. // There should only be 1 cert.
  4500. certContext =
  4501. CertEnumCertificatesInStore(certStore, certContext);
  4502. if (certContext != nullptr) {
  4503. // The hash is 20 bytes
  4504. BYTE hashData[20];
  4505. DWORD hashLength = 20;
  4506. // Buffer to print the hash. Each byte takes 2 chars +
  4507. // terminating character
  4508. char hashPrint[41];
  4509. char* pHashPrint = hashPrint;
  4510. // Get the hash property from the certificate
  4511. if (CertGetCertificateContextProperty(
  4512. certContext, CERT_HASH_PROP_ID, hashData, &hashLength)) {
  4513. for (DWORD i = 0; i < hashLength; i++) {
  4514. // Convert each byte to hexadecimal
  4515. snprintf(pHashPrint, 3, "%02X", hashData[i]);
  4516. pHashPrint += 2;
  4517. }
  4518. *pHashPrint = '\0';
  4519. thumbprint = hashPrint;
  4520. }
  4521. CertFreeCertificateContext(certContext);
  4522. }
  4523. CertCloseStore(certStore, 0);
  4524. }
  4525. }
  4526. }
  4527. }
  4528. }
  4529. CloseHandle(certFile);
  4530. }
  4531. return thumbprint;
  4532. }
  4533. }
  4534. void cmVisualStudio10TargetGenerator::WriteWinRTPackageCertificateKeyFile(
  4535. Elem& e0)
  4536. {
  4537. if ((this->GlobalGenerator->TargetsWindowsStore() ||
  4538. this->GlobalGenerator->TargetsWindowsPhone()) &&
  4539. (cmStateEnums::EXECUTABLE == this->GeneratorTarget->GetType())) {
  4540. std::string pfxFile;
  4541. for (cmGeneratorTarget::AllConfigSource const& source :
  4542. this->GeneratorTarget->GetAllConfigSources()) {
  4543. if (source.Kind == cmGeneratorTarget::SourceKindCertificate) {
  4544. pfxFile = this->ConvertPath(source.Source->GetFullPath(), false);
  4545. ConvertToWindowsSlash(pfxFile);
  4546. break;
  4547. }
  4548. }
  4549. if (this->IsMissingFiles &&
  4550. !(this->GlobalGenerator->TargetsWindowsPhone() &&
  4551. this->GlobalGenerator->GetSystemVersion() == "8.0"_s)) {
  4552. // Move the manifest to a project directory to avoid clashes
  4553. std::string artifactDir =
  4554. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4555. ConvertToWindowsSlash(artifactDir);
  4556. Elem e1(e0, "PropertyGroup");
  4557. e1.Element("AppxPackageArtifactsDir", cmStrCat(artifactDir, '\\'));
  4558. std::string resourcePriFile =
  4559. cmStrCat(this->DefaultArtifactDir, "/resources.pri");
  4560. ConvertToWindowsSlash(resourcePriFile);
  4561. e1.Element("ProjectPriFullPath", resourcePriFile);
  4562. // If we are missing files and we don't have a certificate and
  4563. // aren't targeting WP8.0, add a default certificate
  4564. if (pfxFile.empty()) {
  4565. std::string templateFolder =
  4566. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/Windows");
  4567. pfxFile =
  4568. cmStrCat(this->DefaultArtifactDir, "/Windows_TemporaryKey.pfx");
  4569. cmSystemTools::CopyAFile(
  4570. cmStrCat(templateFolder, "/Windows_TemporaryKey.pfx"), pfxFile,
  4571. false);
  4572. ConvertToWindowsSlash(pfxFile);
  4573. this->AddedFiles.push_back(pfxFile);
  4574. this->AddedDefaultCertificate = true;
  4575. }
  4576. e1.Element("PackageCertificateKeyFile", pfxFile);
  4577. std::string thumb = ComputeCertificateThumbprint(pfxFile);
  4578. if (!thumb.empty()) {
  4579. e1.Element("PackageCertificateThumbprint", thumb);
  4580. }
  4581. } else if (!pfxFile.empty()) {
  4582. Elem e1(e0, "PropertyGroup");
  4583. e1.Element("PackageCertificateKeyFile", pfxFile);
  4584. std::string thumb = ComputeCertificateThumbprint(pfxFile);
  4585. if (!thumb.empty()) {
  4586. e1.Element("PackageCertificateThumbprint", thumb);
  4587. }
  4588. }
  4589. }
  4590. }
  4591. void cmVisualStudio10TargetGenerator::ClassifyAllConfigSources()
  4592. {
  4593. for (cmGeneratorTarget::AllConfigSource const& source :
  4594. this->GeneratorTarget->GetAllConfigSources()) {
  4595. this->ClassifyAllConfigSource(source);
  4596. }
  4597. }
  4598. void cmVisualStudio10TargetGenerator::ClassifyAllConfigSource(
  4599. cmGeneratorTarget::AllConfigSource const& acs)
  4600. {
  4601. switch (acs.Kind) {
  4602. case cmGeneratorTarget::SourceKindResx: {
  4603. // Build and save the name of the corresponding .h file
  4604. // This relationship will be used later when building the project files.
  4605. // Both names would have been auto generated from Visual Studio
  4606. // where the user supplied the file name and Visual Studio
  4607. // appended the suffix.
  4608. std::string resx = acs.Source->ResolveFullPath();
  4609. std::string hFileName =
  4610. cmStrCat(resx.substr(0, resx.find_last_of('.')), ".h");
  4611. this->ExpectedResxHeaders.insert(hFileName);
  4612. } break;
  4613. case cmGeneratorTarget::SourceKindXaml: {
  4614. // Build and save the name of the corresponding .h and .cpp file
  4615. // This relationship will be used later when building the project files.
  4616. // Both names would have been auto generated from Visual Studio
  4617. // where the user supplied the file name and Visual Studio
  4618. // appended the suffix.
  4619. std::string xaml = acs.Source->ResolveFullPath();
  4620. std::string hFileName = cmStrCat(xaml, ".h");
  4621. std::string cppFileName = cmStrCat(xaml, ".cpp");
  4622. this->ExpectedXamlHeaders.insert(hFileName);
  4623. this->ExpectedXamlSources.insert(cppFileName);
  4624. } break;
  4625. default:
  4626. break;
  4627. }
  4628. }
  4629. bool cmVisualStudio10TargetGenerator::IsResxHeader(
  4630. const std::string& headerFile)
  4631. {
  4632. return this->ExpectedResxHeaders.count(headerFile) > 0;
  4633. }
  4634. bool cmVisualStudio10TargetGenerator::IsXamlHeader(
  4635. const std::string& headerFile)
  4636. {
  4637. return this->ExpectedXamlHeaders.count(headerFile) > 0;
  4638. }
  4639. bool cmVisualStudio10TargetGenerator::IsXamlSource(
  4640. const std::string& sourceFile)
  4641. {
  4642. return this->ExpectedXamlSources.count(sourceFile) > 0;
  4643. }
  4644. void cmVisualStudio10TargetGenerator::WriteApplicationTypeSettings(Elem& e1)
  4645. {
  4646. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  4647. bool isAppContainer = false;
  4648. bool const isWindowsPhone = this->GlobalGenerator->TargetsWindowsPhone();
  4649. bool const isWindowsStore = this->GlobalGenerator->TargetsWindowsStore();
  4650. bool const isAndroid = this->GlobalGenerator->TargetsAndroid();
  4651. std::string const& rev = this->GlobalGenerator->GetApplicationTypeRevision();
  4652. if (isWindowsPhone || isWindowsStore) {
  4653. e1.Element("ApplicationType",
  4654. (isWindowsPhone ? "Windows Phone" : "Windows Store"));
  4655. e1.Element("DefaultLanguage", "en-US");
  4656. if (rev == "10.0"_s) {
  4657. e1.Element("ApplicationTypeRevision", rev);
  4658. // Visual Studio 14.0 is necessary for building 10.0 apps
  4659. e1.Element("MinimumVisualStudioVersion", "14.0");
  4660. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4661. isAppContainer = true;
  4662. }
  4663. } else if (rev == "8.1"_s) {
  4664. e1.Element("ApplicationTypeRevision", rev);
  4665. // Visual Studio 12.0 is necessary for building 8.1 apps
  4666. e1.Element("MinimumVisualStudioVersion", "12.0");
  4667. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4668. isAppContainer = true;
  4669. }
  4670. } else if (rev == "8.0"_s) {
  4671. e1.Element("ApplicationTypeRevision", rev);
  4672. // Visual Studio 11.0 is necessary for building 8.0 apps
  4673. e1.Element("MinimumVisualStudioVersion", "11.0");
  4674. if (isWindowsStore &&
  4675. this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4676. isAppContainer = true;
  4677. } else if (isWindowsPhone &&
  4678. this->GeneratorTarget->GetType() ==
  4679. cmStateEnums::EXECUTABLE) {
  4680. e1.Element("XapOutputs", "true");
  4681. e1.Element("XapFilename",
  4682. cmStrCat(this->Name, "_$(Configuration)_$(Platform).xap"));
  4683. }
  4684. }
  4685. } else if (isAndroid) {
  4686. e1.Element("ApplicationType", "Android");
  4687. e1.Element("ApplicationTypeRevision",
  4688. gg->GetAndroidApplicationTypeRevision());
  4689. }
  4690. if (isAppContainer) {
  4691. e1.Element("AppContainerApplication", "true");
  4692. } else if (!isAndroid) {
  4693. if (this->Platform == "ARM64"_s) {
  4694. e1.Element("WindowsSDKDesktopARM64Support", "true");
  4695. } else if (this->Platform == "ARM"_s) {
  4696. e1.Element("WindowsSDKDesktopARMSupport", "true");
  4697. }
  4698. }
  4699. std::string const& targetPlatformVersion =
  4700. gg->GetWindowsTargetPlatformVersion();
  4701. if (!targetPlatformVersion.empty()) {
  4702. e1.Element("WindowsTargetPlatformVersion", targetPlatformVersion);
  4703. }
  4704. cmValue targetPlatformMinVersion = this->GeneratorTarget->GetProperty(
  4705. "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION");
  4706. if (targetPlatformMinVersion) {
  4707. e1.Element("WindowsTargetPlatformMinVersion", *targetPlatformMinVersion);
  4708. } else if (isWindowsStore && rev == "10.0"_s) {
  4709. // If the min version is not set, then use the TargetPlatformVersion
  4710. if (!targetPlatformVersion.empty()) {
  4711. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformVersion);
  4712. }
  4713. }
  4714. // Added IoT Startup Task support
  4715. if (this->GeneratorTarget->GetPropertyAsBool("VS_IOT_STARTUP_TASK")) {
  4716. e1.Element("ContainsStartupTask", "true");
  4717. }
  4718. }
  4719. void cmVisualStudio10TargetGenerator::VerifyNecessaryFiles()
  4720. {
  4721. // For Windows and Windows Phone executables, we will assume that if a
  4722. // manifest is not present that we need to add all the necessary files
  4723. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4724. std::vector<cmGeneratorTarget::AllConfigSource> manifestSources =
  4725. this->GeneratorTarget->GetAllConfigSources(
  4726. cmGeneratorTarget::SourceKindAppManifest);
  4727. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4728. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4729. if (v == "8.0"_s) {
  4730. // Look through the sources for WMAppManifest.xml
  4731. bool foundManifest = false;
  4732. for (cmGeneratorTarget::AllConfigSource const& source :
  4733. this->GeneratorTarget->GetAllConfigSources()) {
  4734. if (source.Kind == cmGeneratorTarget::SourceKindExtra &&
  4735. "wmappmanifest.xml" ==
  4736. cmSystemTools::LowerCase(
  4737. source.Source->GetLocation().GetName())) {
  4738. foundManifest = true;
  4739. break;
  4740. }
  4741. }
  4742. if (!foundManifest) {
  4743. this->IsMissingFiles = true;
  4744. }
  4745. } else if (v == "8.1"_s) {
  4746. if (manifestSources.empty()) {
  4747. this->IsMissingFiles = true;
  4748. }
  4749. }
  4750. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4751. if (manifestSources.empty()) {
  4752. if (v == "8.0"_s) {
  4753. this->IsMissingFiles = true;
  4754. } else if (v == "8.1"_s || cmHasLiteralPrefix(v, "10.0")) {
  4755. this->IsMissingFiles = true;
  4756. }
  4757. }
  4758. }
  4759. }
  4760. }
  4761. void cmVisualStudio10TargetGenerator::WriteMissingFiles(Elem& e1)
  4762. {
  4763. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4764. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4765. if (v == "8.0"_s) {
  4766. this->WriteMissingFilesWP80(e1);
  4767. } else if (v == "8.1"_s) {
  4768. this->WriteMissingFilesWP81(e1);
  4769. }
  4770. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4771. if (v == "8.0"_s) {
  4772. this->WriteMissingFilesWS80(e1);
  4773. } else if (v == "8.1"_s) {
  4774. this->WriteMissingFilesWS81(e1);
  4775. } else if (cmHasLiteralPrefix(v, "10.0")) {
  4776. this->WriteMissingFilesWS10_0(e1);
  4777. }
  4778. }
  4779. }
  4780. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP80(Elem& e1)
  4781. {
  4782. std::string templateFolder =
  4783. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/Windows");
  4784. // For WP80, the manifest needs to be in the same folder as the project
  4785. // this can cause an overwrite problem if projects aren't organized in
  4786. // folders
  4787. std::string manifestFile = cmStrCat(
  4788. this->LocalGenerator->GetCurrentBinaryDirectory(), "/WMAppManifest.xml");
  4789. std::string artifactDir =
  4790. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4791. ConvertToWindowsSlash(artifactDir);
  4792. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4793. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4794. cmGeneratedFileStream fout(manifestFile);
  4795. fout.SetCopyIfDifferent(true);
  4796. /* clang-format off */
  4797. fout <<
  4798. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4799. "<Deployment"
  4800. " xmlns=\"http://schemas.microsoft.com/windowsphone/2012/deployment\""
  4801. " AppPlatformVersion=\"8.0\">\n"
  4802. "\t<DefaultLanguage xmlns=\"\" code=\"en-US\"/>\n"
  4803. "\t<App xmlns=\"\" ProductID=\"{" << this->GUID << "}\""
  4804. " Title=\"CMake Test Program\" RuntimeType=\"Modern Native\""
  4805. " Version=\"1.0.0.0\" Genre=\"apps.normal\" Author=\"CMake\""
  4806. " Description=\"Default CMake App\" Publisher=\"CMake\""
  4807. " PublisherID=\"{" << this->GUID << "}\">\n"
  4808. "\t\t<IconPath IsRelative=\"true\" IsResource=\"false\">"
  4809. << artifactDirXML << "\\ApplicationIcon.png</IconPath>\n"
  4810. "\t\t<Capabilities/>\n"
  4811. "\t\t<Tasks>\n"
  4812. "\t\t\t<DefaultTask Name=\"_default\""
  4813. " ImagePath=\"" << targetNameXML << ".exe\" ImageParams=\"\" />\n"
  4814. "\t\t</Tasks>\n"
  4815. "\t\t<Tokens>\n"
  4816. "\t\t\t<PrimaryToken TokenID=\"" << targetNameXML << "Token\""
  4817. " TaskName=\"_default\">\n"
  4818. "\t\t\t\t<TemplateFlip>\n"
  4819. "\t\t\t\t\t<SmallImageURI IsRelative=\"true\" IsResource=\"false\">"
  4820. << artifactDirXML << "\\SmallLogo.png</SmallImageURI>\n"
  4821. "\t\t\t\t\t<Count>0</Count>\n"
  4822. "\t\t\t\t\t<BackgroundImageURI IsRelative=\"true\" IsResource=\"false\">"
  4823. << artifactDirXML << "\\Logo.png</BackgroundImageURI>\n"
  4824. "\t\t\t\t</TemplateFlip>\n"
  4825. "\t\t\t</PrimaryToken>\n"
  4826. "\t\t</Tokens>\n"
  4827. "\t\t<ScreenResolutions>\n"
  4828. "\t\t\t<ScreenResolution Name=\"ID_RESOLUTION_WVGA\" />\n"
  4829. "\t\t</ScreenResolutions>\n"
  4830. "\t</App>\n"
  4831. "</Deployment>\n";
  4832. /* clang-format on */
  4833. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4834. ConvertToWindowsSlash(sourceFile);
  4835. {
  4836. Elem e2(e1, "Xml");
  4837. e2.Attribute("Include", sourceFile);
  4838. e2.Element("SubType", "Designer");
  4839. }
  4840. this->AddedFiles.push_back(sourceFile);
  4841. std::string smallLogo = cmStrCat(this->DefaultArtifactDir, "/SmallLogo.png");
  4842. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SmallLogo.png"),
  4843. smallLogo, false);
  4844. ConvertToWindowsSlash(smallLogo);
  4845. Elem(e1, "Image").Attribute("Include", smallLogo);
  4846. this->AddedFiles.push_back(smallLogo);
  4847. std::string logo = cmStrCat(this->DefaultArtifactDir, "/Logo.png");
  4848. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/Logo.png"), logo, false);
  4849. ConvertToWindowsSlash(logo);
  4850. Elem(e1, "Image").Attribute("Include", logo);
  4851. this->AddedFiles.push_back(logo);
  4852. std::string applicationIcon =
  4853. cmStrCat(this->DefaultArtifactDir, "/ApplicationIcon.png");
  4854. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/ApplicationIcon.png"),
  4855. applicationIcon, false);
  4856. ConvertToWindowsSlash(applicationIcon);
  4857. Elem(e1, "Image").Attribute("Include", applicationIcon);
  4858. this->AddedFiles.push_back(applicationIcon);
  4859. }
  4860. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP81(Elem& e1)
  4861. {
  4862. std::string manifestFile =
  4863. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  4864. std::string artifactDir =
  4865. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4866. ConvertToWindowsSlash(artifactDir);
  4867. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4868. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4869. cmGeneratedFileStream fout(manifestFile);
  4870. fout.SetCopyIfDifferent(true);
  4871. /* clang-format off */
  4872. fout <<
  4873. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4874. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4875. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\""
  4876. " xmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\">\n"
  4877. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4878. " Version=\"1.0.0.0\" />\n"
  4879. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID << "\""
  4880. " PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4881. "\t<Properties>\n"
  4882. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4883. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4884. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4885. "\t</Properties>\n"
  4886. "\t<Prerequisites>\n"
  4887. "\t\t<OSMinVersion>6.3.1</OSMinVersion>\n"
  4888. "\t\t<OSMaxVersionTested>6.3.1</OSMaxVersionTested>\n"
  4889. "\t</Prerequisites>\n"
  4890. "\t<Resources>\n"
  4891. "\t\t<Resource Language=\"x-generate\" />\n"
  4892. "\t</Resources>\n"
  4893. "\t<Applications>\n"
  4894. "\t\t<Application Id=\"App\""
  4895. " Executable=\"" << targetNameXML << ".exe\""
  4896. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4897. "\t\t\t<m2:VisualElements\n"
  4898. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4899. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4900. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4901. "\t\t\t\tForegroundText=\"light\"\n"
  4902. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4903. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4904. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4905. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4906. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4907. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4908. "\t\t\t\t</m2:DefaultTile>\n"
  4909. "\t\t\t\t<m2:SplashScreen"
  4910. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4911. "\t\t\t</m2:VisualElements>\n"
  4912. "\t\t</Application>\n"
  4913. "\t</Applications>\n"
  4914. "</Package>\n";
  4915. /* clang-format on */
  4916. this->WriteCommonMissingFiles(e1, manifestFile);
  4917. }
  4918. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS80(Elem& e1)
  4919. {
  4920. std::string manifestFile =
  4921. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  4922. std::string artifactDir =
  4923. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4924. ConvertToWindowsSlash(artifactDir);
  4925. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4926. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4927. cmGeneratedFileStream fout(manifestFile);
  4928. fout.SetCopyIfDifferent(true);
  4929. /* clang-format off */
  4930. fout <<
  4931. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4932. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\">\n"
  4933. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4934. " Version=\"1.0.0.0\" />\n"
  4935. "\t<Properties>\n"
  4936. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4937. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4938. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4939. "\t</Properties>\n"
  4940. "\t<Prerequisites>\n"
  4941. "\t\t<OSMinVersion>6.2.1</OSMinVersion>\n"
  4942. "\t\t<OSMaxVersionTested>6.2.1</OSMaxVersionTested>\n"
  4943. "\t</Prerequisites>\n"
  4944. "\t<Resources>\n"
  4945. "\t\t<Resource Language=\"x-generate\" />\n"
  4946. "\t</Resources>\n"
  4947. "\t<Applications>\n"
  4948. "\t\t<Application Id=\"App\""
  4949. " Executable=\"" << targetNameXML << ".exe\""
  4950. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4951. "\t\t\t<VisualElements"
  4952. " DisplayName=\"" << targetNameXML << "\""
  4953. " Description=\"" << targetNameXML << "\""
  4954. " BackgroundColor=\"#336699\" ForegroundText=\"light\""
  4955. " Logo=\"" << artifactDirXML << "\\Logo.png\""
  4956. " SmallLogo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4957. "\t\t\t\t<DefaultTile ShowName=\"allLogos\""
  4958. " ShortName=\"" << targetNameXML << "\" />\n"
  4959. "\t\t\t\t<SplashScreen"
  4960. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4961. "\t\t\t</VisualElements>\n"
  4962. "\t\t</Application>\n"
  4963. "\t</Applications>\n"
  4964. "</Package>\n";
  4965. /* clang-format on */
  4966. this->WriteCommonMissingFiles(e1, manifestFile);
  4967. }
  4968. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS81(Elem& e1)
  4969. {
  4970. std::string manifestFile =
  4971. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  4972. std::string artifactDir =
  4973. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4974. ConvertToWindowsSlash(artifactDir);
  4975. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4976. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4977. cmGeneratedFileStream fout(manifestFile);
  4978. fout.SetCopyIfDifferent(true);
  4979. /* clang-format off */
  4980. fout <<
  4981. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4982. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4983. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\">\n"
  4984. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4985. " Version=\"1.0.0.0\" />\n"
  4986. "\t<Properties>\n"
  4987. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4988. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4989. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4990. "\t</Properties>\n"
  4991. "\t<Prerequisites>\n"
  4992. "\t\t<OSMinVersion>6.3</OSMinVersion>\n"
  4993. "\t\t<OSMaxVersionTested>6.3</OSMaxVersionTested>\n"
  4994. "\t</Prerequisites>\n"
  4995. "\t<Resources>\n"
  4996. "\t\t<Resource Language=\"x-generate\" />\n"
  4997. "\t</Resources>\n"
  4998. "\t<Applications>\n"
  4999. "\t\t<Application Id=\"App\""
  5000. " Executable=\"" << targetNameXML << ".exe\""
  5001. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  5002. "\t\t\t<m2:VisualElements\n"
  5003. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  5004. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  5005. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  5006. "\t\t\t\tForegroundText=\"light\"\n"
  5007. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  5008. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  5009. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  5010. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  5011. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  5012. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  5013. "\t\t\t\t</m2:DefaultTile>\n"
  5014. "\t\t\t\t<m2:SplashScreen"
  5015. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  5016. "\t\t\t</m2:VisualElements>\n"
  5017. "\t\t</Application>\n"
  5018. "\t</Applications>\n"
  5019. "</Package>\n";
  5020. /* clang-format on */
  5021. this->WriteCommonMissingFiles(e1, manifestFile);
  5022. }
  5023. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS10_0(Elem& e1)
  5024. {
  5025. std::string manifestFile =
  5026. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  5027. std::string artifactDir =
  5028. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  5029. ConvertToWindowsSlash(artifactDir);
  5030. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  5031. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  5032. cmGeneratedFileStream fout(manifestFile);
  5033. fout.SetCopyIfDifferent(true);
  5034. /* clang-format off */
  5035. fout <<
  5036. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  5037. "<Package\n\t"
  5038. "xmlns=\"http://schemas.microsoft.com/appx/manifest/foundation/windows10\""
  5039. "\txmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\"\n"
  5040. "\txmlns:uap=\"http://schemas.microsoft.com/appx/manifest/uap/windows10\""
  5041. "\n\tIgnorableNamespaces=\"uap mp\">\n\n"
  5042. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  5043. " Version=\"1.0.0.0\" />\n"
  5044. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID <<
  5045. "\" PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  5046. "\t<Properties>\n"
  5047. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  5048. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  5049. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  5050. "\t</Properties>\n"
  5051. "\t<Dependencies>\n"
  5052. "\t\t<TargetDeviceFamily Name=\"Windows.Universal\" "
  5053. "MinVersion=\"10.0.0.0\" MaxVersionTested=\"10.0.0.0\" />\n"
  5054. "\t</Dependencies>\n"
  5055. "\t<Resources>\n"
  5056. "\t\t<Resource Language=\"x-generate\" />\n"
  5057. "\t</Resources>\n"
  5058. "\t<Applications>\n"
  5059. "\t\t<Application Id=\"App\""
  5060. " Executable=\"" << targetNameXML << ".exe\""
  5061. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  5062. "\t\t\t<uap:VisualElements\n"
  5063. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  5064. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  5065. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  5066. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  5067. "\t\t\t\tSquare44x44Logo=\"" << artifactDirXML <<
  5068. "\\SmallLogo44x44.png\">\n"
  5069. "\t\t\t\t<uap:SplashScreen"
  5070. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  5071. "\t\t\t</uap:VisualElements>\n"
  5072. "\t\t</Application>\n"
  5073. "\t</Applications>\n"
  5074. "</Package>\n";
  5075. /* clang-format on */
  5076. this->WriteCommonMissingFiles(e1, manifestFile);
  5077. }
  5078. void cmVisualStudio10TargetGenerator::WriteCommonMissingFiles(
  5079. Elem& e1, const std::string& manifestFile)
  5080. {
  5081. std::string templateFolder =
  5082. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/Windows");
  5083. std::string sourceFile = this->ConvertPath(manifestFile, false);
  5084. ConvertToWindowsSlash(sourceFile);
  5085. {
  5086. Elem e2(e1, "AppxManifest");
  5087. e2.Attribute("Include", sourceFile);
  5088. e2.Element("SubType", "Designer");
  5089. }
  5090. this->AddedFiles.push_back(sourceFile);
  5091. std::string smallLogo = cmStrCat(this->DefaultArtifactDir, "/SmallLogo.png");
  5092. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SmallLogo.png"),
  5093. smallLogo, false);
  5094. ConvertToWindowsSlash(smallLogo);
  5095. Elem(e1, "Image").Attribute("Include", smallLogo);
  5096. this->AddedFiles.push_back(smallLogo);
  5097. std::string smallLogo44 =
  5098. cmStrCat(this->DefaultArtifactDir, "/SmallLogo44x44.png");
  5099. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SmallLogo44x44.png"),
  5100. smallLogo44, false);
  5101. ConvertToWindowsSlash(smallLogo44);
  5102. Elem(e1, "Image").Attribute("Include", smallLogo44);
  5103. this->AddedFiles.push_back(smallLogo44);
  5104. std::string logo = cmStrCat(this->DefaultArtifactDir, "/Logo.png");
  5105. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/Logo.png"), logo, false);
  5106. ConvertToWindowsSlash(logo);
  5107. Elem(e1, "Image").Attribute("Include", logo);
  5108. this->AddedFiles.push_back(logo);
  5109. std::string storeLogo = cmStrCat(this->DefaultArtifactDir, "/StoreLogo.png");
  5110. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/StoreLogo.png"),
  5111. storeLogo, false);
  5112. ConvertToWindowsSlash(storeLogo);
  5113. Elem(e1, "Image").Attribute("Include", storeLogo);
  5114. this->AddedFiles.push_back(storeLogo);
  5115. std::string splashScreen =
  5116. cmStrCat(this->DefaultArtifactDir, "/SplashScreen.png");
  5117. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SplashScreen.png"),
  5118. splashScreen, false);
  5119. ConvertToWindowsSlash(splashScreen);
  5120. Elem(e1, "Image").Attribute("Include", splashScreen);
  5121. this->AddedFiles.push_back(splashScreen);
  5122. if (this->AddedDefaultCertificate) {
  5123. // This file has already been added to the build so don't copy it
  5124. std::string keyFile =
  5125. cmStrCat(this->DefaultArtifactDir, "/Windows_TemporaryKey.pfx");
  5126. ConvertToWindowsSlash(keyFile);
  5127. Elem(e1, "None").Attribute("Include", keyFile);
  5128. }
  5129. }
  5130. bool cmVisualStudio10TargetGenerator::ForceOld(const std::string& source) const
  5131. {
  5132. HANDLE h =
  5133. CreateFileW(cmSystemTools::ConvertToWindowsExtendedPath(source).c_str(),
  5134. FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING,
  5135. FILE_FLAG_BACKUP_SEMANTICS, 0);
  5136. if (!h) {
  5137. return false;
  5138. }
  5139. FILETIME const ftime_20010101 = { 3365781504u, 29389701u };
  5140. if (!SetFileTime(h, &ftime_20010101, &ftime_20010101, &ftime_20010101)) {
  5141. CloseHandle(h);
  5142. return false;
  5143. }
  5144. CloseHandle(h);
  5145. return true;
  5146. }
  5147. void cmVisualStudio10TargetGenerator::GetCSharpSourceProperties(
  5148. cmSourceFile const* sf, std::map<std::string, std::string>& tags)
  5149. {
  5150. if (this->ProjectType == VsProjectType::csproj) {
  5151. const cmPropertyMap& props = sf->GetProperties();
  5152. for (const std::string& p : props.GetKeys()) {
  5153. static const cm::string_view propNamePrefix = "VS_CSHARP_";
  5154. if (cmHasPrefix(p, propNamePrefix)) {
  5155. std::string tagName = p.substr(propNamePrefix.length());
  5156. if (!tagName.empty()) {
  5157. cmValue val = props.GetPropertyValue(p);
  5158. if (cmNonempty(val)) {
  5159. tags[tagName] = *val;
  5160. } else {
  5161. tags.erase(tagName);
  5162. }
  5163. }
  5164. }
  5165. }
  5166. }
  5167. }
  5168. void cmVisualStudio10TargetGenerator::WriteCSharpSourceProperties(
  5169. Elem& e2, const std::map<std::string, std::string>& tags)
  5170. {
  5171. for (const auto& i : tags) {
  5172. e2.Element(i.first, i.second);
  5173. }
  5174. }
  5175. std::string cmVisualStudio10TargetGenerator::GetCSharpSourceLink(
  5176. cmSourceFile const* source)
  5177. {
  5178. // For out of source files, we first check if a matching source group
  5179. // for this file exists, otherwise we check if the path relative to current
  5180. // source- or binary-dir is used within the link and return that.
  5181. // In case of .cs files we can't do that automatically for files in the
  5182. // binary directory, because this leads to compilation errors.
  5183. std::string link;
  5184. std::string sourceGroupedFile;
  5185. std::string const& fullFileName = source->GetFullPath();
  5186. std::string const& srcDir = this->Makefile->GetCurrentSourceDirectory();
  5187. std::string const& binDir = this->Makefile->GetCurrentBinaryDirectory();
  5188. // unfortunately we have to copy the source groups, because
  5189. // FindSourceGroup uses a regex which is modifying the group
  5190. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  5191. cmSourceGroup* sourceGroup =
  5192. this->Makefile->FindSourceGroup(fullFileName, sourceGroups);
  5193. if (sourceGroup && !sourceGroup->GetFullName().empty()) {
  5194. sourceGroupedFile =
  5195. cmStrCat(sourceGroup->GetFullName(), '/',
  5196. cmsys::SystemTools::GetFilenameName(fullFileName));
  5197. cmsys::SystemTools::ConvertToUnixSlashes(sourceGroupedFile);
  5198. }
  5199. if (!sourceGroupedFile.empty() &&
  5200. cmHasSuffix(fullFileName, sourceGroupedFile)) {
  5201. link = sourceGroupedFile;
  5202. } else if (cmHasPrefix(fullFileName, srcDir)) {
  5203. link = fullFileName.substr(srcDir.length() + 1);
  5204. } else if (!cmHasSuffix(fullFileName, ".cs") &&
  5205. cmHasPrefix(fullFileName, binDir)) {
  5206. link = fullFileName.substr(binDir.length() + 1);
  5207. } else if (cmValue l = source->GetProperty("VS_CSHARP_Link")) {
  5208. link = *l;
  5209. }
  5210. ConvertToWindowsSlash(link);
  5211. return link;
  5212. }
  5213. std::string cmVisualStudio10TargetGenerator::GetCMakeFilePath(
  5214. const char* relativeFilePath) const
  5215. {
  5216. // Always search in the standard modules location.
  5217. std::string path =
  5218. cmStrCat(cmSystemTools::GetCMakeRoot(), '/', relativeFilePath);
  5219. ConvertToWindowsSlash(path);
  5220. return path;
  5221. }
  5222. void cmVisualStudio10TargetGenerator::WriteStdOutEncodingUtf8(Elem& e1)
  5223. {
  5224. if (this->GlobalGenerator->IsUtf8EncodingSupported()) {
  5225. e1.Element("UseUtf8Encoding", "Always");
  5226. } else if (this->GlobalGenerator->IsStdOutEncodingSupported()) {
  5227. e1.Element("StdOutEncoding", "UTF-8");
  5228. }
  5229. }
  5230. void cmVisualStudio10TargetGenerator::UpdateCache()
  5231. {
  5232. std::vector<std::string> packageReferences;
  5233. if (this->GeneratorTarget->HasPackageReferences()) {
  5234. // Store a cache entry that later determines, if a package restore is
  5235. // required.
  5236. this->GeneratorTarget->Makefile->AddCacheDefinition(
  5237. cmStrCat(this->GeneratorTarget->GetName(),
  5238. "_REQUIRES_VS_PACKAGE_RESTORE"),
  5239. "ON", "Value Computed by CMake", cmStateEnums::STATIC);
  5240. } else {
  5241. // If there are any dependencies that require package restore, inherit the
  5242. // cache variable.
  5243. cmGlobalGenerator::TargetDependSet const& unordered =
  5244. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  5245. using OrderedTargetDependSet =
  5246. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  5247. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  5248. for (cmGeneratorTarget const* dt : depends) {
  5249. if (dt->HasPackageReferences()) {
  5250. this->GeneratorTarget->Makefile->AddCacheDefinition(
  5251. cmStrCat(this->GeneratorTarget->GetName(),
  5252. "_REQUIRES_VS_PACKAGE_RESTORE"),
  5253. "ON", "Value Computed by CMake", cmStateEnums::STATIC);
  5254. }
  5255. }
  5256. }
  5257. }