cmVisualStudio10TargetGenerator.cxx 63 KB

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