cmVisualStudio10TargetGenerator.cxx 62 KB

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