cmVisualStudio10TargetGenerator.cxx 62 KB

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