cmVisualStudio10TargetGenerator.cxx 66 KB

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