cmVisualStudio10TargetGenerator.cxx 193 KB

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