cmVisualStudio10TargetGenerator.cxx 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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 "cmGeneratorTarget.h"
  13. #include "cmTarget.h"
  14. #include "cmComputeLinkInformation.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include "cmVisualStudioGeneratorOptions.h"
  19. #include "cmLocalVisualStudio7Generator.h"
  20. #include "cmCustomCommandGenerator.h"
  21. #include "cmVS10CLFlagTable.h"
  22. #include "cmVS10LinkFlagTable.h"
  23. #include "cmVS10LibFlagTable.h"
  24. #include "cmVS11CLFlagTable.h"
  25. #include "cmVS11LinkFlagTable.h"
  26. #include "cmVS11LibFlagTable.h"
  27. #include "cmVS12CLFlagTable.h"
  28. #include "cmVS12LinkFlagTable.h"
  29. #include "cmVS12LibFlagTable.h"
  30. #include "cmVS14CLFlagTable.h"
  31. #include "cmVS14LinkFlagTable.h"
  32. #include "cmVS14LibFlagTable.h"
  33. #include <cmsys/auto_ptr.hxx>
  34. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetClFlagTable() const
  35. {
  36. cmLocalVisualStudioGenerator::VSVersion
  37. v = this->LocalGenerator->GetVersion();
  38. if(v >= cmLocalVisualStudioGenerator::VS14)
  39. { return cmVS14CLFlagTable; }
  40. else if(v >= cmLocalVisualStudioGenerator::VS12)
  41. { return cmVS12CLFlagTable; }
  42. else if(v == cmLocalVisualStudioGenerator::VS11)
  43. { return cmVS11CLFlagTable; }
  44. else
  45. { return cmVS10CLFlagTable; }
  46. }
  47. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetLibFlagTable() const
  48. {
  49. cmLocalVisualStudioGenerator::VSVersion
  50. v = this->LocalGenerator->GetVersion();
  51. if(v >= cmLocalVisualStudioGenerator::VS14)
  52. { return cmVS14LibFlagTable; }
  53. else if(v >= cmLocalVisualStudioGenerator::VS12)
  54. { return cmVS12LibFlagTable; }
  55. else if(v == cmLocalVisualStudioGenerator::VS11)
  56. { return cmVS11LibFlagTable; }
  57. else
  58. { return cmVS10LibFlagTable; }
  59. }
  60. cmIDEFlagTable const* cmVisualStudio10TargetGenerator::GetLinkFlagTable() const
  61. {
  62. cmLocalVisualStudioGenerator::VSVersion
  63. v = this->LocalGenerator->GetVersion();
  64. if(v >= cmLocalVisualStudioGenerator::VS14)
  65. { return cmVS14LinkFlagTable; }
  66. else if(v >= cmLocalVisualStudioGenerator::VS12)
  67. { return cmVS12LinkFlagTable; }
  68. else if(v == cmLocalVisualStudioGenerator::VS11)
  69. { return cmVS11LinkFlagTable; }
  70. else
  71. { return cmVS10LinkFlagTable; }
  72. }
  73. static std::string cmVS10EscapeXML(std::string arg)
  74. {
  75. cmSystemTools::ReplaceString(arg, "&", "&amp;");
  76. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  77. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  78. return arg;
  79. }
  80. static std::string cmVS10EscapeComment(std::string comment)
  81. {
  82. // MSBuild takes the CDATA of a <Message></Message> element and just
  83. // does "echo $CDATA" with no escapes. We must encode the string.
  84. // http://technet.microsoft.com/en-us/library/cc772462%28WS.10%29.aspx
  85. std::string echoable;
  86. for(std::string::iterator c = comment.begin(); c != comment.end(); ++c)
  87. {
  88. switch (*c)
  89. {
  90. case '\r': break;
  91. case '\n': echoable += '\t'; break;
  92. case '"': /* no break */
  93. case '|': /* no break */
  94. case '&': /* no break */
  95. case '<': /* no break */
  96. case '>': /* no break */
  97. case '^': echoable += '^'; /* no break */
  98. default: echoable += *c; break;
  99. }
  100. }
  101. return echoable;
  102. }
  103. cmVisualStudio10TargetGenerator::
  104. cmVisualStudio10TargetGenerator(cmTarget* target,
  105. cmGlobalVisualStudio10Generator* gg)
  106. {
  107. this->GlobalGenerator = gg;
  108. this->Target = target;
  109. this->GeneratorTarget = gg->GetGeneratorTarget(target);
  110. this->Makefile = target->GetMakefile();
  111. this->LocalGenerator =
  112. (cmLocalVisualStudio7Generator*)
  113. this->Makefile->GetLocalGenerator();
  114. this->Name = this->Target->GetName();
  115. this->GlobalGenerator->CreateGUID(this->Name.c_str());
  116. this->GUID = this->GlobalGenerator->GetGUID(this->Name.c_str());
  117. this->Platform = gg->GetPlatformName();
  118. this->BuildFileStream = 0;
  119. }
  120. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
  121. {
  122. for(OptionsMap::iterator i = this->ClOptions.begin();
  123. i != this->ClOptions.end(); ++i)
  124. {
  125. delete i->second;
  126. }
  127. for(OptionsMap::iterator i = this->LinkOptions.begin();
  128. i != this->LinkOptions.end(); ++i)
  129. {
  130. delete i->second;
  131. }
  132. if(!this->BuildFileStream)
  133. {
  134. return;
  135. }
  136. if (this->BuildFileStream->Close())
  137. {
  138. this->GlobalGenerator
  139. ->FileReplacedDuringGenerate(this->PathToVcxproj);
  140. }
  141. delete this->BuildFileStream;
  142. }
  143. void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
  144. const char* tag,
  145. const std::string& config,
  146. int indentLevel,
  147. const char* attribute,
  148. const char* end,
  149. std::ostream* stream)
  150. {
  151. if(!stream)
  152. {
  153. stream = this->BuildFileStream;
  154. }
  155. stream->fill(' ');
  156. stream->width(indentLevel*2 );
  157. (*stream ) << "";
  158. (*stream ) << "<" << tag
  159. << " Condition=\"'$(Configuration)|$(Platform)'=='";
  160. (*stream ) << config << "|" << this->Platform << "'\"";
  161. if(attribute)
  162. {
  163. (*stream ) << attribute;
  164. }
  165. // close the tag
  166. (*stream ) << ">";
  167. if(end)
  168. {
  169. (*stream ) << end;
  170. }
  171. }
  172. void cmVisualStudio10TargetGenerator::WriteString(const char* line,
  173. int indentLevel)
  174. {
  175. this->BuildFileStream->fill(' ');
  176. this->BuildFileStream->width(indentLevel*2 );
  177. // write an empty string to get the fill level indent to print
  178. (*this->BuildFileStream ) << "";
  179. (*this->BuildFileStream ) << line;
  180. }
  181. #define VS10_USER_PROPS "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props"
  182. void cmVisualStudio10TargetGenerator::Generate()
  183. {
  184. // do not generate external ms projects
  185. if(this->Target->GetType() == cmTarget::INTERFACE_LIBRARY
  186. || this->Target->GetProperty("EXTERNAL_MSPROJECT"))
  187. {
  188. return;
  189. }
  190. // Tell the global generator the name of the project file
  191. this->Target->SetProperty("GENERATOR_FILE_NAME",this->Name.c_str());
  192. this->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  193. ".vcxproj");
  194. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  195. {
  196. if(!this->ComputeClOptions())
  197. {
  198. return;
  199. }
  200. if(!this->ComputeLinkOptions())
  201. {
  202. return;
  203. }
  204. }
  205. cmMakefile* mf = this->Target->GetMakefile();
  206. std::string path = mf->GetStartOutputDirectory();
  207. path += "/";
  208. path += this->Name;
  209. path += ".vcxproj";
  210. this->BuildFileStream =
  211. new cmGeneratedFileStream(path.c_str());
  212. this->PathToVcxproj = path;
  213. this->BuildFileStream->SetCopyIfDifferent(true);
  214. // Write the encoding header into the file
  215. char magic[] = {0xEF,0xBB, 0xBF};
  216. this->BuildFileStream->write(magic, 3);
  217. //get the tools version to use
  218. const std::string toolsVer(this->GlobalGenerator->GetToolsVersion());
  219. std::string project_defaults=
  220. "<?xml version=\"1.0\" encoding=\"" +
  221. this->GlobalGenerator->Encoding() + "\"?>\n";
  222. project_defaults.append("<Project DefaultTargets=\"Build\" ToolsVersion=\"");
  223. project_defaults.append(toolsVer +"\" ");
  224. project_defaults.append(
  225. "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  226. this->WriteString(project_defaults.c_str(),0);
  227. this->WriteProjectConfigurations();
  228. this->WriteString("<PropertyGroup Label=\"Globals\">\n", 1);
  229. this->WriteString("<ProjectGUID>", 2);
  230. (*this->BuildFileStream) << "{" << this->GUID << "}</ProjectGUID>\n";
  231. const char* vsProjectTypes =
  232. this->Target->GetProperty("VS_GLOBAL_PROJECT_TYPES");
  233. if(vsProjectTypes)
  234. {
  235. this->WriteString("<ProjectTypes>", 2);
  236. (*this->BuildFileStream) << cmVS10EscapeXML(vsProjectTypes) <<
  237. "</ProjectTypes>\n";
  238. }
  239. const char* vsProjectName = this->Target->GetProperty("VS_SCC_PROJECTNAME");
  240. const char* vsLocalPath = this->Target->GetProperty("VS_SCC_LOCALPATH");
  241. const char* vsProvider = this->Target->GetProperty("VS_SCC_PROVIDER");
  242. if( vsProjectName && vsLocalPath && vsProvider )
  243. {
  244. this->WriteString("<SccProjectName>", 2);
  245. (*this->BuildFileStream) << cmVS10EscapeXML(vsProjectName) <<
  246. "</SccProjectName>\n";
  247. this->WriteString("<SccLocalPath>", 2);
  248. (*this->BuildFileStream) << cmVS10EscapeXML(vsLocalPath) <<
  249. "</SccLocalPath>\n";
  250. this->WriteString("<SccProvider>", 2);
  251. (*this->BuildFileStream) << cmVS10EscapeXML(vsProvider) <<
  252. "</SccProvider>\n";
  253. const char* vsAuxPath = this->Target->GetProperty("VS_SCC_AUXPATH");
  254. if( vsAuxPath )
  255. {
  256. this->WriteString("<SccAuxPath>", 2);
  257. (*this->BuildFileStream) << cmVS10EscapeXML(vsAuxPath) <<
  258. "</SccAuxPath>\n";
  259. }
  260. }
  261. const char* vsGlobalKeyword =
  262. this->Target->GetProperty("VS_GLOBAL_KEYWORD");
  263. if(!vsGlobalKeyword)
  264. {
  265. this->WriteString("<Keyword>Win32Proj</Keyword>\n", 2);
  266. }
  267. else
  268. {
  269. this->WriteString("<Keyword>", 2);
  270. (*this->BuildFileStream) << cmVS10EscapeXML(vsGlobalKeyword) <<
  271. "</Keyword>\n";
  272. }
  273. const char* vsGlobalRootNamespace =
  274. this->Target->GetProperty("VS_GLOBAL_ROOTNAMESPACE");
  275. if(vsGlobalRootNamespace)
  276. {
  277. this->WriteString("<RootNamespace>", 2);
  278. (*this->BuildFileStream) << cmVS10EscapeXML(vsGlobalRootNamespace) <<
  279. "</RootNamespace>\n";
  280. }
  281. this->WriteString("<Platform>", 2);
  282. (*this->BuildFileStream) << this->Platform << "</Platform>\n";
  283. const char* projLabel = this->Target->GetProperty("PROJECT_LABEL");
  284. if(!projLabel)
  285. {
  286. projLabel = this->Name.c_str();
  287. }
  288. this->WriteString("<ProjectName>", 2);
  289. (*this->BuildFileStream) << projLabel << "</ProjectName>\n";
  290. if(const char* targetFrameworkVersion = this->Target->GetProperty(
  291. "VS_DOTNET_TARGET_FRAMEWORK_VERSION"))
  292. {
  293. this->WriteString("<TargetFrameworkVersion>", 2);
  294. (*this->BuildFileStream) << targetFrameworkVersion
  295. << "</TargetFrameworkVersion>\n";
  296. }
  297. this->WriteString("</PropertyGroup>\n", 1);
  298. this->WriteString("<Import Project="
  299. "\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n",
  300. 1);
  301. this->WriteProjectConfigurationValues();
  302. this->WriteString(
  303. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n", 1);
  304. this->WriteString("<ImportGroup Label=\"ExtensionSettings\">\n", 1);
  305. if (this->GlobalGenerator->IsMasmEnabled())
  306. {
  307. this->WriteString("<Import Project=\"$(VCTargetsPath)\\"
  308. "BuildCustomizations\\masm.props\" />\n", 2);
  309. }
  310. this->WriteString("</ImportGroup>\n", 1);
  311. this->WriteString("<ImportGroup Label=\"PropertySheets\">\n", 1);
  312. this->WriteString("<Import Project=\"" VS10_USER_PROPS "\""
  313. " Condition=\"exists('" VS10_USER_PROPS "')\""
  314. " Label=\"LocalAppDataPlatform\" />", 2);
  315. this->WriteString("</ImportGroup>\n", 1);
  316. this->WriteString("<PropertyGroup Label=\"UserMacros\" />\n", 1);
  317. this->WritePathAndIncrementalLinkOptions();
  318. this->WriteItemDefinitionGroups();
  319. this->WriteCustomCommands();
  320. this->WriteAllSources();
  321. this->WriteDotNetReferences();
  322. this->WriteEmbeddedResourceGroup();
  323. this->WriteWinRTReferences();
  324. this->WriteProjectReferences();
  325. this->WriteString(
  326. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\""
  327. " />\n", 1);
  328. this->WriteString("<ImportGroup Label=\"ExtensionTargets\">\n", 1);
  329. if (this->GlobalGenerator->IsMasmEnabled())
  330. {
  331. this->WriteString("<Import Project=\"$(VCTargetsPath)\\"
  332. "BuildCustomizations\\masm.targets\" />\n", 2);
  333. }
  334. this->WriteString("</ImportGroup>\n", 1);
  335. this->WriteString("</Project>", 0);
  336. // The groups are stored in a separate file for VS 10
  337. this->WriteGroups();
  338. }
  339. void cmVisualStudio10TargetGenerator::WriteDotNetReferences()
  340. {
  341. std::vector<std::string> references;
  342. if(const char* vsDotNetReferences =
  343. this->Target->GetProperty("VS_DOTNET_REFERENCES"))
  344. {
  345. cmSystemTools::ExpandListArgument(vsDotNetReferences, references);
  346. }
  347. if(!references.empty())
  348. {
  349. this->WriteString("<ItemGroup>\n", 1);
  350. for(std::vector<std::string>::iterator ri = references.begin();
  351. ri != references.end(); ++ri)
  352. {
  353. this->WriteString("<Reference Include=\"", 2);
  354. (*this->BuildFileStream) << cmVS10EscapeXML(*ri) << "\">\n";
  355. this->WriteString("<CopyLocalSatelliteAssemblies>true"
  356. "</CopyLocalSatelliteAssemblies>\n", 3);
  357. this->WriteString("<ReferenceOutputAssembly>true"
  358. "</ReferenceOutputAssembly>\n", 3);
  359. this->WriteString("</Reference>\n", 2);
  360. }
  361. this->WriteString("</ItemGroup>\n", 1);
  362. }
  363. }
  364. void cmVisualStudio10TargetGenerator::WriteEmbeddedResourceGroup()
  365. {
  366. std::vector<cmSourceFile const*> resxObjs;
  367. this->GeneratorTarget->GetResxSources(resxObjs, "");
  368. if(!resxObjs.empty())
  369. {
  370. this->WriteString("<ItemGroup>\n", 1);
  371. for(std::vector<cmSourceFile const*>::const_iterator oi = resxObjs.begin();
  372. oi != resxObjs.end(); ++oi)
  373. {
  374. std::string obj = (*oi)->GetFullPath();
  375. this->WriteString("<EmbeddedResource Include=\"", 2);
  376. this->ConvertToWindowsSlash(obj);
  377. (*this->BuildFileStream ) << obj << "\">\n";
  378. this->WriteString("<DependentUpon>", 3);
  379. std::string hFileName = obj.substr(0, obj.find_last_of(".")) + ".h";
  380. (*this->BuildFileStream ) << hFileName;
  381. this->WriteString("</DependentUpon>\n", 3);
  382. std::vector<std::string> const * configs =
  383. this->GlobalGenerator->GetConfigurations();
  384. for(std::vector<std::string>::const_iterator i = configs->begin();
  385. i != configs->end(); ++i)
  386. {
  387. this->WritePlatformConfigTag("LogicalName", i->c_str(), 3);
  388. if(this->Target->GetProperty("VS_GLOBAL_ROOTNAMESPACE"))
  389. {
  390. (*this->BuildFileStream ) << "$(RootNamespace).";
  391. }
  392. (*this->BuildFileStream ) << "%(Filename)";
  393. (*this->BuildFileStream ) << ".resources";
  394. (*this->BuildFileStream ) << "</LogicalName>\n";
  395. }
  396. this->WriteString("</EmbeddedResource>\n", 2);
  397. }
  398. this->WriteString("</ItemGroup>\n", 1);
  399. }
  400. }
  401. void cmVisualStudio10TargetGenerator::WriteWinRTReferences()
  402. {
  403. std::vector<std::string> references;
  404. if(const char* vsWinRTReferences =
  405. this->Target->GetProperty("VS_WINRT_REFERENCES"))
  406. {
  407. cmSystemTools::ExpandListArgument(vsWinRTReferences, references);
  408. }
  409. if(!references.empty())
  410. {
  411. this->WriteString("<ItemGroup>\n", 1);
  412. for(std::vector<std::string>::iterator ri = references.begin();
  413. ri != references.end(); ++ri)
  414. {
  415. this->WriteString("<Reference Include=\"", 2);
  416. (*this->BuildFileStream) << cmVS10EscapeXML(*ri) << "\">\n";
  417. this->WriteString("<IsWinMDFile>true</IsWinMDFile>\n", 3);
  418. this->WriteString("</Reference>\n", 2);
  419. }
  420. this->WriteString("</ItemGroup>\n", 1);
  421. }
  422. }
  423. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  424. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations()
  425. {
  426. this->WriteString("<ItemGroup Label=\"ProjectConfigurations\">\n", 1);
  427. std::vector<std::string> *configs =
  428. static_cast<cmGlobalVisualStudio7Generator *>
  429. (this->GlobalGenerator)->GetConfigurations();
  430. for(std::vector<std::string>::iterator i = configs->begin();
  431. i != configs->end(); ++i)
  432. {
  433. this->WriteString("<ProjectConfiguration Include=\"", 2);
  434. (*this->BuildFileStream ) << *i << "|" << this->Platform << "\">\n";
  435. this->WriteString("<Configuration>", 3);
  436. (*this->BuildFileStream ) << *i << "</Configuration>\n";
  437. this->WriteString("<Platform>", 3);
  438. (*this->BuildFileStream) << this->Platform << "</Platform>\n";
  439. this->WriteString("</ProjectConfiguration>\n", 2);
  440. }
  441. this->WriteString("</ItemGroup>\n", 1);
  442. }
  443. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues()
  444. {
  445. std::vector<std::string> *configs =
  446. static_cast<cmGlobalVisualStudio7Generator *>
  447. (this->GlobalGenerator)->GetConfigurations();
  448. for(std::vector<std::string>::iterator i = configs->begin();
  449. i != configs->end(); ++i)
  450. {
  451. this->WritePlatformConfigTag("PropertyGroup",
  452. i->c_str(),
  453. 1, " Label=\"Configuration\"", "\n");
  454. std::string configType = "<ConfigurationType>";
  455. switch(this->Target->GetType())
  456. {
  457. case cmTarget::SHARED_LIBRARY:
  458. case cmTarget::MODULE_LIBRARY:
  459. configType += "DynamicLibrary";
  460. break;
  461. case cmTarget::OBJECT_LIBRARY:
  462. case cmTarget::STATIC_LIBRARY:
  463. configType += "StaticLibrary";
  464. break;
  465. case cmTarget::EXECUTABLE:
  466. configType += "Application";
  467. break;
  468. case cmTarget::UTILITY:
  469. configType += "Utility";
  470. break;
  471. case cmTarget::GLOBAL_TARGET:
  472. case cmTarget::UNKNOWN_LIBRARY:
  473. case cmTarget::INTERFACE_LIBRARY:
  474. break;
  475. }
  476. configType += "</ConfigurationType>\n";
  477. this->WriteString(configType.c_str(), 2);
  478. this->WriteMSToolConfigurationValues(*i);
  479. this->WriteString("</PropertyGroup>\n", 1);
  480. }
  481. }
  482. //----------------------------------------------------------------------------
  483. void cmVisualStudio10TargetGenerator
  484. ::WriteMSToolConfigurationValues(std::string const& config)
  485. {
  486. cmGlobalVisualStudio10Generator* gg =
  487. static_cast<cmGlobalVisualStudio10Generator*>(this->GlobalGenerator);
  488. const char* mfcFlag =
  489. this->Target->GetMakefile()->GetDefinition("CMAKE_MFC_FLAG");
  490. std::string mfcFlagValue = mfcFlag ? mfcFlag : "0";
  491. std::string useOfMfcValue = "false";
  492. if(mfcFlagValue == "1")
  493. {
  494. useOfMfcValue = "Static";
  495. }
  496. else if(mfcFlagValue == "2")
  497. {
  498. useOfMfcValue = "Dynamic";
  499. }
  500. std::string mfcLine = "<UseOfMfc>";
  501. mfcLine += useOfMfcValue + "</UseOfMfc>\n";
  502. this->WriteString(mfcLine.c_str(), 2);
  503. if((this->Target->GetType() <= cmTarget::OBJECT_LIBRARY &&
  504. this->ClOptions[config]->UsingUnicode()) ||
  505. this->Target->GetPropertyAsBool("VS_WINRT_EXTENSIONS"))
  506. {
  507. this->WriteString("<CharacterSet>Unicode</CharacterSet>\n", 2);
  508. }
  509. else if (this->Target->GetType() <= cmTarget::MODULE_LIBRARY &&
  510. this->ClOptions[config]->UsingSBCS())
  511. {
  512. this->WriteString("<CharacterSet>NotSet</CharacterSet>\n", 2);
  513. }
  514. else
  515. {
  516. this->WriteString("<CharacterSet>MultiByte</CharacterSet>\n", 2);
  517. }
  518. if(const char* toolset = gg->GetPlatformToolset())
  519. {
  520. std::string pts = "<PlatformToolset>";
  521. pts += toolset;
  522. pts += "</PlatformToolset>\n";
  523. this->WriteString(pts.c_str(), 2);
  524. }
  525. if(this->Target->GetPropertyAsBool("VS_WINRT_EXTENSIONS"))
  526. {
  527. this->WriteString("<WindowsAppContainer>true"
  528. "</WindowsAppContainer>\n", 2);
  529. }
  530. }
  531. void cmVisualStudio10TargetGenerator::WriteCustomCommands()
  532. {
  533. this->SourcesVisited.clear();
  534. std::vector<cmSourceFile const*> customCommands;
  535. this->GeneratorTarget->GetCustomCommands(customCommands, "");
  536. for(std::vector<cmSourceFile const*>::const_iterator
  537. si = customCommands.begin();
  538. si != customCommands.end(); ++si)
  539. {
  540. this->WriteCustomCommand(*si);
  541. }
  542. }
  543. //----------------------------------------------------------------------------
  544. void cmVisualStudio10TargetGenerator
  545. ::WriteCustomCommand(cmSourceFile const* sf)
  546. {
  547. if(this->SourcesVisited.insert(sf).second)
  548. {
  549. if(std::vector<cmSourceFile*> const* depends =
  550. this->GeneratorTarget->GetSourceDepends(sf))
  551. {
  552. for(std::vector<cmSourceFile*>::const_iterator di = depends->begin();
  553. di != depends->end(); ++di)
  554. {
  555. this->WriteCustomCommand(*di);
  556. }
  557. }
  558. if(cmCustomCommand const* command = sf->GetCustomCommand())
  559. {
  560. this->WriteString("<ItemGroup>\n", 1);
  561. this->WriteCustomRule(sf, *command);
  562. this->WriteString("</ItemGroup>\n", 1);
  563. }
  564. }
  565. }
  566. void
  567. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile const* source,
  568. cmCustomCommand const &
  569. command)
  570. {
  571. std::string sourcePath = source->GetFullPath();
  572. // VS 10 will always rebuild a custom command attached to a .rule
  573. // file that doesn't exist so create the file explicitly.
  574. if (source->GetPropertyAsBool("__CMAKE_RULE"))
  575. {
  576. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  577. {
  578. // Make sure the path exists for the file
  579. std::string path = cmSystemTools::GetFilenamePath(sourcePath);
  580. cmSystemTools::MakeDirectory(path.c_str());
  581. cmsys::ofstream fout(sourcePath.c_str());
  582. if(fout)
  583. {
  584. fout << "# generated from CMake\n";
  585. fout.flush();
  586. fout.close();
  587. }
  588. else
  589. {
  590. std::string error = "Could not create file: [";
  591. error += sourcePath;
  592. error += "] ";
  593. cmSystemTools::Error
  594. (error.c_str(), cmSystemTools::GetLastSystemError().c_str());
  595. }
  596. }
  597. }
  598. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  599. std::vector<std::string> *configs =
  600. static_cast<cmGlobalVisualStudio7Generator *>
  601. (this->GlobalGenerator)->GetConfigurations();
  602. this->WriteSource("CustomBuild", source, ">\n");
  603. for(std::vector<std::string>::iterator i = configs->begin();
  604. i != configs->end(); ++i)
  605. {
  606. cmCustomCommandGenerator ccg(command, *i, this->Makefile);
  607. std::string comment = lg->ConstructComment(ccg);
  608. comment = cmVS10EscapeComment(comment);
  609. std::string script =
  610. cmVS10EscapeXML(lg->ConstructScript(ccg));
  611. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  612. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  613. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  614. (*this->BuildFileStream ) << script << "</Command>\n";
  615. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  616. (*this->BuildFileStream ) << source->GetFullPath();
  617. for(std::vector<std::string>::const_iterator d =
  618. ccg.GetDepends().begin();
  619. d != ccg.GetDepends().end();
  620. ++d)
  621. {
  622. std::string dep;
  623. if(this->LocalGenerator->GetRealDependency(d->c_str(), i->c_str(), dep))
  624. {
  625. this->ConvertToWindowsSlash(dep);
  626. (*this->BuildFileStream ) << ";" << dep;
  627. }
  628. }
  629. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  630. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  631. const char* sep = "";
  632. for(std::vector<std::string>::const_iterator o =
  633. ccg.GetOutputs().begin();
  634. o != ccg.GetOutputs().end();
  635. ++o)
  636. {
  637. std::string out = *o;
  638. this->ConvertToWindowsSlash(out);
  639. (*this->BuildFileStream ) << sep << out;
  640. sep = ";";
  641. }
  642. (*this->BuildFileStream ) << "</Outputs>\n";
  643. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  644. {
  645. // VS >= 11 let us turn off linking of custom command outputs.
  646. this->WritePlatformConfigTag("LinkObjects", i->c_str(), 3);
  647. (*this->BuildFileStream ) << "false</LinkObjects>\n";
  648. }
  649. }
  650. this->WriteString("</CustomBuild>\n", 2);
  651. }
  652. std::string
  653. cmVisualStudio10TargetGenerator::ConvertPath(std::string const& path,
  654. bool forceRelative)
  655. {
  656. return forceRelative
  657. ? cmSystemTools::RelativePath(
  658. this->Makefile->GetCurrentOutputDirectory(), path.c_str())
  659. : this->LocalGenerator->Convert(path.c_str(),
  660. cmLocalGenerator::START_OUTPUT,
  661. cmLocalGenerator::UNCHANGED,
  662. /* optional = */ true);
  663. }
  664. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  665. {
  666. // first convert all of the slashes
  667. std::string::size_type pos = 0;
  668. while((pos = s.find('/', pos)) != std::string::npos)
  669. {
  670. s[pos] = '\\';
  671. pos++;
  672. }
  673. }
  674. void cmVisualStudio10TargetGenerator::WriteGroups()
  675. {
  676. // collect up group information
  677. std::vector<cmSourceGroup> sourceGroups =
  678. this->Makefile->GetSourceGroups();
  679. std::vector<cmSourceFile*> classes;
  680. if (!this->Target->GetConfigCommonSourceFiles(classes))
  681. {
  682. return;
  683. }
  684. std::set<cmSourceGroup*> groupsUsed;
  685. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  686. s != classes.end(); s++)
  687. {
  688. cmSourceFile* sf = *s;
  689. std::string const& source = sf->GetFullPath();
  690. cmSourceGroup* sourceGroup =
  691. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  692. groupsUsed.insert(sourceGroup);
  693. }
  694. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  695. // Write out group file
  696. std::string path = this->Makefile->GetStartOutputDirectory();
  697. path += "/";
  698. path += this->Name;
  699. path += ".vcxproj.filters";
  700. cmGeneratedFileStream fout(path.c_str());
  701. fout.SetCopyIfDifferent(true);
  702. char magic[] = {0xEF,0xBB, 0xBF};
  703. fout.write(magic, 3);
  704. cmGeneratedFileStream* save = this->BuildFileStream;
  705. this->BuildFileStream = & fout;
  706. //get the tools version to use
  707. const std::string toolsVer(this->GlobalGenerator->GetToolsVersion());
  708. std::string project_defaults=
  709. "<?xml version=\"1.0\" encoding=\"" +
  710. this->GlobalGenerator->Encoding() + "\"?>\n";
  711. project_defaults.append("<Project ToolsVersion=\"");
  712. project_defaults.append(toolsVer +"\" ");
  713. project_defaults.append(
  714. "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  715. this->WriteString(project_defaults.c_str(),0);
  716. for(ToolSourceMap::const_iterator ti = this->Tools.begin();
  717. ti != this->Tools.end(); ++ti)
  718. {
  719. this->WriteGroupSources(ti->first.c_str(), ti->second, sourceGroups);
  720. }
  721. std::vector<cmSourceFile const*> resxObjs;
  722. this->GeneratorTarget->GetResxSources(resxObjs, "");
  723. if(!resxObjs.empty())
  724. {
  725. this->WriteString("<ItemGroup>\n", 1);
  726. for(std::vector<cmSourceFile const*>::const_iterator oi = resxObjs.begin();
  727. oi != resxObjs.end(); ++oi)
  728. {
  729. std::string obj = (*oi)->GetFullPath();
  730. this->WriteString("<EmbeddedResource Include=\"", 2);
  731. this->ConvertToWindowsSlash(obj);
  732. (*this->BuildFileStream ) << obj << "\">\n";
  733. this->WriteString("<Filter>Resource Files</Filter>\n", 3);
  734. this->WriteString("</EmbeddedResource>\n", 2);
  735. }
  736. this->WriteString("</ItemGroup>\n", 1);
  737. }
  738. // Add object library contents as external objects.
  739. std::vector<std::string> objs;
  740. this->GeneratorTarget->UseObjectLibraries(objs, "");
  741. if(!objs.empty())
  742. {
  743. this->WriteString("<ItemGroup>\n", 1);
  744. for(std::vector<std::string>::const_iterator
  745. oi = objs.begin(); oi != objs.end(); ++oi)
  746. {
  747. std::string obj = *oi;
  748. this->WriteString("<Object Include=\"", 2);
  749. this->ConvertToWindowsSlash(obj);
  750. (*this->BuildFileStream ) << obj << "\">\n";
  751. this->WriteString("<Filter>Object Libraries</Filter>\n", 3);
  752. this->WriteString("</Object>\n", 2);
  753. }
  754. this->WriteString("</ItemGroup>\n", 1);
  755. }
  756. this->WriteString("<ItemGroup>\n", 1);
  757. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  758. g != groupsUsed.end(); ++g)
  759. {
  760. cmSourceGroup* sg = *g;
  761. const char* name = sg->GetFullName();
  762. if(strlen(name) != 0)
  763. {
  764. this->WriteString("<Filter Include=\"", 2);
  765. (*this->BuildFileStream) << name << "\">\n";
  766. std::string guidName = "SG_Filter_";
  767. guidName += name;
  768. this->GlobalGenerator->CreateGUID(guidName.c_str());
  769. this->WriteString("<UniqueIdentifier>", 3);
  770. std::string guid
  771. = this->GlobalGenerator->GetGUID(guidName.c_str());
  772. (*this->BuildFileStream)
  773. << "{"
  774. << guid << "}"
  775. << "</UniqueIdentifier>\n";
  776. this->WriteString("</Filter>\n", 2);
  777. }
  778. }
  779. if(!objs.empty())
  780. {
  781. this->WriteString("<Filter Include=\"Object Libraries\">\n", 2);
  782. std::string guidName = "SG_Filter_Object Libraries";
  783. this->GlobalGenerator->CreateGUID(guidName.c_str());
  784. this->WriteString("<UniqueIdentifier>", 3);
  785. std::string guid =
  786. this->GlobalGenerator->GetGUID(guidName.c_str());
  787. (*this->BuildFileStream) << "{" << guid << "}"
  788. << "</UniqueIdentifier>\n";
  789. this->WriteString("</Filter>\n", 2);
  790. }
  791. if(!resxObjs.empty())
  792. {
  793. this->WriteString("<Filter Include=\"Resource Files\">\n", 2);
  794. std::string guidName = "SG_Filter_Resource Files";
  795. this->GlobalGenerator->CreateGUID(guidName.c_str());
  796. this->WriteString("<UniqueIdentifier>", 3);
  797. std::string guid =
  798. this->GlobalGenerator->GetGUID(guidName.c_str());
  799. (*this->BuildFileStream) << "{" << guid << "}"
  800. << "</UniqueIdentifier>\n";
  801. this->WriteString("<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;", 3);
  802. (*this->BuildFileStream) << "gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;";
  803. (*this->BuildFileStream) << "mfcribbon-ms</Extensions>\n";
  804. this->WriteString("</Filter>\n", 2);
  805. }
  806. this->WriteString("</ItemGroup>\n", 1);
  807. this->WriteString("</Project>\n", 0);
  808. // restore stream pointer
  809. this->BuildFileStream = save;
  810. if (fout.Close())
  811. {
  812. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  813. }
  814. }
  815. // Add to groupsUsed empty source groups that have non-empty children.
  816. void
  817. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  818. std::set<cmSourceGroup*>& groupsUsed,
  819. const std::vector<cmSourceGroup>& allGroups
  820. )
  821. {
  822. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  823. current != allGroups.end(); ++current)
  824. {
  825. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  826. if(children.empty())
  827. {
  828. continue; // the group is really empty
  829. }
  830. this->AddMissingSourceGroups(groupsUsed, children);
  831. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  832. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  833. {
  834. continue; // group has already been added to set
  835. }
  836. // check if it least one of the group's descendants is not empty
  837. // (at least one child must already have been added)
  838. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  839. while(child_it != children.end())
  840. {
  841. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  842. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  843. {
  844. break; // found a child that was already added => add current group too
  845. }
  846. child_it++;
  847. }
  848. if(child_it == children.end())
  849. {
  850. continue; // no descendants have source files => ignore this group
  851. }
  852. groupsUsed.insert(current_ptr);
  853. }
  854. }
  855. void
  856. cmVisualStudio10TargetGenerator::
  857. WriteGroupSources(const char* name,
  858. ToolSources const& sources,
  859. std::vector<cmSourceGroup>& sourceGroups)
  860. {
  861. this->WriteString("<ItemGroup>\n", 1);
  862. for(ToolSources::const_iterator s = sources.begin();
  863. s != sources.end(); ++s)
  864. {
  865. cmSourceFile const* sf = s->SourceFile;
  866. std::string const& source = sf->GetFullPath();
  867. cmSourceGroup* sourceGroup =
  868. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  869. const char* filter = sourceGroup->GetFullName();
  870. this->WriteString("<", 2);
  871. std::string path = this->ConvertPath(source, s->RelativePath);
  872. this->ConvertToWindowsSlash(path);
  873. (*this->BuildFileStream) << name << " Include=\""
  874. << path;
  875. if(strlen(filter))
  876. {
  877. (*this->BuildFileStream) << "\">\n";
  878. this->WriteString("<Filter>", 3);
  879. (*this->BuildFileStream) << filter << "</Filter>\n";
  880. this->WriteString("</", 2);
  881. (*this->BuildFileStream) << name << ">\n";
  882. }
  883. else
  884. {
  885. (*this->BuildFileStream) << "\" />\n";
  886. }
  887. }
  888. this->WriteString("</ItemGroup>\n", 1);
  889. }
  890. void cmVisualStudio10TargetGenerator::WriteSource(
  891. const char* tool, cmSourceFile const* sf, const char* end)
  892. {
  893. // Visual Studio tools append relative paths to the current dir, as in:
  894. //
  895. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  896. //
  897. // and fail if this exceeds the maximum allowed path length. Our path
  898. // conversion uses full paths when possible to allow deeper trees.
  899. bool forceRelative = false;
  900. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), false);
  901. if(this->LocalGenerator->GetVersion() == cmLocalVisualStudioGenerator::VS10
  902. && cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  903. {
  904. // Normal path conversion resulted in a full path. VS 10 (but not 11)
  905. // refuses to show the property page in the IDE for a source file with a
  906. // full path (not starting in a '.' or '/' AFAICT). CMake <= 2.8.4 used a
  907. // relative path but to allow deeper build trees CMake 2.8.[5678] used a
  908. // full path except for custom commands. Custom commands do not work
  909. // without a relative path, but they do not seem to be involved in tools
  910. // with the above behavior. For other sources we now use a relative path
  911. // when the combined path will not be too long so property pages appear.
  912. std::string sourceRel = this->ConvertPath(sf->GetFullPath(), true);
  913. size_t const maxLen = 250;
  914. if(sf->GetCustomCommand() ||
  915. ((strlen(this->Makefile->GetCurrentOutputDirectory()) + 1 +
  916. sourceRel.length()) <= maxLen))
  917. {
  918. forceRelative = true;
  919. sourceFile = sourceRel;
  920. }
  921. else
  922. {
  923. this->GlobalGenerator->PathTooLong(this->Target, sf, sourceRel);
  924. }
  925. }
  926. this->ConvertToWindowsSlash(sourceFile);
  927. this->WriteString("<", 2);
  928. (*this->BuildFileStream ) << tool << " Include=\"" << sourceFile << "\"";
  929. if(sf->GetExtension() == "h" &&
  930. this->IsResxHeader(sf->GetFullPath()))
  931. {
  932. (*this->BuildFileStream ) << ">\n";
  933. this->WriteString("<FileType>CppForm</FileType>\n", 3);
  934. this->WriteString("</ClInclude>\n", 2);
  935. }
  936. else
  937. {
  938. (*this->BuildFileStream ) << (end? end : " />\n");
  939. }
  940. ToolSource toolSource = {sf, forceRelative};
  941. this->Tools[tool].push_back(toolSource);
  942. }
  943. void cmVisualStudio10TargetGenerator::WriteSources(
  944. const char* tool, std::vector<cmSourceFile const*> const& sources)
  945. {
  946. for(std::vector<cmSourceFile const*>::const_iterator
  947. si = sources.begin(); si != sources.end(); ++si)
  948. {
  949. this->WriteSource(tool, *si);
  950. }
  951. }
  952. void cmVisualStudio10TargetGenerator::WriteAllSources()
  953. {
  954. if(this->Target->GetType() > cmTarget::UTILITY)
  955. {
  956. return;
  957. }
  958. this->WriteString("<ItemGroup>\n", 1);
  959. std::vector<cmSourceFile const*> headerSources;
  960. this->GeneratorTarget->GetHeaderSources(headerSources, "");
  961. this->WriteSources("ClInclude", headerSources);
  962. std::vector<cmSourceFile const*> idlSources;
  963. this->GeneratorTarget->GetIDLSources(idlSources, "");
  964. this->WriteSources("Midl", idlSources);
  965. std::vector<cmSourceFile const*> objectSources;
  966. this->GeneratorTarget->GetObjectSources(objectSources, "");
  967. for(std::vector<cmSourceFile const*>::const_iterator
  968. si = objectSources.begin();
  969. si != objectSources.end(); ++si)
  970. {
  971. const std::string& lang = (*si)->GetLanguage();
  972. const char* tool = NULL;
  973. if (lang == "C"|| lang == "CXX")
  974. {
  975. tool = "ClCompile";
  976. }
  977. else if (lang == "ASM_NASM" &&
  978. this->GlobalGenerator->IsMasmEnabled())
  979. {
  980. tool = "MASM";
  981. }
  982. else if (lang == "RC")
  983. {
  984. tool = "ResourceCompile";
  985. }
  986. if (tool)
  987. {
  988. this->WriteSource(tool, *si, " ");
  989. if (this->OutputSourceSpecificFlags(*si))
  990. {
  991. this->WriteString("</", 2);
  992. (*this->BuildFileStream ) << tool << ">\n";
  993. }
  994. else
  995. {
  996. (*this->BuildFileStream ) << " />\n";
  997. }
  998. }
  999. else
  1000. {
  1001. this->WriteSource("None", *si);
  1002. }
  1003. }
  1004. std::vector<cmSourceFile const*> externalObjects;
  1005. this->GeneratorTarget->GetExternalObjects(externalObjects, "");
  1006. for(std::vector<cmSourceFile const*>::iterator
  1007. si = externalObjects.begin();
  1008. si != externalObjects.end(); )
  1009. {
  1010. if (!(*si)->GetObjectLibrary().empty())
  1011. {
  1012. si = externalObjects.erase(si);
  1013. }
  1014. else
  1015. {
  1016. ++si;
  1017. }
  1018. }
  1019. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  1020. {
  1021. // For VS >= 11 we use LinkObjects to avoid linking custom command
  1022. // outputs. Use Object for all external objects, generated or not.
  1023. this->WriteSources("Object", externalObjects);
  1024. }
  1025. else
  1026. {
  1027. // If an object file is generated in this target, then vs10 will use
  1028. // it in the build, and we have to list it as None instead of Object.
  1029. for(std::vector<cmSourceFile const*>::const_iterator
  1030. si = externalObjects.begin();
  1031. si != externalObjects.end(); ++si)
  1032. {
  1033. std::vector<cmSourceFile*> const* d =
  1034. this->GeneratorTarget->GetSourceDepends(*si);
  1035. this->WriteSource((d && !d->empty())? "None":"Object", *si);
  1036. }
  1037. }
  1038. std::vector<cmSourceFile const*> extraSources;
  1039. this->GeneratorTarget->GetExtraSources(extraSources, "");
  1040. this->WriteSources("None", extraSources);
  1041. // Add object library contents as external objects.
  1042. std::vector<std::string> objs;
  1043. this->GeneratorTarget->UseObjectLibraries(objs, "");
  1044. for(std::vector<std::string>::const_iterator
  1045. oi = objs.begin(); oi != objs.end(); ++oi)
  1046. {
  1047. std::string obj = *oi;
  1048. this->WriteString("<Object Include=\"", 2);
  1049. this->ConvertToWindowsSlash(obj);
  1050. (*this->BuildFileStream ) << obj << "\" />\n";
  1051. }
  1052. this->WriteString("</ItemGroup>\n", 1);
  1053. }
  1054. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  1055. cmSourceFile const* source)
  1056. {
  1057. cmSourceFile const& sf = *source;
  1058. std::string objectName;
  1059. if(this->GeneratorTarget->HasExplicitObjectName(&sf))
  1060. {
  1061. objectName = this->GeneratorTarget->GetObjectName(&sf);
  1062. }
  1063. std::string flags;
  1064. std::string defines;
  1065. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  1066. {
  1067. flags += cflags;
  1068. }
  1069. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  1070. {
  1071. defines += cdefs;
  1072. }
  1073. std::string lang =
  1074. this->GlobalGenerator->GetLanguageFromExtension
  1075. (sf.GetExtension().c_str());
  1076. std::string sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  1077. const std::string& linkLanguage = this->Target->GetLinkerLanguage();
  1078. bool needForceLang = false;
  1079. // source file does not match its extension language
  1080. if(lang != sourceLang)
  1081. {
  1082. needForceLang = true;
  1083. lang = sourceLang;
  1084. }
  1085. // if the source file does not match the linker language
  1086. // then force c or c++
  1087. const char* compileAs = 0;
  1088. if(needForceLang || (linkLanguage != lang))
  1089. {
  1090. if(lang == "CXX")
  1091. {
  1092. // force a C++ file type
  1093. compileAs = "CompileAsCpp";
  1094. }
  1095. else if(lang == "C")
  1096. {
  1097. // force to c
  1098. compileAs = "CompileAsC";
  1099. }
  1100. }
  1101. bool hasFlags = false;
  1102. // for the first time we need a new line if there is something
  1103. // produced here.
  1104. const char* firstString = ">\n";
  1105. if(objectName.size())
  1106. {
  1107. (*this->BuildFileStream ) << firstString;
  1108. firstString = "";
  1109. hasFlags = true;
  1110. this->WriteString("<ObjectFileName>", 3);
  1111. (*this->BuildFileStream )
  1112. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  1113. }
  1114. std::vector<std::string> *configs =
  1115. static_cast<cmGlobalVisualStudio7Generator *>
  1116. (this->GlobalGenerator)->GetConfigurations();
  1117. for( std::vector<std::string>::iterator config = configs->begin();
  1118. config != configs->end(); ++config)
  1119. {
  1120. std::string configUpper = cmSystemTools::UpperCase(*config);
  1121. std::string configDefines = defines;
  1122. std::string defPropName = "COMPILE_DEFINITIONS_";
  1123. defPropName += configUpper;
  1124. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  1125. {
  1126. if(configDefines.size())
  1127. {
  1128. configDefines += ";";
  1129. }
  1130. configDefines += ccdefs;
  1131. }
  1132. // if we have flags or defines for this config then
  1133. // use them
  1134. if(!flags.empty() || !configDefines.empty() || compileAs)
  1135. {
  1136. (*this->BuildFileStream ) << firstString;
  1137. firstString = ""; // only do firstString once
  1138. hasFlags = true;
  1139. cmVisualStudioGeneratorOptions
  1140. clOptions(this->LocalGenerator,
  1141. cmVisualStudioGeneratorOptions::Compiler,
  1142. this->GetClFlagTable(), 0, this);
  1143. if(compileAs)
  1144. {
  1145. clOptions.AddFlag("CompileAs", compileAs);
  1146. }
  1147. clOptions.Parse(flags.c_str());
  1148. clOptions.AddDefines(configDefines.c_str());
  1149. clOptions.SetConfiguration((*config).c_str());
  1150. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1151. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1152. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  1153. " ", "\n", lang);
  1154. }
  1155. }
  1156. return hasFlags;
  1157. }
  1158. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  1159. {
  1160. cmTarget::TargetType ttype = this->Target->GetType();
  1161. if(ttype > cmTarget::GLOBAL_TARGET)
  1162. {
  1163. return;
  1164. }
  1165. this->WriteString("<PropertyGroup>\n", 2);
  1166. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  1167. "</_ProjectFileVersion>\n", 3);
  1168. std::vector<std::string> *configs =
  1169. static_cast<cmGlobalVisualStudio7Generator *>
  1170. (this->GlobalGenerator)->GetConfigurations();
  1171. for(std::vector<std::string>::iterator config = configs->begin();
  1172. config != configs->end(); ++config)
  1173. {
  1174. if(ttype >= cmTarget::UTILITY)
  1175. {
  1176. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1177. *this->BuildFileStream
  1178. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  1179. << "</IntDir>\n";
  1180. }
  1181. else
  1182. {
  1183. std::string intermediateDir = this->LocalGenerator->
  1184. GetTargetDirectory(*this->Target);
  1185. intermediateDir += "/";
  1186. intermediateDir += *config;
  1187. intermediateDir += "/";
  1188. std::string outDir;
  1189. std::string targetNameFull;
  1190. if(ttype == cmTarget::OBJECT_LIBRARY)
  1191. {
  1192. outDir = intermediateDir;
  1193. targetNameFull = this->Target->GetName();
  1194. targetNameFull += ".lib";
  1195. }
  1196. else
  1197. {
  1198. outDir = this->Target->GetDirectory(config->c_str()) + "/";
  1199. targetNameFull = this->Target->GetFullName(config->c_str());
  1200. }
  1201. this->ConvertToWindowsSlash(intermediateDir);
  1202. this->ConvertToWindowsSlash(outDir);
  1203. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  1204. *this->BuildFileStream << cmVS10EscapeXML(outDir)
  1205. << "</OutDir>\n";
  1206. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1207. *this->BuildFileStream << cmVS10EscapeXML(intermediateDir)
  1208. << "</IntDir>\n";
  1209. std::string name =
  1210. cmSystemTools::GetFilenameWithoutLastExtension(targetNameFull);
  1211. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  1212. *this->BuildFileStream << cmVS10EscapeXML(name) << "</TargetName>\n";
  1213. std::string ext =
  1214. cmSystemTools::GetFilenameLastExtension(targetNameFull);
  1215. if(ext.empty())
  1216. {
  1217. // An empty TargetExt causes a default extension to be used.
  1218. // A single "." appears to be treated as an empty extension.
  1219. ext = ".";
  1220. }
  1221. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  1222. *this->BuildFileStream << cmVS10EscapeXML(ext) << "</TargetExt>\n";
  1223. this->OutputLinkIncremental(*config);
  1224. }
  1225. }
  1226. this->WriteString("</PropertyGroup>\n", 2);
  1227. }
  1228. void
  1229. cmVisualStudio10TargetGenerator::
  1230. OutputLinkIncremental(std::string const& configName)
  1231. {
  1232. // static libraries and things greater than modules do not need
  1233. // to set this option
  1234. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1235. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1236. {
  1237. return;
  1238. }
  1239. Options& linkOptions = *(this->LinkOptions[configName]);
  1240. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  1241. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  1242. *this->BuildFileStream << (incremental?incremental:"true")
  1243. << "</LinkIncremental>\n";
  1244. linkOptions.RemoveFlag("LinkIncremental");
  1245. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  1246. this->WritePlatformConfigTag("GenerateManifest", configName.c_str(), 3);
  1247. *this->BuildFileStream << (manifest?manifest:"true")
  1248. << "</GenerateManifest>\n";
  1249. linkOptions.RemoveFlag("GenerateManifest");
  1250. // Some link options belong here. Use them now and remove them so that
  1251. // WriteLinkOptions does not use them.
  1252. const char* flags[] = {
  1253. "LinkDelaySign",
  1254. "LinkKeyFile",
  1255. 0};
  1256. for(const char** f = flags; *f; ++f)
  1257. {
  1258. const char* flag = *f;
  1259. if(const char* value = linkOptions.GetFlag(flag))
  1260. {
  1261. this->WritePlatformConfigTag(flag, configName.c_str(), 3);
  1262. *this->BuildFileStream << value << "</" << flag << ">\n";
  1263. linkOptions.RemoveFlag(flag);
  1264. }
  1265. }
  1266. }
  1267. //----------------------------------------------------------------------------
  1268. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  1269. {
  1270. std::vector<std::string> const* configs =
  1271. this->GlobalGenerator->GetConfigurations();
  1272. for(std::vector<std::string>::const_iterator i = configs->begin();
  1273. i != configs->end(); ++i)
  1274. {
  1275. if(!this->ComputeClOptions(*i))
  1276. {
  1277. return false;
  1278. }
  1279. }
  1280. return true;
  1281. }
  1282. //----------------------------------------------------------------------------
  1283. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  1284. std::string const& configName)
  1285. {
  1286. // much of this was copied from here:
  1287. // copied from cmLocalVisualStudio7Generator.cxx 805
  1288. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  1289. cmsys::auto_ptr<Options> pOptions(
  1290. new Options(this->LocalGenerator, Options::Compiler,
  1291. this->GetClFlagTable()));
  1292. Options& clOptions = *pOptions;
  1293. std::string flags;
  1294. const std::string& linkLanguage =
  1295. this->Target->GetLinkerLanguage(configName.c_str());
  1296. if(linkLanguage.empty())
  1297. {
  1298. cmSystemTools::Error
  1299. ("CMake can not determine linker language for target: ",
  1300. this->Name.c_str());
  1301. return false;
  1302. }
  1303. if(linkLanguage == "C" || linkLanguage == "CXX"
  1304. || linkLanguage == "Fortran")
  1305. {
  1306. std::string baseFlagVar = "CMAKE_";
  1307. baseFlagVar += linkLanguage;
  1308. baseFlagVar += "_FLAGS";
  1309. flags = this->
  1310. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  1311. std::string flagVar = baseFlagVar + std::string("_") +
  1312. cmSystemTools::UpperCase(configName);
  1313. flags += " ";
  1314. flags += this->
  1315. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  1316. }
  1317. // set the correct language
  1318. if(linkLanguage == "C")
  1319. {
  1320. clOptions.AddFlag("CompileAs", "CompileAsC");
  1321. }
  1322. if(linkLanguage == "CXX")
  1323. {
  1324. clOptions.AddFlag("CompileAs", "CompileAsCpp");
  1325. }
  1326. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  1327. linkLanguage, configName.c_str());
  1328. // Get preprocessor definitions for this directory.
  1329. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1330. clOptions.FixExceptionHandlingDefault();
  1331. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  1332. std::string asmLocation = configName + "/";
  1333. clOptions.AddFlag("AssemblerListingLocation", asmLocation.c_str());
  1334. clOptions.Parse(flags.c_str());
  1335. clOptions.Parse(defineFlags.c_str());
  1336. std::vector<std::string> targetDefines;
  1337. this->Target->GetCompileDefinitions(targetDefines, configName.c_str());
  1338. clOptions.AddDefines(targetDefines);
  1339. clOptions.SetVerboseMakefile(
  1340. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1341. // Add a definition for the configuration name.
  1342. std::string configDefine = "CMAKE_INTDIR=\"";
  1343. configDefine += configName;
  1344. configDefine += "\"";
  1345. clOptions.AddDefine(configDefine);
  1346. if(const char* exportMacro = this->Target->GetExportMacro())
  1347. {
  1348. clOptions.AddDefine(exportMacro);
  1349. }
  1350. this->ClOptions[configName] = pOptions.release();
  1351. return true;
  1352. }
  1353. //----------------------------------------------------------------------------
  1354. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1355. std::string const& configName,
  1356. std::vector<std::string> const& includes)
  1357. {
  1358. Options& clOptions = *(this->ClOptions[configName]);
  1359. this->WriteString("<ClCompile>\n", 2);
  1360. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1361. this->OutputIncludes(includes);
  1362. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1363. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1364. "\n", "CXX");
  1365. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1366. // If not in debug mode, write the DebugInformationFormat field
  1367. // without value so PDBs don't get generated uselessly.
  1368. if(!clOptions.IsDebug())
  1369. {
  1370. this->WriteString("<DebugInformationFormat>"
  1371. "</DebugInformationFormat>\n", 3);
  1372. }
  1373. // Specify the compiler program database file if configured.
  1374. std::string pdb = this->Target->GetCompilePDBPath(configName.c_str());
  1375. if(!pdb.empty())
  1376. {
  1377. this->ConvertToWindowsSlash(pdb);
  1378. this->WriteString("<ProgramDataBaseFileName>", 3);
  1379. *this->BuildFileStream << cmVS10EscapeXML(pdb)
  1380. << "</ProgramDataBaseFileName>\n";
  1381. }
  1382. this->WriteString("</ClCompile>\n", 2);
  1383. }
  1384. void cmVisualStudio10TargetGenerator::
  1385. OutputIncludes(std::vector<std::string> const & includes)
  1386. {
  1387. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1388. for(std::vector<std::string>::const_iterator i = includes.begin();
  1389. i != includes.end(); ++i)
  1390. {
  1391. std::string incDir = *i;
  1392. this->ConvertToWindowsSlash(incDir);
  1393. *this->BuildFileStream << cmVS10EscapeXML(incDir) << ";";
  1394. }
  1395. this->WriteString("%(AdditionalIncludeDirectories)"
  1396. "</AdditionalIncludeDirectories>\n", 0);
  1397. }
  1398. void cmVisualStudio10TargetGenerator::
  1399. WriteRCOptions(std::string const& configName,
  1400. std::vector<std::string> const & includes)
  1401. {
  1402. this->WriteString("<ResourceCompile>\n", 2);
  1403. Options& clOptions = *(this->ClOptions[configName]);
  1404. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1405. "\n", "RC");
  1406. this->OutputIncludes(includes);
  1407. this->WriteString("</ResourceCompile>\n", 2);
  1408. }
  1409. void
  1410. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1411. {
  1412. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1413. {
  1414. return;
  1415. }
  1416. std::string libflags;
  1417. this->LocalGenerator->GetStaticLibraryFlags(libflags,
  1418. cmSystemTools::UpperCase(config), this->Target);
  1419. if(!libflags.empty())
  1420. {
  1421. this->WriteString("<Lib>\n", 2);
  1422. cmVisualStudioGeneratorOptions
  1423. libOptions(this->LocalGenerator,
  1424. cmVisualStudioGeneratorOptions::Linker,
  1425. this->GetLibFlagTable(), 0, this);
  1426. libOptions.Parse(libflags.c_str());
  1427. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1428. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1429. this->WriteString("</Lib>\n", 2);
  1430. }
  1431. }
  1432. //----------------------------------------------------------------------------
  1433. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  1434. {
  1435. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  1436. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  1437. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1438. {
  1439. std::vector<std::string> const* configs =
  1440. this->GlobalGenerator->GetConfigurations();
  1441. for(std::vector<std::string>::const_iterator i = configs->begin();
  1442. i != configs->end(); ++i)
  1443. {
  1444. if(!this->ComputeLinkOptions(*i))
  1445. {
  1446. return false;
  1447. }
  1448. }
  1449. }
  1450. return true;
  1451. }
  1452. //----------------------------------------------------------------------------
  1453. bool
  1454. cmVisualStudio10TargetGenerator::ComputeLinkOptions(std::string const& config)
  1455. {
  1456. cmsys::auto_ptr<Options> pOptions(
  1457. new Options(this->LocalGenerator, Options::Linker,
  1458. this->GetLinkFlagTable(), 0, this));
  1459. Options& linkOptions = *pOptions;
  1460. const std::string& linkLanguage =
  1461. this->Target->GetLinkerLanguage(config.c_str());
  1462. if(linkLanguage.empty())
  1463. {
  1464. cmSystemTools::Error
  1465. ("CMake can not determine linker language for target: ",
  1466. this->Name.c_str());
  1467. return false;
  1468. }
  1469. std::string CONFIG = cmSystemTools::UpperCase(config);
  1470. const char* linkType = "SHARED";
  1471. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1472. {
  1473. linkType = "MODULE";
  1474. }
  1475. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1476. {
  1477. linkType = "EXE";
  1478. }
  1479. std::string flags;
  1480. std::string linkFlagVarBase = "CMAKE_";
  1481. linkFlagVarBase += linkType;
  1482. linkFlagVarBase += "_LINKER_FLAGS";
  1483. flags += " ";
  1484. flags += this->
  1485. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1486. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1487. flags += " ";
  1488. flags += this->
  1489. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1490. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1491. if(targetLinkFlags)
  1492. {
  1493. flags += " ";
  1494. flags += targetLinkFlags;
  1495. }
  1496. std::string flagsProp = "LINK_FLAGS_";
  1497. flagsProp += CONFIG;
  1498. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1499. {
  1500. flags += " ";
  1501. flags += flagsConfig;
  1502. }
  1503. std::string standardLibsVar = "CMAKE_";
  1504. standardLibsVar += linkLanguage;
  1505. standardLibsVar += "_STANDARD_LIBRARIES";
  1506. std::string
  1507. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1508. // Remove trailing spaces from libs
  1509. std::string::size_type pos = libs.size()-1;
  1510. if(libs.size() != 0)
  1511. {
  1512. while(libs[pos] == ' ')
  1513. {
  1514. pos--;
  1515. }
  1516. }
  1517. if(pos != libs.size()-1)
  1518. {
  1519. libs = libs.substr(0, pos+1);
  1520. }
  1521. // Replace spaces in libs with ;
  1522. cmSystemTools::ReplaceString(libs, " ", ";");
  1523. cmComputeLinkInformation* pcli =
  1524. this->Target->GetLinkInformation(config.c_str());
  1525. if(!pcli)
  1526. {
  1527. cmSystemTools::Error
  1528. ("CMake can not compute cmComputeLinkInformation for target: ",
  1529. this->Name.c_str());
  1530. return false;
  1531. }
  1532. // add the libraries for the target to libs string
  1533. cmComputeLinkInformation& cli = *pcli;
  1534. this->AddLibraries(cli, libs);
  1535. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1536. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1537. const char* sep = "";
  1538. std::string linkDirs;
  1539. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1540. d != ldirs.end(); ++d)
  1541. {
  1542. // first just full path
  1543. linkDirs += sep;
  1544. linkDirs += *d;
  1545. sep = ";";
  1546. linkDirs += sep;
  1547. // next path with configuration type Debug, Release, etc
  1548. linkDirs += *d;
  1549. linkDirs += "/$(Configuration)";
  1550. linkDirs += sep;
  1551. }
  1552. linkDirs += "%(AdditionalLibraryDirectories)";
  1553. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1554. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1555. std::string targetName;
  1556. std::string targetNameSO;
  1557. std::string targetNameFull;
  1558. std::string targetNameImport;
  1559. std::string targetNamePDB;
  1560. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1561. {
  1562. this->Target->GetExecutableNames(targetName, targetNameFull,
  1563. targetNameImport, targetNamePDB,
  1564. config.c_str());
  1565. }
  1566. else
  1567. {
  1568. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1569. targetNameImport, targetNamePDB,
  1570. config.c_str());
  1571. }
  1572. linkOptions.AddFlag("Version", "");
  1573. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1574. {
  1575. linkOptions.AddFlag("SubSystem", "Windows");
  1576. }
  1577. else
  1578. {
  1579. linkOptions.AddFlag("SubSystem", "Console");
  1580. }
  1581. if(const char* stackVal =
  1582. this->Makefile->GetDefinition("CMAKE_"+linkLanguage+"_STACK_SIZE"))
  1583. {
  1584. linkOptions.AddFlag("StackReserveSize", stackVal);
  1585. }
  1586. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1587. {
  1588. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1589. }
  1590. else
  1591. {
  1592. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1593. }
  1594. std::string pdb = this->Target->GetPDBDirectory(config.c_str());
  1595. pdb += "/";
  1596. pdb += targetNamePDB;
  1597. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1598. imLib += "/";
  1599. imLib += targetNameImport;
  1600. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1601. linkOptions.AddFlag("ProgramDataBaseFile", pdb.c_str());
  1602. linkOptions.Parse(flags.c_str());
  1603. std::string def = this->GeneratorTarget->GetModuleDefinitionFile("");
  1604. if(!def.empty())
  1605. {
  1606. linkOptions.AddFlag("ModuleDefinitionFile", def.c_str());
  1607. }
  1608. this->LinkOptions[config] = pOptions.release();
  1609. return true;
  1610. }
  1611. //----------------------------------------------------------------------------
  1612. void
  1613. cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const& config)
  1614. {
  1615. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1616. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1617. {
  1618. return;
  1619. }
  1620. Options& linkOptions = *(this->LinkOptions[config]);
  1621. this->WriteString("<Link>\n", 2);
  1622. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1623. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1624. this->WriteString("</Link>\n", 2);
  1625. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1626. {
  1627. this->WriteString("<ProjectReference>\n", 2);
  1628. this->WriteString(
  1629. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1630. this->WriteString("</ProjectReference>\n", 2);
  1631. }
  1632. }
  1633. void cmVisualStudio10TargetGenerator::AddLibraries(
  1634. cmComputeLinkInformation& cli,
  1635. std::string& libstring)
  1636. {
  1637. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1638. ItemVector libs = cli.GetItems();
  1639. const char* sep = ";";
  1640. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1641. {
  1642. if(l->IsPath)
  1643. {
  1644. std::string path = this->LocalGenerator->
  1645. Convert(l->Value.c_str(),
  1646. cmLocalGenerator::START_OUTPUT,
  1647. cmLocalGenerator::UNCHANGED);
  1648. this->ConvertToWindowsSlash(path);
  1649. libstring += sep;
  1650. libstring += path;
  1651. }
  1652. else if (!l->Target
  1653. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1654. {
  1655. libstring += sep;
  1656. libstring += l->Value;
  1657. }
  1658. }
  1659. }
  1660. void cmVisualStudio10TargetGenerator::
  1661. WriteMidlOptions(std::string const& /*config*/,
  1662. std::vector<std::string> const & includes)
  1663. {
  1664. // This processes *any* of the .idl files specified in the project's file
  1665. // list (and passed as the item metadata %(Filename) expressing the rule
  1666. // input filename) into output files at the per-config *build* dir
  1667. // ($(IntDir)) each.
  1668. //
  1669. // IOW, this MIDL section is intended to provide a fully generic syntax
  1670. // content suitable for most cases (read: if you get errors, then it's quite
  1671. // probable that the error is on your side of the .idl setup).
  1672. //
  1673. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  1674. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  1675. // project's file list, otherwise the compiler-side processing won't pick it
  1676. // up (for non-directory form, it ends up looking in project binary dir
  1677. // only). Perhaps there's something to be done to make this more automatic
  1678. // on the CMake side?
  1679. this->WriteString("<Midl>\n", 2);
  1680. this->OutputIncludes(includes);
  1681. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1682. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1683. this->WriteString(
  1684. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1685. this->WriteString(
  1686. "<InterfaceIdentifierFileName>"
  1687. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1688. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1689. this->WriteString("</Midl>\n", 2);
  1690. }
  1691. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1692. {
  1693. std::vector<std::string> *configs =
  1694. static_cast<cmGlobalVisualStudio7Generator *>
  1695. (this->GlobalGenerator)->GetConfigurations();
  1696. for(std::vector<std::string>::iterator i = configs->begin();
  1697. i != configs->end(); ++i)
  1698. {
  1699. std::vector<std::string> includes;
  1700. this->LocalGenerator->GetIncludeDirectories(includes,
  1701. this->GeneratorTarget,
  1702. "C", i->c_str());
  1703. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1704. *this->BuildFileStream << "\n";
  1705. // output cl compile flags <ClCompile></ClCompile>
  1706. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  1707. {
  1708. this->WriteClOptions(*i, includes);
  1709. // output rc compile flags <ResourceCompile></ResourceCompile>
  1710. this->WriteRCOptions(*i, includes);
  1711. }
  1712. // output midl flags <Midl></Midl>
  1713. this->WriteMidlOptions(*i, includes);
  1714. // write events
  1715. this->WriteEvents(*i);
  1716. // output link flags <Link></Link>
  1717. this->WriteLinkOptions(*i);
  1718. // output lib flags <Lib></Lib>
  1719. this->WriteLibOptions(*i);
  1720. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1721. }
  1722. }
  1723. void
  1724. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1725. {
  1726. this->WriteEvent("PreLinkEvent",
  1727. this->Target->GetPreLinkCommands(), configName);
  1728. this->WriteEvent("PreBuildEvent",
  1729. this->Target->GetPreBuildCommands(), configName);
  1730. this->WriteEvent("PostBuildEvent",
  1731. this->Target->GetPostBuildCommands(), configName);
  1732. }
  1733. void cmVisualStudio10TargetGenerator::WriteEvent(
  1734. const char* name,
  1735. std::vector<cmCustomCommand> const& commands,
  1736. std::string const& configName)
  1737. {
  1738. if(commands.size() == 0)
  1739. {
  1740. return;
  1741. }
  1742. this->WriteString("<", 2);
  1743. (*this->BuildFileStream ) << name << ">\n";
  1744. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1745. std::string script;
  1746. const char* pre = "";
  1747. std::string comment;
  1748. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1749. i != commands.end(); ++i)
  1750. {
  1751. cmCustomCommandGenerator ccg(*i, configName, this->Makefile);
  1752. comment += pre;
  1753. comment += lg->ConstructComment(ccg);
  1754. script += pre;
  1755. pre = "\n";
  1756. script += cmVS10EscapeXML(lg->ConstructScript(ccg));
  1757. }
  1758. comment = cmVS10EscapeComment(comment);
  1759. this->WriteString("<Message>",3);
  1760. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1761. this->WriteString("<Command>", 3);
  1762. (*this->BuildFileStream ) << script;
  1763. (*this->BuildFileStream ) << "</Command>" << "\n";
  1764. this->WriteString("</", 2);
  1765. (*this->BuildFileStream ) << name << ">\n";
  1766. }
  1767. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1768. {
  1769. cmGlobalGenerator::TargetDependSet const& unordered
  1770. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1771. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1772. OrderedTargetDependSet;
  1773. OrderedTargetDependSet depends(unordered);
  1774. this->WriteString("<ItemGroup>\n", 1);
  1775. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1776. i != depends.end(); ++i)
  1777. {
  1778. cmTarget const* dt = *i;
  1779. if(dt->GetType() == cmTarget::INTERFACE_LIBRARY)
  1780. {
  1781. continue;
  1782. }
  1783. // skip fortran targets as they can not be processed by MSBuild
  1784. // the only reference will be in the .sln file
  1785. if(static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
  1786. ->TargetIsFortranOnly(*dt))
  1787. {
  1788. continue;
  1789. }
  1790. this->WriteString("<ProjectReference Include=\"", 2);
  1791. cmMakefile* mf = dt->GetMakefile();
  1792. std::string name = dt->GetName();
  1793. std::string path;
  1794. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1795. if(p)
  1796. {
  1797. path = p;
  1798. }
  1799. else
  1800. {
  1801. path = mf->GetStartOutputDirectory();
  1802. path += "/";
  1803. path += dt->GetName();
  1804. path += ".vcxproj";
  1805. }
  1806. (*this->BuildFileStream) << path << "\">\n";
  1807. this->WriteString("<Project>", 3);
  1808. (*this->BuildFileStream)
  1809. << this->GlobalGenerator->GetGUID(name.c_str())
  1810. << "</Project>\n";
  1811. this->WriteString("</ProjectReference>\n", 2);
  1812. }
  1813. this->WriteString("</ItemGroup>\n", 1);
  1814. }
  1815. bool cmVisualStudio10TargetGenerator::
  1816. IsResxHeader(const std::string& headerFile)
  1817. {
  1818. std::set<std::string> expectedResxHeaders;
  1819. this->GeneratorTarget->GetExpectedResxHeaders(expectedResxHeaders, "");
  1820. std::set<std::string>::const_iterator it =
  1821. expectedResxHeaders.find(headerFile);
  1822. return it != expectedResxHeaders.end();
  1823. }