cmVisualStudio10TargetGenerator.cxx 62 KB

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