cmVisualStudio10TargetGenerator.cxx 202 KB

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