cmVisualStudio10TargetGenerator.cxx 172 KB

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