cmVisualStudio10TargetGenerator.cxx 67 KB

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