cmVisualStudio10TargetGenerator.cxx 62 KB

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