cmVisualStudio10TargetGenerator.cxx 201 KB

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