cmVisualStudio10TargetGenerator.cxx 64 KB

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