cmVisualStudio10TargetGenerator.cxx 47 KB

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