cmVisualStudio10TargetGenerator.cxx 208 KB

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