cmVisualStudio10TargetGenerator.cxx 48 KB

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