cmVisualStudio10TargetGenerator.cxx 193 KB

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