cmVisualStudio10TargetGenerator.cxx 204 KB

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