cmVisualStudio10TargetGenerator.cxx 49 KB

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