cmVisualStudio10TargetGenerator.cxx 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmVisualStudio10TargetGenerator.h"
  14. #include "cmGlobalVisualStudio7Generator.h"
  15. #include "cmTarget.h"
  16. #include "cmComputeLinkInformation.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmVisualStudioGeneratorOptions.h"
  21. #include "cmLocalVisualStudio7Generator.h"
  22. #include "cmVS10CLFlagTable.h"
  23. #include "cmVS10LinkFlagTable.h"
  24. #include "cmVS10LibFlagTable.h"
  25. static std::string cmVS10EscapeXML(std::string arg)
  26. {
  27. cmSystemTools::ReplaceString(arg, "&", "&");
  28. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  29. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  30. return arg;
  31. }
  32. cmVisualStudio10TargetGenerator::
  33. cmVisualStudio10TargetGenerator(cmTarget* target,
  34. cmGlobalVisualStudio7Generator* gg)
  35. {
  36. this->GlobalGenerator = gg;
  37. this->Target = target;
  38. this->Makefile = target->GetMakefile();
  39. this->LocalGenerator =
  40. (cmLocalVisualStudio7Generator*)
  41. this->Makefile->GetLocalGenerator();
  42. this->Name = this->Target->GetName();
  43. this->GlobalGenerator->CreateGUID(this->Name.c_str());
  44. this->GUID = this->GlobalGenerator->GetGUID(this->Name.c_str());
  45. this->Platform = "|Win32";
  46. this->ComputeObjectNames();
  47. this->BuildFileStream = 0;
  48. }
  49. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
  50. {
  51. if(!this->BuildFileStream)
  52. {
  53. return;
  54. }
  55. if (this->BuildFileStream->Close())
  56. {
  57. this->GlobalGenerator
  58. ->FileReplacedDuringGenerate(this->PathToVcxproj);
  59. }
  60. delete this->BuildFileStream;
  61. }
  62. void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
  63. const char* tag,
  64. const char* config,
  65. int indentLevel,
  66. const char* attribute,
  67. const char* end,
  68. std::ostream* stream)
  69. {
  70. if(!stream)
  71. {
  72. stream = this->BuildFileStream;
  73. }
  74. stream->fill(' ');
  75. stream->width(indentLevel*2 );
  76. (*stream ) << "";
  77. (*stream ) << "<" << tag
  78. << " Condition=\"'$(Configuration)|$(Platform)'=='";
  79. (*stream ) << config << this->Platform << "'\"";
  80. if(attribute)
  81. {
  82. (*stream ) << attribute;
  83. }
  84. // close the tag
  85. (*stream ) << ">";
  86. if(end)
  87. {
  88. (*stream ) << end;
  89. }
  90. }
  91. void cmVisualStudio10TargetGenerator::WriteString(const char* line,
  92. int indentLevel)
  93. {
  94. this->BuildFileStream->fill(' ');
  95. this->BuildFileStream->width(indentLevel*2 );
  96. // write an empty string to get the fill level indent to print
  97. (*this->BuildFileStream ) << "";
  98. (*this->BuildFileStream ) << line;
  99. }
  100. void cmVisualStudio10TargetGenerator::Generate()
  101. {
  102. // do not generate external ms projects
  103. if(this->Target->GetProperty("EXTERNAL_MSPROJECT"))
  104. {
  105. return;
  106. }
  107. // Tell the global generator the name of the project file
  108. this->Target->SetProperty("GENERATOR_FILE_NAME",this->Name.c_str());
  109. this->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  110. ".vcxproj");
  111. cmMakefile* mf = this->Target->GetMakefile();
  112. std::string path = mf->GetStartOutputDirectory();
  113. path += "/";
  114. path += this->Name;
  115. path += ".vcxproj";
  116. this->BuildFileStream =
  117. new cmGeneratedFileStream(path.c_str());
  118. this->PathToVcxproj = path;
  119. this->BuildFileStream->SetCopyIfDifferent(true);
  120. // Write the encoding header into the file
  121. char magic[] = {0xEF,0xBB, 0xBF};
  122. this->BuildFileStream->write(magic, 3);
  123. this->WriteString("<Project DefaultTargets=\"Build\" "
  124. "ToolsVersion=\"4.0\" "
  125. "xmlns=\"http://schemas.microsoft.com/"
  126. "developer/msbuild/2003\">\n",
  127. 0);
  128. this->WriteProjectConfigurations();
  129. this->WriteString("<PropertyGroup Label=\"Globals\">\n", 1);
  130. this->WriteString("<ProjectGUID>", 2);
  131. (*this->BuildFileStream) << "{" << this->GUID << "}</ProjectGUID>\n";
  132. this->WriteString("<SccProjectName />\n", 2);
  133. this->WriteString("<SccLocalPath />\n", 2);
  134. this->WriteString("<Keyword>Win32Proj</Keyword>\n", 2);
  135. this->WriteString("</PropertyGroup>\n", 1);
  136. this->WriteString("<Import Project="
  137. "\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n",
  138. 1);
  139. this->WriteProjectConfigurationValues();
  140. this->WriteString(
  141. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n", 1);
  142. this->WriteString("<ImportGroup Label=\"ExtensionSettings\">\n", 1);
  143. this->WriteString("</ImportGroup>\n", 1);
  144. this->WriteString("<ImportGroup Label=\"PropertySheets\">\n", 1);
  145. this->WriteString("<Import Project="
  146. "\"$(LocalAppData)\\Microsoft\\VisualStudio\\10.0\\"
  147. "Microsoft.Cpp.$(Platform).user.props\" "
  148. "Condition=\"exists('$(LocalAppData)\\Microsoft"
  149. "\\VisualStudio\\10.0\\"
  150. "Microsoft.Cpp.$(Platform).user.props')\" />\n", 2);
  151. this->WriteString("</ImportGroup>\n", 1);
  152. this->WriteString("<PropertyGroup Label=\"UserMacros\" />\n", 1);
  153. this->WritePathAndIncrementalLinkOptions();
  154. this->WriteItemDefinitionGroups();
  155. this->WriteCustomCommands();
  156. this->WriteObjSources();
  157. this->WriteCLSources();
  158. this->WriteProjectReferences();
  159. this->WriteString(
  160. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\""
  161. " />\n", 1);
  162. this->WriteString("<ImportGroup Label=\"ExtensionTargets\">\n", 1);
  163. this->WriteString("</ImportGroup>\n", 1);
  164. this->WriteString("</Project>", 0);
  165. // The groups are stored in a separate file for VS 10
  166. this->WriteGroups();
  167. }
  168. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  169. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations()
  170. {
  171. this->WriteString("<ItemGroup Label=\"ProjectConfigurations\">\n", 1);
  172. std::vector<std::string> *configs =
  173. static_cast<cmGlobalVisualStudio7Generator *>
  174. (this->GlobalGenerator)->GetConfigurations();
  175. for(std::vector<std::string>::iterator i = configs->begin();
  176. i != configs->end(); ++i)
  177. {
  178. this->WriteString("<ProjectConfiguration Include=\"", 2);
  179. (*this->BuildFileStream ) << *i << this->Platform << "\">\n";
  180. this->WriteString("<Configuration>", 3);
  181. (*this->BuildFileStream ) << *i << "</Configuration>\n";
  182. this->WriteString("<Platform>Win32</Platform>\n", 3);
  183. this->WriteString("</ProjectConfiguration>\n", 2);
  184. }
  185. this->WriteString("</ItemGroup>\n", 1);
  186. }
  187. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues()
  188. {
  189. std::vector<std::string> *configs =
  190. static_cast<cmGlobalVisualStudio7Generator *>
  191. (this->GlobalGenerator)->GetConfigurations();
  192. for(std::vector<std::string>::iterator i = configs->begin();
  193. i != configs->end(); ++i)
  194. {
  195. this->WritePlatformConfigTag("PropertyGroup",
  196. i->c_str(),
  197. 1, " Label=\"Configuration\"", "\n");
  198. std::string configType = "<ConfigurationType>";
  199. switch(this->Target->GetType())
  200. {
  201. case cmTarget::SHARED_LIBRARY:
  202. case cmTarget::MODULE_LIBRARY:
  203. configType += "DynamicLibrary";
  204. break;
  205. case cmTarget::STATIC_LIBRARY:
  206. configType += "StaticLibrary";
  207. break;
  208. case cmTarget::EXECUTABLE:
  209. configType += "Application";
  210. break;
  211. case cmTarget::UTILITY:
  212. configType += "Utility";
  213. break;
  214. }
  215. configType += "</ConfigurationType>\n";
  216. this->WriteString(configType.c_str(), 2);
  217. const char* mfcFlag =
  218. this->Target->GetMakefile()->GetDefinition("CMAKE_MFC_FLAG");
  219. if(mfcFlag)
  220. {
  221. this->WriteString("<UseOfMfc>true</UseOfMfc>\n", 2);
  222. }
  223. else
  224. {
  225. this->WriteString("<UseOfMfc>false</UseOfMfc>\n", 2);
  226. }
  227. this->WriteString("<CharacterSet>MultiByte</CharacterSet>\n", 2);
  228. this->WriteString("</PropertyGroup>\n", 1);
  229. }
  230. }
  231. void cmVisualStudio10TargetGenerator::WriteCustomCommands()
  232. {
  233. this->SourcesVisited.clear();
  234. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  235. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  236. source != sources.end(); ++source)
  237. {
  238. cmSourceFile* sf = *source;
  239. this->WriteCustomCommand(sf);
  240. }
  241. }
  242. //----------------------------------------------------------------------------
  243. void cmVisualStudio10TargetGenerator::WriteCustomCommand(cmSourceFile* sf)
  244. {
  245. if(this->SourcesVisited.insert(sf).second)
  246. {
  247. if(std::vector<cmSourceFile*> const* depends =
  248. this->Target->GetSourceDepends(sf))
  249. {
  250. for(std::vector<cmSourceFile*>::const_iterator di = depends->begin();
  251. di != depends->end(); ++di)
  252. {
  253. this->WriteCustomCommand(*di);
  254. }
  255. }
  256. if(cmCustomCommand const* command = sf->GetCustomCommand())
  257. {
  258. this->WriteString("<ItemGroup>\n", 1);
  259. this->WriteCustomRule(sf, *command);
  260. this->WriteString("</ItemGroup>\n", 1);
  261. }
  262. }
  263. }
  264. void
  265. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile* source,
  266. cmCustomCommand const &
  267. command)
  268. {
  269. std::string sourcePath = source->GetFullPath();
  270. // the rule file seems to need to exist for vs10
  271. if (source->GetExtension() == "rule")
  272. {
  273. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  274. {
  275. std::ofstream fout(sourcePath.c_str());
  276. if(fout)
  277. {
  278. fout << "# generated from CMake\n";
  279. fout.flush();
  280. fout.close();
  281. }
  282. }
  283. }
  284. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  285. std::string comment = lg->ConstructComment(command);
  286. std::vector<std::string> *configs =
  287. static_cast<cmGlobalVisualStudio7Generator *>
  288. (this->GlobalGenerator)->GetConfigurations();
  289. this->WriteString("<CustomBuild Include=\"", 2);
  290. std::string path =
  291. cmSystemTools::RelativePath(
  292. this->Makefile->GetCurrentOutputDirectory(),
  293. sourcePath.c_str());
  294. this->ConvertToWindowsSlash(path);
  295. (*this->BuildFileStream ) << path << "\">\n";
  296. for(std::vector<std::string>::iterator i = configs->begin();
  297. i != configs->end(); ++i)
  298. {
  299. std::string script =
  300. cmVS10EscapeXML(
  301. lg->ConstructScript(command.GetCommandLines(),
  302. command.GetWorkingDirectory(),
  303. i->c_str(),
  304. command.GetEscapeOldStyle(),
  305. command.GetEscapeAllowMakeVars())
  306. );
  307. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  308. (*this->BuildFileStream ) << comment << "</Message>\n";
  309. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  310. (*this->BuildFileStream ) << script << "</Command>\n";
  311. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  312. (*this->BuildFileStream ) << source->GetFullPath();
  313. for(std::vector<std::string>::const_iterator d =
  314. command.GetDepends().begin();
  315. d != command.GetDepends().end();
  316. ++d)
  317. {
  318. std::string dep = this->LocalGenerator->
  319. GetRealDependency(d->c_str(), i->c_str());
  320. this->ConvertToWindowsSlash(dep);
  321. (*this->BuildFileStream ) << ";" << dep;
  322. }
  323. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  324. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  325. const char* sep = "";
  326. for(std::vector<std::string>::const_iterator o =
  327. command.GetOutputs().begin();
  328. o != command.GetOutputs().end();
  329. ++o)
  330. {
  331. std::string out = *o;
  332. this->ConvertToWindowsSlash(out);
  333. (*this->BuildFileStream ) << sep << out;
  334. sep = ";";
  335. }
  336. (*this->BuildFileStream ) << ";%(Outputs)</Outputs>\n";
  337. }
  338. this->WriteString("</CustomBuild>\n", 2);
  339. }
  340. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  341. {
  342. // first convert all of the slashes
  343. std::string::size_type pos = 0;
  344. while((pos = s.find('/', pos)) != std::string::npos)
  345. {
  346. s[pos] = '\\';
  347. pos++;
  348. }
  349. }
  350. void cmVisualStudio10TargetGenerator::WriteGroups()
  351. {
  352. // collect up group information
  353. std::vector<cmSourceGroup> sourceGroups =
  354. this->Makefile->GetSourceGroups();
  355. std::vector<cmSourceFile*> classes = this->Target->GetSourceFiles();
  356. std::set<cmSourceGroup*> groupsUsed;
  357. std::vector<cmSourceFile*> clCompile;
  358. std::vector<cmSourceFile*> customBuild;
  359. std::vector<cmSourceFile*> none;
  360. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  361. s != classes.end(); s++)
  362. {
  363. cmSourceFile* sf = *s;
  364. std::string const& source = sf->GetFullPath();
  365. cmSourceGroup& sourceGroup =
  366. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  367. groupsUsed.insert(&sourceGroup);
  368. const char* lang = sf->GetLanguage();
  369. if(!lang)
  370. {
  371. lang = "None";
  372. }
  373. if(lang[0] == 'C')
  374. {
  375. clCompile.push_back(sf);
  376. }
  377. else if(sf->GetCustomCommand())
  378. {
  379. customBuild.push_back(sf);
  380. }
  381. else
  382. {
  383. none.push_back(sf);
  384. }
  385. }
  386. // Write out group file
  387. std::string path = this->Makefile->GetStartOutputDirectory();
  388. path += "/";
  389. path += this->Name;
  390. path += ".vcxproj.filters";
  391. cmGeneratedFileStream fout(path.c_str());
  392. char magic[] = {0xEF,0xBB, 0xBF};
  393. fout.write(magic, 3);
  394. cmGeneratedFileStream* save = this->BuildFileStream;
  395. this->BuildFileStream = & fout;
  396. this->WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  397. "<Project "
  398. "ToolsVersion=\"4.0\" "
  399. "xmlns=\"http://schemas.microsoft.com/"
  400. "developer/msbuild/2003\">\n",
  401. 0);
  402. this->WriteGroupSources("ClCompile", clCompile, sourceGroups);
  403. this->WriteGroupSources("CustomBuild", customBuild, sourceGroups);
  404. this->WriteString("<ItemGroup>\n", 1);
  405. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  406. g != groupsUsed.end(); ++g)
  407. {
  408. cmSourceGroup* sg = *g;
  409. const char* name = sg->GetFullName();
  410. if(strlen(name) != 0)
  411. {
  412. this->WriteString("<Filter Include=\"", 2);
  413. (*this->BuildFileStream) << name << "\">\n";
  414. std::string guidName = "SG_Filter_";
  415. guidName += name;
  416. this->GlobalGenerator->CreateGUID(guidName.c_str());
  417. this->WriteString("<UniqueIdentifier>", 3);
  418. std::string guid
  419. = this->GlobalGenerator->GetGUID(guidName.c_str());
  420. (*this->BuildFileStream)
  421. << "{"
  422. << guid << "}"
  423. << "</UniqueIdentifier>\n";
  424. this->WriteString("</Filter>\n", 2);
  425. }
  426. }
  427. this->WriteString("</ItemGroup>\n", 1);
  428. this->WriteGroupSources("None", none, sourceGroups);
  429. this->WriteString("</Project>\n", 0);
  430. // restore stream pointer
  431. this->BuildFileStream = save;
  432. }
  433. void
  434. cmVisualStudio10TargetGenerator::
  435. WriteGroupSources(const char* name,
  436. std::vector<cmSourceFile*> const& sources,
  437. std::vector<cmSourceGroup>& sourceGroups)
  438. {
  439. this->WriteString("<ItemGroup>\n", 1);
  440. for(std::vector<cmSourceFile*>::const_iterator s = sources.begin();
  441. s != sources.end(); ++s)
  442. {
  443. cmSourceFile* sf = *s;
  444. std::string const& source = sf->GetFullPath();
  445. cmSourceGroup& sourceGroup =
  446. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  447. const char* filter = sourceGroup.GetFullName();
  448. this->WriteString("<", 2);
  449. std::string path = source;
  450. path = cmSystemTools::RelativePath(
  451. this->Makefile->GetCurrentOutputDirectory(),
  452. source.c_str());
  453. this->ConvertToWindowsSlash(path);
  454. (*this->BuildFileStream) << name << " Include=\""
  455. << path;
  456. if(strlen(filter))
  457. {
  458. (*this->BuildFileStream) << "\">\n";
  459. this->WriteString("<Filter>", 3);
  460. (*this->BuildFileStream) << filter << "</Filter>\n";
  461. this->WriteString("</", 2);
  462. (*this->BuildFileStream) << name << ">\n";
  463. }
  464. else
  465. {
  466. (*this->BuildFileStream) << "\" />\n";
  467. }
  468. }
  469. this->WriteString("</ItemGroup>\n", 1);
  470. }
  471. void cmVisualStudio10TargetGenerator::WriteObjSources()
  472. {
  473. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  474. {
  475. return;
  476. }
  477. bool first = true;
  478. std::vector<cmSourceFile*>const & sources = this->Target->GetSourceFiles();
  479. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  480. source != sources.end(); ++source)
  481. {
  482. if((*source)->GetExtension() == "obj")
  483. {
  484. if(first)
  485. {
  486. this->WriteString("<ItemGroup>\n", 1);
  487. first = false;
  488. }
  489. this->WriteString("<None Include=\"", 2);
  490. (*this->BuildFileStream ) << (*source)->GetFullPath() << "\" />\n";
  491. }
  492. }
  493. if(!first)
  494. {
  495. this->WriteString("</ItemGroup>\n", 1);
  496. }
  497. }
  498. void cmVisualStudio10TargetGenerator::WriteCLSources()
  499. {
  500. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  501. {
  502. return;
  503. }
  504. this->WriteString("<ItemGroup>\n", 1);
  505. std::vector<cmSourceFile*>const& sources = this->Target->GetSourceFiles();
  506. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  507. source != sources.end(); ++source)
  508. {
  509. // if it is not a custom command then add it as a c/c++ file,
  510. // TODO: need to check for idl or rc
  511. if(!(*source)->GetCustomCommand()
  512. && !(*source)->GetPropertyAsBool("HEADER_FILE_ONLY")
  513. && !this->GlobalGenerator->IgnoreFile
  514. ((*source)->GetExtension().c_str()))
  515. {
  516. const char* lang = (*source)->GetLanguage();
  517. if(lang && (strcmp(lang, "C") == 0 || strcmp(lang, "CXX") ==0))
  518. {
  519. std::string sourceFile = (*source)->GetFullPath();
  520. sourceFile = cmSystemTools::RelativePath(
  521. this->Makefile->GetCurrentOutputDirectory(),
  522. sourceFile.c_str());
  523. this->ConvertToWindowsSlash(sourceFile);
  524. // output the source file
  525. this->WriteString("<ClCompile Include=\"", 2);
  526. (*this->BuildFileStream ) << sourceFile << "\"";
  527. // ouput any flags specific to this source file
  528. if(this->OutputSourceSpecificFlags(*source))
  529. {
  530. // if the source file has specific flags the tag
  531. // is ended on a new line
  532. this->WriteString("</ClCompile>\n", 2);
  533. }
  534. else
  535. {
  536. (*this->BuildFileStream ) << " />\n";
  537. }
  538. }
  539. }
  540. }
  541. this->WriteString("</ItemGroup>\n", 1);
  542. }
  543. void cmVisualStudio10TargetGenerator::ComputeObjectNames()
  544. {
  545. // We may be modifying the source groups temporarily, so make a copy.
  546. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  547. // get the classes from the source lists then add them to the groups
  548. std::vector<cmSourceFile*>const & classes = this->Target->GetSourceFiles();
  549. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  550. i != classes.end(); i++)
  551. {
  552. // Add the file to the list of sources.
  553. std::string source = (*i)->GetFullPath();
  554. if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
  555. {
  556. this->ModuleDefinitionFile = (*i)->GetFullPath();
  557. }
  558. cmSourceGroup& sourceGroup =
  559. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  560. sourceGroup.AssignSource(*i);
  561. }
  562. // Compute which sources need unique object computation.
  563. this->LocalGenerator->ComputeObjectNameRequirements(sourceGroups);
  564. }
  565. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  566. cmSourceFile* source)
  567. {
  568. cmSourceFile& sf = *source;
  569. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  570. // Compute the maximum length full path to the intermediate
  571. // files directory for any configuration. This is used to construct
  572. // object file names that do not produce paths that are too long.
  573. std::string dir_max;
  574. lg->ComputeMaxDirectoryLength(dir_max, *this->Target);
  575. std::string objectName;
  576. if(lg->NeedObjectName.find(&sf) != lg->NeedObjectName.end())
  577. {
  578. objectName = lg->GetObjectFileNameWithoutTarget(sf, dir_max);
  579. }
  580. std::string flags;
  581. std::string defines;
  582. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  583. {
  584. flags += cflags;
  585. }
  586. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  587. {
  588. defines += cdefs;
  589. }
  590. const char* lang =
  591. this->GlobalGenerator->GetLanguageFromExtension
  592. (sf.GetExtension().c_str());
  593. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  594. const char* linkLanguage = this->Target->GetLinkerLanguage();
  595. bool needForceLang = false;
  596. // source file does not match its extension language
  597. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  598. {
  599. needForceLang = true;
  600. lang = sourceLang;
  601. }
  602. // if the source file does not match the linker language
  603. // then force c or c++
  604. if(needForceLang || (linkLanguage && lang
  605. && strcmp(lang, linkLanguage) != 0))
  606. {
  607. if(strcmp(lang, "CXX") == 0)
  608. {
  609. // force a C++ file type
  610. flags += " /TP ";
  611. }
  612. else if(strcmp(lang, "C") == 0)
  613. {
  614. // force to c
  615. flags += " /TC ";
  616. }
  617. }
  618. bool hasFlags = false;
  619. // for the first time we need a new line if there is something
  620. // produced here.
  621. const char* firstString = ">\n";
  622. if(objectName.size())
  623. {
  624. (*this->BuildFileStream ) << firstString;
  625. firstString = "";
  626. hasFlags = true;
  627. this->WriteString("<ObjectFileName>", 3);
  628. (*this->BuildFileStream )
  629. << "$(Configuration)/" << objectName << "</ObjectFileName>\n";
  630. }
  631. std::vector<std::string> *configs =
  632. static_cast<cmGlobalVisualStudio7Generator *>
  633. (this->GlobalGenerator)->GetConfigurations();
  634. for( std::vector<std::string>::iterator config = configs->begin();
  635. config != configs->end(); ++config)
  636. {
  637. std::string configUpper = cmSystemTools::UpperCase(*config);
  638. std::string configDefines = defines;
  639. std::string defPropName = "COMPILE_DEFINITIONS_";
  640. defPropName += configUpper;
  641. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  642. {
  643. if(configDefines.size())
  644. {
  645. configDefines += ";";
  646. }
  647. configDefines += ccdefs;
  648. }
  649. // if we have flags or defines for this config then
  650. // use them
  651. if(flags.size() || configDefines.size())
  652. {
  653. (*this->BuildFileStream ) << firstString;
  654. firstString = ""; // only do firstString once
  655. hasFlags = true;
  656. cmVisualStudioGeneratorOptions
  657. clOptions(this->LocalGenerator,
  658. 10, cmVisualStudioGeneratorOptions::Compiler,
  659. cmVS10CLFlagTable, 0, this);
  660. clOptions.Parse(flags.c_str());
  661. clOptions.AddDefines(configDefines.c_str());
  662. clOptions.SetConfiguration((*config).c_str());
  663. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  664. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  665. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  666. " ", "\n");
  667. }
  668. }
  669. return hasFlags;
  670. }
  671. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  672. {
  673. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  674. {
  675. return;
  676. }
  677. this->WriteString("<PropertyGroup>\n", 2);
  678. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  679. "</_ProjectFileVersion>\n", 3);
  680. std::vector<std::string> *configs =
  681. static_cast<cmGlobalVisualStudio7Generator *>
  682. (this->GlobalGenerator)->GetConfigurations();
  683. for(std::vector<std::string>::iterator config = configs->begin();
  684. config != configs->end(); ++config)
  685. {
  686. std::string targetNameFull =
  687. this->Target->GetFullName(config->c_str());
  688. std::string intermediateDir = this->LocalGenerator->
  689. GetTargetDirectory(*this->Target);
  690. intermediateDir += "/";
  691. intermediateDir += *config;
  692. intermediateDir += "/";
  693. this->ConvertToWindowsSlash(intermediateDir);
  694. std::string outDir = this->Target->GetDirectory(config->c_str());
  695. this->ConvertToWindowsSlash(outDir);
  696. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  697. *this->BuildFileStream << outDir
  698. << "\\"
  699. << "</OutDir>\n";
  700. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  701. *this->BuildFileStream << intermediateDir
  702. << "</IntDir>\n";
  703. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  704. *this->BuildFileStream << cmSystemTools::GetFilenameWithoutExtension(
  705. targetNameFull.c_str())
  706. << "</TargetName>\n";
  707. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  708. *this->BuildFileStream << cmSystemTools::GetFilenameLastExtension(
  709. targetNameFull.c_str())
  710. << "</TargetExt>\n";
  711. this->OutputLinkIncremental(*config);
  712. }
  713. this->WriteString("</PropertyGroup>\n", 2);
  714. }
  715. void
  716. cmVisualStudio10TargetGenerator::
  717. OutputLinkIncremental(std::string const& configName)
  718. {
  719. std::string CONFIG = cmSystemTools::UpperCase(configName);
  720. // static libraries and things greater than modules do not need
  721. // to set this option
  722. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  723. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  724. {
  725. return;
  726. }
  727. const char* linkType = "SHARED";
  728. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  729. {
  730. linkType = "EXE";
  731. }
  732. // assume incremental linking
  733. const char* incremental = "true";
  734. const char* linkLanguage =
  735. this->Target->GetLinkerLanguage(configName.c_str());
  736. if(!linkLanguage)
  737. {
  738. cmSystemTools::Error
  739. ("CMake can not determine linker language for target:",
  740. this->Name.c_str());
  741. return;
  742. }
  743. std::string linkFlagVarBase = "CMAKE_";
  744. linkFlagVarBase += linkType;
  745. linkFlagVarBase += "_LINKER_FLAGS";
  746. std::string flags = this->
  747. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  748. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  749. flags += this->
  750. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  751. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  752. || strcmp(linkLanguage, "Fortran") == 0)
  753. {
  754. std::string baseFlagVar = "CMAKE_";
  755. baseFlagVar += linkLanguage;
  756. baseFlagVar += "_FLAGS";
  757. flags += this->
  758. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  759. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  760. flags +=
  761. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  762. }
  763. if(flags.find("INCREMENTAL:NO") != flags.npos)
  764. {
  765. incremental = "false";
  766. }
  767. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  768. *this->BuildFileStream << incremental
  769. << "</LinkIncremental>\n";
  770. }
  771. void
  772. cmVisualStudio10TargetGenerator::
  773. WriteClOptions(std::string const& configName,
  774. std::vector<std::string> const & includes)
  775. {
  776. // much of this was copied from here:
  777. // copied from cmLocalVisualStudio7Generator.cxx 805
  778. this->WriteString("<ClCompile>\n", 2);
  779. cmVisualStudioGeneratorOptions
  780. clOptions(this->LocalGenerator,
  781. 10, cmVisualStudioGeneratorOptions::Compiler,
  782. cmVS10CLFlagTable);
  783. std::string flags;
  784. // collect up flags for
  785. if(this->Target->GetType() < cmTarget::UTILITY)
  786. {
  787. const char* linkLanguage =
  788. this->Target->GetLinkerLanguage(configName.c_str());
  789. if(!linkLanguage)
  790. {
  791. cmSystemTools::Error
  792. ("CMake can not determine linker language for target:",
  793. this->Name.c_str());
  794. return;
  795. }
  796. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  797. || strcmp(linkLanguage, "Fortran") == 0)
  798. {
  799. std::string baseFlagVar = "CMAKE_";
  800. baseFlagVar += linkLanguage;
  801. baseFlagVar += "_FLAGS";
  802. flags = this->
  803. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  804. std::string flagVar = baseFlagVar + std::string("_") +
  805. cmSystemTools::UpperCase(configName);
  806. flags += " ";
  807. flags += this->
  808. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  809. }
  810. // set the correct language
  811. if(strcmp(linkLanguage, "C") == 0)
  812. {
  813. flags += " /TC ";
  814. }
  815. if(strcmp(linkLanguage, "CXX") == 0)
  816. {
  817. flags += " /TP ";
  818. }
  819. }
  820. // Add the target-specific flags.
  821. if(const char* targetFlags = this->Target->GetProperty("COMPILE_FLAGS"))
  822. {
  823. flags += " ";
  824. flags += targetFlags;
  825. }
  826. std::string configUpper = cmSystemTools::UpperCase(configName);
  827. std::string defPropName = "COMPILE_DEFINITIONS_";
  828. defPropName += configUpper;
  829. // Get preprocessor definitions for this directory.
  830. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  831. clOptions.FixExceptionHandlingDefault();
  832. clOptions.Parse(flags.c_str());
  833. clOptions.Parse(defineFlags.c_str());
  834. clOptions.AddDefines
  835. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  836. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  837. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  838. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  839. clOptions.SetVerboseMakefile(
  840. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  841. // Add a definition for the configuration name.
  842. std::string configDefine = "CMAKE_INTDIR=\"";
  843. configDefine += configName;
  844. configDefine += "\"";
  845. clOptions.AddDefine(configDefine);
  846. if(const char* exportMacro = this->Target->GetExportMacro())
  847. {
  848. clOptions.AddDefine(exportMacro);
  849. }
  850. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  851. this->OutputIncludes(includes);
  852. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  853. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  854. "\n");
  855. this->WriteString("<AssemblerListingLocation>", 3);
  856. *this->BuildFileStream << configName
  857. << "</AssemblerListingLocation>\n";
  858. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  859. this->WriteString("<ProgramDataBaseFileName>", 3);
  860. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  861. << "/"
  862. << this->Target->GetPDBName(configName.c_str())
  863. << "</ProgramDataBaseFileName>\n";
  864. this->WriteString("</ClCompile>\n", 2);
  865. }
  866. void cmVisualStudio10TargetGenerator::
  867. OutputIncludes(std::vector<std::string> const & includes)
  868. {
  869. this->WriteString("<AdditionalIncludeDirectories>", 3);
  870. for(std::vector<std::string>::const_iterator i = includes.begin();
  871. i != includes.end(); ++i)
  872. {
  873. *this->BuildFileStream << *i << ";";
  874. }
  875. this->WriteString("%(AdditionalIncludeDirectories)"
  876. "</AdditionalIncludeDirectories>\n", 0);
  877. }
  878. void cmVisualStudio10TargetGenerator::
  879. WriteRCOptions(std::string const& ,
  880. std::vector<std::string> const & includes)
  881. {
  882. this->WriteString("<ResourceCompile>\n", 2);
  883. this->OutputIncludes(includes);
  884. this->WriteString("</ResourceCompile>\n", 2);
  885. }
  886. void cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const&
  887. )
  888. {
  889. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  890. {
  891. return;
  892. }
  893. if(const char* libflags = this->Target
  894. ->GetProperty("STATIC_LIBRARY_FLAGS"))
  895. {
  896. this->WriteString("<Lib>\n", 2);
  897. cmVisualStudioGeneratorOptions
  898. libOptions(this->LocalGenerator,
  899. 10, cmVisualStudioGeneratorOptions::Compiler,
  900. cmVS10LibFlagTable, 0, this);
  901. libOptions.Parse(libflags);
  902. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  903. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  904. this->WriteString("</Lib>\n", 2);
  905. }
  906. }
  907. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  908. config)
  909. {
  910. // static libraries and things greater than modules do not need
  911. // to set this option
  912. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  913. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  914. {
  915. return;
  916. }
  917. const char* linkLanguage =
  918. this->Target->GetLinkerLanguage(config.c_str());
  919. if(!linkLanguage)
  920. {
  921. cmSystemTools::Error
  922. ("CMake can not determine linker language for target:",
  923. this->Name.c_str());
  924. return;
  925. }
  926. this->WriteString("<Link>\n", 2);
  927. std::string CONFIG = cmSystemTools::UpperCase(config);
  928. const char* linkType = "SHARED";
  929. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  930. {
  931. linkType = "MODULE";
  932. }
  933. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  934. {
  935. linkType = "EXE";
  936. }
  937. std::string stackVar = "CMAKE_";
  938. stackVar += linkLanguage;
  939. stackVar += "_STACK_SIZE";
  940. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  941. std::string flags;
  942. if(stackVal)
  943. {
  944. flags += " ";
  945. flags += stackVal;
  946. }
  947. // assume incremental linking
  948. std::string linkFlagVarBase = "CMAKE_";
  949. linkFlagVarBase += linkType;
  950. linkFlagVarBase += "_LINKER_FLAGS";
  951. flags += " ";
  952. flags += this->
  953. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  954. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  955. flags += " ";
  956. flags += this->
  957. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  958. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  959. if(targetLinkFlags)
  960. {
  961. flags += " ";
  962. flags += targetLinkFlags;
  963. }
  964. cmVisualStudioGeneratorOptions
  965. linkOptions(this->LocalGenerator,
  966. 10, cmVisualStudioGeneratorOptions::Compiler,
  967. cmVS10LinkFlagTable);
  968. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  969. {
  970. flags += " /SUBSYSTEM:WINDOWS";
  971. }
  972. else
  973. {
  974. flags += " /SUBSYSTEM:CONSOLE";
  975. }
  976. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  977. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  978. std::string standardLibsVar = "CMAKE_";
  979. standardLibsVar += linkLanguage;
  980. standardLibsVar += "_STANDARD_LIBRARIES";
  981. std::string
  982. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  983. // Remove trailing spaces from libs
  984. std::string::size_type pos = libs.size()-1;
  985. if(libs.size() != 0)
  986. {
  987. while(libs[pos] == ' ')
  988. {
  989. pos--;
  990. }
  991. }
  992. if(pos != libs.size()-1)
  993. {
  994. libs = libs.substr(0, pos+1);
  995. }
  996. // Replace spaces in libs with ;
  997. cmSystemTools::ReplaceString(libs, " ", ";");
  998. cmComputeLinkInformation* pcli =
  999. this->Target->GetLinkInformation(config.c_str());
  1000. if(!pcli)
  1001. {
  1002. cmSystemTools::Error
  1003. ("CMake can not compute cmComputeLinkInformation for target:",
  1004. this->Name.c_str());
  1005. return;
  1006. }
  1007. // add the libraries for the target to libs string
  1008. cmComputeLinkInformation& cli = *pcli;
  1009. this->AddLibraries(cli, libs);
  1010. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1011. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1012. const char* sep = "";
  1013. std::string linkDirs;
  1014. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1015. d != ldirs.end(); ++d)
  1016. {
  1017. // first just full path
  1018. linkDirs += sep;
  1019. linkDirs += *d;
  1020. linkDirs += sep;
  1021. // next path with configuration type Debug, Release, etc
  1022. linkDirs += *d;
  1023. linkDirs += "/$(Configuration)";
  1024. sep = ";";
  1025. }
  1026. linkDirs += "%(AdditionalLibraryDirectories)";
  1027. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1028. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1029. linkOptions.AddFlag("Version", "0.0");
  1030. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1031. {
  1032. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1033. }
  1034. else
  1035. {
  1036. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1037. }
  1038. std::string targetName;
  1039. std::string targetNameSO;
  1040. std::string targetNameFull;
  1041. std::string targetNameImport;
  1042. std::string targetNamePDB;
  1043. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1044. {
  1045. this->Target->GetExecutableNames(targetName, targetNameFull,
  1046. targetNameImport, targetNamePDB,
  1047. config.c_str());
  1048. }
  1049. else
  1050. {
  1051. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1052. targetNameImport, targetNamePDB,
  1053. config.c_str());
  1054. }
  1055. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1056. std::string dir = this->Target->GetDirectory(config.c_str(), true);
  1057. dir += "/";
  1058. imLib += "/";
  1059. imLib += targetNameImport;
  1060. std::string pdb = dir;
  1061. pdb += targetNamePDB;
  1062. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1063. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  1064. linkOptions.Parse(flags.c_str());
  1065. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1066. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1067. this->WriteString("</Link>\n", 2);
  1068. }
  1069. void cmVisualStudio10TargetGenerator::AddLibraries(
  1070. cmComputeLinkInformation& cli,
  1071. std::string& libstring)
  1072. {
  1073. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1074. ItemVector libs = cli.GetItems();
  1075. const char* sep = ";";
  1076. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1077. {
  1078. if(l->IsPath)
  1079. {
  1080. std::string path = this->LocalGenerator->
  1081. Convert(l->Value.c_str(),
  1082. cmLocalGenerator::START_OUTPUT,
  1083. cmLocalGenerator::UNCHANGED);
  1084. libstring += sep;
  1085. libstring += path;
  1086. }
  1087. else
  1088. {
  1089. libstring += sep;
  1090. libstring += l->Value;
  1091. }
  1092. }
  1093. }
  1094. void cmVisualStudio10TargetGenerator::
  1095. WriteMidlOptions(std::string const& /*config*/,
  1096. std::vector<std::string> const & includes)
  1097. {
  1098. this->WriteString("<Midl>\n", 2);
  1099. this->OutputIncludes(includes);
  1100. this->WriteString("</Midl>\n", 2);
  1101. }
  1102. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1103. {
  1104. std::vector<std::string> *configs =
  1105. static_cast<cmGlobalVisualStudio7Generator *>
  1106. (this->GlobalGenerator)->GetConfigurations();
  1107. std::vector<std::string> includes;
  1108. this->LocalGenerator->GetIncludeDirectories(includes);
  1109. for(std::vector<std::string>::iterator i = configs->begin();
  1110. i != configs->end(); ++i)
  1111. {
  1112. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1113. *this->BuildFileStream << "\n";
  1114. // output cl compile flags <ClCompile></ClCompile>
  1115. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  1116. {
  1117. this->WriteClOptions(*i, includes);
  1118. // output rc compile flags <ResourceCompile></ResourceCompile>
  1119. this->WriteRCOptions(*i, includes);
  1120. }
  1121. // output midl flags <Midl></Midl>
  1122. this->WriteMidlOptions(*i, includes);
  1123. // write events
  1124. this->WriteEvents(*i);
  1125. // output link flags <Link></Link>
  1126. this->WriteLinkOptions(*i);
  1127. // output lib flags <Lib></Lib>
  1128. this->WriteLibOptions(*i);
  1129. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1130. }
  1131. }
  1132. void
  1133. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1134. {
  1135. this->WriteEvent("PreLinkEvent",
  1136. this->Target->GetPreLinkCommands(), configName);
  1137. this->WriteEvent("PreBuildEvent",
  1138. this->Target->GetPreBuildCommands(), configName);
  1139. this->WriteEvent("PostBuildEvent",
  1140. this->Target->GetPostBuildCommands(), configName);
  1141. }
  1142. void cmVisualStudio10TargetGenerator::WriteEvent(
  1143. const char* name,
  1144. std::vector<cmCustomCommand> & commands,
  1145. std::string const& configName)
  1146. {
  1147. if(commands.size() == 0)
  1148. {
  1149. return;
  1150. }
  1151. this->WriteString("<", 2);
  1152. (*this->BuildFileStream ) << name << ">\n";
  1153. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1154. std::string script;
  1155. const char* pre = "";
  1156. std::string comment;
  1157. for(std::vector<cmCustomCommand>::iterator i = commands.begin();
  1158. i != commands.end(); ++i)
  1159. {
  1160. cmCustomCommand& command = *i;
  1161. comment += pre;
  1162. comment += lg->ConstructComment(command);
  1163. script += pre;
  1164. pre = "\n";
  1165. script +=
  1166. cmVS10EscapeXML(
  1167. lg->ConstructScript(command.GetCommandLines(),
  1168. command.GetWorkingDirectory(),
  1169. configName.c_str(),
  1170. command.GetEscapeOldStyle(),
  1171. command.GetEscapeAllowMakeVars())
  1172. );
  1173. }
  1174. this->WriteString("<Message>",3);
  1175. (*this->BuildFileStream ) << comment << "</Message>\n";
  1176. this->WriteString("<Command>", 3);
  1177. (*this->BuildFileStream ) << script;
  1178. (*this->BuildFileStream ) << "</Command>" << "\n";
  1179. this->WriteString("</", 2);
  1180. (*this->BuildFileStream ) << name << ">\n";
  1181. }
  1182. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1183. {
  1184. cmGlobalGenerator::TargetDependSet& depends
  1185. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1186. this->WriteString("<ItemGroup>\n", 1);
  1187. for( cmGlobalGenerator::TargetDependSet::const_iterator i = depends.begin();
  1188. i != depends.end(); ++i)
  1189. {
  1190. cmTarget* dt = *i;
  1191. this->WriteString("<ProjectReference Include=\"", 2);
  1192. cmMakefile* mf = dt->GetMakefile();
  1193. std::string name = dt->GetName();
  1194. std::string path;
  1195. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1196. if(p)
  1197. {
  1198. path = p;
  1199. }
  1200. else
  1201. {
  1202. path = mf->GetStartOutputDirectory();
  1203. path += "/";
  1204. path += dt->GetName();
  1205. path += ".vcxproj";
  1206. }
  1207. (*this->BuildFileStream) << path << "\">\n";
  1208. this->WriteString("<Project>", 3);
  1209. (*this->BuildFileStream)
  1210. << this->GlobalGenerator->GetGUID(name.c_str())
  1211. << "</Project>\n";
  1212. this->WriteString("</ProjectReference>\n", 2);
  1213. }
  1214. this->WriteString("</ItemGroup>\n", 1);
  1215. }