cmVisualStudio10TargetGenerator.cxx 207 KB

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