cmVisualStudio10TargetGenerator.cxx 49 KB

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