cmVisualStudio10TargetGenerator.cxx 49 KB

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