cmVisualStudio10TargetGenerator.cxx 41 KB

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