cmVisualStudio10TargetGenerator.cxx 175 KB

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