cmVisualStudio10TargetGenerator.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmVisualStudio10TargetGenerator.h"
  11. #include "cmGlobalVisualStudio10Generator.h"
  12. #include "cmGeneratorTarget.h"
  13. #include "cmTarget.h"
  14. #include "cmComputeLinkInformation.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include "cmVisualStudioGeneratorOptions.h"
  19. #include "cmLocalVisualStudio7Generator.h"
  20. #include "cmCustomCommandGenerator.h"
  21. #include "cmVS10CLFlagTable.h"
  22. #include "cmVS10RCFlagTable.h"
  23. #include "cmVS10LinkFlagTable.h"
  24. #include "cmVS10LibFlagTable.h"
  25. #include "cmVS11CLFlagTable.h"
  26. #include "cmVS11RCFlagTable.h"
  27. #include "cmVS11LinkFlagTable.h"
  28. #include "cmVS11LibFlagTable.h"
  29. #include "cmVS12CLFlagTable.h"
  30. #include "cmVS12RCFlagTable.h"
  31. #include "cmVS12LinkFlagTable.h"
  32. #include "cmVS12LibFlagTable.h"
  33. #include "cmVS14CLFlagTable.h"
  34. #include "cmVS14RCFlagTable.h"
  35. #include "cmVS14LinkFlagTable.h"
  36. #include "cmVS14LibFlagTable.h"
  37. #include <cmsys/auto_ptr.hxx>
  38. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetClFlagTable() const
  39. {
  40. if(this->MSTools)
  41. {
  42. cmLocalVisualStudioGenerator::VSVersion
  43. v = this->LocalGenerator->GetVersion();
  44. if(v >= cmLocalVisualStudioGenerator::VS14)
  45. { return cmVS14CLFlagTable; }
  46. else if(v >= cmLocalVisualStudioGenerator::VS12)
  47. { return cmVS12CLFlagTable; }
  48. else if(v == cmLocalVisualStudioGenerator::VS11)
  49. { return cmVS11CLFlagTable; }
  50. else
  51. { return cmVS10CLFlagTable; }
  52. }
  53. return 0;
  54. }
  55. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetRcFlagTable() const
  56. {
  57. if(this->MSTools)
  58. {
  59. cmLocalVisualStudioGenerator::VSVersion
  60. v = this->LocalGenerator->GetVersion();
  61. if(v >= cmLocalVisualStudioGenerator::VS14)
  62. { return cmVS14RCFlagTable; }
  63. else if(v >= cmLocalVisualStudioGenerator::VS12)
  64. { return cmVS12RCFlagTable; }
  65. else if(v == cmLocalVisualStudioGenerator::VS11)
  66. { return cmVS11RCFlagTable; }
  67. else
  68. { return cmVS10RCFlagTable; }
  69. }
  70. return 0;
  71. }
  72. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetLibFlagTable() const
  73. {
  74. if(this->MSTools)
  75. {
  76. cmLocalVisualStudioGenerator::VSVersion
  77. v = this->LocalGenerator->GetVersion();
  78. if(v >= cmLocalVisualStudioGenerator::VS14)
  79. { return cmVS14LibFlagTable; }
  80. else if(v >= cmLocalVisualStudioGenerator::VS12)
  81. { return cmVS12LibFlagTable; }
  82. else if(v == cmLocalVisualStudioGenerator::VS11)
  83. { return cmVS11LibFlagTable; }
  84. else
  85. { return cmVS10LibFlagTable; }
  86. }
  87. return 0;
  88. }
  89. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetLinkFlagTable() const
  90. {
  91. if(this->MSTools)
  92. {
  93. cmLocalVisualStudioGenerator::VSVersion
  94. v = this->LocalGenerator->GetVersion();
  95. if(v >= cmLocalVisualStudioGenerator::VS14)
  96. { return cmVS14LinkFlagTable; }
  97. else if(v >= cmLocalVisualStudioGenerator::VS12)
  98. { return cmVS12LinkFlagTable; }
  99. else if(v == cmLocalVisualStudioGenerator::VS11)
  100. { return cmVS11LinkFlagTable; }
  101. else
  102. { return cmVS10LinkFlagTable; }
  103. }
  104. return 0;
  105. }
  106. static std::string cmVS10EscapeXML(std::string arg)
  107. {
  108. cmSystemTools::ReplaceString(arg, "&", "&amp;");
  109. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  110. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  111. return arg;
  112. }
  113. static std::string cmVS10EscapeComment(std::string comment)
  114. {
  115. // MSBuild takes the CDATA of a <Message></Message> element and just
  116. // does "echo $CDATA" with no escapes. We must encode the string.
  117. // http://technet.microsoft.com/en-us/library/cc772462%28WS.10%29.aspx
  118. std::string echoable;
  119. for(std::string::iterator c = comment.begin(); c != comment.end(); ++c)
  120. {
  121. switch (*c)
  122. {
  123. case '\r': break;
  124. case '\n': echoable += '\t'; break;
  125. case '"': /* no break */
  126. case '|': /* no break */
  127. case '&': /* no break */
  128. case '<': /* no break */
  129. case '>': /* no break */
  130. case '^': echoable += '^'; /* no break */
  131. default: echoable += *c; break;
  132. }
  133. }
  134. return echoable;
  135. }
  136. cmVisualStudio10TargetGenerator::
  137. cmVisualStudio10TargetGenerator(cmTarget* target,
  138. cmGlobalVisualStudio10Generator* gg)
  139. {
  140. this->GlobalGenerator = gg;
  141. this->Target = target;
  142. this->GeneratorTarget = gg->GetGeneratorTarget(target);
  143. this->Makefile = target->GetMakefile();
  144. this->LocalGenerator =
  145. (cmLocalVisualStudio7Generator*)
  146. this->Makefile->GetLocalGenerator();
  147. this->Name = this->Target->GetName();
  148. this->GlobalGenerator->CreateGUID(this->Name.c_str());
  149. this->GUID = this->GlobalGenerator->GetGUID(this->Name.c_str());
  150. this->Platform = gg->GetPlatformName();
  151. this->MSTools = true;
  152. this->BuildFileStream = 0;
  153. }
  154. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
  155. {
  156. for(OptionsMap::iterator i = this->ClOptions.begin();
  157. i != this->ClOptions.end(); ++i)
  158. {
  159. delete i->second;
  160. }
  161. for(OptionsMap::iterator i = this->LinkOptions.begin();
  162. i != this->LinkOptions.end(); ++i)
  163. {
  164. delete i->second;
  165. }
  166. if(!this->BuildFileStream)
  167. {
  168. return;
  169. }
  170. if (this->BuildFileStream->Close())
  171. {
  172. this->GlobalGenerator
  173. ->FileReplacedDuringGenerate(this->PathToVcxproj);
  174. }
  175. delete this->BuildFileStream;
  176. }
  177. void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
  178. const char* tag,
  179. const std::string& config,
  180. int indentLevel,
  181. const char* attribute,
  182. const char* end,
  183. std::ostream* stream)
  184. {
  185. if(!stream)
  186. {
  187. stream = this->BuildFileStream;
  188. }
  189. stream->fill(' ');
  190. stream->width(indentLevel*2 );
  191. (*stream ) << "";
  192. (*stream ) << "<" << tag
  193. << " Condition=\"'$(Configuration)|$(Platform)'=='";
  194. (*stream ) << config << "|" << this->Platform << "'\"";
  195. if(attribute)
  196. {
  197. (*stream ) << attribute;
  198. }
  199. // close the tag
  200. (*stream ) << ">";
  201. if(end)
  202. {
  203. (*stream ) << end;
  204. }
  205. }
  206. void cmVisualStudio10TargetGenerator::WriteString(const char* line,
  207. int indentLevel)
  208. {
  209. this->BuildFileStream->fill(' ');
  210. this->BuildFileStream->width(indentLevel*2 );
  211. // write an empty string to get the fill level indent to print
  212. (*this->BuildFileStream ) << "";
  213. (*this->BuildFileStream ) << line;
  214. }
  215. #define VS10_USER_PROPS "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props"
  216. void cmVisualStudio10TargetGenerator::Generate()
  217. {
  218. // do not generate external ms projects
  219. if(this->Target->GetType() == cmTarget::INTERFACE_LIBRARY
  220. || this->Target->GetProperty("EXTERNAL_MSPROJECT"))
  221. {
  222. return;
  223. }
  224. // Tell the global generator the name of the project file
  225. this->Target->SetProperty("GENERATOR_FILE_NAME",this->Name.c_str());
  226. this->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  227. ".vcxproj");
  228. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  229. {
  230. if(!this->ComputeClOptions())
  231. {
  232. return;
  233. }
  234. if(!this->ComputeRcOptions())
  235. {
  236. return;
  237. }
  238. if(!this->ComputeLinkOptions())
  239. {
  240. return;
  241. }
  242. }
  243. cmMakefile* mf = this->Target->GetMakefile();
  244. std::string path = mf->GetStartOutputDirectory();
  245. path += "/";
  246. path += this->Name;
  247. path += ".vcxproj";
  248. this->BuildFileStream =
  249. new cmGeneratedFileStream(path.c_str());
  250. this->PathToVcxproj = path;
  251. this->BuildFileStream->SetCopyIfDifferent(true);
  252. // Write the encoding header into the file
  253. char magic[] = {0xEF,0xBB, 0xBF};
  254. this->BuildFileStream->write(magic, 3);
  255. //get the tools version to use
  256. const std::string toolsVer(this->GlobalGenerator->GetToolsVersion());
  257. std::string project_defaults=
  258. "<?xml version=\"1.0\" encoding=\"" +
  259. this->GlobalGenerator->Encoding() + "\"?>\n";
  260. project_defaults.append("<Project DefaultTargets=\"Build\" ToolsVersion=\"");
  261. project_defaults.append(toolsVer +"\" ");
  262. project_defaults.append(
  263. "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  264. this->WriteString(project_defaults.c_str(),0);
  265. this->WriteProjectConfigurations();
  266. this->WriteString("<PropertyGroup Label=\"Globals\">\n", 1);
  267. this->WriteString("<ProjectGUID>", 2);
  268. (*this->BuildFileStream) << "{" << this->GUID << "}</ProjectGUID>\n";
  269. if(this->MSTools && this->Target->GetType() <= cmTarget::UTILITY)
  270. {
  271. this->WriteApplicationTypeSettings();
  272. }
  273. const char* vsProjectTypes =
  274. this->Target->GetProperty("VS_GLOBAL_PROJECT_TYPES");
  275. if(vsProjectTypes)
  276. {
  277. this->WriteString("<ProjectTypes>", 2);
  278. (*this->BuildFileStream) << cmVS10EscapeXML(vsProjectTypes) <<
  279. "</ProjectTypes>\n";
  280. }
  281. const char* vsProjectName = this->Target->GetProperty("VS_SCC_PROJECTNAME");
  282. const char* vsLocalPath = this->Target->GetProperty("VS_SCC_LOCALPATH");
  283. const char* vsProvider = this->Target->GetProperty("VS_SCC_PROVIDER");
  284. if( vsProjectName && vsLocalPath && vsProvider )
  285. {
  286. this->WriteString("<SccProjectName>", 2);
  287. (*this->BuildFileStream) << cmVS10EscapeXML(vsProjectName) <<
  288. "</SccProjectName>\n";
  289. this->WriteString("<SccLocalPath>", 2);
  290. (*this->BuildFileStream) << cmVS10EscapeXML(vsLocalPath) <<
  291. "</SccLocalPath>\n";
  292. this->WriteString("<SccProvider>", 2);
  293. (*this->BuildFileStream) << cmVS10EscapeXML(vsProvider) <<
  294. "</SccProvider>\n";
  295. const char* vsAuxPath = this->Target->GetProperty("VS_SCC_AUXPATH");
  296. if( vsAuxPath )
  297. {
  298. this->WriteString("<SccAuxPath>", 2);
  299. (*this->BuildFileStream) << cmVS10EscapeXML(vsAuxPath) <<
  300. "</SccAuxPath>\n";
  301. }
  302. }
  303. const char* vsGlobalKeyword =
  304. this->Target->GetProperty("VS_GLOBAL_KEYWORD");
  305. if(!vsGlobalKeyword)
  306. {
  307. this->WriteString("<Keyword>Win32Proj</Keyword>\n", 2);
  308. }
  309. else
  310. {
  311. this->WriteString("<Keyword>", 2);
  312. (*this->BuildFileStream) << cmVS10EscapeXML(vsGlobalKeyword) <<
  313. "</Keyword>\n";
  314. }
  315. const char* vsGlobalRootNamespace =
  316. this->Target->GetProperty("VS_GLOBAL_ROOTNAMESPACE");
  317. if(vsGlobalRootNamespace)
  318. {
  319. this->WriteString("<RootNamespace>", 2);
  320. (*this->BuildFileStream) << cmVS10EscapeXML(vsGlobalRootNamespace) <<
  321. "</RootNamespace>\n";
  322. }
  323. this->WriteString("<Platform>", 2);
  324. (*this->BuildFileStream) << cmVS10EscapeXML(this->Platform)
  325. << "</Platform>\n";
  326. const char* projLabel = this->Target->GetProperty("PROJECT_LABEL");
  327. if(!projLabel)
  328. {
  329. projLabel = this->Name.c_str();
  330. }
  331. this->WriteString("<ProjectName>", 2);
  332. (*this->BuildFileStream) << cmVS10EscapeXML(projLabel) << "</ProjectName>\n";
  333. if(const char* targetFrameworkVersion = this->Target->GetProperty(
  334. "VS_DOTNET_TARGET_FRAMEWORK_VERSION"))
  335. {
  336. this->WriteString("<TargetFrameworkVersion>", 2);
  337. (*this->BuildFileStream) << cmVS10EscapeXML(targetFrameworkVersion)
  338. << "</TargetFrameworkVersion>\n";
  339. }
  340. this->WriteString("</PropertyGroup>\n", 1);
  341. this->WriteString("<Import Project="
  342. "\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n",
  343. 1);
  344. this->WriteProjectConfigurationValues();
  345. this->WriteString(
  346. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n", 1);
  347. this->WriteString("<ImportGroup Label=\"ExtensionSettings\">\n", 1);
  348. if (this->GlobalGenerator->IsMasmEnabled())
  349. {
  350. this->WriteString("<Import Project=\"$(VCTargetsPath)\\"
  351. "BuildCustomizations\\masm.props\" />\n", 2);
  352. }
  353. this->WriteString("</ImportGroup>\n", 1);
  354. this->WriteString("<ImportGroup Label=\"PropertySheets\">\n", 1);
  355. this->WriteString("<Import Project=\"" VS10_USER_PROPS "\""
  356. " Condition=\"exists('" VS10_USER_PROPS "')\""
  357. " Label=\"LocalAppDataPlatform\" />\n", 2);
  358. this->WriteString("</ImportGroup>\n", 1);
  359. this->WriteString("<PropertyGroup Label=\"UserMacros\" />\n", 1);
  360. this->WritePathAndIncrementalLinkOptions();
  361. this->WriteItemDefinitionGroups();
  362. this->WriteCustomCommands();
  363. this->WriteAllSources();
  364. this->WriteDotNetReferences();
  365. this->WriteEmbeddedResourceGroup();
  366. this->WriteWinRTReferences();
  367. this->WriteProjectReferences();
  368. this->WriteString(
  369. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\""
  370. " />\n", 1);
  371. this->WriteString("<ImportGroup Label=\"ExtensionTargets\">\n", 1);
  372. if (this->GlobalGenerator->IsMasmEnabled())
  373. {
  374. this->WriteString("<Import Project=\"$(VCTargetsPath)\\"
  375. "BuildCustomizations\\masm.targets\" />\n", 2);
  376. }
  377. this->WriteString("</ImportGroup>\n", 1);
  378. this->WriteString("</Project>", 0);
  379. // The groups are stored in a separate file for VS 10
  380. this->WriteGroups();
  381. }
  382. void cmVisualStudio10TargetGenerator::WriteDotNetReferences()
  383. {
  384. std::vector<std::string> references;
  385. if(const char* vsDotNetReferences =
  386. this->Target->GetProperty("VS_DOTNET_REFERENCES"))
  387. {
  388. cmSystemTools::ExpandListArgument(vsDotNetReferences, references);
  389. }
  390. if(!references.empty())
  391. {
  392. this->WriteString("<ItemGroup>\n", 1);
  393. for(std::vector<std::string>::iterator ri = references.begin();
  394. ri != references.end(); ++ri)
  395. {
  396. this->WriteString("<Reference Include=\"", 2);
  397. (*this->BuildFileStream) << cmVS10EscapeXML(*ri) << "\">\n";
  398. this->WriteString("<CopyLocalSatelliteAssemblies>true"
  399. "</CopyLocalSatelliteAssemblies>\n", 3);
  400. this->WriteString("<ReferenceOutputAssembly>true"
  401. "</ReferenceOutputAssembly>\n", 3);
  402. this->WriteString("</Reference>\n", 2);
  403. }
  404. this->WriteString("</ItemGroup>\n", 1);
  405. }
  406. }
  407. void cmVisualStudio10TargetGenerator::WriteEmbeddedResourceGroup()
  408. {
  409. std::vector<cmSourceFile const*> resxObjs;
  410. this->GeneratorTarget->GetResxSources(resxObjs, "");
  411. if(!resxObjs.empty())
  412. {
  413. this->WriteString("<ItemGroup>\n", 1);
  414. for(std::vector<cmSourceFile const*>::const_iterator oi = resxObjs.begin();
  415. oi != resxObjs.end(); ++oi)
  416. {
  417. std::string obj = (*oi)->GetFullPath();
  418. this->WriteString("<EmbeddedResource Include=\"", 2);
  419. this->ConvertToWindowsSlash(obj);
  420. (*this->BuildFileStream ) << obj << "\">\n";
  421. this->WriteString("<DependentUpon>", 3);
  422. std::string hFileName = obj.substr(0, obj.find_last_of(".")) + ".h";
  423. (*this->BuildFileStream ) << hFileName;
  424. this->WriteString("</DependentUpon>\n", 3);
  425. std::vector<std::string> const * configs =
  426. this->GlobalGenerator->GetConfigurations();
  427. for(std::vector<std::string>::const_iterator i = configs->begin();
  428. i != configs->end(); ++i)
  429. {
  430. this->WritePlatformConfigTag("LogicalName", i->c_str(), 3);
  431. if(this->Target->GetProperty("VS_GLOBAL_ROOTNAMESPACE"))
  432. {
  433. (*this->BuildFileStream ) << "$(RootNamespace).";
  434. }
  435. (*this->BuildFileStream ) << "%(Filename)";
  436. (*this->BuildFileStream ) << ".resources";
  437. (*this->BuildFileStream ) << "</LogicalName>\n";
  438. }
  439. this->WriteString("</EmbeddedResource>\n", 2);
  440. }
  441. this->WriteString("</ItemGroup>\n", 1);
  442. }
  443. }
  444. void cmVisualStudio10TargetGenerator::WriteWinRTReferences()
  445. {
  446. std::vector<std::string> references;
  447. if(const char* vsWinRTReferences =
  448. this->Target->GetProperty("VS_WINRT_REFERENCES"))
  449. {
  450. cmSystemTools::ExpandListArgument(vsWinRTReferences, references);
  451. }
  452. if(!references.empty())
  453. {
  454. this->WriteString("<ItemGroup>\n", 1);
  455. for(std::vector<std::string>::iterator ri = references.begin();
  456. ri != references.end(); ++ri)
  457. {
  458. this->WriteString("<Reference Include=\"", 2);
  459. (*this->BuildFileStream) << cmVS10EscapeXML(*ri) << "\">\n";
  460. this->WriteString("<IsWinMDFile>true</IsWinMDFile>\n", 3);
  461. this->WriteString("</Reference>\n", 2);
  462. }
  463. this->WriteString("</ItemGroup>\n", 1);
  464. }
  465. }
  466. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  467. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations()
  468. {
  469. this->WriteString("<ItemGroup Label=\"ProjectConfigurations\">\n", 1);
  470. std::vector<std::string> *configs =
  471. static_cast<cmGlobalVisualStudio7Generator *>
  472. (this->GlobalGenerator)->GetConfigurations();
  473. for(std::vector<std::string>::iterator i = configs->begin();
  474. i != configs->end(); ++i)
  475. {
  476. this->WriteString("<ProjectConfiguration Include=\"", 2);
  477. (*this->BuildFileStream ) << *i << "|" << this->Platform << "\">\n";
  478. this->WriteString("<Configuration>", 3);
  479. (*this->BuildFileStream ) << *i << "</Configuration>\n";
  480. this->WriteString("<Platform>", 3);
  481. (*this->BuildFileStream) << cmVS10EscapeXML(this->Platform)
  482. << "</Platform>\n";
  483. this->WriteString("</ProjectConfiguration>\n", 2);
  484. }
  485. this->WriteString("</ItemGroup>\n", 1);
  486. }
  487. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues()
  488. {
  489. std::vector<std::string> *configs =
  490. static_cast<cmGlobalVisualStudio7Generator *>
  491. (this->GlobalGenerator)->GetConfigurations();
  492. for(std::vector<std::string>::iterator i = configs->begin();
  493. i != configs->end(); ++i)
  494. {
  495. this->WritePlatformConfigTag("PropertyGroup",
  496. i->c_str(),
  497. 1, " Label=\"Configuration\"", "\n");
  498. std::string configType = "<ConfigurationType>";
  499. switch(this->Target->GetType())
  500. {
  501. case cmTarget::SHARED_LIBRARY:
  502. case cmTarget::MODULE_LIBRARY:
  503. configType += "DynamicLibrary";
  504. break;
  505. case cmTarget::OBJECT_LIBRARY:
  506. case cmTarget::STATIC_LIBRARY:
  507. configType += "StaticLibrary";
  508. break;
  509. case cmTarget::EXECUTABLE:
  510. configType += "Application";
  511. break;
  512. case cmTarget::UTILITY:
  513. configType += "Utility";
  514. break;
  515. case cmTarget::GLOBAL_TARGET:
  516. case cmTarget::UNKNOWN_LIBRARY:
  517. case cmTarget::INTERFACE_LIBRARY:
  518. break;
  519. }
  520. configType += "</ConfigurationType>\n";
  521. this->WriteString(configType.c_str(), 2);
  522. if(this->MSTools)
  523. {
  524. this->WriteMSToolConfigurationValues(*i);
  525. }
  526. this->WriteString("</PropertyGroup>\n", 1);
  527. }
  528. }
  529. //----------------------------------------------------------------------------
  530. void cmVisualStudio10TargetGenerator
  531. ::WriteMSToolConfigurationValues(std::string const& config)
  532. {
  533. cmGlobalVisualStudio10Generator* gg =
  534. static_cast<cmGlobalVisualStudio10Generator*>(this->GlobalGenerator);
  535. const char* mfcFlag =
  536. this->Target->GetMakefile()->GetDefinition("CMAKE_MFC_FLAG");
  537. std::string mfcFlagValue = mfcFlag ? mfcFlag : "0";
  538. std::string useOfMfcValue = "false";
  539. if(mfcFlagValue == "1")
  540. {
  541. useOfMfcValue = "Static";
  542. }
  543. else if(mfcFlagValue == "2")
  544. {
  545. useOfMfcValue = "Dynamic";
  546. }
  547. std::string mfcLine = "<UseOfMfc>";
  548. mfcLine += useOfMfcValue + "</UseOfMfc>\n";
  549. this->WriteString(mfcLine.c_str(), 2);
  550. if((this->Target->GetType() <= cmTarget::OBJECT_LIBRARY &&
  551. this->ClOptions[config]->UsingUnicode()) ||
  552. this->Target->GetPropertyAsBool("VS_WINRT_EXTENSIONS"))
  553. {
  554. this->WriteString("<CharacterSet>Unicode</CharacterSet>\n", 2);
  555. }
  556. else if (this->Target->GetType() <= cmTarget::MODULE_LIBRARY &&
  557. this->ClOptions[config]->UsingSBCS())
  558. {
  559. this->WriteString("<CharacterSet>NotSet</CharacterSet>\n", 2);
  560. }
  561. else
  562. {
  563. this->WriteString("<CharacterSet>MultiByte</CharacterSet>\n", 2);
  564. }
  565. if(const char* toolset = gg->GetPlatformToolset())
  566. {
  567. std::string pts = "<PlatformToolset>";
  568. pts += toolset;
  569. pts += "</PlatformToolset>\n";
  570. this->WriteString(pts.c_str(), 2);
  571. }
  572. if(this->Target->GetPropertyAsBool("VS_WINRT_EXTENSIONS"))
  573. {
  574. this->WriteString("<WindowsAppContainer>true"
  575. "</WindowsAppContainer>\n", 2);
  576. }
  577. }
  578. void cmVisualStudio10TargetGenerator::WriteCustomCommands()
  579. {
  580. this->SourcesVisited.clear();
  581. std::vector<cmSourceFile const*> customCommands;
  582. this->GeneratorTarget->GetCustomCommands(customCommands, "");
  583. for(std::vector<cmSourceFile const*>::const_iterator
  584. si = customCommands.begin();
  585. si != customCommands.end(); ++si)
  586. {
  587. this->WriteCustomCommand(*si);
  588. }
  589. }
  590. //----------------------------------------------------------------------------
  591. void cmVisualStudio10TargetGenerator
  592. ::WriteCustomCommand(cmSourceFile const* sf)
  593. {
  594. if(this->SourcesVisited.insert(sf).second)
  595. {
  596. if(std::vector<cmSourceFile*> const* depends =
  597. this->GeneratorTarget->GetSourceDepends(sf))
  598. {
  599. for(std::vector<cmSourceFile*>::const_iterator di = depends->begin();
  600. di != depends->end(); ++di)
  601. {
  602. this->WriteCustomCommand(*di);
  603. }
  604. }
  605. if(cmCustomCommand const* command = sf->GetCustomCommand())
  606. {
  607. this->WriteString("<ItemGroup>\n", 1);
  608. this->WriteCustomRule(sf, *command);
  609. this->WriteString("</ItemGroup>\n", 1);
  610. }
  611. }
  612. }
  613. void
  614. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile const* source,
  615. cmCustomCommand const &
  616. command)
  617. {
  618. std::string sourcePath = source->GetFullPath();
  619. // VS 10 will always rebuild a custom command attached to a .rule
  620. // file that doesn't exist so create the file explicitly.
  621. if (source->GetPropertyAsBool("__CMAKE_RULE"))
  622. {
  623. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  624. {
  625. // Make sure the path exists for the file
  626. std::string path = cmSystemTools::GetFilenamePath(sourcePath);
  627. cmSystemTools::MakeDirectory(path.c_str());
  628. cmsys::ofstream fout(sourcePath.c_str());
  629. if(fout)
  630. {
  631. fout << "# generated from CMake\n";
  632. fout.flush();
  633. fout.close();
  634. }
  635. else
  636. {
  637. std::string error = "Could not create file: [";
  638. error += sourcePath;
  639. error += "] ";
  640. cmSystemTools::Error
  641. (error.c_str(), cmSystemTools::GetLastSystemError().c_str());
  642. }
  643. }
  644. }
  645. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  646. std::vector<std::string> *configs =
  647. static_cast<cmGlobalVisualStudio7Generator *>
  648. (this->GlobalGenerator)->GetConfigurations();
  649. this->WriteSource("CustomBuild", source, ">\n");
  650. for(std::vector<std::string>::iterator i = configs->begin();
  651. i != configs->end(); ++i)
  652. {
  653. cmCustomCommandGenerator ccg(command, *i, this->Makefile);
  654. std::string comment = lg->ConstructComment(ccg);
  655. comment = cmVS10EscapeComment(comment);
  656. std::string script =
  657. cmVS10EscapeXML(lg->ConstructScript(ccg));
  658. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  659. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  660. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  661. (*this->BuildFileStream ) << script << "</Command>\n";
  662. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  663. (*this->BuildFileStream ) << cmVS10EscapeXML(source->GetFullPath());
  664. for(std::vector<std::string>::const_iterator d =
  665. ccg.GetDepends().begin();
  666. d != ccg.GetDepends().end();
  667. ++d)
  668. {
  669. std::string dep;
  670. if(this->LocalGenerator->GetRealDependency(d->c_str(), i->c_str(), dep))
  671. {
  672. this->ConvertToWindowsSlash(dep);
  673. (*this->BuildFileStream ) << ";" << cmVS10EscapeXML(dep);
  674. }
  675. }
  676. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  677. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  678. const char* sep = "";
  679. for(std::vector<std::string>::const_iterator o =
  680. ccg.GetOutputs().begin();
  681. o != ccg.GetOutputs().end();
  682. ++o)
  683. {
  684. std::string out = *o;
  685. this->ConvertToWindowsSlash(out);
  686. (*this->BuildFileStream ) << sep << cmVS10EscapeXML(out);
  687. sep = ";";
  688. }
  689. (*this->BuildFileStream ) << "</Outputs>\n";
  690. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  691. {
  692. // VS >= 11 let us turn off linking of custom command outputs.
  693. this->WritePlatformConfigTag("LinkObjects", i->c_str(), 3);
  694. (*this->BuildFileStream ) << "false</LinkObjects>\n";
  695. }
  696. }
  697. this->WriteString("</CustomBuild>\n", 2);
  698. }
  699. std::string
  700. cmVisualStudio10TargetGenerator::ConvertPath(std::string const& path,
  701. bool forceRelative)
  702. {
  703. return forceRelative
  704. ? cmSystemTools::RelativePath(
  705. this->Makefile->GetCurrentOutputDirectory(), path.c_str())
  706. : this->LocalGenerator->Convert(path.c_str(),
  707. cmLocalGenerator::START_OUTPUT,
  708. cmLocalGenerator::UNCHANGED,
  709. /* optional = */ true);
  710. }
  711. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  712. {
  713. // first convert all of the slashes
  714. std::string::size_type pos = 0;
  715. while((pos = s.find('/', pos)) != std::string::npos)
  716. {
  717. s[pos] = '\\';
  718. pos++;
  719. }
  720. }
  721. void cmVisualStudio10TargetGenerator::WriteGroups()
  722. {
  723. // collect up group information
  724. std::vector<cmSourceGroup> sourceGroups =
  725. this->Makefile->GetSourceGroups();
  726. std::vector<cmSourceFile*> classes;
  727. if (!this->Target->GetConfigCommonSourceFiles(classes))
  728. {
  729. return;
  730. }
  731. std::set<cmSourceGroup*> groupsUsed;
  732. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  733. s != classes.end(); s++)
  734. {
  735. cmSourceFile* sf = *s;
  736. std::string const& source = sf->GetFullPath();
  737. cmSourceGroup* sourceGroup =
  738. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  739. groupsUsed.insert(sourceGroup);
  740. }
  741. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  742. // Write out group file
  743. std::string path = this->Makefile->GetStartOutputDirectory();
  744. path += "/";
  745. path += this->Name;
  746. path += ".vcxproj.filters";
  747. cmGeneratedFileStream fout(path.c_str());
  748. fout.SetCopyIfDifferent(true);
  749. char magic[] = {0xEF,0xBB, 0xBF};
  750. fout.write(magic, 3);
  751. cmGeneratedFileStream* save = this->BuildFileStream;
  752. this->BuildFileStream = & fout;
  753. //get the tools version to use
  754. const std::string toolsVer(this->GlobalGenerator->GetToolsVersion());
  755. std::string project_defaults=
  756. "<?xml version=\"1.0\" encoding=\"" +
  757. this->GlobalGenerator->Encoding() + "\"?>\n";
  758. project_defaults.append("<Project ToolsVersion=\"");
  759. project_defaults.append(toolsVer +"\" ");
  760. project_defaults.append(
  761. "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  762. this->WriteString(project_defaults.c_str(),0);
  763. for(ToolSourceMap::const_iterator ti = this->Tools.begin();
  764. ti != this->Tools.end(); ++ti)
  765. {
  766. this->WriteGroupSources(ti->first.c_str(), ti->second, sourceGroups);
  767. }
  768. std::vector<cmSourceFile const*> resxObjs;
  769. this->GeneratorTarget->GetResxSources(resxObjs, "");
  770. if(!resxObjs.empty())
  771. {
  772. this->WriteString("<ItemGroup>\n", 1);
  773. for(std::vector<cmSourceFile const*>::const_iterator oi = resxObjs.begin();
  774. oi != resxObjs.end(); ++oi)
  775. {
  776. std::string obj = (*oi)->GetFullPath();
  777. this->WriteString("<EmbeddedResource Include=\"", 2);
  778. this->ConvertToWindowsSlash(obj);
  779. (*this->BuildFileStream ) << cmVS10EscapeXML(obj) << "\">\n";
  780. this->WriteString("<Filter>Resource Files</Filter>\n", 3);
  781. this->WriteString("</EmbeddedResource>\n", 2);
  782. }
  783. this->WriteString("</ItemGroup>\n", 1);
  784. }
  785. // Add object library contents as external objects.
  786. std::vector<std::string> objs;
  787. this->GeneratorTarget->UseObjectLibraries(objs, "");
  788. if(!objs.empty())
  789. {
  790. this->WriteString("<ItemGroup>\n", 1);
  791. for(std::vector<std::string>::const_iterator
  792. oi = objs.begin(); oi != objs.end(); ++oi)
  793. {
  794. std::string obj = *oi;
  795. this->WriteString("<Object Include=\"", 2);
  796. this->ConvertToWindowsSlash(obj);
  797. (*this->BuildFileStream ) << cmVS10EscapeXML(obj) << "\">\n";
  798. this->WriteString("<Filter>Object Libraries</Filter>\n", 3);
  799. this->WriteString("</Object>\n", 2);
  800. }
  801. this->WriteString("</ItemGroup>\n", 1);
  802. }
  803. this->WriteString("<ItemGroup>\n", 1);
  804. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  805. g != groupsUsed.end(); ++g)
  806. {
  807. cmSourceGroup* sg = *g;
  808. const char* name = sg->GetFullName();
  809. if(strlen(name) != 0)
  810. {
  811. this->WriteString("<Filter Include=\"", 2);
  812. (*this->BuildFileStream) << name << "\">\n";
  813. std::string guidName = "SG_Filter_";
  814. guidName += name;
  815. this->GlobalGenerator->CreateGUID(guidName.c_str());
  816. this->WriteString("<UniqueIdentifier>", 3);
  817. std::string guid
  818. = this->GlobalGenerator->GetGUID(guidName.c_str());
  819. (*this->BuildFileStream)
  820. << "{"
  821. << guid << "}"
  822. << "</UniqueIdentifier>\n";
  823. this->WriteString("</Filter>\n", 2);
  824. }
  825. }
  826. if(!objs.empty())
  827. {
  828. this->WriteString("<Filter Include=\"Object Libraries\">\n", 2);
  829. std::string guidName = "SG_Filter_Object Libraries";
  830. this->GlobalGenerator->CreateGUID(guidName.c_str());
  831. this->WriteString("<UniqueIdentifier>", 3);
  832. std::string guid =
  833. this->GlobalGenerator->GetGUID(guidName.c_str());
  834. (*this->BuildFileStream) << "{" << guid << "}"
  835. << "</UniqueIdentifier>\n";
  836. this->WriteString("</Filter>\n", 2);
  837. }
  838. if(!resxObjs.empty())
  839. {
  840. this->WriteString("<Filter Include=\"Resource Files\">\n", 2);
  841. std::string guidName = "SG_Filter_Resource Files";
  842. this->GlobalGenerator->CreateGUID(guidName.c_str());
  843. this->WriteString("<UniqueIdentifier>", 3);
  844. std::string guid =
  845. this->GlobalGenerator->GetGUID(guidName.c_str());
  846. (*this->BuildFileStream) << "{" << guid << "}"
  847. << "</UniqueIdentifier>\n";
  848. this->WriteString("<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;", 3);
  849. (*this->BuildFileStream) << "gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;";
  850. (*this->BuildFileStream) << "mfcribbon-ms</Extensions>\n";
  851. this->WriteString("</Filter>\n", 2);
  852. }
  853. this->WriteString("</ItemGroup>\n", 1);
  854. this->WriteString("</Project>\n", 0);
  855. // restore stream pointer
  856. this->BuildFileStream = save;
  857. if (fout.Close())
  858. {
  859. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  860. }
  861. }
  862. // Add to groupsUsed empty source groups that have non-empty children.
  863. void
  864. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  865. std::set<cmSourceGroup*>& groupsUsed,
  866. const std::vector<cmSourceGroup>& allGroups
  867. )
  868. {
  869. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  870. current != allGroups.end(); ++current)
  871. {
  872. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  873. if(children.empty())
  874. {
  875. continue; // the group is really empty
  876. }
  877. this->AddMissingSourceGroups(groupsUsed, children);
  878. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  879. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  880. {
  881. continue; // group has already been added to set
  882. }
  883. // check if it least one of the group's descendants is not empty
  884. // (at least one child must already have been added)
  885. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  886. while(child_it != children.end())
  887. {
  888. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  889. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  890. {
  891. break; // found a child that was already added => add current group too
  892. }
  893. child_it++;
  894. }
  895. if(child_it == children.end())
  896. {
  897. continue; // no descendants have source files => ignore this group
  898. }
  899. groupsUsed.insert(current_ptr);
  900. }
  901. }
  902. void
  903. cmVisualStudio10TargetGenerator::
  904. WriteGroupSources(const char* name,
  905. ToolSources const& sources,
  906. std::vector<cmSourceGroup>& sourceGroups)
  907. {
  908. this->WriteString("<ItemGroup>\n", 1);
  909. for(ToolSources::const_iterator s = sources.begin();
  910. s != sources.end(); ++s)
  911. {
  912. cmSourceFile const* sf = s->SourceFile;
  913. std::string const& source = sf->GetFullPath();
  914. cmSourceGroup* sourceGroup =
  915. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  916. const char* filter = sourceGroup->GetFullName();
  917. this->WriteString("<", 2);
  918. std::string path = this->ConvertPath(source, s->RelativePath);
  919. this->ConvertToWindowsSlash(path);
  920. (*this->BuildFileStream) << name << " Include=\""
  921. << cmVS10EscapeXML(path);
  922. if(strlen(filter))
  923. {
  924. (*this->BuildFileStream) << "\">\n";
  925. this->WriteString("<Filter>", 3);
  926. (*this->BuildFileStream) << filter << "</Filter>\n";
  927. this->WriteString("</", 2);
  928. (*this->BuildFileStream) << name << ">\n";
  929. }
  930. else
  931. {
  932. (*this->BuildFileStream) << "\" />\n";
  933. }
  934. }
  935. this->WriteString("</ItemGroup>\n", 1);
  936. }
  937. void cmVisualStudio10TargetGenerator::WriteHeaderSource(cmSourceFile const* sf)
  938. {
  939. if(this->IsResxHeader(sf->GetFullPath()))
  940. {
  941. this->WriteSource("ClInclude", sf, ">\n");
  942. this->WriteString("<FileType>CppForm</FileType>\n", 3);
  943. this->WriteString("</ClInclude>\n", 2);
  944. }
  945. else
  946. {
  947. this->WriteSource("ClInclude", sf);
  948. }
  949. }
  950. void cmVisualStudio10TargetGenerator::WriteExtraSource(cmSourceFile const* sf)
  951. {
  952. std::string tool = "None";
  953. std::string const& ext = sf->GetExtension();
  954. if(ext == "appxmanifest")
  955. {
  956. tool = "AppxManifest";
  957. }
  958. else if(ext == "jpg" ||
  959. ext == "png")
  960. {
  961. tool = "Image";
  962. }
  963. else if(ext == "xml")
  964. {
  965. tool = "XML";
  966. }
  967. this->WriteSource(tool, sf);
  968. }
  969. void cmVisualStudio10TargetGenerator::WriteSource(
  970. std::string const& tool, cmSourceFile const* sf, const char* end)
  971. {
  972. // Visual Studio tools append relative paths to the current dir, as in:
  973. //
  974. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  975. //
  976. // and fail if this exceeds the maximum allowed path length. Our path
  977. // conversion uses full paths when possible to allow deeper trees.
  978. bool forceRelative = false;
  979. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), false);
  980. if(this->LocalGenerator->GetVersion() == cmLocalVisualStudioGenerator::VS10
  981. && cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  982. {
  983. // Normal path conversion resulted in a full path. VS 10 (but not 11)
  984. // refuses to show the property page in the IDE for a source file with a
  985. // full path (not starting in a '.' or '/' AFAICT). CMake <= 2.8.4 used a
  986. // relative path but to allow deeper build trees CMake 2.8.[5678] used a
  987. // full path except for custom commands. Custom commands do not work
  988. // without a relative path, but they do not seem to be involved in tools
  989. // with the above behavior. For other sources we now use a relative path
  990. // when the combined path will not be too long so property pages appear.
  991. std::string sourceRel = this->ConvertPath(sf->GetFullPath(), true);
  992. size_t const maxLen = 250;
  993. if(sf->GetCustomCommand() ||
  994. ((strlen(this->Makefile->GetCurrentOutputDirectory()) + 1 +
  995. sourceRel.length()) <= maxLen))
  996. {
  997. forceRelative = true;
  998. sourceFile = sourceRel;
  999. }
  1000. else
  1001. {
  1002. this->GlobalGenerator->PathTooLong(this->Target, sf, sourceRel);
  1003. }
  1004. }
  1005. this->ConvertToWindowsSlash(sourceFile);
  1006. this->WriteString("<", 2);
  1007. (*this->BuildFileStream ) << tool << " Include=\""
  1008. << cmVS10EscapeXML(sourceFile) << "\""
  1009. << (end? end : " />\n");
  1010. ToolSource toolSource = {sf, forceRelative};
  1011. this->Tools[tool].push_back(toolSource);
  1012. }
  1013. void cmVisualStudio10TargetGenerator::WriteSources(
  1014. std::string const& tool, std::vector<cmSourceFile const*> const& sources)
  1015. {
  1016. for(std::vector<cmSourceFile const*>::const_iterator
  1017. si = sources.begin(); si != sources.end(); ++si)
  1018. {
  1019. this->WriteSource(tool, *si);
  1020. }
  1021. }
  1022. void cmVisualStudio10TargetGenerator::WriteAllSources()
  1023. {
  1024. if(this->Target->GetType() > cmTarget::UTILITY)
  1025. {
  1026. return;
  1027. }
  1028. this->WriteString("<ItemGroup>\n", 1);
  1029. std::vector<cmSourceFile const*> headerSources;
  1030. this->GeneratorTarget->GetHeaderSources(headerSources, "");
  1031. for(std::vector<cmSourceFile const*>::const_iterator
  1032. si = headerSources.begin(); si != headerSources.end(); ++si)
  1033. {
  1034. this->WriteHeaderSource(*si);
  1035. }
  1036. std::vector<cmSourceFile const*> idlSources;
  1037. this->GeneratorTarget->GetIDLSources(idlSources, "");
  1038. this->WriteSources("Midl", idlSources);
  1039. std::vector<cmSourceFile const*> objectSources;
  1040. this->GeneratorTarget->GetObjectSources(objectSources, "");
  1041. for(std::vector<cmSourceFile const*>::const_iterator
  1042. si = objectSources.begin();
  1043. si != objectSources.end(); ++si)
  1044. {
  1045. const std::string& lang = (*si)->GetLanguage();
  1046. std::string tool;
  1047. if (lang == "C"|| lang == "CXX")
  1048. {
  1049. tool = "ClCompile";
  1050. }
  1051. else if (lang == "ASM_NASM" &&
  1052. this->GlobalGenerator->IsMasmEnabled())
  1053. {
  1054. tool = "MASM";
  1055. }
  1056. else if (lang == "RC")
  1057. {
  1058. tool = "ResourceCompile";
  1059. }
  1060. if (!tool.empty())
  1061. {
  1062. this->WriteSource(tool, *si, " ");
  1063. if (this->OutputSourceSpecificFlags(*si))
  1064. {
  1065. this->WriteString("</", 2);
  1066. (*this->BuildFileStream ) << tool << ">\n";
  1067. }
  1068. else
  1069. {
  1070. (*this->BuildFileStream ) << " />\n";
  1071. }
  1072. }
  1073. else
  1074. {
  1075. this->WriteSource("None", *si);
  1076. }
  1077. }
  1078. std::vector<cmSourceFile const*> externalObjects;
  1079. this->GeneratorTarget->GetExternalObjects(externalObjects, "");
  1080. for(std::vector<cmSourceFile const*>::iterator
  1081. si = externalObjects.begin();
  1082. si != externalObjects.end(); )
  1083. {
  1084. if (!(*si)->GetObjectLibrary().empty())
  1085. {
  1086. si = externalObjects.erase(si);
  1087. }
  1088. else
  1089. {
  1090. ++si;
  1091. }
  1092. }
  1093. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  1094. {
  1095. // For VS >= 11 we use LinkObjects to avoid linking custom command
  1096. // outputs. Use Object for all external objects, generated or not.
  1097. this->WriteSources("Object", externalObjects);
  1098. }
  1099. else
  1100. {
  1101. // If an object file is generated in this target, then vs10 will use
  1102. // it in the build, and we have to list it as None instead of Object.
  1103. for(std::vector<cmSourceFile const*>::const_iterator
  1104. si = externalObjects.begin();
  1105. si != externalObjects.end(); ++si)
  1106. {
  1107. std::vector<cmSourceFile*> const* d =
  1108. this->GeneratorTarget->GetSourceDepends(*si);
  1109. this->WriteSource((d && !d->empty())? "None":"Object", *si);
  1110. }
  1111. }
  1112. std::vector<cmSourceFile const*> extraSources;
  1113. this->GeneratorTarget->GetExtraSources(extraSources, "");
  1114. for(std::vector<cmSourceFile const*>::const_iterator
  1115. si = extraSources.begin(); si != extraSources.end(); ++si)
  1116. {
  1117. this->WriteExtraSource(*si);
  1118. }
  1119. // Add object library contents as external objects.
  1120. std::vector<std::string> objs;
  1121. this->GeneratorTarget->UseObjectLibraries(objs, "");
  1122. for(std::vector<std::string>::const_iterator
  1123. oi = objs.begin(); oi != objs.end(); ++oi)
  1124. {
  1125. std::string obj = *oi;
  1126. this->WriteString("<Object Include=\"", 2);
  1127. this->ConvertToWindowsSlash(obj);
  1128. (*this->BuildFileStream ) << cmVS10EscapeXML(obj) << "\" />\n";
  1129. }
  1130. this->WriteString("</ItemGroup>\n", 1);
  1131. }
  1132. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  1133. cmSourceFile const* source)
  1134. {
  1135. cmSourceFile const& sf = *source;
  1136. std::string objectName;
  1137. if(this->GeneratorTarget->HasExplicitObjectName(&sf))
  1138. {
  1139. objectName = this->GeneratorTarget->GetObjectName(&sf);
  1140. }
  1141. std::string flags;
  1142. std::string defines;
  1143. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  1144. {
  1145. flags += cflags;
  1146. }
  1147. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  1148. {
  1149. defines += cdefs;
  1150. }
  1151. std::string lang =
  1152. this->GlobalGenerator->GetLanguageFromExtension
  1153. (sf.GetExtension().c_str());
  1154. std::string sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  1155. const std::string& linkLanguage = this->Target->GetLinkerLanguage();
  1156. bool needForceLang = false;
  1157. // source file does not match its extension language
  1158. if(lang != sourceLang)
  1159. {
  1160. needForceLang = true;
  1161. lang = sourceLang;
  1162. }
  1163. // if the source file does not match the linker language
  1164. // then force c or c++
  1165. const char* compileAs = 0;
  1166. if(needForceLang || (linkLanguage != lang))
  1167. {
  1168. if(lang == "CXX")
  1169. {
  1170. // force a C++ file type
  1171. compileAs = "CompileAsCpp";
  1172. }
  1173. else if(lang == "C")
  1174. {
  1175. // force to c
  1176. compileAs = "CompileAsC";
  1177. }
  1178. }
  1179. bool hasFlags = false;
  1180. // for the first time we need a new line if there is something
  1181. // produced here.
  1182. const char* firstString = ">\n";
  1183. if(objectName.size())
  1184. {
  1185. (*this->BuildFileStream ) << firstString;
  1186. firstString = "";
  1187. hasFlags = true;
  1188. this->WriteString("<ObjectFileName>", 3);
  1189. (*this->BuildFileStream )
  1190. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  1191. }
  1192. std::vector<std::string> *configs =
  1193. static_cast<cmGlobalVisualStudio7Generator *>
  1194. (this->GlobalGenerator)->GetConfigurations();
  1195. for( std::vector<std::string>::iterator config = configs->begin();
  1196. config != configs->end(); ++config)
  1197. {
  1198. std::string configUpper = cmSystemTools::UpperCase(*config);
  1199. std::string configDefines = defines;
  1200. std::string defPropName = "COMPILE_DEFINITIONS_";
  1201. defPropName += configUpper;
  1202. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  1203. {
  1204. if(configDefines.size())
  1205. {
  1206. configDefines += ";";
  1207. }
  1208. configDefines += ccdefs;
  1209. }
  1210. // if we have flags or defines for this config then
  1211. // use them
  1212. if(!flags.empty() || !configDefines.empty() || compileAs)
  1213. {
  1214. (*this->BuildFileStream ) << firstString;
  1215. firstString = ""; // only do firstString once
  1216. hasFlags = true;
  1217. cmVisualStudioGeneratorOptions
  1218. clOptions(this->LocalGenerator,
  1219. cmVisualStudioGeneratorOptions::Compiler,
  1220. this->GetClFlagTable(), 0, this);
  1221. if(compileAs)
  1222. {
  1223. clOptions.AddFlag("CompileAs", compileAs);
  1224. }
  1225. clOptions.Parse(flags.c_str());
  1226. clOptions.AddDefines(configDefines.c_str());
  1227. clOptions.SetConfiguration((*config).c_str());
  1228. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1229. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1230. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  1231. " ", "\n", lang);
  1232. }
  1233. }
  1234. return hasFlags;
  1235. }
  1236. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  1237. {
  1238. cmTarget::TargetType ttype = this->Target->GetType();
  1239. if(ttype > cmTarget::GLOBAL_TARGET)
  1240. {
  1241. return;
  1242. }
  1243. this->WriteString("<PropertyGroup>\n", 2);
  1244. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  1245. "</_ProjectFileVersion>\n", 3);
  1246. std::vector<std::string> *configs =
  1247. static_cast<cmGlobalVisualStudio7Generator *>
  1248. (this->GlobalGenerator)->GetConfigurations();
  1249. for(std::vector<std::string>::iterator config = configs->begin();
  1250. config != configs->end(); ++config)
  1251. {
  1252. if(ttype >= cmTarget::UTILITY)
  1253. {
  1254. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1255. *this->BuildFileStream
  1256. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  1257. << "</IntDir>\n";
  1258. }
  1259. else
  1260. {
  1261. std::string intermediateDir = this->LocalGenerator->
  1262. GetTargetDirectory(*this->Target);
  1263. intermediateDir += "/";
  1264. intermediateDir += *config;
  1265. intermediateDir += "/";
  1266. std::string outDir;
  1267. std::string targetNameFull;
  1268. if(ttype == cmTarget::OBJECT_LIBRARY)
  1269. {
  1270. outDir = intermediateDir;
  1271. targetNameFull = this->Target->GetName();
  1272. targetNameFull += ".lib";
  1273. }
  1274. else
  1275. {
  1276. outDir = this->Target->GetDirectory(config->c_str()) + "/";
  1277. targetNameFull = this->Target->GetFullName(config->c_str());
  1278. }
  1279. this->ConvertToWindowsSlash(intermediateDir);
  1280. this->ConvertToWindowsSlash(outDir);
  1281. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  1282. *this->BuildFileStream << cmVS10EscapeXML(outDir)
  1283. << "</OutDir>\n";
  1284. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1285. *this->BuildFileStream << cmVS10EscapeXML(intermediateDir)
  1286. << "</IntDir>\n";
  1287. std::string name =
  1288. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  1289. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  1290. *this->BuildFileStream << cmVS10EscapeXML(name) << "</TargetName>\n";
  1291. std::string ext =
  1292. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  1293. if(ext.empty())
  1294. {
  1295. // An empty TargetExt causes a default extension to be used.
  1296. // A single "." appears to be treated as an empty extension.
  1297. ext = ".";
  1298. }
  1299. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  1300. *this->BuildFileStream << cmVS10EscapeXML(ext) << "</TargetExt>\n";
  1301. this->OutputLinkIncremental(*config);
  1302. }
  1303. }
  1304. this->WriteString("</PropertyGroup>\n", 2);
  1305. }
  1306. void
  1307. cmVisualStudio10TargetGenerator::
  1308. OutputLinkIncremental(std::string const& configName)
  1309. {
  1310. // static libraries and things greater than modules do not need
  1311. // to set this option
  1312. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1313. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1314. {
  1315. return;
  1316. }
  1317. Options& linkOptions = *(this->LinkOptions[configName]);
  1318. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  1319. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  1320. *this->BuildFileStream << (incremental?incremental:"true")
  1321. << "</LinkIncremental>\n";
  1322. linkOptions.RemoveFlag("LinkIncremental");
  1323. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  1324. this->WritePlatformConfigTag("GenerateManifest", configName.c_str(), 3);
  1325. *this->BuildFileStream << (manifest?manifest:"true")
  1326. << "</GenerateManifest>\n";
  1327. linkOptions.RemoveFlag("GenerateManifest");
  1328. // Some link options belong here. Use them now and remove them so that
  1329. // WriteLinkOptions does not use them.
  1330. const char* flags[] = {
  1331. "LinkDelaySign",
  1332. "LinkKeyFile",
  1333. 0};
  1334. for(const char** f = flags; *f; ++f)
  1335. {
  1336. const char* flag = *f;
  1337. if(const char* value = linkOptions.GetFlag(flag))
  1338. {
  1339. this->WritePlatformConfigTag(flag, configName.c_str(), 3);
  1340. *this->BuildFileStream << value << "</" << flag << ">\n";
  1341. linkOptions.RemoveFlag(flag);
  1342. }
  1343. }
  1344. }
  1345. //----------------------------------------------------------------------------
  1346. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  1347. {
  1348. std::vector<std::string> const* configs =
  1349. this->GlobalGenerator->GetConfigurations();
  1350. for(std::vector<std::string>::const_iterator i = configs->begin();
  1351. i != configs->end(); ++i)
  1352. {
  1353. if(!this->ComputeClOptions(*i))
  1354. {
  1355. return false;
  1356. }
  1357. }
  1358. return true;
  1359. }
  1360. //----------------------------------------------------------------------------
  1361. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  1362. std::string const& configName)
  1363. {
  1364. // much of this was copied from here:
  1365. // copied from cmLocalVisualStudio7Generator.cxx 805
  1366. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  1367. cmsys::auto_ptr<Options> pOptions(
  1368. new Options(this->LocalGenerator, Options::Compiler,
  1369. this->GetClFlagTable()));
  1370. Options& clOptions = *pOptions;
  1371. std::string flags;
  1372. const std::string& linkLanguage =
  1373. this->Target->GetLinkerLanguage(configName.c_str());
  1374. if(linkLanguage.empty())
  1375. {
  1376. cmSystemTools::Error
  1377. ("CMake can not determine linker language for target: ",
  1378. this->Name.c_str());
  1379. return false;
  1380. }
  1381. if(linkLanguage == "C" || linkLanguage == "CXX"
  1382. || linkLanguage == "Fortran")
  1383. {
  1384. std::string baseFlagVar = "CMAKE_";
  1385. baseFlagVar += linkLanguage;
  1386. baseFlagVar += "_FLAGS";
  1387. flags = this->
  1388. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  1389. std::string flagVar = baseFlagVar + std::string("_") +
  1390. cmSystemTools::UpperCase(configName);
  1391. flags += " ";
  1392. flags += this->
  1393. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  1394. }
  1395. // set the correct language
  1396. if(linkLanguage == "C")
  1397. {
  1398. clOptions.AddFlag("CompileAs", "CompileAsC");
  1399. }
  1400. if(linkLanguage == "CXX")
  1401. {
  1402. clOptions.AddFlag("CompileAs", "CompileAsCpp");
  1403. }
  1404. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  1405. linkLanguage, configName.c_str());
  1406. // Get preprocessor definitions for this directory.
  1407. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1408. if(this->MSTools)
  1409. {
  1410. clOptions.FixExceptionHandlingDefault();
  1411. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  1412. std::string asmLocation = configName + "/";
  1413. clOptions.AddFlag("AssemblerListingLocation", asmLocation.c_str());
  1414. }
  1415. clOptions.Parse(flags.c_str());
  1416. clOptions.Parse(defineFlags.c_str());
  1417. std::vector<std::string> targetDefines;
  1418. this->Target->GetCompileDefinitions(targetDefines, configName.c_str());
  1419. clOptions.AddDefines(targetDefines);
  1420. if(this->MSTools)
  1421. {
  1422. clOptions.SetVerboseMakefile(
  1423. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1424. }
  1425. // Add a definition for the configuration name.
  1426. std::string configDefine = "CMAKE_INTDIR=\"";
  1427. configDefine += configName;
  1428. configDefine += "\"";
  1429. clOptions.AddDefine(configDefine);
  1430. if(const char* exportMacro = this->Target->GetExportMacro())
  1431. {
  1432. clOptions.AddDefine(exportMacro);
  1433. }
  1434. this->ClOptions[configName] = pOptions.release();
  1435. return true;
  1436. }
  1437. //----------------------------------------------------------------------------
  1438. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1439. std::string const& configName,
  1440. std::vector<std::string> const& includes)
  1441. {
  1442. Options& clOptions = *(this->ClOptions[configName]);
  1443. this->WriteString("<ClCompile>\n", 2);
  1444. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1445. this->OutputIncludes(includes);
  1446. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1447. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1448. "\n", "CXX");
  1449. if(this->MSTools)
  1450. {
  1451. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1452. // If not in debug mode, write the DebugInformationFormat field
  1453. // without value so PDBs don't get generated uselessly.
  1454. if(!clOptions.IsDebug())
  1455. {
  1456. this->WriteString("<DebugInformationFormat>"
  1457. "</DebugInformationFormat>\n", 3);
  1458. }
  1459. // Specify the compiler program database file if configured.
  1460. std::string pdb = this->Target->GetCompilePDBPath(configName.c_str());
  1461. if(!pdb.empty())
  1462. {
  1463. this->ConvertToWindowsSlash(pdb);
  1464. this->WriteString("<ProgramDataBaseFileName>", 3);
  1465. *this->BuildFileStream << cmVS10EscapeXML(pdb)
  1466. << "</ProgramDataBaseFileName>\n";
  1467. }
  1468. }
  1469. this->WriteString("</ClCompile>\n", 2);
  1470. }
  1471. void cmVisualStudio10TargetGenerator::
  1472. OutputIncludes(std::vector<std::string> const & includes)
  1473. {
  1474. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1475. for(std::vector<std::string>::const_iterator i = includes.begin();
  1476. i != includes.end(); ++i)
  1477. {
  1478. *this->BuildFileStream << cmVS10EscapeXML(*i) << ";";
  1479. }
  1480. this->WriteString("%(AdditionalIncludeDirectories)"
  1481. "</AdditionalIncludeDirectories>\n", 0);
  1482. }
  1483. //----------------------------------------------------------------------------
  1484. bool cmVisualStudio10TargetGenerator::ComputeRcOptions()
  1485. {
  1486. std::vector<std::string> const* configs =
  1487. this->GlobalGenerator->GetConfigurations();
  1488. for(std::vector<std::string>::const_iterator i = configs->begin();
  1489. i != configs->end(); ++i)
  1490. {
  1491. if(!this->ComputeRcOptions(*i))
  1492. {
  1493. return false;
  1494. }
  1495. }
  1496. return true;
  1497. }
  1498. //----------------------------------------------------------------------------
  1499. bool cmVisualStudio10TargetGenerator::ComputeRcOptions(
  1500. std::string const& configName)
  1501. {
  1502. cmsys::auto_ptr<Options> pOptions(
  1503. new Options(this->LocalGenerator, Options::ResourceCompiler,
  1504. this->GetRcFlagTable()));
  1505. Options& rcOptions = *pOptions;
  1506. std::string CONFIG = cmSystemTools::UpperCase(configName);
  1507. std::string rcConfigFlagsVar = std::string("CMAKE_RC_FLAGS_") + CONFIG;
  1508. std::string flags =
  1509. std::string(this->Makefile->GetSafeDefinition("CMAKE_RC_FLAGS")) +
  1510. std::string(" ") +
  1511. std::string(this->Makefile->GetSafeDefinition(rcConfigFlagsVar));
  1512. rcOptions.Parse(flags.c_str());
  1513. this->RcOptions[configName] = pOptions.release();
  1514. return true;
  1515. }
  1516. void cmVisualStudio10TargetGenerator::
  1517. WriteRCOptions(std::string const& configName,
  1518. std::vector<std::string> const & includes)
  1519. {
  1520. if(!this->MSTools)
  1521. {
  1522. return;
  1523. }
  1524. this->WriteString("<ResourceCompile>\n", 2);
  1525. // Preprocessor definitions and includes are shared with clOptions.
  1526. Options& clOptions = *(this->ClOptions[configName]);
  1527. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1528. "\n", "RC");
  1529. this->OutputIncludes(includes);
  1530. Options& rcOptions = *(this->RcOptions[configName]);
  1531. rcOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1532. rcOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1533. this->WriteString("</ResourceCompile>\n", 2);
  1534. }
  1535. void
  1536. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1537. {
  1538. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1539. {
  1540. return;
  1541. }
  1542. std::string libflags;
  1543. this->LocalGenerator->GetStaticLibraryFlags(libflags,
  1544. cmSystemTools::UpperCase(config), this->Target);
  1545. if(!libflags.empty())
  1546. {
  1547. this->WriteString("<Lib>\n", 2);
  1548. cmVisualStudioGeneratorOptions
  1549. libOptions(this->LocalGenerator,
  1550. cmVisualStudioGeneratorOptions::Linker,
  1551. this->GetLibFlagTable(), 0, this);
  1552. libOptions.Parse(libflags.c_str());
  1553. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1554. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1555. this->WriteString("</Lib>\n", 2);
  1556. }
  1557. }
  1558. //----------------------------------------------------------------------------
  1559. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  1560. {
  1561. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  1562. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  1563. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1564. {
  1565. std::vector<std::string> const* configs =
  1566. this->GlobalGenerator->GetConfigurations();
  1567. for(std::vector<std::string>::const_iterator i = configs->begin();
  1568. i != configs->end(); ++i)
  1569. {
  1570. if(!this->ComputeLinkOptions(*i))
  1571. {
  1572. return false;
  1573. }
  1574. }
  1575. }
  1576. return true;
  1577. }
  1578. //----------------------------------------------------------------------------
  1579. bool
  1580. cmVisualStudio10TargetGenerator::ComputeLinkOptions(std::string const& config)
  1581. {
  1582. cmsys::auto_ptr<Options> pOptions(
  1583. new Options(this->LocalGenerator, Options::Linker,
  1584. this->GetLinkFlagTable(), 0, this));
  1585. Options& linkOptions = *pOptions;
  1586. const std::string& linkLanguage =
  1587. this->Target->GetLinkerLanguage(config.c_str());
  1588. if(linkLanguage.empty())
  1589. {
  1590. cmSystemTools::Error
  1591. ("CMake can not determine linker language for target: ",
  1592. this->Name.c_str());
  1593. return false;
  1594. }
  1595. std::string CONFIG = cmSystemTools::UpperCase(config);
  1596. const char* linkType = "SHARED";
  1597. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1598. {
  1599. linkType = "MODULE";
  1600. }
  1601. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1602. {
  1603. linkType = "EXE";
  1604. }
  1605. std::string flags;
  1606. std::string linkFlagVarBase = "CMAKE_";
  1607. linkFlagVarBase += linkType;
  1608. linkFlagVarBase += "_LINKER_FLAGS";
  1609. flags += " ";
  1610. flags += this->
  1611. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1612. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1613. flags += " ";
  1614. flags += this->
  1615. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1616. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1617. if(targetLinkFlags)
  1618. {
  1619. flags += " ";
  1620. flags += targetLinkFlags;
  1621. }
  1622. std::string flagsProp = "LINK_FLAGS_";
  1623. flagsProp += CONFIG;
  1624. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1625. {
  1626. flags += " ";
  1627. flags += flagsConfig;
  1628. }
  1629. std::string standardLibsVar = "CMAKE_";
  1630. standardLibsVar += linkLanguage;
  1631. standardLibsVar += "_STANDARD_LIBRARIES";
  1632. std::string
  1633. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1634. // Remove trailing spaces from libs
  1635. std::string::size_type pos = libs.size()-1;
  1636. if(libs.size() != 0)
  1637. {
  1638. while(libs[pos] == ' ')
  1639. {
  1640. pos--;
  1641. }
  1642. }
  1643. if(pos != libs.size()-1)
  1644. {
  1645. libs = libs.substr(0, pos+1);
  1646. }
  1647. // Replace spaces in libs with ;
  1648. cmSystemTools::ReplaceString(libs, " ", ";");
  1649. std::vector<std::string> libVec;
  1650. cmSystemTools::ExpandListArgument(libs, libVec);
  1651. cmComputeLinkInformation* pcli =
  1652. this->Target->GetLinkInformation(config.c_str());
  1653. if(!pcli)
  1654. {
  1655. cmSystemTools::Error
  1656. ("CMake can not compute cmComputeLinkInformation for target: ",
  1657. this->Name.c_str());
  1658. return false;
  1659. }
  1660. // add the libraries for the target to libs string
  1661. cmComputeLinkInformation& cli = *pcli;
  1662. this->AddLibraries(cli, libVec);
  1663. linkOptions.AddFlag("AdditionalDependencies", libVec);
  1664. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1665. std::vector<std::string> linkDirs;
  1666. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1667. d != ldirs.end(); ++d)
  1668. {
  1669. // first just full path
  1670. linkDirs.push_back(*d);
  1671. // next path with configuration type Debug, Release, etc
  1672. linkDirs.push_back(*d + "/$(Configuration)");
  1673. }
  1674. linkDirs.push_back("%(AdditionalLibraryDirectories)");
  1675. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs);
  1676. std::string targetName;
  1677. std::string targetNameSO;
  1678. std::string targetNameFull;
  1679. std::string targetNameImport;
  1680. std::string targetNamePDB;
  1681. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1682. {
  1683. this->Target->GetExecutableNames(targetName, targetNameFull,
  1684. targetNameImport, targetNamePDB,
  1685. config.c_str());
  1686. }
  1687. else
  1688. {
  1689. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1690. targetNameImport, targetNamePDB,
  1691. config.c_str());
  1692. }
  1693. if(this->MSTools)
  1694. {
  1695. linkOptions.AddFlag("Version", "");
  1696. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1697. {
  1698. linkOptions.AddFlag("SubSystem", "Windows");
  1699. }
  1700. else
  1701. {
  1702. linkOptions.AddFlag("SubSystem", "Console");
  1703. }
  1704. if(const char* stackVal =
  1705. this->Makefile->GetDefinition("CMAKE_"+linkLanguage+"_STACK_SIZE"))
  1706. {
  1707. linkOptions.AddFlag("StackReserveSize", stackVal);
  1708. }
  1709. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1710. {
  1711. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1712. }
  1713. else
  1714. {
  1715. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1716. }
  1717. std::string pdb = this->Target->GetPDBDirectory(config.c_str());
  1718. pdb += "/";
  1719. pdb += targetNamePDB;
  1720. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1721. imLib += "/";
  1722. imLib += targetNameImport;
  1723. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1724. linkOptions.AddFlag("ProgramDataBaseFile", pdb.c_str());
  1725. }
  1726. linkOptions.Parse(flags.c_str());
  1727. if(this->MSTools)
  1728. {
  1729. std::string def = this->GeneratorTarget->GetModuleDefinitionFile("");
  1730. if(!def.empty())
  1731. {
  1732. linkOptions.AddFlag("ModuleDefinitionFile", def.c_str());
  1733. }
  1734. linkOptions.AppendFlag("IgnoreSpecificDefaultLibraries",
  1735. "%(IgnoreSpecificDefaultLibraries)");
  1736. }
  1737. this->LinkOptions[config] = pOptions.release();
  1738. return true;
  1739. }
  1740. //----------------------------------------------------------------------------
  1741. void
  1742. cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const& config)
  1743. {
  1744. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1745. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1746. {
  1747. return;
  1748. }
  1749. Options& linkOptions = *(this->LinkOptions[config]);
  1750. this->WriteString("<Link>\n", 2);
  1751. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1752. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1753. this->WriteString("</Link>\n", 2);
  1754. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1755. {
  1756. this->WriteString("<ProjectReference>\n", 2);
  1757. this->WriteString(
  1758. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1759. this->WriteString("</ProjectReference>\n", 2);
  1760. }
  1761. }
  1762. void cmVisualStudio10TargetGenerator::AddLibraries(
  1763. cmComputeLinkInformation& cli,
  1764. std::vector<std::string>& libVec)
  1765. {
  1766. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1767. ItemVector libs = cli.GetItems();
  1768. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1769. {
  1770. if(l->IsPath)
  1771. {
  1772. std::string path = this->LocalGenerator->
  1773. Convert(l->Value.c_str(),
  1774. cmLocalGenerator::START_OUTPUT,
  1775. cmLocalGenerator::UNCHANGED);
  1776. this->ConvertToWindowsSlash(path);
  1777. libVec.push_back(path);
  1778. }
  1779. else if (!l->Target
  1780. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1781. {
  1782. libVec.push_back(l->Value);
  1783. }
  1784. }
  1785. }
  1786. void cmVisualStudio10TargetGenerator::
  1787. WriteMidlOptions(std::string const& /*config*/,
  1788. std::vector<std::string> const & includes)
  1789. {
  1790. if(!this->MSTools)
  1791. {
  1792. return;
  1793. }
  1794. // This processes *any* of the .idl files specified in the project's file
  1795. // list (and passed as the item metadata %(Filename) expressing the rule
  1796. // input filename) into output files at the per-config *build* dir
  1797. // ($(IntDir)) each.
  1798. //
  1799. // IOW, this MIDL section is intended to provide a fully generic syntax
  1800. // content suitable for most cases (read: if you get errors, then it's quite
  1801. // probable that the error is on your side of the .idl setup).
  1802. //
  1803. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  1804. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  1805. // project's file list, otherwise the compiler-side processing won't pick it
  1806. // up (for non-directory form, it ends up looking in project binary dir
  1807. // only). Perhaps there's something to be done to make this more automatic
  1808. // on the CMake side?
  1809. this->WriteString("<Midl>\n", 2);
  1810. this->OutputIncludes(includes);
  1811. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1812. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1813. this->WriteString(
  1814. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1815. this->WriteString(
  1816. "<InterfaceIdentifierFileName>"
  1817. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1818. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1819. this->WriteString("</Midl>\n", 2);
  1820. }
  1821. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1822. {
  1823. std::vector<std::string> *configs =
  1824. static_cast<cmGlobalVisualStudio7Generator *>
  1825. (this->GlobalGenerator)->GetConfigurations();
  1826. for(std::vector<std::string>::iterator i = configs->begin();
  1827. i != configs->end(); ++i)
  1828. {
  1829. std::vector<std::string> includes;
  1830. this->LocalGenerator->GetIncludeDirectories(includes,
  1831. this->GeneratorTarget,
  1832. "C", i->c_str());
  1833. for(std::vector<std::string>::iterator ii = includes.begin();
  1834. ii != includes.end(); ++ii)
  1835. {
  1836. this->ConvertToWindowsSlash(*ii);
  1837. }
  1838. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1839. *this->BuildFileStream << "\n";
  1840. // output cl compile flags <ClCompile></ClCompile>
  1841. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  1842. {
  1843. this->WriteClOptions(*i, includes);
  1844. // output rc compile flags <ResourceCompile></ResourceCompile>
  1845. this->WriteRCOptions(*i, includes);
  1846. }
  1847. // output midl flags <Midl></Midl>
  1848. this->WriteMidlOptions(*i, includes);
  1849. // write events
  1850. this->WriteEvents(*i);
  1851. // output link flags <Link></Link>
  1852. this->WriteLinkOptions(*i);
  1853. // output lib flags <Lib></Lib>
  1854. this->WriteLibOptions(*i);
  1855. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1856. }
  1857. }
  1858. void
  1859. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1860. {
  1861. this->WriteEvent("PreLinkEvent",
  1862. this->Target->GetPreLinkCommands(), configName);
  1863. this->WriteEvent("PreBuildEvent",
  1864. this->Target->GetPreBuildCommands(), configName);
  1865. this->WriteEvent("PostBuildEvent",
  1866. this->Target->GetPostBuildCommands(), configName);
  1867. }
  1868. void cmVisualStudio10TargetGenerator::WriteEvent(
  1869. const char* name,
  1870. std::vector<cmCustomCommand> const& commands,
  1871. std::string const& configName)
  1872. {
  1873. if(commands.size() == 0)
  1874. {
  1875. return;
  1876. }
  1877. this->WriteString("<", 2);
  1878. (*this->BuildFileStream ) << name << ">\n";
  1879. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1880. std::string script;
  1881. const char* pre = "";
  1882. std::string comment;
  1883. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1884. i != commands.end(); ++i)
  1885. {
  1886. cmCustomCommandGenerator ccg(*i, configName, this->Makefile);
  1887. comment += pre;
  1888. comment += lg->ConstructComment(ccg);
  1889. script += pre;
  1890. pre = "\n";
  1891. script += cmVS10EscapeXML(lg->ConstructScript(ccg));
  1892. }
  1893. comment = cmVS10EscapeComment(comment);
  1894. this->WriteString("<Message>",3);
  1895. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1896. this->WriteString("<Command>", 3);
  1897. (*this->BuildFileStream ) << script;
  1898. (*this->BuildFileStream ) << "</Command>" << "\n";
  1899. this->WriteString("</", 2);
  1900. (*this->BuildFileStream ) << name << ">\n";
  1901. }
  1902. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1903. {
  1904. cmGlobalGenerator::TargetDependSet const& unordered
  1905. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1906. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1907. OrderedTargetDependSet;
  1908. OrderedTargetDependSet depends(unordered);
  1909. this->WriteString("<ItemGroup>\n", 1);
  1910. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1911. i != depends.end(); ++i)
  1912. {
  1913. cmTarget const* dt = *i;
  1914. if(dt->GetType() == cmTarget::INTERFACE_LIBRARY)
  1915. {
  1916. continue;
  1917. }
  1918. // skip fortran targets as they can not be processed by MSBuild
  1919. // the only reference will be in the .sln file
  1920. if(static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
  1921. ->TargetIsFortranOnly(*dt))
  1922. {
  1923. continue;
  1924. }
  1925. this->WriteString("<ProjectReference Include=\"", 2);
  1926. cmMakefile* mf = dt->GetMakefile();
  1927. std::string name = dt->GetName();
  1928. std::string path;
  1929. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1930. if(p)
  1931. {
  1932. path = p;
  1933. }
  1934. else
  1935. {
  1936. path = mf->GetStartOutputDirectory();
  1937. path += "/";
  1938. path += dt->GetName();
  1939. path += ".vcxproj";
  1940. }
  1941. (*this->BuildFileStream) << cmVS10EscapeXML(path) << "\">\n";
  1942. this->WriteString("<Project>", 3);
  1943. (*this->BuildFileStream)
  1944. << this->GlobalGenerator->GetGUID(name.c_str())
  1945. << "</Project>\n";
  1946. this->WriteString("</ProjectReference>\n", 2);
  1947. }
  1948. this->WriteString("</ItemGroup>\n", 1);
  1949. }
  1950. bool cmVisualStudio10TargetGenerator::
  1951. IsResxHeader(const std::string& headerFile)
  1952. {
  1953. std::set<std::string> expectedResxHeaders;
  1954. this->GeneratorTarget->GetExpectedResxHeaders(expectedResxHeaders, "");
  1955. std::set<std::string>::const_iterator it =
  1956. expectedResxHeaders.find(headerFile);
  1957. return it != expectedResxHeaders.end();
  1958. }
  1959. void cmVisualStudio10TargetGenerator::WriteApplicationTypeSettings()
  1960. {
  1961. bool const isWindowsPhone = this->GlobalGenerator->TargetsWindowsPhone();
  1962. bool const isWindowsStore = this->GlobalGenerator->TargetsWindowsStore();
  1963. std::string const& v = this->GlobalGenerator->GetSystemVersion();
  1964. if(isWindowsPhone || isWindowsStore)
  1965. {
  1966. this->WriteString("<ApplicationType>", 2);
  1967. (*this->BuildFileStream) << (isWindowsPhone ?
  1968. "Windows Phone" : "Windows Store")
  1969. << "</ApplicationType>\n";
  1970. this->WriteString("<ApplicationTypeRevision>", 2);
  1971. (*this->BuildFileStream) << cmVS10EscapeXML(v)
  1972. << "</ApplicationTypeRevision>\n";
  1973. if(v == "8.1")
  1974. {
  1975. // Visual Studio 12.0 is necessary for building 8.1 apps
  1976. this->WriteString("<MinimumVisualStudioVersion>12.0"
  1977. "</MinimumVisualStudioVersion>\n", 2);
  1978. }
  1979. else if (v == "8.0")
  1980. {
  1981. // Visual Studio 11.0 is necessary for building 8.0 apps
  1982. this->WriteString("<MinimumVisualStudioVersion>11.0"
  1983. "</MinimumVisualStudioVersion>\n", 2);
  1984. }
  1985. }
  1986. if (this->Platform == "ARM")
  1987. {
  1988. this->WriteString("<WindowsSDKDesktopARMSupport>true"
  1989. "</WindowsSDKDesktopARMSupport>", 2);
  1990. }
  1991. }