cmVisualStudio10TargetGenerator.cxx 200 KB

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