cmVisualStudio10TargetGenerator.cxx 59 KB

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