cmVisualStudio10TargetGenerator.cxx 177 KB

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