cmVisualStudio10TargetGenerator.cxx 63 KB

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