cmVisualStudio10TargetGenerator.cxx 67 KB

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