cmVisualStudio10TargetGenerator.cxx 209 KB

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