cmVisualStudio10TargetGenerator.cxx 42 KB

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