cmVisualStudio10TargetGenerator.cxx 172 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883
  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. bool symbolic = false;
  1335. // input files for custom command
  1336. std::stringstream additional_inputs;
  1337. {
  1338. const char* sep = "";
  1339. if (this->ProjectType == csproj) {
  1340. // csproj files do not attach the command to a specific file
  1341. // so the primary input must be listed explicitly.
  1342. additional_inputs << source->GetFullPath();
  1343. sep = ";";
  1344. }
  1345. // Avoid listing an input more than once.
  1346. std::set<std::string> unique_inputs;
  1347. // The source is either implicit an input or has been added above.
  1348. unique_inputs.insert(source->GetFullPath());
  1349. for (std::string const& d : ccg.GetDepends()) {
  1350. std::string dep;
  1351. if (lg->GetRealDependency(d, c, dep)) {
  1352. if (!unique_inputs.insert(dep).second) {
  1353. // already listed
  1354. continue;
  1355. }
  1356. ConvertToWindowsSlash(dep);
  1357. additional_inputs << sep << dep;
  1358. sep = ";";
  1359. if (!symbolic) {
  1360. if (cmSourceFile* sf = this->Makefile->GetSource(
  1361. dep, cmSourceFileLocationKind::Known)) {
  1362. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1363. }
  1364. }
  1365. }
  1366. }
  1367. if (this->ProjectType != csproj) {
  1368. additional_inputs << sep << "%(AdditionalInputs)";
  1369. }
  1370. }
  1371. // output files for custom command
  1372. std::stringstream outputs;
  1373. {
  1374. const char* sep = "";
  1375. for (std::string const& o : ccg.GetOutputs()) {
  1376. std::string out = o;
  1377. ConvertToWindowsSlash(out);
  1378. outputs << sep << out;
  1379. sep = ";";
  1380. if (!symbolic) {
  1381. if (cmSourceFile* sf = this->Makefile->GetSource(
  1382. o, cmSourceFileLocationKind::Known)) {
  1383. symbolic = sf->GetPropertyAsBool("SYMBOLIC");
  1384. }
  1385. }
  1386. }
  1387. }
  1388. if (this->ProjectType == csproj) {
  1389. std::string name = "CustomCommand_" + c + "_" +
  1390. cmSystemTools::ComputeStringMD5(sourcePath);
  1391. this->WriteCustomRuleCSharp(e0, c, name, script, additional_inputs.str(),
  1392. outputs.str(), comment);
  1393. } else {
  1394. this->WriteCustomRuleCpp(*spe2, c, script, additional_inputs.str(),
  1395. outputs.str(), comment, symbolic);
  1396. }
  1397. }
  1398. }
  1399. void cmVisualStudio10TargetGenerator::WriteCustomRuleCpp(
  1400. Elem& e2, std::string const& config, std::string const& script,
  1401. std::string const& additional_inputs, std::string const& outputs,
  1402. std::string const& comment, bool symbolic)
  1403. {
  1404. const std::string cond = this->CalcCondition(config);
  1405. e2.WritePlatformConfigTag("Message", cond, comment);
  1406. e2.WritePlatformConfigTag("Command", cond, script);
  1407. e2.WritePlatformConfigTag("AdditionalInputs", cond, additional_inputs);
  1408. e2.WritePlatformConfigTag("Outputs", cond, outputs);
  1409. if (this->LocalGenerator->GetVersion() >
  1410. cmGlobalVisualStudioGenerator::VS10) {
  1411. // VS >= 11 let us turn off linking of custom command outputs.
  1412. e2.WritePlatformConfigTag("LinkObjects", cond, "false");
  1413. }
  1414. if (symbolic &&
  1415. this->LocalGenerator->GetVersion() >=
  1416. cmGlobalVisualStudioGenerator::VS16) {
  1417. // VS >= 16.4 warn if outputs are not created, but one of our
  1418. // outputs is marked SYMBOLIC and not expected to be created.
  1419. e2.WritePlatformConfigTag("VerifyInputsAndOutputsExist", cond, "false");
  1420. }
  1421. }
  1422. void cmVisualStudio10TargetGenerator::WriteCustomRuleCSharp(
  1423. Elem& e0, std::string const& config, std::string const& name,
  1424. std::string const& script, std::string const& inputs,
  1425. std::string const& outputs, std::string const& comment)
  1426. {
  1427. this->CSharpCustomCommandNames.insert(name);
  1428. Elem e1(e0, "Target");
  1429. e1.Attribute("Condition", this->CalcCondition(config));
  1430. e1.S << "\n Name=\"" << name << "\"";
  1431. e1.S << "\n Inputs=\"" << cmVS10EscapeAttr(inputs) << "\"";
  1432. e1.S << "\n Outputs=\"" << cmVS10EscapeAttr(outputs) << "\"";
  1433. if (!comment.empty()) {
  1434. Elem(e1, "Exec").Attribute("Command", "echo " + comment);
  1435. }
  1436. Elem(e1, "Exec").Attribute("Command", script);
  1437. }
  1438. std::string cmVisualStudio10TargetGenerator::ConvertPath(
  1439. std::string const& path, bool forceRelative)
  1440. {
  1441. return forceRelative
  1442. ? cmSystemTools::RelativePath(
  1443. this->LocalGenerator->GetCurrentBinaryDirectory(), path)
  1444. : path;
  1445. }
  1446. static void ConvertToWindowsSlash(std::string& s)
  1447. {
  1448. // first convert all of the slashes
  1449. for (auto& ch : s) {
  1450. if (ch == '/') {
  1451. ch = '\\';
  1452. }
  1453. }
  1454. }
  1455. void cmVisualStudio10TargetGenerator::WriteGroups()
  1456. {
  1457. if (this->ProjectType == csproj) {
  1458. return;
  1459. }
  1460. // collect up group information
  1461. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  1462. std::vector<cmGeneratorTarget::AllConfigSource> const& sources =
  1463. this->GeneratorTarget->GetAllConfigSources();
  1464. std::set<cmSourceGroup const*> groupsUsed;
  1465. for (cmGeneratorTarget::AllConfigSource const& si : sources) {
  1466. std::string const& source = si.Source->GetFullPath();
  1467. cmSourceGroup* sourceGroup =
  1468. this->Makefile->FindSourceGroup(source, sourceGroups);
  1469. groupsUsed.insert(sourceGroup);
  1470. }
  1471. if (cmSourceFile const* srcCMakeLists =
  1472. this->LocalGenerator->CreateVCProjBuildRule()) {
  1473. std::string const& source = srcCMakeLists->GetFullPath();
  1474. cmSourceGroup* sourceGroup =
  1475. this->Makefile->FindSourceGroup(source, sourceGroups);
  1476. groupsUsed.insert(sourceGroup);
  1477. }
  1478. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  1479. // Write out group file
  1480. std::string path = cmStrCat(
  1481. this->LocalGenerator->GetCurrentBinaryDirectory(), '/', this->Name,
  1482. computeProjectFileExtension(this->GeneratorTarget), ".filters");
  1483. cmGeneratedFileStream fout(path);
  1484. fout.SetCopyIfDifferent(true);
  1485. char magic[] = { char(0xEF), char(0xBB), char(0xBF) };
  1486. fout.write(magic, 3);
  1487. fout << "<?xml version=\"1.0\" encoding=\""
  1488. << this->GlobalGenerator->Encoding() << "\"?>"
  1489. << "\n";
  1490. {
  1491. Elem e0(fout, "Project");
  1492. e0.Attribute("ToolsVersion", this->GlobalGenerator->GetToolsVersion());
  1493. e0.Attribute("xmlns",
  1494. "http://schemas.microsoft.com/developer/msbuild/2003");
  1495. for (auto const& ti : this->Tools) {
  1496. this->WriteGroupSources(e0, ti.first, ti.second, sourceGroups);
  1497. }
  1498. // Added files are images and the manifest.
  1499. if (!this->AddedFiles.empty()) {
  1500. Elem e1(e0, "ItemGroup");
  1501. e1.SetHasElements();
  1502. for (std::string const& oi : this->AddedFiles) {
  1503. std::string fileName =
  1504. cmSystemTools::LowerCase(cmSystemTools::GetFilenameName(oi));
  1505. if (fileName == "wmappmanifest.xml") {
  1506. Elem e2(e1, "XML");
  1507. e2.Attribute("Include", oi);
  1508. e2.Element("Filter", "Resource Files");
  1509. } else if (cmSystemTools::GetFilenameExtension(fileName) ==
  1510. ".appxmanifest") {
  1511. Elem e2(e1, "AppxManifest");
  1512. e2.Attribute("Include", oi);
  1513. e2.Element("Filter", "Resource Files");
  1514. } else if (cmSystemTools::GetFilenameExtension(fileName) == ".pfx") {
  1515. Elem e2(e1, "None");
  1516. e2.Attribute("Include", oi);
  1517. e2.Element("Filter", "Resource Files");
  1518. } else {
  1519. Elem e2(e1, "Image");
  1520. e2.Attribute("Include", oi);
  1521. e2.Element("Filter", "Resource Files");
  1522. }
  1523. }
  1524. }
  1525. std::vector<cmSourceFile const*> resxObjs;
  1526. this->GeneratorTarget->GetResxSources(resxObjs, "");
  1527. if (!resxObjs.empty()) {
  1528. Elem e1(e0, "ItemGroup");
  1529. for (cmSourceFile const* oi : resxObjs) {
  1530. std::string obj = oi->GetFullPath();
  1531. ConvertToWindowsSlash(obj);
  1532. Elem e2(e1, "EmbeddedResource");
  1533. e2.Attribute("Include", obj);
  1534. e2.Element("Filter", "Resource Files");
  1535. }
  1536. }
  1537. {
  1538. Elem e1(e0, "ItemGroup");
  1539. e1.SetHasElements();
  1540. std::vector<cmSourceGroup const*> groupsVec(groupsUsed.begin(),
  1541. groupsUsed.end());
  1542. std::sort(groupsVec.begin(), groupsVec.end(),
  1543. [](cmSourceGroup const* l, cmSourceGroup const* r) {
  1544. return l->GetFullName() < r->GetFullName();
  1545. });
  1546. for (cmSourceGroup const* sg : groupsVec) {
  1547. std::string const& name = sg->GetFullName();
  1548. if (!name.empty()) {
  1549. std::string guidName = "SG_Filter_" + name;
  1550. std::string guid = this->GlobalGenerator->GetGUID(guidName);
  1551. Elem e2(e1, "Filter");
  1552. e2.Attribute("Include", name);
  1553. e2.Element("UniqueIdentifier", "{" + guid + "}");
  1554. }
  1555. }
  1556. if (!resxObjs.empty() || !this->AddedFiles.empty()) {
  1557. std::string guidName = "SG_Filter_Resource Files";
  1558. std::string guid = this->GlobalGenerator->GetGUID(guidName);
  1559. Elem e2(e1, "Filter");
  1560. e2.Attribute("Include", "Resource Files");
  1561. e2.Element("UniqueIdentifier", "{" + guid + "}");
  1562. e2.Element("Extensions",
  1563. "rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;"
  1564. "gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms");
  1565. }
  1566. }
  1567. }
  1568. fout << '\n';
  1569. if (fout.Close()) {
  1570. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  1571. }
  1572. }
  1573. // Add to groupsUsed empty source groups that have non-empty children.
  1574. void cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  1575. std::set<cmSourceGroup const*>& groupsUsed,
  1576. const std::vector<cmSourceGroup>& allGroups)
  1577. {
  1578. for (cmSourceGroup const& current : allGroups) {
  1579. std::vector<cmSourceGroup> const& children = current.GetGroupChildren();
  1580. if (children.empty()) {
  1581. continue; // the group is really empty
  1582. }
  1583. this->AddMissingSourceGroups(groupsUsed, children);
  1584. if (groupsUsed.count(&current) > 0) {
  1585. continue; // group has already been added to set
  1586. }
  1587. // check if it least one of the group's descendants is not empty
  1588. // (at least one child must already have been added)
  1589. auto child_it = children.begin();
  1590. while (child_it != children.end()) {
  1591. if (groupsUsed.count(&(*child_it)) > 0) {
  1592. break; // found a child that was already added => add current group too
  1593. }
  1594. child_it++;
  1595. }
  1596. if (child_it == children.end()) {
  1597. continue; // no descendants have source files => ignore this group
  1598. }
  1599. groupsUsed.insert(&current);
  1600. }
  1601. }
  1602. void cmVisualStudio10TargetGenerator::WriteGroupSources(
  1603. Elem& e0, std::string const& name, ToolSources const& sources,
  1604. std::vector<cmSourceGroup>& sourceGroups)
  1605. {
  1606. Elem e1(e0, "ItemGroup");
  1607. e1.SetHasElements();
  1608. for (ToolSource const& s : sources) {
  1609. cmSourceFile const* sf = s.SourceFile;
  1610. std::string const& source = sf->GetFullPath();
  1611. cmSourceGroup* sourceGroup =
  1612. this->Makefile->FindSourceGroup(source, sourceGroups);
  1613. std::string const& filter = sourceGroup->GetFullName();
  1614. std::string path = this->ConvertPath(source, s.RelativePath);
  1615. ConvertToWindowsSlash(path);
  1616. Elem e2(e1, name.c_str());
  1617. e2.Attribute("Include", path);
  1618. if (!filter.empty()) {
  1619. e2.Element("Filter", filter);
  1620. }
  1621. }
  1622. }
  1623. void cmVisualStudio10TargetGenerator::WriteHeaderSource(Elem& e1,
  1624. cmSourceFile const* sf)
  1625. {
  1626. std::string const& fileName = sf->GetFullPath();
  1627. Elem e2(e1, "ClInclude");
  1628. this->WriteSource(e2, sf);
  1629. if (this->IsResxHeader(fileName)) {
  1630. e2.Element("FileType", "CppForm");
  1631. } else if (this->IsXamlHeader(fileName)) {
  1632. std::string xamlFileName = fileName.substr(0, fileName.find_last_of("."));
  1633. e2.Element("DependentUpon", xamlFileName);
  1634. }
  1635. }
  1636. void cmVisualStudio10TargetGenerator::WriteExtraSource(Elem& e1,
  1637. cmSourceFile const* sf)
  1638. {
  1639. bool toolHasSettings = false;
  1640. const char* tool = "None";
  1641. std::string shaderType;
  1642. std::string shaderEntryPoint;
  1643. std::string shaderModel;
  1644. std::string shaderAdditionalFlags;
  1645. std::string shaderDisableOptimizations;
  1646. std::string shaderEnableDebug;
  1647. std::string shaderObjectFileName;
  1648. std::string outputHeaderFile;
  1649. std::string variableName;
  1650. std::string settingsGenerator;
  1651. std::string settingsLastGenOutput;
  1652. std::string sourceLink;
  1653. std::string subType;
  1654. std::string copyToOutDir;
  1655. std::string includeInVsix;
  1656. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  1657. if (this->ProjectType == csproj) {
  1658. // EVERY extra source file must have a <Link>, otherwise it might not
  1659. // be visible in Visual Studio at all. The path relative to current
  1660. // source- or binary-dir is used within the link, if the file is
  1661. // in none of these paths, it is added with the plain filename without
  1662. // any path. This means the file will show up at root-level of the csproj
  1663. // (where CMakeLists.txt etc. are).
  1664. if (!this->InSourceBuild) {
  1665. toolHasSettings = true;
  1666. std::string fullFileName = sf->GetFullPath();
  1667. std::string srcDir = this->Makefile->GetCurrentSourceDirectory();
  1668. std::string binDir = this->Makefile->GetCurrentBinaryDirectory();
  1669. if (fullFileName.find(binDir) != std::string::npos) {
  1670. sourceLink.clear();
  1671. } else if (fullFileName.find(srcDir) != std::string::npos) {
  1672. sourceLink = fullFileName.substr(srcDir.length() + 1);
  1673. } else {
  1674. // fallback: add plain filename without any path
  1675. sourceLink = cmsys::SystemTools::GetFilenameName(fullFileName);
  1676. }
  1677. if (!sourceLink.empty()) {
  1678. ConvertToWindowsSlash(sourceLink);
  1679. }
  1680. }
  1681. }
  1682. if (ext == "hlsl") {
  1683. tool = "FXCompile";
  1684. // Figure out the type of shader compiler to use.
  1685. if (const char* st = sf->GetProperty("VS_SHADER_TYPE")) {
  1686. shaderType = st;
  1687. toolHasSettings = true;
  1688. }
  1689. // Figure out which entry point to use if any
  1690. if (const char* se = sf->GetProperty("VS_SHADER_ENTRYPOINT")) {
  1691. shaderEntryPoint = se;
  1692. toolHasSettings = true;
  1693. }
  1694. // Figure out which shader model to use if any
  1695. if (const char* sm = sf->GetProperty("VS_SHADER_MODEL")) {
  1696. shaderModel = sm;
  1697. toolHasSettings = true;
  1698. }
  1699. // Figure out which output header file to use if any
  1700. if (const char* ohf = sf->GetProperty("VS_SHADER_OUTPUT_HEADER_FILE")) {
  1701. outputHeaderFile = ohf;
  1702. toolHasSettings = true;
  1703. }
  1704. // Figure out which variable name to use if any
  1705. if (const char* vn = sf->GetProperty("VS_SHADER_VARIABLE_NAME")) {
  1706. variableName = vn;
  1707. toolHasSettings = true;
  1708. }
  1709. // Figure out if there's any additional flags to use
  1710. if (const char* saf = sf->GetProperty("VS_SHADER_FLAGS")) {
  1711. shaderAdditionalFlags = saf;
  1712. toolHasSettings = true;
  1713. }
  1714. // Figure out if debug information should be generated
  1715. if (const char* sed = sf->GetProperty("VS_SHADER_ENABLE_DEBUG")) {
  1716. shaderEnableDebug = sed;
  1717. toolHasSettings = true;
  1718. }
  1719. // Figure out if optimizations should be disabled
  1720. if (const char* sdo = sf->GetProperty("VS_SHADER_DISABLE_OPTIMIZATIONS")) {
  1721. shaderDisableOptimizations = sdo;
  1722. toolHasSettings = true;
  1723. }
  1724. if (const char* sofn = sf->GetProperty("VS_SHADER_OBJECT_FILE_NAME")) {
  1725. shaderObjectFileName = sofn;
  1726. toolHasSettings = true;
  1727. }
  1728. } else if (ext == "jpg" || ext == "png") {
  1729. tool = "Image";
  1730. } else if (ext == "resw") {
  1731. tool = "PRIResource";
  1732. } else if (ext == "xml") {
  1733. tool = "XML";
  1734. } else if (ext == "natvis") {
  1735. tool = "Natvis";
  1736. } else if (ext == "settings") {
  1737. settingsLastGenOutput =
  1738. cmsys::SystemTools::GetFilenameName(sf->GetFullPath());
  1739. std::size_t pos = settingsLastGenOutput.find(".settings");
  1740. settingsLastGenOutput.replace(pos, 9, ".Designer.cs");
  1741. settingsGenerator = "SettingsSingleFileGenerator";
  1742. toolHasSettings = true;
  1743. } else if (ext == "vsixmanifest") {
  1744. subType = "Designer";
  1745. }
  1746. if (const char* c = sf->GetProperty("VS_COPY_TO_OUT_DIR")) {
  1747. tool = "Content";
  1748. copyToOutDir = c;
  1749. toolHasSettings = true;
  1750. }
  1751. if (sf->GetPropertyAsBool("VS_INCLUDE_IN_VSIX")) {
  1752. includeInVsix = "True";
  1753. tool = "Content";
  1754. toolHasSettings = true;
  1755. }
  1756. // Collect VS_CSHARP_* property values (if some are set)
  1757. std::map<std::string, std::string> sourceFileTags;
  1758. this->GetCSharpSourceProperties(sf, sourceFileTags);
  1759. if (this->NsightTegra) {
  1760. // Nsight Tegra needs specific file types to check up-to-dateness.
  1761. std::string name = cmSystemTools::LowerCase(sf->GetLocation().GetName());
  1762. if (name == "androidmanifest.xml" || name == "build.xml" ||
  1763. name == "proguard.cfg" || name == "proguard-project.txt" ||
  1764. ext == "properties") {
  1765. tool = "AndroidBuild";
  1766. } else if (ext == "java") {
  1767. tool = "JCompile";
  1768. } else if (ext == "asm" || ext == "s") {
  1769. tool = "ClCompile";
  1770. }
  1771. }
  1772. const char* toolOverride = sf->GetProperty("VS_TOOL_OVERRIDE");
  1773. if (toolOverride && *toolOverride) {
  1774. tool = toolOverride;
  1775. }
  1776. std::string deployContent;
  1777. std::string deployLocation;
  1778. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  1779. this->GlobalGenerator->TargetsWindowsStore()) {
  1780. const char* content = sf->GetProperty("VS_DEPLOYMENT_CONTENT");
  1781. if (content && *content) {
  1782. toolHasSettings = true;
  1783. deployContent = content;
  1784. const char* location = sf->GetProperty("VS_DEPLOYMENT_LOCATION");
  1785. if (location && *location) {
  1786. deployLocation = location;
  1787. }
  1788. }
  1789. }
  1790. Elem e2(e1, tool);
  1791. this->WriteSource(e2, sf);
  1792. if (toolHasSettings) {
  1793. e2.SetHasElements();
  1794. if (!deployContent.empty()) {
  1795. cmGeneratorExpression ge;
  1796. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  1797. ge.Parse(deployContent);
  1798. // Deployment location cannot be set on a configuration basis
  1799. if (!deployLocation.empty()) {
  1800. e2.Element("Link", deployLocation + "\\%(FileName)%(Extension)");
  1801. }
  1802. for (size_t i = 0; i != this->Configurations.size(); ++i) {
  1803. if (cge->Evaluate(this->LocalGenerator, this->Configurations[i]) ==
  1804. "1") {
  1805. e2.WritePlatformConfigTag("DeploymentContent",
  1806. "'$(Configuration)|$(Platform)'=='" +
  1807. this->Configurations[i] + "|" +
  1808. this->Platform + "'",
  1809. "true");
  1810. } else {
  1811. e2.WritePlatformConfigTag("ExcludedFromBuild",
  1812. "'$(Configuration)|$(Platform)'=='" +
  1813. this->Configurations[i] + "|" +
  1814. this->Platform + "'",
  1815. "true");
  1816. }
  1817. }
  1818. }
  1819. if (!shaderType.empty()) {
  1820. e2.Element("ShaderType", shaderType);
  1821. }
  1822. if (!shaderEntryPoint.empty()) {
  1823. e2.Element("EntryPointName", shaderEntryPoint);
  1824. }
  1825. if (!shaderModel.empty()) {
  1826. e2.Element("ShaderModel", shaderModel);
  1827. }
  1828. if (!outputHeaderFile.empty()) {
  1829. for (size_t i = 0; i != this->Configurations.size(); ++i) {
  1830. e2.WritePlatformConfigTag("HeaderFileOutput",
  1831. "'$(Configuration)|$(Platform)'=='" +
  1832. this->Configurations[i] + "|" +
  1833. this->Platform + "'",
  1834. outputHeaderFile);
  1835. }
  1836. }
  1837. if (!variableName.empty()) {
  1838. for (size_t i = 0; i != this->Configurations.size(); ++i) {
  1839. e2.WritePlatformConfigTag("VariableName",
  1840. "'$(Configuration)|$(Platform)'=='" +
  1841. this->Configurations[i] + "|" +
  1842. this->Platform + "'",
  1843. variableName);
  1844. }
  1845. }
  1846. if (!shaderEnableDebug.empty()) {
  1847. cmGeneratorExpression ge;
  1848. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  1849. ge.Parse(shaderEnableDebug);
  1850. for (size_t i = 0; i != this->Configurations.size(); ++i) {
  1851. const std::string& enableDebug =
  1852. cge->Evaluate(this->LocalGenerator, this->Configurations[i]);
  1853. if (!enableDebug.empty()) {
  1854. e2.WritePlatformConfigTag("EnableDebuggingInformation",
  1855. "'$(Configuration)|$(Platform)'=='" +
  1856. this->Configurations[i] + "|" +
  1857. this->Platform + "'",
  1858. cmIsOn(enableDebug) ? "true" : "false");
  1859. }
  1860. }
  1861. }
  1862. if (!shaderDisableOptimizations.empty()) {
  1863. cmGeneratorExpression ge;
  1864. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  1865. ge.Parse(shaderDisableOptimizations);
  1866. for (size_t i = 0; i != this->Configurations.size(); ++i) {
  1867. const std::string& disableOptimizations =
  1868. cge->Evaluate(this->LocalGenerator, this->Configurations[i]);
  1869. if (!disableOptimizations.empty()) {
  1870. e2.WritePlatformConfigTag(
  1871. "DisableOptimizations",
  1872. "'$(Configuration)|$(Platform)'=='" + this->Configurations[i] +
  1873. "|" + this->Platform + "'",
  1874. (cmIsOn(disableOptimizations) ? "true" : "false"));
  1875. }
  1876. }
  1877. }
  1878. if (!shaderObjectFileName.empty()) {
  1879. e2.Element("ObjectFileOutput", shaderObjectFileName);
  1880. }
  1881. if (!shaderAdditionalFlags.empty()) {
  1882. e2.Element("AdditionalOptions", shaderAdditionalFlags);
  1883. }
  1884. if (!settingsGenerator.empty()) {
  1885. e2.Element("Generator", settingsGenerator);
  1886. }
  1887. if (!settingsLastGenOutput.empty()) {
  1888. e2.Element("LastGenOutput", settingsLastGenOutput);
  1889. }
  1890. if (!sourceLink.empty()) {
  1891. e2.Element("Link", sourceLink);
  1892. }
  1893. if (!subType.empty()) {
  1894. e2.Element("SubType", subType);
  1895. }
  1896. if (!copyToOutDir.empty()) {
  1897. e2.Element("CopyToOutputDirectory", copyToOutDir);
  1898. }
  1899. if (!includeInVsix.empty()) {
  1900. e2.Element("IncludeInVSIX", includeInVsix);
  1901. }
  1902. // write source file specific tags
  1903. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  1904. }
  1905. }
  1906. void cmVisualStudio10TargetGenerator::WriteSource(Elem& e2,
  1907. cmSourceFile const* sf)
  1908. {
  1909. // Visual Studio tools append relative paths to the current dir, as in:
  1910. //
  1911. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  1912. //
  1913. // and fail if this exceeds the maximum allowed path length. Our path
  1914. // conversion uses full paths when possible to allow deeper trees.
  1915. // However, CUDA 8.0 msbuild rules fail on absolute paths so for CUDA
  1916. // we must use relative paths.
  1917. bool forceRelative = sf->GetLanguage() == "CUDA";
  1918. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), forceRelative);
  1919. if (this->LocalGenerator->GetVersion() ==
  1920. cmGlobalVisualStudioGenerator::VS10 &&
  1921. cmSystemTools::FileIsFullPath(sourceFile)) {
  1922. // Normal path conversion resulted in a full path. VS 10 (but not 11)
  1923. // refuses to show the property page in the IDE for a source file with a
  1924. // full path (not starting in a '.' or '/' AFAICT). CMake <= 2.8.4 used a
  1925. // relative path but to allow deeper build trees CMake 2.8.[5678] used a
  1926. // full path except for custom commands. Custom commands do not work
  1927. // without a relative path, but they do not seem to be involved in tools
  1928. // with the above behavior. For other sources we now use a relative path
  1929. // when the combined path will not be too long so property pages appear.
  1930. std::string sourceRel = this->ConvertPath(sf->GetFullPath(), true);
  1931. size_t const maxLen = 250;
  1932. if (sf->GetCustomCommand() ||
  1933. ((this->LocalGenerator->GetCurrentBinaryDirectory().length() + 1 +
  1934. sourceRel.length()) <= maxLen)) {
  1935. forceRelative = true;
  1936. sourceFile = sourceRel;
  1937. } else {
  1938. this->GlobalGenerator->PathTooLong(this->GeneratorTarget, sf, sourceRel);
  1939. }
  1940. }
  1941. ConvertToWindowsSlash(sourceFile);
  1942. e2.Attribute("Include", sourceFile);
  1943. ToolSource toolSource = { sf, forceRelative };
  1944. this->Tools[e2.Tag].push_back(toolSource);
  1945. }
  1946. void cmVisualStudio10TargetGenerator::WriteAllSources(Elem& e0)
  1947. {
  1948. if (this->GeneratorTarget->GetType() > cmStateEnums::UTILITY) {
  1949. return;
  1950. }
  1951. const bool haveUnityBuild =
  1952. this->GeneratorTarget->GetPropertyAsBool("UNITY_BUILD");
  1953. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  1954. Elem e1(e0, "PropertyGroup");
  1955. e1.Element("EnableUnitySupport", "true");
  1956. }
  1957. Elem e1(e0, "ItemGroup");
  1958. e1.SetHasElements();
  1959. std::vector<size_t> all_configs;
  1960. for (size_t ci = 0; ci < this->Configurations.size(); ++ci) {
  1961. all_configs.push_back(ci);
  1962. }
  1963. std::vector<cmGeneratorTarget::AllConfigSource> const& sources =
  1964. this->GeneratorTarget->GetAllConfigSources();
  1965. cmSourceFile const* srcCMakeLists =
  1966. this->LocalGenerator->CreateVCProjBuildRule();
  1967. for (cmGeneratorTarget::AllConfigSource const& si : sources) {
  1968. if (si.Source == srcCMakeLists) {
  1969. // Skip explicit reference to CMakeLists.txt source.
  1970. continue;
  1971. }
  1972. const char* tool = nullptr;
  1973. switch (si.Kind) {
  1974. case cmGeneratorTarget::SourceKindAppManifest:
  1975. tool = "AppxManifest";
  1976. break;
  1977. case cmGeneratorTarget::SourceKindCertificate:
  1978. tool = "None";
  1979. break;
  1980. case cmGeneratorTarget::SourceKindCustomCommand:
  1981. // Handled elsewhere.
  1982. break;
  1983. case cmGeneratorTarget::SourceKindExternalObject:
  1984. tool = "Object";
  1985. if (this->LocalGenerator->GetVersion() <
  1986. cmGlobalVisualStudioGenerator::VS11) {
  1987. // For VS == 10 we cannot use LinkObjects to avoid linking custom
  1988. // command outputs. If an object file is generated in this target,
  1989. // then vs10 will use it in the build, and we have to list it as
  1990. // None instead of Object.
  1991. std::vector<cmSourceFile*> const* d =
  1992. this->GeneratorTarget->GetSourceDepends(si.Source);
  1993. if (d && !d->empty()) {
  1994. tool = "None";
  1995. }
  1996. }
  1997. break;
  1998. case cmGeneratorTarget::SourceKindExtra:
  1999. this->WriteExtraSource(e1, si.Source);
  2000. break;
  2001. case cmGeneratorTarget::SourceKindHeader:
  2002. this->WriteHeaderSource(e1, si.Source);
  2003. break;
  2004. case cmGeneratorTarget::SourceKindIDL:
  2005. tool = "Midl";
  2006. break;
  2007. case cmGeneratorTarget::SourceKindManifest:
  2008. // Handled elsewhere.
  2009. break;
  2010. case cmGeneratorTarget::SourceKindModuleDefinition:
  2011. tool = "None";
  2012. break;
  2013. case cmGeneratorTarget::SourceKindUnityBatched:
  2014. case cmGeneratorTarget::SourceKindObjectSource: {
  2015. const std::string& lang = si.Source->GetLanguage();
  2016. if (lang == "C" || lang == "CXX") {
  2017. tool = "ClCompile";
  2018. } else if (lang == "ASM_MASM" &&
  2019. this->GlobalGenerator->IsMasmEnabled()) {
  2020. tool = "MASM";
  2021. } else if (lang == "ASM_NASM" &&
  2022. this->GlobalGenerator->IsNasmEnabled()) {
  2023. tool = "NASM";
  2024. } else if (lang == "RC") {
  2025. tool = "ResourceCompile";
  2026. } else if (lang == "CSharp") {
  2027. tool = "Compile";
  2028. } else if (lang == "CUDA" && this->GlobalGenerator->IsCudaEnabled()) {
  2029. tool = "CudaCompile";
  2030. } else {
  2031. tool = "None";
  2032. }
  2033. } break;
  2034. case cmGeneratorTarget::SourceKindResx:
  2035. // Handled elsewhere.
  2036. break;
  2037. case cmGeneratorTarget::SourceKindXaml:
  2038. // Handled elsewhere.
  2039. break;
  2040. }
  2041. if (tool) {
  2042. // Compute set of configurations to exclude, if any.
  2043. std::vector<size_t> const& include_configs = si.Configs;
  2044. std::vector<size_t> exclude_configs;
  2045. std::set_difference(all_configs.begin(), all_configs.end(),
  2046. include_configs.begin(), include_configs.end(),
  2047. std::back_inserter(exclude_configs));
  2048. Elem e2(e1, tool);
  2049. this->WriteSource(e2, si.Source);
  2050. bool useNativeUnityBuild = false;
  2051. if (haveUnityBuild && this->GlobalGenerator->GetSupportsUnityBuilds()) {
  2052. // Magic value taken from cmGlobalVisualStudioVersionedGenerator.cxx
  2053. static const std::string vs15 = "141";
  2054. std::string toolset =
  2055. this->GlobalGenerator->GetPlatformToolsetString();
  2056. cmSystemTools::ReplaceString(toolset, "v", "");
  2057. if (toolset.empty() ||
  2058. cmSystemTools::VersionCompareGreaterEq(toolset, vs15)) {
  2059. useNativeUnityBuild = true;
  2060. }
  2061. }
  2062. if (haveUnityBuild && strcmp(tool, "ClCompile") == 0 &&
  2063. si.Source->GetProperty("UNITY_SOURCE_FILE")) {
  2064. if (useNativeUnityBuild) {
  2065. e2.Attribute(
  2066. "IncludeInUnityFile",
  2067. si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")
  2068. ? "false"
  2069. : "true");
  2070. e2.Attribute("CustomUnityFile", "true");
  2071. std::string unityDir = cmSystemTools::GetFilenamePath(
  2072. si.Source->GetProperty("UNITY_SOURCE_FILE"));
  2073. e2.Attribute("UnityFilesDirectory", unityDir);
  2074. } else {
  2075. // Visual Studio versions prior to 2017 15.8 do not know about unity
  2076. // builds, thus we exclude the files alredy part of unity sources.
  2077. if (!si.Source->GetPropertyAsBool("SKIP_UNITY_BUILD_INCLUSION")) {
  2078. exclude_configs = si.Configs;
  2079. }
  2080. }
  2081. }
  2082. if (si.Kind == cmGeneratorTarget::SourceKindObjectSource) {
  2083. this->OutputSourceSpecificFlags(e2, si.Source);
  2084. }
  2085. if (si.Source->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  2086. e2.Element("PrecompiledHeader", "NotUsing");
  2087. }
  2088. if (!exclude_configs.empty()) {
  2089. this->WriteExcludeFromBuild(e2, exclude_configs);
  2090. }
  2091. }
  2092. }
  2093. if (this->IsMissingFiles) {
  2094. this->WriteMissingFiles(e1);
  2095. }
  2096. }
  2097. void cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  2098. Elem& e2, cmSourceFile const* source)
  2099. {
  2100. cmSourceFile const& sf = *source;
  2101. std::string objectName;
  2102. if (this->GeneratorTarget->HasExplicitObjectName(&sf)) {
  2103. objectName = this->GeneratorTarget->GetObjectName(&sf);
  2104. }
  2105. std::string flags;
  2106. bool configDependentFlags = false;
  2107. std::string options;
  2108. bool configDependentOptions = false;
  2109. std::string defines;
  2110. bool configDependentDefines = false;
  2111. std::string includes;
  2112. bool configDependentIncludes = false;
  2113. if (const char* cflags = sf.GetProperty("COMPILE_FLAGS")) {
  2114. configDependentFlags =
  2115. cmGeneratorExpression::Find(cflags) != std::string::npos;
  2116. flags += cflags;
  2117. }
  2118. if (const char* coptions = sf.GetProperty("COMPILE_OPTIONS")) {
  2119. configDependentOptions =
  2120. cmGeneratorExpression::Find(coptions) != std::string::npos;
  2121. options += coptions;
  2122. }
  2123. if (const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS")) {
  2124. configDependentDefines =
  2125. cmGeneratorExpression::Find(cdefs) != std::string::npos;
  2126. defines += cdefs;
  2127. }
  2128. if (const char* cincludes = sf.GetProperty("INCLUDE_DIRECTORIES")) {
  2129. configDependentIncludes =
  2130. cmGeneratorExpression::Find(cincludes) != std::string::npos;
  2131. includes += cincludes;
  2132. }
  2133. std::string lang =
  2134. this->GlobalGenerator->GetLanguageFromExtension(sf.GetExtension().c_str());
  2135. std::string sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  2136. const std::string& linkLanguage =
  2137. this->GeneratorTarget->GetLinkerLanguage("");
  2138. bool needForceLang = false;
  2139. // source file does not match its extension language
  2140. if (lang != sourceLang) {
  2141. needForceLang = true;
  2142. lang = sourceLang;
  2143. }
  2144. // if the source file does not match the linker language
  2145. // then force c or c++
  2146. const char* compileAs = 0;
  2147. if (needForceLang || (linkLanguage != lang)) {
  2148. if (lang == "CXX") {
  2149. // force a C++ file type
  2150. compileAs = "CompileAsCpp";
  2151. } else if (lang == "C") {
  2152. // force to c
  2153. compileAs = "CompileAsC";
  2154. }
  2155. }
  2156. bool noWinRT = this->TargetCompileAsWinRT && lang == "C";
  2157. // for the first time we need a new line if there is something
  2158. // produced here.
  2159. if (!objectName.empty()) {
  2160. if (lang == "CUDA") {
  2161. e2.Element("CompileOut", "$(IntDir)/" + objectName);
  2162. } else {
  2163. e2.Element("ObjectFileName", "$(IntDir)/" + objectName);
  2164. }
  2165. }
  2166. for (std::string const& config : this->Configurations) {
  2167. std::string configUpper = cmSystemTools::UpperCase(config);
  2168. std::string configDefines = defines;
  2169. std::string defPropName = cmStrCat("COMPILE_DEFINITIONS_", configUpper);
  2170. if (const char* ccdefs = sf.GetProperty(defPropName)) {
  2171. if (!configDefines.empty()) {
  2172. configDefines += ";";
  2173. }
  2174. configDependentDefines |=
  2175. cmGeneratorExpression::Find(ccdefs) != std::string::npos;
  2176. configDefines += ccdefs;
  2177. }
  2178. // Add precompile headers compile options.
  2179. std::string customAndPchOptions = options;
  2180. const std::string pchSource =
  2181. this->GeneratorTarget->GetPchSource(config, lang);
  2182. if (!pchSource.empty() && !sf.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
  2183. std::string pchOptions;
  2184. if (sf.GetFullPath() == pchSource) {
  2185. pchOptions =
  2186. this->GeneratorTarget->GetPchCreateCompileOptions(config, lang);
  2187. } else {
  2188. pchOptions =
  2189. this->GeneratorTarget->GetPchUseCompileOptions(config, lang);
  2190. }
  2191. customAndPchOptions = cmStrCat(customAndPchOptions, ';', pchOptions);
  2192. }
  2193. // if we have flags or defines for this config then
  2194. // use them
  2195. if (!flags.empty() || !options.empty() || !configDefines.empty() ||
  2196. !includes.empty() || compileAs || noWinRT ||
  2197. !customAndPchOptions.empty()) {
  2198. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2199. cmIDEFlagTable const* flagtable = nullptr;
  2200. const std::string& srclang = source->GetLanguage();
  2201. if (srclang == "C" || srclang == "CXX") {
  2202. flagtable = gg->GetClFlagTable();
  2203. } else if (srclang == "ASM_MASM" &&
  2204. this->GlobalGenerator->IsMasmEnabled()) {
  2205. flagtable = gg->GetMasmFlagTable();
  2206. } else if (lang == "ASM_NASM" &&
  2207. this->GlobalGenerator->IsNasmEnabled()) {
  2208. flagtable = gg->GetNasmFlagTable();
  2209. } else if (srclang == "RC") {
  2210. flagtable = gg->GetRcFlagTable();
  2211. } else if (srclang == "CSharp") {
  2212. flagtable = gg->GetCSharpFlagTable();
  2213. }
  2214. cmGeneratorExpressionInterpreter genexInterpreter(
  2215. this->LocalGenerator, config, this->GeneratorTarget, lang);
  2216. cmVS10GeneratorOptions clOptions(
  2217. this->LocalGenerator, cmVisualStudioGeneratorOptions::Compiler,
  2218. flagtable, this);
  2219. if (compileAs) {
  2220. clOptions.AddFlag("CompileAs", compileAs);
  2221. }
  2222. if (noWinRT) {
  2223. clOptions.AddFlag("CompileAsWinRT", "false");
  2224. }
  2225. if (configDependentFlags) {
  2226. clOptions.Parse(genexInterpreter.Evaluate(flags, "COMPILE_FLAGS"));
  2227. } else {
  2228. clOptions.Parse(flags);
  2229. }
  2230. if (!customAndPchOptions.empty()) {
  2231. std::string expandedOptions;
  2232. if (configDependentOptions) {
  2233. this->LocalGenerator->AppendCompileOptions(
  2234. expandedOptions,
  2235. genexInterpreter.Evaluate(customAndPchOptions, "COMPILE_OPTIONS"));
  2236. } else {
  2237. this->LocalGenerator->AppendCompileOptions(expandedOptions,
  2238. customAndPchOptions);
  2239. }
  2240. clOptions.Parse(expandedOptions);
  2241. }
  2242. if (clOptions.HasFlag("DisableSpecificWarnings")) {
  2243. clOptions.AppendFlag("DisableSpecificWarnings",
  2244. "%(DisableSpecificWarnings)");
  2245. }
  2246. if (clOptions.HasFlag("ForcedIncludeFiles")) {
  2247. clOptions.AppendFlag("ForcedIncludeFiles", "%(ForcedIncludeFiles)");
  2248. }
  2249. if (configDependentDefines) {
  2250. clOptions.AddDefines(
  2251. genexInterpreter.Evaluate(configDefines, "COMPILE_DEFINITIONS"));
  2252. } else {
  2253. clOptions.AddDefines(configDefines);
  2254. }
  2255. std::vector<std::string> includeList;
  2256. if (configDependentIncludes) {
  2257. this->LocalGenerator->AppendIncludeDirectories(
  2258. includeList,
  2259. genexInterpreter.Evaluate(includes, "INCLUDE_DIRECTORIES"), *source);
  2260. } else {
  2261. this->LocalGenerator->AppendIncludeDirectories(includeList, includes,
  2262. *source);
  2263. }
  2264. clOptions.AddIncludes(includeList);
  2265. clOptions.SetConfiguration(config);
  2266. OptionsHelper oh(clOptions, e2);
  2267. oh.PrependInheritedString("AdditionalOptions");
  2268. oh.OutputAdditionalIncludeDirectories(lang);
  2269. oh.OutputFlagMap();
  2270. oh.OutputPreprocessorDefinitions(lang);
  2271. }
  2272. }
  2273. if (this->IsXamlSource(source->GetFullPath())) {
  2274. const std::string& fileName = source->GetFullPath();
  2275. std::string xamlFileName = fileName.substr(0, fileName.find_last_of("."));
  2276. e2.Element("DependentUpon", xamlFileName);
  2277. }
  2278. if (this->ProjectType == csproj) {
  2279. std::string f = source->GetFullPath();
  2280. using CsPropMap = std::map<std::string, std::string>;
  2281. CsPropMap sourceFileTags;
  2282. // set <Link> tag if necessary
  2283. std::string link;
  2284. this->GetCSharpSourceLink(source, link);
  2285. if (!link.empty()) {
  2286. sourceFileTags["Link"] = link;
  2287. }
  2288. this->GetCSharpSourceProperties(&sf, sourceFileTags);
  2289. // write source file specific tags
  2290. if (!sourceFileTags.empty()) {
  2291. this->WriteCSharpSourceProperties(e2, sourceFileTags);
  2292. }
  2293. }
  2294. }
  2295. void cmVisualStudio10TargetGenerator::WriteExcludeFromBuild(
  2296. Elem& e2, std::vector<size_t> const& exclude_configs)
  2297. {
  2298. for (size_t ci : exclude_configs) {
  2299. e2.WritePlatformConfigTag("ExcludedFromBuild",
  2300. "'$(Configuration)|$(Platform)'=='" +
  2301. this->Configurations[ci] + "|" +
  2302. this->Platform + "'",
  2303. "true");
  2304. }
  2305. }
  2306. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions(
  2307. Elem& e0)
  2308. {
  2309. cmStateEnums::TargetType ttype = this->GeneratorTarget->GetType();
  2310. if (ttype > cmStateEnums::GLOBAL_TARGET) {
  2311. return;
  2312. }
  2313. if (this->ProjectType == csproj) {
  2314. return;
  2315. }
  2316. Elem e1(e0, "PropertyGroup");
  2317. e1.Element("_ProjectFileVersion", "10.0.20506.1");
  2318. for (std::string const& config : this->Configurations) {
  2319. const std::string cond = this->CalcCondition(config);
  2320. if (ttype <= cmStateEnums::UTILITY) {
  2321. if (const char* workingDir = this->GeneratorTarget->GetProperty(
  2322. "VS_DEBUGGER_WORKING_DIRECTORY")) {
  2323. std::string genWorkingDir = cmGeneratorExpression::Evaluate(
  2324. workingDir, this->LocalGenerator, config);
  2325. e1.WritePlatformConfigTag("LocalDebuggerWorkingDirectory", cond,
  2326. genWorkingDir);
  2327. }
  2328. if (const char* environment =
  2329. this->GeneratorTarget->GetProperty("VS_DEBUGGER_ENVIRONMENT")) {
  2330. std::string genEnvironment = cmGeneratorExpression::Evaluate(
  2331. environment, this->LocalGenerator, config);
  2332. e1.WritePlatformConfigTag("LocalDebuggerEnvironment", cond,
  2333. genEnvironment);
  2334. }
  2335. if (const char* debuggerCommand =
  2336. this->GeneratorTarget->GetProperty("VS_DEBUGGER_COMMAND")) {
  2337. std::string genDebuggerCommand = cmGeneratorExpression::Evaluate(
  2338. debuggerCommand, this->LocalGenerator, config);
  2339. e1.WritePlatformConfigTag("LocalDebuggerCommand", cond,
  2340. genDebuggerCommand);
  2341. }
  2342. if (const char* commandArguments = this->GeneratorTarget->GetProperty(
  2343. "VS_DEBUGGER_COMMAND_ARGUMENTS")) {
  2344. std::string genCommandArguments = cmGeneratorExpression::Evaluate(
  2345. commandArguments, this->LocalGenerator, config);
  2346. e1.WritePlatformConfigTag("LocalDebuggerCommandArguments", cond,
  2347. genCommandArguments);
  2348. }
  2349. }
  2350. if (ttype >= cmStateEnums::UTILITY) {
  2351. e1.WritePlatformConfigTag(
  2352. "IntDir", cond, "$(Platform)\\$(Configuration)\\$(ProjectName)\\");
  2353. } else {
  2354. std::string intermediateDir = cmStrCat(
  2355. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget), '/',
  2356. config, '/');
  2357. std::string outDir;
  2358. std::string targetNameFull;
  2359. if (ttype == cmStateEnums::OBJECT_LIBRARY) {
  2360. outDir = intermediateDir;
  2361. targetNameFull = cmStrCat(this->GeneratorTarget->GetName(), ".lib");
  2362. } else {
  2363. outDir = this->GeneratorTarget->GetDirectory(config) + "/";
  2364. targetNameFull = this->GeneratorTarget->GetFullName(config);
  2365. }
  2366. ConvertToWindowsSlash(intermediateDir);
  2367. ConvertToWindowsSlash(outDir);
  2368. e1.WritePlatformConfigTag("OutDir", cond, outDir);
  2369. e1.WritePlatformConfigTag("IntDir", cond, intermediateDir);
  2370. if (const char* sdkExecutableDirectories = this->Makefile->GetDefinition(
  2371. "CMAKE_VS_SDK_EXECUTABLE_DIRECTORIES")) {
  2372. e1.WritePlatformConfigTag("ExecutablePath", cond,
  2373. sdkExecutableDirectories);
  2374. }
  2375. if (const char* sdkIncludeDirectories = this->Makefile->GetDefinition(
  2376. "CMAKE_VS_SDK_INCLUDE_DIRECTORIES")) {
  2377. e1.WritePlatformConfigTag("IncludePath", cond, sdkIncludeDirectories);
  2378. }
  2379. if (const char* sdkReferenceDirectories = this->Makefile->GetDefinition(
  2380. "CMAKE_VS_SDK_REFERENCE_DIRECTORIES")) {
  2381. e1.WritePlatformConfigTag("ReferencePath", cond,
  2382. sdkReferenceDirectories);
  2383. }
  2384. if (const char* sdkLibraryDirectories = this->Makefile->GetDefinition(
  2385. "CMAKE_VS_SDK_LIBRARY_DIRECTORIES")) {
  2386. e1.WritePlatformConfigTag("LibraryPath", cond, sdkLibraryDirectories);
  2387. }
  2388. if (const char* sdkLibraryWDirectories = this->Makefile->GetDefinition(
  2389. "CMAKE_VS_SDK_LIBRARY_WINRT_DIRECTORIES")) {
  2390. e1.WritePlatformConfigTag("LibraryWPath", cond,
  2391. sdkLibraryWDirectories);
  2392. }
  2393. if (const char* sdkSourceDirectories =
  2394. this->Makefile->GetDefinition("CMAKE_VS_SDK_SOURCE_DIRECTORIES")) {
  2395. e1.WritePlatformConfigTag("SourcePath", cond, sdkSourceDirectories);
  2396. }
  2397. if (const char* sdkExcludeDirectories = this->Makefile->GetDefinition(
  2398. "CMAKE_VS_SDK_EXCLUDE_DIRECTORIES")) {
  2399. e1.WritePlatformConfigTag("ExcludePath", cond, sdkExcludeDirectories);
  2400. }
  2401. std::string name =
  2402. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  2403. e1.WritePlatformConfigTag("TargetName", cond, name);
  2404. std::string ext =
  2405. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  2406. if (ext.empty()) {
  2407. // An empty TargetExt causes a default extension to be used.
  2408. // A single "." appears to be treated as an empty extension.
  2409. ext = ".";
  2410. }
  2411. e1.WritePlatformConfigTag("TargetExt", cond, ext);
  2412. this->OutputLinkIncremental(e1, config);
  2413. }
  2414. }
  2415. }
  2416. void cmVisualStudio10TargetGenerator::OutputLinkIncremental(
  2417. Elem& e1, std::string const& configName)
  2418. {
  2419. if (!this->MSTools) {
  2420. return;
  2421. }
  2422. if (this->ProjectType == csproj) {
  2423. return;
  2424. }
  2425. // static libraries and things greater than modules do not need
  2426. // to set this option
  2427. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2428. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  2429. return;
  2430. }
  2431. Options& linkOptions = *(this->LinkOptions[configName]);
  2432. const std::string cond = this->CalcCondition(configName);
  2433. if (this->IPOEnabledConfigurations.count(configName) == 0) {
  2434. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  2435. e1.WritePlatformConfigTag("LinkIncremental", cond,
  2436. (incremental ? incremental : "true"));
  2437. }
  2438. linkOptions.RemoveFlag("LinkIncremental");
  2439. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  2440. e1.WritePlatformConfigTag("GenerateManifest", cond,
  2441. (manifest ? manifest : "true"));
  2442. linkOptions.RemoveFlag("GenerateManifest");
  2443. // Some link options belong here. Use them now and remove them so that
  2444. // WriteLinkOptions does not use them.
  2445. const char* flags[] = { "LinkDelaySign", "LinkKeyFile", 0 };
  2446. for (const char** f = flags; *f; ++f) {
  2447. const char* flag = *f;
  2448. if (const char* value = linkOptions.GetFlag(flag)) {
  2449. e1.WritePlatformConfigTag(flag, cond, value);
  2450. linkOptions.RemoveFlag(flag);
  2451. }
  2452. }
  2453. }
  2454. std::vector<std::string> cmVisualStudio10TargetGenerator::GetIncludes(
  2455. std::string const& config, std::string const& lang) const
  2456. {
  2457. std::vector<std::string> includes;
  2458. this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
  2459. lang, config);
  2460. for (std::string& i : includes) {
  2461. ConvertToWindowsSlash(i);
  2462. }
  2463. return includes;
  2464. }
  2465. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  2466. {
  2467. for (std::string const& c : this->Configurations) {
  2468. if (!this->ComputeClOptions(c)) {
  2469. return false;
  2470. }
  2471. }
  2472. return true;
  2473. }
  2474. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  2475. std::string const& configName)
  2476. {
  2477. // much of this was copied from here:
  2478. // copied from cmLocalVisualStudio7Generator.cxx 805
  2479. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  2480. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2481. std::unique_ptr<Options> pOptions;
  2482. switch (this->ProjectType) {
  2483. case vcxproj:
  2484. pOptions = cm::make_unique<Options>(
  2485. this->LocalGenerator, Options::Compiler, gg->GetClFlagTable());
  2486. break;
  2487. case csproj:
  2488. pOptions =
  2489. cm::make_unique<Options>(this->LocalGenerator, Options::CSharpCompiler,
  2490. gg->GetCSharpFlagTable());
  2491. break;
  2492. }
  2493. Options& clOptions = *pOptions;
  2494. std::string flags;
  2495. const std::string& linkLanguage =
  2496. this->GeneratorTarget->GetLinkerLanguage(configName);
  2497. if (linkLanguage.empty()) {
  2498. cmSystemTools::Error(
  2499. "CMake can not determine linker language for target: " + this->Name);
  2500. return false;
  2501. }
  2502. // Choose a language whose flags to use for ClCompile.
  2503. static const char* clLangs[] = { "CXX", "C", "Fortran" };
  2504. std::string langForClCompile;
  2505. if (this->ProjectType == csproj) {
  2506. langForClCompile = "CSharp";
  2507. } else if (cmContains(clLangs, linkLanguage)) {
  2508. langForClCompile = linkLanguage;
  2509. } else {
  2510. std::set<std::string> languages;
  2511. this->GeneratorTarget->GetLanguages(languages, configName);
  2512. for (const char* l : clLangs) {
  2513. if (languages.count(l)) {
  2514. langForClCompile = l;
  2515. break;
  2516. }
  2517. }
  2518. }
  2519. this->LangForClCompile = langForClCompile;
  2520. if (!langForClCompile.empty()) {
  2521. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  2522. langForClCompile, configName);
  2523. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget,
  2524. langForClCompile, configName);
  2525. }
  2526. // set the correct language
  2527. if (linkLanguage == "C") {
  2528. clOptions.AddFlag("CompileAs", "CompileAsC");
  2529. }
  2530. if (linkLanguage == "CXX") {
  2531. clOptions.AddFlag("CompileAs", "CompileAsCpp");
  2532. }
  2533. // Put the IPO enabled configurations into a set.
  2534. if (this->GeneratorTarget->IsIPOEnabled(linkLanguage, configName)) {
  2535. this->IPOEnabledConfigurations.insert(configName);
  2536. }
  2537. // Precompile Headers
  2538. std::string pchHeader =
  2539. this->GeneratorTarget->GetPchHeader(configName, linkLanguage);
  2540. if (this->MSTools && vcxproj == this->ProjectType && pchHeader.empty()) {
  2541. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  2542. }
  2543. // Get preprocessor definitions for this directory.
  2544. std::string defineFlags = this->Makefile->GetDefineFlags();
  2545. if (this->MSTools) {
  2546. if (this->ProjectType == vcxproj) {
  2547. clOptions.FixExceptionHandlingDefault();
  2548. if (this->GlobalGenerator->GetVersion() >=
  2549. cmGlobalVisualStudioGenerator::VS15) {
  2550. // Toolsets that come with VS 2017 may now enable UseFullPaths
  2551. // by default and there is no negative /FC option that projects
  2552. // can use to switch it back. Older toolsets disable this by
  2553. // default anyway so this will not hurt them. If the project
  2554. // is using an explicit /FC option then parsing flags will
  2555. // replace this setting with "true" below.
  2556. clOptions.AddFlag("UseFullPaths", "false");
  2557. }
  2558. clOptions.AddFlag("AssemblerListingLocation", "$(IntDir)");
  2559. }
  2560. }
  2561. // check for managed C++ assembly compiler flag. This overrides any
  2562. // /clr* compiler flags which may be defined in the flags variable(s).
  2563. if (this->ProjectType != csproj) {
  2564. // Warn if /clr was added manually. This should not be done
  2565. // anymore, because cmGeneratorTarget may not be aware that the
  2566. // target uses C++/CLI.
  2567. if (flags.find("/clr") != std::string::npos ||
  2568. defineFlags.find("/clr") != std::string::npos) {
  2569. if (configName == this->Configurations[0]) {
  2570. std::string message = "For the target \"" +
  2571. this->GeneratorTarget->GetName() +
  2572. "\" the /clr compiler flag was added manually. " +
  2573. "Set usage of C++/CLI by setting COMMON_LANGUAGE_RUNTIME "
  2574. "target property.";
  2575. this->Makefile->IssueMessage(MessageType::WARNING, message);
  2576. }
  2577. }
  2578. if (auto* clr =
  2579. this->GeneratorTarget->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  2580. std::string clrString = clr;
  2581. if (!clrString.empty()) {
  2582. clrString = ":" + clrString;
  2583. }
  2584. flags += " /clr" + clrString;
  2585. }
  2586. }
  2587. clOptions.Parse(flags);
  2588. clOptions.Parse(defineFlags);
  2589. std::vector<std::string> targetDefines;
  2590. switch (this->ProjectType) {
  2591. case vcxproj:
  2592. if (!langForClCompile.empty()) {
  2593. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  2594. langForClCompile);
  2595. }
  2596. break;
  2597. case csproj:
  2598. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  2599. "CSharp");
  2600. break;
  2601. }
  2602. clOptions.AddDefines(targetDefines);
  2603. if (this->ProjectType == csproj) {
  2604. clOptions.AppendFlag("DefineConstants", targetDefines);
  2605. }
  2606. // Get includes for this target
  2607. if (!this->LangForClCompile.empty()) {
  2608. clOptions.AddIncludes(
  2609. this->GetIncludes(configName, this->LangForClCompile));
  2610. }
  2611. if (this->MSTools) {
  2612. clOptions.SetVerboseMakefile(
  2613. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  2614. }
  2615. // Add a definition for the configuration name.
  2616. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  2617. clOptions.AddDefine(configDefine);
  2618. if (const std::string* exportMacro =
  2619. this->GeneratorTarget->GetExportMacro()) {
  2620. clOptions.AddDefine(*exportMacro);
  2621. }
  2622. if (this->MSTools) {
  2623. // If we have the VS_WINRT_COMPONENT or CMAKE_VS_WINRT_BY_DEFAULT
  2624. // set then force Compile as WinRT.
  2625. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") ||
  2626. this->Makefile->IsOn("CMAKE_VS_WINRT_BY_DEFAULT")) {
  2627. clOptions.AddFlag("CompileAsWinRT", "true");
  2628. // For WinRT components, add the _WINRT_DLL define to produce a lib
  2629. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2630. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2631. clOptions.AddDefine("_WINRT_DLL");
  2632. }
  2633. } else if (this->GlobalGenerator->TargetsWindowsStore() ||
  2634. this->GlobalGenerator->TargetsWindowsPhone()) {
  2635. if (!clOptions.IsWinRt()) {
  2636. clOptions.AddFlag("CompileAsWinRT", "false");
  2637. }
  2638. }
  2639. if (const char* winRT = clOptions.GetFlag("CompileAsWinRT")) {
  2640. if (cmIsOn(winRT)) {
  2641. this->TargetCompileAsWinRT = true;
  2642. }
  2643. }
  2644. }
  2645. if (this->ProjectType != csproj && clOptions.IsManaged()) {
  2646. this->Managed = true;
  2647. std::string managedType = clOptions.GetFlag("CompileAsManaged");
  2648. if (managedType == "Safe" || managedType == "Pure") {
  2649. // force empty calling convention if safe clr is used
  2650. clOptions.AddFlag("CallingConvention", "");
  2651. }
  2652. // The default values of these flags are incompatible to
  2653. // managed assemblies. We have to force valid values if
  2654. // the target is a managed C++ target.
  2655. clOptions.AddFlag("ExceptionHandling", "Async");
  2656. clOptions.AddFlag("BasicRuntimeChecks", "Default");
  2657. }
  2658. if (this->ProjectType == csproj) {
  2659. // /nowin32manifest overrides /win32manifest: parameter
  2660. if (clOptions.HasFlag("NoWin32Manifest")) {
  2661. clOptions.RemoveFlag("ApplicationManifest");
  2662. }
  2663. }
  2664. if (const char* s = clOptions.GetFlag("SpectreMitigation")) {
  2665. this->SpectreMitigation[configName] = s;
  2666. clOptions.RemoveFlag("SpectreMitigation");
  2667. }
  2668. this->ClOptions[configName] = std::move(pOptions);
  2669. return true;
  2670. }
  2671. void cmVisualStudio10TargetGenerator::WriteClOptions(
  2672. Elem& e1, std::string const& configName)
  2673. {
  2674. Options& clOptions = *(this->ClOptions[configName]);
  2675. if (this->ProjectType == csproj) {
  2676. return;
  2677. }
  2678. Elem e2(e1, "ClCompile");
  2679. OptionsHelper oh(clOptions, e2);
  2680. oh.PrependInheritedString("AdditionalOptions");
  2681. oh.OutputAdditionalIncludeDirectories(this->LangForClCompile);
  2682. oh.OutputFlagMap();
  2683. oh.OutputPreprocessorDefinitions(this->LangForClCompile);
  2684. if (this->NsightTegra) {
  2685. if (const char* processMax =
  2686. this->GeneratorTarget->GetProperty("ANDROID_PROCESS_MAX")) {
  2687. e2.Element("ProcessMax", processMax);
  2688. }
  2689. }
  2690. if (this->MSTools) {
  2691. cmsys::RegularExpression clangToolset("v[0-9]+_clang_.*");
  2692. const char* toolset = this->GlobalGenerator->GetPlatformToolset();
  2693. if (toolset && clangToolset.find(toolset)) {
  2694. e2.Element("ObjectFileName", "$(IntDir)%(filename).obj");
  2695. } else {
  2696. e2.Element("ObjectFileName", "$(IntDir)");
  2697. }
  2698. // If not in debug mode, write the DebugInformationFormat field
  2699. // without value so PDBs don't get generated uselessly. Each tag
  2700. // goes on its own line because Visual Studio corrects it this
  2701. // way when saving the project after CMake generates it.
  2702. if (!clOptions.IsDebug()) {
  2703. Elem e3(e2, "DebugInformationFormat");
  2704. e3.SetHasElements();
  2705. }
  2706. // Specify the compiler program database file if configured.
  2707. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  2708. if (!pdb.empty()) {
  2709. if (this->GlobalGenerator->IsCudaEnabled()) {
  2710. // CUDA does not quote paths with spaces correctly when forwarding
  2711. // this to the host compiler. Use a relative path to avoid spaces.
  2712. // FIXME: We can likely do this even when CUDA is not involved,
  2713. // but for now we will make a minimal change.
  2714. pdb = this->ConvertPath(pdb, true);
  2715. }
  2716. ConvertToWindowsSlash(pdb);
  2717. e2.Element("ProgramDataBaseFileName", pdb);
  2718. }
  2719. // add AdditionalUsingDirectories
  2720. if (this->AdditionalUsingDirectories.count(configName) > 0) {
  2721. std::string dirs;
  2722. for (auto u : this->AdditionalUsingDirectories[configName]) {
  2723. if (!dirs.empty()) {
  2724. dirs.append(";");
  2725. }
  2726. dirs.append(u);
  2727. }
  2728. e2.Element("AdditionalUsingDirectories", dirs);
  2729. }
  2730. }
  2731. }
  2732. bool cmVisualStudio10TargetGenerator::ComputeRcOptions()
  2733. {
  2734. for (std::string const& c : this->Configurations) {
  2735. if (!this->ComputeRcOptions(c)) {
  2736. return false;
  2737. }
  2738. }
  2739. return true;
  2740. }
  2741. bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
  2742. std::string const& configName)
  2743. {
  2744. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2745. auto pOptions = cm::make_unique<Options>(
  2746. this->LocalGenerator, Options::ResourceCompiler, gg->GetRcFlagTable());
  2747. Options& rcOptions = *pOptions;
  2748. std::string CONFIG = cmSystemTools::UpperCase(configName);
  2749. std::string rcConfigFlagsVar = "CMAKE_RC_FLAGS_" + CONFIG;
  2750. std::string flags = this->Makefile->GetSafeDefinition("CMAKE_RC_FLAGS") +
  2751. " " + this->Makefile->GetSafeDefinition(rcConfigFlagsVar);
  2752. rcOptions.Parse(flags);
  2753. // For historical reasons, add the C preprocessor defines to RC.
  2754. Options& clOptions = *(this->ClOptions[configName]);
  2755. rcOptions.AddDefines(clOptions.GetDefines());
  2756. // Get includes for this target
  2757. rcOptions.AddIncludes(this->GetIncludes(configName, "RC"));
  2758. this->RcOptions[configName] = std::move(pOptions);
  2759. return true;
  2760. }
  2761. void cmVisualStudio10TargetGenerator::WriteRCOptions(
  2762. Elem& e1, std::string const& configName)
  2763. {
  2764. if (!this->MSTools) {
  2765. return;
  2766. }
  2767. Elem e2(e1, "ResourceCompile");
  2768. OptionsHelper rcOptions(*(this->RcOptions[configName]), e2);
  2769. rcOptions.OutputPreprocessorDefinitions("RC");
  2770. rcOptions.OutputAdditionalIncludeDirectories("RC");
  2771. rcOptions.PrependInheritedString("AdditionalOptions");
  2772. rcOptions.OutputFlagMap();
  2773. }
  2774. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions()
  2775. {
  2776. if (!this->GlobalGenerator->IsCudaEnabled()) {
  2777. return true;
  2778. }
  2779. for (std::string const& c : this->Configurations) {
  2780. if (!this->ComputeCudaOptions(c)) {
  2781. return false;
  2782. }
  2783. }
  2784. return true;
  2785. }
  2786. bool cmVisualStudio10TargetGenerator::ComputeCudaOptions(
  2787. std::string const& configName)
  2788. {
  2789. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2790. auto pOptions = cm::make_unique<Options>(
  2791. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  2792. Options& cudaOptions = *pOptions;
  2793. // Get compile flags for CUDA in this directory.
  2794. std::string flags;
  2795. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget, "CUDA",
  2796. configName);
  2797. this->LocalGenerator->AddCompileOptions(flags, this->GeneratorTarget, "CUDA",
  2798. configName);
  2799. // Get preprocessor definitions for this directory.
  2800. std::string defineFlags = this->Makefile->GetDefineFlags();
  2801. cudaOptions.Parse(flags);
  2802. cudaOptions.Parse(defineFlags);
  2803. cudaOptions.ParseFinish();
  2804. // If we haven't explicitly enabled GPU debug information
  2805. // explicitly disable it
  2806. if (!cudaOptions.HasFlag("GPUDebugInfo")) {
  2807. cudaOptions.AddFlag("GPUDebugInfo", "false");
  2808. }
  2809. // The extension on object libraries the CUDA gives isn't
  2810. // consistent with how MSVC generates object libraries for C+, so set
  2811. // the default to not have any extension
  2812. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).obj");
  2813. bool notPtx = true;
  2814. if (this->GeneratorTarget->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  2815. cudaOptions.AddFlag("GenerateRelocatableDeviceCode", "true");
  2816. } else if (this->GeneratorTarget->GetPropertyAsBool(
  2817. "CUDA_PTX_COMPILATION")) {
  2818. cudaOptions.AddFlag("NvccCompilation", "ptx");
  2819. // We drop the %(Extension) component as CMake expects all PTX files
  2820. // to not have the source file extension at all
  2821. cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).ptx");
  2822. notPtx = false;
  2823. }
  2824. if (notPtx &&
  2825. cmSystemTools::VersionCompareGreaterEq(
  2826. "8.0", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  2827. // Explicitly state that we want this file to be treated as a
  2828. // CUDA file no matter what the file extensions is
  2829. // This is only needed for < CUDA 9
  2830. cudaOptions.AppendFlagString("AdditionalOptions", "-x cu");
  2831. }
  2832. // Specify the compiler program database file if configured.
  2833. std::string pdb = this->GeneratorTarget->GetCompilePDBPath(configName);
  2834. if (!pdb.empty()) {
  2835. // CUDA does not make the directory if it is non-standard.
  2836. std::string const pdbDir = cmSystemTools::GetFilenamePath(pdb);
  2837. cmSystemTools::MakeDirectory(pdbDir);
  2838. if (cmSystemTools::VersionCompareGreaterEq(
  2839. "9.2", this->GlobalGenerator->GetPlatformToolsetCudaString())) {
  2840. // CUDA does not have a field for this and does not honor the
  2841. // ProgramDataBaseFileName field in ClCompile. Work around this
  2842. // limitation by creating the directory and passing the flag ourselves.
  2843. pdb = this->ConvertPath(pdb, true);
  2844. ConvertToWindowsSlash(pdb);
  2845. std::string const clFd = "-Xcompiler=\"-Fd\\\"" + pdb + "\\\"\"";
  2846. cudaOptions.AppendFlagString("AdditionalOptions", clFd);
  2847. }
  2848. }
  2849. // CUDA automatically passes the proper '--machine' flag to nvcc
  2850. // for the current architecture, but does not reflect this default
  2851. // in the user-visible IDE settings. Set it explicitly.
  2852. if (this->Platform == "x64") {
  2853. cudaOptions.AddFlag("TargetMachinePlatform", "64");
  2854. }
  2855. // Convert the host compiler options to the toolset's abstractions
  2856. // using a secondary flag table.
  2857. cudaOptions.ClearTables();
  2858. cudaOptions.AddTable(gg->GetCudaHostFlagTable());
  2859. cudaOptions.Reparse("AdditionalCompilerOptions");
  2860. // `CUDA 8.0.targets` places AdditionalCompilerOptions before nvcc!
  2861. // Pass them through -Xcompiler in AdditionalOptions instead.
  2862. if (const char* acoPtr = cudaOptions.GetFlag("AdditionalCompilerOptions")) {
  2863. std::string aco = acoPtr;
  2864. cudaOptions.RemoveFlag("AdditionalCompilerOptions");
  2865. if (!aco.empty()) {
  2866. aco = this->LocalGenerator->EscapeForShell(aco, false);
  2867. cudaOptions.AppendFlagString("AdditionalOptions", "-Xcompiler=" + aco);
  2868. }
  2869. }
  2870. cudaOptions.FixCudaCodeGeneration();
  2871. std::vector<std::string> targetDefines;
  2872. this->GeneratorTarget->GetCompileDefinitions(targetDefines, configName,
  2873. "CUDA");
  2874. cudaOptions.AddDefines(targetDefines);
  2875. // Add a definition for the configuration name.
  2876. std::string configDefine = cmStrCat("CMAKE_INTDIR=\"", configName, '"');
  2877. cudaOptions.AddDefine(configDefine);
  2878. if (const std::string* exportMacro =
  2879. this->GeneratorTarget->GetExportMacro()) {
  2880. cudaOptions.AddDefine(*exportMacro);
  2881. }
  2882. // Get includes for this target
  2883. cudaOptions.AddIncludes(this->GetIncludes(configName, "CUDA"));
  2884. cudaOptions.AddFlag("UseHostInclude", "false");
  2885. this->CudaOptions[configName] = std::move(pOptions);
  2886. return true;
  2887. }
  2888. void cmVisualStudio10TargetGenerator::WriteCudaOptions(
  2889. Elem& e1, std::string const& configName)
  2890. {
  2891. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  2892. return;
  2893. }
  2894. Elem e2(e1, "CudaCompile");
  2895. OptionsHelper cudaOptions(*(this->CudaOptions[configName]), e2);
  2896. cudaOptions.OutputAdditionalIncludeDirectories("CUDA");
  2897. cudaOptions.OutputPreprocessorDefinitions("CUDA");
  2898. cudaOptions.PrependInheritedString("AdditionalOptions");
  2899. cudaOptions.OutputFlagMap();
  2900. }
  2901. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions()
  2902. {
  2903. if (!this->GlobalGenerator->IsCudaEnabled()) {
  2904. return true;
  2905. }
  2906. for (std::string const& c : this->Configurations) {
  2907. if (!this->ComputeCudaLinkOptions(c)) {
  2908. return false;
  2909. }
  2910. }
  2911. return true;
  2912. }
  2913. bool cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions(
  2914. std::string const& configName)
  2915. {
  2916. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  2917. auto pOptions = cm::make_unique<Options>(
  2918. this->LocalGenerator, Options::CudaCompiler, gg->GetCudaFlagTable());
  2919. Options& cudaLinkOptions = *pOptions;
  2920. // Determine if we need to do a device link
  2921. const bool doDeviceLinking = requireDeviceLinking(
  2922. *this->GeneratorTarget, *this->LocalGenerator, configName);
  2923. cudaLinkOptions.AddFlag("PerformDeviceLink",
  2924. doDeviceLinking ? "true" : "false");
  2925. // Suppress deprecation warnings for default GPU targets during device link.
  2926. if (cmSystemTools::VersionCompareGreaterEq(
  2927. this->GlobalGenerator->GetPlatformToolsetCudaString(), "8.0")) {
  2928. cudaLinkOptions.AppendFlagString("AdditionalOptions",
  2929. "-Wno-deprecated-gpu-targets");
  2930. }
  2931. // For static libraries that have device linking enabled compute
  2932. // the libraries
  2933. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY &&
  2934. doDeviceLinking) {
  2935. cmComputeLinkInformation* pcli =
  2936. this->GeneratorTarget->GetLinkInformation(configName);
  2937. if (!pcli) {
  2938. cmSystemTools::Error(
  2939. "CMake can not compute cmComputeLinkInformation for target: " +
  2940. this->Name);
  2941. return false;
  2942. }
  2943. // Would like to use:
  2944. // cmLinkLineDeviceComputer computer(this->LocalGenerator,
  2945. // this->LocalGenerator->GetStateSnapshot().GetDirectory());
  2946. // std::string computed_libs = computer.ComputeLinkLibraries(cli,
  2947. // std::string{}); but it outputs in "<libA> <libB>" format instead of
  2948. // "<libA>;<libB>"
  2949. // Note:
  2950. // Any modification of this algorithm should be reflected also in
  2951. // cmLinkLineDeviceComputer
  2952. cmComputeLinkInformation& cli = *pcli;
  2953. std::vector<std::string> libVec;
  2954. const std::string currentBinDir =
  2955. this->LocalGenerator->GetCurrentBinaryDirectory();
  2956. const auto& libs = cli.GetItems();
  2957. for (cmComputeLinkInformation::Item const& l : libs) {
  2958. if (l.Target) {
  2959. auto managedType = l.Target->GetManagedType(configName);
  2960. // Do not allow C# targets to be added to the LIB listing. LIB files
  2961. // are used for linking C++ dependencies. C# libraries do not have lib
  2962. // files. Instead, they compile down to C# reference libraries (DLL
  2963. // files). The
  2964. // `<ProjectReference>` elements added to the vcxproj are enough for
  2965. // the IDE to deduce the DLL file required by other C# projects that
  2966. // need its reference library.
  2967. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  2968. continue;
  2969. }
  2970. const auto type = l.Target->GetType();
  2971. bool skip = false;
  2972. switch (type) {
  2973. case cmStateEnums::SHARED_LIBRARY:
  2974. case cmStateEnums::MODULE_LIBRARY:
  2975. case cmStateEnums::INTERFACE_LIBRARY:
  2976. skip = true;
  2977. break;
  2978. case cmStateEnums::STATIC_LIBRARY:
  2979. skip = l.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
  2980. break;
  2981. default:
  2982. break;
  2983. }
  2984. if (skip) {
  2985. continue;
  2986. }
  2987. }
  2988. if (l.IsPath) {
  2989. std::string path = this->LocalGenerator->MaybeConvertToRelativePath(
  2990. currentBinDir, l.Value.Value);
  2991. ConvertToWindowsSlash(path);
  2992. if (!cmVS10IsTargetsFile(l.Value.Value)) {
  2993. libVec.push_back(path);
  2994. }
  2995. } else {
  2996. libVec.push_back(l.Value.Value);
  2997. }
  2998. }
  2999. cudaLinkOptions.AddFlag("AdditionalDependencies", libVec);
  3000. }
  3001. this->CudaLinkOptions[configName] = std::move(pOptions);
  3002. return true;
  3003. }
  3004. void cmVisualStudio10TargetGenerator::WriteCudaLinkOptions(
  3005. Elem& e1, std::string const& configName)
  3006. {
  3007. if (this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  3008. return;
  3009. }
  3010. if (!this->MSTools || !this->GlobalGenerator->IsCudaEnabled()) {
  3011. return;
  3012. }
  3013. Elem e2(e1, "CudaLink");
  3014. OptionsHelper cudaLinkOptions(*(this->CudaLinkOptions[configName]), e2);
  3015. cudaLinkOptions.OutputFlagMap();
  3016. }
  3017. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions()
  3018. {
  3019. if (!this->GlobalGenerator->IsMasmEnabled()) {
  3020. return true;
  3021. }
  3022. for (std::string const& c : this->Configurations) {
  3023. if (!this->ComputeMasmOptions(c)) {
  3024. return false;
  3025. }
  3026. }
  3027. return true;
  3028. }
  3029. bool cmVisualStudio10TargetGenerator::ComputeMasmOptions(
  3030. std::string const& configName)
  3031. {
  3032. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3033. auto pOptions = cm::make_unique<Options>(
  3034. this->LocalGenerator, Options::MasmCompiler, gg->GetMasmFlagTable());
  3035. Options& masmOptions = *pOptions;
  3036. std::string flags;
  3037. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3038. "ASM_MASM", configName);
  3039. masmOptions.Parse(flags);
  3040. // Get includes for this target
  3041. masmOptions.AddIncludes(this->GetIncludes(configName, "ASM_MASM"));
  3042. this->MasmOptions[configName] = std::move(pOptions);
  3043. return true;
  3044. }
  3045. void cmVisualStudio10TargetGenerator::WriteMasmOptions(
  3046. Elem& e1, std::string const& configName)
  3047. {
  3048. if (!this->MSTools || !this->GlobalGenerator->IsMasmEnabled()) {
  3049. return;
  3050. }
  3051. Elem e2(e1, "MASM");
  3052. // Preprocessor definitions and includes are shared with clOptions.
  3053. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3054. clOptions.OutputPreprocessorDefinitions("ASM_MASM");
  3055. OptionsHelper masmOptions(*(this->MasmOptions[configName]), e2);
  3056. masmOptions.OutputAdditionalIncludeDirectories("ASM_MASM");
  3057. masmOptions.PrependInheritedString("AdditionalOptions");
  3058. masmOptions.OutputFlagMap();
  3059. }
  3060. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions()
  3061. {
  3062. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3063. return true;
  3064. }
  3065. for (std::string const& c : this->Configurations) {
  3066. if (!this->ComputeNasmOptions(c)) {
  3067. return false;
  3068. }
  3069. }
  3070. return true;
  3071. }
  3072. bool cmVisualStudio10TargetGenerator::ComputeNasmOptions(
  3073. std::string const& configName)
  3074. {
  3075. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3076. auto pOptions = cm::make_unique<Options>(
  3077. this->LocalGenerator, Options::NasmCompiler, gg->GetNasmFlagTable());
  3078. Options& nasmOptions = *pOptions;
  3079. std::string flags;
  3080. this->LocalGenerator->AddLanguageFlags(flags, this->GeneratorTarget,
  3081. "ASM_NASM", configName);
  3082. flags += " -f";
  3083. flags += this->Makefile->GetSafeDefinition("CMAKE_ASM_NASM_OBJECT_FORMAT");
  3084. nasmOptions.Parse(flags);
  3085. // Get includes for this target
  3086. nasmOptions.AddIncludes(this->GetIncludes(configName, "ASM_NASM"));
  3087. this->NasmOptions[configName] = std::move(pOptions);
  3088. return true;
  3089. }
  3090. void cmVisualStudio10TargetGenerator::WriteNasmOptions(
  3091. Elem& e1, std::string const& configName)
  3092. {
  3093. if (!this->GlobalGenerator->IsNasmEnabled()) {
  3094. return;
  3095. }
  3096. Elem e2(e1, "NASM");
  3097. std::vector<std::string> includes =
  3098. this->GetIncludes(configName, "ASM_NASM");
  3099. OptionsHelper nasmOptions(*(this->NasmOptions[configName]), e2);
  3100. nasmOptions.OutputAdditionalIncludeDirectories("ASM_NASM");
  3101. nasmOptions.OutputFlagMap();
  3102. nasmOptions.PrependInheritedString("AdditionalOptions");
  3103. nasmOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3104. // Preprocessor definitions and includes are shared with clOptions.
  3105. OptionsHelper clOptions(*(this->ClOptions[configName]), e2);
  3106. clOptions.OutputPreprocessorDefinitions("ASM_NASM");
  3107. }
  3108. void cmVisualStudio10TargetGenerator::WriteLibOptions(
  3109. Elem& e1, std::string const& config)
  3110. {
  3111. if (this->GeneratorTarget->GetType() != cmStateEnums::STATIC_LIBRARY &&
  3112. this->GeneratorTarget->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  3113. return;
  3114. }
  3115. const std::string& linkLanguage =
  3116. this->GeneratorTarget->GetLinkClosure(config)->LinkerLanguage;
  3117. std::string libflags;
  3118. this->LocalGenerator->GetStaticLibraryFlags(
  3119. libflags, cmSystemTools::UpperCase(config), linkLanguage,
  3120. this->GeneratorTarget);
  3121. if (!libflags.empty()) {
  3122. Elem e2(e1, "Lib");
  3123. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3124. cmVS10GeneratorOptions libOptions(this->LocalGenerator,
  3125. cmVisualStudioGeneratorOptions::Linker,
  3126. gg->GetLibFlagTable(), this);
  3127. libOptions.Parse(libflags);
  3128. OptionsHelper oh(libOptions, e2);
  3129. oh.PrependInheritedString("AdditionalOptions");
  3130. oh.OutputFlagMap();
  3131. }
  3132. // We cannot generate metadata for static libraries. WindowsPhone
  3133. // and WindowsStore tools look at GenerateWindowsMetadata in the
  3134. // Link tool options even for static libraries.
  3135. if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3136. this->GlobalGenerator->TargetsWindowsStore()) {
  3137. Elem e2(e1, "Link");
  3138. e2.Element("GenerateWindowsMetadata", "false");
  3139. }
  3140. }
  3141. void cmVisualStudio10TargetGenerator::WriteManifestOptions(
  3142. Elem& e1, std::string const& config)
  3143. {
  3144. if (this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE &&
  3145. this->GeneratorTarget->GetType() != cmStateEnums::SHARED_LIBRARY &&
  3146. this->GeneratorTarget->GetType() != cmStateEnums::MODULE_LIBRARY) {
  3147. return;
  3148. }
  3149. std::vector<cmSourceFile const*> manifest_srcs;
  3150. this->GeneratorTarget->GetManifests(manifest_srcs, config);
  3151. const char* dpiAware = this->GeneratorTarget->GetProperty("VS_DPI_AWARE");
  3152. if (!manifest_srcs.empty() || dpiAware) {
  3153. Elem e2(e1, "Manifest");
  3154. if (!manifest_srcs.empty()) {
  3155. std::ostringstream oss;
  3156. for (cmSourceFile const* mi : manifest_srcs) {
  3157. std::string m = this->ConvertPath(mi->GetFullPath(), false);
  3158. ConvertToWindowsSlash(m);
  3159. oss << m << ";";
  3160. }
  3161. e2.Element("AdditionalManifestFiles", oss.str());
  3162. }
  3163. if (dpiAware) {
  3164. if (!strcmp(dpiAware, "PerMonitor")) {
  3165. e2.Element("EnableDpiAwareness", "PerMonitorHighDPIAware");
  3166. } else if (cmIsOn(dpiAware)) {
  3167. e2.Element("EnableDpiAwareness", "true");
  3168. } else if (cmIsOff(dpiAware)) {
  3169. e2.Element("EnableDpiAwareness", "false");
  3170. } else {
  3171. cmSystemTools::Error("Bad parameter for VS_DPI_AWARE: " +
  3172. std::string(dpiAware));
  3173. }
  3174. }
  3175. }
  3176. }
  3177. void cmVisualStudio10TargetGenerator::WriteAntBuildOptions(
  3178. Elem& e1, std::string const& configName)
  3179. {
  3180. // Look through the sources for AndroidManifest.xml and use
  3181. // its location as the root source directory.
  3182. std::string rootDir = this->LocalGenerator->GetCurrentSourceDirectory();
  3183. {
  3184. std::vector<cmSourceFile const*> extraSources;
  3185. this->GeneratorTarget->GetExtraSources(extraSources, "");
  3186. for (cmSourceFile const* si : extraSources) {
  3187. if ("androidmanifest.xml" ==
  3188. cmSystemTools::LowerCase(si->GetLocation().GetName())) {
  3189. rootDir = si->GetLocation().GetDirectory();
  3190. break;
  3191. }
  3192. }
  3193. }
  3194. // Tell MSBuild to launch Ant.
  3195. Elem e2(e1, "AntBuild");
  3196. {
  3197. std::string antBuildPath = rootDir;
  3198. ConvertToWindowsSlash(antBuildPath);
  3199. e2.Element("AntBuildPath", antBuildPath);
  3200. }
  3201. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_SKIP_ANT_STEP")) {
  3202. e2.Element("SkipAntStep", "true");
  3203. }
  3204. if (this->GeneratorTarget->GetPropertyAsBool("ANDROID_PROGUARD")) {
  3205. e2.Element("EnableProGuard", "true");
  3206. }
  3207. if (const char* proGuardConfigLocation =
  3208. this->GeneratorTarget->GetProperty("ANDROID_PROGUARD_CONFIG_PATH")) {
  3209. e2.Element("ProGuardConfigLocation", proGuardConfigLocation);
  3210. }
  3211. if (const char* securePropertiesLocation =
  3212. this->GeneratorTarget->GetProperty("ANDROID_SECURE_PROPS_PATH")) {
  3213. e2.Element("SecurePropertiesLocation", securePropertiesLocation);
  3214. }
  3215. if (const char* nativeLibDirectoriesExpression =
  3216. this->GeneratorTarget->GetProperty("ANDROID_NATIVE_LIB_DIRECTORIES")) {
  3217. std::string nativeLibDirs = cmGeneratorExpression::Evaluate(
  3218. nativeLibDirectoriesExpression, this->LocalGenerator, configName);
  3219. e2.Element("NativeLibDirectories", nativeLibDirs);
  3220. }
  3221. if (const char* nativeLibDependenciesExpression =
  3222. this->GeneratorTarget->GetProperty(
  3223. "ANDROID_NATIVE_LIB_DEPENDENCIES")) {
  3224. std::string nativeLibDeps = cmGeneratorExpression::Evaluate(
  3225. nativeLibDependenciesExpression, this->LocalGenerator, configName);
  3226. e2.Element("NativeLibDependencies", nativeLibDeps);
  3227. }
  3228. if (const char* javaSourceDir =
  3229. this->GeneratorTarget->GetProperty("ANDROID_JAVA_SOURCE_DIR")) {
  3230. e2.Element("JavaSourceDir", javaSourceDir);
  3231. }
  3232. if (const char* jarDirectoriesExpression =
  3233. this->GeneratorTarget->GetProperty("ANDROID_JAR_DIRECTORIES")) {
  3234. std::string jarDirectories = cmGeneratorExpression::Evaluate(
  3235. jarDirectoriesExpression, this->LocalGenerator, configName);
  3236. e2.Element("JarDirectories", jarDirectories);
  3237. }
  3238. if (const char* jarDeps =
  3239. this->GeneratorTarget->GetProperty("ANDROID_JAR_DEPENDENCIES")) {
  3240. e2.Element("JarDependencies", jarDeps);
  3241. }
  3242. if (const char* assetsDirectories =
  3243. this->GeneratorTarget->GetProperty("ANDROID_ASSETS_DIRECTORIES")) {
  3244. e2.Element("AssetsDirectories", assetsDirectories);
  3245. }
  3246. {
  3247. std::string manifest_xml = rootDir + "/AndroidManifest.xml";
  3248. ConvertToWindowsSlash(manifest_xml);
  3249. e2.Element("AndroidManifestLocation", manifest_xml);
  3250. }
  3251. if (const char* antAdditionalOptions =
  3252. this->GeneratorTarget->GetProperty("ANDROID_ANT_ADDITIONAL_OPTIONS")) {
  3253. e2.Element("AdditionalOptions",
  3254. std::string(antAdditionalOptions) + " %(AdditionalOptions)");
  3255. }
  3256. }
  3257. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  3258. {
  3259. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
  3260. this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3261. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3262. for (std::string const& c : this->Configurations) {
  3263. if (!this->ComputeLinkOptions(c)) {
  3264. return false;
  3265. }
  3266. }
  3267. }
  3268. return true;
  3269. }
  3270. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions(
  3271. std::string const& config)
  3272. {
  3273. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3274. auto pOptions = cm::make_unique<Options>(
  3275. this->LocalGenerator, Options::Linker, gg->GetLinkFlagTable(), this);
  3276. Options& linkOptions = *pOptions;
  3277. cmGeneratorTarget::LinkClosure const* linkClosure =
  3278. this->GeneratorTarget->GetLinkClosure(config);
  3279. const std::string& linkLanguage = linkClosure->LinkerLanguage;
  3280. if (linkLanguage.empty()) {
  3281. cmSystemTools::Error(
  3282. "CMake can not determine linker language for target: " + this->Name);
  3283. return false;
  3284. }
  3285. std::string CONFIG = cmSystemTools::UpperCase(config);
  3286. const char* linkType = "SHARED";
  3287. if (this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3288. linkType = "MODULE";
  3289. }
  3290. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3291. linkType = "EXE";
  3292. }
  3293. std::string flags;
  3294. std::string linkFlagVarBase = cmStrCat("CMAKE_", linkType, "_LINKER_FLAGS");
  3295. flags += " ";
  3296. flags += this->Makefile->GetRequiredDefinition(linkFlagVarBase);
  3297. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  3298. flags += " ";
  3299. flags += this->Makefile->GetRequiredDefinition(linkFlagVar);
  3300. const char* targetLinkFlags =
  3301. this->GeneratorTarget->GetProperty("LINK_FLAGS");
  3302. if (targetLinkFlags) {
  3303. flags += " ";
  3304. flags += targetLinkFlags;
  3305. }
  3306. std::string flagsProp = cmStrCat("LINK_FLAGS_", CONFIG);
  3307. if (const char* flagsConfig =
  3308. this->GeneratorTarget->GetProperty(flagsProp)) {
  3309. flags += " ";
  3310. flags += flagsConfig;
  3311. }
  3312. std::vector<std::string> opts;
  3313. this->GeneratorTarget->GetLinkOptions(opts, config, linkLanguage);
  3314. // LINK_OPTIONS are escaped.
  3315. this->LocalGenerator->AppendCompileOptions(flags, opts);
  3316. cmComputeLinkInformation* pcli =
  3317. this->GeneratorTarget->GetLinkInformation(config);
  3318. if (!pcli) {
  3319. cmSystemTools::Error(
  3320. "CMake can not compute cmComputeLinkInformation for target: " +
  3321. this->Name);
  3322. return false;
  3323. }
  3324. cmComputeLinkInformation& cli = *pcli;
  3325. std::vector<std::string> libVec;
  3326. std::vector<std::string> vsTargetVec;
  3327. this->AddLibraries(cli, libVec, vsTargetVec, config);
  3328. if (cmContains(linkClosure->Languages, "CUDA") &&
  3329. this->CudaOptions[config] != nullptr) {
  3330. switch (this->CudaOptions[config]->GetCudaRuntime()) {
  3331. case cmVisualStudioGeneratorOptions::CudaRuntimeStatic:
  3332. libVec.push_back("cudadevrt.lib");
  3333. libVec.push_back("cudart_static.lib");
  3334. break;
  3335. case cmVisualStudioGeneratorOptions::CudaRuntimeShared:
  3336. libVec.push_back("cudadevrt.lib");
  3337. libVec.push_back("cudart.lib");
  3338. break;
  3339. case cmVisualStudioGeneratorOptions::CudaRuntimeNone:
  3340. break;
  3341. }
  3342. }
  3343. std::string standardLibsVar =
  3344. cmStrCat("CMAKE_", linkLanguage, "_STANDARD_LIBRARIES");
  3345. std::string const& libs = this->Makefile->GetSafeDefinition(standardLibsVar);
  3346. cmSystemTools::ParseWindowsCommandLine(libs.c_str(), libVec);
  3347. linkOptions.AddFlag("AdditionalDependencies", libVec);
  3348. // Populate TargetsFileAndConfigsVec
  3349. for (std::string const& ti : vsTargetVec) {
  3350. this->AddTargetsFileAndConfigPair(ti, config);
  3351. }
  3352. std::vector<std::string> const& ldirs = cli.GetDirectories();
  3353. std::vector<std::string> linkDirs;
  3354. for (std::string const& d : ldirs) {
  3355. // first just full path
  3356. linkDirs.push_back(d);
  3357. // next path with configuration type Debug, Release, etc
  3358. linkDirs.push_back(d + "/$(Configuration)");
  3359. }
  3360. linkDirs.push_back("%(AdditionalLibraryDirectories)");
  3361. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs);
  3362. cmGeneratorTarget::Names targetNames;
  3363. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3364. targetNames = this->GeneratorTarget->GetExecutableNames(config);
  3365. } else {
  3366. targetNames = this->GeneratorTarget->GetLibraryNames(config);
  3367. }
  3368. if (this->MSTools) {
  3369. if (this->GeneratorTarget->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  3370. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3371. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3372. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3373. if (this->ClOptions[config]->UsingUnicode()) {
  3374. linkOptions.AddFlag("EntryPointSymbol", "wWinMainCRTStartup");
  3375. } else {
  3376. linkOptions.AddFlag("EntryPointSymbol", "WinMainCRTStartup");
  3377. }
  3378. }
  3379. } else {
  3380. linkOptions.AddFlag("SubSystem", "Windows");
  3381. }
  3382. } else {
  3383. if (this->GlobalGenerator->TargetsWindowsCE()) {
  3384. linkOptions.AddFlag("SubSystem", "WindowsCE");
  3385. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  3386. if (this->ClOptions[config]->UsingUnicode()) {
  3387. linkOptions.AddFlag("EntryPointSymbol", "mainWCRTStartup");
  3388. } else {
  3389. linkOptions.AddFlag("EntryPointSymbol", "mainACRTStartup");
  3390. }
  3391. }
  3392. } else {
  3393. linkOptions.AddFlag("SubSystem", "Console");
  3394. };
  3395. }
  3396. if (const char* stackVal = this->Makefile->GetDefinition(
  3397. "CMAKE_" + linkLanguage + "_STACK_SIZE")) {
  3398. linkOptions.AddFlag("StackReserveSize", stackVal);
  3399. }
  3400. linkOptions.AddFlag("GenerateDebugInformation", "false");
  3401. std::string pdb = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config),
  3402. '/', targetNames.PDB);
  3403. std::string imLib =
  3404. cmStrCat(this->GeneratorTarget->GetDirectory(
  3405. config, cmStateEnums::ImportLibraryArtifact),
  3406. '/', targetNames.ImportLibrary);
  3407. linkOptions.AddFlag("ImportLibrary", imLib);
  3408. linkOptions.AddFlag("ProgramDataBaseFile", pdb);
  3409. // A Windows Runtime component uses internal .NET metadata,
  3410. // so does not have an import library.
  3411. if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") &&
  3412. this->GeneratorTarget->GetType() != cmStateEnums::EXECUTABLE) {
  3413. linkOptions.AddFlag("GenerateWindowsMetadata", "true");
  3414. } else if (this->GlobalGenerator->TargetsWindowsPhone() ||
  3415. this->GlobalGenerator->TargetsWindowsStore()) {
  3416. // WindowsPhone and WindowsStore components are in an app container
  3417. // and produce WindowsMetadata. If we are not producing a WINRT
  3418. // component, then do not generate the metadata here.
  3419. linkOptions.AddFlag("GenerateWindowsMetadata", "false");
  3420. }
  3421. if (this->GlobalGenerator->TargetsWindowsPhone() &&
  3422. this->GlobalGenerator->GetSystemVersion() == "8.0") {
  3423. // WindowsPhone 8.0 does not have ole32.
  3424. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries", "ole32.lib");
  3425. }
  3426. } else if (this->NsightTegra) {
  3427. linkOptions.AddFlag("SoName", targetNames.SharedObject);
  3428. }
  3429. linkOptions.Parse(flags);
  3430. linkOptions.FixManifestUACFlags();
  3431. if (this->MSTools) {
  3432. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  3433. this->GeneratorTarget->GetModuleDefinitionInfo(config);
  3434. if (mdi && !mdi->DefFile.empty()) {
  3435. linkOptions.AddFlag("ModuleDefinitionFile", mdi->DefFile);
  3436. }
  3437. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries",
  3438. "%(IgnoreSpecificDefaultLibraries)");
  3439. }
  3440. // VS 2015 without all updates has a v140 toolset whose
  3441. // GenerateDebugInformation expects No/Debug instead of false/true.
  3442. if (gg->GetPlatformToolsetNeedsDebugEnum()) {
  3443. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  3444. if (strcmp(debug, "false") == 0) {
  3445. linkOptions.AddFlag("GenerateDebugInformation", "No");
  3446. } else if (strcmp(debug, "true") == 0) {
  3447. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  3448. }
  3449. }
  3450. }
  3451. // Managed code cannot be linked with /DEBUG:FASTLINK
  3452. if (this->Managed) {
  3453. if (const char* debug = linkOptions.GetFlag("GenerateDebugInformation")) {
  3454. if (strcmp(debug, "DebugFastLink") == 0) {
  3455. linkOptions.AddFlag("GenerateDebugInformation", "Debug");
  3456. }
  3457. }
  3458. }
  3459. this->LinkOptions[config] = std::move(pOptions);
  3460. return true;
  3461. }
  3462. bool cmVisualStudio10TargetGenerator::ComputeLibOptions()
  3463. {
  3464. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3465. for (std::string const& c : this->Configurations) {
  3466. if (!this->ComputeLibOptions(c)) {
  3467. return false;
  3468. }
  3469. }
  3470. }
  3471. return true;
  3472. }
  3473. bool cmVisualStudio10TargetGenerator::ComputeLibOptions(
  3474. std::string const& config)
  3475. {
  3476. cmComputeLinkInformation* pcli =
  3477. this->GeneratorTarget->GetLinkInformation(config);
  3478. if (!pcli) {
  3479. cmSystemTools::Error(
  3480. "CMake can not compute cmComputeLinkInformation for target: " +
  3481. this->Name);
  3482. return false;
  3483. }
  3484. cmComputeLinkInformation& cli = *pcli;
  3485. using ItemVector = cmComputeLinkInformation::ItemVector;
  3486. const ItemVector& libs = cli.GetItems();
  3487. std::string currentBinDir =
  3488. this->LocalGenerator->GetCurrentBinaryDirectory();
  3489. for (cmComputeLinkInformation::Item const& l : libs) {
  3490. if (l.IsPath && cmVS10IsTargetsFile(l.Value.Value)) {
  3491. std::string path = this->LocalGenerator->MaybeConvertToRelativePath(
  3492. currentBinDir, l.Value.Value);
  3493. ConvertToWindowsSlash(path);
  3494. this->AddTargetsFileAndConfigPair(path, config);
  3495. }
  3496. }
  3497. return true;
  3498. }
  3499. void cmVisualStudio10TargetGenerator::WriteLinkOptions(
  3500. Elem& e1, std::string const& config)
  3501. {
  3502. if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3503. this->GeneratorTarget->GetType() > cmStateEnums::MODULE_LIBRARY) {
  3504. return;
  3505. }
  3506. if (this->ProjectType == csproj) {
  3507. return;
  3508. }
  3509. {
  3510. Elem e2(e1, "Link");
  3511. OptionsHelper linkOptions(*(this->LinkOptions[config]), e2);
  3512. linkOptions.PrependInheritedString("AdditionalOptions");
  3513. linkOptions.OutputFlagMap();
  3514. }
  3515. if (!this->GlobalGenerator->NeedLinkLibraryDependencies(
  3516. this->GeneratorTarget)) {
  3517. Elem e2(e1, "ProjectReference");
  3518. e2.Element("LinkLibraryDependencies", "false");
  3519. }
  3520. }
  3521. void cmVisualStudio10TargetGenerator::AddLibraries(
  3522. const cmComputeLinkInformation& cli, std::vector<std::string>& libVec,
  3523. std::vector<std::string>& vsTargetVec, const std::string& config)
  3524. {
  3525. using ItemVector = cmComputeLinkInformation::ItemVector;
  3526. ItemVector const& libs = cli.GetItems();
  3527. std::string currentBinDir =
  3528. this->LocalGenerator->GetCurrentBinaryDirectory();
  3529. for (cmComputeLinkInformation::Item const& l : libs) {
  3530. if (l.Target) {
  3531. auto managedType = l.Target->GetManagedType(config);
  3532. if (managedType != cmGeneratorTarget::ManagedType::Native &&
  3533. this->GeneratorTarget->GetManagedType(config) !=
  3534. cmGeneratorTarget::ManagedType::Native &&
  3535. l.Target->IsImported()) {
  3536. auto location = l.Target->GetFullPath(config);
  3537. if (!location.empty()) {
  3538. ConvertToWindowsSlash(location);
  3539. switch (this->ProjectType) {
  3540. case csproj:
  3541. // If the target we want to "link" to is an imported managed
  3542. // target and this is a C# project, we add a hint reference. This
  3543. // reference is written to project file in
  3544. // WriteDotNetReferences().
  3545. this->DotNetHintReferences[config].push_back(
  3546. DotNetHintReference(l.Target->GetName(), location));
  3547. break;
  3548. case vcxproj:
  3549. // Add path of assembly to list of using-directories, so the
  3550. // managed assembly can be used by '#using <assembly.dll>' in
  3551. // code.
  3552. this->AdditionalUsingDirectories[config].insert(
  3553. cmSystemTools::GetFilenamePath(location));
  3554. break;
  3555. }
  3556. }
  3557. }
  3558. // Do not allow C# targets to be added to the LIB listing. LIB files are
  3559. // used for linking C++ dependencies. C# libraries do not have lib files.
  3560. // Instead, they compile down to C# reference libraries (DLL files). The
  3561. // `<ProjectReference>` elements added to the vcxproj are enough for the
  3562. // IDE to deduce the DLL file required by other C# projects that need its
  3563. // reference library.
  3564. if (managedType == cmGeneratorTarget::ManagedType::Managed) {
  3565. continue;
  3566. }
  3567. }
  3568. if (l.IsPath) {
  3569. std::string path = this->LocalGenerator->MaybeConvertToRelativePath(
  3570. currentBinDir, l.Value.Value);
  3571. ConvertToWindowsSlash(path);
  3572. if (cmVS10IsTargetsFile(l.Value.Value)) {
  3573. vsTargetVec.push_back(path);
  3574. } else {
  3575. libVec.push_back(path);
  3576. }
  3577. } else if (!l.Target ||
  3578. l.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  3579. libVec.push_back(l.Value.Value);
  3580. }
  3581. }
  3582. }
  3583. void cmVisualStudio10TargetGenerator::AddTargetsFileAndConfigPair(
  3584. std::string const& targetsFile, std::string const& config)
  3585. {
  3586. for (TargetsFileAndConfigs& i : this->TargetsFileAndConfigsVec) {
  3587. if (cmSystemTools::ComparePath(targetsFile, i.File)) {
  3588. if (!cmContains(i.Configs, config)) {
  3589. i.Configs.push_back(config);
  3590. }
  3591. return;
  3592. }
  3593. }
  3594. TargetsFileAndConfigs entry;
  3595. entry.File = targetsFile;
  3596. entry.Configs.push_back(config);
  3597. this->TargetsFileAndConfigsVec.push_back(entry);
  3598. }
  3599. void cmVisualStudio10TargetGenerator::WriteMidlOptions(
  3600. Elem& e1, std::string const& configName)
  3601. {
  3602. if (!this->MSTools) {
  3603. return;
  3604. }
  3605. if (this->ProjectType == csproj) {
  3606. return;
  3607. }
  3608. // This processes *any* of the .idl files specified in the project's file
  3609. // list (and passed as the item metadata %(Filename) expressing the rule
  3610. // input filename) into output files at the per-config *build* dir
  3611. // ($(IntDir)) each.
  3612. //
  3613. // IOW, this MIDL section is intended to provide a fully generic syntax
  3614. // content suitable for most cases (read: if you get errors, then it's quite
  3615. // probable that the error is on your side of the .idl setup).
  3616. //
  3617. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  3618. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  3619. // project's file list, otherwise the compiler-side processing won't pick it
  3620. // up (for non-directory form, it ends up looking in project binary dir
  3621. // only). Perhaps there's something to be done to make this more automatic
  3622. // on the CMake side?
  3623. std::vector<std::string> const includes =
  3624. this->GetIncludes(configName, "MIDL");
  3625. std::ostringstream oss;
  3626. for (std::string const& i : includes) {
  3627. oss << i << ";";
  3628. }
  3629. oss << "%(AdditionalIncludeDirectories)";
  3630. Elem e2(e1, "Midl");
  3631. e2.Element("AdditionalIncludeDirectories", oss.str());
  3632. e2.Element("OutputDirectory", "$(ProjectDir)/$(IntDir)");
  3633. e2.Element("HeaderFileName", "%(Filename).h");
  3634. e2.Element("TypeLibraryName", "%(Filename).tlb");
  3635. e2.Element("InterfaceIdentifierFileName", "%(Filename)_i.c");
  3636. e2.Element("ProxyFileName", "%(Filename)_p.c");
  3637. }
  3638. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups(Elem& e0)
  3639. {
  3640. if (this->ProjectType == csproj) {
  3641. return;
  3642. }
  3643. for (const std::string& c : this->Configurations) {
  3644. Elem e1(e0, "ItemDefinitionGroup");
  3645. e1.Attribute("Condition", this->CalcCondition(c));
  3646. // output cl compile flags <ClCompile></ClCompile>
  3647. if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
  3648. this->WriteClOptions(e1, c);
  3649. // output rc compile flags <ResourceCompile></ResourceCompile>
  3650. this->WriteRCOptions(e1, c);
  3651. this->WriteCudaOptions(e1, c);
  3652. this->WriteMasmOptions(e1, c);
  3653. this->WriteNasmOptions(e1, c);
  3654. }
  3655. // output midl flags <Midl></Midl>
  3656. this->WriteMidlOptions(e1, c);
  3657. // write events
  3658. if (this->ProjectType != csproj) {
  3659. this->WriteEvents(e1, c);
  3660. }
  3661. // output link flags <Link></Link>
  3662. this->WriteLinkOptions(e1, c);
  3663. this->WriteCudaLinkOptions(e1, c);
  3664. // output lib flags <Lib></Lib>
  3665. this->WriteLibOptions(e1, c);
  3666. // output manifest flags <Manifest></Manifest>
  3667. this->WriteManifestOptions(e1, c);
  3668. if (this->NsightTegra &&
  3669. this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE &&
  3670. this->GeneratorTarget->GetPropertyAsBool("ANDROID_GUI")) {
  3671. this->WriteAntBuildOptions(e1, c);
  3672. }
  3673. }
  3674. }
  3675. void cmVisualStudio10TargetGenerator::WriteEvents(
  3676. Elem& e1, std::string const& configName)
  3677. {
  3678. bool addedPrelink = false;
  3679. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  3680. this->GeneratorTarget->GetModuleDefinitionInfo(configName);
  3681. if (mdi && mdi->DefFileGenerated) {
  3682. addedPrelink = true;
  3683. std::vector<cmCustomCommand> commands =
  3684. this->GeneratorTarget->GetPreLinkCommands();
  3685. this->GlobalGenerator->AddSymbolExportCommand(this->GeneratorTarget,
  3686. commands, configName);
  3687. this->WriteEvent(e1, "PreLinkEvent", commands, configName);
  3688. }
  3689. if (!addedPrelink) {
  3690. this->WriteEvent(e1, "PreLinkEvent",
  3691. this->GeneratorTarget->GetPreLinkCommands(), configName);
  3692. }
  3693. this->WriteEvent(e1, "PreBuildEvent",
  3694. this->GeneratorTarget->GetPreBuildCommands(), configName);
  3695. this->WriteEvent(e1, "PostBuildEvent",
  3696. this->GeneratorTarget->GetPostBuildCommands(), configName);
  3697. }
  3698. void cmVisualStudio10TargetGenerator::WriteEvent(
  3699. Elem& e1, const char* name, std::vector<cmCustomCommand> const& commands,
  3700. std::string const& configName)
  3701. {
  3702. if (commands.empty()) {
  3703. return;
  3704. }
  3705. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  3706. std::string script;
  3707. const char* pre = "";
  3708. std::string comment;
  3709. for (cmCustomCommand const& cc : commands) {
  3710. cmCustomCommandGenerator ccg(cc, configName, lg);
  3711. if (!ccg.HasOnlyEmptyCommandLines()) {
  3712. comment += pre;
  3713. comment += lg->ConstructComment(ccg);
  3714. script += pre;
  3715. pre = "\n";
  3716. script += lg->ConstructScript(ccg);
  3717. }
  3718. }
  3719. comment = cmVS10EscapeComment(comment);
  3720. if (this->ProjectType != csproj) {
  3721. Elem e2(e1, name);
  3722. e2.Element("Message", comment);
  3723. e2.Element("Command", script);
  3724. } else {
  3725. std::string strippedComment = comment;
  3726. strippedComment.erase(
  3727. std::remove(strippedComment.begin(), strippedComment.end(), '\t'),
  3728. strippedComment.end());
  3729. std::ostringstream oss;
  3730. if (!comment.empty() && !strippedComment.empty()) {
  3731. oss << "echo " << comment << "\n";
  3732. }
  3733. oss << script << "\n";
  3734. e1.Element(name, oss.str());
  3735. }
  3736. }
  3737. void cmVisualStudio10TargetGenerator::WriteProjectReferences(Elem& e0)
  3738. {
  3739. cmGlobalGenerator::TargetDependSet const& unordered =
  3740. this->GlobalGenerator->GetTargetDirectDepends(this->GeneratorTarget);
  3741. using OrderedTargetDependSet =
  3742. cmGlobalVisualStudioGenerator::OrderedTargetDependSet;
  3743. OrderedTargetDependSet depends(unordered, CMAKE_CHECK_BUILD_SYSTEM_TARGET);
  3744. Elem e1(e0, "ItemGroup");
  3745. e1.SetHasElements();
  3746. for (cmGeneratorTarget const* dt : depends) {
  3747. if (dt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3748. continue;
  3749. }
  3750. // skip fortran targets as they can not be processed by MSBuild
  3751. // the only reference will be in the .sln file
  3752. if (this->GlobalGenerator->TargetIsFortranOnly(dt)) {
  3753. continue;
  3754. }
  3755. cmLocalGenerator* lg = dt->GetLocalGenerator();
  3756. std::string name = dt->GetName();
  3757. std::string path;
  3758. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  3759. if (p) {
  3760. path = p;
  3761. } else {
  3762. path = cmStrCat(lg->GetCurrentBinaryDirectory(), '/', dt->GetName(),
  3763. computeProjectFileExtension(dt));
  3764. }
  3765. ConvertToWindowsSlash(path);
  3766. Elem e2(e1, "ProjectReference");
  3767. e2.Attribute("Include", path);
  3768. e2.Element("Project", "{" + this->GlobalGenerator->GetGUID(name) + "}");
  3769. e2.Element("Name", name);
  3770. this->WriteDotNetReferenceCustomTags(e2, name);
  3771. // Don't reference targets that don't produce any output.
  3772. if (dt->GetManagedType("") == cmGeneratorTarget::ManagedType::Undefined) {
  3773. e2.Element("ReferenceOutputAssembly", "false");
  3774. e2.Element("CopyToOutputDirectory", "Never");
  3775. }
  3776. }
  3777. }
  3778. void cmVisualStudio10TargetGenerator::WritePlatformExtensions(Elem& e1)
  3779. {
  3780. // This only applies to Windows 10 apps
  3781. if (this->GlobalGenerator->TargetsWindowsStore() &&
  3782. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  3783. const char* desktopExtensionsVersion =
  3784. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  3785. if (desktopExtensionsVersion) {
  3786. this->WriteSinglePlatformExtension(e1, "WindowsDesktop",
  3787. desktopExtensionsVersion);
  3788. }
  3789. const char* mobileExtensionsVersion =
  3790. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  3791. if (mobileExtensionsVersion) {
  3792. this->WriteSinglePlatformExtension(e1, "WindowsMobile",
  3793. mobileExtensionsVersion);
  3794. }
  3795. }
  3796. }
  3797. void cmVisualStudio10TargetGenerator::WriteSinglePlatformExtension(
  3798. Elem& e1, std::string const& extension, std::string const& version)
  3799. {
  3800. const std::string s = "$([Microsoft.Build.Utilities.ToolLocationHelper]"
  3801. "::GetPlatformExtensionSDKLocation(`" +
  3802. extension + ", Version=" + version +
  3803. "`, $(TargetPlatformIdentifier), $(TargetPlatformVersion), null, "
  3804. "$(ExtensionSDKDirectoryRoot), null))"
  3805. "\\DesignTime\\CommonConfiguration\\Neutral\\" +
  3806. extension + ".props";
  3807. Elem e2(e1, "Import");
  3808. e2.Attribute("Project", s);
  3809. e2.Attribute("Condition", "exists('" + s + "')");
  3810. }
  3811. void cmVisualStudio10TargetGenerator::WriteSDKReferences(Elem& e0)
  3812. {
  3813. std::vector<std::string> sdkReferences;
  3814. std::unique_ptr<Elem> spe1;
  3815. if (const char* vsSDKReferences =
  3816. this->GeneratorTarget->GetProperty("VS_SDK_REFERENCES")) {
  3817. cmExpandList(vsSDKReferences, sdkReferences);
  3818. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  3819. for (std::string const& ri : sdkReferences) {
  3820. Elem(*spe1, "SDKReference").Attribute("Include", ri);
  3821. }
  3822. }
  3823. // This only applies to Windows 10 apps
  3824. if (this->GlobalGenerator->TargetsWindowsStore() &&
  3825. cmHasLiteralPrefix(this->GlobalGenerator->GetSystemVersion(), "10.0")) {
  3826. const char* desktopExtensionsVersion =
  3827. this->GeneratorTarget->GetProperty("VS_DESKTOP_EXTENSIONS_VERSION");
  3828. const char* mobileExtensionsVersion =
  3829. this->GeneratorTarget->GetProperty("VS_MOBILE_EXTENSIONS_VERSION");
  3830. const char* iotExtensionsVersion =
  3831. this->GeneratorTarget->GetProperty("VS_IOT_EXTENSIONS_VERSION");
  3832. if (desktopExtensionsVersion || mobileExtensionsVersion ||
  3833. iotExtensionsVersion) {
  3834. if (!spe1) {
  3835. spe1 = cm::make_unique<Elem>(e0, "ItemGroup");
  3836. }
  3837. if (desktopExtensionsVersion) {
  3838. this->WriteSingleSDKReference(*spe1, "WindowsDesktop",
  3839. desktopExtensionsVersion);
  3840. }
  3841. if (mobileExtensionsVersion) {
  3842. this->WriteSingleSDKReference(*spe1, "WindowsMobile",
  3843. mobileExtensionsVersion);
  3844. }
  3845. if (iotExtensionsVersion) {
  3846. this->WriteSingleSDKReference(*spe1, "WindowsIoT",
  3847. iotExtensionsVersion);
  3848. }
  3849. }
  3850. }
  3851. }
  3852. void cmVisualStudio10TargetGenerator::WriteSingleSDKReference(
  3853. Elem& e1, std::string const& extension, std::string const& version)
  3854. {
  3855. Elem(e1, "SDKReference")
  3856. .Attribute("Include", extension + ", Version=" + version);
  3857. }
  3858. void cmVisualStudio10TargetGenerator::WriteWinRTPackageCertificateKeyFile(
  3859. Elem& e0)
  3860. {
  3861. if ((this->GlobalGenerator->TargetsWindowsStore() ||
  3862. this->GlobalGenerator->TargetsWindowsPhone()) &&
  3863. (cmStateEnums::EXECUTABLE == this->GeneratorTarget->GetType())) {
  3864. std::string pfxFile;
  3865. std::vector<cmSourceFile const*> certificates;
  3866. this->GeneratorTarget->GetCertificates(certificates, "");
  3867. for (cmSourceFile const* si : certificates) {
  3868. pfxFile = this->ConvertPath(si->GetFullPath(), false);
  3869. ConvertToWindowsSlash(pfxFile);
  3870. break;
  3871. }
  3872. if (this->IsMissingFiles &&
  3873. !(this->GlobalGenerator->TargetsWindowsPhone() &&
  3874. this->GlobalGenerator->GetSystemVersion() == "8.0")) {
  3875. // Move the manifest to a project directory to avoid clashes
  3876. std::string artifactDir =
  3877. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  3878. ConvertToWindowsSlash(artifactDir);
  3879. Elem e1(e0, "PropertyGroup");
  3880. e1.Element("AppxPackageArtifactsDir", artifactDir + "\\");
  3881. std::string resourcePriFile =
  3882. this->DefaultArtifactDir + "/resources.pri";
  3883. ConvertToWindowsSlash(resourcePriFile);
  3884. e1.Element("ProjectPriFullPath", resourcePriFile);
  3885. // If we are missing files and we don't have a certificate and
  3886. // aren't targeting WP8.0, add a default certificate
  3887. if (pfxFile.empty()) {
  3888. std::string templateFolder =
  3889. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  3890. pfxFile = this->DefaultArtifactDir + "/Windows_TemporaryKey.pfx";
  3891. cmSystemTools::CopyAFile(templateFolder + "/Windows_TemporaryKey.pfx",
  3892. pfxFile, false);
  3893. ConvertToWindowsSlash(pfxFile);
  3894. this->AddedFiles.push_back(pfxFile);
  3895. this->AddedDefaultCertificate = true;
  3896. }
  3897. e1.Element("PackageCertificateKeyFile", pfxFile);
  3898. std::string thumb = cmSystemTools::ComputeCertificateThumbprint(pfxFile);
  3899. if (!thumb.empty()) {
  3900. e1.Element("PackageCertificateThumbprint", thumb);
  3901. }
  3902. } else if (!pfxFile.empty()) {
  3903. Elem e1(e0, "PropertyGroup");
  3904. e1.Element("PackageCertificateKeyFile", pfxFile);
  3905. std::string thumb = cmSystemTools::ComputeCertificateThumbprint(pfxFile);
  3906. if (!thumb.empty()) {
  3907. e1.Element("PackageCertificateThumbprint", thumb);
  3908. }
  3909. }
  3910. }
  3911. }
  3912. bool cmVisualStudio10TargetGenerator::IsResxHeader(
  3913. const std::string& headerFile)
  3914. {
  3915. std::set<std::string> expectedResxHeaders;
  3916. this->GeneratorTarget->GetExpectedResxHeaders(expectedResxHeaders, "");
  3917. return expectedResxHeaders.count(headerFile) > 0;
  3918. }
  3919. bool cmVisualStudio10TargetGenerator::IsXamlHeader(
  3920. const std::string& headerFile)
  3921. {
  3922. std::set<std::string> expectedXamlHeaders;
  3923. this->GeneratorTarget->GetExpectedXamlHeaders(expectedXamlHeaders, "");
  3924. return expectedXamlHeaders.count(headerFile) > 0;
  3925. }
  3926. bool cmVisualStudio10TargetGenerator::IsXamlSource(
  3927. const std::string& sourceFile)
  3928. {
  3929. std::set<std::string> expectedXamlSources;
  3930. this->GeneratorTarget->GetExpectedXamlSources(expectedXamlSources, "");
  3931. return expectedXamlSources.count(sourceFile) > 0;
  3932. }
  3933. void cmVisualStudio10TargetGenerator::WriteApplicationTypeSettings(Elem& e1)
  3934. {
  3935. cmGlobalVisualStudio10Generator* gg = this->GlobalGenerator;
  3936. bool isAppContainer = false;
  3937. bool const isWindowsPhone = this->GlobalGenerator->TargetsWindowsPhone();
  3938. bool const isWindowsStore = this->GlobalGenerator->TargetsWindowsStore();
  3939. std::string const& rev = this->GlobalGenerator->GetApplicationTypeRevision();
  3940. if (isWindowsPhone || isWindowsStore) {
  3941. e1.Element("ApplicationType",
  3942. (isWindowsPhone ? "Windows Phone" : "Windows Store"));
  3943. e1.Element("DefaultLanguage", "en-US");
  3944. if (rev == "10.0") {
  3945. e1.Element("ApplicationTypeRevision", rev);
  3946. // Visual Studio 14.0 is necessary for building 10.0 apps
  3947. e1.Element("MinimumVisualStudioVersion", "14.0");
  3948. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  3949. isAppContainer = true;
  3950. }
  3951. } else if (rev == "8.1") {
  3952. e1.Element("ApplicationTypeRevision", rev);
  3953. // Visual Studio 12.0 is necessary for building 8.1 apps
  3954. e1.Element("MinimumVisualStudioVersion", "12.0");
  3955. if (this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  3956. isAppContainer = true;
  3957. }
  3958. } else if (rev == "8.0") {
  3959. e1.Element("ApplicationTypeRevision", rev);
  3960. // Visual Studio 11.0 is necessary for building 8.0 apps
  3961. e1.Element("MinimumVisualStudioVersion", "11.0");
  3962. if (isWindowsStore &&
  3963. this->GeneratorTarget->GetType() < cmStateEnums::UTILITY) {
  3964. isAppContainer = true;
  3965. } else if (isWindowsPhone &&
  3966. this->GeneratorTarget->GetType() ==
  3967. cmStateEnums::EXECUTABLE) {
  3968. e1.Element("XapOutputs", "true");
  3969. e1.Element("XapFilename",
  3970. this->Name + "_$(Configuration)_$(Platform).xap");
  3971. }
  3972. }
  3973. }
  3974. if (isAppContainer) {
  3975. e1.Element("AppContainerApplication", "true");
  3976. } else if (this->Platform == "ARM64") {
  3977. e1.Element("WindowsSDKDesktopARM64Support", "true");
  3978. } else if (this->Platform == "ARM") {
  3979. e1.Element("WindowsSDKDesktopARMSupport", "true");
  3980. }
  3981. std::string const& targetPlatformVersion =
  3982. gg->GetWindowsTargetPlatformVersion();
  3983. if (!targetPlatformVersion.empty()) {
  3984. e1.Element("WindowsTargetPlatformVersion", targetPlatformVersion);
  3985. }
  3986. const char* targetPlatformMinVersion = this->GeneratorTarget->GetProperty(
  3987. "VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION");
  3988. if (targetPlatformMinVersion) {
  3989. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformMinVersion);
  3990. } else if (isWindowsStore && rev == "10.0") {
  3991. // If the min version is not set, then use the TargetPlatformVersion
  3992. if (!targetPlatformVersion.empty()) {
  3993. e1.Element("WindowsTargetPlatformMinVersion", targetPlatformVersion);
  3994. }
  3995. }
  3996. // Added IoT Startup Task support
  3997. if (this->GeneratorTarget->GetPropertyAsBool("VS_IOT_STARTUP_TASK")) {
  3998. e1.Element("ContainsStartupTask", "true");
  3999. }
  4000. }
  4001. void cmVisualStudio10TargetGenerator::VerifyNecessaryFiles()
  4002. {
  4003. // For Windows and Windows Phone executables, we will assume that if a
  4004. // manifest is not present that we need to add all the necessary files
  4005. if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE) {
  4006. std::vector<cmSourceFile const*> manifestSources;
  4007. this->GeneratorTarget->GetAppManifest(manifestSources, "");
  4008. {
  4009. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4010. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4011. if (v == "8.0") {
  4012. // Look through the sources for WMAppManifest.xml
  4013. std::vector<cmSourceFile const*> extraSources;
  4014. this->GeneratorTarget->GetExtraSources(extraSources, "");
  4015. bool foundManifest = false;
  4016. for (cmSourceFile const* si : extraSources) {
  4017. // Need to do a lowercase comparison on the filename
  4018. if ("wmappmanifest.xml" ==
  4019. cmSystemTools::LowerCase(si->GetLocation().GetName())) {
  4020. foundManifest = true;
  4021. break;
  4022. }
  4023. }
  4024. if (!foundManifest) {
  4025. this->IsMissingFiles = true;
  4026. }
  4027. } else if (v == "8.1") {
  4028. if (manifestSources.empty()) {
  4029. this->IsMissingFiles = true;
  4030. }
  4031. }
  4032. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4033. if (manifestSources.empty()) {
  4034. if (v == "8.0") {
  4035. this->IsMissingFiles = true;
  4036. } else if (v == "8.1" || cmHasLiteralPrefix(v, "10.0")) {
  4037. this->IsMissingFiles = true;
  4038. }
  4039. }
  4040. }
  4041. }
  4042. }
  4043. }
  4044. void cmVisualStudio10TargetGenerator::WriteMissingFiles(Elem& e1)
  4045. {
  4046. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  4047. if (this->GlobalGenerator->TargetsWindowsPhone()) {
  4048. if (v == "8.0") {
  4049. this->WriteMissingFilesWP80(e1);
  4050. } else if (v == "8.1") {
  4051. this->WriteMissingFilesWP81(e1);
  4052. }
  4053. } else if (this->GlobalGenerator->TargetsWindowsStore()) {
  4054. if (v == "8.0") {
  4055. this->WriteMissingFilesWS80(e1);
  4056. } else if (v == "8.1") {
  4057. this->WriteMissingFilesWS81(e1);
  4058. } else if (cmHasLiteralPrefix(v, "10.0")) {
  4059. this->WriteMissingFilesWS10_0(e1);
  4060. }
  4061. }
  4062. }
  4063. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP80(Elem& e1)
  4064. {
  4065. std::string templateFolder =
  4066. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4067. // For WP80, the manifest needs to be in the same folder as the project
  4068. // this can cause an overwrite problem if projects aren't organized in
  4069. // folders
  4070. std::string manifestFile =
  4071. this->LocalGenerator->GetCurrentBinaryDirectory() + "/WMAppManifest.xml";
  4072. std::string artifactDir =
  4073. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4074. ConvertToWindowsSlash(artifactDir);
  4075. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4076. std::string targetNameXML =
  4077. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4078. cmGeneratedFileStream fout(manifestFile);
  4079. fout.SetCopyIfDifferent(true);
  4080. /* clang-format off */
  4081. fout <<
  4082. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4083. "<Deployment"
  4084. " xmlns=\"http://schemas.microsoft.com/windowsphone/2012/deployment\""
  4085. " AppPlatformVersion=\"8.0\">\n"
  4086. "\t<DefaultLanguage xmlns=\"\" code=\"en-US\"/>\n"
  4087. "\t<App xmlns=\"\" ProductID=\"{" << this->GUID << "}\""
  4088. " Title=\"CMake Test Program\" RuntimeType=\"Modern Native\""
  4089. " Version=\"1.0.0.0\" Genre=\"apps.normal\" Author=\"CMake\""
  4090. " Description=\"Default CMake App\" Publisher=\"CMake\""
  4091. " PublisherID=\"{" << this->GUID << "}\">\n"
  4092. "\t\t<IconPath IsRelative=\"true\" IsResource=\"false\">"
  4093. << artifactDirXML << "\\ApplicationIcon.png</IconPath>\n"
  4094. "\t\t<Capabilities/>\n"
  4095. "\t\t<Tasks>\n"
  4096. "\t\t\t<DefaultTask Name=\"_default\""
  4097. " ImagePath=\"" << targetNameXML << ".exe\" ImageParams=\"\" />\n"
  4098. "\t\t</Tasks>\n"
  4099. "\t\t<Tokens>\n"
  4100. "\t\t\t<PrimaryToken TokenID=\"" << targetNameXML << "Token\""
  4101. " TaskName=\"_default\">\n"
  4102. "\t\t\t\t<TemplateFlip>\n"
  4103. "\t\t\t\t\t<SmallImageURI IsRelative=\"true\" IsResource=\"false\">"
  4104. << artifactDirXML << "\\SmallLogo.png</SmallImageURI>\n"
  4105. "\t\t\t\t\t<Count>0</Count>\n"
  4106. "\t\t\t\t\t<BackgroundImageURI IsRelative=\"true\" IsResource=\"false\">"
  4107. << artifactDirXML << "\\Logo.png</BackgroundImageURI>\n"
  4108. "\t\t\t\t</TemplateFlip>\n"
  4109. "\t\t\t</PrimaryToken>\n"
  4110. "\t\t</Tokens>\n"
  4111. "\t\t<ScreenResolutions>\n"
  4112. "\t\t\t<ScreenResolution Name=\"ID_RESOLUTION_WVGA\" />\n"
  4113. "\t\t</ScreenResolutions>\n"
  4114. "\t</App>\n"
  4115. "</Deployment>\n";
  4116. /* clang-format on */
  4117. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4118. ConvertToWindowsSlash(sourceFile);
  4119. {
  4120. Elem e2(e1, "Xml");
  4121. e2.Attribute("Include", sourceFile);
  4122. e2.Element("SubType", "Designer");
  4123. }
  4124. this->AddedFiles.push_back(sourceFile);
  4125. std::string smallLogo = this->DefaultArtifactDir + "/SmallLogo.png";
  4126. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo.png", smallLogo,
  4127. false);
  4128. ConvertToWindowsSlash(smallLogo);
  4129. Elem(e1, "Image").Attribute("Include", smallLogo);
  4130. this->AddedFiles.push_back(smallLogo);
  4131. std::string logo = this->DefaultArtifactDir + "/Logo.png";
  4132. cmSystemTools::CopyAFile(templateFolder + "/Logo.png", logo, false);
  4133. ConvertToWindowsSlash(logo);
  4134. Elem(e1, "Image").Attribute("Include", logo);
  4135. this->AddedFiles.push_back(logo);
  4136. std::string applicationIcon =
  4137. this->DefaultArtifactDir + "/ApplicationIcon.png";
  4138. cmSystemTools::CopyAFile(templateFolder + "/ApplicationIcon.png",
  4139. applicationIcon, false);
  4140. ConvertToWindowsSlash(applicationIcon);
  4141. Elem(e1, "Image").Attribute("Include", applicationIcon);
  4142. this->AddedFiles.push_back(applicationIcon);
  4143. }
  4144. void cmVisualStudio10TargetGenerator::WriteMissingFilesWP81(Elem& e1)
  4145. {
  4146. std::string manifestFile =
  4147. this->DefaultArtifactDir + "/package.appxManifest";
  4148. std::string artifactDir =
  4149. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4150. ConvertToWindowsSlash(artifactDir);
  4151. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4152. std::string targetNameXML =
  4153. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4154. cmGeneratedFileStream fout(manifestFile);
  4155. fout.SetCopyIfDifferent(true);
  4156. /* clang-format off */
  4157. fout <<
  4158. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4159. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4160. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\""
  4161. " xmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\">\n"
  4162. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4163. " Version=\"1.0.0.0\" />\n"
  4164. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID << "\""
  4165. " PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4166. "\t<Properties>\n"
  4167. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4168. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4169. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4170. "\t</Properties>\n"
  4171. "\t<Prerequisites>\n"
  4172. "\t\t<OSMinVersion>6.3.1</OSMinVersion>\n"
  4173. "\t\t<OSMaxVersionTested>6.3.1</OSMaxVersionTested>\n"
  4174. "\t</Prerequisites>\n"
  4175. "\t<Resources>\n"
  4176. "\t\t<Resource Language=\"x-generate\" />\n"
  4177. "\t</Resources>\n"
  4178. "\t<Applications>\n"
  4179. "\t\t<Application Id=\"App\""
  4180. " Executable=\"" << targetNameXML << ".exe\""
  4181. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4182. "\t\t\t<m2:VisualElements\n"
  4183. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4184. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4185. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4186. "\t\t\t\tForegroundText=\"light\"\n"
  4187. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4188. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4189. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4190. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4191. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4192. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4193. "\t\t\t\t</m2:DefaultTile>\n"
  4194. "\t\t\t\t<m2:SplashScreen"
  4195. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4196. "\t\t\t</m2:VisualElements>\n"
  4197. "\t\t</Application>\n"
  4198. "\t</Applications>\n"
  4199. "</Package>\n";
  4200. /* clang-format on */
  4201. this->WriteCommonMissingFiles(e1, manifestFile);
  4202. }
  4203. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS80(Elem& e1)
  4204. {
  4205. std::string manifestFile =
  4206. this->DefaultArtifactDir + "/package.appxManifest";
  4207. std::string artifactDir =
  4208. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4209. ConvertToWindowsSlash(artifactDir);
  4210. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4211. std::string targetNameXML =
  4212. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4213. cmGeneratedFileStream fout(manifestFile);
  4214. fout.SetCopyIfDifferent(true);
  4215. /* clang-format off */
  4216. fout <<
  4217. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4218. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\">\n"
  4219. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4220. " Version=\"1.0.0.0\" />\n"
  4221. "\t<Properties>\n"
  4222. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4223. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4224. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4225. "\t</Properties>\n"
  4226. "\t<Prerequisites>\n"
  4227. "\t\t<OSMinVersion>6.2.1</OSMinVersion>\n"
  4228. "\t\t<OSMaxVersionTested>6.2.1</OSMaxVersionTested>\n"
  4229. "\t</Prerequisites>\n"
  4230. "\t<Resources>\n"
  4231. "\t\t<Resource Language=\"x-generate\" />\n"
  4232. "\t</Resources>\n"
  4233. "\t<Applications>\n"
  4234. "\t\t<Application Id=\"App\""
  4235. " Executable=\"" << targetNameXML << ".exe\""
  4236. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4237. "\t\t\t<VisualElements"
  4238. " DisplayName=\"" << targetNameXML << "\""
  4239. " Description=\"" << targetNameXML << "\""
  4240. " BackgroundColor=\"#336699\" ForegroundText=\"light\""
  4241. " Logo=\"" << artifactDirXML << "\\Logo.png\""
  4242. " SmallLogo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4243. "\t\t\t\t<DefaultTile ShowName=\"allLogos\""
  4244. " ShortName=\"" << targetNameXML << "\" />\n"
  4245. "\t\t\t\t<SplashScreen"
  4246. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4247. "\t\t\t</VisualElements>\n"
  4248. "\t\t</Application>\n"
  4249. "\t</Applications>\n"
  4250. "</Package>\n";
  4251. /* clang-format on */
  4252. this->WriteCommonMissingFiles(e1, manifestFile);
  4253. }
  4254. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS81(Elem& e1)
  4255. {
  4256. std::string manifestFile =
  4257. this->DefaultArtifactDir + "/package.appxManifest";
  4258. std::string artifactDir =
  4259. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4260. ConvertToWindowsSlash(artifactDir);
  4261. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4262. std::string targetNameXML =
  4263. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4264. cmGeneratedFileStream fout(manifestFile);
  4265. fout.SetCopyIfDifferent(true);
  4266. /* clang-format off */
  4267. fout <<
  4268. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4269. "<Package xmlns=\"http://schemas.microsoft.com/appx/2010/manifest\""
  4270. " xmlns:m2=\"http://schemas.microsoft.com/appx/2013/manifest\">\n"
  4271. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4272. " Version=\"1.0.0.0\" />\n"
  4273. "\t<Properties>\n"
  4274. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4275. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4276. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4277. "\t</Properties>\n"
  4278. "\t<Prerequisites>\n"
  4279. "\t\t<OSMinVersion>6.3</OSMinVersion>\n"
  4280. "\t\t<OSMaxVersionTested>6.3</OSMaxVersionTested>\n"
  4281. "\t</Prerequisites>\n"
  4282. "\t<Resources>\n"
  4283. "\t\t<Resource Language=\"x-generate\" />\n"
  4284. "\t</Resources>\n"
  4285. "\t<Applications>\n"
  4286. "\t\t<Application Id=\"App\""
  4287. " Executable=\"" << targetNameXML << ".exe\""
  4288. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4289. "\t\t\t<m2:VisualElements\n"
  4290. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4291. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4292. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4293. "\t\t\t\tForegroundText=\"light\"\n"
  4294. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4295. "\t\t\t\tSquare30x30Logo=\"" << artifactDirXML << "\\SmallLogo.png\">\n"
  4296. "\t\t\t\t<m2:DefaultTile ShortName=\"" << targetNameXML << "\">\n"
  4297. "\t\t\t\t\t<m2:ShowNameOnTiles>\n"
  4298. "\t\t\t\t\t\t<m2:ShowOn Tile=\"square150x150Logo\" />\n"
  4299. "\t\t\t\t\t</m2:ShowNameOnTiles>\n"
  4300. "\t\t\t\t</m2:DefaultTile>\n"
  4301. "\t\t\t\t<m2:SplashScreen"
  4302. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4303. "\t\t\t</m2:VisualElements>\n"
  4304. "\t\t</Application>\n"
  4305. "\t</Applications>\n"
  4306. "</Package>\n";
  4307. /* clang-format on */
  4308. this->WriteCommonMissingFiles(e1, manifestFile);
  4309. }
  4310. void cmVisualStudio10TargetGenerator::WriteMissingFilesWS10_0(Elem& e1)
  4311. {
  4312. std::string manifestFile =
  4313. this->DefaultArtifactDir + "/package.appxManifest";
  4314. std::string artifactDir =
  4315. this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
  4316. ConvertToWindowsSlash(artifactDir);
  4317. std::string artifactDirXML = cmVS10EscapeXML(artifactDir);
  4318. std::string targetNameXML =
  4319. cmVS10EscapeXML(this->GeneratorTarget->GetName());
  4320. cmGeneratedFileStream fout(manifestFile);
  4321. fout.SetCopyIfDifferent(true);
  4322. /* clang-format off */
  4323. fout <<
  4324. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  4325. "<Package\n\t"
  4326. "xmlns=\"http://schemas.microsoft.com/appx/manifest/foundation/windows10\""
  4327. "\txmlns:mp=\"http://schemas.microsoft.com/appx/2014/phone/manifest\"\n"
  4328. "\txmlns:uap=\"http://schemas.microsoft.com/appx/manifest/uap/windows10\""
  4329. "\n\tIgnorableNamespaces=\"uap mp\">\n\n"
  4330. "\t<Identity Name=\"" << this->GUID << "\" Publisher=\"CN=CMake\""
  4331. " Version=\"1.0.0.0\" />\n"
  4332. "\t<mp:PhoneIdentity PhoneProductId=\"" << this->GUID <<
  4333. "\" PhonePublisherId=\"00000000-0000-0000-0000-000000000000\"/>\n"
  4334. "\t<Properties>\n"
  4335. "\t\t<DisplayName>" << targetNameXML << "</DisplayName>\n"
  4336. "\t\t<PublisherDisplayName>CMake</PublisherDisplayName>\n"
  4337. "\t\t<Logo>" << artifactDirXML << "\\StoreLogo.png</Logo>\n"
  4338. "\t</Properties>\n"
  4339. "\t<Dependencies>\n"
  4340. "\t\t<TargetDeviceFamily Name=\"Windows.Universal\" "
  4341. "MinVersion=\"10.0.0.0\" MaxVersionTested=\"10.0.0.0\" />\n"
  4342. "\t</Dependencies>\n"
  4343. "\t<Resources>\n"
  4344. "\t\t<Resource Language=\"x-generate\" />\n"
  4345. "\t</Resources>\n"
  4346. "\t<Applications>\n"
  4347. "\t\t<Application Id=\"App\""
  4348. " Executable=\"" << targetNameXML << ".exe\""
  4349. " EntryPoint=\"" << targetNameXML << ".App\">\n"
  4350. "\t\t\t<uap:VisualElements\n"
  4351. "\t\t\t\tDisplayName=\"" << targetNameXML << "\"\n"
  4352. "\t\t\t\tDescription=\"" << targetNameXML << "\"\n"
  4353. "\t\t\t\tBackgroundColor=\"#336699\"\n"
  4354. "\t\t\t\tSquare150x150Logo=\"" << artifactDirXML << "\\Logo.png\"\n"
  4355. "\t\t\t\tSquare44x44Logo=\"" << artifactDirXML <<
  4356. "\\SmallLogo44x44.png\">\n"
  4357. "\t\t\t\t<uap:SplashScreen"
  4358. " Image=\"" << artifactDirXML << "\\SplashScreen.png\" />\n"
  4359. "\t\t\t</uap:VisualElements>\n"
  4360. "\t\t</Application>\n"
  4361. "\t</Applications>\n"
  4362. "</Package>\n";
  4363. /* clang-format on */
  4364. this->WriteCommonMissingFiles(e1, manifestFile);
  4365. }
  4366. void cmVisualStudio10TargetGenerator::WriteCommonMissingFiles(
  4367. Elem& e1, const std::string& manifestFile)
  4368. {
  4369. std::string templateFolder =
  4370. cmSystemTools::GetCMakeRoot() + "/Templates/Windows";
  4371. std::string sourceFile = this->ConvertPath(manifestFile, false);
  4372. ConvertToWindowsSlash(sourceFile);
  4373. {
  4374. Elem e2(e1, "AppxManifest");
  4375. e2.Attribute("Include", sourceFile);
  4376. e2.Element("SubType", "Designer");
  4377. }
  4378. this->AddedFiles.push_back(sourceFile);
  4379. std::string smallLogo = this->DefaultArtifactDir + "/SmallLogo.png";
  4380. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo.png", smallLogo,
  4381. false);
  4382. ConvertToWindowsSlash(smallLogo);
  4383. Elem(e1, "Image").Attribute("Include", smallLogo);
  4384. this->AddedFiles.push_back(smallLogo);
  4385. std::string smallLogo44 = this->DefaultArtifactDir + "/SmallLogo44x44.png";
  4386. cmSystemTools::CopyAFile(templateFolder + "/SmallLogo44x44.png", smallLogo44,
  4387. false);
  4388. ConvertToWindowsSlash(smallLogo44);
  4389. Elem(e1, "Image").Attribute("Include", smallLogo44);
  4390. this->AddedFiles.push_back(smallLogo44);
  4391. std::string logo = this->DefaultArtifactDir + "/Logo.png";
  4392. cmSystemTools::CopyAFile(templateFolder + "/Logo.png", logo, false);
  4393. ConvertToWindowsSlash(logo);
  4394. Elem(e1, "Image").Attribute("Include", logo);
  4395. this->AddedFiles.push_back(logo);
  4396. std::string storeLogo = this->DefaultArtifactDir + "/StoreLogo.png";
  4397. cmSystemTools::CopyAFile(templateFolder + "/StoreLogo.png", storeLogo,
  4398. false);
  4399. ConvertToWindowsSlash(storeLogo);
  4400. Elem(e1, "Image").Attribute("Include", storeLogo);
  4401. this->AddedFiles.push_back(storeLogo);
  4402. std::string splashScreen = this->DefaultArtifactDir + "/SplashScreen.png";
  4403. cmSystemTools::CopyAFile(templateFolder + "/SplashScreen.png", splashScreen,
  4404. false);
  4405. ConvertToWindowsSlash(splashScreen);
  4406. Elem(e1, "Image").Attribute("Include", splashScreen);
  4407. this->AddedFiles.push_back(splashScreen);
  4408. if (this->AddedDefaultCertificate) {
  4409. // This file has already been added to the build so don't copy it
  4410. std::string keyFile =
  4411. this->DefaultArtifactDir + "/Windows_TemporaryKey.pfx";
  4412. ConvertToWindowsSlash(keyFile);
  4413. Elem(e1, "None").Attribute("Include", keyFile);
  4414. }
  4415. }
  4416. bool cmVisualStudio10TargetGenerator::ForceOld(const std::string& source) const
  4417. {
  4418. HANDLE h =
  4419. CreateFileW(cmSystemTools::ConvertToWindowsExtendedPath(source).c_str(),
  4420. FILE_WRITE_ATTRIBUTES, FILE_SHARE_WRITE, 0, OPEN_EXISTING,
  4421. FILE_FLAG_BACKUP_SEMANTICS, 0);
  4422. if (!h) {
  4423. return false;
  4424. }
  4425. FILETIME const ftime_20010101 = { 3365781504u, 29389701u };
  4426. if (!SetFileTime(h, &ftime_20010101, &ftime_20010101, &ftime_20010101)) {
  4427. CloseHandle(h);
  4428. return false;
  4429. }
  4430. CloseHandle(h);
  4431. return true;
  4432. }
  4433. void cmVisualStudio10TargetGenerator::GetCSharpSourceProperties(
  4434. cmSourceFile const* sf, std::map<std::string, std::string>& tags)
  4435. {
  4436. if (this->ProjectType == csproj) {
  4437. const cmPropertyMap& props = sf->GetProperties();
  4438. for (const std::string& p : props.GetKeys()) {
  4439. static const std::string propNamePrefix = "VS_CSHARP_";
  4440. if (p.find(propNamePrefix) == 0) {
  4441. std::string tagName = p.substr(propNamePrefix.length());
  4442. if (!tagName.empty()) {
  4443. const std::string val = props.GetPropertyValue(p);
  4444. if (!val.empty()) {
  4445. tags[tagName] = val;
  4446. } else {
  4447. tags.erase(tagName);
  4448. }
  4449. }
  4450. }
  4451. }
  4452. }
  4453. }
  4454. void cmVisualStudio10TargetGenerator::WriteCSharpSourceProperties(
  4455. Elem& e2, const std::map<std::string, std::string>& tags)
  4456. {
  4457. for (const auto& i : tags) {
  4458. e2.Element(i.first.c_str(), i.second);
  4459. }
  4460. }
  4461. void cmVisualStudio10TargetGenerator::GetCSharpSourceLink(
  4462. cmSourceFile const* sf, std::string& link)
  4463. {
  4464. std::string const& sourceFilePath = sf->GetFullPath();
  4465. std::string const& binaryDir = LocalGenerator->GetCurrentBinaryDirectory();
  4466. if (!cmSystemTools::IsSubDirectory(sourceFilePath, binaryDir)) {
  4467. const std::string& stripFromPath =
  4468. this->Makefile->GetCurrentSourceDirectory();
  4469. if (sourceFilePath.find(stripFromPath) == 0) {
  4470. if (const char* l = sf->GetProperty("VS_CSHARP_Link")) {
  4471. link = l;
  4472. } else {
  4473. link = sourceFilePath.substr(stripFromPath.length() + 1);
  4474. }
  4475. ConvertToWindowsSlash(link);
  4476. }
  4477. }
  4478. }
  4479. std::string cmVisualStudio10TargetGenerator::GetCMakeFilePath(
  4480. const char* relativeFilePath) const
  4481. {
  4482. // Always search in the standard modules location.
  4483. std::string path =
  4484. cmStrCat(cmSystemTools::GetCMakeRoot(), '/', relativeFilePath);
  4485. ConvertToWindowsSlash(path);
  4486. return path;
  4487. }