cmVisualStudio10TargetGenerator.cxx 62 KB

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