cmVisualStudio10TargetGenerator.cxx 59 KB

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