cmVisualStudio10TargetGenerator.cxx 210 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846
  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. if (cmValue p = this->GeneratorTarget->GetProperty("VS_FILTER_PROPS")) {
  1921. auto props = *p;
  1922. if (!props.empty()) {
  1923. ConvertToWindowsSlash(props);
  1924. Elem(e0, "Import")
  1925. .Attribute("Project", props)
  1926. .Attribute("Condition", cmStrCat("exists('", props, "')"))
  1927. .Attribute("Label", "LocalAppDataPlatform");
  1928. }
  1929. }
  1930. }
  1931. }
  1932. fout << '\n';
  1933. if (fout.Close()) {
  1934. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  1935. }
  1936. }
  1937. // Add to groupsUsed empty source groups that have non-empty children.
  1938. void cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  1939. std::set<cmSourceGroup const*>& groupsUsed,
  1940. const std::vector<cmSourceGroup>& allGroups)
  1941. {
  1942. for (cmSourceGroup const& current : allGroups) {
  1943. std::vector<cmSourceGroup> const& children = current.GetGroupChildren();
  1944. if (children.empty()) {
  1945. continue; // the group is really empty
  1946. }
  1947. this->AddMissingSourceGroups(groupsUsed, children);
  1948. if (groupsUsed.count(&current) > 0) {
  1949. continue; // group has already been added to set
  1950. }
  1951. // check if it least one of the group's descendants is not empty
  1952. // (at least one child must already have been added)
  1953. auto child_it = children.begin();
  1954. while (child_it != children.end()) {
  1955. if (groupsUsed.count(&(*child_it)) > 0) {
  1956. break; // found a child that was already added => add current group too
  1957. }
  1958. child_it++;
  1959. }
  1960. if (child_it == children.end()) {
  1961. continue; // no descendants have source files => ignore this group
  1962. }
  1963. groupsUsed.insert(&current);
  1964. }
  1965. }
  1966. void cmVisualStudio10TargetGenerator::WriteGroupSources(
  1967. Elem& e0, std::string const& name, ToolSources const& sources,
  1968. std::vector<cmSourceGroup>& sourceGroups)
  1969. {
  1970. Elem e1(e0, "ItemGroup");
  1971. e1.SetHasElements();
  1972. for (ToolSource const& s : sources) {
  1973. cmSourceFile const* sf = s.SourceFile;
  1974. std::string const& source = sf->GetFullPath();
  1975. cmSourceGroup* sourceGroup =
  1976. this->Makefile->FindSourceGroup(source, sourceGroups);
  1977. std::string const& filter = sourceGroup->GetFullName();
  1978. std::string path = this->ConvertPath(source, s.RelativePath);
  1979. ConvertToWindowsSlash(path);
  1980. Elem e2(e1, name);
  1981. e2.Attribute("Include", path);
  1982. if (!filter.empty()) {
  1983. e2.Element("Filter", filter);
  1984. }
  1985. }
  1986. }
  1987. void cmVisualStudio10TargetGenerator::WriteHeaderSource(
  1988. Elem& e1, cmSourceFile const* sf, ConfigToSettings const& toolSettings)
  1989. {
  1990. std::string const& fileName = sf->GetFullPath();
  1991. Elem e2(e1, "ClInclude");
  1992. this->WriteSource(e2, sf);
  1993. if (this->IsResxHeader(fileName)) {
  1994. e2.Element("FileType", "CppForm");
  1995. } else if (this->IsXamlHeader(fileName)) {
  1996. e2.Element("DependentUpon",
  1997. fileName.substr(0, fileName.find_last_of('.')));
  1998. }
  1999. this->FinishWritingSource(e2, toolSettings);
  2000. }
  2001. void cmVisualStudio10TargetGenerator::ParseSettingsProperty(
  2002. const std::string& settingsPropertyValue, ConfigToSettings& toolSettings)
  2003. {
  2004. if (!settingsPropertyValue.empty()) {
  2005. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2006. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2007. ge.Parse(settingsPropertyValue);
  2008. for (const std::string& config : this->Configurations) {
  2009. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2010. cmList settings{ evaluated };
  2011. for (const auto& setting : settings) {
  2012. const std::string::size_type assignment = setting.find('=');
  2013. if (assignment != std::string::npos) {
  2014. const std::string propName = setting.substr(0, assignment);
  2015. const std::string propValue = setting.substr(assignment + 1);
  2016. if (!propValue.empty()) {
  2017. toolSettings[config][propName] = propValue;
  2018. }
  2019. }
  2020. }
  2021. }
  2022. }
  2023. }
  2024. bool cmVisualStudio10TargetGenerator::PropertyIsSameInAllConfigs(
  2025. const ConfigToSettings& toolSettings, const std::string& propName)
  2026. {
  2027. std::string firstPropValue;
  2028. for (const auto& configToSettings : toolSettings) {
  2029. const std::unordered_map<std::string, std::string>& settings =
  2030. configToSettings.second;
  2031. if (firstPropValue.empty()) {
  2032. if (settings.find(propName) != settings.end()) {
  2033. firstPropValue = settings.find(propName)->second;
  2034. }
  2035. }
  2036. if (settings.find(propName) == settings.end()) {
  2037. return false;
  2038. }
  2039. if (settings.find(propName)->second != firstPropValue) {
  2040. return false;
  2041. }
  2042. }
  2043. return true;
  2044. }
  2045. void cmVisualStudio10TargetGenerator::WriteExtraSource(
  2046. Elem& e1, cmSourceFile const* sf, ConfigToSettings& toolSettings)
  2047. {
  2048. bool toolHasSettings = false;
  2049. const char* tool = "None";
  2050. std::string settingsGenerator;
  2051. std::string settingsLastGenOutput;
  2052. std::string sourceLink;
  2053. std::string subType;
  2054. std::string copyToOutDir;
  2055. std::string includeInVsix;
  2056. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  2057. if (this->ProjectType == VsProjectType::csproj && !this->InSourceBuild) {
  2058. toolHasSettings = true;
  2059. }
  2060. if (ext == "hlsl"_s) {
  2061. tool = "FXCompile";
  2062. // Figure out the type of shader compiler to use.
  2063. if (cmValue st = sf->GetProperty("VS_SHADER_TYPE")) {
  2064. for (const std::string& config : this->Configurations) {
  2065. toolSettings[config]["ShaderType"] = *st;
  2066. }
  2067. }
  2068. // Figure out which entry point to use if any
  2069. if (cmValue se = sf->GetProperty("VS_SHADER_ENTRYPOINT")) {
  2070. for (const std::string& config : this->Configurations) {
  2071. toolSettings[config]["EntryPointName"] = *se;
  2072. }
  2073. }
  2074. // Figure out which shader model to use if any
  2075. if (cmValue sm = sf->GetProperty("VS_SHADER_MODEL")) {
  2076. for (const std::string& config : this->Configurations) {
  2077. toolSettings[config]["ShaderModel"] = *sm;
  2078. }
  2079. }
  2080. // Figure out which output header file to use if any
  2081. if (cmValue ohf = sf->GetProperty("VS_SHADER_OUTPUT_HEADER_FILE")) {
  2082. for (const std::string& config : this->Configurations) {
  2083. toolSettings[config]["HeaderFileOutput"] = *ohf;
  2084. }
  2085. }
  2086. // Figure out which variable name to use if any
  2087. if (cmValue vn = sf->GetProperty("VS_SHADER_VARIABLE_NAME")) {
  2088. for (const std::string& config : this->Configurations) {
  2089. toolSettings[config]["VariableName"] = *vn;
  2090. }
  2091. }
  2092. // Figure out if there's any additional flags to use
  2093. if (cmValue saf = sf->GetProperty("VS_SHADER_FLAGS")) {
  2094. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2095. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*saf);
  2096. for (const std::string& config : this->Configurations) {
  2097. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2098. if (!evaluated.empty()) {
  2099. toolSettings[config]["AdditionalOptions"] = evaluated;
  2100. }
  2101. }
  2102. }
  2103. // Figure out if debug information should be generated
  2104. if (cmValue sed = sf->GetProperty("VS_SHADER_ENABLE_DEBUG")) {
  2105. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2106. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*sed);
  2107. for (const std::string& config : this->Configurations) {
  2108. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2109. if (!evaluated.empty()) {
  2110. toolSettings[config]["EnableDebuggingInformation"] =
  2111. cmIsOn(evaluated) ? "true" : "false";
  2112. }
  2113. }
  2114. }
  2115. // Figure out if optimizations should be disabled
  2116. if (cmValue sdo = sf->GetProperty("VS_SHADER_DISABLE_OPTIMIZATIONS")) {
  2117. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2118. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*sdo);
  2119. for (const std::string& config : this->Configurations) {
  2120. std::string evaluated = cge->Evaluate(this->LocalGenerator, config);
  2121. if (!evaluated.empty()) {
  2122. toolSettings[config]["DisableOptimizations"] =
  2123. cmIsOn(evaluated) ? "true" : "false";
  2124. }
  2125. }
  2126. }
  2127. if (cmValue sofn = sf->GetProperty("VS_SHADER_OBJECT_FILE_NAME")) {
  2128. for (const std::string& config : this->Configurations) {
  2129. toolSettings[config]["ObjectFileOutput"] = *sofn;
  2130. }
  2131. }
  2132. } else if (ext == "jpg"_s || ext == "png"_s) {
  2133. tool = "Image";
  2134. } else if (ext == "resw"_s) {
  2135. tool = "PRIResource";
  2136. } else if (ext == "xml"_s) {
  2137. tool = "XML";
  2138. } else if (ext == "natvis"_s) {
  2139. tool = "Natvis";
  2140. } else if (ext == "settings"_s) {
  2141. settingsLastGenOutput =
  2142. cmsys::SystemTools::GetFilenameName(sf->GetFullPath());
  2143. std::size_t pos = settingsLastGenOutput.find(".settings");
  2144. settingsLastGenOutput.replace(pos, 9, ".Designer.cs");
  2145. settingsGenerator = "SettingsSingleFileGenerator";
  2146. toolHasSettings = true;
  2147. } else if (ext == "vsixmanifest"_s) {
  2148. subType = "Designer";
  2149. }
  2150. if (cmValue c = sf->GetProperty("VS_COPY_TO_OUT_DIR")) {
  2151. tool = "Content";
  2152. copyToOutDir = *c;
  2153. toolHasSettings = true;
  2154. }
  2155. if (sf->GetPropertyAsBool("VS_INCLUDE_IN_VSIX")) {
  2156. includeInVsix = "True";
  2157. tool = "Content";
  2158. toolHasSettings = true;
  2159. }
  2160. // Collect VS_CSHARP_* property values (if some are set)
  2161. std::map<std::string, std::string> sourceFileTags;
  2162. this->GetCSharpSourceProperties(sf, sourceFileTags);
  2163. if (this->NsightTegra) {
  2164. // Nsight Tegra needs specific file types to check up-to-dateness.
  2165. std::string name = cmSystemTools::LowerCase(sf->GetLocation().GetName());
  2166. if (name == "androidmanifest.xml"_s || name == "build.xml"_s ||
  2167. name == "proguard.cfg"_s || name == "proguard-project.txt"_s ||
  2168. ext == "properties"_s) {
  2169. tool = "AndroidBuild";
  2170. } else if (ext == "java"_s) {
  2171. tool = "JCompile";
  2172. } else if (ext == "asm"_s || ext == "s"_s) {
  2173. tool = "ClCompile";
  2174. }
  2175. }
  2176. cmValue toolOverride = sf->GetProperty("VS_TOOL_OVERRIDE");
  2177. if (cmNonempty(toolOverride)) {
  2178. tool = toolOverride->c_str();
  2179. }
  2180. std::string deployContent;
  2181. std::string deployLocation;
  2182. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  2183. this->GlobalGenerator->TargetsWindowsStore()) {
  2184. cmValue content = sf->GetProperty("VS_DEPLOYMENT_CONTENT");
  2185. if (cmNonempty(content)) {
  2186. toolHasSettings = true;
  2187. deployContent = *content;
  2188. cmValue location = sf->GetProperty("VS_DEPLOYMENT_LOCATION");
  2189. if (cmNonempty(location)) {
  2190. deployLocation = *location;
  2191. }
  2192. }
  2193. }
  2194. if (ParsedToolTargetSettings.find(tool) == ParsedToolTargetSettings.end()) {
  2195. cmValue toolTargetProperty = this->GeneratorTarget->Target->GetProperty(
  2196. cmStrCat("VS_SOURCE_SETTINGS_", tool));
  2197. ConfigToSettings toolTargetSettings;
  2198. if (toolTargetProperty) {
  2199. ParseSettingsProperty(*toolTargetProperty, toolTargetSettings);
  2200. }
  2201. ParsedToolTargetSettings[tool] = toolTargetSettings;
  2202. }
  2203. for (const auto& configToSetting : ParsedToolTargetSettings[tool]) {
  2204. for (const auto& setting : configToSetting.second) {
  2205. toolSettings[configToSetting.first][setting.first] = setting.second;
  2206. }
  2207. }
  2208. if (!toolSettings.empty()) {
  2209. toolHasSettings = true;
  2210. }
  2211. Elem e2(e1, tool);
  2212. this->WriteSource(e2, sf);
  2213. if (toolHasSettings) {
  2214. e2.SetHasElements();
  2215. this->FinishWritingSource(e2, toolSettings);
  2216. if (!deployContent.empty()) {
  2217. cmGeneratorExpression ge(*this->LocalGenerator->GetCMakeInstance());
  2218. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2219. ge.Parse(deployContent);
  2220. // Deployment location cannot be set on a configuration basis
  2221. if (!deployLocation.empty()) {
  2222. e2.Element("Link",
  2223. cmStrCat(deployLocation, "\\%(FileName)%(Extension)"));
  2224. }
  2225. for (auto& config : this->Configurations) {
  2226. if (cge->Evaluate(this->LocalGenerator, config) == "1"_s) {
  2227. e2.WritePlatformConfigTag(
  2228. "DeploymentContent",
  2229. cmStrCat("'$(Configuration)|$(Platform)'=='", config, '|',
  2230. this->Platform, '\''),
  2231. "true");
  2232. } else {
  2233. e2.WritePlatformConfigTag(
  2234. "ExcludedFromBuild",
  2235. cmStrCat("'$(Configuration)|$(Platform)'=='", config, '|',
  2236. this->Platform, '\''),
  2237. "true");
  2238. }
  2239. }
  2240. }
  2241. if (!settingsGenerator.empty()) {
  2242. e2.Element("Generator", settingsGenerator);
  2243. }
  2244. if (!settingsLastGenOutput.empty()) {
  2245. e2.Element("LastGenOutput", settingsLastGenOutput);
  2246. }
  2247. if (!subType.empty()) {
  2248. e2.Element("SubType", subType);
  2249. }
  2250. if (!copyToOutDir.empty()) {
  2251. e2.Element("CopyToOutputDirectory", copyToOutDir);
  2252. }
  2253. if (!includeInVsix.empty()) {
  2254. e2.Element("IncludeInVSIX", includeInVsix);
  2255. }
  2256. // write source file specific tags
  2257. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2258. }
  2259. }
  2260. void cmVisualStudio10TargetGenerator::WriteSource(Elem& e2,
  2261. cmSourceFile const* sf)
  2262. {
  2263. // Visual Studio tools append relative paths to the current dir, as in:
  2264. //
  2265. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  2266. //
  2267. // and fail if this exceeds the maximum allowed path length. Our path
  2268. // conversion uses full paths when possible to allow deeper trees.
  2269. // However, CUDA 8.0 msbuild rules fail on absolute paths so for CUDA
  2270. // we must use relative paths.
  2271. bool forceRelative = sf->GetLanguage() == "CUDA"_s;
  2272. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), forceRelative);
  2273. ConvertToWindowsSlash(sourceFile);
  2274. e2.Attribute("Include", sourceFile);
  2275. if (this->ProjectType == VsProjectType::csproj && !this->InSourceBuild) {
  2276. // For out of source projects we have to provide a link (if not specified
  2277. // via property) for every source file (besides .cs files) otherwise they
  2278. // will not be visible in VS at all.
  2279. // First we check if the file is in a source group, then we check if the
  2280. // file path is relative to current source- or binary-dir, otherwise it is
  2281. // added with the plain filename without any path. This means the file will
  2282. // show up at root-level of the csproj (where CMakeLists.txt etc. are).
  2283. std::string link = this->GetCSharpSourceLink(sf);
  2284. if (link.empty()) {
  2285. link = cmsys::SystemTools::GetFilenameName(sf->GetFullPath());
  2286. }
  2287. e2.Element("Link", link);
  2288. }
  2289. ToolSource toolSource = { sf, forceRelative };
  2290. this->Tools[e2.Tag].push_back(toolSource);
  2291. }
  2292. void cmVisualStudio10TargetGenerator::WriteAllSources(Elem& e0)
  2293. {
  2294. if (this->GeneratorTarget->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2295. return;
  2296. }
  2297. const bool haveUnityBuild =
  2298. this->GeneratorTarget->GetPropertyAsBool("UNITY_BUILD");
  2299. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  2300. Elem e1(e0, "PropertyGroup");
  2301. e1.Element("EnableUnitySupport", "true");
  2302. }
  2303. Elem e1(e0, "ItemGroup");
  2304. e1.SetHasElements();
  2305. std::vector<size_t> all_configs;
  2306. for (size_t ci = 0; ci < this->Configurations.size(); ++ci) {
  2307. all_configs.push_back(ci);
  2308. }
  2309. std::vector<cmGeneratorTarget::AllConfigSource> const& sources =
  2310. this->GeneratorTarget->GetAllConfigSources();
  2311. cmSourceFile const* srcCMakeLists =
  2312. this->LocalGenerator->CreateVCProjBuildRule();
  2313. for (cmGeneratorTarget::AllConfigSource const& si : sources) {
  2314. if (si.Source == srcCMakeLists) {
  2315. // Skip explicit reference to CMakeLists.txt source.
  2316. continue;
  2317. }
  2318. ConfigToSettings toolSettings;
  2319. for (const auto& config : this->Configurations) {
  2320. toolSettings[config];
  2321. }
  2322. if (cmValue p = si.Source->GetProperty("VS_SETTINGS")) {
  2323. ParseSettingsProperty(*p, toolSettings);
  2324. }
  2325. const char* tool = nullptr;
  2326. switch (si.Kind) {
  2327. case cmGeneratorTarget::SourceKindAppManifest:
  2328. tool = "AppxManifest";
  2329. break;
  2330. case cmGeneratorTarget::SourceKindCertificate:
  2331. tool = "None";
  2332. break;
  2333. case cmGeneratorTarget::SourceKindCustomCommand:
  2334. // Handled elsewhere.
  2335. break;
  2336. case cmGeneratorTarget::SourceKindExternalObject:
  2337. tool = "Object";
  2338. break;
  2339. case cmGeneratorTarget::SourceKindExtra:
  2340. this->WriteExtraSource(e1, si.Source, toolSettings);
  2341. break;
  2342. case cmGeneratorTarget::SourceKindHeader:
  2343. this->WriteHeaderSource(e1, si.Source, toolSettings);
  2344. break;
  2345. case cmGeneratorTarget::SourceKindIDL:
  2346. tool = "Midl";
  2347. break;
  2348. case cmGeneratorTarget::SourceKindManifest:
  2349. // Handled elsewhere.
  2350. break;
  2351. case cmGeneratorTarget::SourceKindModuleDefinition:
  2352. tool = "None";
  2353. break;
  2354. case cmGeneratorTarget::SourceKindCxxModuleSource:
  2355. case cmGeneratorTarget::SourceKindUnityBatched:
  2356. case cmGeneratorTarget::SourceKindObjectSource: {
  2357. const std::string& lang = si.Source->GetLanguage();
  2358. if (lang == "C"_s || lang == "CXX"_s) {
  2359. tool = "ClCompile";
  2360. } else if (lang == "ASM_MARMASM"_s &&
  2361. this->GlobalGenerator->IsMarmasmEnabled()) {
  2362. tool = "MARMASM";
  2363. } else if (lang == "ASM_MASM"_s &&
  2364. this->GlobalGenerator->IsMasmEnabled()) {
  2365. tool = "MASM";
  2366. } else if (lang == "ASM_NASM"_s &&
  2367. this->GlobalGenerator->IsNasmEnabled()) {
  2368. tool = "NASM";
  2369. } else if (lang == "RC"_s) {
  2370. tool = "ResourceCompile";
  2371. } else if (lang == "CSharp"_s) {
  2372. tool = "Compile";
  2373. } else if (lang == "CUDA"_s &&
  2374. this->GlobalGenerator->IsCudaEnabled()) {
  2375. tool = "CudaCompile";
  2376. } else {
  2377. tool = "None";
  2378. }
  2379. } break;
  2380. case cmGeneratorTarget::SourceKindResx:
  2381. this->ResxObjs.push_back(si.Source);
  2382. break;
  2383. case cmGeneratorTarget::SourceKindXaml:
  2384. this->XamlObjs.push_back(si.Source);
  2385. break;
  2386. }
  2387. std::string config;
  2388. if (!this->Configurations.empty()) {
  2389. config = this->Configurations[si.Configs[0]];
  2390. }
  2391. auto const* fs =
  2392. this->GeneratorTarget->GetFileSetForSource(config, si.Source);
  2393. if (tool) {
  2394. // Compute set of configurations to exclude, if any.
  2395. std::vector<size_t> const& include_configs = si.Configs;
  2396. std::vector<size_t> exclude_configs;
  2397. std::set_difference(all_configs.begin(), all_configs.end(),
  2398. include_configs.begin(), include_configs.end(),
  2399. std::back_inserter(exclude_configs));
  2400. Elem e2(e1, tool);
  2401. bool isCSharp = (si.Source->GetLanguage() == "CSharp"_s);
  2402. if (isCSharp && !exclude_configs.empty()) {
  2403. std::stringstream conditions;
  2404. bool firstConditionSet{ false };
  2405. for (const auto& ci : include_configs) {
  2406. if (firstConditionSet) {
  2407. conditions << " Or ";
  2408. }
  2409. conditions << "('$(Configuration)|$(Platform)'=='"
  2410. << this->Configurations[ci] << '|' << this->Platform
  2411. << "')";
  2412. firstConditionSet = true;
  2413. }
  2414. e2.Attribute("Condition", conditions.str());
  2415. }
  2416. this->WriteSource(e2, si.Source);
  2417. bool useNativeUnityBuild = false;
  2418. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  2419. // Magic value taken from cmGlobalVisualStudioVersionedGenerator.cxx
  2420. static const std::string vs15 = "141";
  2421. std::string toolset =
  2422. this->GlobalGenerator->GetPlatformToolsetString();
  2423. cmSystemTools::ReplaceString(toolset, "v", "");
  2424. if (toolset.empty() ||
  2425. cmSystemTools::VersionCompareGreaterEq(toolset, vs15)) {
  2426. useNativeUnityBuild = true;
  2427. }
  2428. }
  2429. if (haveUnityBuild && strcmp(tool, "ClCompile") == 0 &&
  2430. si.Source->GetProperty("UNITY_SOURCE_FILE")) {
  2431. if (useNativeUnityBuild) {
  2432. e2.Attribute(
  2433. "IncludeInUnityFile",
  2434. si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")
  2435. ? "false"
  2436. : "true");
  2437. e2.Attribute("CustomUnityFile", "true");
  2438. std::string unityDir = cmSystemTools::GetFilenamePath(
  2439. *si.Source->GetProperty("UNITY_SOURCE_FILE"));
  2440. e2.Attribute("UnityFilesDirectory", unityDir);
  2441. } else {
  2442. // Visual Studio versions prior to 2017 15.8 do not know about unity
  2443. // builds, thus we exclude the files already part of unity sources.
  2444. if (!si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")) {
  2445. exclude_configs = all_configs;
  2446. }
  2447. }
  2448. }
  2449. if (si.Kind == cmGeneratorTarget::SourceKindObjectSource ||
  2450. si.Kind == cmGeneratorTarget::SourceKindUnityBatched) {
  2451. this->OutputSourceSpecificFlags(e2, si.Source);
  2452. } else if (fs && fs->GetType() == "CXX_MODULES"_s) {
  2453. this->GeneratorTarget->Makefile->IssueMessage(
  2454. MessageType::FATAL_ERROR,
  2455. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  2456. "\" has source file\n ", si.Source->GetFullPath(),
  2457. "\nin a \"FILE_SET TYPE CXX_MODULES\" but it is not "
  2458. "scheduled for compilation."));
  2459. }
  2460. if (si.Source->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  2461. e2.Element("PrecompiledHeader", "NotUsing");
  2462. }
  2463. if (!isCSharp && !exclude_configs.empty()) {
  2464. this->WriteExcludeFromBuild(e2, exclude_configs);
  2465. }
  2466. this->FinishWritingSource(e2, toolSettings);
  2467. } else if (fs && fs->GetType() == "CXX_MODULES"_s) {
  2468. this->GeneratorTarget->Makefile->IssueMessage(
  2469. MessageType::FATAL_ERROR,
  2470. cmStrCat("Target \"", this->GeneratorTarget->GetName(),
  2471. "\" has source file\n ", si.Source->GetFullPath(),
  2472. "\nin a \"FILE_SET TYPE CXX_MODULES\" but it is not "
  2473. "scheduled for compilation."));
  2474. }
  2475. }
  2476. if (this->IsMissingFiles) {
  2477. this->WriteMissingFiles(e1);
  2478. }
  2479. }
  2480. void cmVisualStudio10TargetGenerator::FinishWritingSource(
  2481. Elem& e2, ConfigToSettings const& toolSettings)
  2482. {
  2483. std::vector<std::string> writtenSettings;
  2484. for (const auto& configSettings : toolSettings) {
  2485. for (const auto& setting : configSettings.second) {
  2486. if (std::find(writtenSettings.begin(), writtenSettings.end(),
  2487. setting.first) != writtenSettings.end()) {
  2488. continue;
  2489. }
  2490. if (PropertyIsSameInAllConfigs(toolSettings, setting.first)) {
  2491. e2.Element(setting.first, setting.second);
  2492. writtenSettings.push_back(setting.first);
  2493. } else {
  2494. e2.WritePlatformConfigTag(setting.first,
  2495. cmStrCat("'$(Configuration)|$(Platform)'=='",
  2496. configSettings.first, '|',
  2497. this->Platform, '\''),
  2498. setting.second);
  2499. }
  2500. }
  2501. }
  2502. }
  2503. void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  2504. Elem& e2, cmSourceFile const* source)
  2505. {
  2506. cmSourceFile const& sf = *source;
  2507. std::string objectName;
  2508. if (this->GeneratorTarget->HasExplicitObjectName(&sf)) {
  2509. objectName = this->GeneratorTarget->GetObjectName(&sf);
  2510. }
  2511. std::string flags;
  2512. bool configDependentFlags = false;
  2513. std::string options;
  2514. bool configDependentOptions = false;
  2515. std::string defines;
  2516. bool configDependentDefines = false;
  2517. std::string includes;
  2518. bool configDependentIncludes = false;
  2519. if (cmValue cflags = sf.GetProperty("COMPILE_FLAGS")) {
  2520. configDependentFlags =
  2521. cmGeneratorExpression::Find(*cflags) != std::string::npos;
  2522. flags += *cflags;
  2523. }
  2524. if (cmValue coptions = sf.GetProperty("COMPILE_OPTIONS")) {
  2525. configDependentOptions =
  2526. cmGeneratorExpression::Find(*coptions) != std::string::npos;
  2527. options += *coptions;
  2528. }
  2529. if (cmValue cdefs = sf.GetProperty("COMPILE_DEFINITIONS")) {
  2530. configDependentDefines =
  2531. cmGeneratorExpression::Find(*cdefs) != std::string::npos;
  2532. defines += *cdefs;
  2533. }
  2534. if (cmValue cincludes = sf.GetProperty("INCLUDE_DIRECTORIES")) {
  2535. configDependentIncludes =
  2536. cmGeneratorExpression::Find(*cincludes) != std::string::npos;
  2537. includes += *cincludes;
  2538. }
  2539. // Force language if the file extension does not match.
  2540. // Note that MSVC treats the upper-case '.C' extension as C and not C++.
  2541. std::string const ext = sf.GetExtension();
  2542. std::string const extLang = ext == "C"_s
  2543. ? "C"
  2544. : this->GlobalGenerator->GetLanguageFromExtension(ext.c_str());
  2545. std::string lang = this->LocalGenerator->GetSourceFileLanguage(sf);
  2546. const char* compileAs = nullptr;
  2547. if (lang != extLang) {
  2548. if (lang == "CXX"_s) {
  2549. // force a C++ file type
  2550. compileAs = "CompileAsCpp";
  2551. } else if (lang == "C"_s) {
  2552. // force to c
  2553. compileAs = "CompileAsC";
  2554. }
  2555. }
  2556. bool noWinRT = this->TargetCompileAsWinRT && lang == "C"_s;
  2557. // for the first time we need a new line if there is something
  2558. // produced here.
  2559. if (!objectName.empty()) {
  2560. if (lang == "CUDA"_s) {
  2561. e2.Element("CompileOut", cmStrCat("$(IntDir)/", objectName));
  2562. } else {
  2563. e2.Element("ObjectFileName", cmStrCat("$(IntDir)/", objectName));
  2564. }
  2565. }
  2566. if (lang == "ASM_NASM"_s) {
  2567. if (cmValue objectDeps = sf.GetProperty("OBJECT_DEPENDS")) {
  2568. cmList depends{ *objectDeps };
  2569. for (auto& d : depends) {
  2570. ConvertToWindowsSlash(d);
  2571. }
  2572. e2.Element("AdditionalDependencies", depends.join(";"));
  2573. }
  2574. }
  2575. bool isCppModule = false;
  2576. for (std::string const& config : this->Configurations) {
  2577. this->GeneratorTarget->NeedCxxModuleSupport(lang, config);
  2578. std::string configUpper = cmSystemTools::UpperCase(config);
  2579. std::string configDefines = defines;
  2580. std::string defPropName = cmStrCat("COMPILE_DEFINITIONS_", configUpper);
  2581. if (cmValue ccdefs = sf.GetProperty(defPropName)) {
  2582. if (!configDefines.empty()) {
  2583. configDefines += ';';
  2584. }
  2585. configDependentDefines |=
  2586. cmGeneratorExpression::Find(*ccdefs) != std::string::npos;
  2587. configDefines += *ccdefs;
  2588. }
  2589. bool const shouldScanForModules = lang == "CXX"_s &&
  2590. this->GeneratorTarget->NeedDyndepForSource(lang, config, source);
  2591. auto const* fs =
  2592. this->GeneratorTarget->GetFileSetForSource(config, source);
  2593. const char* compileAsPerConfig = compileAs;
  2594. if (fs && fs->GetType() == "CXX_MODULES"_s) {
  2595. if (lang == "CXX"_s) {
  2596. if (fs->GetType() == "CXX_MODULES"_s) {
  2597. isCppModule = true;
  2598. if (shouldScanForModules &&
  2599. this->GlobalGenerator->IsScanDependenciesSupported()) {
  2600. // ScanSourceForModuleDependencies uses 'cl /scanDependencies' and
  2601. // can distinguish module interface units and internal partitions.
  2602. compileAsPerConfig = "CompileAsCpp";
  2603. } else {
  2604. compileAsPerConfig = "CompileAsCppModule";
  2605. }
  2606. } else {
  2607. compileAsPerConfig = "CompileAsHeaderUnit";
  2608. }
  2609. } else {
  2610. this->Makefile->IssueMessage(
  2611. MessageType::FATAL_ERROR,
  2612. cmStrCat(
  2613. "Target \"", this->GeneratorTarget->Target->GetName(),
  2614. "\" contains the source\n ", source->GetFullPath(),
  2615. "\nin a file set of type \"", fs->GetType(),
  2616. R"(" but the source is not classified as a "CXX" source.)"));
  2617. }
  2618. }
  2619. // We have pch state in the following situation:
  2620. // 1. We have SKIP_PRECOMPILE_HEADERS == true
  2621. // 2. We are creating the pre-compiled header
  2622. // 3. We are a different language than the linker language AND pch is
  2623. // enabled.
  2624. std::string const& linkLanguage =
  2625. this->GeneratorTarget->GetLinkerLanguage(config);
  2626. std::string const& pchSource =
  2627. this->GeneratorTarget->GetPchSource(config, lang);
  2628. const bool skipPCH =
  2629. pchSource.empty() || sf.GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS");
  2630. const bool makePCH = (sf.GetFullPath() == pchSource);
  2631. const bool useSharedPCH = !skipPCH && (lang == linkLanguage);
  2632. const bool useDifferentLangPCH = !skipPCH && (lang != linkLanguage);
  2633. const bool useNoPCH = skipPCH && (lang != linkLanguage) &&
  2634. !this->GeneratorTarget->GetPchHeader(config, linkLanguage).empty();
  2635. const bool needsPCHFlags =
  2636. (makePCH || useSharedPCH || useDifferentLangPCH || useNoPCH);
  2637. // if we have flags or defines for this config then
  2638. // use them
  2639. if (!flags.empty() || !options.empty() || !configDefines.empty() ||
  2640. !includes.empty() || compileAsPerConfig || noWinRT ||
  2641. !options.empty() || needsPCHFlags || shouldScanForModules) {
  2642. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2643. cmIDEFlagTable const* flagtable = nullptr;
  2644. const std::string& srclang = source->GetLanguage();
  2645. if (srclang == "C"_s || srclang == "CXX"_s) {
  2646. flagtable = gg->GetClFlagTable();
  2647. } else if (srclang == "ASM_MARMASM"_s &&
  2648. this->GlobalGenerator->IsMarmasmEnabled()) {
  2649. flagtable = gg->GetMarmasmFlagTable();
  2650. } else if (srclang == "ASM_MASM"_s &&
  2651. this->GlobalGenerator->IsMasmEnabled()) {
  2652. flagtable = gg->GetMasmFlagTable();
  2653. } else if (lang == "ASM_NASM"_s &&
  2654. this->GlobalGenerator->IsNasmEnabled()) {
  2655. flagtable = gg->GetNasmFlagTable();
  2656. } else if (srclang == "RC"_s) {
  2657. flagtable = gg->GetRcFlagTable();
  2658. } else if (srclang == "CSharp"_s) {
  2659. flagtable = gg->GetCSharpFlagTable();
  2660. }
  2661. cmGeneratorExpressionInterpreter genexInterpreter(
  2662. this->LocalGenerator, config, this->GeneratorTarget, lang);
  2663. cmVS10GeneratorOptions clOptions(
  2664. this->LocalGenerator, cmVisualStudioGeneratorOptions::Compiler,
  2665. flagtable, this);
  2666. if (compileAsPerConfig) {
  2667. clOptions.AddFlag("CompileAs", compileAsPerConfig);
  2668. }
  2669. if (shouldScanForModules) {
  2670. clOptions.AddFlag("ScanSourceForModuleDependencies", "true");
  2671. }
  2672. if (noWinRT) {
  2673. clOptions.AddFlag("CompileAsWinRT", "false");
  2674. }
  2675. if (configDependentFlags) {
  2676. clOptions.Parse(genexInterpreter.Evaluate(flags, "COMPILE_FLAGS"));
  2677. } else {
  2678. clOptions.Parse(flags);
  2679. }
  2680. if (needsPCHFlags) {
  2681. // Add precompile headers compile options.
  2682. if (makePCH) {
  2683. clOptions.AddFlag("PrecompiledHeader", "Create");
  2684. std::string pchHeader =
  2685. this->GeneratorTarget->GetPchHeader(config, lang);
  2686. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2687. std::string pchFile =
  2688. this->GeneratorTarget->GetPchFile(config, lang);
  2689. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  2690. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2691. } else if (useNoPCH) {
  2692. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  2693. } else if (useSharedPCH) {
  2694. std::string pchHeader =
  2695. this->GeneratorTarget->GetPchHeader(config, lang);
  2696. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2697. } else if (useDifferentLangPCH) {
  2698. clOptions.AddFlag("PrecompiledHeader", "Use");
  2699. std::string pchHeader =
  2700. this->GeneratorTarget->GetPchHeader(config, lang);
  2701. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  2702. std::string pchFile =
  2703. this->GeneratorTarget->GetPchFile(config, lang);
  2704. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  2705. clOptions.AddFlag("ForcedIncludeFiles", pchHeader);
  2706. }
  2707. }
  2708. if (!options.empty()) {
  2709. std::string expandedOptions;
  2710. if (configDependentOptions) {
  2711. this->LocalGenerator->AppendCompileOptions(
  2712. expandedOptions,
  2713. genexInterpreter.Evaluate(options, "COMPILE_OPTIONS"));
  2714. } else {
  2715. this->LocalGenerator->AppendCompileOptions(expandedOptions, options);
  2716. }
  2717. clOptions.Parse(expandedOptions);
  2718. }
  2719. if (clOptions.HasFlag("DisableSpecificWarnings")) {
  2720. clOptions.AppendFlag("DisableSpecificWarnings",
  2721. "%(DisableSpecificWarnings)");
  2722. }
  2723. if (clOptions.HasFlag("ForcedIncludeFiles")) {
  2724. clOptions.AppendFlag("ForcedIncludeFiles", "%(ForcedIncludeFiles)");
  2725. }
  2726. if (configDependentDefines) {
  2727. clOptions.AddDefines(
  2728. genexInterpreter.Evaluate(configDefines, "COMPILE_DEFINITIONS"));
  2729. } else {
  2730. clOptions.AddDefines(configDefines);
  2731. }
  2732. std::vector<std::string> includeList;
  2733. if (configDependentIncludes) {
  2734. this->LocalGenerator->AppendIncludeDirectories(
  2735. includeList,
  2736. genexInterpreter.Evaluate(includes, "INCLUDE_DIRECTORIES"), *source);
  2737. } else {
  2738. this->LocalGenerator->AppendIncludeDirectories(includeList, includes,
  2739. *source);
  2740. }
  2741. clOptions.AddIncludes(includeList);
  2742. clOptions.SetConfiguration(config);
  2743. OptionsHelper oh(clOptions, e2);
  2744. oh.PrependInheritedString("AdditionalOptions");
  2745. oh.OutputAdditionalIncludeDirectories(lang);
  2746. oh.OutputFlagMap();
  2747. oh.OutputPreprocessorDefinitions(lang);
  2748. }
  2749. }
  2750. if (isCppModule && !objectName.empty()) {
  2751. std::string baseName = cmStrCat("$(IntDir)/", objectName);
  2752. cmStripSuffixIfExists(baseName, ".obj");
  2753. e2.Element("ModuleOutputFile", cmStrCat(baseName, ".ifc"));
  2754. e2.Element("ModuleDependenciesFile", cmStrCat(baseName, ".module.json"));
  2755. }
  2756. if (this->IsXamlSource(source->GetFullPath())) {
  2757. const std::string& fileName = source->GetFullPath();
  2758. e2.Element("DependentUpon",
  2759. fileName.substr(0, fileName.find_last_of('.')));
  2760. }
  2761. if (this->ProjectType == VsProjectType::csproj) {
  2762. using CsPropMap = std::map<std::string, std::string>;
  2763. CsPropMap sourceFileTags;
  2764. this->GetCSharpSourceProperties(&sf, sourceFileTags);
  2765. // write source file specific tags
  2766. if (!sourceFileTags.empty()) {
  2767. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2768. }
  2769. }
  2770. }
  2771. void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild(
  2772. Elem& e2, std::vector<size_t> const& exclude_configs)
  2773. {
  2774. for (size_t ci : exclude_configs) {
  2775. e2.WritePlatformConfigTag("ExcludedFromBuild",
  2776. cmStrCat("'$(Configuration)|$(Platform)'=='",
  2777. this->Configurations[ci], '|',
  2778. this->Platform, '\''),
  2779. "true");
  2780. }
  2781. }
  2782. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions(
  2783. Elem& e0)
  2784. {
  2785. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2786. if (ttype > cmStateEnums::INTERFACE_LIBRARY) {
  2787. return;
  2788. }
  2789. if (this->ProjectType == VsProjectType::csproj) {
  2790. return;
  2791. }
  2792. Elem e1(e0, "PropertyGroup");
  2793. e1.Element("_ProjectFileVersion", "10.0.20506.1");
  2794. for (std::string const& config : this->Configurations) {
  2795. const std::string cond = this->CalcCondition(config);
  2796. if (ttype >= cmStateEnums::UTILITY) {
  2797. e1.WritePlatformConfigTag(
  2798. "IntDir", cond, R"($(Platform)\$(Configuration)\$(ProjectName)\)");
  2799. } else {
  2800. if (ttype == cmStateEnums::SHARED_LIBRARY ||
  2801. ttype == cmStateEnums::MODULE_LIBRARY ||
  2802. ttype == cmStateEnums::EXECUTABLE) {
  2803. auto linker = this->GeneratorTarget->GetLinkerTool(config);
  2804. if (!linker.empty()) {
  2805. ConvertToWindowsSlash(linker);
  2806. e1.WritePlatformConfigTag("LinkToolExe", cond, linker);
  2807. }
  2808. }
  2809. std::string intermediateDir = cmStrCat(
  2810. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  2811. config, '/');
  2812. std::string outDir;
  2813. std::string targetNameFull;
  2814. if (ttype == cmStateEnums::OBJECT_LIBRARY) {
  2815. outDir = intermediateDir;
  2816. targetNameFull = cmStrCat(this->GeneratorTarget->GetName(), ".lib");
  2817. } else {
  2818. outDir = cmStrCat(this->GeneratorTarget->GetDirectory(config), '/');
  2819. targetNameFull = this->GeneratorTarget->GetFullName(config);
  2820. }
  2821. ConvertToWindowsSlash(intermediateDir);
  2822. ConvertToWindowsSlash(outDir);
  2823. e1.WritePlatformConfigTag("OutDir", cond, outDir);
  2824. e1.WritePlatformConfigTag("IntDir", cond, intermediateDir);
  2825. if (cmValue sdkExecutableDirectories = this->Makefile->GetDefinition(
  2826. "CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES")) {
  2827. e1.WritePlatformConfigTag("ExecutablePath", cond,
  2828. *sdkExecutableDirectories);
  2829. }
  2830. if (cmValue sdkIncludeDirectories = this->Makefile->GetDefinition(
  2831. "CMAKE_VS_SDK_INCLUDE_DIRECTORIES")) {
  2832. e1.WritePlatformConfigTag("IncludePath", cond, *sdkIncludeDirectories);
  2833. }
  2834. if (cmValue sdkReferenceDirectories = this->Makefile->GetDefinition(
  2835. "CMAKE_VS_SDK_REFERENCE_DIRECTORIES")) {
  2836. e1.WritePlatformConfigTag("ReferencePath", cond,
  2837. *sdkReferenceDirectories);
  2838. }
  2839. if (cmValue sdkLibraryDirectories = this->Makefile->GetDefinition(
  2840. "CMAKE_VS_SDK_LIBRARY_DIRECTORIES")) {
  2841. e1.WritePlatformConfigTag("LibraryPath", cond, *sdkLibraryDirectories);
  2842. }
  2843. if (cmValue sdkLibraryWDirectories = this->Makefile->GetDefinition(
  2844. "CMAKE_VS_SDK_LIBRARY_WINRT_DIRECTORIES")) {
  2845. e1.WritePlatformConfigTag("LibraryWPath", cond,
  2846. *sdkLibraryWDirectories);
  2847. }
  2848. if (cmValue sdkSourceDirectories =
  2849. this->Makefile->GetDefinition("CMAKE_VS_SDK_SOURCE_DIRECTORIES")) {
  2850. e1.WritePlatformConfigTag("SourcePath", cond, *sdkSourceDirectories);
  2851. }
  2852. if (cmValue sdkExcludeDirectories = this->Makefile->GetDefinition(
  2853. "CMAKE_VS_SDK_EXCLUDE_DIRECTORIES")) {
  2854. e1.WritePlatformConfigTag("ExcludePath", cond, *sdkExcludeDirectories);
  2855. }
  2856. std::string name =
  2857. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  2858. e1.WritePlatformConfigTag("TargetName", cond, name);
  2859. std::string ext =
  2860. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  2861. if (ext.empty()) {
  2862. // An empty TargetExt causes a default extension to be used.
  2863. // A single "." appears to be treated as an empty extension.
  2864. ext = ".";
  2865. }
  2866. e1.WritePlatformConfigTag("TargetExt", cond, ext);
  2867. this->OutputLinkIncremental(e1, config);
  2868. }
  2869. if (ttype <= cmStateEnums::UTILITY) {
  2870. if (cmValue workingDir = this->GeneratorTarget->GetProperty(
  2871. "VS_DEBUGGER_WORKING_DIRECTORY")) {
  2872. std::string genWorkingDir = cmGeneratorExpression::Evaluate(
  2873. *workingDir, this->LocalGenerator, config);
  2874. e1.WritePlatformConfigTag("LocalDebuggerWorkingDirectory", cond,
  2875. genWorkingDir);
  2876. }
  2877. if (cmValue environment =
  2878. this->GeneratorTarget->GetProperty("VS_DEBUGGER_ENVIRONMENT")) {
  2879. std::string genEnvironment = cmGeneratorExpression::Evaluate(
  2880. *environment, this->LocalGenerator, config);
  2881. e1.WritePlatformConfigTag("LocalDebuggerEnvironment", cond,
  2882. genEnvironment);
  2883. }
  2884. if (cmValue debuggerCommand =
  2885. this->GeneratorTarget->GetProperty("VS_DEBUGGER_COMMAND")) {
  2886. std::string genDebuggerCommand = cmGeneratorExpression::Evaluate(
  2887. *debuggerCommand, this->LocalGenerator, config);
  2888. e1.WritePlatformConfigTag("LocalDebuggerCommand", cond,
  2889. genDebuggerCommand);
  2890. }
  2891. if (cmValue commandArguments = this->GeneratorTarget->GetProperty(
  2892. "VS_DEBUGGER_COMMAND_ARGUMENTS")) {
  2893. std::string genCommandArguments = cmGeneratorExpression::Evaluate(
  2894. *commandArguments, this->LocalGenerator, config);
  2895. e1.WritePlatformConfigTag("LocalDebuggerCommandArguments", cond,
  2896. genCommandArguments);
  2897. }
  2898. }
  2899. }
  2900. }
  2901. void cmVisualStudio10TargetGenerator::WritePublicProjectContentOptions(
  2902. Elem& e0)
  2903. {
  2904. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2905. if (ttype != cmStateEnums::SHARED_LIBRARY) {
  2906. return;
  2907. }
  2908. if (this->ProjectType != VsProjectType::vcxproj) {
  2909. return;
  2910. }
  2911. Elem e1(e0, "PropertyGroup");
  2912. for (std::string const& config : this->Configurations) {
  2913. if (this->GeneratorTarget->HaveCxx20ModuleSources() &&
  2914. this->GeneratorTarget->HaveCxxModuleSupport(config) ==
  2915. cmGeneratorTarget::Cxx20SupportLevel::Supported) {
  2916. const std::string cond = this->CalcCondition(config);
  2917. // For DLL projects, we export all BMIs for now
  2918. e1.WritePlatformConfigTag("AllProjectBMIsArePublic", cond, "true");
  2919. }
  2920. }
  2921. }
  2922. void cmVisualStudio10TargetGenerator::OutputLinkIncremental(
  2923. Elem& e1, std::string const& configName)
  2924. {
  2925. if (!this->MSTools) {
  2926. return;
  2927. }
  2928. if (this->ProjectType == VsProjectType::csproj) {
  2929. return;
  2930. }
  2931. // static libraries and things greater than modules do not need
  2932. // to set this option
  2933. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2934. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  2935. return;
  2936. }
  2937. Options& linkOptions = *(this->LinkOptions[configName]);
  2938. const std::string cond = this->CalcCondition(configName);
  2939. if (this->IPOEnabledConfigurations.count(configName) > 0) {
  2940. // Suppress LinkIncremental in favor of WholeProgramOptimization.
  2941. e1.WritePlatformConfigTag("LinkIncremental", cond, "");
  2942. } else {
  2943. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  2944. e1.WritePlatformConfigTag("LinkIncremental", cond,
  2945. (incremental ? incremental : "true"));
  2946. }
  2947. linkOptions.RemoveFlag("LinkIncremental");
  2948. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  2949. e1.WritePlatformConfigTag("GenerateManifest", cond,
  2950. (manifest ? manifest : "true"));
  2951. linkOptions.RemoveFlag("GenerateManifest");
  2952. // Some link options belong here. Use them now and remove them so that
  2953. // WriteLinkOptions does not use them.
  2954. static const std::vector<std::string> flags{ "LinkDelaySign",
  2955. "LinkKeyFile" };
  2956. for (const std::string& flag : flags) {
  2957. if (const char* value = linkOptions.GetFlag(flag)) {
  2958. e1.WritePlatformConfigTag(flag, cond, value);
  2959. linkOptions.RemoveFlag(flag);
  2960. }
  2961. }
  2962. }
  2963. std::vector<std::string> cmVisualStudio10TargetGenerator::GetIncludes(
  2964. std::string const& config, std::string const& lang) const
  2965. {
  2966. std::vector<std::string> includes;
  2967. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  2968. lang, config);
  2969. for (std::string& i : includes) {
  2970. ConvertToWindowsSlash(i);
  2971. }
  2972. return includes;
  2973. }
  2974. std::string cmVisualStudio10TargetGenerator::GetTargetOutputName() const
  2975. {
  2976. std::string config;
  2977. if (!this->Configurations.empty()) {
  2978. config = this->Configurations[0];
  2979. }
  2980. const auto& nameComponents =
  2981. this->GeneratorTarget->GetFullNameComponents(config);
  2982. return cmStrCat(nameComponents.prefix, nameComponents.base);
  2983. }
  2984. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  2985. {
  2986. return std::all_of(
  2987. this->Configurations.begin(), this->Configurations.end(),
  2988. [this](std::string const& c) { return this->ComputeClOptions(c); });
  2989. }
  2990. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  2991. std::string const& configName)
  2992. {
  2993. // much of this was copied from here:
  2994. // copied from cmLocalVisualStudio7Generator.cxx 805
  2995. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  2996. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2997. std::unique_ptr<Options> pOptions;
  2998. switch (this->ProjectType) {
  2999. case VsProjectType::vcxproj:
  3000. pOptions = cm::make_unique<Options>(
  3001. this->LocalGenerator, Options::Compiler, gg->GetClFlagTable());
  3002. break;
  3003. case VsProjectType::csproj:
  3004. pOptions =
  3005. cm::make_unique<Options>(this->LocalGenerator, Options::CSharpCompiler,
  3006. gg->GetCSharpFlagTable());
  3007. break;
  3008. default:
  3009. break;
  3010. }
  3011. Options& clOptions = *pOptions;
  3012. std::string flags;
  3013. const std::string& linkLanguage =
  3014. this->GeneratorTarget->GetLinkerLanguage(configName);
  3015. if (linkLanguage.empty()) {
  3016. cmSystemTools::Error(cmStrCat(
  3017. "CMake can not determine linker language for target: ", this->Name));
  3018. return false;
  3019. }
  3020. // Choose a language whose flags to use for ClCompile.
  3021. static const char* clLangs[] = { "CXX", "C", "Fortran" };
  3022. std::string langForClCompile;
  3023. if (this->ProjectType == VsProjectType::csproj) {
  3024. langForClCompile = "CSharp";
  3025. } else if (cm::contains(clLangs, linkLanguage)) {
  3026. langForClCompile = linkLanguage;
  3027. } else {
  3028. std::set<std::string> languages;
  3029. this->GeneratorTarget->GetLanguages(languages, configName);
  3030. for (const char* l : clLangs) {
  3031. if (languages.count(l)) {
  3032. langForClCompile = l;
  3033. break;
  3034. }
  3035. }
  3036. }
  3037. this->LangForClCompile = langForClCompile;
  3038. if (!langForClCompile.empty()) {
  3039. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3040. cmBuildStep::Compile,
  3041. langForClCompile, configName);
  3042. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3043. langForClCompile, configName);
  3044. }
  3045. // Put the IPO enabled configurations into a set.
  3046. if (this->GeneratorTarget->IsIPOEnabled(linkLanguage, configName)) {
  3047. this->IPOEnabledConfigurations.insert(configName);
  3048. }
  3049. // Check if ASan is enabled.
  3050. if (flags.find("/fsanitize=address") != std::string::npos ||
  3051. flags.find("-fsanitize=address") != std::string::npos) {
  3052. this->ASanEnabledConfigurations.insert(configName);
  3053. }
  3054. // Check if (lib)Fuzzer is enabled.
  3055. if (flags.find("/fsanitize=fuzzer") != std::string::npos ||
  3056. flags.find("-fsanitize=fuzzer") != std::string::npos) {
  3057. this->FuzzerEnabledConfigurations.insert(configName);
  3058. }
  3059. // Precompile Headers
  3060. std::string pchHeader =
  3061. this->GeneratorTarget->GetPchHeader(configName, linkLanguage);
  3062. if (this->MSTools && VsProjectType::vcxproj == this->ProjectType &&
  3063. pchHeader.empty()) {
  3064. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  3065. } else if (this->MSTools && VsProjectType::vcxproj == this->ProjectType &&
  3066. !pchHeader.empty()) {
  3067. clOptions.AddFlag("PrecompiledHeader", "Use");
  3068. clOptions.AddFlag("PrecompiledHeaderFile", pchHeader);
  3069. std::string pchFile =
  3070. this->GeneratorTarget->GetPchFile(configName, linkLanguage);
  3071. clOptions.AddFlag("PrecompiledHeaderOutputFile", pchFile);
  3072. }
  3073. // Get preprocessor definitions for this directory.
  3074. std::string defineFlags = this->Makefile->GetDefineFlags();
  3075. if (this->MSTools) {
  3076. if (this->ProjectType == VsProjectType::vcxproj) {
  3077. clOptions.FixExceptionHandlingDefault();
  3078. if (this->GlobalGenerator->GetVersion() >=
  3079. cmGlobalVisualStudioGenerator::VSVersion::VS15) {
  3080. // Toolsets that come with VS 2017 may now enable UseFullPaths
  3081. // by default and there is no negative /FC option that projects
  3082. // can use to switch it back. Older toolsets disable this by
  3083. // default anyway so this will not hurt them. If the project
  3084. // is using an explicit /FC option then parsing flags will
  3085. // replace this setting with "true" below.
  3086. clOptions.AddFlag("UseFullPaths", "false");
  3087. }
  3088. clOptions.AddFlag("AssemblerListingLocation", "$(IntDir)");
  3089. }
  3090. }
  3091. // check for managed C++ assembly compiler flag. This overrides any
  3092. // /clr* compiler flags which may be defined in the flags variable(s).
  3093. if (this->ProjectType != VsProjectType::csproj) {
  3094. // Warn if /clr was added manually. This should not be done
  3095. // anymore, because cmGeneratorTarget may not be aware that the
  3096. // target uses C++/CLI.
  3097. if (flags.find("/clr") != std::string::npos ||
  3098. flags.find("-clr") != std::string::npos ||
  3099. defineFlags.find("/clr") != std::string::npos ||
  3100. defineFlags.find("-clr") != std::string::npos) {
  3101. if (configName == this->Configurations[0]) {
  3102. std::string message =
  3103. cmStrCat("For the target \"", this->GeneratorTarget->GetName(),
  3104. "\" the /clr compiler flag was added manually. ",
  3105. "Set usage of C++/CLI by setting COMMON_LANGUAGE_RUNTIME "
  3106. "target property.");
  3107. this->Makefile->IssueMessage(MessageType::WARNING, message);
  3108. }
  3109. }
  3110. if (cmValue clr =
  3111. this->GeneratorTarget->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  3112. std::string clrString = *clr;
  3113. if (!clrString.empty()) {
  3114. clrString = cmStrCat(':', clrString);
  3115. }
  3116. flags += cmStrCat(" /clr", clrString);
  3117. }
  3118. }
  3119. // Get includes for this target
  3120. if (!this->LangForClCompile.empty()) {
  3121. auto includeList = this->GetIncludes(configName, this->LangForClCompile);
  3122. auto sysIncludeFlag = this->Makefile->GetDefinition(
  3123. cmStrCat("CMAKE_INCLUDE_SYSTEM_FLAG_", this->LangForClCompile));
  3124. if (sysIncludeFlag) {
  3125. bool gotOneSys = false;
  3126. for (auto i : includeList) {
  3127. cmSystemTools::ConvertToUnixSlashes(i);
  3128. if (this->GeneratorTarget->IsSystemIncludeDirectory(
  3129. i, configName, this->LangForClCompile)) {
  3130. auto flag = cmTrimWhitespace(*sysIncludeFlag);
  3131. if (this->MSTools) {
  3132. cmSystemTools::ReplaceString(flag, "-external:I", "/external:I");
  3133. }
  3134. clOptions.AppendFlagString("AdditionalOptions",
  3135. cmStrCat(flag, " \"", i, '"'));
  3136. gotOneSys = true;
  3137. } else {
  3138. clOptions.AddInclude(i);
  3139. }
  3140. }
  3141. if (gotOneSys) {
  3142. if (auto sysIncludeFlagWarning = this->Makefile->GetDefinition(
  3143. cmStrCat("_CMAKE_INCLUDE_SYSTEM_FLAG_", this->LangForClCompile,
  3144. "_WARNING"))) {
  3145. flags = cmStrCat(flags, ' ', *sysIncludeFlagWarning);
  3146. }
  3147. }
  3148. } else {
  3149. clOptions.AddIncludes(includeList);
  3150. }
  3151. }
  3152. clOptions.Parse(flags);
  3153. clOptions.Parse(defineFlags);
  3154. std::vector<std::string> targetDefines;
  3155. switch (this->ProjectType) {
  3156. case VsProjectType::vcxproj:
  3157. if (!langForClCompile.empty()) {
  3158. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3159. langForClCompile);
  3160. }
  3161. break;
  3162. case VsProjectType::csproj:
  3163. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3164. "CSharp");
  3165. cm::erase_if(targetDefines, [](std::string const& def) {
  3166. return def.find('=') != std::string::npos;
  3167. });
  3168. break;
  3169. default:
  3170. break;
  3171. }
  3172. clOptions.AddDefines(targetDefines);
  3173. if (this->ProjectType == VsProjectType::csproj) {
  3174. clOptions.AppendFlag("DefineConstants", targetDefines);
  3175. }
  3176. if (this->MSTools) {
  3177. clOptions.SetVerboseMakefile(
  3178. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  3179. }
  3180. // Add C-specific flags expressible in a ClCompile meant for C++.
  3181. if (langForClCompile == "CXX"_s) {
  3182. std::set<std::string> languages;
  3183. this->GeneratorTarget->GetLanguages(languages, configName);
  3184. if (languages.count("C")) {
  3185. std::string flagsC;
  3186. this->LocalGenerator->AddLanguageFlags(
  3187. flagsC, this->GeneratorTarget, cmBuildStep::Compile, "C", configName);
  3188. this->LocalGenerator->AddCompileOptions(flagsC, this->GeneratorTarget,
  3189. "C", configName);
  3190. Options optC(this->LocalGenerator, Options::Compiler,
  3191. gg->GetClFlagTable());
  3192. optC.Parse(flagsC);
  3193. if (const char* stdC = optC.GetFlag("LanguageStandard_C")) {
  3194. clOptions.AddFlag("LanguageStandard_C", stdC);
  3195. }
  3196. }
  3197. }
  3198. // Add a definition for the configuration name.
  3199. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  3200. clOptions.AddDefine(configDefine);
  3201. if (const std::string* exportMacro =
  3202. this->GeneratorTarget->GetExportMacro()) {
  3203. clOptions.AddDefine(*exportMacro);
  3204. }
  3205. if (this->MSTools) {
  3206. // If we have the VS_WINRT_COMPONENT set then force Compile as WinRT
  3207. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT")) {
  3208. clOptions.AddFlag("CompileAsWinRT", "true");
  3209. // For WinRT components, add the _WINRT_DLL define to produce a lib
  3210. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3211. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3212. clOptions.AddDefine("_WINRT_DLL");
  3213. }
  3214. } else if (this->GlobalGenerator->TargetsWindowsStore() ||
  3215. this->GlobalGenerator->TargetsWindowsPhone() ||
  3216. this->Makefile->IsOn("CMAKE_VS_WINRT_BY_DEFAULT")) {
  3217. if (!clOptions.IsWinRt()) {
  3218. clOptions.AddFlag("CompileAsWinRT", "false");
  3219. }
  3220. }
  3221. if (const char* winRT = clOptions.GetFlag("CompileAsWinRT")) {
  3222. if (cmIsOn(winRT)) {
  3223. this->TargetCompileAsWinRT = true;
  3224. }
  3225. }
  3226. }
  3227. if (this->ProjectType != VsProjectType::csproj &&
  3228. (clOptions.IsManaged() || clOptions.HasFlag("CLRSupport"))) {
  3229. this->Managed = true;
  3230. std::string managedType = clOptions.HasFlag("CompileAsManaged")
  3231. ? clOptions.GetFlag("CompileAsManaged")
  3232. : "Mixed";
  3233. if (managedType == "Safe"_s || managedType == "Pure"_s) {
  3234. // force empty calling convention if safe clr is used
  3235. clOptions.AddFlag("CallingConvention", "");
  3236. }
  3237. // The default values of these flags are incompatible to
  3238. // managed assemblies. We have to force valid values if
  3239. // the target is a managed C++ target.
  3240. clOptions.AddFlag("ExceptionHandling", "Async");
  3241. clOptions.AddFlag("BasicRuntimeChecks", "Default");
  3242. }
  3243. if (this->ProjectType == VsProjectType::csproj) {
  3244. // /nowin32manifest overrides /win32manifest: parameter
  3245. if (clOptions.HasFlag("NoWin32Manifest")) {
  3246. clOptions.RemoveFlag("ApplicationManifest");
  3247. }
  3248. }
  3249. if (const char* s = clOptions.GetFlag("SpectreMitigation")) {
  3250. this->SpectreMitigation[configName] = s;
  3251. clOptions.RemoveFlag("SpectreMitigation");
  3252. }
  3253. // Remove any target-wide -TC or -TP flag added by the project.
  3254. // Such flags are unnecessary and break our model of language selection.
  3255. if (langForClCompile == "C"_s || langForClCompile == "CXX"_s) {
  3256. clOptions.RemoveFlag("CompileAs");
  3257. }
  3258. if (this->ProjectType == VsProjectType::vcxproj && this->MSTools) {
  3259. // Suppress Microsoft.Cl.Common.props default settings for which the
  3260. // project specifies no flags. Do not let UseDebugLibraries affect them.
  3261. if (!clOptions.HasFlag("BasicRuntimeChecks")) {
  3262. clOptions.AddFlag("BasicRuntimeChecks", "Default");
  3263. }
  3264. if (!clOptions.HasFlag("MinimalRebuild")) {
  3265. clOptions.AddFlag("MinimalRebuild", "");
  3266. }
  3267. if (!clOptions.HasFlag("Optimization")) {
  3268. clOptions.AddFlag("Optimization", "");
  3269. }
  3270. if (!clOptions.HasFlag("RuntimeLibrary")) {
  3271. clOptions.AddFlag("RuntimeLibrary", "");
  3272. }
  3273. if (!clOptions.HasFlag("SupportJustMyCode")) {
  3274. clOptions.AddFlag("SupportJustMyCode", "");
  3275. }
  3276. }
  3277. this->ClOptions[configName] = std::move(pOptions);
  3278. return true;
  3279. }
  3280. void cmVisualStudio10TargetGenerator::WriteClOptions(
  3281. Elem& e1, std::string const& configName)
  3282. {
  3283. Options& clOptions = *(this->ClOptions[configName]);
  3284. if (this->ProjectType == VsProjectType::csproj) {
  3285. return;
  3286. }
  3287. Elem e2(e1, "ClCompile");
  3288. OptionsHelper oh(clOptions, e2);
  3289. oh.PrependInheritedString("AdditionalOptions");
  3290. oh.OutputAdditionalIncludeDirectories(this->LangForClCompile);
  3291. oh.OutputFlagMap();
  3292. oh.OutputPreprocessorDefinitions(this->LangForClCompile);
  3293. if (this->NsightTegra) {
  3294. if (cmValue processMax =
  3295. this->GeneratorTarget->GetProperty("ANDROID_PROCESS_MAX")) {
  3296. e2.Element("ProcessMax", *processMax);
  3297. }
  3298. }
  3299. if (this->Android) {
  3300. e2.Element("ObjectFileName", "$(IntDir)%(filename).o");
  3301. } else if (this->MSTools) {
  3302. cmsys::RegularExpression clangToolset("v[0-9]+_clang_.*");
  3303. const char* toolset = this->GlobalGenerator->GetPlatformToolset();
  3304. cmValue noCompileBatching =
  3305. this->GeneratorTarget->GetProperty("VS_NO_COMPILE_BATCHING");
  3306. if (noCompileBatching.IsOn() || (toolset && clangToolset.find(toolset))) {
  3307. e2.Element("ObjectFileName", "$(IntDir)%(filename).obj");
  3308. } else {
  3309. e2.Element("ObjectFileName", "$(IntDir)");
  3310. }
  3311. // If not in debug mode, write the DebugInformationFormat field
  3312. // without value so PDBs don't get generated uselessly. Each tag
  3313. // goes on its own line because Visual Studio corrects it this
  3314. // way when saving the project after CMake generates it.
  3315. if (!clOptions.UsingDebugInfo()) {
  3316. Elem e3(e2, "DebugInformationFormat");
  3317. e3.SetHasElements();
  3318. }
  3319. // Specify the compiler program database file if configured.
  3320. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  3321. if (!pdb.empty()) {
  3322. if (this->GlobalGenerator->IsCudaEnabled()) {
  3323. // CUDA does not quote paths with spaces correctly when forwarding
  3324. // this to the host compiler. Use a relative path to avoid spaces.
  3325. // FIXME: We can likely do this even when CUDA is not involved,
  3326. // but for now we will make a minimal change.
  3327. pdb = this->ConvertPath(pdb, true);
  3328. }
  3329. ConvertToWindowsSlash(pdb);
  3330. e2.Element("ProgramDataBaseFileName", pdb);
  3331. }
  3332. // add AdditionalUsingDirectories
  3333. if (this->AdditionalUsingDirectories.count(configName) > 0) {
  3334. std::string dirs;
  3335. for (auto const& u : this->AdditionalUsingDirectories[configName]) {
  3336. if (!dirs.empty()) {
  3337. dirs.append(";");
  3338. }
  3339. dirs.append(u);
  3340. }
  3341. e2.Element("AdditionalUsingDirectories", dirs);
  3342. }
  3343. }
  3344. // Disable C++ source scanning by default.
  3345. e2.Element("ScanSourceForModuleDependencies", "false");
  3346. }
  3347. bool cmVisualStudio10TargetGenerator::ComputeRcOptions()
  3348. {
  3349. return std::all_of(
  3350. this->Configurations.begin(), this->Configurations.end(),
  3351. [this](std::string const& c) { return this->ComputeRcOptions(c); });
  3352. }
  3353. bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
  3354. std::string const& configName)
  3355. {
  3356. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3357. auto pOptions = cm::make_unique<Options>(
  3358. this->LocalGenerator, Options::ResourceCompiler, gg->GetRcFlagTable());
  3359. Options& rcOptions = *pOptions;
  3360. std::string CONFIG = cmSystemTools::UpperCase(configName);
  3361. std::string rcConfigFlagsVar = cmStrCat("CMAKE_RC_FLAGS_", CONFIG);
  3362. std::string flags =
  3363. cmStrCat(this->Makefile->GetSafeDefinition("CMAKE_RC_FLAGS"), ' ',
  3364. this->Makefile->GetSafeDefinition(rcConfigFlagsVar));
  3365. rcOptions.Parse(flags);
  3366. // For historical reasons, add the C preprocessor defines to RC.
  3367. Options& clOptions = *(this->ClOptions[configName]);
  3368. rcOptions.AddDefines(clOptions.GetDefines());
  3369. // Get includes for this target
  3370. rcOptions.AddIncludes(this->GetIncludes(configName, "RC"));
  3371. this->RcOptions[configName] = std::move(pOptions);
  3372. return true;
  3373. }
  3374. void cmVisualStudio10TargetGenerator::WriteRCOptions(
  3375. Elem& e1, std::string const& configName)
  3376. {
  3377. if (!this->MSTools) {
  3378. return;
  3379. }
  3380. Elem e2(e1, "ResourceCompile");
  3381. OptionsHelper rcOptions(*(this->RcOptions[configName]), e2);
  3382. rcOptions.OutputPreprocessorDefinitions("RC");
  3383. rcOptions.OutputAdditionalIncludeDirectories("RC");
  3384. rcOptions.PrependInheritedString("AdditionalOptions");
  3385. rcOptions.OutputFlagMap();
  3386. }
  3387. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions()
  3388. {
  3389. if (!this->GlobalGenerator->IsCudaEnabled()) {
  3390. return true;
  3391. }
  3392. return std::all_of(this->Configurations.begin(), this->Configurations.end(),
  3393. [this](std::string const& c) {
  3394. return !this->GeneratorTarget->IsLanguageUsed("CUDA",
  3395. c) ||
  3396. this->ComputeCudaOptions(c);
  3397. });
  3398. }
  3399. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
  3400. std::string const& configName)
  3401. {
  3402. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3403. auto pOptions = cm::make_unique<Options>(
  3404. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  3405. Options& cudaOptions = *pOptions;
  3406. auto cudaVersion = this->GlobalGenerator->GetPlatformToolsetCudaString();
  3407. // Get compile flags for CUDA in this directory.
  3408. std::string flags;
  3409. this->LocalGenerator->AddLanguageFlags(
  3410. flags, this->GeneratorTarget, cmBuildStep::Compile, "CUDA", configName);
  3411. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, "CUDA",
  3412. configName);
  3413. // Get preprocessor definitions for this directory.
  3414. std::string defineFlags = this->Makefile->GetDefineFlags();
  3415. cudaOptions.Parse(flags);
  3416. cudaOptions.Parse(defineFlags);
  3417. cudaOptions.ParseFinish();
  3418. // If we haven't explicitly enabled GPU debug information
  3419. // explicitly disable it
  3420. if (!cudaOptions.HasFlag("GPUDebugInfo")) {
  3421. cudaOptions.AddFlag("GPUDebugInfo", "false");
  3422. }
  3423. // The extension on object libraries the CUDA gives isn't
  3424. // consistent with how MSVC generates object libraries for C+, so set
  3425. // the default to not have any extension
  3426. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).obj");
  3427. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  3428. cudaOptions.AddFlag("GenerateRelocatableDeviceCode", "true");
  3429. }
  3430. bool notPtxLike = true;
  3431. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) {
  3432. cudaOptions.AddFlag("NvccCompilation", "ptx");
  3433. // We drop the %(Extension) component as CMake expects all PTX files
  3434. // to not have the source file extension at all
  3435. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).ptx");
  3436. notPtxLike = false;
  3437. if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL,
  3438. cudaVersion, "9.0") &&
  3439. cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, cudaVersion,
  3440. "11.5")) {
  3441. // The DriverApi flag before 11.5 ( verified back to 9.0 ) which controls
  3442. // PTX compilation doesn't propagate user defines causing
  3443. // target_compile_definitions to behave differently for VS +
  3444. // PTX compared to other generators so we patch the rules
  3445. // to normalize behavior
  3446. cudaOptions.AddFlag("DriverApiCommandLineTemplate",
  3447. "%(BaseCommandLineTemplate) [CompileOut] [FastMath] "
  3448. "[Defines] \"%(FullPath)\"");
  3449. }
  3450. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3451. "CUDA_CUBIN_COMPILATION")) {
  3452. cudaOptions.AddFlag("NvccCompilation", "cubin");
  3453. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).cubin");
  3454. notPtxLike = false;
  3455. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3456. "CUDA_FATBIN_COMPILATION")) {
  3457. cudaOptions.AddFlag("NvccCompilation", "fatbin");
  3458. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).fatbin");
  3459. notPtxLike = false;
  3460. } else if (this->GeneratorTarget->GetPropertyAsBool(
  3461. "CUDA_OPTIX_COMPILATION")) {
  3462. cudaOptions.AddFlag("NvccCompilation", "optix-ir");
  3463. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).optixir");
  3464. notPtxLike = false;
  3465. }
  3466. if (notPtxLike &&
  3467. cmSystemTools::VersionCompareGreaterEq(
  3468. "8.0", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  3469. // Explicitly state that we want this file to be treated as a
  3470. // CUDA file no matter what the file extensions is
  3471. // This is only needed for < CUDA 9
  3472. cudaOptions.AppendFlagString("AdditionalOptions", "-x cu");
  3473. }
  3474. // Specify the compiler program database file if configured.
  3475. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  3476. if (!pdb.empty()) {
  3477. // CUDA does not make the directory if it is non-standard.
  3478. std::string const pdbDir = cmSystemTools::GetFilenamePath(pdb);
  3479. cmSystemTools::MakeDirectory(pdbDir);
  3480. if (cmSystemTools::VersionCompareGreaterEq(
  3481. "9.2", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  3482. // CUDA does not have a field for this and does not honor the
  3483. // ProgramDataBaseFileName field in ClCompile. Work around this
  3484. // limitation by creating the directory and passing the flag ourselves.
  3485. pdb = this->ConvertPath(pdb, true);
  3486. ConvertToWindowsSlash(pdb);
  3487. std::string const clFd = cmStrCat(R"(-Xcompiler="-Fd\")", pdb, R"(\"")");
  3488. cudaOptions.AppendFlagString("AdditionalOptions", clFd);
  3489. }
  3490. }
  3491. // CUDA automatically passes the proper '--machine' flag to nvcc
  3492. // for the current architecture, but does not reflect this default
  3493. // in the user-visible IDE settings. Set it explicitly.
  3494. if (this->Platform == "x64"_s) {
  3495. cudaOptions.AddFlag("TargetMachinePlatform", "64");
  3496. }
  3497. // Convert the host compiler options to the toolset's abstractions
  3498. // using a secondary flag table.
  3499. cudaOptions.ClearTables();
  3500. cudaOptions.AddTable(gg->GetCudaHostFlagTable());
  3501. cudaOptions.Reparse("AdditionalCompilerOptions");
  3502. // `CUDA 8.0.targets` places AdditionalCompilerOptions before nvcc!
  3503. // Pass them through -Xcompiler in AdditionalOptions instead.
  3504. if (const char* acoPtr = cudaOptions.GetFlag("AdditionalCompilerOptions")) {
  3505. std::string aco = acoPtr;
  3506. cudaOptions.RemoveFlag("AdditionalCompilerOptions");
  3507. if (!aco.empty()) {
  3508. aco = this->LocalGenerator->EscapeForShell(aco, false);
  3509. cudaOptions.AppendFlagString("AdditionalOptions",
  3510. cmStrCat("-Xcompiler=", aco));
  3511. }
  3512. }
  3513. cudaOptions.FixCudaCodeGeneration();
  3514. std::vector<std::string> targetDefines;
  3515. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  3516. "CUDA");
  3517. cudaOptions.AddDefines(targetDefines);
  3518. // Add a definition for the configuration name.
  3519. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  3520. cudaOptions.AddDefine(configDefine);
  3521. if (const std::string* exportMacro =
  3522. this->GeneratorTarget->GetExportMacro()) {
  3523. cudaOptions.AddDefine(*exportMacro);
  3524. }
  3525. // Get includes for this target
  3526. cudaOptions.AddIncludes(this->GetIncludes(configName, "CUDA"));
  3527. cudaOptions.AddFlag("UseHostInclude", "false");
  3528. // Add runtime library selection flag.
  3529. std::string const& cudaRuntime =
  3530. this->GeneratorTarget->GetRuntimeLinkLibrary("CUDA", configName);
  3531. if (cudaRuntime == "STATIC"_s) {
  3532. cudaOptions.AddFlag("CudaRuntime", "Static");
  3533. } else if (cudaRuntime == "SHARED"_s) {
  3534. cudaOptions.AddFlag("CudaRuntime", "Shared");
  3535. } else if (cudaRuntime == "NONE"_s) {
  3536. cudaOptions.AddFlag("CudaRuntime", "None");
  3537. }
  3538. this->CudaOptions[configName] = std::move(pOptions);
  3539. return true;
  3540. }
  3541. void cmVisualStudio10TargetGenerator::WriteCudaOptions(
  3542. Elem& e1, std::string const& configName)
  3543. {
  3544. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled() ||
  3545. !this->GeneratorTarget->IsLanguageUsed("CUDA", configName)) {
  3546. return;
  3547. }
  3548. Elem e2(e1, "CudaCompile");
  3549. OptionsHelper cudaOptions(*(this->CudaOptions[configName]), e2);
  3550. cudaOptions.OutputAdditionalIncludeDirectories("CUDA");
  3551. cudaOptions.OutputPreprocessorDefinitions("CUDA");
  3552. cudaOptions.PrependInheritedString("AdditionalOptions");
  3553. cudaOptions.OutputFlagMap();
  3554. }
  3555. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions()
  3556. {
  3557. if (!this->GlobalGenerator->IsCudaEnabled()) {
  3558. return true;
  3559. }
  3560. return std::all_of(
  3561. this->Configurations.begin(), this->Configurations.end(),
  3562. [this](std::string const& c) { return this->ComputeCudaLinkOptions(c); });
  3563. }
  3564. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions(
  3565. std::string const& configName)
  3566. {
  3567. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3568. auto pOptions = cm::make_unique<Options>(
  3569. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  3570. Options& cudaLinkOptions = *pOptions;
  3571. cmGeneratorTarget::DeviceLinkSetter setter(*this->GeneratorTarget);
  3572. // Determine if we need to do a device link
  3573. const bool doDeviceLinking = requireDeviceLinking(
  3574. *this->GeneratorTarget, *this->LocalGenerator, configName);
  3575. cudaLinkOptions.AddFlag("PerformDeviceLink",
  3576. doDeviceLinking ? "true" : "false");
  3577. // Add extra flags for device linking
  3578. cudaLinkOptions.AppendFlagString(
  3579. "AdditionalOptions",
  3580. this->Makefile->GetSafeDefinition("_CMAKE_CUDA_EXTRA_FLAGS"));
  3581. cudaLinkOptions.AppendFlagString(
  3582. "AdditionalOptions",
  3583. this->Makefile->GetSafeDefinition("_CMAKE_CUDA_EXTRA_DEVICE_LINK_FLAGS"));
  3584. std::vector<std::string> linkOpts;
  3585. std::string linkFlags;
  3586. this->GeneratorTarget->GetLinkOptions(linkOpts, configName, "CUDA");
  3587. // LINK_OPTIONS are escaped.
  3588. this->LocalGenerator->AppendCompileOptions(linkFlags, linkOpts);
  3589. cmComputeLinkInformation* pcli =
  3590. this->GeneratorTarget->GetLinkInformation(configName);
  3591. if (doDeviceLinking && pcli) {
  3592. cmLinkLineDeviceComputer computer(
  3593. this->LocalGenerator,
  3594. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  3595. std::string ignored_;
  3596. this->LocalGenerator->GetDeviceLinkFlags(computer, configName, ignored_,
  3597. linkFlags, ignored_, ignored_,
  3598. this->GeneratorTarget);
  3599. this->LocalGenerator->AddLanguageFlagsForLinking(
  3600. linkFlags, this->GeneratorTarget, "CUDA", configName);
  3601. }
  3602. cudaLinkOptions.AppendFlagString("AdditionalOptions", linkFlags);
  3603. if (doDeviceLinking) {
  3604. std::vector<std::string> libVec;
  3605. auto const& kinded = this->GeneratorTarget->GetKindedSources(configName);
  3606. // CMake conversion uses full paths when possible to allow deeper trees.
  3607. // However, CUDA 8.0 msbuild rules fail on absolute paths so for CUDA
  3608. // we must use relative paths.
  3609. const bool forceRelative = true;
  3610. for (cmGeneratorTarget::SourceAndKind const& si : kinded.Sources) {
  3611. switch (si.Kind) {
  3612. case cmGeneratorTarget::SourceKindExternalObject: {
  3613. std::string path =
  3614. this->ConvertPath(si.Source.Value->GetFullPath(), forceRelative);
  3615. ConvertToWindowsSlash(path);
  3616. libVec.emplace_back(std::move(path));
  3617. } break;
  3618. default:
  3619. break;
  3620. }
  3621. }
  3622. // For static libraries that have device linking enabled compute
  3623. // the libraries
  3624. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3625. cmComputeLinkInformation& cli = *pcli;
  3626. cmLinkLineDeviceComputer computer(
  3627. this->LocalGenerator,
  3628. this->LocalGenerator->GetStateSnapshot().GetDirectory());
  3629. std::vector<BT<std::string>> btLibVec;
  3630. computer.ComputeLinkLibraries(cli, std::string{}, btLibVec);
  3631. for (auto const& item : btLibVec) {
  3632. libVec.emplace_back(item.Value);
  3633. }
  3634. }
  3635. if (!libVec.empty()) {
  3636. cudaLinkOptions.AddFlag("AdditionalDependencies", libVec);
  3637. }
  3638. }
  3639. this->CudaLinkOptions[configName] = std::move(pOptions);
  3640. return true;
  3641. }
  3642. void cmVisualStudio10TargetGenerator::WriteCudaLinkOptions(
  3643. Elem& e1, std::string const& configName)
  3644. {
  3645. // We need to write link options for OBJECT libraries so that
  3646. // we override the default device link behavior ( enabled ) when
  3647. // building object libraries with ptx/optix-ir/etc
  3648. if (this->GeneratorTarget->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  3649. return;
  3650. }
  3651. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  3652. return;
  3653. }
  3654. Elem e2(e1, "CudaLink");
  3655. OptionsHelper cudaLinkOptions(*(this->CudaLinkOptions[configName]), e2);
  3656. cudaLinkOptions.OutputFlagMap();
  3657. }
  3658. bool cmVisualStudio10TargetGenerator::ComputeMarmasmOptions()
  3659. {
  3660. if (!this->GlobalGenerator->IsMarmasmEnabled()) {
  3661. return true;
  3662. }
  3663. return std::all_of(
  3664. this->Configurations.begin(), this->Configurations.end(),
  3665. [this](std::string const& c) { return this->ComputeMarmasmOptions(c); });
  3666. }
  3667. bool cmVisualStudio10TargetGenerator::ComputeMarmasmOptions(
  3668. std::string const& configName)
  3669. {
  3670. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3671. auto pOptions = cm::make_unique<Options>(
  3672. this->LocalGenerator, Options::MarmasmCompiler, gg->GetMarmasmFlagTable());
  3673. Options& marmasmOptions = *pOptions;
  3674. std::string flags;
  3675. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3676. cmBuildStep::Compile, "ASM_MARMASM",
  3677. configName);
  3678. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3679. "ASM_MARMASM", configName);
  3680. marmasmOptions.Parse(flags);
  3681. // Get includes for this target
  3682. marmasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MARMASM"));
  3683. this->MarmasmOptions[configName] = std::move(pOptions);
  3684. return true;
  3685. }
  3686. void cmVisualStudio10TargetGenerator::WriteMarmasmOptions(
  3687. Elem& e1, std::string const& configName)
  3688. {
  3689. if (!this->MSTools || !this->GlobalGenerator->IsMarmasmEnabled()) {
  3690. return;
  3691. }
  3692. Elem e2(e1, "MARMASM");
  3693. // Preprocessor definitions and includes are shared with clOptions.
  3694. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3695. clOptions.OutputPreprocessorDefinitions("ASM_MARMASM");
  3696. OptionsHelper marmasmOptions(*(this->MarmasmOptions[configName]), e2);
  3697. marmasmOptions.OutputAdditionalIncludeDirectories("ASM_MARMASM");
  3698. marmasmOptions.PrependInheritedString("AdditionalOptions");
  3699. marmasmOptions.OutputFlagMap();
  3700. }
  3701. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions()
  3702. {
  3703. if (!this->GlobalGenerator->IsMasmEnabled()) {
  3704. return true;
  3705. }
  3706. return std::all_of(
  3707. this->Configurations.begin(), this->Configurations.end(),
  3708. [this](std::string const& c) { return this->ComputeMasmOptions(c); });
  3709. }
  3710. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions(
  3711. std::string const& configName)
  3712. {
  3713. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3714. auto pOptions = cm::make_unique<Options>(
  3715. this->LocalGenerator, Options::MasmCompiler, gg->GetMasmFlagTable());
  3716. Options& masmOptions = *pOptions;
  3717. // MSBuild enables debug information by default.
  3718. // Disable it explicitly unless a flag parsed below re-enables it.
  3719. masmOptions.AddFlag("GenerateDebugInformation", "false");
  3720. std::string flags;
  3721. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3722. cmBuildStep::Compile, "ASM_MASM",
  3723. configName);
  3724. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3725. "ASM_MASM", configName);
  3726. masmOptions.Parse(flags);
  3727. // Get includes for this target
  3728. masmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MASM"));
  3729. this->MasmOptions[configName] = std::move(pOptions);
  3730. return true;
  3731. }
  3732. void cmVisualStudio10TargetGenerator::WriteMasmOptions(
  3733. Elem& e1, std::string const& configName)
  3734. {
  3735. if (!this->MSTools || !this->GlobalGenerator->IsMasmEnabled()) {
  3736. return;
  3737. }
  3738. Elem e2(e1, "MASM");
  3739. // Preprocessor definitions and includes are shared with clOptions.
  3740. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3741. clOptions.OutputPreprocessorDefinitions("ASM_MASM");
  3742. OptionsHelper masmOptions(*(this->MasmOptions[configName]), e2);
  3743. masmOptions.OutputAdditionalIncludeDirectories("ASM_MASM");
  3744. masmOptions.PrependInheritedString("AdditionalOptions");
  3745. masmOptions.OutputFlagMap();
  3746. }
  3747. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions()
  3748. {
  3749. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3750. return true;
  3751. }
  3752. return std::all_of(
  3753. this->Configurations.begin(), this->Configurations.end(),
  3754. [this](std::string const& c) { return this->ComputeNasmOptions(c); });
  3755. }
  3756. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions(
  3757. std::string const& configName)
  3758. {
  3759. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3760. auto pOptions = cm::make_unique<Options>(
  3761. this->LocalGenerator, Options::NasmCompiler, gg->GetNasmFlagTable());
  3762. Options& nasmOptions = *pOptions;
  3763. std::string flags;
  3764. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3765. cmBuildStep::Compile, "ASM_NASM",
  3766. configName);
  3767. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  3768. "ASM_NASM", configName);
  3769. flags += " -f";
  3770. flags += this->Makefile->GetSafeDefinition("CMAKE_ASM_NASM_OBJECT_FORMAT");
  3771. nasmOptions.Parse(flags);
  3772. // Get includes for this target
  3773. nasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_NASM"));
  3774. this->NasmOptions[configName] = std::move(pOptions);
  3775. return true;
  3776. }
  3777. void cmVisualStudio10TargetGenerator::WriteNasmOptions(
  3778. Elem& e1, std::string const& configName)
  3779. {
  3780. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3781. return;
  3782. }
  3783. Elem e2(e1, "NASM");
  3784. OptionsHelper nasmOptions(*(this->NasmOptions[configName]), e2);
  3785. nasmOptions.OutputAdditionalIncludeDirectories("ASM_NASM");
  3786. nasmOptions.OutputFlagMap();
  3787. nasmOptions.PrependInheritedString("AdditionalOptions");
  3788. nasmOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3789. // Preprocessor definitions and includes are shared with clOptions.
  3790. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3791. clOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3792. }
  3793. void cmVisualStudio10TargetGenerator::WriteLibOptions(
  3794. Elem& e1, std::string const& config)
  3795. {
  3796. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3797. this->GeneratorTarget->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  3798. return;
  3799. }
  3800. const std::string& linkLanguage =
  3801. this->GeneratorTarget->GetLinkClosure(config)->LinkerLanguage;
  3802. std::string libflags;
  3803. this->LocalGenerator->GetStaticLibraryFlags(libflags, config, linkLanguage,
  3804. this->GeneratorTarget);
  3805. if (!libflags.empty()) {
  3806. Elem e2(e1, "Lib");
  3807. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3808. cmVS10GeneratorOptions libOptions(this->LocalGenerator,
  3809. cmVisualStudioGeneratorOptions::Linker,
  3810. gg->GetLibFlagTable(), this);
  3811. libOptions.Parse(libflags);
  3812. OptionsHelper oh(libOptions, e2);
  3813. oh.PrependInheritedString("AdditionalOptions");
  3814. oh.OutputFlagMap();
  3815. }
  3816. // We cannot generate metadata for static libraries. WindowsPhone
  3817. // and WindowsStore tools look at GenerateWindowsMetadata in the
  3818. // Link tool options even for static libraries.
  3819. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3820. this->GlobalGenerator->TargetsWindowsStore()) {
  3821. Elem e2(e1, "Link");
  3822. e2.Element("GenerateWindowsMetadata", "false");
  3823. }
  3824. }
  3825. void cmVisualStudio10TargetGenerator::WriteManifestOptions(
  3826. Elem& e1, std::string const& config)
  3827. {
  3828. if (this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE &&
  3829. this->GeneratorTarget->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3830. this->GeneratorTarget->GetType() != cmStateEnums::MODULE_LIBRARY) {
  3831. return;
  3832. }
  3833. std::vector<cmSourceFile const*> manifest_srcs;
  3834. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  3835. cmValue dpiAware = this->GeneratorTarget->GetProperty("VS_DPI_AWARE");
  3836. if (!manifest_srcs.empty() || dpiAware) {
  3837. Elem e2(e1, "Manifest");
  3838. if (!manifest_srcs.empty()) {
  3839. std::ostringstream oss;
  3840. for (cmSourceFile const* mi : manifest_srcs) {
  3841. std::string m = this->ConvertPath(mi->GetFullPath(), false);
  3842. ConvertToWindowsSlash(m);
  3843. oss << m << ";";
  3844. }
  3845. e2.Element("AdditionalManifestFiles", oss.str());
  3846. }
  3847. if (dpiAware) {
  3848. if (*dpiAware == "PerMonitor"_s) {
  3849. e2.Element("EnableDpiAwareness", "PerMonitorHighDPIAware");
  3850. } else if (dpiAware.IsOn()) {
  3851. e2.Element("EnableDpiAwareness", "true");
  3852. } else if (dpiAware.IsOff()) {
  3853. e2.Element("EnableDpiAwareness", "false");
  3854. } else {
  3855. cmSystemTools::Error(
  3856. cmStrCat("Bad parameter for VS_DPI_AWARE: ", *dpiAware));
  3857. }
  3858. }
  3859. }
  3860. }
  3861. void cmVisualStudio10TargetGenerator::WriteAntBuildOptions(
  3862. Elem& e1, std::string const& configName)
  3863. {
  3864. // Look through the sources for AndroidManifest.xml and use
  3865. // its location as the root source directory.
  3866. std::string rootDir = this->LocalGenerator->GetCurrentSourceDirectory();
  3867. {
  3868. for (cmGeneratorTarget::AllConfigSource const& source :
  3869. this->GeneratorTarget->GetAllConfigSources()) {
  3870. if (source.Kind == cmGeneratorTarget::SourceKindExtra &&
  3871. "androidmanifest.xml" ==
  3872. cmSystemTools::LowerCase(source.Source->GetLocation().GetName())) {
  3873. rootDir = source.Source->GetLocation().GetDirectory();
  3874. break;
  3875. }
  3876. }
  3877. }
  3878. // Tell MSBuild to launch Ant.
  3879. Elem e2(e1, "AntBuild");
  3880. {
  3881. std::string antBuildPath = rootDir;
  3882. ConvertToWindowsSlash(antBuildPath);
  3883. e2.Element("AntBuildPath", antBuildPath);
  3884. }
  3885. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_SKIP_ANT_STEP")) {
  3886. e2.Element("SkipAntStep", "true");
  3887. }
  3888. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_PROGUARD")) {
  3889. e2.Element("EnableProGuard", "true");
  3890. }
  3891. if (cmValue proGuardConfigLocation =
  3892. this->GeneratorTarget->GetProperty("ANDROID_PROGUARD_CONFIG_PATH")) {
  3893. e2.Element("ProGuardConfigLocation", *proGuardConfigLocation);
  3894. }
  3895. if (cmValue securePropertiesLocation =
  3896. this->GeneratorTarget->GetProperty("ANDROID_SECURE_PROPS_PATH")) {
  3897. e2.Element("SecurePropertiesLocation", *securePropertiesLocation);
  3898. }
  3899. if (cmValue nativeLibDirectoriesExpression =
  3900. this->GeneratorTarget->GetProperty("ANDROID_NATIVE_LIB_DIRECTORIES")) {
  3901. std::string nativeLibDirs = cmGeneratorExpression::Evaluate(
  3902. *nativeLibDirectoriesExpression, this->LocalGenerator, configName);
  3903. e2.Element("NativeLibDirectories", nativeLibDirs);
  3904. }
  3905. if (cmValue nativeLibDependenciesExpression =
  3906. this->GeneratorTarget->GetProperty(
  3907. "ANDROID_NATIVE_LIB_DEPENDENCIES")) {
  3908. std::string nativeLibDeps = cmGeneratorExpression::Evaluate(
  3909. *nativeLibDependenciesExpression, this->LocalGenerator, configName);
  3910. e2.Element("NativeLibDependencies", nativeLibDeps);
  3911. }
  3912. if (cmValue javaSourceDir =
  3913. this->GeneratorTarget->GetProperty("ANDROID_JAVA_SOURCE_DIR")) {
  3914. e2.Element("JavaSourceDir", *javaSourceDir);
  3915. }
  3916. if (cmValue jarDirectoriesExpression =
  3917. this->GeneratorTarget->GetProperty("ANDROID_JAR_DIRECTORIES")) {
  3918. std::string jarDirectories = cmGeneratorExpression::Evaluate(
  3919. *jarDirectoriesExpression, this->LocalGenerator, configName);
  3920. e2.Element("JarDirectories", jarDirectories);
  3921. }
  3922. if (cmValue jarDeps =
  3923. this->GeneratorTarget->GetProperty("ANDROID_JAR_DEPENDENCIES")) {
  3924. e2.Element("JarDependencies", *jarDeps);
  3925. }
  3926. if (cmValue assetsDirectories =
  3927. this->GeneratorTarget->GetProperty("ANDROID_ASSETS_DIRECTORIES")) {
  3928. e2.Element("AssetsDirectories", *assetsDirectories);
  3929. }
  3930. {
  3931. std::string manifest_xml = cmStrCat(rootDir, "/AndroidManifest.xml");
  3932. ConvertToWindowsSlash(manifest_xml);
  3933. e2.Element("AndroidManifestLocation", manifest_xml);
  3934. }
  3935. if (cmValue antAdditionalOptions =
  3936. this->GeneratorTarget->GetProperty("ANDROID_ANT_ADDITIONAL_OPTIONS")) {
  3937. e2.Element("AdditionalOptions",
  3938. cmStrCat(*antAdditionalOptions, " %(AdditionalOptions)"));
  3939. }
  3940. }
  3941. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  3942. {
  3943. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  3944. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3945. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3946. for (std::string const& c : this->Configurations) {
  3947. if (!this->ComputeLinkOptions(c)) {
  3948. return false;
  3949. }
  3950. }
  3951. }
  3952. return true;
  3953. }
  3954. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
  3955. std::string const& config)
  3956. {
  3957. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3958. auto pOptions = cm::make_unique<Options>(
  3959. this->LocalGenerator, Options::Linker, gg->GetLinkFlagTable(), this);
  3960. Options& linkOptions = *pOptions;
  3961. cmGeneratorTarget::LinkClosure const* linkClosure =
  3962. this->GeneratorTarget->GetLinkClosure(config);
  3963. const std::string& linkLanguage = linkClosure->LinkerLanguage;
  3964. if (linkLanguage.empty()) {
  3965. cmSystemTools::Error(cmStrCat(
  3966. "CMake can not determine linker language for target: ", this->Name));
  3967. return false;
  3968. }
  3969. std::string CONFIG = cmSystemTools::UpperCase(config);
  3970. const char* linkType = "SHARED";
  3971. if (this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3972. linkType = "MODULE";
  3973. }
  3974. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3975. linkType = "EXE";
  3976. }
  3977. std::string flags;
  3978. std::string linkFlagVarBase = cmStrCat("CMAKE_", linkType, "_LINKER_FLAGS");
  3979. flags += ' ';
  3980. flags += this->Makefile->GetRequiredDefinition(linkFlagVarBase);
  3981. std::string linkFlagVar = cmStrCat(linkFlagVarBase, '_', CONFIG);
  3982. flags += ' ';
  3983. flags += this->Makefile->GetRequiredDefinition(linkFlagVar);
  3984. cmValue targetLinkFlags = this->GeneratorTarget->GetProperty("LINK_FLAGS");
  3985. if (targetLinkFlags) {
  3986. flags += ' ';
  3987. flags += *targetLinkFlags;
  3988. }
  3989. std::string flagsProp = cmStrCat("LINK_FLAGS_", CONFIG);
  3990. if (cmValue flagsConfig = this->GeneratorTarget->GetProperty(flagsProp)) {
  3991. flags += ' ';
  3992. flags += *flagsConfig;
  3993. }
  3994. std::vector<std::string> opts;
  3995. this->GeneratorTarget->GetLinkOptions(opts, config, linkLanguage);
  3996. // LINK_OPTIONS are escaped.
  3997. this->LocalGenerator->AppendCompileOptions(flags, opts);
  3998. cmComputeLinkInformation* pcli =
  3999. this->GeneratorTarget->GetLinkInformation(config);
  4000. if (!pcli) {
  4001. cmSystemTools::Error(
  4002. cmStrCat("CMake can not compute cmComputeLinkInformation for target: ",
  4003. this->Name));
  4004. return false;
  4005. }
  4006. cmComputeLinkInformation& cli = *pcli;
  4007. std::vector<std::string> libVec;
  4008. std::vector<std::string> vsTargetVec;
  4009. this->AddLibraries(cli, libVec, vsTargetVec, config);
  4010. std::string standardLibsVar =
  4011. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  4012. std::string const& libs = this->Makefile->GetSafeDefinition(standardLibsVar);
  4013. cmSystemTools::ParseWindowsCommandLine(libs.c_str(), libVec);
  4014. linkOptions.AddFlag("AdditionalDependencies", libVec);
  4015. // Populate TargetsFileAndConfigsVec
  4016. for (std::string const& ti : vsTargetVec) {
  4017. this->AddTargetsFileAndConfigPair(ti, config);
  4018. }
  4019. std::vector<std::string> const& ldirs = cli.GetDirectories();
  4020. std::vector<std::string> linkDirs;
  4021. for (std::string const& d : ldirs) {
  4022. // first just full path
  4023. linkDirs.push_back(d);
  4024. // next path with configuration type Debug, Release, etc
  4025. linkDirs.emplace_back(cmStrCat(d, "/$(Configuration)"));
  4026. }
  4027. linkDirs.push_back("%(AdditionalLibraryDirectories)");
  4028. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs);
  4029. cmGeneratorTarget::Names targetNames;
  4030. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4031. targetNames = this->GeneratorTarget->GetExecutableNames(config);
  4032. } else {
  4033. targetNames = this->GeneratorTarget->GetLibraryNames(config);
  4034. }
  4035. if (this->MSTools) {
  4036. if (this->GeneratorTarget->IsWin32Executable(config)) {
  4037. if (this->GlobalGenerator->TargetsWindowsCE()) {
  4038. linkOptions.AddFlag("SubSystem", "WindowsCE");
  4039. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4040. if (this->ClOptions[config]->UsingUnicode()) {
  4041. linkOptions.AddFlag("EntryPointSymbol", "wWinMainCRTStartup");
  4042. } else {
  4043. linkOptions.AddFlag("EntryPointSymbol", "WinMainCRTStartup");
  4044. }
  4045. }
  4046. } else {
  4047. linkOptions.AddFlag("SubSystem", "Windows");
  4048. }
  4049. } else {
  4050. if (this->GlobalGenerator->TargetsWindowsCE()) {
  4051. linkOptions.AddFlag("SubSystem", "WindowsCE");
  4052. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4053. if (this->ClOptions[config]->UsingUnicode()) {
  4054. linkOptions.AddFlag("EntryPointSymbol", "mainWCRTStartup");
  4055. } else {
  4056. linkOptions.AddFlag("EntryPointSymbol", "mainACRTStartup");
  4057. }
  4058. }
  4059. } else {
  4060. linkOptions.AddFlag("SubSystem", "Console");
  4061. };
  4062. }
  4063. if (cmValue stackVal = this->Makefile->GetDefinition(
  4064. cmStrCat("CMAKE_", linkLanguage, "_STACK_SIZE"))) {
  4065. linkOptions.AddFlag("StackReserveSize", *stackVal);
  4066. }
  4067. linkOptions.AddFlag("GenerateDebugInformation", "false");
  4068. std::string pdb = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config),
  4069. '/', targetNames.PDB);
  4070. if (!targetNames.ImportLibrary.empty()) {
  4071. std::string imLib =
  4072. cmStrCat(this->GeneratorTarget->GetDirectory(
  4073. config, cmStateEnums::ImportLibraryArtifact),
  4074. '/', targetNames.ImportLibrary);
  4075. linkOptions.AddFlag("ImportLibrary", imLib);
  4076. }
  4077. linkOptions.AddFlag("ProgramDataBaseFile", pdb);
  4078. // A Windows Runtime component uses internal .NET metadata,
  4079. // so does not have an import library.
  4080. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") &&
  4081. this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE) {
  4082. linkOptions.AddFlag("GenerateWindowsMetadata", "true");
  4083. } else if (this->GlobalGenerator->TargetsWindowsPhone() ||
  4084. this->GlobalGenerator->TargetsWindowsStore()) {
  4085. // WindowsPhone and WindowsStore components are in an app container
  4086. // and produce WindowsMetadata. If we are not producing a WINRT
  4087. // component, then do not generate the metadata here.
  4088. linkOptions.AddFlag("GenerateWindowsMetadata", "false");
  4089. }
  4090. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  4091. this->GlobalGenerator->GetSystemVersion() == "8.0"_s) {
  4092. // WindowsPhone 8.0 does not have ole32.
  4093. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries", "ole32.lib");
  4094. }
  4095. } else if (this->NsightTegra) {
  4096. linkOptions.AddFlag("SoName", targetNames.SharedObject);
  4097. }
  4098. linkOptions.Parse(flags);
  4099. linkOptions.FixManifestUACFlags();
  4100. if (this->MSTools) {
  4101. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  4102. this->GeneratorTarget->GetModuleDefinitionInfo(config);
  4103. if (mdi && !mdi->DefFile.empty()) {
  4104. linkOptions.AddFlag("ModuleDefinitionFile", mdi->DefFile);
  4105. }
  4106. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries",
  4107. "%(IgnoreSpecificDefaultLibraries)");
  4108. }
  4109. // VS 2015 without all updates has a v140 toolset whose
  4110. // GenerateDebugInformation expects No/Debug instead of false/true.
  4111. if (gg->GetPlatformToolsetNeedsDebugEnum()) {
  4112. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  4113. if (strcmp(debug, "false") == 0) {
  4114. linkOptions.AddFlag("GenerateDebugInformation", "No");
  4115. } else if (strcmp(debug, "true") == 0) {
  4116. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  4117. }
  4118. }
  4119. }
  4120. // Managed code cannot be linked with /DEBUG:FASTLINK
  4121. if (this->Managed) {
  4122. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  4123. if (strcmp(debug, "DebugFastLink") == 0) {
  4124. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  4125. }
  4126. }
  4127. }
  4128. this->LinkOptions[config] = std::move(pOptions);
  4129. return true;
  4130. }
  4131. bool cmVisualStudio10TargetGenerator::ComputeLibOptions()
  4132. {
  4133. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4134. for (std::string const& c : this->Configurations) {
  4135. if (!this->ComputeLibOptions(c)) {
  4136. return false;
  4137. }
  4138. }
  4139. }
  4140. return true;
  4141. }
  4142. bool cmVisualStudio10TargetGenerator::ComputeLibOptions(
  4143. std::string const& config)
  4144. {
  4145. cmComputeLinkInformation* pcli =
  4146. this->GeneratorTarget->GetLinkInformation(config);
  4147. if (!pcli) {
  4148. cmSystemTools::Error(
  4149. cmStrCat("CMake can not compute cmComputeLinkInformation for target: ",
  4150. this->Name));
  4151. return false;
  4152. }
  4153. cmComputeLinkInformation& cli = *pcli;
  4154. using ItemVector = cmComputeLinkInformation::ItemVector;
  4155. const ItemVector& libs = cli.GetItems();
  4156. for (cmComputeLinkInformation::Item const& l : libs) {
  4157. if (l.IsPath == cmComputeLinkInformation::ItemIsPath::Yes &&
  4158. cmVS10IsTargetsFile(l.Value.Value)) {
  4159. std::string path =
  4160. this->LocalGenerator->MaybeRelativeToCurBinDir(l.Value.Value);
  4161. ConvertToWindowsSlash(path);
  4162. this->AddTargetsFileAndConfigPair(path, config);
  4163. }
  4164. }
  4165. return true;
  4166. }
  4167. void cmVisualStudio10TargetGenerator::WriteLinkOptions(
  4168. Elem& e1, std::string const& config)
  4169. {
  4170. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4171. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  4172. return;
  4173. }
  4174. if (this->ProjectType == VsProjectType::csproj) {
  4175. return;
  4176. }
  4177. {
  4178. Elem e2(e1, "Link");
  4179. OptionsHelper linkOptions(*(this->LinkOptions[config]), e2);
  4180. linkOptions.PrependInheritedString("AdditionalOptions");
  4181. linkOptions.OutputFlagMap();
  4182. }
  4183. if (!this->GlobalGenerator->NeedLinkLibraryDependencies(
  4184. this->GeneratorTarget)) {
  4185. Elem e2(e1, "ProjectReference");
  4186. e2.Element("LinkLibraryDependencies", "false");
  4187. }
  4188. }
  4189. void cmVisualStudio10TargetGenerator::AddLibraries(
  4190. const cmComputeLinkInformation& cli, std::vector<std::string>& libVec,
  4191. std::vector<std::string>& vsTargetVec, const std::string& config)
  4192. {
  4193. using ItemVector = cmComputeLinkInformation::ItemVector;
  4194. ItemVector const& libs = cli.GetItems();
  4195. for (cmComputeLinkInformation::Item const& l : libs) {
  4196. if (l.Target) {
  4197. auto managedType = l.Target->GetManagedType(config);
  4198. if (managedType != cmGeneratorTarget::ManagedType::Native &&
  4199. this->GeneratorTarget->GetManagedType(config) !=
  4200. cmGeneratorTarget::ManagedType::Native &&
  4201. l.Target->IsImported() &&
  4202. l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4203. auto location = l.Target->GetFullPath(config);
  4204. if (!location.empty()) {
  4205. ConvertToWindowsSlash(location);
  4206. switch (this->ProjectType) {
  4207. case VsProjectType::csproj:
  4208. // If the target we want to "link" to is an imported managed
  4209. // target and this is a C# project, we add a hint reference. This
  4210. // reference is written to project file in
  4211. // WriteDotNetReferences().
  4212. this->DotNetHintReferences[config].push_back(
  4213. DotNetHintReference(l.Target->GetName(), location));
  4214. break;
  4215. case VsProjectType::vcxproj:
  4216. // Add path of assembly to list of using-directories, so the
  4217. // managed assembly can be used by '#using <assembly.dll>' in
  4218. // code.
  4219. this->AdditionalUsingDirectories[config].insert(
  4220. cmSystemTools::GetFilenamePath(location));
  4221. break;
  4222. default:
  4223. // In .proj files, we wouldn't be referencing libraries.
  4224. break;
  4225. }
  4226. }
  4227. }
  4228. // Do not allow C# targets to be added to the LIB listing. LIB files are
  4229. // used for linking C++ dependencies. C# libraries do not have lib files.
  4230. // Instead, they compile down to C# reference libraries (DLL files). The
  4231. // `<ProjectReference>` elements added to the vcxproj are enough for the
  4232. // IDE to deduce the DLL file required by other C# projects that need its
  4233. // reference library.
  4234. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  4235. continue;
  4236. }
  4237. }
  4238. if (l.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  4239. std::string path =
  4240. this->LocalGenerator->MaybeRelativeToCurBinDir(l.Value.Value);
  4241. ConvertToWindowsSlash(path);
  4242. if (cmVS10IsTargetsFile(l.Value.Value)) {
  4243. vsTargetVec.push_back(path);
  4244. } else {
  4245. libVec.push_back(l.HasFeature() ? l.GetFormattedItem(path).Value
  4246. : path);
  4247. }
  4248. } else if (!l.Target ||
  4249. (l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY &&
  4250. l.Target->GetType() != cmStateEnums::OBJECT_LIBRARY)) {
  4251. libVec.push_back(l.Value.Value);
  4252. }
  4253. }
  4254. }
  4255. void cmVisualStudio10TargetGenerator::AddTargetsFileAndConfigPair(
  4256. std::string const& targetsFile, std::string const& config)
  4257. {
  4258. for (TargetsFileAndConfigs& i : this->TargetsFileAndConfigsVec) {
  4259. if (cmSystemTools::ComparePath(targetsFile, i.File)) {
  4260. if (!cm::contains(i.Configs, config)) {
  4261. i.Configs.push_back(config);
  4262. }
  4263. return;
  4264. }
  4265. }
  4266. TargetsFileAndConfigs entry;
  4267. entry.File = targetsFile;
  4268. entry.Configs.push_back(config);
  4269. this->TargetsFileAndConfigsVec.push_back(entry);
  4270. }
  4271. void cmVisualStudio10TargetGenerator::WriteMidlOptions(
  4272. Elem& e1, std::string const& configName)
  4273. {
  4274. if (!this->MSTools) {
  4275. return;
  4276. }
  4277. if (this->ProjectType == VsProjectType::csproj) {
  4278. return;
  4279. }
  4280. if (this->GeneratorTarget->GetType() > cmStateEnums::UTILITY) {
  4281. return;
  4282. }
  4283. // This processes *any* of the .idl files specified in the project's file
  4284. // list (and passed as the item metadata %(Filename) expressing the rule
  4285. // input filename) into output files at the per-config *build* dir
  4286. // ($(IntDir)) each.
  4287. //
  4288. // IOW, this MIDL section is intended to provide a fully generic syntax
  4289. // content suitable for most cases (read: if you get errors, then it's quite
  4290. // probable that the error is on your side of the .idl setup).
  4291. //
  4292. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  4293. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  4294. // project's file list, otherwise the compiler-side processing won't pick it
  4295. // up (for non-directory form, it ends up looking in project binary dir
  4296. // only). Perhaps there's something to be done to make this more automatic
  4297. // on the CMake side?
  4298. std::vector<std::string> const includes =
  4299. this->GetIncludes(configName, "MIDL");
  4300. std::ostringstream oss;
  4301. for (std::string const& i : includes) {
  4302. oss << i << ";";
  4303. }
  4304. oss << "%(AdditionalIncludeDirectories)";
  4305. Elem e2(e1, "Midl");
  4306. e2.Element("AdditionalIncludeDirectories", oss.str());
  4307. e2.Element("OutputDirectory", "$(ProjectDir)/$(IntDir)");
  4308. e2.Element("HeaderFileName", "%(Filename).h");
  4309. e2.Element("TypeLibraryName", "%(Filename).tlb");
  4310. e2.Element("InterfaceIdentifierFileName", "%(Filename)_i.c");
  4311. e2.Element("ProxyFileName", "%(Filename)_p.c");
  4312. }
  4313. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups(Elem& e0)
  4314. {
  4315. if (this->ProjectType == VsProjectType::csproj) {
  4316. return;
  4317. }
  4318. for (const std::string& c : this->Configurations) {
  4319. Elem e1(e0, "ItemDefinitionGroup");
  4320. e1.Attribute("Condition", this->CalcCondition(c));
  4321. // output cl compile flags <ClCompile></ClCompile>
  4322. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  4323. this->WriteClOptions(e1, c);
  4324. // output rc compile flags <ResourceCompile></ResourceCompile>
  4325. this->WriteRCOptions(e1, c);
  4326. this->WriteCudaOptions(e1, c);
  4327. this->WriteMarmasmOptions(e1, c);
  4328. this->WriteMasmOptions(e1, c);
  4329. this->WriteNasmOptions(e1, c);
  4330. }
  4331. // output midl flags <Midl></Midl>
  4332. this->WriteMidlOptions(e1, c);
  4333. // write events
  4334. if (this->ProjectType != VsProjectType::csproj) {
  4335. this->WriteEvents(e1, c);
  4336. }
  4337. // output link flags <Link></Link>
  4338. this->WriteLinkOptions(e1, c);
  4339. this->WriteCudaLinkOptions(e1, c);
  4340. // output lib flags <Lib></Lib>
  4341. this->WriteLibOptions(e1, c);
  4342. // output manifest flags <Manifest></Manifest>
  4343. this->WriteManifestOptions(e1, c);
  4344. if (this->NsightTegra &&
  4345. this->GeneratorTarget->Target->IsAndroidGuiExecutable()) {
  4346. this->WriteAntBuildOptions(e1, c);
  4347. }
  4348. }
  4349. }
  4350. void cmVisualStudio10TargetGenerator::WriteEvents(
  4351. Elem& e1, std::string const& configName)
  4352. {
  4353. bool addedPrelink = false;
  4354. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  4355. this->GeneratorTarget->GetModuleDefinitionInfo(configName);
  4356. if (mdi && mdi->DefFileGenerated) {
  4357. addedPrelink = true;
  4358. std::vector<cmCustomCommand> commands =
  4359. this->GeneratorTarget->GetPreLinkCommands();
  4360. this->GlobalGenerator->AddSymbolExportCommand(this->GeneratorTarget,
  4361. commands, configName);
  4362. this->WriteEvent(e1, "PreLinkEvent", commands, configName);
  4363. }
  4364. if (!addedPrelink) {
  4365. this->WriteEvent(e1, "PreLinkEvent",
  4366. this->GeneratorTarget->GetPreLinkCommands(), configName);
  4367. }
  4368. this->WriteEvent(e1, "PreBuildEvent",
  4369. this->GeneratorTarget->GetPreBuildCommands(), configName);
  4370. this->WriteEvent(e1, "PostBuildEvent",
  4371. this->GeneratorTarget->GetPostBuildCommands(), configName);
  4372. }
  4373. void cmVisualStudio10TargetGenerator::WriteEvent(
  4374. Elem& e1, const std::string& name,
  4375. std::vector<cmCustomCommand> const& commands, std::string const& configName)
  4376. {
  4377. if (commands.empty()) {
  4378. return;
  4379. }
  4380. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  4381. std::string script;
  4382. const char* pre = "";
  4383. std::string comment;
  4384. bool stdPipesUTF8 = false;
  4385. for (cmCustomCommand const& cc : commands) {
  4386. cmCustomCommandGenerator ccg(cc, configName, lg);
  4387. if (!ccg.HasOnlyEmptyCommandLines()) {
  4388. comment += pre;
  4389. comment += lg->ConstructComment(ccg);
  4390. script += pre;
  4391. pre = "\n";
  4392. script += lg->ConstructScript(ccg);
  4393. stdPipesUTF8 = stdPipesUTF8 || cc.GetStdPipesUTF8();
  4394. }
  4395. }
  4396. if (!script.empty()) {
  4397. script += lg->FinishConstructScript(this->ProjectType);
  4398. }
  4399. comment = cmVS10EscapeComment(comment);
  4400. if (this->ProjectType != VsProjectType::csproj) {
  4401. Elem e2(e1, name);
  4402. if (stdPipesUTF8) {
  4403. this->WriteStdOutEncodingUtf8(e2);
  4404. }
  4405. e2.Element("Message", comment);
  4406. e2.Element("Command", script);
  4407. } else {
  4408. std::string strippedComment = comment;
  4409. strippedComment.erase(
  4410. std::remove(strippedComment.begin(), strippedComment.end(), '\t'),
  4411. strippedComment.end());
  4412. std::ostringstream oss;
  4413. if (!comment.empty() && !strippedComment.empty()) {
  4414. oss << "echo " << comment << "\n";
  4415. }
  4416. oss << script << "\n";
  4417. e1.Element(name, oss.str());
  4418. }
  4419. }
  4420. void cmVisualStudio10TargetGenerator::WriteProjectReferences(Elem& e0)
  4421. {
  4422. cmGlobalGenerator::TargetDependSet const& unordered =
  4423. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  4424. using OrderedTargetDependSet =
  4425. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  4426. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  4427. Elem e1(e0, "ItemGroup");
  4428. e1.SetHasElements();
  4429. for (cmGeneratorTarget const* dt : depends) {
  4430. if (!dt->IsInBuildSystem()) {
  4431. continue;
  4432. }
  4433. // skip fortran targets as they can not be processed by MSBuild
  4434. // the only reference will be in the .sln file
  4435. if (this->GlobalGenerator->TargetIsFortranOnly(dt)) {
  4436. continue;
  4437. }
  4438. cmLocalGenerator* lg = dt->GetLocalGenerator();
  4439. std::string name = dt->GetName();
  4440. std::string path;
  4441. if (cmValue p = dt->GetProperty("EXTERNAL_MSPROJECT")) {
  4442. path = *p;
  4443. } else {
  4444. path = cmStrCat(lg->GetCurrentBinaryDirectory(), '/', dt->GetName(),
  4445. computeProjectFileExtension(dt));
  4446. }
  4447. ConvertToWindowsSlash(path);
  4448. Elem e2(e1, "ProjectReference");
  4449. e2.Attribute("Include", path);
  4450. e2.Element("Project",
  4451. cmStrCat('{', this->GlobalGenerator->GetGUID(name), '}'));
  4452. e2.Element("Name", name);
  4453. this->WriteDotNetReferenceCustomTags(e2, name);
  4454. if (dt->IsCSharpOnly() || cmHasLiteralSuffix(path, "csproj")) {
  4455. e2.Element("SkipGetTargetFrameworkProperties", "true");
  4456. }
  4457. // Don't reference targets that don't produce any output.
  4458. else if (this->Configurations.empty() ||
  4459. dt->GetManagedType(this->Configurations[0]) ==
  4460. cmGeneratorTarget::ManagedType::Undefined) {
  4461. e2.Element("ReferenceOutputAssembly", "false");
  4462. e2.Element("CopyToOutputDirectory", "Never");
  4463. }
  4464. }
  4465. }
  4466. void cmVisualStudio10TargetGenerator::WritePlatformExtensions(Elem& e1)
  4467. {
  4468. // This only applies to Windows 10 apps
  4469. if (this->GlobalGenerator->TargetsWindowsStore() &&
  4470. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  4471. cmValue desktopExtensionsVersion =
  4472. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  4473. if (desktopExtensionsVersion) {
  4474. this->WriteSinglePlatformExtension(e1, "WindowsDesktop",
  4475. *desktopExtensionsVersion);
  4476. }
  4477. cmValue mobileExtensionsVersion =
  4478. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  4479. if (mobileExtensionsVersion) {
  4480. this->WriteSinglePlatformExtension(e1, "WindowsMobile",
  4481. *mobileExtensionsVersion);
  4482. }
  4483. }
  4484. }
  4485. void cmVisualStudio10TargetGenerator::WriteSinglePlatformExtension(
  4486. Elem& e1, std::string const& extension, std::string const& version)
  4487. {
  4488. const std::string s =
  4489. cmStrCat("$([Microsoft.Build.Utilities.ToolLocationHelper]"
  4490. "::GetPlatformExtensionSDKLocation(`",
  4491. extension, ", Version=", version,
  4492. "`, $(TargetPlatformIdentifier), $(TargetPlatformVersion), null, "
  4493. "$(ExtensionSDKDirectoryRoot), null))"
  4494. "\\DesignTime\\CommonConfiguration\\Neutral\\",
  4495. extension, ".props");
  4496. Elem e2(e1, "Import");
  4497. e2.Attribute("Project", s);
  4498. e2.Attribute("Condition", cmStrCat("exists('", s, "')"));
  4499. }
  4500. void cmVisualStudio10TargetGenerator::WriteSDKReferences(Elem& e0)
  4501. {
  4502. cmList sdkReferences;
  4503. std::unique_ptr<Elem> spe1;
  4504. if (cmValue vsSDKReferences =
  4505. this->GeneratorTarget->GetProperty("VS_SDK_REFERENCES")) {
  4506. sdkReferences.assign(*vsSDKReferences);
  4507. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  4508. for (auto const& ri : sdkReferences) {
  4509. Elem(*spe1, "SDKReference").Attribute("Include", ri);
  4510. }
  4511. }
  4512. // This only applies to Windows 10 apps
  4513. if (this->GlobalGenerator->TargetsWindowsStore() &&
  4514. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  4515. cmValue desktopExtensionsVersion =
  4516. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  4517. cmValue mobileExtensionsVersion =
  4518. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  4519. cmValue iotExtensionsVersion =
  4520. this->GeneratorTarget->GetProperty("VS_IOT_EXTENSIONS_VERSION");
  4521. if (desktopExtensionsVersion || mobileExtensionsVersion ||
  4522. iotExtensionsVersion) {
  4523. if (!spe1) {
  4524. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  4525. }
  4526. if (desktopExtensionsVersion) {
  4527. this->WriteSingleSDKReference(*spe1, "WindowsDesktop",
  4528. *desktopExtensionsVersion);
  4529. }
  4530. if (mobileExtensionsVersion) {
  4531. this->WriteSingleSDKReference(*spe1, "WindowsMobile",
  4532. *mobileExtensionsVersion);
  4533. }
  4534. if (iotExtensionsVersion) {
  4535. this->WriteSingleSDKReference(*spe1, "WindowsIoT",
  4536. *iotExtensionsVersion);
  4537. }
  4538. }
  4539. }
  4540. }
  4541. void cmVisualStudio10TargetGenerator::WriteSingleSDKReference(
  4542. Elem& e1, std::string const& extension, std::string const& version)
  4543. {
  4544. Elem(e1, "SDKReference")
  4545. .Attribute("Include", cmStrCat(extension, ", Version=", version));
  4546. }
  4547. namespace {
  4548. std::string ComputeCertificateThumbprint(const std::string& source)
  4549. {
  4550. std::string thumbprint;
  4551. CRYPT_INTEGER_BLOB cryptBlob;
  4552. HCERTSTORE certStore = nullptr;
  4553. PCCERT_CONTEXT certContext = nullptr;
  4554. HANDLE certFile = CreateFileW(
  4555. cmsys::Encoding::ToWide(source.c_str()).c_str(), GENERIC_READ,
  4556. FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  4557. if (certFile != INVALID_HANDLE_VALUE && certFile != nullptr) {
  4558. DWORD fileSize = GetFileSize(certFile, nullptr);
  4559. if (fileSize != INVALID_FILE_SIZE) {
  4560. auto certData = cm::make_unique<BYTE[]>(fileSize);
  4561. if (certData != nullptr) {
  4562. DWORD dwRead = 0;
  4563. if (ReadFile(certFile, certData.get(), fileSize, &dwRead, nullptr)) {
  4564. cryptBlob.cbData = fileSize;
  4565. cryptBlob.pbData = certData.get();
  4566. // Verify that this is a valid cert
  4567. if (PFXIsPFXBlob(&cryptBlob)) {
  4568. // Open the certificate as a store
  4569. certStore =
  4570. PFXImportCertStore(&cryptBlob, nullptr, CRYPT_EXPORTABLE);
  4571. if (certStore != nullptr) {
  4572. // There should only be 1 cert.
  4573. certContext =
  4574. CertEnumCertificatesInStore(certStore, certContext);
  4575. if (certContext != nullptr) {
  4576. // The hash is 20 bytes
  4577. BYTE hashData[20];
  4578. DWORD hashLength = 20;
  4579. // Buffer to print the hash. Each byte takes 2 chars +
  4580. // terminating character
  4581. char hashPrint[41];
  4582. char* pHashPrint = hashPrint;
  4583. // Get the hash property from the certificate
  4584. if (CertGetCertificateContextProperty(
  4585. certContext, CERT_HASH_PROP_ID, hashData, &hashLength)) {
  4586. for (DWORD i = 0; i < hashLength; i++) {
  4587. // Convert each byte to hexadecimal
  4588. snprintf(pHashPrint, 3, "%02X", hashData[i]);
  4589. pHashPrint += 2;
  4590. }
  4591. *pHashPrint = '\0';
  4592. thumbprint = hashPrint;
  4593. }
  4594. CertFreeCertificateContext(certContext);
  4595. }
  4596. CertCloseStore(certStore, 0);
  4597. }
  4598. }
  4599. }
  4600. }
  4601. }
  4602. CloseHandle(certFile);
  4603. }
  4604. return thumbprint;
  4605. }
  4606. }
  4607. void cmVisualStudio10TargetGenerator::WriteWinRTPackageCertificateKeyFile(
  4608. Elem& e0)
  4609. {
  4610. if ((this->GlobalGenerator->TargetsWindowsStore() ||
  4611. this->GlobalGenerator->TargetsWindowsPhone()) &&
  4612. (cmStateEnums::EXECUTABLE == this->GeneratorTarget->GetType())) {
  4613. std::string pfxFile;
  4614. for (cmGeneratorTarget::AllConfigSource const& source :
  4615. this->GeneratorTarget->GetAllConfigSources()) {
  4616. if (source.Kind == cmGeneratorTarget::SourceKindCertificate) {
  4617. pfxFile = this->ConvertPath(source.Source->GetFullPath(), false);
  4618. ConvertToWindowsSlash(pfxFile);
  4619. break;
  4620. }
  4621. }
  4622. if (this->IsMissingFiles &&
  4623. !(this->GlobalGenerator->TargetsWindowsPhone() &&
  4624. this->GlobalGenerator->GetSystemVersion() == "8.0"_s)) {
  4625. // Move the manifest to a project directory to avoid clashes
  4626. std::string artifactDir =
  4627. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4628. ConvertToWindowsSlash(artifactDir);
  4629. Elem e1(e0, "PropertyGroup");
  4630. e1.Element("AppxPackageArtifactsDir", cmStrCat(artifactDir, '\\'));
  4631. std::string resourcePriFile =
  4632. cmStrCat(this->DefaultArtifactDir, "/resources.pri");
  4633. ConvertToWindowsSlash(resourcePriFile);
  4634. e1.Element("ProjectPriFullPath", resourcePriFile);
  4635. // If we are missing files and we don't have a certificate and
  4636. // aren't targeting WP8.0, add a default certificate
  4637. if (pfxFile.empty()) {
  4638. std::string templateFolder =
  4639. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/Windows");
  4640. pfxFile =
  4641. cmStrCat(this->DefaultArtifactDir, "/Windows_TemporaryKey.pfx");
  4642. cmSystemTools::CopyAFile(
  4643. cmStrCat(templateFolder, "/Windows_TemporaryKey.pfx"), pfxFile,
  4644. false);
  4645. ConvertToWindowsSlash(pfxFile);
  4646. this->AddedFiles.push_back(pfxFile);
  4647. this->AddedDefaultCertificate = true;
  4648. }
  4649. e1.Element("PackageCertificateKeyFile", pfxFile);
  4650. std::string thumb = ComputeCertificateThumbprint(pfxFile);
  4651. if (!thumb.empty()) {
  4652. e1.Element("PackageCertificateThumbprint", thumb);
  4653. }
  4654. } else if (!pfxFile.empty()) {
  4655. Elem e1(e0, "PropertyGroup");
  4656. e1.Element("PackageCertificateKeyFile", pfxFile);
  4657. std::string thumb = ComputeCertificateThumbprint(pfxFile);
  4658. if (!thumb.empty()) {
  4659. e1.Element("PackageCertificateThumbprint", thumb);
  4660. }
  4661. }
  4662. }
  4663. }
  4664. void cmVisualStudio10TargetGenerator::ClassifyAllConfigSources()
  4665. {
  4666. for (cmGeneratorTarget::AllConfigSource const& source :
  4667. this->GeneratorTarget->GetAllConfigSources()) {
  4668. this->ClassifyAllConfigSource(source);
  4669. }
  4670. }
  4671. void cmVisualStudio10TargetGenerator::ClassifyAllConfigSource(
  4672. cmGeneratorTarget::AllConfigSource const& acs)
  4673. {
  4674. switch (acs.Kind) {
  4675. case cmGeneratorTarget::SourceKindResx: {
  4676. // Build and save the name of the corresponding .h file
  4677. // This relationship will be used later when building the project files.
  4678. // Both names would have been auto generated from Visual Studio
  4679. // where the user supplied the file name and Visual Studio
  4680. // appended the suffix.
  4681. std::string resx = acs.Source->ResolveFullPath();
  4682. std::string hFileName =
  4683. cmStrCat(resx.substr(0, resx.find_last_of('.')), ".h");
  4684. this->ExpectedResxHeaders.insert(hFileName);
  4685. } break;
  4686. case cmGeneratorTarget::SourceKindXaml: {
  4687. // Build and save the name of the corresponding .h and .cpp file
  4688. // This relationship will be used later when building the project files.
  4689. // Both names would have been auto generated from Visual Studio
  4690. // where the user supplied the file name and Visual Studio
  4691. // appended the suffix.
  4692. std::string xaml = acs.Source->ResolveFullPath();
  4693. std::string hFileName = cmStrCat(xaml, ".h");
  4694. std::string cppFileName = cmStrCat(xaml, ".cpp");
  4695. this->ExpectedXamlHeaders.insert(hFileName);
  4696. this->ExpectedXamlSources.insert(cppFileName);
  4697. } break;
  4698. default:
  4699. break;
  4700. }
  4701. }
  4702. bool cmVisualStudio10TargetGenerator::IsResxHeader(
  4703. const std::string& headerFile)
  4704. {
  4705. return this->ExpectedResxHeaders.count(headerFile) > 0;
  4706. }
  4707. bool cmVisualStudio10TargetGenerator::IsXamlHeader(
  4708. const std::string& headerFile)
  4709. {
  4710. return this->ExpectedXamlHeaders.count(headerFile) > 0;
  4711. }
  4712. bool cmVisualStudio10TargetGenerator::IsXamlSource(
  4713. const std::string& sourceFile)
  4714. {
  4715. return this->ExpectedXamlSources.count(sourceFile) > 0;
  4716. }
  4717. void cmVisualStudio10TargetGenerator::WriteApplicationTypeSettings(Elem& e1)
  4718. {
  4719. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  4720. bool isAppContainer = false;
  4721. bool const isWindowsPhone = this->GlobalGenerator->TargetsWindowsPhone();
  4722. bool const isWindowsStore = this->GlobalGenerator->TargetsWindowsStore();
  4723. bool const isAndroid = this->GlobalGenerator->TargetsAndroid();
  4724. std::string const& rev = this->GlobalGenerator->GetApplicationTypeRevision();
  4725. if (isWindowsPhone || isWindowsStore) {
  4726. e1.Element("ApplicationType",
  4727. (isWindowsPhone ? "Windows Phone" : "Windows Store"));
  4728. e1.Element("DefaultLanguage", "en-US");
  4729. if (rev == "10.0"_s) {
  4730. e1.Element("ApplicationTypeRevision", rev);
  4731. // Visual Studio 14.0 is necessary for building 10.0 apps
  4732. e1.Element("MinimumVisualStudioVersion", "14.0");
  4733. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4734. isAppContainer = true;
  4735. }
  4736. } else if (rev == "8.1"_s) {
  4737. e1.Element("ApplicationTypeRevision", rev);
  4738. // Visual Studio 12.0 is necessary for building 8.1 apps
  4739. e1.Element("MinimumVisualStudioVersion", "12.0");
  4740. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4741. isAppContainer = true;
  4742. }
  4743. } else if (rev == "8.0"_s) {
  4744. e1.Element("ApplicationTypeRevision", rev);
  4745. // Visual Studio 11.0 is necessary for building 8.0 apps
  4746. e1.Element("MinimumVisualStudioVersion", "11.0");
  4747. if (isWindowsStore &&
  4748. this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  4749. isAppContainer = true;
  4750. } else if (isWindowsPhone &&
  4751. this->GeneratorTarget->GetType() ==
  4752. cmStateEnums::EXECUTABLE) {
  4753. e1.Element("XapOutputs", "true");
  4754. e1.Element("XapFilename",
  4755. cmStrCat(this->Name, "_$(Configuration)_$(Platform).xap"));
  4756. }
  4757. }
  4758. } else if (isAndroid) {
  4759. e1.Element("ApplicationType", "Android");
  4760. e1.Element("ApplicationTypeRevision",
  4761. gg->GetAndroidApplicationTypeRevision());
  4762. }
  4763. if (isAppContainer) {
  4764. e1.Element("AppContainerApplication", "true");
  4765. } else if (!isAndroid) {
  4766. if (this->Platform == "ARM64"_s) {
  4767. e1.Element("WindowsSDKDesktopARM64Support", "true");
  4768. } else if (this->Platform == "ARM"_s) {
  4769. e1.Element("WindowsSDKDesktopARMSupport", "true");
  4770. }
  4771. }
  4772. std::string const& targetPlatformVersion =
  4773. gg->GetWindowsTargetPlatformVersion();
  4774. if (!targetPlatformVersion.empty()) {
  4775. e1.Element("WindowsTargetPlatformVersion", targetPlatformVersion);
  4776. }
  4777. cmValue targetPlatformMinVersion = this->GeneratorTarget->GetProperty(
  4778. "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION");
  4779. if (targetPlatformMinVersion) {
  4780. e1.Element("WindowsTargetPlatformMinVersion", *targetPlatformMinVersion);
  4781. } else if (isWindowsStore && rev == "10.0"_s) {
  4782. // If the min version is not set, then use the TargetPlatformVersion
  4783. if (!targetPlatformVersion.empty()) {
  4784. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformVersion);
  4785. }
  4786. }
  4787. // Added IoT Startup Task support
  4788. if (this->GeneratorTarget->GetPropertyAsBool("VS_IOT_STARTUP_TASK")) {
  4789. e1.Element("ContainsStartupTask", "true");
  4790. }
  4791. }
  4792. void cmVisualStudio10TargetGenerator::VerifyNecessaryFiles()
  4793. {
  4794. // For Windows and Windows Phone executables, we will assume that if a
  4795. // manifest is not present that we need to add all the necessary files
  4796. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4797. std::vector<cmGeneratorTarget::AllConfigSource> manifestSources =
  4798. this->GeneratorTarget->GetAllConfigSources(
  4799. cmGeneratorTarget::SourceKindAppManifest);
  4800. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4801. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4802. if (v == "8.0"_s) {
  4803. // Look through the sources for WMAppManifest.xml
  4804. bool foundManifest = false;
  4805. for (cmGeneratorTarget::AllConfigSource const& source :
  4806. this->GeneratorTarget->GetAllConfigSources()) {
  4807. if (source.Kind == cmGeneratorTarget::SourceKindExtra &&
  4808. "wmappmanifest.xml" ==
  4809. cmSystemTools::LowerCase(
  4810. source.Source->GetLocation().GetName())) {
  4811. foundManifest = true;
  4812. break;
  4813. }
  4814. }
  4815. if (!foundManifest) {
  4816. this->IsMissingFiles = true;
  4817. }
  4818. } else if (v == "8.1"_s) {
  4819. if (manifestSources.empty()) {
  4820. this->IsMissingFiles = true;
  4821. }
  4822. }
  4823. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4824. if (manifestSources.empty()) {
  4825. if (v == "8.0"_s) {
  4826. this->IsMissingFiles = true;
  4827. } else if (v == "8.1"_s || cmHasLiteralPrefix(v, "10.0")) {
  4828. this->IsMissingFiles = true;
  4829. }
  4830. }
  4831. }
  4832. }
  4833. }
  4834. void cmVisualStudio10TargetGenerator::WriteMissingFiles(Elem& e1)
  4835. {
  4836. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4837. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4838. if (v == "8.0"_s) {
  4839. this->WriteMissingFilesWP80(e1);
  4840. } else if (v == "8.1"_s) {
  4841. this->WriteMissingFilesWP81(e1);
  4842. }
  4843. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4844. if (v == "8.0"_s) {
  4845. this->WriteMissingFilesWS80(e1);
  4846. } else if (v == "8.1"_s) {
  4847. this->WriteMissingFilesWS81(e1);
  4848. } else if (cmHasLiteralPrefix(v, "10.0")) {
  4849. this->WriteMissingFilesWS10_0(e1);
  4850. }
  4851. }
  4852. }
  4853. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP80(Elem& e1)
  4854. {
  4855. std::string templateFolder =
  4856. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/Windows");
  4857. // For WP80, the manifest needs to be in the same folder as the project
  4858. // this can cause an overwrite problem if projects aren't organized in
  4859. // folders
  4860. std::string manifestFile = cmStrCat(
  4861. this->LocalGenerator->GetCurrentBinaryDirectory(), "/WMAppManifest.xml");
  4862. std::string artifactDir =
  4863. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4864. ConvertToWindowsSlash(artifactDir);
  4865. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4866. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4867. cmGeneratedFileStream fout(manifestFile);
  4868. fout.SetCopyIfDifferent(true);
  4869. /* clang-format off */
  4870. fout <<
  4871. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4872. "<Deployment"
  4873. " xmlns=\"http://schemas.microsoft.com/windowsphone/2012/deployment\""
  4874. " AppPlatformVersion=\"8.0\">\n"
  4875. "\t<DefaultLanguage xmlns=\"\" code=\"en-US\"/>\n"
  4876. "\t<App xmlns=\"\" ProductID=\"{" << this->GUID << "}\""
  4877. " Title=\"CMake Test Program\" RuntimeType=\"Modern Native\""
  4878. " Version=\"1.0.0.0\" Genre=\"apps.normal\" Author=\"CMake\""
  4879. " Description=\"Default CMake App\" Publisher=\"CMake\""
  4880. " PublisherID=\"{" << this->GUID << "}\">\n"
  4881. "\t\t<IconPath IsRelative=\"true\" IsResource=\"false\">"
  4882. << artifactDirXML << "\\ApplicationIcon.png</IconPath>\n"
  4883. "\t\t<Capabilities/>\n"
  4884. "\t\t<Tasks>\n"
  4885. "\t\t\t<DefaultTask Name=\"_default\""
  4886. " ImagePath=\"" << targetNameXML << ".exe\" ImageParams=\"\" />\n"
  4887. "\t\t</Tasks>\n"
  4888. "\t\t<Tokens>\n"
  4889. "\t\t\t<PrimaryToken TokenID=\"" << targetNameXML << "Token\""
  4890. " TaskName=\"_default\">\n"
  4891. "\t\t\t\t<TemplateFlip>\n"
  4892. "\t\t\t\t\t<SmallImageURI IsRelative=\"true\" IsResource=\"false\">"
  4893. << artifactDirXML << "\\SmallLogo.png</SmallImageURI>\n"
  4894. "\t\t\t\t\t<Count>0</Count>\n"
  4895. "\t\t\t\t\t<BackgroundImageURI IsRelative=\"true\" IsResource=\"false\">"
  4896. << artifactDirXML << "\\Logo.png</BackgroundImageURI>\n"
  4897. "\t\t\t\t</TemplateFlip>\n"
  4898. "\t\t\t</PrimaryToken>\n"
  4899. "\t\t</Tokens>\n"
  4900. "\t\t<ScreenResolutions>\n"
  4901. "\t\t\t<ScreenResolution Name=\"ID_RESOLUTION_WVGA\" />\n"
  4902. "\t\t</ScreenResolutions>\n"
  4903. "\t</App>\n"
  4904. "</Deployment>\n";
  4905. /* clang-format on */
  4906. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4907. ConvertToWindowsSlash(sourceFile);
  4908. {
  4909. Elem e2(e1, "Xml");
  4910. e2.Attribute("Include", sourceFile);
  4911. e2.Element("SubType", "Designer");
  4912. }
  4913. this->AddedFiles.push_back(sourceFile);
  4914. std::string smallLogo = cmStrCat(this->DefaultArtifactDir, "/SmallLogo.png");
  4915. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SmallLogo.png"),
  4916. smallLogo, false);
  4917. ConvertToWindowsSlash(smallLogo);
  4918. Elem(e1, "Image").Attribute("Include", smallLogo);
  4919. this->AddedFiles.push_back(smallLogo);
  4920. std::string logo = cmStrCat(this->DefaultArtifactDir, "/Logo.png");
  4921. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/Logo.png"), logo, false);
  4922. ConvertToWindowsSlash(logo);
  4923. Elem(e1, "Image").Attribute("Include", logo);
  4924. this->AddedFiles.push_back(logo);
  4925. std::string applicationIcon =
  4926. cmStrCat(this->DefaultArtifactDir, "/ApplicationIcon.png");
  4927. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/ApplicationIcon.png"),
  4928. applicationIcon, false);
  4929. ConvertToWindowsSlash(applicationIcon);
  4930. Elem(e1, "Image").Attribute("Include", applicationIcon);
  4931. this->AddedFiles.push_back(applicationIcon);
  4932. }
  4933. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP81(Elem& e1)
  4934. {
  4935. std::string manifestFile =
  4936. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  4937. std::string artifactDir =
  4938. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4939. ConvertToWindowsSlash(artifactDir);
  4940. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4941. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  4942. cmGeneratedFileStream fout(manifestFile);
  4943. fout.SetCopyIfDifferent(true);
  4944. /* clang-format off */
  4945. fout <<
  4946. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4947. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4948. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\""
  4949. " xmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\">\n"
  4950. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4951. " Version=\"1.0.0.0\" />\n"
  4952. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID << "\""
  4953. " PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4954. "\t<Properties>\n"
  4955. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4956. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4957. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4958. "\t</Properties>\n"
  4959. "\t<Prerequisites>\n"
  4960. "\t\t<OSMinVersion>6.3.1</OSMinVersion>\n"
  4961. "\t\t<OSMaxVersionTested>6.3.1</OSMaxVersionTested>\n"
  4962. "\t</Prerequisites>\n"
  4963. "\t<Resources>\n"
  4964. "\t\t<Resource Language=\"x-generate\" />\n"
  4965. "\t</Resources>\n"
  4966. "\t<Applications>\n"
  4967. "\t\t<Application Id=\"App\""
  4968. " Executable=\"" << targetNameXML << ".exe\""
  4969. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4970. "\t\t\t<m2:VisualElements\n"
  4971. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4972. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4973. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4974. "\t\t\t\tForegroundText=\"light\"\n"
  4975. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4976. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4977. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4978. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4979. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4980. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4981. "\t\t\t\t</m2:DefaultTile>\n"
  4982. "\t\t\t\t<m2:SplashScreen"
  4983. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4984. "\t\t\t</m2:VisualElements>\n"
  4985. "\t\t</Application>\n"
  4986. "\t</Applications>\n"
  4987. "</Package>\n";
  4988. /* clang-format on */
  4989. this->WriteCommonMissingFiles(e1, manifestFile);
  4990. }
  4991. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS80(Elem& e1)
  4992. {
  4993. std::string manifestFile =
  4994. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  4995. std::string artifactDir =
  4996. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4997. ConvertToWindowsSlash(artifactDir);
  4998. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4999. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  5000. cmGeneratedFileStream fout(manifestFile);
  5001. fout.SetCopyIfDifferent(true);
  5002. /* clang-format off */
  5003. fout <<
  5004. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  5005. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\">\n"
  5006. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  5007. " Version=\"1.0.0.0\" />\n"
  5008. "\t<Properties>\n"
  5009. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  5010. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  5011. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  5012. "\t</Properties>\n"
  5013. "\t<Prerequisites>\n"
  5014. "\t\t<OSMinVersion>6.2.1</OSMinVersion>\n"
  5015. "\t\t<OSMaxVersionTested>6.2.1</OSMaxVersionTested>\n"
  5016. "\t</Prerequisites>\n"
  5017. "\t<Resources>\n"
  5018. "\t\t<Resource Language=\"x-generate\" />\n"
  5019. "\t</Resources>\n"
  5020. "\t<Applications>\n"
  5021. "\t\t<Application Id=\"App\""
  5022. " Executable=\"" << targetNameXML << ".exe\""
  5023. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  5024. "\t\t\t<VisualElements"
  5025. " DisplayName=\"" << targetNameXML << "\""
  5026. " Description=\"" << targetNameXML << "\""
  5027. " BackgroundColor=\"#336699\" ForegroundText=\"light\""
  5028. " Logo=\"" << artifactDirXML << "\\Logo.png\""
  5029. " SmallLogo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  5030. "\t\t\t\t<DefaultTile ShowName=\"allLogos\""
  5031. " ShortName=\"" << targetNameXML << "\" />\n"
  5032. "\t\t\t\t<SplashScreen"
  5033. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  5034. "\t\t\t</VisualElements>\n"
  5035. "\t\t</Application>\n"
  5036. "\t</Applications>\n"
  5037. "</Package>\n";
  5038. /* clang-format on */
  5039. this->WriteCommonMissingFiles(e1, manifestFile);
  5040. }
  5041. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS81(Elem& e1)
  5042. {
  5043. std::string manifestFile =
  5044. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  5045. std::string artifactDir =
  5046. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  5047. ConvertToWindowsSlash(artifactDir);
  5048. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  5049. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  5050. cmGeneratedFileStream fout(manifestFile);
  5051. fout.SetCopyIfDifferent(true);
  5052. /* clang-format off */
  5053. fout <<
  5054. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  5055. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  5056. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\">\n"
  5057. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  5058. " Version=\"1.0.0.0\" />\n"
  5059. "\t<Properties>\n"
  5060. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  5061. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  5062. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  5063. "\t</Properties>\n"
  5064. "\t<Prerequisites>\n"
  5065. "\t\t<OSMinVersion>6.3</OSMinVersion>\n"
  5066. "\t\t<OSMaxVersionTested>6.3</OSMaxVersionTested>\n"
  5067. "\t</Prerequisites>\n"
  5068. "\t<Resources>\n"
  5069. "\t\t<Resource Language=\"x-generate\" />\n"
  5070. "\t</Resources>\n"
  5071. "\t<Applications>\n"
  5072. "\t\t<Application Id=\"App\""
  5073. " Executable=\"" << targetNameXML << ".exe\""
  5074. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  5075. "\t\t\t<m2:VisualElements\n"
  5076. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  5077. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  5078. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  5079. "\t\t\t\tForegroundText=\"light\"\n"
  5080. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  5081. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  5082. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  5083. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  5084. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  5085. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  5086. "\t\t\t\t</m2:DefaultTile>\n"
  5087. "\t\t\t\t<m2:SplashScreen"
  5088. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  5089. "\t\t\t</m2:VisualElements>\n"
  5090. "\t\t</Application>\n"
  5091. "\t</Applications>\n"
  5092. "</Package>\n";
  5093. /* clang-format on */
  5094. this->WriteCommonMissingFiles(e1, manifestFile);
  5095. }
  5096. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS10_0(Elem& e1)
  5097. {
  5098. std::string manifestFile =
  5099. cmStrCat(this->DefaultArtifactDir, "/package.appxManifest");
  5100. std::string artifactDir =
  5101. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  5102. ConvertToWindowsSlash(artifactDir);
  5103. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  5104. const std::string& targetNameXML = cmVS10EscapeXML(GetTargetOutputName());
  5105. cmGeneratedFileStream fout(manifestFile);
  5106. fout.SetCopyIfDifferent(true);
  5107. /* clang-format off */
  5108. fout <<
  5109. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  5110. "<Package\n\t"
  5111. "xmlns=\"http://schemas.microsoft.com/appx/manifest/foundation/windows10\""
  5112. "\txmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\"\n"
  5113. "\txmlns:uap=\"http://schemas.microsoft.com/appx/manifest/uap/windows10\""
  5114. "\n\tIgnorableNamespaces=\"uap mp\">\n\n"
  5115. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  5116. " Version=\"1.0.0.0\" />\n"
  5117. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID <<
  5118. "\" PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  5119. "\t<Properties>\n"
  5120. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  5121. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  5122. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  5123. "\t</Properties>\n"
  5124. "\t<Dependencies>\n"
  5125. "\t\t<TargetDeviceFamily Name=\"Windows.Universal\" "
  5126. "MinVersion=\"10.0.0.0\" MaxVersionTested=\"10.0.0.0\" />\n"
  5127. "\t</Dependencies>\n"
  5128. "\t<Resources>\n"
  5129. "\t\t<Resource Language=\"x-generate\" />\n"
  5130. "\t</Resources>\n"
  5131. "\t<Applications>\n"
  5132. "\t\t<Application Id=\"App\""
  5133. " Executable=\"" << targetNameXML << ".exe\""
  5134. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  5135. "\t\t\t<uap:VisualElements\n"
  5136. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  5137. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  5138. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  5139. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  5140. "\t\t\t\tSquare44x44Logo=\"" << artifactDirXML <<
  5141. "\\SmallLogo44x44.png\">\n"
  5142. "\t\t\t\t<uap:SplashScreen"
  5143. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  5144. "\t\t\t</uap:VisualElements>\n"
  5145. "\t\t</Application>\n"
  5146. "\t</Applications>\n"
  5147. "</Package>\n";
  5148. /* clang-format on */
  5149. this->WriteCommonMissingFiles(e1, manifestFile);
  5150. }
  5151. void cmVisualStudio10TargetGenerator::WriteCommonMissingFiles(
  5152. Elem& e1, const std::string& manifestFile)
  5153. {
  5154. std::string templateFolder =
  5155. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Templates/Windows");
  5156. std::string sourceFile = this->ConvertPath(manifestFile, false);
  5157. ConvertToWindowsSlash(sourceFile);
  5158. {
  5159. Elem e2(e1, "AppxManifest");
  5160. e2.Attribute("Include", sourceFile);
  5161. e2.Element("SubType", "Designer");
  5162. }
  5163. this->AddedFiles.push_back(sourceFile);
  5164. std::string smallLogo = cmStrCat(this->DefaultArtifactDir, "/SmallLogo.png");
  5165. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SmallLogo.png"),
  5166. smallLogo, false);
  5167. ConvertToWindowsSlash(smallLogo);
  5168. Elem(e1, "Image").Attribute("Include", smallLogo);
  5169. this->AddedFiles.push_back(smallLogo);
  5170. std::string smallLogo44 =
  5171. cmStrCat(this->DefaultArtifactDir, "/SmallLogo44x44.png");
  5172. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SmallLogo44x44.png"),
  5173. smallLogo44, false);
  5174. ConvertToWindowsSlash(smallLogo44);
  5175. Elem(e1, "Image").Attribute("Include", smallLogo44);
  5176. this->AddedFiles.push_back(smallLogo44);
  5177. std::string logo = cmStrCat(this->DefaultArtifactDir, "/Logo.png");
  5178. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/Logo.png"), logo, false);
  5179. ConvertToWindowsSlash(logo);
  5180. Elem(e1, "Image").Attribute("Include", logo);
  5181. this->AddedFiles.push_back(logo);
  5182. std::string storeLogo = cmStrCat(this->DefaultArtifactDir, "/StoreLogo.png");
  5183. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/StoreLogo.png"),
  5184. storeLogo, false);
  5185. ConvertToWindowsSlash(storeLogo);
  5186. Elem(e1, "Image").Attribute("Include", storeLogo);
  5187. this->AddedFiles.push_back(storeLogo);
  5188. std::string splashScreen =
  5189. cmStrCat(this->DefaultArtifactDir, "/SplashScreen.png");
  5190. cmSystemTools::CopyAFile(cmStrCat(templateFolder, "/SplashScreen.png"),
  5191. splashScreen, false);
  5192. ConvertToWindowsSlash(splashScreen);
  5193. Elem(e1, "Image").Attribute("Include", splashScreen);
  5194. this->AddedFiles.push_back(splashScreen);
  5195. if (this->AddedDefaultCertificate) {
  5196. // This file has already been added to the build so don't copy it
  5197. std::string keyFile =
  5198. cmStrCat(this->DefaultArtifactDir, "/Windows_TemporaryKey.pfx");
  5199. ConvertToWindowsSlash(keyFile);
  5200. Elem(e1, "None").Attribute("Include", keyFile);
  5201. }
  5202. }
  5203. bool cmVisualStudio10TargetGenerator::ForceOld(const std::string& source) const
  5204. {
  5205. HANDLE h =
  5206. CreateFileW(cmSystemTools::ConvertToWindowsExtendedPath(source).c_str(),
  5207. FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING,
  5208. FILE_FLAG_BACKUP_SEMANTICS, 0);
  5209. if (!h) {
  5210. return false;
  5211. }
  5212. FILETIME const ftime_20010101 = { 3365781504u, 29389701u };
  5213. if (!SetFileTime(h, &ftime_20010101, &ftime_20010101, &ftime_20010101)) {
  5214. CloseHandle(h);
  5215. return false;
  5216. }
  5217. CloseHandle(h);
  5218. return true;
  5219. }
  5220. void cmVisualStudio10TargetGenerator::GetCSharpSourceProperties(
  5221. cmSourceFile const* sf, std::map<std::string, std::string>& tags)
  5222. {
  5223. if (this->ProjectType == VsProjectType::csproj) {
  5224. const cmPropertyMap& props = sf->GetProperties();
  5225. for (const std::string& p : props.GetKeys()) {
  5226. static const cm::string_view propNamePrefix = "VS_CSHARP_";
  5227. if (cmHasPrefix(p, propNamePrefix)) {
  5228. std::string tagName = p.substr(propNamePrefix.length());
  5229. if (!tagName.empty()) {
  5230. cmValue val = props.GetPropertyValue(p);
  5231. if (cmNonempty(val)) {
  5232. tags[tagName] = *val;
  5233. } else {
  5234. tags.erase(tagName);
  5235. }
  5236. }
  5237. }
  5238. }
  5239. }
  5240. }
  5241. void cmVisualStudio10TargetGenerator::WriteCSharpSourceProperties(
  5242. Elem& e2, const std::map<std::string, std::string>& tags)
  5243. {
  5244. for (const auto& i : tags) {
  5245. e2.Element(i.first, i.second);
  5246. }
  5247. }
  5248. std::string cmVisualStudio10TargetGenerator::GetCSharpSourceLink(
  5249. cmSourceFile const* source)
  5250. {
  5251. // For out of source files, we first check if a matching source group
  5252. // for this file exists, otherwise we check if the path relative to current
  5253. // source- or binary-dir is used within the link and return that.
  5254. // In case of .cs files we can't do that automatically for files in the
  5255. // binary directory, because this leads to compilation errors.
  5256. std::string link;
  5257. std::string sourceGroupedFile;
  5258. std::string const& fullFileName = source->GetFullPath();
  5259. std::string const& srcDir = this->Makefile->GetCurrentSourceDirectory();
  5260. std::string const& binDir = this->Makefile->GetCurrentBinaryDirectory();
  5261. // unfortunately we have to copy the source groups, because
  5262. // FindSourceGroup uses a regex which is modifying the group
  5263. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  5264. cmSourceGroup* sourceGroup =
  5265. this->Makefile->FindSourceGroup(fullFileName, sourceGroups);
  5266. if (sourceGroup && !sourceGroup->GetFullName().empty()) {
  5267. sourceGroupedFile =
  5268. cmStrCat(sourceGroup->GetFullName(), '/',
  5269. cmsys::SystemTools::GetFilenameName(fullFileName));
  5270. cmsys::SystemTools::ConvertToUnixSlashes(sourceGroupedFile);
  5271. }
  5272. if (!sourceGroupedFile.empty() &&
  5273. cmHasSuffix(fullFileName, sourceGroupedFile)) {
  5274. link = sourceGroupedFile;
  5275. } else if (cmHasPrefix(fullFileName, srcDir)) {
  5276. link = fullFileName.substr(srcDir.length() + 1);
  5277. } else if (!cmHasSuffix(fullFileName, ".cs") &&
  5278. cmHasPrefix(fullFileName, binDir)) {
  5279. link = fullFileName.substr(binDir.length() + 1);
  5280. } else if (cmValue l = source->GetProperty("VS_CSHARP_Link")) {
  5281. link = *l;
  5282. }
  5283. ConvertToWindowsSlash(link);
  5284. return link;
  5285. }
  5286. std::string cmVisualStudio10TargetGenerator::GetCMakeFilePath(
  5287. const char* relativeFilePath) const
  5288. {
  5289. // Always search in the standard modules location.
  5290. std::string path =
  5291. cmStrCat(cmSystemTools::GetCMakeRoot(), '/', relativeFilePath);
  5292. ConvertToWindowsSlash(path);
  5293. return path;
  5294. }
  5295. void cmVisualStudio10TargetGenerator::WriteStdOutEncodingUtf8(Elem& e1)
  5296. {
  5297. if (this->GlobalGenerator->IsUtf8EncodingSupported()) {
  5298. e1.Element("UseUtf8Encoding", "Always");
  5299. } else if (this->GlobalGenerator->IsStdOutEncodingSupported()) {
  5300. e1.Element("StdOutEncoding", "UTF-8");
  5301. }
  5302. }
  5303. void cmVisualStudio10TargetGenerator::UpdateCache()
  5304. {
  5305. std::vector<std::string> packageReferences;
  5306. if (this->GeneratorTarget->HasPackageReferences()) {
  5307. // Store a cache entry that later determines, if a package restore is
  5308. // required.
  5309. this->GeneratorTarget->Makefile->AddCacheDefinition(
  5310. cmStrCat(this->GeneratorTarget->GetName(),
  5311. "_REQUIRES_VS_PACKAGE_RESTORE"),
  5312. "ON", "Value Computed by CMake", cmStateEnums::STATIC);
  5313. } else {
  5314. // If there are any dependencies that require package restore, inherit the
  5315. // cache variable.
  5316. cmGlobalGenerator::TargetDependSet const& unordered =
  5317. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  5318. using OrderedTargetDependSet =
  5319. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  5320. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  5321. for (cmGeneratorTarget const* dt : depends) {
  5322. if (dt->HasPackageReferences()) {
  5323. this->GeneratorTarget->Makefile->AddCacheDefinition(
  5324. cmStrCat(this->GeneratorTarget->GetName(),
  5325. "_REQUIRES_VS_PACKAGE_RESTORE"),
  5326. "ON", "Value Computed by CMake", cmStateEnums::STATIC);
  5327. }
  5328. }
  5329. }
  5330. }