cmVisualStudio10TargetGenerator.cxx 199 KB

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