cmVisualStudio10TargetGenerator.cxx 203 KB

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