cmVisualStudio10TargetGenerator.cxx 42 KB

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