cmVisualStudio10TargetGenerator.cxx 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  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. if (!this->Target->GetConfigCommonSourceFiles(classes))
  662. {
  663. return;
  664. }
  665. std::set<cmSourceGroup*> groupsUsed;
  666. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  667. s != classes.end(); s++)
  668. {
  669. cmSourceFile* sf = *s;
  670. std::string const& source = sf->GetFullPath();
  671. cmSourceGroup* sourceGroup =
  672. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  673. groupsUsed.insert(sourceGroup);
  674. }
  675. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  676. // Write out group file
  677. std::string path = this->Makefile->GetStartOutputDirectory();
  678. path += "/";
  679. path += this->Name;
  680. path += ".vcxproj.filters";
  681. cmGeneratedFileStream fout(path.c_str());
  682. fout.SetCopyIfDifferent(true);
  683. char magic[] = {0xEF,0xBB, 0xBF};
  684. fout.write(magic, 3);
  685. cmGeneratedFileStream* save = this->BuildFileStream;
  686. this->BuildFileStream = & fout;
  687. //get the tools version to use
  688. const std::string toolsVer(this->GlobalGenerator->GetToolsVersion());
  689. std::string project_defaults=
  690. "<?xml version=\"1.0\" encoding=\"" +
  691. this->GlobalGenerator->Encoding() + "\"?>\n";
  692. project_defaults.append("<Project ToolsVersion=\"");
  693. project_defaults.append(toolsVer +"\" ");
  694. project_defaults.append(
  695. "xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">\n");
  696. this->WriteString(project_defaults.c_str(),0);
  697. for(ToolSourceMap::const_iterator ti = this->Tools.begin();
  698. ti != this->Tools.end(); ++ti)
  699. {
  700. this->WriteGroupSources(ti->first.c_str(), ti->second, sourceGroups);
  701. }
  702. std::vector<cmSourceFile const*> resxObjs;
  703. this->GeneratorTarget->GetResxSources(resxObjs, "");
  704. if(!resxObjs.empty())
  705. {
  706. this->WriteString("<ItemGroup>\n", 1);
  707. for(std::vector<cmSourceFile const*>::const_iterator oi = resxObjs.begin();
  708. oi != resxObjs.end(); ++oi)
  709. {
  710. std::string obj = (*oi)->GetFullPath();
  711. this->WriteString("<EmbeddedResource Include=\"", 2);
  712. this->ConvertToWindowsSlash(obj);
  713. (*this->BuildFileStream ) << obj << "\">\n";
  714. this->WriteString("<Filter>Resource Files</Filter>\n", 3);
  715. this->WriteString("</EmbeddedResource>\n", 2);
  716. }
  717. this->WriteString("</ItemGroup>\n", 1);
  718. }
  719. // Add object library contents as external objects.
  720. std::vector<std::string> objs;
  721. this->GeneratorTarget->UseObjectLibraries(objs, "");
  722. if(!objs.empty())
  723. {
  724. this->WriteString("<ItemGroup>\n", 1);
  725. for(std::vector<std::string>::const_iterator
  726. oi = objs.begin(); oi != objs.end(); ++oi)
  727. {
  728. std::string obj = *oi;
  729. this->WriteString("<Object Include=\"", 2);
  730. this->ConvertToWindowsSlash(obj);
  731. (*this->BuildFileStream ) << obj << "\">\n";
  732. this->WriteString("<Filter>Object Libraries</Filter>\n", 3);
  733. this->WriteString("</Object>\n", 2);
  734. }
  735. this->WriteString("</ItemGroup>\n", 1);
  736. }
  737. this->WriteString("<ItemGroup>\n", 1);
  738. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  739. g != groupsUsed.end(); ++g)
  740. {
  741. cmSourceGroup* sg = *g;
  742. const char* name = sg->GetFullName();
  743. if(strlen(name) != 0)
  744. {
  745. this->WriteString("<Filter Include=\"", 2);
  746. (*this->BuildFileStream) << name << "\">\n";
  747. std::string guidName = "SG_Filter_";
  748. guidName += name;
  749. this->GlobalGenerator->CreateGUID(guidName.c_str());
  750. this->WriteString("<UniqueIdentifier>", 3);
  751. std::string guid
  752. = this->GlobalGenerator->GetGUID(guidName.c_str());
  753. (*this->BuildFileStream)
  754. << "{"
  755. << guid << "}"
  756. << "</UniqueIdentifier>\n";
  757. this->WriteString("</Filter>\n", 2);
  758. }
  759. }
  760. if(!objs.empty())
  761. {
  762. this->WriteString("<Filter Include=\"Object Libraries\">\n", 2);
  763. std::string guidName = "SG_Filter_Object Libraries";
  764. this->GlobalGenerator->CreateGUID(guidName.c_str());
  765. this->WriteString("<UniqueIdentifier>", 3);
  766. std::string guid =
  767. this->GlobalGenerator->GetGUID(guidName.c_str());
  768. (*this->BuildFileStream) << "{" << guid << "}"
  769. << "</UniqueIdentifier>\n";
  770. this->WriteString("</Filter>\n", 2);
  771. }
  772. if(!resxObjs.empty())
  773. {
  774. this->WriteString("<Filter Include=\"Resource Files\">\n", 2);
  775. std::string guidName = "SG_Filter_Resource Files";
  776. this->GlobalGenerator->CreateGUID(guidName.c_str());
  777. this->WriteString("<UniqueIdentifier>", 3);
  778. std::string guid =
  779. this->GlobalGenerator->GetGUID(guidName.c_str());
  780. (*this->BuildFileStream) << "{" << guid << "}"
  781. << "</UniqueIdentifier>\n";
  782. this->WriteString("<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;", 3);
  783. (*this->BuildFileStream) << "gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;";
  784. (*this->BuildFileStream) << "mfcribbon-ms</Extensions>\n";
  785. this->WriteString("</Filter>\n", 2);
  786. }
  787. this->WriteString("</ItemGroup>\n", 1);
  788. this->WriteString("</Project>\n", 0);
  789. // restore stream pointer
  790. this->BuildFileStream = save;
  791. if (fout.Close())
  792. {
  793. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  794. }
  795. }
  796. // Add to groupsUsed empty source groups that have non-empty children.
  797. void
  798. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  799. std::set<cmSourceGroup*>& groupsUsed,
  800. const std::vector<cmSourceGroup>& allGroups
  801. )
  802. {
  803. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  804. current != allGroups.end(); ++current)
  805. {
  806. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  807. if(children.empty())
  808. {
  809. continue; // the group is really empty
  810. }
  811. this->AddMissingSourceGroups(groupsUsed, children);
  812. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  813. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  814. {
  815. continue; // group has already been added to set
  816. }
  817. // check if it least one of the group's descendants is not empty
  818. // (at least one child must already have been added)
  819. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  820. while(child_it != children.end())
  821. {
  822. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  823. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  824. {
  825. break; // found a child that was already added => add current group too
  826. }
  827. child_it++;
  828. }
  829. if(child_it == children.end())
  830. {
  831. continue; // no descendants have source files => ignore this group
  832. }
  833. groupsUsed.insert(current_ptr);
  834. }
  835. }
  836. void
  837. cmVisualStudio10TargetGenerator::
  838. WriteGroupSources(const char* name,
  839. ToolSources const& sources,
  840. std::vector<cmSourceGroup>& sourceGroups)
  841. {
  842. this->WriteString("<ItemGroup>\n", 1);
  843. for(ToolSources::const_iterator s = sources.begin();
  844. s != sources.end(); ++s)
  845. {
  846. cmSourceFile const* sf = s->SourceFile;
  847. std::string const& source = sf->GetFullPath();
  848. cmSourceGroup* sourceGroup =
  849. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  850. const char* filter = sourceGroup->GetFullName();
  851. this->WriteString("<", 2);
  852. std::string path = this->ConvertPath(source, s->RelativePath);
  853. this->ConvertToWindowsSlash(path);
  854. (*this->BuildFileStream) << name << " Include=\""
  855. << path;
  856. if(strlen(filter))
  857. {
  858. (*this->BuildFileStream) << "\">\n";
  859. this->WriteString("<Filter>", 3);
  860. (*this->BuildFileStream) << filter << "</Filter>\n";
  861. this->WriteString("</", 2);
  862. (*this->BuildFileStream) << name << ">\n";
  863. }
  864. else
  865. {
  866. (*this->BuildFileStream) << "\" />\n";
  867. }
  868. }
  869. this->WriteString("</ItemGroup>\n", 1);
  870. }
  871. void cmVisualStudio10TargetGenerator::WriteSource(
  872. const char* tool, cmSourceFile const* sf, const char* end)
  873. {
  874. // Visual Studio tools append relative paths to the current dir, as in:
  875. //
  876. // c:\path\to\current\dir\..\..\..\relative\path\to\source.c
  877. //
  878. // and fail if this exceeds the maximum allowed path length. Our path
  879. // conversion uses full paths outside the build tree to allow deeper trees.
  880. bool forceRelative = false;
  881. std::string sourceFile = this->ConvertPath(sf->GetFullPath(), false);
  882. if(this->LocalGenerator->GetVersion() == cmLocalVisualStudioGenerator::VS10
  883. && cmSystemTools::FileIsFullPath(sourceFile.c_str()))
  884. {
  885. // Normal path conversion resulted in a full path. VS 10 (but not 11)
  886. // refuses to show the property page in the IDE for a source file with a
  887. // full path (not starting in a '.' or '/' AFAICT). CMake <= 2.8.4 used a
  888. // relative path but to allow deeper build trees CMake 2.8.[5678] used a
  889. // full path except for custom commands. Custom commands do not work
  890. // without a relative path, but they do not seem to be involved in tools
  891. // with the above behavior. For other sources we now use a relative path
  892. // when the combined path will not be too long so property pages appear.
  893. std::string sourceRel = this->ConvertPath(sf->GetFullPath(), true);
  894. size_t const maxLen = 250;
  895. if(sf->GetCustomCommand() ||
  896. ((strlen(this->Makefile->GetCurrentOutputDirectory()) + 1 +
  897. sourceRel.length()) <= maxLen))
  898. {
  899. forceRelative = true;
  900. sourceFile = sourceRel;
  901. }
  902. else
  903. {
  904. this->GlobalGenerator->PathTooLong(this->Target, sf, sourceRel);
  905. }
  906. }
  907. this->ConvertToWindowsSlash(sourceFile);
  908. this->WriteString("<", 2);
  909. (*this->BuildFileStream ) << tool << " Include=\"" << sourceFile << "\"";
  910. if(sf->GetExtension() == "h" &&
  911. this->IsResxHeader(sf->GetFullPath()))
  912. {
  913. (*this->BuildFileStream ) << ">\n";
  914. this->WriteString("<FileType>CppForm</FileType>\n", 3);
  915. this->WriteString("</ClInclude>\n", 2);
  916. }
  917. else
  918. {
  919. (*this->BuildFileStream ) << (end? end : " />\n");
  920. }
  921. ToolSource toolSource = {sf, forceRelative};
  922. this->Tools[tool].push_back(toolSource);
  923. }
  924. void cmVisualStudio10TargetGenerator::WriteSources(
  925. const char* tool, std::vector<cmSourceFile const*> const& sources)
  926. {
  927. for(std::vector<cmSourceFile const*>::const_iterator
  928. si = sources.begin(); si != sources.end(); ++si)
  929. {
  930. this->WriteSource(tool, *si);
  931. }
  932. }
  933. void cmVisualStudio10TargetGenerator::WriteAllSources()
  934. {
  935. if(this->Target->GetType() > cmTarget::UTILITY)
  936. {
  937. return;
  938. }
  939. this->WriteString("<ItemGroup>\n", 1);
  940. std::vector<cmSourceFile const*> headerSources;
  941. this->GeneratorTarget->GetHeaderSources(headerSources, "");
  942. this->WriteSources("ClInclude", headerSources);
  943. std::vector<cmSourceFile const*> idlSources;
  944. this->GeneratorTarget->GetIDLSources(idlSources, "");
  945. this->WriteSources("Midl", idlSources);
  946. std::vector<cmSourceFile const*> objectSources;
  947. this->GeneratorTarget->GetObjectSources(objectSources, "");
  948. for(std::vector<cmSourceFile const*>::const_iterator
  949. si = objectSources.begin();
  950. si != objectSources.end(); ++si)
  951. {
  952. const std::string& lang = (*si)->GetLanguage();
  953. const char* tool = NULL;
  954. if (lang == "C"|| lang == "CXX")
  955. {
  956. tool = "ClCompile";
  957. }
  958. else if (lang == "ASM_NASM" &&
  959. this->GlobalGenerator->IsMasmEnabled())
  960. {
  961. tool = "MASM";
  962. }
  963. else if (lang == "RC")
  964. {
  965. tool = "ResourceCompile";
  966. }
  967. if (tool)
  968. {
  969. this->WriteSource(tool, *si, " ");
  970. if (this->OutputSourceSpecificFlags(*si))
  971. {
  972. this->WriteString("</", 2);
  973. (*this->BuildFileStream ) << tool << ">\n";
  974. }
  975. else
  976. {
  977. (*this->BuildFileStream ) << " />\n";
  978. }
  979. }
  980. else
  981. {
  982. this->WriteSource("None", *si);
  983. }
  984. }
  985. std::vector<cmSourceFile const*> externalObjects;
  986. this->GeneratorTarget->GetExternalObjects(externalObjects, "");
  987. for(std::vector<cmSourceFile const*>::iterator
  988. si = externalObjects.begin();
  989. si != externalObjects.end(); )
  990. {
  991. if (!(*si)->GetObjectLibrary().empty())
  992. {
  993. si = externalObjects.erase(si);
  994. }
  995. else
  996. {
  997. ++si;
  998. }
  999. }
  1000. if(this->LocalGenerator->GetVersion() > cmLocalVisualStudioGenerator::VS10)
  1001. {
  1002. // For VS >= 11 we use LinkObjects to avoid linking custom command
  1003. // outputs. Use Object for all external objects, generated or not.
  1004. this->WriteSources("Object", externalObjects);
  1005. }
  1006. else
  1007. {
  1008. // If an object file is generated in this target, then vs10 will use
  1009. // it in the build, and we have to list it as None instead of Object.
  1010. for(std::vector<cmSourceFile const*>::const_iterator
  1011. si = externalObjects.begin();
  1012. si != externalObjects.end(); ++si)
  1013. {
  1014. std::vector<cmSourceFile*> const* d =
  1015. this->GeneratorTarget->GetSourceDepends(*si);
  1016. this->WriteSource((d && !d->empty())? "None":"Object", *si);
  1017. }
  1018. }
  1019. std::vector<cmSourceFile const*> extraSources;
  1020. this->GeneratorTarget->GetExtraSources(extraSources, "");
  1021. this->WriteSources("None", extraSources);
  1022. // Add object library contents as external objects.
  1023. std::vector<std::string> objs;
  1024. this->GeneratorTarget->UseObjectLibraries(objs, "");
  1025. for(std::vector<std::string>::const_iterator
  1026. oi = objs.begin(); oi != objs.end(); ++oi)
  1027. {
  1028. std::string obj = *oi;
  1029. this->WriteString("<Object Include=\"", 2);
  1030. this->ConvertToWindowsSlash(obj);
  1031. (*this->BuildFileStream ) << obj << "\" />\n";
  1032. }
  1033. this->WriteString("</ItemGroup>\n", 1);
  1034. }
  1035. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  1036. cmSourceFile const* source)
  1037. {
  1038. cmSourceFile const& sf = *source;
  1039. std::string objectName;
  1040. if(this->GeneratorTarget->HasExplicitObjectName(&sf))
  1041. {
  1042. objectName = this->GeneratorTarget->GetObjectName(&sf);
  1043. }
  1044. std::string flags;
  1045. std::string defines;
  1046. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  1047. {
  1048. flags += cflags;
  1049. }
  1050. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  1051. {
  1052. defines += cdefs;
  1053. }
  1054. std::string lang =
  1055. this->GlobalGenerator->GetLanguageFromExtension
  1056. (sf.GetExtension().c_str());
  1057. std::string sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  1058. const std::string& linkLanguage = this->Target->GetLinkerLanguage();
  1059. bool needForceLang = false;
  1060. // source file does not match its extension language
  1061. if(lang != sourceLang)
  1062. {
  1063. needForceLang = true;
  1064. lang = sourceLang;
  1065. }
  1066. // if the source file does not match the linker language
  1067. // then force c or c++
  1068. if(needForceLang || (linkLanguage != lang))
  1069. {
  1070. if(lang == "CXX")
  1071. {
  1072. // force a C++ file type
  1073. flags += " /TP ";
  1074. }
  1075. else if(lang == "C")
  1076. {
  1077. // force to c
  1078. flags += " /TC ";
  1079. }
  1080. }
  1081. bool hasFlags = false;
  1082. // for the first time we need a new line if there is something
  1083. // produced here.
  1084. const char* firstString = ">\n";
  1085. if(objectName.size())
  1086. {
  1087. (*this->BuildFileStream ) << firstString;
  1088. firstString = "";
  1089. hasFlags = true;
  1090. this->WriteString("<ObjectFileName>", 3);
  1091. (*this->BuildFileStream )
  1092. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  1093. }
  1094. std::vector<std::string> *configs =
  1095. static_cast<cmGlobalVisualStudio7Generator *>
  1096. (this->GlobalGenerator)->GetConfigurations();
  1097. for( std::vector<std::string>::iterator config = configs->begin();
  1098. config != configs->end(); ++config)
  1099. {
  1100. std::string configUpper = cmSystemTools::UpperCase(*config);
  1101. std::string configDefines = defines;
  1102. std::string defPropName = "COMPILE_DEFINITIONS_";
  1103. defPropName += configUpper;
  1104. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  1105. {
  1106. if(configDefines.size())
  1107. {
  1108. configDefines += ";";
  1109. }
  1110. configDefines += ccdefs;
  1111. }
  1112. // if we have flags or defines for this config then
  1113. // use them
  1114. if(flags.size() || configDefines.size())
  1115. {
  1116. (*this->BuildFileStream ) << firstString;
  1117. firstString = ""; // only do firstString once
  1118. hasFlags = true;
  1119. cmVisualStudioGeneratorOptions
  1120. clOptions(this->LocalGenerator,
  1121. cmVisualStudioGeneratorOptions::Compiler,
  1122. cmVSGetCLFlagTable(this->LocalGenerator), 0, this);
  1123. clOptions.Parse(flags.c_str());
  1124. clOptions.AddDefines(configDefines.c_str());
  1125. clOptions.SetConfiguration((*config).c_str());
  1126. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1127. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1128. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  1129. " ", "\n", lang);
  1130. }
  1131. }
  1132. return hasFlags;
  1133. }
  1134. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  1135. {
  1136. cmTarget::TargetType ttype = this->Target->GetType();
  1137. if(ttype > cmTarget::GLOBAL_TARGET)
  1138. {
  1139. return;
  1140. }
  1141. this->WriteString("<PropertyGroup>\n", 2);
  1142. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  1143. "</_ProjectFileVersion>\n", 3);
  1144. std::vector<std::string> *configs =
  1145. static_cast<cmGlobalVisualStudio7Generator *>
  1146. (this->GlobalGenerator)->GetConfigurations();
  1147. for(std::vector<std::string>::iterator config = configs->begin();
  1148. config != configs->end(); ++config)
  1149. {
  1150. if(ttype >= cmTarget::UTILITY)
  1151. {
  1152. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1153. *this->BuildFileStream
  1154. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  1155. << "</IntDir>\n";
  1156. }
  1157. else
  1158. {
  1159. std::string intermediateDir = this->LocalGenerator->
  1160. GetTargetDirectory(*this->Target);
  1161. intermediateDir += "/";
  1162. intermediateDir += *config;
  1163. intermediateDir += "/";
  1164. std::string outDir;
  1165. std::string targetNameFull;
  1166. if(ttype == cmTarget::OBJECT_LIBRARY)
  1167. {
  1168. outDir = intermediateDir;
  1169. targetNameFull = this->Target->GetName();
  1170. targetNameFull += ".lib";
  1171. }
  1172. else
  1173. {
  1174. outDir = this->Target->GetDirectory(config->c_str()) + "/";
  1175. targetNameFull = this->Target->GetFullName(config->c_str());
  1176. }
  1177. this->ConvertToWindowsSlash(intermediateDir);
  1178. this->ConvertToWindowsSlash(outDir);
  1179. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  1180. *this->BuildFileStream << outDir
  1181. << "</OutDir>\n";
  1182. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  1183. *this->BuildFileStream << intermediateDir
  1184. << "</IntDir>\n";
  1185. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  1186. *this->BuildFileStream
  1187. << cmSystemTools::GetFilenameWithoutLastExtension(
  1188. targetNameFull.c_str())
  1189. << "</TargetName>\n";
  1190. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  1191. *this->BuildFileStream
  1192. << cmSystemTools::GetFilenameLastExtension(targetNameFull.c_str())
  1193. << "</TargetExt>\n";
  1194. this->OutputLinkIncremental(*config);
  1195. }
  1196. }
  1197. this->WriteString("</PropertyGroup>\n", 2);
  1198. }
  1199. void
  1200. cmVisualStudio10TargetGenerator::
  1201. OutputLinkIncremental(std::string const& configName)
  1202. {
  1203. // static libraries and things greater than modules do not need
  1204. // to set this option
  1205. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1206. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1207. {
  1208. return;
  1209. }
  1210. Options& linkOptions = *(this->LinkOptions[configName]);
  1211. const char* incremental = linkOptions.GetFlag("LinkIncremental");
  1212. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  1213. *this->BuildFileStream << (incremental?incremental:"true")
  1214. << "</LinkIncremental>\n";
  1215. linkOptions.RemoveFlag("LinkIncremental");
  1216. const char* manifest = linkOptions.GetFlag("GenerateManifest");
  1217. this->WritePlatformConfigTag("GenerateManifest", configName.c_str(), 3);
  1218. *this->BuildFileStream << (manifest?manifest:"true")
  1219. << "</GenerateManifest>\n";
  1220. linkOptions.RemoveFlag("GenerateManifest");
  1221. // Some link options belong here. Use them now and remove them so that
  1222. // WriteLinkOptions does not use them.
  1223. const char* flags[] = {
  1224. "LinkDelaySign",
  1225. "LinkKeyFile",
  1226. 0};
  1227. for(const char** f = flags; *f; ++f)
  1228. {
  1229. const char* flag = *f;
  1230. if(const char* value = linkOptions.GetFlag(flag))
  1231. {
  1232. this->WritePlatformConfigTag(flag, configName.c_str(), 3);
  1233. *this->BuildFileStream << value << "</" << flag << ">\n";
  1234. linkOptions.RemoveFlag(flag);
  1235. }
  1236. }
  1237. }
  1238. //----------------------------------------------------------------------------
  1239. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  1240. {
  1241. std::vector<std::string> const* configs =
  1242. this->GlobalGenerator->GetConfigurations();
  1243. for(std::vector<std::string>::const_iterator i = configs->begin();
  1244. i != configs->end(); ++i)
  1245. {
  1246. if(!this->ComputeClOptions(*i))
  1247. {
  1248. return false;
  1249. }
  1250. }
  1251. return true;
  1252. }
  1253. //----------------------------------------------------------------------------
  1254. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  1255. std::string const& configName)
  1256. {
  1257. // much of this was copied from here:
  1258. // copied from cmLocalVisualStudio7Generator.cxx 805
  1259. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  1260. cmsys::auto_ptr<Options> pOptions(
  1261. new Options(this->LocalGenerator, Options::Compiler,
  1262. cmVSGetCLFlagTable(this->LocalGenerator)));
  1263. Options& clOptions = *pOptions;
  1264. std::string flags;
  1265. // collect up flags for
  1266. if(this->Target->GetType() < cmTarget::UTILITY)
  1267. {
  1268. const std::string& linkLanguage =
  1269. this->Target->GetLinkerLanguage(configName.c_str());
  1270. if(linkLanguage.empty())
  1271. {
  1272. cmSystemTools::Error
  1273. ("CMake can not determine linker language for target: ",
  1274. this->Name.c_str());
  1275. return false;
  1276. }
  1277. if(linkLanguage == "C" || linkLanguage == "CXX"
  1278. || linkLanguage == "Fortran")
  1279. {
  1280. std::string baseFlagVar = "CMAKE_";
  1281. baseFlagVar += linkLanguage;
  1282. baseFlagVar += "_FLAGS";
  1283. flags = this->
  1284. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  1285. std::string flagVar = baseFlagVar + std::string("_") +
  1286. cmSystemTools::UpperCase(configName);
  1287. flags += " ";
  1288. flags += this->
  1289. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  1290. }
  1291. // set the correct language
  1292. if(linkLanguage == "C")
  1293. {
  1294. flags += " /TC ";
  1295. }
  1296. if(linkLanguage == "CXX")
  1297. {
  1298. flags += " /TP ";
  1299. }
  1300. this->LocalGenerator->AddCompileOptions(flags, this->Target,
  1301. linkLanguage, configName.c_str());
  1302. }
  1303. // Get preprocessor definitions for this directory.
  1304. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1305. clOptions.FixExceptionHandlingDefault();
  1306. clOptions.AddFlag("PrecompiledHeader", "NotUsing");
  1307. std::string asmLocation = configName + "/";
  1308. clOptions.AddFlag("AssemblerListingLocation", asmLocation.c_str());
  1309. clOptions.Parse(flags.c_str());
  1310. clOptions.Parse(defineFlags.c_str());
  1311. std::vector<std::string> targetDefines;
  1312. this->Target->GetCompileDefinitions(targetDefines, configName.c_str());
  1313. clOptions.AddDefines(targetDefines);
  1314. clOptions.SetVerboseMakefile(
  1315. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1316. // Add a definition for the configuration name.
  1317. std::string configDefine = "CMAKE_INTDIR=\"";
  1318. configDefine += configName;
  1319. configDefine += "\"";
  1320. clOptions.AddDefine(configDefine);
  1321. if(const char* exportMacro = this->Target->GetExportMacro())
  1322. {
  1323. clOptions.AddDefine(exportMacro);
  1324. }
  1325. this->ClOptions[configName] = pOptions.release();
  1326. return true;
  1327. }
  1328. //----------------------------------------------------------------------------
  1329. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1330. std::string const& configName,
  1331. std::vector<std::string> const& includes)
  1332. {
  1333. Options& clOptions = *(this->ClOptions[configName]);
  1334. this->WriteString("<ClCompile>\n", 2);
  1335. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1336. this->OutputIncludes(includes);
  1337. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1338. // If not in debug mode, write the DebugInformationFormat field
  1339. // without value so PDBs don't get generated uselessly.
  1340. if(!clOptions.IsDebug())
  1341. {
  1342. this->WriteString("<DebugInformationFormat>"
  1343. "</DebugInformationFormat>\n", 3);
  1344. }
  1345. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1346. "\n", "CXX");
  1347. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1348. // Specify the compiler program database file if configured.
  1349. std::string pdb = this->Target->GetCompilePDBPath(configName.c_str());
  1350. if(!pdb.empty())
  1351. {
  1352. this->ConvertToWindowsSlash(pdb);
  1353. this->WriteString("<ProgramDataBaseFileName>", 3);
  1354. *this->BuildFileStream << cmVS10EscapeXML(pdb)
  1355. << "</ProgramDataBaseFileName>\n";
  1356. }
  1357. this->WriteString("</ClCompile>\n", 2);
  1358. }
  1359. void cmVisualStudio10TargetGenerator::
  1360. OutputIncludes(std::vector<std::string> const & includes)
  1361. {
  1362. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1363. for(std::vector<std::string>::const_iterator i = includes.begin();
  1364. i != includes.end(); ++i)
  1365. {
  1366. std::string incDir = *i;
  1367. this->ConvertToWindowsSlash(incDir);
  1368. *this->BuildFileStream << cmVS10EscapeXML(incDir) << ";";
  1369. }
  1370. this->WriteString("%(AdditionalIncludeDirectories)"
  1371. "</AdditionalIncludeDirectories>\n", 0);
  1372. }
  1373. void cmVisualStudio10TargetGenerator::
  1374. WriteRCOptions(std::string const& configName,
  1375. std::vector<std::string> const & includes)
  1376. {
  1377. this->WriteString("<ResourceCompile>\n", 2);
  1378. Options& clOptions = *(this->ClOptions[configName]);
  1379. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1380. "\n", "RC");
  1381. this->OutputIncludes(includes);
  1382. this->WriteString("</ResourceCompile>\n", 2);
  1383. }
  1384. void
  1385. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1386. {
  1387. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1388. {
  1389. return;
  1390. }
  1391. std::string libflags;
  1392. this->LocalGenerator->GetStaticLibraryFlags(libflags,
  1393. cmSystemTools::UpperCase(config), this->Target);
  1394. if(!libflags.empty())
  1395. {
  1396. this->WriteString("<Lib>\n", 2);
  1397. cmVisualStudioGeneratorOptions
  1398. libOptions(this->LocalGenerator,
  1399. cmVisualStudioGeneratorOptions::Linker,
  1400. cmVSGetLibFlagTable(this->LocalGenerator), 0, this);
  1401. libOptions.Parse(libflags.c_str());
  1402. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1403. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1404. this->WriteString("</Lib>\n", 2);
  1405. }
  1406. }
  1407. //----------------------------------------------------------------------------
  1408. bool cmVisualStudio10TargetGenerator::ComputeLinkOptions()
  1409. {
  1410. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  1411. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  1412. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1413. {
  1414. std::vector<std::string> const* configs =
  1415. this->GlobalGenerator->GetConfigurations();
  1416. for(std::vector<std::string>::const_iterator i = configs->begin();
  1417. i != configs->end(); ++i)
  1418. {
  1419. if(!this->ComputeLinkOptions(*i))
  1420. {
  1421. return false;
  1422. }
  1423. }
  1424. }
  1425. return true;
  1426. }
  1427. //----------------------------------------------------------------------------
  1428. bool
  1429. cmVisualStudio10TargetGenerator::ComputeLinkOptions(std::string const& config)
  1430. {
  1431. cmsys::auto_ptr<Options> pOptions(
  1432. new Options(this->LocalGenerator, Options::Linker,
  1433. cmVSGetLinkFlagTable(this->LocalGenerator), 0, this));
  1434. Options& linkOptions = *pOptions;
  1435. const std::string& linkLanguage =
  1436. this->Target->GetLinkerLanguage(config.c_str());
  1437. if(linkLanguage.empty())
  1438. {
  1439. cmSystemTools::Error
  1440. ("CMake can not determine linker language for target: ",
  1441. this->Name.c_str());
  1442. return false;
  1443. }
  1444. std::string CONFIG = cmSystemTools::UpperCase(config);
  1445. const char* linkType = "SHARED";
  1446. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1447. {
  1448. linkType = "MODULE";
  1449. }
  1450. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1451. {
  1452. linkType = "EXE";
  1453. }
  1454. std::string stackVar = "CMAKE_";
  1455. stackVar += linkLanguage;
  1456. stackVar += "_STACK_SIZE";
  1457. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  1458. std::string flags;
  1459. if(stackVal)
  1460. {
  1461. flags += " /STACK:";
  1462. flags += stackVal;
  1463. }
  1464. std::string linkFlagVarBase = "CMAKE_";
  1465. linkFlagVarBase += linkType;
  1466. linkFlagVarBase += "_LINKER_FLAGS";
  1467. flags += " ";
  1468. flags += this->
  1469. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1470. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1471. flags += " ";
  1472. flags += this->
  1473. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1474. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1475. if(targetLinkFlags)
  1476. {
  1477. flags += " ";
  1478. flags += targetLinkFlags;
  1479. }
  1480. std::string flagsProp = "LINK_FLAGS_";
  1481. flagsProp += CONFIG;
  1482. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1483. {
  1484. flags += " ";
  1485. flags += flagsConfig;
  1486. }
  1487. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1488. {
  1489. linkOptions.AddFlag("SubSystem", "Windows");
  1490. }
  1491. else
  1492. {
  1493. linkOptions.AddFlag("SubSystem", "Console");
  1494. }
  1495. std::string standardLibsVar = "CMAKE_";
  1496. standardLibsVar += linkLanguage;
  1497. standardLibsVar += "_STANDARD_LIBRARIES";
  1498. std::string
  1499. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1500. // Remove trailing spaces from libs
  1501. std::string::size_type pos = libs.size()-1;
  1502. if(libs.size() != 0)
  1503. {
  1504. while(libs[pos] == ' ')
  1505. {
  1506. pos--;
  1507. }
  1508. }
  1509. if(pos != libs.size()-1)
  1510. {
  1511. libs = libs.substr(0, pos+1);
  1512. }
  1513. // Replace spaces in libs with ;
  1514. cmSystemTools::ReplaceString(libs, " ", ";");
  1515. cmComputeLinkInformation* pcli =
  1516. this->Target->GetLinkInformation(config.c_str());
  1517. if(!pcli)
  1518. {
  1519. cmSystemTools::Error
  1520. ("CMake can not compute cmComputeLinkInformation for target: ",
  1521. this->Name.c_str());
  1522. return false;
  1523. }
  1524. // add the libraries for the target to libs string
  1525. cmComputeLinkInformation& cli = *pcli;
  1526. this->AddLibraries(cli, libs);
  1527. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1528. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1529. const char* sep = "";
  1530. std::string linkDirs;
  1531. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1532. d != ldirs.end(); ++d)
  1533. {
  1534. // first just full path
  1535. linkDirs += sep;
  1536. linkDirs += *d;
  1537. sep = ";";
  1538. linkDirs += sep;
  1539. // next path with configuration type Debug, Release, etc
  1540. linkDirs += *d;
  1541. linkDirs += "/$(Configuration)";
  1542. linkDirs += sep;
  1543. }
  1544. linkDirs += "%(AdditionalLibraryDirectories)";
  1545. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1546. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1547. linkOptions.AddFlag("Version", "");
  1548. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1549. {
  1550. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1551. }
  1552. else
  1553. {
  1554. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1555. }
  1556. std::string targetName;
  1557. std::string targetNameSO;
  1558. std::string targetNameFull;
  1559. std::string targetNameImport;
  1560. std::string targetNamePDB;
  1561. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1562. {
  1563. this->Target->GetExecutableNames(targetName, targetNameFull,
  1564. targetNameImport, targetNamePDB,
  1565. config.c_str());
  1566. }
  1567. else
  1568. {
  1569. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1570. targetNameImport, targetNamePDB,
  1571. config.c_str());
  1572. }
  1573. std::string pdb = this->Target->GetPDBDirectory(config.c_str());
  1574. pdb += "/";
  1575. pdb += targetNamePDB;
  1576. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1577. imLib += "/";
  1578. imLib += targetNameImport;
  1579. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1580. linkOptions.AddFlag("ProgramDataBaseFile", pdb.c_str());
  1581. linkOptions.Parse(flags.c_str());
  1582. std::string def = this->GeneratorTarget->GetModuleDefinitionFile("");
  1583. if(!def.empty())
  1584. {
  1585. linkOptions.AddFlag("ModuleDefinitionFile", def.c_str());
  1586. }
  1587. this->LinkOptions[config] = pOptions.release();
  1588. return true;
  1589. }
  1590. //----------------------------------------------------------------------------
  1591. void
  1592. cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const& config)
  1593. {
  1594. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1595. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1596. {
  1597. return;
  1598. }
  1599. Options& linkOptions = *(this->LinkOptions[config]);
  1600. this->WriteString("<Link>\n", 2);
  1601. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1602. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1603. this->WriteString("</Link>\n", 2);
  1604. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1605. {
  1606. this->WriteString("<ProjectReference>\n", 2);
  1607. this->WriteString(
  1608. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1609. this->WriteString("</ProjectReference>\n", 2);
  1610. }
  1611. }
  1612. void cmVisualStudio10TargetGenerator::AddLibraries(
  1613. cmComputeLinkInformation& cli,
  1614. std::string& libstring)
  1615. {
  1616. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1617. ItemVector libs = cli.GetItems();
  1618. const char* sep = ";";
  1619. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1620. {
  1621. if(l->IsPath)
  1622. {
  1623. std::string path = this->LocalGenerator->
  1624. Convert(l->Value.c_str(),
  1625. cmLocalGenerator::START_OUTPUT,
  1626. cmLocalGenerator::UNCHANGED);
  1627. this->ConvertToWindowsSlash(path);
  1628. libstring += sep;
  1629. libstring += path;
  1630. }
  1631. else if (!l->Target
  1632. || l->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  1633. {
  1634. libstring += sep;
  1635. libstring += l->Value;
  1636. }
  1637. }
  1638. }
  1639. void cmVisualStudio10TargetGenerator::
  1640. WriteMidlOptions(std::string const& /*config*/,
  1641. std::vector<std::string> const & includes)
  1642. {
  1643. // This processes *any* of the .idl files specified in the project's file
  1644. // list (and passed as the item metadata %(Filename) expressing the rule
  1645. // input filename) into output files at the per-config *build* dir
  1646. // ($(IntDir)) each.
  1647. //
  1648. // IOW, this MIDL section is intended to provide a fully generic syntax
  1649. // content suitable for most cases (read: if you get errors, then it's quite
  1650. // probable that the error is on your side of the .idl setup).
  1651. //
  1652. // Also, note that the marked-as-generated _i.c file in the Visual Studio
  1653. // generator case needs to be referred to as $(IntDir)\foo_i.c at the
  1654. // project's file list, otherwise the compiler-side processing won't pick it
  1655. // up (for non-directory form, it ends up looking in project binary dir
  1656. // only). Perhaps there's something to be done to make this more automatic
  1657. // on the CMake side?
  1658. this->WriteString("<Midl>\n", 2);
  1659. this->OutputIncludes(includes);
  1660. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1661. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1662. this->WriteString(
  1663. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1664. this->WriteString(
  1665. "<InterfaceIdentifierFileName>"
  1666. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1667. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1668. this->WriteString("</Midl>\n", 2);
  1669. }
  1670. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1671. {
  1672. std::vector<std::string> *configs =
  1673. static_cast<cmGlobalVisualStudio7Generator *>
  1674. (this->GlobalGenerator)->GetConfigurations();
  1675. for(std::vector<std::string>::iterator i = configs->begin();
  1676. i != configs->end(); ++i)
  1677. {
  1678. std::vector<std::string> includes;
  1679. this->LocalGenerator->GetIncludeDirectories(includes,
  1680. this->GeneratorTarget,
  1681. "C", i->c_str());
  1682. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1683. *this->BuildFileStream << "\n";
  1684. // output cl compile flags <ClCompile></ClCompile>
  1685. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  1686. {
  1687. this->WriteClOptions(*i, includes);
  1688. // output rc compile flags <ResourceCompile></ResourceCompile>
  1689. this->WriteRCOptions(*i, includes);
  1690. }
  1691. // output midl flags <Midl></Midl>
  1692. this->WriteMidlOptions(*i, includes);
  1693. // write events
  1694. this->WriteEvents(*i);
  1695. // output link flags <Link></Link>
  1696. this->WriteLinkOptions(*i);
  1697. // output lib flags <Lib></Lib>
  1698. this->WriteLibOptions(*i);
  1699. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1700. }
  1701. }
  1702. void
  1703. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1704. {
  1705. this->WriteEvent("PreLinkEvent",
  1706. this->Target->GetPreLinkCommands(), configName);
  1707. this->WriteEvent("PreBuildEvent",
  1708. this->Target->GetPreBuildCommands(), configName);
  1709. this->WriteEvent("PostBuildEvent",
  1710. this->Target->GetPostBuildCommands(), configName);
  1711. }
  1712. void cmVisualStudio10TargetGenerator::WriteEvent(
  1713. const char* name,
  1714. std::vector<cmCustomCommand> const& commands,
  1715. std::string const& configName)
  1716. {
  1717. if(commands.size() == 0)
  1718. {
  1719. return;
  1720. }
  1721. this->WriteString("<", 2);
  1722. (*this->BuildFileStream ) << name << ">\n";
  1723. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1724. std::string script;
  1725. const char* pre = "";
  1726. std::string comment;
  1727. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1728. i != commands.end(); ++i)
  1729. {
  1730. cmCustomCommandGenerator ccg(*i, configName, this->Makefile);
  1731. comment += pre;
  1732. comment += lg->ConstructComment(ccg);
  1733. script += pre;
  1734. pre = "\n";
  1735. script += cmVS10EscapeXML(lg->ConstructScript(ccg));
  1736. }
  1737. comment = cmVS10EscapeComment(comment);
  1738. this->WriteString("<Message>",3);
  1739. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1740. this->WriteString("<Command>", 3);
  1741. (*this->BuildFileStream ) << script;
  1742. (*this->BuildFileStream ) << "</Command>" << "\n";
  1743. this->WriteString("</", 2);
  1744. (*this->BuildFileStream ) << name << ">\n";
  1745. }
  1746. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1747. {
  1748. cmGlobalGenerator::TargetDependSet const& unordered
  1749. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1750. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1751. OrderedTargetDependSet;
  1752. OrderedTargetDependSet depends(unordered);
  1753. this->WriteString("<ItemGroup>\n", 1);
  1754. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1755. i != depends.end(); ++i)
  1756. {
  1757. cmTarget const* dt = *i;
  1758. if(dt->GetType() == cmTarget::INTERFACE_LIBRARY)
  1759. {
  1760. continue;
  1761. }
  1762. // skip fortran targets as they can not be processed by MSBuild
  1763. // the only reference will be in the .sln file
  1764. if(static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
  1765. ->TargetIsFortranOnly(*dt))
  1766. {
  1767. continue;
  1768. }
  1769. this->WriteString("<ProjectReference Include=\"", 2);
  1770. cmMakefile* mf = dt->GetMakefile();
  1771. std::string name = dt->GetName();
  1772. std::string path;
  1773. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1774. if(p)
  1775. {
  1776. path = p;
  1777. }
  1778. else
  1779. {
  1780. path = mf->GetStartOutputDirectory();
  1781. path += "/";
  1782. path += dt->GetName();
  1783. path += ".vcxproj";
  1784. }
  1785. (*this->BuildFileStream) << path << "\">\n";
  1786. this->WriteString("<Project>", 3);
  1787. (*this->BuildFileStream)
  1788. << this->GlobalGenerator->GetGUID(name.c_str())
  1789. << "</Project>\n";
  1790. this->WriteString("</ProjectReference>\n", 2);
  1791. }
  1792. this->WriteString("</ItemGroup>\n", 1);
  1793. }
  1794. bool cmVisualStudio10TargetGenerator::
  1795. IsResxHeader(const std::string& headerFile)
  1796. {
  1797. std::set<std::string> expectedResxHeaders;
  1798. this->GeneratorTarget->GetExpectedResxHeaders(expectedResxHeaders, "");
  1799. std::set<std::string>::const_iterator it =
  1800. expectedResxHeaders.find(headerFile);
  1801. return it != expectedResxHeaders.end();
  1802. }