cmVisualStudio10TargetGenerator.cxx 50 KB

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