cmVisualStudio10TargetGenerator.cxx 49 KB

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