cmVisualStudio10TargetGenerator.cxx 204 KB

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