cmVisualStudio10TargetGenerator.cxx 62 KB

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