cmVisualStudio10TargetGenerator.cxx 45 KB

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