cmVisualStudio10TargetGenerator.cxx 48 KB

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