cmVisualStudio10TargetGenerator.cxx 201 KB

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