cmVisualStudio10TargetGenerator.cxx 62 KB

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