cmVisualStudio10TargetGenerator.cxx 41 KB

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