cmVisualStudio10TargetGenerator.cxx 61 KB

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