cmVisualStudio10TargetGenerator.cxx 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  416. // Write out group file
  417. std::string path = this->Makefile->GetStartOutputDirectory();
  418. path += "/";
  419. path += this->Name;
  420. path += ".vcxproj.filters";
  421. cmGeneratedFileStream fout(path.c_str());
  422. fout.SetCopyIfDifferent(true);
  423. char magic[] = {0xEF,0xBB, 0xBF};
  424. fout.write(magic, 3);
  425. cmGeneratedFileStream* save = this->BuildFileStream;
  426. this->BuildFileStream = & fout;
  427. this->WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  428. "<Project "
  429. "ToolsVersion=\"4.0\" "
  430. "xmlns=\"http://schemas.microsoft.com/"
  431. "developer/msbuild/2003\">\n",
  432. 0);
  433. this->WriteGroupSources("ClCompile", clCompile, sourceGroups);
  434. this->WriteGroupSources("ClInclude", headers, sourceGroups);
  435. this->WriteGroupSources("ResourceCompile", resource, sourceGroups);
  436. this->WriteGroupSources("CustomBuild", customBuild, sourceGroups);
  437. this->WriteString("<ItemGroup>\n", 1);
  438. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  439. g != groupsUsed.end(); ++g)
  440. {
  441. cmSourceGroup* sg = *g;
  442. const char* name = sg->GetFullName();
  443. if(strlen(name) != 0)
  444. {
  445. this->WriteString("<Filter Include=\"", 2);
  446. (*this->BuildFileStream) << name << "\">\n";
  447. std::string guidName = "SG_Filter_";
  448. guidName += name;
  449. this->GlobalGenerator->CreateGUID(guidName.c_str());
  450. this->WriteString("<UniqueIdentifier>", 3);
  451. std::string guid
  452. = this->GlobalGenerator->GetGUID(guidName.c_str());
  453. (*this->BuildFileStream)
  454. << "{"
  455. << guid << "}"
  456. << "</UniqueIdentifier>\n";
  457. this->WriteString("</Filter>\n", 2);
  458. }
  459. }
  460. this->WriteString("</ItemGroup>\n", 1);
  461. this->WriteGroupSources("None", none, sourceGroups);
  462. this->WriteString("</Project>\n", 0);
  463. // restore stream pointer
  464. this->BuildFileStream = save;
  465. if (fout.Close())
  466. {
  467. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  468. }
  469. }
  470. // Add to groupsUsed empty source groups that have non-empty children.
  471. void
  472. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  473. std::set<cmSourceGroup*>& groupsUsed,
  474. const std::vector<cmSourceGroup>& allGroups
  475. )
  476. {
  477. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  478. current != allGroups.end(); ++current)
  479. {
  480. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  481. if(children.empty())
  482. {
  483. continue; // the group is really empty
  484. }
  485. this->AddMissingSourceGroups(groupsUsed, children);
  486. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  487. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  488. {
  489. continue; // group has already been added to set
  490. }
  491. // check if it least one of the group's descendants is not empty
  492. // (at least one child must already have been added)
  493. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  494. while(child_it != children.end())
  495. {
  496. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  497. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  498. {
  499. break; // found a child that was already added => add current group too
  500. }
  501. child_it++;
  502. }
  503. if(child_it == children.end())
  504. {
  505. continue; // no descendants have source files => ignore this group
  506. }
  507. groupsUsed.insert(current_ptr);
  508. }
  509. }
  510. void
  511. cmVisualStudio10TargetGenerator::
  512. WriteGroupSources(const char* name,
  513. std::vector<cmSourceFile*> const& sources,
  514. std::vector<cmSourceGroup>& sourceGroups)
  515. {
  516. this->WriteString("<ItemGroup>\n", 1);
  517. for(std::vector<cmSourceFile*>::const_iterator s = sources.begin();
  518. s != sources.end(); ++s)
  519. {
  520. cmSourceFile* sf = *s;
  521. std::string const& source = sf->GetFullPath();
  522. cmSourceGroup& sourceGroup =
  523. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  524. const char* filter = sourceGroup.GetFullName();
  525. this->WriteString("<", 2);
  526. std::string path = source;
  527. path = cmSystemTools::RelativePath(
  528. this->Makefile->GetCurrentOutputDirectory(),
  529. source.c_str());
  530. this->ConvertToWindowsSlash(path);
  531. (*this->BuildFileStream) << name << " Include=\""
  532. << path;
  533. if(strlen(filter))
  534. {
  535. (*this->BuildFileStream) << "\">\n";
  536. this->WriteString("<Filter>", 3);
  537. (*this->BuildFileStream) << filter << "</Filter>\n";
  538. this->WriteString("</", 2);
  539. (*this->BuildFileStream) << name << ">\n";
  540. }
  541. else
  542. {
  543. (*this->BuildFileStream) << "\" />\n";
  544. }
  545. }
  546. this->WriteString("</ItemGroup>\n", 1);
  547. }
  548. void cmVisualStudio10TargetGenerator::WriteObjSources()
  549. {
  550. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  551. {
  552. return;
  553. }
  554. bool first = true;
  555. std::vector<cmSourceFile*>const & sources = this->Target->GetSourceFiles();
  556. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  557. source != sources.end(); ++source)
  558. {
  559. if((*source)->GetExtension() == "obj")
  560. {
  561. if(first)
  562. {
  563. this->WriteString("<ItemGroup>\n", 1);
  564. first = false;
  565. }
  566. this->WriteString("<None Include=\"", 2);
  567. (*this->BuildFileStream ) << (*source)->GetFullPath() << "\" />\n";
  568. }
  569. }
  570. if(!first)
  571. {
  572. this->WriteString("</ItemGroup>\n", 1);
  573. }
  574. }
  575. void cmVisualStudio10TargetGenerator::WriteCLSources()
  576. {
  577. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  578. {
  579. return;
  580. }
  581. this->WriteString("<ItemGroup>\n", 1);
  582. std::vector<cmSourceFile*>const& sources = this->Target->GetSourceFiles();
  583. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  584. source != sources.end(); ++source)
  585. {
  586. // if it is not a custom command then add it as a c/c++ file,
  587. // TODO: need to check for idl or rc
  588. if(!(*source)->GetCustomCommand())
  589. {
  590. bool header = (*source)->GetPropertyAsBool("HEADER_FILE_ONLY")
  591. || this->GlobalGenerator->IgnoreFile
  592. ((*source)->GetExtension().c_str());
  593. const char* lang = (*source)->GetLanguage();
  594. bool cl = lang && (strcmp(lang, "C") == 0 || strcmp(lang, "CXX") ==0);
  595. bool rc = lang && (strcmp(lang, "RC") == 0);
  596. std::string sourceFile = (*source)->GetFullPath();
  597. sourceFile = cmSystemTools::RelativePath(
  598. this->Makefile->GetCurrentOutputDirectory(),
  599. sourceFile.c_str());
  600. this->ConvertToWindowsSlash(sourceFile);
  601. // output the source file
  602. if(header)
  603. {
  604. this->WriteString("<ClInclude Include=\"", 2);
  605. }
  606. else if(cl)
  607. {
  608. this->WriteString("<ClCompile Include=\"", 2);
  609. }
  610. else if(rc)
  611. {
  612. this->WriteString("<ResourceCompile Include=\"", 2);
  613. }
  614. else
  615. {
  616. this->WriteString("<None Include=\"", 2);
  617. }
  618. (*this->BuildFileStream ) << sourceFile << "\"";
  619. // ouput any flags specific to this source file
  620. if(cl && this->OutputSourceSpecificFlags(*source))
  621. {
  622. // if the source file has specific flags the tag
  623. // is ended on a new line
  624. this->WriteString("</ClCompile>\n", 2);
  625. }
  626. else
  627. {
  628. (*this->BuildFileStream ) << " />\n";
  629. }
  630. }
  631. }
  632. this->WriteString("</ItemGroup>\n", 1);
  633. }
  634. void cmVisualStudio10TargetGenerator::ComputeObjectNames()
  635. {
  636. // We may be modifying the source groups temporarily, so make a copy.
  637. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  638. // get the classes from the source lists then add them to the groups
  639. std::vector<cmSourceFile*>const & classes = this->Target->GetSourceFiles();
  640. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  641. i != classes.end(); i++)
  642. {
  643. // Add the file to the list of sources.
  644. std::string source = (*i)->GetFullPath();
  645. if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
  646. {
  647. this->ModuleDefinitionFile = (*i)->GetFullPath();
  648. }
  649. cmSourceGroup& sourceGroup =
  650. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  651. sourceGroup.AssignSource(*i);
  652. }
  653. // Compute which sources need unique object computation.
  654. this->LocalGenerator->ComputeObjectNameRequirements(sourceGroups);
  655. }
  656. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  657. cmSourceFile* source)
  658. {
  659. cmSourceFile& sf = *source;
  660. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  661. // Compute the maximum length full path to the intermediate
  662. // files directory for any configuration. This is used to construct
  663. // object file names that do not produce paths that are too long.
  664. std::string dir_max;
  665. lg->ComputeMaxDirectoryLength(dir_max, *this->Target);
  666. std::string objectName;
  667. if(lg->NeedObjectName.find(&sf) != lg->NeedObjectName.end())
  668. {
  669. objectName = lg->GetObjectFileNameWithoutTarget(sf, dir_max);
  670. }
  671. std::string flags;
  672. std::string defines;
  673. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  674. {
  675. flags += cflags;
  676. }
  677. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  678. {
  679. defines += cdefs;
  680. }
  681. const char* lang =
  682. this->GlobalGenerator->GetLanguageFromExtension
  683. (sf.GetExtension().c_str());
  684. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  685. const char* linkLanguage = this->Target->GetLinkerLanguage();
  686. bool needForceLang = false;
  687. // source file does not match its extension language
  688. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  689. {
  690. needForceLang = true;
  691. lang = sourceLang;
  692. }
  693. // if the source file does not match the linker language
  694. // then force c or c++
  695. if(needForceLang || (linkLanguage && lang
  696. && strcmp(lang, linkLanguage) != 0))
  697. {
  698. if(strcmp(lang, "CXX") == 0)
  699. {
  700. // force a C++ file type
  701. flags += " /TP ";
  702. }
  703. else if(strcmp(lang, "C") == 0)
  704. {
  705. // force to c
  706. flags += " /TC ";
  707. }
  708. }
  709. bool hasFlags = false;
  710. // for the first time we need a new line if there is something
  711. // produced here.
  712. const char* firstString = ">\n";
  713. if(objectName.size())
  714. {
  715. (*this->BuildFileStream ) << firstString;
  716. firstString = "";
  717. hasFlags = true;
  718. this->WriteString("<ObjectFileName>", 3);
  719. (*this->BuildFileStream )
  720. << "$(Configuration)/" << objectName << "</ObjectFileName>\n";
  721. }
  722. std::vector<std::string> *configs =
  723. static_cast<cmGlobalVisualStudio7Generator *>
  724. (this->GlobalGenerator)->GetConfigurations();
  725. for( std::vector<std::string>::iterator config = configs->begin();
  726. config != configs->end(); ++config)
  727. {
  728. std::string configUpper = cmSystemTools::UpperCase(*config);
  729. std::string configDefines = defines;
  730. std::string defPropName = "COMPILE_DEFINITIONS_";
  731. defPropName += configUpper;
  732. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  733. {
  734. if(configDefines.size())
  735. {
  736. configDefines += ";";
  737. }
  738. configDefines += ccdefs;
  739. }
  740. // if we have flags or defines for this config then
  741. // use them
  742. if(flags.size() || configDefines.size())
  743. {
  744. (*this->BuildFileStream ) << firstString;
  745. firstString = ""; // only do firstString once
  746. hasFlags = true;
  747. cmVisualStudioGeneratorOptions
  748. clOptions(this->LocalGenerator,
  749. 10, cmVisualStudioGeneratorOptions::Compiler,
  750. cmVS10CLFlagTable, 0, this);
  751. clOptions.Parse(flags.c_str());
  752. clOptions.AddDefines(configDefines.c_str());
  753. clOptions.SetConfiguration((*config).c_str());
  754. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  755. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  756. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  757. " ", "\n");
  758. }
  759. }
  760. return hasFlags;
  761. }
  762. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  763. {
  764. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  765. {
  766. return;
  767. }
  768. this->WriteString("<PropertyGroup>\n", 2);
  769. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  770. "</_ProjectFileVersion>\n", 3);
  771. std::vector<std::string> *configs =
  772. static_cast<cmGlobalVisualStudio7Generator *>
  773. (this->GlobalGenerator)->GetConfigurations();
  774. for(std::vector<std::string>::iterator config = configs->begin();
  775. config != configs->end(); ++config)
  776. {
  777. std::string targetNameFull =
  778. this->Target->GetFullName(config->c_str());
  779. std::string intermediateDir = this->LocalGenerator->
  780. GetTargetDirectory(*this->Target);
  781. intermediateDir += "/";
  782. intermediateDir += *config;
  783. intermediateDir += "/";
  784. this->ConvertToWindowsSlash(intermediateDir);
  785. std::string outDir = this->Target->GetDirectory(config->c_str());
  786. this->ConvertToWindowsSlash(outDir);
  787. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  788. *this->BuildFileStream << outDir
  789. << "\\"
  790. << "</OutDir>\n";
  791. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  792. *this->BuildFileStream << intermediateDir
  793. << "</IntDir>\n";
  794. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  795. *this->BuildFileStream
  796. << cmSystemTools::GetFilenameWithoutLastExtension(
  797. targetNameFull.c_str())
  798. << "</TargetName>\n";
  799. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  800. *this->BuildFileStream << cmSystemTools::GetFilenameLastExtension(
  801. targetNameFull.c_str())
  802. << "</TargetExt>\n";
  803. this->OutputLinkIncremental(*config);
  804. }
  805. this->WriteString("</PropertyGroup>\n", 2);
  806. }
  807. void
  808. cmVisualStudio10TargetGenerator::
  809. OutputLinkIncremental(std::string const& configName)
  810. {
  811. std::string CONFIG = cmSystemTools::UpperCase(configName);
  812. // static libraries and things greater than modules do not need
  813. // to set this option
  814. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  815. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  816. {
  817. return;
  818. }
  819. const char* linkType = "SHARED";
  820. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  821. {
  822. linkType = "EXE";
  823. }
  824. // assume incremental linking
  825. const char* incremental = "true";
  826. const char* linkLanguage =
  827. this->Target->GetLinkerLanguage(configName.c_str());
  828. if(!linkLanguage)
  829. {
  830. cmSystemTools::Error
  831. ("CMake can not determine linker language for target:",
  832. this->Name.c_str());
  833. return;
  834. }
  835. std::string linkFlagVarBase = "CMAKE_";
  836. linkFlagVarBase += linkType;
  837. linkFlagVarBase += "_LINKER_FLAGS";
  838. std::string flags = this->
  839. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  840. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  841. flags += this->
  842. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  843. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  844. || strcmp(linkLanguage, "Fortran") == 0)
  845. {
  846. std::string baseFlagVar = "CMAKE_";
  847. baseFlagVar += linkLanguage;
  848. baseFlagVar += "_FLAGS";
  849. flags += this->
  850. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  851. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  852. flags +=
  853. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  854. }
  855. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  856. if(targetLinkFlags)
  857. {
  858. flags += " ";
  859. flags += targetLinkFlags;
  860. }
  861. std::string flagsProp = "LINK_FLAGS_";
  862. flagsProp += CONFIG;
  863. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  864. {
  865. flags += " ";
  866. flags += flagsConfig;
  867. }
  868. if(flags.find("INCREMENTAL:NO") != flags.npos)
  869. {
  870. incremental = "false";
  871. }
  872. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  873. *this->BuildFileStream << incremental
  874. << "</LinkIncremental>\n";
  875. }
  876. //----------------------------------------------------------------------------
  877. void cmVisualStudio10TargetGenerator::ComputeClOptions()
  878. {
  879. std::vector<std::string> const* configs =
  880. this->GlobalGenerator->GetConfigurations();
  881. for(std::vector<std::string>::const_iterator i = configs->begin();
  882. i != configs->end(); ++i)
  883. {
  884. this->ComputeClOptions(*i);
  885. }
  886. }
  887. //----------------------------------------------------------------------------
  888. void cmVisualStudio10TargetGenerator::ComputeClOptions(
  889. std::string const& configName)
  890. {
  891. // much of this was copied from here:
  892. // copied from cmLocalVisualStudio7Generator.cxx 805
  893. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  894. cmsys::auto_ptr<Options> pOptions(
  895. new Options(this->LocalGenerator, 10, Options::Compiler,
  896. cmVS10CLFlagTable));
  897. Options& clOptions = *pOptions;
  898. std::string flags;
  899. // collect up flags for
  900. if(this->Target->GetType() < cmTarget::UTILITY)
  901. {
  902. const char* linkLanguage =
  903. this->Target->GetLinkerLanguage(configName.c_str());
  904. if(!linkLanguage)
  905. {
  906. cmSystemTools::Error
  907. ("CMake can not determine linker language for target:",
  908. this->Name.c_str());
  909. return;
  910. }
  911. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  912. || strcmp(linkLanguage, "Fortran") == 0)
  913. {
  914. std::string baseFlagVar = "CMAKE_";
  915. baseFlagVar += linkLanguage;
  916. baseFlagVar += "_FLAGS";
  917. flags = this->
  918. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  919. std::string flagVar = baseFlagVar + std::string("_") +
  920. cmSystemTools::UpperCase(configName);
  921. flags += " ";
  922. flags += this->
  923. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  924. }
  925. // set the correct language
  926. if(strcmp(linkLanguage, "C") == 0)
  927. {
  928. flags += " /TC ";
  929. }
  930. if(strcmp(linkLanguage, "CXX") == 0)
  931. {
  932. flags += " /TP ";
  933. }
  934. }
  935. // Add the target-specific flags.
  936. if(const char* targetFlags = this->Target->GetProperty("COMPILE_FLAGS"))
  937. {
  938. flags += " ";
  939. flags += targetFlags;
  940. }
  941. std::string configUpper = cmSystemTools::UpperCase(configName);
  942. std::string defPropName = "COMPILE_DEFINITIONS_";
  943. defPropName += configUpper;
  944. // Get preprocessor definitions for this directory.
  945. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  946. clOptions.FixExceptionHandlingDefault();
  947. clOptions.Parse(flags.c_str());
  948. clOptions.Parse(defineFlags.c_str());
  949. clOptions.AddDefines
  950. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  951. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  952. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  953. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  954. clOptions.SetVerboseMakefile(
  955. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  956. // Add a definition for the configuration name.
  957. std::string configDefine = "CMAKE_INTDIR=\"";
  958. configDefine += configName;
  959. configDefine += "\"";
  960. clOptions.AddDefine(configDefine);
  961. if(const char* exportMacro = this->Target->GetExportMacro())
  962. {
  963. clOptions.AddDefine(exportMacro);
  964. }
  965. this->ClOptions[configName] = pOptions.release();
  966. }
  967. //----------------------------------------------------------------------------
  968. void cmVisualStudio10TargetGenerator::WriteClOptions(
  969. std::string const& configName,
  970. std::vector<std::string> const& includes)
  971. {
  972. Options& clOptions = *(this->ClOptions[configName]);
  973. this->WriteString("<ClCompile>\n", 2);
  974. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  975. this->OutputIncludes(includes);
  976. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  977. // If not in debug mode, write the DebugInformationFormat field
  978. // without value so PDBs don't get generated uselessly.
  979. if(!clOptions.IsDebug())
  980. {
  981. this->WriteString("<DebugInformationFormat>"
  982. "</DebugInformationFormat>\n", 3);
  983. }
  984. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  985. "\n");
  986. this->WriteString("<AssemblerListingLocation>", 3);
  987. *this->BuildFileStream << configName
  988. << "</AssemblerListingLocation>\n";
  989. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  990. this->WriteString("<ProgramDataBaseFileName>", 3);
  991. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  992. << "/"
  993. << this->Target->GetPDBName(configName.c_str())
  994. << "</ProgramDataBaseFileName>\n";
  995. this->WriteString("</ClCompile>\n", 2);
  996. }
  997. void cmVisualStudio10TargetGenerator::
  998. OutputIncludes(std::vector<std::string> const & includes)
  999. {
  1000. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1001. for(std::vector<std::string>::const_iterator i = includes.begin();
  1002. i != includes.end(); ++i)
  1003. {
  1004. *this->BuildFileStream << *i << ";";
  1005. }
  1006. this->WriteString("%(AdditionalIncludeDirectories)"
  1007. "</AdditionalIncludeDirectories>\n", 0);
  1008. }
  1009. void cmVisualStudio10TargetGenerator::
  1010. WriteRCOptions(std::string const& ,
  1011. std::vector<std::string> const & includes)
  1012. {
  1013. this->WriteString("<ResourceCompile>\n", 2);
  1014. this->OutputIncludes(includes);
  1015. this->WriteString("</ResourceCompile>\n", 2);
  1016. }
  1017. void
  1018. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1019. {
  1020. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1021. {
  1022. return;
  1023. }
  1024. const char* libflags = this->Target->GetProperty("STATIC_LIBRARY_FLAGS");
  1025. std::string flagsConfigVar = "STATIC_LIBRARY_FLAGS_";
  1026. flagsConfigVar += cmSystemTools::UpperCase(config);
  1027. const char* libflagsConfig =
  1028. this->Target->GetProperty(flagsConfigVar.c_str());
  1029. if(libflags || libflagsConfig)
  1030. {
  1031. this->WriteString("<Lib>\n", 2);
  1032. cmVisualStudioGeneratorOptions
  1033. libOptions(this->LocalGenerator, 10,
  1034. cmVisualStudioGeneratorOptions::Linker,
  1035. cmVS10LibFlagTable, 0, this);
  1036. libOptions.Parse(libflags?libflags:"");
  1037. libOptions.Parse(libflagsConfig?libflagsConfig:"");
  1038. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1039. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1040. this->WriteString("</Lib>\n", 2);
  1041. }
  1042. }
  1043. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  1044. config)
  1045. {
  1046. // static libraries and things greater than modules do not need
  1047. // to set this option
  1048. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1049. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1050. {
  1051. return;
  1052. }
  1053. const char* linkLanguage =
  1054. this->Target->GetLinkerLanguage(config.c_str());
  1055. if(!linkLanguage)
  1056. {
  1057. cmSystemTools::Error
  1058. ("CMake can not determine linker language for target:",
  1059. this->Name.c_str());
  1060. return;
  1061. }
  1062. this->WriteString("<Link>\n", 2);
  1063. std::string CONFIG = cmSystemTools::UpperCase(config);
  1064. const char* linkType = "SHARED";
  1065. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1066. {
  1067. linkType = "MODULE";
  1068. }
  1069. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1070. {
  1071. linkType = "EXE";
  1072. }
  1073. std::string stackVar = "CMAKE_";
  1074. stackVar += linkLanguage;
  1075. stackVar += "_STACK_SIZE";
  1076. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  1077. std::string flags;
  1078. if(stackVal)
  1079. {
  1080. flags += " ";
  1081. flags += stackVal;
  1082. }
  1083. // assume incremental linking
  1084. std::string linkFlagVarBase = "CMAKE_";
  1085. linkFlagVarBase += linkType;
  1086. linkFlagVarBase += "_LINKER_FLAGS";
  1087. flags += " ";
  1088. flags += this->
  1089. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1090. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1091. flags += " ";
  1092. flags += this->
  1093. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1094. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1095. if(targetLinkFlags)
  1096. {
  1097. flags += " ";
  1098. flags += targetLinkFlags;
  1099. }
  1100. std::string flagsProp = "LINK_FLAGS_";
  1101. flagsProp += CONFIG;
  1102. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1103. {
  1104. flags += " ";
  1105. flags += flagsConfig;
  1106. }
  1107. cmVisualStudioGeneratorOptions
  1108. linkOptions(this->LocalGenerator, 10,
  1109. cmVisualStudioGeneratorOptions::Linker,
  1110. cmVS10LinkFlagTable, 0, this);
  1111. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1112. {
  1113. flags += " /SUBSYSTEM:WINDOWS";
  1114. }
  1115. else
  1116. {
  1117. flags += " /SUBSYSTEM:CONSOLE";
  1118. }
  1119. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  1120. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  1121. std::string standardLibsVar = "CMAKE_";
  1122. standardLibsVar += linkLanguage;
  1123. standardLibsVar += "_STANDARD_LIBRARIES";
  1124. std::string
  1125. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1126. // Remove trailing spaces from libs
  1127. std::string::size_type pos = libs.size()-1;
  1128. if(libs.size() != 0)
  1129. {
  1130. while(libs[pos] == ' ')
  1131. {
  1132. pos--;
  1133. }
  1134. }
  1135. if(pos != libs.size()-1)
  1136. {
  1137. libs = libs.substr(0, pos+1);
  1138. }
  1139. // Replace spaces in libs with ;
  1140. cmSystemTools::ReplaceString(libs, " ", ";");
  1141. cmComputeLinkInformation* pcli =
  1142. this->Target->GetLinkInformation(config.c_str());
  1143. if(!pcli)
  1144. {
  1145. cmSystemTools::Error
  1146. ("CMake can not compute cmComputeLinkInformation for target:",
  1147. this->Name.c_str());
  1148. return;
  1149. }
  1150. // add the libraries for the target to libs string
  1151. cmComputeLinkInformation& cli = *pcli;
  1152. this->AddLibraries(cli, libs);
  1153. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1154. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1155. const char* sep = "";
  1156. std::string linkDirs;
  1157. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1158. d != ldirs.end(); ++d)
  1159. {
  1160. // first just full path
  1161. linkDirs += sep;
  1162. linkDirs += *d;
  1163. sep = ";";
  1164. linkDirs += sep;
  1165. // next path with configuration type Debug, Release, etc
  1166. linkDirs += *d;
  1167. linkDirs += "/$(Configuration)";
  1168. linkDirs += sep;
  1169. }
  1170. linkDirs += "%(AdditionalLibraryDirectories)";
  1171. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1172. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1173. linkOptions.AddFlag("Version", "0.0");
  1174. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1175. {
  1176. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1177. }
  1178. else
  1179. {
  1180. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1181. }
  1182. std::string targetName;
  1183. std::string targetNameSO;
  1184. std::string targetNameFull;
  1185. std::string targetNameImport;
  1186. std::string targetNamePDB;
  1187. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1188. {
  1189. this->Target->GetExecutableNames(targetName, targetNameFull,
  1190. targetNameImport, targetNamePDB,
  1191. config.c_str());
  1192. }
  1193. else
  1194. {
  1195. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1196. targetNameImport, targetNamePDB,
  1197. config.c_str());
  1198. }
  1199. std::string dir = this->Target->GetDirectory(config.c_str());
  1200. dir += "/";
  1201. std::string pdb = dir;
  1202. pdb += targetNamePDB;
  1203. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1204. imLib += "/";
  1205. imLib += targetNameImport;
  1206. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1207. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  1208. linkOptions.Parse(flags.c_str());
  1209. if(!this->ModuleDefinitionFile.empty())
  1210. {
  1211. linkOptions.AddFlag("ModuleDefinitionFile",
  1212. this->ModuleDefinitionFile.c_str());
  1213. }
  1214. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1215. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1216. this->WriteString("</Link>\n", 2);
  1217. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1218. {
  1219. this->WriteString("<ProjectReference>\n", 2);
  1220. this->WriteString(
  1221. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1222. this->WriteString("</ProjectReference>\n", 2);
  1223. }
  1224. }
  1225. void cmVisualStudio10TargetGenerator::AddLibraries(
  1226. cmComputeLinkInformation& cli,
  1227. std::string& libstring)
  1228. {
  1229. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1230. ItemVector libs = cli.GetItems();
  1231. const char* sep = ";";
  1232. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1233. {
  1234. if(l->IsPath)
  1235. {
  1236. std::string path = this->LocalGenerator->
  1237. Convert(l->Value.c_str(),
  1238. cmLocalGenerator::START_OUTPUT,
  1239. cmLocalGenerator::UNCHANGED);
  1240. this->ConvertToWindowsSlash(path);
  1241. libstring += sep;
  1242. libstring += path;
  1243. }
  1244. else
  1245. {
  1246. libstring += sep;
  1247. libstring += l->Value;
  1248. }
  1249. }
  1250. }
  1251. void cmVisualStudio10TargetGenerator::
  1252. WriteMidlOptions(std::string const& /*config*/,
  1253. std::vector<std::string> const & includes)
  1254. {
  1255. this->WriteString("<Midl>\n", 2);
  1256. this->OutputIncludes(includes);
  1257. this->WriteString("</Midl>\n", 2);
  1258. }
  1259. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1260. {
  1261. std::vector<std::string> *configs =
  1262. static_cast<cmGlobalVisualStudio7Generator *>
  1263. (this->GlobalGenerator)->GetConfigurations();
  1264. std::vector<std::string> includes;
  1265. this->LocalGenerator->GetIncludeDirectories(includes);
  1266. for(std::vector<std::string>::iterator i = configs->begin();
  1267. i != configs->end(); ++i)
  1268. {
  1269. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1270. *this->BuildFileStream << "\n";
  1271. // output cl compile flags <ClCompile></ClCompile>
  1272. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  1273. {
  1274. this->WriteClOptions(*i, includes);
  1275. // output rc compile flags <ResourceCompile></ResourceCompile>
  1276. this->WriteRCOptions(*i, includes);
  1277. }
  1278. // output midl flags <Midl></Midl>
  1279. this->WriteMidlOptions(*i, includes);
  1280. // write events
  1281. this->WriteEvents(*i);
  1282. // output link flags <Link></Link>
  1283. this->WriteLinkOptions(*i);
  1284. // output lib flags <Lib></Lib>
  1285. this->WriteLibOptions(*i);
  1286. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1287. }
  1288. }
  1289. void
  1290. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1291. {
  1292. this->WriteEvent("PreLinkEvent",
  1293. this->Target->GetPreLinkCommands(), configName);
  1294. this->WriteEvent("PreBuildEvent",
  1295. this->Target->GetPreBuildCommands(), configName);
  1296. this->WriteEvent("PostBuildEvent",
  1297. this->Target->GetPostBuildCommands(), configName);
  1298. }
  1299. void cmVisualStudio10TargetGenerator::WriteEvent(
  1300. const char* name,
  1301. std::vector<cmCustomCommand> & commands,
  1302. std::string const& configName)
  1303. {
  1304. if(commands.size() == 0)
  1305. {
  1306. return;
  1307. }
  1308. this->WriteString("<", 2);
  1309. (*this->BuildFileStream ) << name << ">\n";
  1310. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1311. std::string script;
  1312. const char* pre = "";
  1313. std::string comment;
  1314. for(std::vector<cmCustomCommand>::iterator i = commands.begin();
  1315. i != commands.end(); ++i)
  1316. {
  1317. cmCustomCommand& command = *i;
  1318. comment += pre;
  1319. comment += lg->ConstructComment(command);
  1320. script += pre;
  1321. pre = "\n";
  1322. script +=
  1323. cmVS10EscapeXML(
  1324. lg->ConstructScript(command.GetCommandLines(),
  1325. command.GetWorkingDirectory(),
  1326. configName.c_str(),
  1327. command.GetEscapeOldStyle(),
  1328. command.GetEscapeAllowMakeVars())
  1329. );
  1330. }
  1331. this->WriteString("<Message>",3);
  1332. (*this->BuildFileStream ) << comment << "</Message>\n";
  1333. this->WriteString("<Command>", 3);
  1334. (*this->BuildFileStream ) << script;
  1335. (*this->BuildFileStream ) << "</Command>" << "\n";
  1336. this->WriteString("</", 2);
  1337. (*this->BuildFileStream ) << name << ">\n";
  1338. }
  1339. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1340. {
  1341. cmGlobalGenerator::TargetDependSet const& depends
  1342. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1343. this->WriteString("<ItemGroup>\n", 1);
  1344. for( cmGlobalGenerator::TargetDependSet::const_iterator i = depends.begin();
  1345. i != depends.end(); ++i)
  1346. {
  1347. cmTarget* dt = *i;
  1348. this->WriteString("<ProjectReference Include=\"", 2);
  1349. cmMakefile* mf = dt->GetMakefile();
  1350. std::string name = dt->GetName();
  1351. std::string path;
  1352. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1353. if(p)
  1354. {
  1355. path = p;
  1356. }
  1357. else
  1358. {
  1359. path = mf->GetStartOutputDirectory();
  1360. path += "/";
  1361. path += dt->GetName();
  1362. path += ".vcxproj";
  1363. }
  1364. (*this->BuildFileStream) << path << "\">\n";
  1365. this->WriteString("<Project>", 3);
  1366. (*this->BuildFileStream)
  1367. << this->GlobalGenerator->GetGUID(name.c_str())
  1368. << "</Project>\n";
  1369. this->WriteString("</ProjectReference>\n", 2);
  1370. }
  1371. this->WriteString("</ItemGroup>\n", 1);
  1372. }