cmVisualStudio10TargetGenerator.cxx 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  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*> resxObjs;
  353. this->GeneratorTarget->GetResxSources(resxObjs);
  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. std::vector<cmSourceFile*> customCommands;
  515. this->GeneratorTarget->GetCustomCommands(customCommands);
  516. for(std::vector<cmSourceFile*>::const_iterator
  517. si = customCommands.begin();
  518. si != customCommands.end(); ++si)
  519. {
  520. this->WriteCustomCommand(*si);
  521. }
  522. }
  523. //----------------------------------------------------------------------------
  524. void cmVisualStudio10TargetGenerator::WriteCustomCommand(cmSourceFile* sf)
  525. {
  526. if(this->SourcesVisited.insert(sf).second)
  527. {
  528. if(std::vector<cmSourceFile*> const* depends =
  529. this->GeneratorTarget->GetSourceDepends(sf))
  530. {
  531. for(std::vector<cmSourceFile*>::const_iterator di = depends->begin();
  532. di != depends->end(); ++di)
  533. {
  534. this->WriteCustomCommand(*di);
  535. }
  536. }
  537. if(cmCustomCommand const* command = sf->GetCustomCommand())
  538. {
  539. this->WriteString("<ItemGroup>\n", 1);
  540. this->WriteCustomRule(sf, *command);
  541. this->WriteString("</ItemGroup>\n", 1);
  542. }
  543. }
  544. }
  545. void
  546. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile* source,
  547. cmCustomCommand const &
  548. command)
  549. {
  550. std::string sourcePath = source->GetFullPath();
  551. // VS 10 will always rebuild a custom command attached to a .rule
  552. // file that doesn't exist so create the file explicitly.
  553. if (source->GetPropertyAsBool("__CMAKE_RULE"))
  554. {
  555. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  556. {
  557. // Make sure the path exists for the file
  558. std::string path = cmSystemTools::GetFilenamePath(sourcePath);
  559. cmSystemTools::MakeDirectory(path.c_str());
  560. cmsys::ofstream fout(sourcePath.c_str());
  561. if(fout)
  562. {
  563. fout << "# generated from CMake\n";
  564. fout.flush();
  565. fout.close();
  566. }
  567. else
  568. {
  569. std::string error = "Could not create file: [";
  570. error += sourcePath;
  571. error += "] ";
  572. cmSystemTools::Error
  573. (error.c_str(), cmSystemTools::GetLastSystemError().c_str());
  574. }
  575. }
  576. }
  577. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  578. std::string comment = lg->ConstructComment(command);
  579. comment = cmVS10EscapeComment(comment);
  580. std::vector<std::string> *configs =
  581. static_cast<cmGlobalVisualStudio7Generator *>
  582. (this->GlobalGenerator)->GetConfigurations();
  583. this->WriteSource("CustomBuild", source, ">\n");
  584. for(std::vector<std::string>::iterator i = configs->begin();
  585. i != configs->end(); ++i)
  586. {
  587. std::string script =
  588. cmVS10EscapeXML(lg->ConstructScript(command, i->c_str()));
  589. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  590. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  591. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  592. (*this->BuildFileStream ) << script << "</Command>\n";
  593. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  594. (*this->BuildFileStream ) << source->GetFullPath();
  595. for(std::vector<std::string>::const_iterator d =
  596. command.GetDepends().begin();
  597. d != command.GetDepends().end();
  598. ++d)
  599. {
  600. std::string dep;
  601. if(this->LocalGenerator->GetRealDependency(d->c_str(), i->c_str(), dep))
  602. {
  603. this->ConvertToWindowsSlash(dep);
  604. (*this->BuildFileStream ) << ";" << dep;
  605. }
  606. }
  607. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  608. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  609. const char* sep = "";
  610. for(std::vector<std::string>::const_iterator o =
  611. command.GetOutputs().begin();
  612. o != command.GetOutputs().end();
  613. ++o)
  614. {
  615. std::string out = *o;
  616. this->ConvertToWindowsSlash(out);
  617. (*this->BuildFileStream ) << sep << out;
  618. sep = ";";
  619. }
  620. (*this->BuildFileStream ) << "</Outputs>\n";
  621. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  622. {
  623. // VS >= 11 let us turn off linking of custom command outputs.
  624. this->WritePlatformConfigTag("LinkObjects", i->c_str(), 3);
  625. (*this->BuildFileStream ) << "false</LinkObjects>\n";
  626. }
  627. }
  628. this->WriteString("</CustomBuild>\n", 2);
  629. }
  630. std::string
  631. cmVisualStudio10TargetGenerator::ConvertPath(std::string const& path,
  632. bool forceRelative)
  633. {
  634. return forceRelative
  635. ? cmSystemTools::RelativePath(
  636. this->Makefile->GetCurrentOutputDirectory(), path.c_str())
  637. : this->LocalGenerator->Convert(path.c_str(),
  638. cmLocalGenerator::START_OUTPUT,
  639. cmLocalGenerator::UNCHANGED);
  640. }
  641. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  642. {
  643. // first convert all of the slashes
  644. std::string::size_type pos = 0;
  645. while((pos = s.find('/', pos)) != std::string::npos)
  646. {
  647. s[pos] = '\\';
  648. pos++;
  649. }
  650. }
  651. void cmVisualStudio10TargetGenerator::WriteGroups()
  652. {
  653. // collect up group information
  654. std::vector<cmSourceGroup> sourceGroups =
  655. this->Makefile->GetSourceGroups();
  656. std::vector<cmSourceFile*> classes;
  657. this->Target->GetSourceFiles(classes);
  658. std::set<cmSourceGroup*> groupsUsed;
  659. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  660. s != classes.end(); s++)
  661. {
  662. cmSourceFile* sf = *s;
  663. std::string const& source = sf->GetFullPath();
  664. cmSourceGroup* sourceGroup =
  665. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  666. groupsUsed.insert(sourceGroup);
  667. }
  668. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  669. // Write out group file
  670. std::string path = this->Makefile->GetStartOutputDirectory();
  671. path += "/";
  672. path += this->Name;
  673. path += ".vcxproj.filters";
  674. cmGeneratedFileStream fout(path.c_str());
  675. fout.SetCopyIfDifferent(true);
  676. char magic[] = {0xEF,0xBB, 0xBF};
  677. fout.write(magic, 3);
  678. cmGeneratedFileStream* save = this->BuildFileStream;
  679. this->BuildFileStream = & fout;
  680. //get the tools version to use
  681. const std::string toolsVer(this->GlobalGenerator->GetToolsVersion());
  682. std::string project_defaults=
  683. "<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\n";
  684. project_defaults.append("<Project ToolsVersion=\"");
  685. project_defaults.append(toolsVer +"\" ");
  686. project_defaults.append(
  687. "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  688. this->WriteString(project_defaults.c_str(),0);
  689. for(ToolSourceMap::const_iterator ti = this->Tools.begin();
  690. ti != this->Tools.end(); ++ti)
  691. {
  692. this->WriteGroupSources(ti->first.c_str(), ti->second, sourceGroups);
  693. }
  694. std::vector<cmSourceFile*> resxObjs;
  695. this->GeneratorTarget->GetResxSources(resxObjs);
  696. if(!resxObjs.empty())
  697. {
  698. this->WriteString("<ItemGroup>\n", 1);
  699. for(std::vector<cmSourceFile*>::const_iterator oi = resxObjs.begin();
  700. oi != resxObjs.end(); ++oi)
  701. {
  702. std::string obj = (*oi)->GetFullPath();
  703. this->WriteString("<EmbeddedResource Include=\"", 2);
  704. this->ConvertToWindowsSlash(obj);
  705. (*this->BuildFileStream ) << obj << "\">\n";
  706. this->WriteString("<Filter>Resource Files</Filter>\n", 3);
  707. this->WriteString("</EmbeddedResource>\n", 2);
  708. }
  709. this->WriteString("</ItemGroup>\n", 1);
  710. }
  711. // Add object library contents as external objects.
  712. std::vector<std::string> objs;
  713. this->GeneratorTarget->UseObjectLibraries(objs);
  714. if(!objs.empty())
  715. {
  716. this->WriteString("<ItemGroup>\n", 1);
  717. for(std::vector<std::string>::const_iterator
  718. oi = objs.begin(); oi != objs.end(); ++oi)
  719. {
  720. std::string obj = *oi;
  721. this->WriteString("<Object Include=\"", 2);
  722. this->ConvertToWindowsSlash(obj);
  723. (*this->BuildFileStream ) << obj << "\">\n";
  724. this->WriteString("<Filter>Object Libraries</Filter>\n", 3);
  725. this->WriteString("</Object>\n", 2);
  726. }
  727. this->WriteString("</ItemGroup>\n", 1);
  728. }
  729. this->WriteString("<ItemGroup>\n", 1);
  730. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  731. g != groupsUsed.end(); ++g)
  732. {
  733. cmSourceGroup* sg = *g;
  734. const char* name = sg->GetFullName();
  735. if(strlen(name) != 0)
  736. {
  737. this->WriteString("<Filter Include=\"", 2);
  738. (*this->BuildFileStream) << name << "\">\n";
  739. std::string guidName = "SG_Filter_";
  740. guidName += name;
  741. this->GlobalGenerator->CreateGUID(guidName.c_str());
  742. this->WriteString("<UniqueIdentifier>", 3);
  743. std::string guid
  744. = this->GlobalGenerator->GetGUID(guidName.c_str());
  745. (*this->BuildFileStream)
  746. << "{"
  747. << guid << "}"
  748. << "</UniqueIdentifier>\n";
  749. this->WriteString("</Filter>\n", 2);
  750. }
  751. }
  752. if(!objs.empty())
  753. {
  754. this->WriteString("<Filter Include=\"Object Libraries\">\n", 2);
  755. std::string guidName = "SG_Filter_Object Libraries";
  756. this->GlobalGenerator->CreateGUID(guidName.c_str());
  757. this->WriteString("<UniqueIdentifier>", 3);
  758. std::string guid =
  759. this->GlobalGenerator->GetGUID(guidName.c_str());
  760. (*this->BuildFileStream) << "{" << guid << "}"
  761. << "</UniqueIdentifier>\n";
  762. this->WriteString("</Filter>\n", 2);
  763. }
  764. if(!resxObjs.empty())
  765. {
  766. this->WriteString("<Filter Include=\"Resource Files\">\n", 2);
  767. std::string guidName = "SG_Filter_Resource Files";
  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) << "{" << guid << "}"
  773. << "</UniqueIdentifier>\n";
  774. this->WriteString("<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;", 3);
  775. (*this->BuildFileStream) << "gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;";
  776. (*this->BuildFileStream) << "mfcribbon-ms</Extensions>\n";
  777. this->WriteString("</Filter>\n", 2);
  778. }
  779. this->WriteString("</ItemGroup>\n", 1);
  780. this->WriteString("</Project>\n", 0);
  781. // restore stream pointer
  782. this->BuildFileStream = save;
  783. if (fout.Close())
  784. {
  785. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  786. }
  787. }
  788. // Add to groupsUsed empty source groups that have non-empty children.
  789. void
  790. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  791. std::set<cmSourceGroup*>& groupsUsed,
  792. const std::vector<cmSourceGroup>& allGroups
  793. )
  794. {
  795. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  796. current != allGroups.end(); ++current)
  797. {
  798. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  799. if(children.empty())
  800. {
  801. continue; // the group is really empty
  802. }
  803. this->AddMissingSourceGroups(groupsUsed, children);
  804. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  805. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  806. {
  807. continue; // group has already been added to set
  808. }
  809. // check if it least one of the group's descendants is not empty
  810. // (at least one child must already have been added)
  811. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  812. while(child_it != children.end())
  813. {
  814. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  815. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  816. {
  817. break; // found a child that was already added => add current group too
  818. }
  819. child_it++;
  820. }
  821. if(child_it == children.end())
  822. {
  823. continue; // no descendants have source files => ignore this group
  824. }
  825. groupsUsed.insert(current_ptr);
  826. }
  827. }
  828. void
  829. cmVisualStudio10TargetGenerator::
  830. WriteGroupSources(const char* name,
  831. ToolSources const& sources,
  832. std::vector<cmSourceGroup>& sourceGroups)
  833. {
  834. this->WriteString("<ItemGroup>\n", 1);
  835. for(ToolSources::const_iterator s = sources.begin();
  836. s != sources.end(); ++s)
  837. {
  838. cmSourceFile* sf = s->SourceFile;
  839. std::string const& source = sf->GetFullPath();
  840. cmSourceGroup* sourceGroup =
  841. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  842. const char* filter = sourceGroup->GetFullName();
  843. this->WriteString("<", 2);
  844. std::string path = this->ConvertPath(source, s->RelativePath);
  845. this->ConvertToWindowsSlash(path);
  846. (*this->BuildFileStream) << name << " Include=\""
  847. << path;
  848. if(strlen(filter))
  849. {
  850. (*this->BuildFileStream) << "\">\n";
  851. this->WriteString("<Filter>", 3);
  852. (*this->BuildFileStream) << filter << "</Filter>\n";
  853. this->WriteString("</", 2);
  854. (*this->BuildFileStream) << name << ">\n";
  855. }
  856. else
  857. {
  858. (*this->BuildFileStream) << "\" />\n";
  859. }
  860. }
  861. this->WriteString("</ItemGroup>\n", 1);
  862. }
  863. void cmVisualStudio10TargetGenerator::WriteSource(
  864. const char* tool, cmSourceFile* sf, const char* end)
  865. {
  866. // Visual Studio tools append relative paths to the current dir, as in:
  867. //
  868. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  869. //
  870. // and fail if this exceeds the maximum allowed path length. Our path
  871. // conversion uses full paths outside the build tree to allow deeper trees.
  872. bool forceRelative = false;
  873. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), false);
  874. if(this->LocalGenerator->GetVersion() == cmLocalVisualStudioGenerator::VS10
  875. && cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  876. {
  877. // Normal path conversion resulted in a full path. VS 10 (but not 11)
  878. // refuses to show the property page in the IDE for a source file with a
  879. // full path (not starting in a '.' or '/' AFAICT). CMake <= 2.8.4 used a
  880. // relative path but to allow deeper build trees CMake 2.8.[5678] used a
  881. // full path except for custom commands. Custom commands do not work
  882. // without a relative path, but they do not seem to be involved in tools
  883. // with the above behavior. For other sources we now use a relative path
  884. // when the combined path will not be too long so property pages appear.
  885. std::string sourceRel = this->ConvertPath(sf->GetFullPath(), true);
  886. size_t const maxLen = 250;
  887. if(sf->GetCustomCommand() ||
  888. ((strlen(this->Makefile->GetCurrentOutputDirectory()) + 1 +
  889. sourceRel.length()) <= maxLen))
  890. {
  891. forceRelative = true;
  892. sourceFile = sourceRel;
  893. }
  894. else
  895. {
  896. this->GlobalGenerator->PathTooLong(this->Target, sf, sourceRel);
  897. }
  898. }
  899. this->ConvertToWindowsSlash(sourceFile);
  900. this->WriteString("<", 2);
  901. (*this->BuildFileStream ) << tool << " Include=\"" << sourceFile << "\"";
  902. if(sf->GetExtension() == "h" &&
  903. this->IsResxHeader(sf->GetFullPath()))
  904. {
  905. (*this->BuildFileStream ) << ">\n";
  906. this->WriteString("<FileType>CppForm</FileType>\n", 3);
  907. this->WriteString("</ClInclude>\n", 2);
  908. }
  909. else
  910. {
  911. (*this->BuildFileStream ) << (end? end : " />\n");
  912. }
  913. ToolSource toolSource = {sf, forceRelative};
  914. this->Tools[tool].push_back(toolSource);
  915. }
  916. void cmVisualStudio10TargetGenerator::WriteSources(
  917. const char* tool, std::vector<cmSourceFile*> const& sources)
  918. {
  919. for(std::vector<cmSourceFile*>::const_iterator
  920. si = sources.begin(); si != sources.end(); ++si)
  921. {
  922. this->WriteSource(tool, *si);
  923. }
  924. }
  925. void cmVisualStudio10TargetGenerator::WriteAllSources()
  926. {
  927. if(this->Target->GetType() > cmTarget::UTILITY)
  928. {
  929. return;
  930. }
  931. this->WriteString("<ItemGroup>\n", 1);
  932. std::vector<cmSourceFile*> headerSources;
  933. this->GeneratorTarget->GetHeaderSources(headerSources);
  934. this->WriteSources("ClInclude", headerSources);
  935. std::vector<cmSourceFile*> idlSources;
  936. this->GeneratorTarget->GetIDLSources(idlSources);
  937. this->WriteSources("Midl", idlSources);
  938. std::vector<cmSourceFile*> objectSources;
  939. this->GeneratorTarget->GetObjectSources(objectSources);
  940. for(std::vector<cmSourceFile*>::const_iterator
  941. si = objectSources.begin();
  942. si != objectSources.end(); ++si)
  943. {
  944. const char* lang = (*si)->GetLanguage();
  945. const char* tool = NULL;
  946. if (strcmp(lang, "C") == 0 || strcmp(lang, "CXX") == 0)
  947. {
  948. tool = "ClCompile";
  949. }
  950. else if (strcmp(lang, "ASM_MASM") == 0 &&
  951. this->GlobalGenerator->IsMasmEnabled())
  952. {
  953. tool = "MASM";
  954. }
  955. else if (strcmp(lang, "RC") == 0)
  956. {
  957. tool = "ResourceCompile";
  958. }
  959. if (tool)
  960. {
  961. this->WriteSource(tool, *si, " ");
  962. if (this->OutputSourceSpecificFlags(*si))
  963. {
  964. this->WriteString("</", 2);
  965. (*this->BuildFileStream ) << tool << ">\n";
  966. }
  967. else
  968. {
  969. (*this->BuildFileStream ) << " />\n";
  970. }
  971. }
  972. else
  973. {
  974. this->WriteSource("None", *si);
  975. }
  976. }
  977. std::vector<cmSourceFile*> externalObjects;
  978. this->GeneratorTarget->GetExternalObjects(externalObjects);
  979. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  980. {
  981. // For VS >= 11 we use LinkObjects to avoid linking custom command
  982. // outputs. Use Object for all external objects, generated or not.
  983. this->WriteSources("Object", externalObjects);
  984. }
  985. else
  986. {
  987. // If an object file is generated in this target, then vs10 will use
  988. // it in the build, and we have to list it as None instead of Object.
  989. for(std::vector<cmSourceFile*>::const_iterator
  990. si = externalObjects.begin();
  991. si != externalObjects.end(); ++si)
  992. {
  993. std::vector<cmSourceFile*> const* d =
  994. this->GeneratorTarget->GetSourceDepends(*si);
  995. this->WriteSource((d && !d->empty())? "None":"Object", *si);
  996. }
  997. }
  998. std::vector<cmSourceFile*> extraSources;
  999. this->GeneratorTarget->GetExtraSources(extraSources);
  1000. this->WriteSources("None", extraSources);
  1001. // Add object library contents as external objects.
  1002. std::vector<std::string> objs;
  1003. this->GeneratorTarget->UseObjectLibraries(objs);
  1004. for(std::vector<std::string>::const_iterator
  1005. oi = objs.begin(); oi != objs.end(); ++oi)
  1006. {
  1007. std::string obj = *oi;
  1008. this->WriteString("<Object Include=\"", 2);
  1009. this->ConvertToWindowsSlash(obj);
  1010. (*this->BuildFileStream ) << obj << "\" />\n";
  1011. }
  1012. this->WriteString("</ItemGroup>\n", 1);
  1013. }
  1014. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  1015. cmSourceFile* source)
  1016. {
  1017. cmSourceFile& sf = *source;
  1018. std::string objectName;
  1019. if(this->GeneratorTarget->HasExplicitObjectName(&sf))
  1020. {
  1021. objectName = this->GeneratorTarget->GetObjectName(&sf);
  1022. }
  1023. std::string flags;
  1024. std::string defines;
  1025. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  1026. {
  1027. flags += cflags;
  1028. }
  1029. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  1030. {
  1031. defines += cdefs;
  1032. }
  1033. const char* lang =
  1034. this->GlobalGenerator->GetLanguageFromExtension
  1035. (sf.GetExtension().c_str());
  1036. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  1037. const char* linkLanguage = this->Target->GetLinkerLanguage();
  1038. bool needForceLang = false;
  1039. // source file does not match its extension language
  1040. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  1041. {
  1042. needForceLang = true;
  1043. lang = sourceLang;
  1044. }
  1045. // if the source file does not match the linker language
  1046. // then force c or c++
  1047. if(needForceLang || (linkLanguage && lang
  1048. && strcmp(lang, linkLanguage) != 0))
  1049. {
  1050. if(strcmp(lang, "CXX") == 0)
  1051. {
  1052. // force a C++ file type
  1053. flags += " /TP ";
  1054. }
  1055. else if(strcmp(lang, "C") == 0)
  1056. {
  1057. // force to c
  1058. flags += " /TC ";
  1059. }
  1060. }
  1061. bool hasFlags = false;
  1062. // for the first time we need a new line if there is something
  1063. // produced here.
  1064. const char* firstString = ">\n";
  1065. if(objectName.size())
  1066. {
  1067. (*this->BuildFileStream ) << firstString;
  1068. firstString = "";
  1069. hasFlags = true;
  1070. this->WriteString("<ObjectFileName>", 3);
  1071. (*this->BuildFileStream )
  1072. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  1073. }
  1074. std::vector<std::string> *configs =
  1075. static_cast<cmGlobalVisualStudio7Generator *>
  1076. (this->GlobalGenerator)->GetConfigurations();
  1077. for( std::vector<std::string>::iterator config = configs->begin();
  1078. config != configs->end(); ++config)
  1079. {
  1080. std::string configUpper = cmSystemTools::UpperCase(*config);
  1081. std::string configDefines = defines;
  1082. std::string defPropName = "COMPILE_DEFINITIONS_";
  1083. defPropName += configUpper;
  1084. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  1085. {
  1086. if(configDefines.size())
  1087. {
  1088. configDefines += ";";
  1089. }
  1090. configDefines += ccdefs;
  1091. }
  1092. // if we have flags or defines for this config then
  1093. // use them
  1094. if(flags.size() || configDefines.size())
  1095. {
  1096. (*this->BuildFileStream ) << firstString;
  1097. firstString = ""; // only do firstString once
  1098. hasFlags = true;
  1099. cmVisualStudioGeneratorOptions
  1100. clOptions(this->LocalGenerator,
  1101. cmVisualStudioGeneratorOptions::Compiler,
  1102. cmVSGetCLFlagTable(this->LocalGenerator), 0, this);
  1103. clOptions.Parse(flags.c_str());
  1104. clOptions.AddDefines(configDefines.c_str());
  1105. clOptions.SetConfiguration((*config).c_str());
  1106. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1107. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1108. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  1109. " ", "\n", lang);
  1110. }
  1111. }
  1112. return hasFlags;
  1113. }
  1114. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  1115. {
  1116. cmTarget::TargetType ttype = this->Target->GetType();
  1117. if(ttype > cmTarget::GLOBAL_TARGET)
  1118. {
  1119. return;
  1120. }
  1121. this->WriteString("<PropertyGroup>\n", 2);
  1122. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  1123. "</_ProjectFileVersion>\n", 3);
  1124. std::vector<std::string> *configs =
  1125. static_cast<cmGlobalVisualStudio7Generator *>
  1126. (this->GlobalGenerator)->GetConfigurations();
  1127. for(std::vector<std::string>::iterator config = configs->begin();
  1128. config != configs->end(); ++config)
  1129. {
  1130. if(ttype >= cmTarget::UTILITY)
  1131. {
  1132. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1133. *this->BuildFileStream
  1134. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  1135. << "</IntDir>\n";
  1136. }
  1137. else
  1138. {
  1139. std::string intermediateDir = this->LocalGenerator->
  1140. GetTargetDirectory(*this->Target);
  1141. intermediateDir += "/";
  1142. intermediateDir += *config;
  1143. intermediateDir += "/";
  1144. std::string outDir;
  1145. std::string targetNameFull;
  1146. if(ttype == cmTarget::OBJECT_LIBRARY)
  1147. {
  1148. outDir = intermediateDir;
  1149. targetNameFull = this->Target->GetName();
  1150. targetNameFull += ".lib";
  1151. }
  1152. else
  1153. {
  1154. outDir = this->Target->GetDirectory(config->c_str()) + "/";
  1155. targetNameFull = this->Target->GetFullName(config->c_str());
  1156. }
  1157. this->ConvertToWindowsSlash(intermediateDir);
  1158. this->ConvertToWindowsSlash(outDir);
  1159. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  1160. *this->BuildFileStream << outDir
  1161. << "</OutDir>\n";
  1162. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1163. *this->BuildFileStream << intermediateDir
  1164. << "</IntDir>\n";
  1165. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  1166. *this->BuildFileStream
  1167. << cmSystemTools::GetFilenameWithoutLastExtension(
  1168. targetNameFull.c_str())
  1169. << "</TargetName>\n";
  1170. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  1171. *this->BuildFileStream
  1172. << cmSystemTools::GetFilenameLastExtension(targetNameFull.c_str())
  1173. << "</TargetExt>\n";
  1174. this->OutputLinkIncremental(*config);
  1175. }
  1176. }
  1177. this->WriteString("</PropertyGroup>\n", 2);
  1178. }
  1179. void
  1180. cmVisualStudio10TargetGenerator::
  1181. OutputLinkIncremental(std::string const& configName)
  1182. {
  1183. // static libraries and things greater than modules do not need
  1184. // to set this option
  1185. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1186. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1187. {
  1188. return;
  1189. }
  1190. Options& linkOptions = *(this->LinkOptions[configName]);
  1191. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  1192. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  1193. *this->BuildFileStream << (incremental?incremental:"true")
  1194. << "</LinkIncremental>\n";
  1195. linkOptions.RemoveFlag("LinkIncremental");
  1196. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  1197. this->WritePlatformConfigTag("GenerateManifest", configName.c_str(), 3);
  1198. *this->BuildFileStream << (manifest?manifest:"true")
  1199. << "</GenerateManifest>\n";
  1200. linkOptions.RemoveFlag("GenerateManifest");
  1201. // Some link options belong here. Use them now and remove them so that
  1202. // WriteLinkOptions does not use them.
  1203. const char* flags[] = {
  1204. "LinkDelaySign",
  1205. "LinkKeyFile",
  1206. 0};
  1207. for(const char** f = flags; *f; ++f)
  1208. {
  1209. const char* flag = *f;
  1210. if(const char* value = linkOptions.GetFlag(flag))
  1211. {
  1212. this->WritePlatformConfigTag(flag, configName.c_str(), 3);
  1213. *this->BuildFileStream << value << "</" << flag << ">\n";
  1214. linkOptions.RemoveFlag(flag);
  1215. }
  1216. }
  1217. }
  1218. //----------------------------------------------------------------------------
  1219. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  1220. {
  1221. std::vector<std::string> const* configs =
  1222. this->GlobalGenerator->GetConfigurations();
  1223. for(std::vector<std::string>::const_iterator i = configs->begin();
  1224. i != configs->end(); ++i)
  1225. {
  1226. if(!this->ComputeClOptions(*i))
  1227. {
  1228. return false;
  1229. }
  1230. }
  1231. return true;
  1232. }
  1233. //----------------------------------------------------------------------------
  1234. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  1235. std::string const& configName)
  1236. {
  1237. // much of this was copied from here:
  1238. // copied from cmLocalVisualStudio7Generator.cxx 805
  1239. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  1240. cmsys::auto_ptr<Options> pOptions(
  1241. new Options(this->LocalGenerator, Options::Compiler,
  1242. cmVSGetCLFlagTable(this->LocalGenerator)));
  1243. Options& clOptions = *pOptions;
  1244. std::string flags;
  1245. // collect up flags for
  1246. if(this->Target->GetType() < cmTarget::UTILITY)
  1247. {
  1248. const char* linkLanguage =
  1249. this->Target->GetLinkerLanguage(configName.c_str());
  1250. if(!linkLanguage)
  1251. {
  1252. cmSystemTools::Error
  1253. ("CMake can not determine linker language for target: ",
  1254. this->Name.c_str());
  1255. return false;
  1256. }
  1257. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  1258. || strcmp(linkLanguage, "Fortran") == 0)
  1259. {
  1260. std::string baseFlagVar = "CMAKE_";
  1261. baseFlagVar += linkLanguage;
  1262. baseFlagVar += "_FLAGS";
  1263. flags = this->
  1264. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  1265. std::string flagVar = baseFlagVar + std::string("_") +
  1266. cmSystemTools::UpperCase(configName);
  1267. flags += " ";
  1268. flags += this->
  1269. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  1270. }
  1271. // set the correct language
  1272. if(strcmp(linkLanguage, "C") == 0)
  1273. {
  1274. flags += " /TC ";
  1275. }
  1276. if(strcmp(linkLanguage, "CXX") == 0)
  1277. {
  1278. flags += " /TP ";
  1279. }
  1280. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  1281. linkLanguage, configName.c_str());
  1282. }
  1283. // Get preprocessor definitions for this directory.
  1284. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1285. clOptions.FixExceptionHandlingDefault();
  1286. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  1287. std::string asmLocation = configName + "/";
  1288. clOptions.AddFlag("AssemblerListingLocation", asmLocation.c_str());
  1289. clOptions.Parse(flags.c_str());
  1290. clOptions.Parse(defineFlags.c_str());
  1291. std::vector<std::string> targetDefines;
  1292. this->Target->GetCompileDefinitions(targetDefines, configName.c_str());
  1293. clOptions.AddDefines(targetDefines);
  1294. clOptions.SetVerboseMakefile(
  1295. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1296. // Add a definition for the configuration name.
  1297. std::string configDefine = "CMAKE_INTDIR=\"";
  1298. configDefine += configName;
  1299. configDefine += "\"";
  1300. clOptions.AddDefine(configDefine);
  1301. if(const char* exportMacro = this->Target->GetExportMacro())
  1302. {
  1303. clOptions.AddDefine(exportMacro);
  1304. }
  1305. this->ClOptions[configName] = pOptions.release();
  1306. return true;
  1307. }
  1308. //----------------------------------------------------------------------------
  1309. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1310. std::string const& configName,
  1311. std::vector<std::string> const& includes)
  1312. {
  1313. Options& clOptions = *(this->ClOptions[configName]);
  1314. this->WriteString("<ClCompile>\n", 2);
  1315. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1316. this->OutputIncludes(includes);
  1317. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1318. // If not in debug mode, write the DebugInformationFormat field
  1319. // without value so PDBs don't get generated uselessly.
  1320. if(!clOptions.IsDebug())
  1321. {
  1322. this->WriteString("<DebugInformationFormat>"
  1323. "</DebugInformationFormat>\n", 3);
  1324. }
  1325. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1326. "\n", "CXX");
  1327. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1328. // Specify the compiler program database file if configured.
  1329. std::string pdb = this->Target->GetCompilePDBPath(configName.c_str());
  1330. if(!pdb.empty())
  1331. {
  1332. this->ConvertToWindowsSlash(pdb);
  1333. this->WriteString("<ProgramDataBaseFileName>", 3);
  1334. *this->BuildFileStream << cmVS10EscapeXML(pdb)
  1335. << "</ProgramDataBaseFileName>\n";
  1336. }
  1337. this->WriteString("</ClCompile>\n", 2);
  1338. }
  1339. void cmVisualStudio10TargetGenerator::
  1340. OutputIncludes(std::vector<std::string> const & includes)
  1341. {
  1342. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1343. for(std::vector<std::string>::const_iterator i = includes.begin();
  1344. i != includes.end(); ++i)
  1345. {
  1346. std::string incDir = *i;
  1347. this->ConvertToWindowsSlash(incDir);
  1348. *this->BuildFileStream << cmVS10EscapeXML(incDir) << ";";
  1349. }
  1350. this->WriteString("%(AdditionalIncludeDirectories)"
  1351. "</AdditionalIncludeDirectories>\n", 0);
  1352. }
  1353. void cmVisualStudio10TargetGenerator::
  1354. WriteRCOptions(std::string const& configName,
  1355. std::vector<std::string> const & includes)
  1356. {
  1357. this->WriteString("<ResourceCompile>\n", 2);
  1358. Options& clOptions = *(this->ClOptions[configName]);
  1359. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1360. "\n", "RC");
  1361. this->OutputIncludes(includes);
  1362. this->WriteString("</ResourceCompile>\n", 2);
  1363. }
  1364. void
  1365. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1366. {
  1367. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1368. {
  1369. return;
  1370. }
  1371. std::string libflags;
  1372. this->LocalGenerator->GetStaticLibraryFlags(libflags,
  1373. cmSystemTools::UpperCase(config), this->Target);
  1374. if(!libflags.empty())
  1375. {
  1376. this->WriteString("<Lib>\n", 2);
  1377. cmVisualStudioGeneratorOptions
  1378. libOptions(this->LocalGenerator,
  1379. cmVisualStudioGeneratorOptions::Linker,
  1380. cmVSGetLibFlagTable(this->LocalGenerator), 0, this);
  1381. libOptions.Parse(libflags.c_str());
  1382. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1383. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1384. this->WriteString("</Lib>\n", 2);
  1385. }
  1386. }
  1387. //----------------------------------------------------------------------------
  1388. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  1389. {
  1390. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  1391. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  1392. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1393. {
  1394. std::vector<std::string> const* configs =
  1395. this->GlobalGenerator->GetConfigurations();
  1396. for(std::vector<std::string>::const_iterator i = configs->begin();
  1397. i != configs->end(); ++i)
  1398. {
  1399. if(!this->ComputeLinkOptions(*i))
  1400. {
  1401. return false;
  1402. }
  1403. }
  1404. }
  1405. return true;
  1406. }
  1407. //----------------------------------------------------------------------------
  1408. bool
  1409. cmVisualStudio10TargetGenerator::ComputeLinkOptions(std::string const& config)
  1410. {
  1411. cmsys::auto_ptr<Options> pOptions(
  1412. new Options(this->LocalGenerator, Options::Linker,
  1413. cmVSGetLinkFlagTable(this->LocalGenerator), 0, this));
  1414. Options& linkOptions = *pOptions;
  1415. const char* linkLanguage =
  1416. this->Target->GetLinkerLanguage(config.c_str());
  1417. if(!linkLanguage)
  1418. {
  1419. cmSystemTools::Error
  1420. ("CMake can not determine linker language for target: ",
  1421. this->Name.c_str());
  1422. return false;
  1423. }
  1424. std::string CONFIG = cmSystemTools::UpperCase(config);
  1425. const char* linkType = "SHARED";
  1426. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1427. {
  1428. linkType = "MODULE";
  1429. }
  1430. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1431. {
  1432. linkType = "EXE";
  1433. }
  1434. std::string stackVar = "CMAKE_";
  1435. stackVar += linkLanguage;
  1436. stackVar += "_STACK_SIZE";
  1437. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  1438. std::string flags;
  1439. if(stackVal)
  1440. {
  1441. flags += " /STACK:";
  1442. flags += stackVal;
  1443. }
  1444. std::string linkFlagVarBase = "CMAKE_";
  1445. linkFlagVarBase += linkType;
  1446. linkFlagVarBase += "_LINKER_FLAGS";
  1447. flags += " ";
  1448. flags += this->
  1449. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1450. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1451. flags += " ";
  1452. flags += this->
  1453. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1454. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1455. if(targetLinkFlags)
  1456. {
  1457. flags += " ";
  1458. flags += targetLinkFlags;
  1459. }
  1460. std::string flagsProp = "LINK_FLAGS_";
  1461. flagsProp += CONFIG;
  1462. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1463. {
  1464. flags += " ";
  1465. flags += flagsConfig;
  1466. }
  1467. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1468. {
  1469. linkOptions.AddFlag("SubSystem", "Windows");
  1470. }
  1471. else
  1472. {
  1473. linkOptions.AddFlag("SubSystem", "Console");
  1474. }
  1475. std::string standardLibsVar = "CMAKE_";
  1476. standardLibsVar += linkLanguage;
  1477. standardLibsVar += "_STANDARD_LIBRARIES";
  1478. std::string
  1479. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1480. // Remove trailing spaces from libs
  1481. std::string::size_type pos = libs.size()-1;
  1482. if(libs.size() != 0)
  1483. {
  1484. while(libs[pos] == ' ')
  1485. {
  1486. pos--;
  1487. }
  1488. }
  1489. if(pos != libs.size()-1)
  1490. {
  1491. libs = libs.substr(0, pos+1);
  1492. }
  1493. // Replace spaces in libs with ;
  1494. cmSystemTools::ReplaceString(libs, " ", ";");
  1495. cmComputeLinkInformation* pcli =
  1496. this->Target->GetLinkInformation(config.c_str());
  1497. if(!pcli)
  1498. {
  1499. cmSystemTools::Error
  1500. ("CMake can not compute cmComputeLinkInformation for target: ",
  1501. this->Name.c_str());
  1502. return false;
  1503. }
  1504. // add the libraries for the target to libs string
  1505. cmComputeLinkInformation& cli = *pcli;
  1506. this->AddLibraries(cli, libs);
  1507. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1508. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1509. const char* sep = "";
  1510. std::string linkDirs;
  1511. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1512. d != ldirs.end(); ++d)
  1513. {
  1514. // first just full path
  1515. linkDirs += sep;
  1516. linkDirs += *d;
  1517. sep = ";";
  1518. linkDirs += sep;
  1519. // next path with configuration type Debug, Release, etc
  1520. linkDirs += *d;
  1521. linkDirs += "/$(Configuration)";
  1522. linkDirs += sep;
  1523. }
  1524. linkDirs += "%(AdditionalLibraryDirectories)";
  1525. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1526. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1527. linkOptions.AddFlag("Version", "");
  1528. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1529. {
  1530. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1531. }
  1532. else
  1533. {
  1534. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1535. }
  1536. std::string targetName;
  1537. std::string targetNameSO;
  1538. std::string targetNameFull;
  1539. std::string targetNameImport;
  1540. std::string targetNamePDB;
  1541. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1542. {
  1543. this->Target->GetExecutableNames(targetName, targetNameFull,
  1544. targetNameImport, targetNamePDB,
  1545. config.c_str());
  1546. }
  1547. else
  1548. {
  1549. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1550. targetNameImport, targetNamePDB,
  1551. config.c_str());
  1552. }
  1553. std::string pdb = this->Target->GetPDBDirectory(config.c_str());
  1554. pdb += "/";
  1555. pdb += targetNamePDB;
  1556. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1557. imLib += "/";
  1558. imLib += targetNameImport;
  1559. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1560. linkOptions.AddFlag("ProgramDataBaseFile", pdb.c_str());
  1561. linkOptions.Parse(flags.c_str());
  1562. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  1563. {
  1564. linkOptions.AddFlag("ModuleDefinitionFile",
  1565. this->GeneratorTarget->ModuleDefinitionFile.c_str());
  1566. }
  1567. this->LinkOptions[config] = pOptions.release();
  1568. return true;
  1569. }
  1570. //----------------------------------------------------------------------------
  1571. void
  1572. cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const& config)
  1573. {
  1574. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1575. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1576. {
  1577. return;
  1578. }
  1579. Options& linkOptions = *(this->LinkOptions[config]);
  1580. this->WriteString("<Link>\n", 2);
  1581. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1582. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1583. this->WriteString("</Link>\n", 2);
  1584. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1585. {
  1586. this->WriteString("<ProjectReference>\n", 2);
  1587. this->WriteString(
  1588. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1589. this->WriteString("</ProjectReference>\n", 2);
  1590. }
  1591. }
  1592. void cmVisualStudio10TargetGenerator::AddLibraries(
  1593. cmComputeLinkInformation& cli,
  1594. std::string& libstring)
  1595. {
  1596. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1597. ItemVector libs = cli.GetItems();
  1598. const char* sep = ";";
  1599. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1600. {
  1601. if(l->IsPath)
  1602. {
  1603. std::string path = this->LocalGenerator->
  1604. Convert(l->Value.c_str(),
  1605. cmLocalGenerator::START_OUTPUT,
  1606. cmLocalGenerator::UNCHANGED);
  1607. this->ConvertToWindowsSlash(path);
  1608. libstring += sep;
  1609. libstring += path;
  1610. }
  1611. else if (!l->Target
  1612. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1613. {
  1614. libstring += sep;
  1615. libstring += l->Value;
  1616. }
  1617. }
  1618. }
  1619. void cmVisualStudio10TargetGenerator::
  1620. WriteMidlOptions(std::string const& /*config*/,
  1621. std::vector<std::string> const & includes)
  1622. {
  1623. // This processes *any* of the .idl files specified in the project's file
  1624. // list (and passed as the item metadata %(Filename) expressing the rule
  1625. // input filename) into output files at the per-config *build* dir
  1626. // ($(IntDir)) each.
  1627. //
  1628. // IOW, this MIDL section is intended to provide a fully generic syntax
  1629. // content suitable for most cases (read: if you get errors, then it's quite
  1630. // probable that the error is on your side of the .idl setup).
  1631. //
  1632. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  1633. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  1634. // project's file list, otherwise the compiler-side processing won't pick it
  1635. // up (for non-directory form, it ends up looking in project binary dir
  1636. // only). Perhaps there's something to be done to make this more automatic
  1637. // on the CMake side?
  1638. this->WriteString("<Midl>\n", 2);
  1639. this->OutputIncludes(includes);
  1640. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1641. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1642. this->WriteString(
  1643. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1644. this->WriteString(
  1645. "<InterfaceIdentifierFileName>"
  1646. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1647. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1648. this->WriteString("</Midl>\n", 2);
  1649. }
  1650. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1651. {
  1652. std::vector<std::string> *configs =
  1653. static_cast<cmGlobalVisualStudio7Generator *>
  1654. (this->GlobalGenerator)->GetConfigurations();
  1655. for(std::vector<std::string>::iterator i = configs->begin();
  1656. i != configs->end(); ++i)
  1657. {
  1658. std::vector<std::string> includes;
  1659. this->LocalGenerator->GetIncludeDirectories(includes,
  1660. this->GeneratorTarget,
  1661. "C", i->c_str());
  1662. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1663. *this->BuildFileStream << "\n";
  1664. // output cl compile flags <ClCompile></ClCompile>
  1665. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  1666. {
  1667. this->WriteClOptions(*i, includes);
  1668. // output rc compile flags <ResourceCompile></ResourceCompile>
  1669. this->WriteRCOptions(*i, includes);
  1670. }
  1671. // output midl flags <Midl></Midl>
  1672. this->WriteMidlOptions(*i, includes);
  1673. // write events
  1674. this->WriteEvents(*i);
  1675. // output link flags <Link></Link>
  1676. this->WriteLinkOptions(*i);
  1677. // output lib flags <Lib></Lib>
  1678. this->WriteLibOptions(*i);
  1679. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1680. }
  1681. }
  1682. void
  1683. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1684. {
  1685. this->WriteEvent("PreLinkEvent",
  1686. this->Target->GetPreLinkCommands(), configName);
  1687. this->WriteEvent("PreBuildEvent",
  1688. this->Target->GetPreBuildCommands(), configName);
  1689. this->WriteEvent("PostBuildEvent",
  1690. this->Target->GetPostBuildCommands(), configName);
  1691. }
  1692. void cmVisualStudio10TargetGenerator::WriteEvent(
  1693. const char* name,
  1694. std::vector<cmCustomCommand> const& commands,
  1695. std::string const& configName)
  1696. {
  1697. if(commands.size() == 0)
  1698. {
  1699. return;
  1700. }
  1701. this->WriteString("<", 2);
  1702. (*this->BuildFileStream ) << name << ">\n";
  1703. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1704. std::string script;
  1705. const char* pre = "";
  1706. std::string comment;
  1707. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1708. i != commands.end(); ++i)
  1709. {
  1710. const cmCustomCommand& command = *i;
  1711. comment += pre;
  1712. comment += lg->ConstructComment(command);
  1713. script += pre;
  1714. pre = "\n";
  1715. script +=
  1716. cmVS10EscapeXML(lg->ConstructScript(command, configName.c_str()));
  1717. }
  1718. comment = cmVS10EscapeComment(comment);
  1719. this->WriteString("<Message>",3);
  1720. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1721. this->WriteString("<Command>", 3);
  1722. (*this->BuildFileStream ) << script;
  1723. (*this->BuildFileStream ) << "</Command>" << "\n";
  1724. this->WriteString("</", 2);
  1725. (*this->BuildFileStream ) << name << ">\n";
  1726. }
  1727. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1728. {
  1729. cmGlobalGenerator::TargetDependSet const& unordered
  1730. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1731. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1732. OrderedTargetDependSet;
  1733. OrderedTargetDependSet depends(unordered);
  1734. this->WriteString("<ItemGroup>\n", 1);
  1735. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1736. i != depends.end(); ++i)
  1737. {
  1738. cmTarget const* dt = *i;
  1739. if(dt->GetType() == cmTarget::INTERFACE_LIBRARY)
  1740. {
  1741. continue;
  1742. }
  1743. // skip fortran targets as they can not be processed by MSBuild
  1744. // the only reference will be in the .sln file
  1745. if(static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
  1746. ->TargetIsFortranOnly(*dt))
  1747. {
  1748. continue;
  1749. }
  1750. this->WriteString("<ProjectReference Include=\"", 2);
  1751. cmMakefile* mf = dt->GetMakefile();
  1752. std::string name = dt->GetName();
  1753. std::string path;
  1754. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1755. if(p)
  1756. {
  1757. path = p;
  1758. }
  1759. else
  1760. {
  1761. path = mf->GetStartOutputDirectory();
  1762. path += "/";
  1763. path += dt->GetName();
  1764. path += ".vcxproj";
  1765. }
  1766. (*this->BuildFileStream) << path << "\">\n";
  1767. this->WriteString("<Project>", 3);
  1768. (*this->BuildFileStream)
  1769. << this->GlobalGenerator->GetGUID(name.c_str())
  1770. << "</Project>\n";
  1771. this->WriteString("</ProjectReference>\n", 2);
  1772. }
  1773. this->WriteString("</ItemGroup>\n", 1);
  1774. }
  1775. bool cmVisualStudio10TargetGenerator::
  1776. IsResxHeader(const std::string& headerFile)
  1777. {
  1778. std::set<std::string> expectedResxHeaders;
  1779. this->GeneratorTarget->GetExpectedResxHeaders(expectedResxHeaders);
  1780. std::set<std::string>::const_iterator it =
  1781. expectedResxHeaders.find(headerFile);
  1782. return it != expectedResxHeaders.end();
  1783. }