cmVisualStudio10TargetGenerator.cxx 202 KB

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