cmVisualStudio10TargetGenerator.cxx 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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 "cmTarget.h"
  13. #include "cmComputeLinkInformation.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmMakefile.h"
  16. #include "cmSourceFile.h"
  17. #include "cmVisualStudioGeneratorOptions.h"
  18. #include "cmLocalVisualStudio7Generator.h"
  19. #include "cmVS10CLFlagTable.h"
  20. #include "cmVS10LinkFlagTable.h"
  21. #include "cmVS10LibFlagTable.h"
  22. #include <cmsys/auto_ptr.hxx>
  23. static std::string cmVS10EscapeXML(std::string arg)
  24. {
  25. cmSystemTools::ReplaceString(arg, "&", "&amp;");
  26. cmSystemTools::ReplaceString(arg, "<", "&lt;");
  27. cmSystemTools::ReplaceString(arg, ">", "&gt;");
  28. return arg;
  29. }
  30. static std::string cmVS10EscapeComment(std::string comment)
  31. {
  32. // MSBuild takes the CDATA of a <Message></Message> element and just
  33. // does "echo $CDATA" with no escapes. We must encode the string.
  34. // http://technet.microsoft.com/en-us/library/cc772462%28WS.10%29.aspx
  35. std::string echoable;
  36. for(std::string::iterator c = comment.begin(); c != comment.end(); ++c)
  37. {
  38. switch (*c)
  39. {
  40. case '\r': break;
  41. case '\n': echoable += '\t'; break;
  42. case '"': /* no break */
  43. case '|': /* no break */
  44. case '&': /* no break */
  45. case '<': /* no break */
  46. case '>': /* no break */
  47. case '^': echoable += '^'; /* no break */
  48. default: echoable += *c; break;
  49. }
  50. }
  51. return echoable;
  52. }
  53. cmVisualStudio10TargetGenerator::
  54. cmVisualStudio10TargetGenerator(cmTarget* target,
  55. cmGlobalVisualStudio10Generator* gg)
  56. {
  57. this->GlobalGenerator = gg;
  58. this->Target = target;
  59. this->Makefile = target->GetMakefile();
  60. this->LocalGenerator =
  61. (cmLocalVisualStudio7Generator*)
  62. this->Makefile->GetLocalGenerator();
  63. this->Name = this->Target->GetName();
  64. this->GlobalGenerator->CreateGUID(this->Name.c_str());
  65. this->GUID = this->GlobalGenerator->GetGUID(this->Name.c_str());
  66. this->Platform = gg->GetPlatformName();
  67. this->ComputeObjectNames();
  68. this->BuildFileStream = 0;
  69. }
  70. cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
  71. {
  72. for(OptionsMap::iterator i = this->ClOptions.begin();
  73. i != this->ClOptions.end(); ++i)
  74. {
  75. delete i->second;
  76. }
  77. if(!this->BuildFileStream)
  78. {
  79. return;
  80. }
  81. if (this->BuildFileStream->Close())
  82. {
  83. this->GlobalGenerator
  84. ->FileReplacedDuringGenerate(this->PathToVcxproj);
  85. }
  86. delete this->BuildFileStream;
  87. }
  88. void cmVisualStudio10TargetGenerator::WritePlatformConfigTag(
  89. const char* tag,
  90. const char* config,
  91. int indentLevel,
  92. const char* attribute,
  93. const char* end,
  94. std::ostream* stream)
  95. {
  96. if(!stream)
  97. {
  98. stream = this->BuildFileStream;
  99. }
  100. stream->fill(' ');
  101. stream->width(indentLevel*2 );
  102. (*stream ) << "";
  103. (*stream ) << "<" << tag
  104. << " Condition=\"'$(Configuration)|$(Platform)'=='";
  105. (*stream ) << config << "|" << this->Platform << "'\"";
  106. if(attribute)
  107. {
  108. (*stream ) << attribute;
  109. }
  110. // close the tag
  111. (*stream ) << ">";
  112. if(end)
  113. {
  114. (*stream ) << end;
  115. }
  116. }
  117. void cmVisualStudio10TargetGenerator::WriteString(const char* line,
  118. int indentLevel)
  119. {
  120. this->BuildFileStream->fill(' ');
  121. this->BuildFileStream->width(indentLevel*2 );
  122. // write an empty string to get the fill level indent to print
  123. (*this->BuildFileStream ) << "";
  124. (*this->BuildFileStream ) << line;
  125. }
  126. #define VS10_USER_PROPS "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props"
  127. void cmVisualStudio10TargetGenerator::Generate()
  128. {
  129. // do not generate external ms projects
  130. if(this->Target->GetProperty("EXTERNAL_MSPROJECT"))
  131. {
  132. return;
  133. }
  134. // Tell the global generator the name of the project file
  135. this->Target->SetProperty("GENERATOR_FILE_NAME",this->Name.c_str());
  136. this->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
  137. ".vcxproj");
  138. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  139. {
  140. if(!this->ComputeClOptions())
  141. {
  142. return;
  143. }
  144. }
  145. cmMakefile* mf = this->Target->GetMakefile();
  146. std::string path = mf->GetStartOutputDirectory();
  147. path += "/";
  148. path += this->Name;
  149. path += ".vcxproj";
  150. this->BuildFileStream =
  151. new cmGeneratedFileStream(path.c_str());
  152. this->PathToVcxproj = path;
  153. this->BuildFileStream->SetCopyIfDifferent(true);
  154. // Write the encoding header into the file
  155. char magic[] = {0xEF,0xBB, 0xBF};
  156. this->BuildFileStream->write(magic, 3);
  157. this->WriteString("<Project DefaultTargets=\"Build\" "
  158. "ToolsVersion=\"4.0\" "
  159. "xmlns=\"http://schemas.microsoft.com/"
  160. "developer/msbuild/2003\">\n",
  161. 0);
  162. this->WriteProjectConfigurations();
  163. this->WriteString("<PropertyGroup Label=\"Globals\">\n", 1);
  164. this->WriteString("<ProjectGUID>", 2);
  165. (*this->BuildFileStream) << "{" << this->GUID << "}</ProjectGUID>\n";
  166. this->WriteString("<SccProjectName />\n", 2);
  167. this->WriteString("<SccLocalPath />\n", 2);
  168. this->WriteString("<Keyword>Win32Proj</Keyword>\n", 2);
  169. this->WriteString("<Platform>", 2);
  170. (*this->BuildFileStream) << this->Platform << "</Platform>\n";
  171. const char* projLabel = this->Target->GetProperty("PROJECT_LABEL");
  172. if(!projLabel)
  173. {
  174. projLabel = this->Name.c_str();
  175. }
  176. this->WriteString("<ProjectName>", 2);
  177. (*this->BuildFileStream) << projLabel << "</ProjectName>\n";
  178. this->WriteString("</PropertyGroup>\n", 1);
  179. this->WriteString("<Import Project="
  180. "\"$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />\n",
  181. 1);
  182. this->WriteProjectConfigurationValues();
  183. this->WriteString(
  184. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.props\" />\n", 1);
  185. this->WriteString("<ImportGroup Label=\"ExtensionSettings\">\n", 1);
  186. this->WriteString("</ImportGroup>\n", 1);
  187. this->WriteString("<ImportGroup Label=\"PropertySheets\">\n", 1);
  188. this->WriteString("<Import Project=\"" VS10_USER_PROPS "\""
  189. " Condition=\"exists('" VS10_USER_PROPS "')\""
  190. " Label=\"LocalAppDataPlatform\" />", 2);
  191. this->WriteString("</ImportGroup>\n", 1);
  192. this->WriteString("<PropertyGroup Label=\"UserMacros\" />\n", 1);
  193. this->WritePathAndIncrementalLinkOptions();
  194. this->WriteItemDefinitionGroups();
  195. this->WriteCustomCommands();
  196. this->WriteObjSources();
  197. this->WriteCLSources();
  198. this->WriteProjectReferences();
  199. this->WriteString(
  200. "<Import Project=\"$(VCTargetsPath)\\Microsoft.Cpp.targets\""
  201. " />\n", 1);
  202. this->WriteString("<ImportGroup Label=\"ExtensionTargets\">\n", 1);
  203. this->WriteString("</ImportGroup>\n", 1);
  204. this->WriteString("</Project>", 0);
  205. // The groups are stored in a separate file for VS 10
  206. this->WriteGroups();
  207. }
  208. // ConfigurationType Application, Utility StaticLibrary DynamicLibrary
  209. void cmVisualStudio10TargetGenerator::WriteProjectConfigurations()
  210. {
  211. this->WriteString("<ItemGroup Label=\"ProjectConfigurations\">\n", 1);
  212. std::vector<std::string> *configs =
  213. static_cast<cmGlobalVisualStudio7Generator *>
  214. (this->GlobalGenerator)->GetConfigurations();
  215. for(std::vector<std::string>::iterator i = configs->begin();
  216. i != configs->end(); ++i)
  217. {
  218. this->WriteString("<ProjectConfiguration Include=\"", 2);
  219. (*this->BuildFileStream ) << *i << "|" << this->Platform << "\">\n";
  220. this->WriteString("<Configuration>", 3);
  221. (*this->BuildFileStream ) << *i << "</Configuration>\n";
  222. this->WriteString("<Platform>", 3);
  223. (*this->BuildFileStream) << this->Platform << "</Platform>\n";
  224. this->WriteString("</ProjectConfiguration>\n", 2);
  225. }
  226. this->WriteString("</ItemGroup>\n", 1);
  227. }
  228. void cmVisualStudio10TargetGenerator::WriteProjectConfigurationValues()
  229. {
  230. cmGlobalVisualStudio10Generator* gg =
  231. static_cast<cmGlobalVisualStudio10Generator*>(this->GlobalGenerator);
  232. std::vector<std::string> *configs =
  233. static_cast<cmGlobalVisualStudio7Generator *>
  234. (this->GlobalGenerator)->GetConfigurations();
  235. for(std::vector<std::string>::iterator i = configs->begin();
  236. i != configs->end(); ++i)
  237. {
  238. this->WritePlatformConfigTag("PropertyGroup",
  239. i->c_str(),
  240. 1, " Label=\"Configuration\"", "\n");
  241. std::string configType = "<ConfigurationType>";
  242. switch(this->Target->GetType())
  243. {
  244. case cmTarget::SHARED_LIBRARY:
  245. case cmTarget::MODULE_LIBRARY:
  246. configType += "DynamicLibrary";
  247. break;
  248. case cmTarget::STATIC_LIBRARY:
  249. configType += "StaticLibrary";
  250. break;
  251. case cmTarget::EXECUTABLE:
  252. configType += "Application";
  253. break;
  254. case cmTarget::UTILITY:
  255. configType += "Utility";
  256. break;
  257. }
  258. configType += "</ConfigurationType>\n";
  259. this->WriteString(configType.c_str(), 2);
  260. const char* mfcFlag =
  261. this->Target->GetMakefile()->GetDefinition("CMAKE_MFC_FLAG");
  262. if(mfcFlag)
  263. {
  264. this->WriteString("<UseOfMfc>true</UseOfMfc>\n", 2);
  265. }
  266. else
  267. {
  268. this->WriteString("<UseOfMfc>false</UseOfMfc>\n", 2);
  269. }
  270. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY &&
  271. this->ClOptions[*i]->UsingUnicode())
  272. {
  273. this->WriteString("<CharacterSet>Unicode</CharacterSet>\n", 2);
  274. }
  275. else
  276. {
  277. this->WriteString("<CharacterSet>MultiByte</CharacterSet>\n", 2);
  278. }
  279. if(const char* toolset = gg->GetPlatformToolset())
  280. {
  281. std::string pts = "<PlatformToolset>";
  282. pts += toolset;
  283. pts += "</PlatformToolset>\n";
  284. this->WriteString(pts.c_str(), 2);
  285. }
  286. this->WriteString("</PropertyGroup>\n", 1);
  287. }
  288. }
  289. void cmVisualStudio10TargetGenerator::WriteCustomCommands()
  290. {
  291. this->SourcesVisited.clear();
  292. std::vector<cmSourceFile*> const& sources = this->Target->GetSourceFiles();
  293. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  294. source != sources.end(); ++source)
  295. {
  296. cmSourceFile* sf = *source;
  297. this->WriteCustomCommand(sf);
  298. }
  299. }
  300. //----------------------------------------------------------------------------
  301. void cmVisualStudio10TargetGenerator::WriteCustomCommand(cmSourceFile* sf)
  302. {
  303. if(this->SourcesVisited.insert(sf).second)
  304. {
  305. if(std::vector<cmSourceFile*> const* depends =
  306. this->Target->GetSourceDepends(sf))
  307. {
  308. for(std::vector<cmSourceFile*>::const_iterator di = depends->begin();
  309. di != depends->end(); ++di)
  310. {
  311. this->WriteCustomCommand(*di);
  312. }
  313. }
  314. if(cmCustomCommand const* command = sf->GetCustomCommand())
  315. {
  316. this->WriteString("<ItemGroup>\n", 1);
  317. this->WriteCustomRule(sf, *command);
  318. this->WriteString("</ItemGroup>\n", 1);
  319. }
  320. }
  321. }
  322. void
  323. cmVisualStudio10TargetGenerator::WriteCustomRule(cmSourceFile* source,
  324. cmCustomCommand const &
  325. command)
  326. {
  327. std::string sourcePath = source->GetFullPath();
  328. // the rule file seems to need to exist for vs10
  329. if (source->GetExtension() == "rule")
  330. {
  331. if(!cmSystemTools::FileExists(sourcePath.c_str()))
  332. {
  333. std::ofstream fout(sourcePath.c_str());
  334. if(fout)
  335. {
  336. fout << "# generated from CMake\n";
  337. fout.flush();
  338. fout.close();
  339. }
  340. }
  341. }
  342. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  343. std::string comment = lg->ConstructComment(command);
  344. comment = cmVS10EscapeComment(comment);
  345. std::vector<std::string> *configs =
  346. static_cast<cmGlobalVisualStudio7Generator *>
  347. (this->GlobalGenerator)->GetConfigurations();
  348. this->WriteString("<CustomBuild Include=\"", 2);
  349. std::string path =
  350. cmSystemTools::RelativePath(
  351. this->Makefile->GetCurrentOutputDirectory(),
  352. sourcePath.c_str());
  353. this->ConvertToWindowsSlash(path);
  354. (*this->BuildFileStream ) << path << "\">\n";
  355. for(std::vector<std::string>::iterator i = configs->begin();
  356. i != configs->end(); ++i)
  357. {
  358. std::string script =
  359. cmVS10EscapeXML(lg->ConstructScript(command, i->c_str()));
  360. this->WritePlatformConfigTag("Message",i->c_str(), 3);
  361. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  362. this->WritePlatformConfigTag("Command", i->c_str(), 3);
  363. (*this->BuildFileStream ) << script << "</Command>\n";
  364. this->WritePlatformConfigTag("AdditionalInputs", i->c_str(), 3);
  365. (*this->BuildFileStream ) << source->GetFullPath();
  366. for(std::vector<std::string>::const_iterator d =
  367. command.GetDepends().begin();
  368. d != command.GetDepends().end();
  369. ++d)
  370. {
  371. std::string dep;
  372. if(this->LocalGenerator->GetRealDependency(d->c_str(), i->c_str(), dep))
  373. {
  374. this->ConvertToWindowsSlash(dep);
  375. (*this->BuildFileStream ) << ";" << dep;
  376. }
  377. }
  378. (*this->BuildFileStream ) << ";%(AdditionalInputs)</AdditionalInputs>\n";
  379. this->WritePlatformConfigTag("Outputs", i->c_str(), 3);
  380. const char* sep = "";
  381. for(std::vector<std::string>::const_iterator o =
  382. command.GetOutputs().begin();
  383. o != command.GetOutputs().end();
  384. ++o)
  385. {
  386. std::string out = *o;
  387. this->ConvertToWindowsSlash(out);
  388. (*this->BuildFileStream ) << sep << out;
  389. sep = ";";
  390. }
  391. (*this->BuildFileStream ) << ";%(Outputs)</Outputs>\n";
  392. }
  393. this->WriteString("</CustomBuild>\n", 2);
  394. }
  395. void cmVisualStudio10TargetGenerator::ConvertToWindowsSlash(std::string& s)
  396. {
  397. // first convert all of the slashes
  398. std::string::size_type pos = 0;
  399. while((pos = s.find('/', pos)) != std::string::npos)
  400. {
  401. s[pos] = '\\';
  402. pos++;
  403. }
  404. }
  405. void cmVisualStudio10TargetGenerator::WriteGroups()
  406. {
  407. // collect up group information
  408. std::vector<cmSourceGroup> sourceGroups =
  409. this->Makefile->GetSourceGroups();
  410. std::vector<cmSourceFile*> classes = this->Target->GetSourceFiles();
  411. std::set<cmSourceGroup*> groupsUsed;
  412. std::vector<cmSourceFile*> clCompile;
  413. std::vector<cmSourceFile*> customBuild;
  414. std::vector<cmSourceFile*> none;
  415. std::vector<cmSourceFile*> headers;
  416. std::vector<cmSourceFile*> idls;
  417. std::vector<cmSourceFile*> resource;
  418. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  419. s != classes.end(); s++)
  420. {
  421. cmSourceFile* sf = *s;
  422. std::string const& source = sf->GetFullPath();
  423. cmSourceGroup& sourceGroup =
  424. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  425. groupsUsed.insert(&sourceGroup);
  426. const char* lang = sf->GetLanguage();
  427. bool header = (*s)->GetPropertyAsBool("HEADER_FILE_ONLY")
  428. || this->GlobalGenerator->IgnoreFile
  429. ((*s)->GetExtension().c_str());
  430. if(!lang)
  431. {
  432. lang = "None";
  433. }
  434. if(lang[0] == 'C')
  435. {
  436. clCompile.push_back(sf);
  437. }
  438. else if(strcmp(lang, "RC") == 0)
  439. {
  440. resource.push_back(sf);
  441. }
  442. else if(sf->GetCustomCommand())
  443. {
  444. customBuild.push_back(sf);
  445. }
  446. else if(header)
  447. {
  448. headers.push_back(sf);
  449. }
  450. else if(sf->GetExtension() == "idl")
  451. {
  452. idls.push_back(sf);
  453. }
  454. else
  455. {
  456. none.push_back(sf);
  457. }
  458. }
  459. this->AddMissingSourceGroups(groupsUsed, sourceGroups);
  460. // Write out group file
  461. std::string path = this->Makefile->GetStartOutputDirectory();
  462. path += "/";
  463. path += this->Name;
  464. path += ".vcxproj.filters";
  465. cmGeneratedFileStream fout(path.c_str());
  466. fout.SetCopyIfDifferent(true);
  467. char magic[] = {0xEF,0xBB, 0xBF};
  468. fout.write(magic, 3);
  469. cmGeneratedFileStream* save = this->BuildFileStream;
  470. this->BuildFileStream = & fout;
  471. this->WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  472. "<Project "
  473. "ToolsVersion=\"4.0\" "
  474. "xmlns=\"http://schemas.microsoft.com/"
  475. "developer/msbuild/2003\">\n",
  476. 0);
  477. this->WriteGroupSources("ClCompile", clCompile, sourceGroups);
  478. this->WriteGroupSources("ClInclude", headers, sourceGroups);
  479. this->WriteGroupSources("ResourceCompile", resource, sourceGroups);
  480. this->WriteGroupSources("Midl", idls, sourceGroups);
  481. this->WriteGroupSources("CustomBuild", customBuild, sourceGroups);
  482. this->WriteString("<ItemGroup>\n", 1);
  483. for(std::set<cmSourceGroup*>::iterator g = groupsUsed.begin();
  484. g != groupsUsed.end(); ++g)
  485. {
  486. cmSourceGroup* sg = *g;
  487. const char* name = sg->GetFullName();
  488. if(strlen(name) != 0)
  489. {
  490. this->WriteString("<Filter Include=\"", 2);
  491. (*this->BuildFileStream) << name << "\">\n";
  492. std::string guidName = "SG_Filter_";
  493. guidName += name;
  494. this->GlobalGenerator->CreateGUID(guidName.c_str());
  495. this->WriteString("<UniqueIdentifier>", 3);
  496. std::string guid
  497. = this->GlobalGenerator->GetGUID(guidName.c_str());
  498. (*this->BuildFileStream)
  499. << "{"
  500. << guid << "}"
  501. << "</UniqueIdentifier>\n";
  502. this->WriteString("</Filter>\n", 2);
  503. }
  504. }
  505. this->WriteString("</ItemGroup>\n", 1);
  506. this->WriteGroupSources("None", none, sourceGroups);
  507. this->WriteString("</Project>\n", 0);
  508. // restore stream pointer
  509. this->BuildFileStream = save;
  510. if (fout.Close())
  511. {
  512. this->GlobalGenerator->FileReplacedDuringGenerate(path);
  513. }
  514. }
  515. // Add to groupsUsed empty source groups that have non-empty children.
  516. void
  517. cmVisualStudio10TargetGenerator::AddMissingSourceGroups(
  518. std::set<cmSourceGroup*>& groupsUsed,
  519. const std::vector<cmSourceGroup>& allGroups
  520. )
  521. {
  522. for(std::vector<cmSourceGroup>::const_iterator current = allGroups.begin();
  523. current != allGroups.end(); ++current)
  524. {
  525. std::vector<cmSourceGroup> const& children = current->GetGroupChildren();
  526. if(children.empty())
  527. {
  528. continue; // the group is really empty
  529. }
  530. this->AddMissingSourceGroups(groupsUsed, children);
  531. cmSourceGroup* current_ptr = const_cast<cmSourceGroup*>(&(*current));
  532. if(groupsUsed.find(current_ptr) != groupsUsed.end())
  533. {
  534. continue; // group has already been added to set
  535. }
  536. // check if it least one of the group's descendants is not empty
  537. // (at least one child must already have been added)
  538. std::vector<cmSourceGroup>::const_iterator child_it = children.begin();
  539. while(child_it != children.end())
  540. {
  541. cmSourceGroup* child_ptr = const_cast<cmSourceGroup*>(&(*child_it));
  542. if(groupsUsed.find(child_ptr) != groupsUsed.end())
  543. {
  544. break; // found a child that was already added => add current group too
  545. }
  546. child_it++;
  547. }
  548. if(child_it == children.end())
  549. {
  550. continue; // no descendants have source files => ignore this group
  551. }
  552. groupsUsed.insert(current_ptr);
  553. }
  554. }
  555. void
  556. cmVisualStudio10TargetGenerator::
  557. WriteGroupSources(const char* name,
  558. std::vector<cmSourceFile*> const& sources,
  559. std::vector<cmSourceGroup>& sourceGroups)
  560. {
  561. this->WriteString("<ItemGroup>\n", 1);
  562. for(std::vector<cmSourceFile*>::const_iterator s = sources.begin();
  563. s != sources.end(); ++s)
  564. {
  565. cmSourceFile* sf = *s;
  566. if(sf->GetExtension() == "obj")
  567. {
  568. continue;
  569. }
  570. std::string const& source = sf->GetFullPath();
  571. cmSourceGroup& sourceGroup =
  572. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  573. const char* filter = sourceGroup.GetFullName();
  574. this->WriteString("<", 2);
  575. std::string path = source;
  576. path = cmSystemTools::RelativePath(
  577. this->Makefile->GetCurrentOutputDirectory(),
  578. source.c_str());
  579. this->ConvertToWindowsSlash(path);
  580. (*this->BuildFileStream) << name << " Include=\""
  581. << path;
  582. if(strlen(filter))
  583. {
  584. (*this->BuildFileStream) << "\">\n";
  585. this->WriteString("<Filter>", 3);
  586. (*this->BuildFileStream) << filter << "</Filter>\n";
  587. this->WriteString("</", 2);
  588. (*this->BuildFileStream) << name << ">\n";
  589. }
  590. else
  591. {
  592. (*this->BuildFileStream) << "\" />\n";
  593. }
  594. }
  595. this->WriteString("</ItemGroup>\n", 1);
  596. }
  597. void cmVisualStudio10TargetGenerator::WriteObjSources()
  598. {
  599. if(this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  600. {
  601. return;
  602. }
  603. bool first = true;
  604. std::vector<cmSourceFile*>const & sources = this->Target->GetSourceFiles();
  605. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  606. source != sources.end(); ++source)
  607. {
  608. if((*source)->GetExtension() == "obj")
  609. {
  610. if(first)
  611. {
  612. this->WriteString("<ItemGroup>\n", 1);
  613. first = false;
  614. }
  615. this->WriteString("<None Include=\"", 2);
  616. (*this->BuildFileStream ) << (*source)->GetFullPath() << "\" />\n";
  617. }
  618. }
  619. if(!first)
  620. {
  621. this->WriteString("</ItemGroup>\n", 1);
  622. }
  623. }
  624. void cmVisualStudio10TargetGenerator::WriteCLSources()
  625. {
  626. if(this->Target->GetType() > cmTarget::UTILITY)
  627. {
  628. return;
  629. }
  630. this->WriteString("<ItemGroup>\n", 1);
  631. std::vector<cmSourceFile*>const& sources = this->Target->GetSourceFiles();
  632. for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
  633. source != sources.end(); ++source)
  634. {
  635. std::string ext = (*source)->GetExtension();
  636. if((*source)->GetCustomCommand() || ext == "obj")
  637. {
  638. continue;
  639. }
  640. // If it is not a custom command and it is not a pre-built obj file,
  641. // then add it as a source (c/c++/header/rc/idl) file
  642. bool header = (*source)->GetPropertyAsBool("HEADER_FILE_ONLY")
  643. || this->GlobalGenerator->IgnoreFile(ext.c_str());
  644. const char* lang = (*source)->GetLanguage();
  645. bool cl = lang && (strcmp(lang, "C") == 0 || strcmp(lang, "CXX") ==0);
  646. bool rc = lang && (strcmp(lang, "RC") == 0);
  647. bool idl = ext == "idl";
  648. std::string sourceFile = (*source)->GetFullPath();
  649. sourceFile = cmSystemTools::RelativePath(
  650. this->Makefile->GetCurrentOutputDirectory(),
  651. sourceFile.c_str());
  652. this->ConvertToWindowsSlash(sourceFile);
  653. // output the source file
  654. if(header)
  655. {
  656. this->WriteString("<ClInclude Include=\"", 2);
  657. }
  658. else if(cl)
  659. {
  660. this->WriteString("<ClCompile Include=\"", 2);
  661. }
  662. else if(rc)
  663. {
  664. this->WriteString("<ResourceCompile Include=\"", 2);
  665. }
  666. else if(idl)
  667. {
  668. this->WriteString("<Midl Include=\"", 2);
  669. }
  670. else
  671. {
  672. this->WriteString("<None Include=\"", 2);
  673. }
  674. (*this->BuildFileStream ) << sourceFile << "\"";
  675. // ouput any flags specific to this source file
  676. if(!header && cl && this->OutputSourceSpecificFlags(*source))
  677. {
  678. // if the source file has specific flags the tag
  679. // is ended on a new line
  680. this->WriteString("</ClCompile>\n", 2);
  681. }
  682. else if(!header && rc && this->OutputSourceSpecificFlags(*source))
  683. {
  684. this->WriteString("</ResourceCompile>\n", 2);
  685. }
  686. else
  687. {
  688. (*this->BuildFileStream ) << " />\n";
  689. }
  690. }
  691. this->WriteString("</ItemGroup>\n", 1);
  692. }
  693. void cmVisualStudio10TargetGenerator::ComputeObjectNames()
  694. {
  695. // We may be modifying the source groups temporarily, so make a copy.
  696. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  697. // get the classes from the source lists then add them to the groups
  698. std::vector<cmSourceFile*>const & classes = this->Target->GetSourceFiles();
  699. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  700. i != classes.end(); i++)
  701. {
  702. // Add the file to the list of sources.
  703. std::string source = (*i)->GetFullPath();
  704. if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
  705. {
  706. this->ModuleDefinitionFile = (*i)->GetFullPath();
  707. }
  708. cmSourceGroup& sourceGroup =
  709. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  710. sourceGroup.AssignSource(*i);
  711. }
  712. // Compute which sources need unique object computation.
  713. this->LocalGenerator->ComputeObjectNameRequirements(sourceGroups);
  714. }
  715. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  716. cmSourceFile* source)
  717. {
  718. cmSourceFile& sf = *source;
  719. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  720. // Compute the maximum length full path to the intermediate
  721. // files directory for any configuration. This is used to construct
  722. // object file names that do not produce paths that are too long.
  723. std::string dir_max;
  724. lg->ComputeMaxDirectoryLength(dir_max, *this->Target);
  725. std::string objectName;
  726. if(lg->NeedObjectName.find(&sf) != lg->NeedObjectName.end())
  727. {
  728. objectName = lg->GetObjectFileNameWithoutTarget(sf, dir_max);
  729. }
  730. std::string flags;
  731. std::string defines;
  732. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  733. {
  734. flags += cflags;
  735. }
  736. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  737. {
  738. defines += cdefs;
  739. }
  740. const char* lang =
  741. this->GlobalGenerator->GetLanguageFromExtension
  742. (sf.GetExtension().c_str());
  743. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  744. const char* linkLanguage = this->Target->GetLinkerLanguage();
  745. bool needForceLang = false;
  746. // source file does not match its extension language
  747. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  748. {
  749. needForceLang = true;
  750. lang = sourceLang;
  751. }
  752. // if the source file does not match the linker language
  753. // then force c or c++
  754. if(needForceLang || (linkLanguage && lang
  755. && strcmp(lang, linkLanguage) != 0))
  756. {
  757. if(strcmp(lang, "CXX") == 0)
  758. {
  759. // force a C++ file type
  760. flags += " /TP ";
  761. }
  762. else if(strcmp(lang, "C") == 0)
  763. {
  764. // force to c
  765. flags += " /TC ";
  766. }
  767. }
  768. bool hasFlags = false;
  769. // for the first time we need a new line if there is something
  770. // produced here.
  771. const char* firstString = ">\n";
  772. if(objectName.size())
  773. {
  774. (*this->BuildFileStream ) << firstString;
  775. firstString = "";
  776. hasFlags = true;
  777. this->WriteString("<ObjectFileName>", 3);
  778. (*this->BuildFileStream )
  779. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  780. }
  781. std::vector<std::string> *configs =
  782. static_cast<cmGlobalVisualStudio7Generator *>
  783. (this->GlobalGenerator)->GetConfigurations();
  784. for( std::vector<std::string>::iterator config = configs->begin();
  785. config != configs->end(); ++config)
  786. {
  787. std::string configUpper = cmSystemTools::UpperCase(*config);
  788. std::string configDefines = defines;
  789. std::string defPropName = "COMPILE_DEFINITIONS_";
  790. defPropName += configUpper;
  791. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  792. {
  793. if(configDefines.size())
  794. {
  795. configDefines += ";";
  796. }
  797. configDefines += ccdefs;
  798. }
  799. // if we have flags or defines for this config then
  800. // use them
  801. if(flags.size() || configDefines.size())
  802. {
  803. (*this->BuildFileStream ) << firstString;
  804. firstString = ""; // only do firstString once
  805. hasFlags = true;
  806. cmVisualStudioGeneratorOptions
  807. clOptions(this->LocalGenerator,
  808. 10, cmVisualStudioGeneratorOptions::Compiler,
  809. cmVS10CLFlagTable, 0, this);
  810. clOptions.Parse(flags.c_str());
  811. clOptions.AddDefines(configDefines.c_str());
  812. clOptions.SetConfiguration((*config).c_str());
  813. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  814. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  815. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  816. " ", "\n", lang);
  817. }
  818. }
  819. return hasFlags;
  820. }
  821. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  822. {
  823. cmTarget::TargetType ttype = this->Target->GetType();
  824. if(ttype > cmTarget::GLOBAL_TARGET)
  825. {
  826. return;
  827. }
  828. this->WriteString("<PropertyGroup>\n", 2);
  829. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  830. "</_ProjectFileVersion>\n", 3);
  831. std::vector<std::string> *configs =
  832. static_cast<cmGlobalVisualStudio7Generator *>
  833. (this->GlobalGenerator)->GetConfigurations();
  834. for(std::vector<std::string>::iterator config = configs->begin();
  835. config != configs->end(); ++config)
  836. {
  837. if(ttype >= cmTarget::UTILITY)
  838. {
  839. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  840. *this->BuildFileStream
  841. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  842. << "</IntDir>\n";
  843. }
  844. else
  845. {
  846. std::string targetNameFull =
  847. this->Target->GetFullName(config->c_str());
  848. std::string intermediateDir = this->LocalGenerator->
  849. GetTargetDirectory(*this->Target);
  850. intermediateDir += "/";
  851. intermediateDir += *config;
  852. intermediateDir += "/";
  853. this->ConvertToWindowsSlash(intermediateDir);
  854. std::string outDir = this->Target->GetDirectory(config->c_str());
  855. this->ConvertToWindowsSlash(outDir);
  856. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  857. *this->BuildFileStream << outDir
  858. << "\\"
  859. << "</OutDir>\n";
  860. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  861. *this->BuildFileStream << intermediateDir
  862. << "</IntDir>\n";
  863. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  864. *this->BuildFileStream
  865. << cmSystemTools::GetFilenameWithoutLastExtension(
  866. targetNameFull.c_str())
  867. << "</TargetName>\n";
  868. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  869. *this->BuildFileStream
  870. << cmSystemTools::GetFilenameLastExtension(targetNameFull.c_str())
  871. << "</TargetExt>\n";
  872. this->OutputLinkIncremental(*config);
  873. }
  874. }
  875. this->WriteString("</PropertyGroup>\n", 2);
  876. }
  877. void
  878. cmVisualStudio10TargetGenerator::
  879. OutputLinkIncremental(std::string const& configName)
  880. {
  881. std::string CONFIG = cmSystemTools::UpperCase(configName);
  882. // static libraries and things greater than modules do not need
  883. // to set this option
  884. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  885. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  886. {
  887. return;
  888. }
  889. const char* linkType = "SHARED";
  890. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  891. {
  892. linkType = "EXE";
  893. }
  894. // assume incremental linking
  895. const char* incremental = "true";
  896. const char* linkLanguage =
  897. this->Target->GetLinkerLanguage(configName.c_str());
  898. if(!linkLanguage)
  899. {
  900. cmSystemTools::Error
  901. ("CMake can not determine linker language for target:",
  902. this->Name.c_str());
  903. return;
  904. }
  905. std::string linkFlagVarBase = "CMAKE_";
  906. linkFlagVarBase += linkType;
  907. linkFlagVarBase += "_LINKER_FLAGS";
  908. std::string flags = this->
  909. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  910. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  911. flags += this->
  912. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  913. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  914. || strcmp(linkLanguage, "Fortran") == 0)
  915. {
  916. std::string baseFlagVar = "CMAKE_";
  917. baseFlagVar += linkLanguage;
  918. baseFlagVar += "_FLAGS";
  919. flags += this->
  920. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  921. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  922. flags +=
  923. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  924. }
  925. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  926. if(targetLinkFlags)
  927. {
  928. flags += " ";
  929. flags += targetLinkFlags;
  930. }
  931. std::string flagsProp = "LINK_FLAGS_";
  932. flagsProp += CONFIG;
  933. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  934. {
  935. flags += " ";
  936. flags += flagsConfig;
  937. }
  938. if(flags.find("INCREMENTAL:NO") != flags.npos)
  939. {
  940. incremental = "false";
  941. }
  942. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  943. *this->BuildFileStream << incremental
  944. << "</LinkIncremental>\n";
  945. }
  946. //----------------------------------------------------------------------------
  947. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  948. {
  949. std::vector<std::string> const* configs =
  950. this->GlobalGenerator->GetConfigurations();
  951. for(std::vector<std::string>::const_iterator i = configs->begin();
  952. i != configs->end(); ++i)
  953. {
  954. if(!this->ComputeClOptions(*i))
  955. {
  956. return false;
  957. }
  958. }
  959. return true;
  960. }
  961. //----------------------------------------------------------------------------
  962. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  963. std::string const& configName)
  964. {
  965. // much of this was copied from here:
  966. // copied from cmLocalVisualStudio7Generator.cxx 805
  967. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  968. cmsys::auto_ptr<Options> pOptions(
  969. new Options(this->LocalGenerator, 10, Options::Compiler,
  970. cmVS10CLFlagTable));
  971. Options& clOptions = *pOptions;
  972. std::string flags;
  973. // collect up flags for
  974. if(this->Target->GetType() < cmTarget::UTILITY)
  975. {
  976. const char* linkLanguage =
  977. this->Target->GetLinkerLanguage(configName.c_str());
  978. if(!linkLanguage)
  979. {
  980. cmSystemTools::Error
  981. ("CMake can not determine linker language for target:",
  982. this->Name.c_str());
  983. return false;
  984. }
  985. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  986. || strcmp(linkLanguage, "Fortran") == 0)
  987. {
  988. std::string baseFlagVar = "CMAKE_";
  989. baseFlagVar += linkLanguage;
  990. baseFlagVar += "_FLAGS";
  991. flags = this->
  992. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  993. std::string flagVar = baseFlagVar + std::string("_") +
  994. cmSystemTools::UpperCase(configName);
  995. flags += " ";
  996. flags += this->
  997. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  998. }
  999. // set the correct language
  1000. if(strcmp(linkLanguage, "C") == 0)
  1001. {
  1002. flags += " /TC ";
  1003. }
  1004. if(strcmp(linkLanguage, "CXX") == 0)
  1005. {
  1006. flags += " /TP ";
  1007. }
  1008. }
  1009. // Add the target-specific flags.
  1010. if(const char* targetFlags = this->Target->GetProperty("COMPILE_FLAGS"))
  1011. {
  1012. flags += " ";
  1013. flags += targetFlags;
  1014. }
  1015. std::string configUpper = cmSystemTools::UpperCase(configName);
  1016. std::string defPropName = "COMPILE_DEFINITIONS_";
  1017. defPropName += configUpper;
  1018. // Get preprocessor definitions for this directory.
  1019. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1020. clOptions.FixExceptionHandlingDefault();
  1021. clOptions.Parse(flags.c_str());
  1022. clOptions.Parse(defineFlags.c_str());
  1023. clOptions.AddDefines
  1024. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  1025. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  1026. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  1027. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  1028. clOptions.SetVerboseMakefile(
  1029. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1030. // Add a definition for the configuration name.
  1031. std::string configDefine = "CMAKE_INTDIR=\"";
  1032. configDefine += configName;
  1033. configDefine += "\"";
  1034. clOptions.AddDefine(configDefine);
  1035. if(const char* exportMacro = this->Target->GetExportMacro())
  1036. {
  1037. clOptions.AddDefine(exportMacro);
  1038. }
  1039. this->ClOptions[configName] = pOptions.release();
  1040. return true;
  1041. }
  1042. //----------------------------------------------------------------------------
  1043. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1044. std::string const& configName,
  1045. std::vector<std::string> const& includes)
  1046. {
  1047. Options& clOptions = *(this->ClOptions[configName]);
  1048. this->WriteString("<ClCompile>\n", 2);
  1049. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1050. this->OutputIncludes(includes);
  1051. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1052. // If not in debug mode, write the DebugInformationFormat field
  1053. // without value so PDBs don't get generated uselessly.
  1054. if(!clOptions.IsDebug())
  1055. {
  1056. this->WriteString("<DebugInformationFormat>"
  1057. "</DebugInformationFormat>\n", 3);
  1058. }
  1059. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1060. "\n", "CXX");
  1061. this->WriteString("<AssemblerListingLocation>", 3);
  1062. *this->BuildFileStream << configName
  1063. << "</AssemblerListingLocation>\n";
  1064. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1065. this->WriteString("<ProgramDataBaseFileName>", 3);
  1066. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  1067. << "/"
  1068. << this->Target->GetPDBName(configName.c_str())
  1069. << "</ProgramDataBaseFileName>\n";
  1070. this->WriteString("</ClCompile>\n", 2);
  1071. }
  1072. void cmVisualStudio10TargetGenerator::
  1073. OutputIncludes(std::vector<std::string> const & includes)
  1074. {
  1075. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1076. for(std::vector<std::string>::const_iterator i = includes.begin();
  1077. i != includes.end(); ++i)
  1078. {
  1079. *this->BuildFileStream << *i << ";";
  1080. }
  1081. this->WriteString("%(AdditionalIncludeDirectories)"
  1082. "</AdditionalIncludeDirectories>\n", 0);
  1083. }
  1084. void cmVisualStudio10TargetGenerator::
  1085. WriteRCOptions(std::string const& configName,
  1086. std::vector<std::string> const & includes)
  1087. {
  1088. this->WriteString("<ResourceCompile>\n", 2);
  1089. Options& clOptions = *(this->ClOptions[configName]);
  1090. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1091. "\n", "RC");
  1092. this->OutputIncludes(includes);
  1093. this->WriteString("</ResourceCompile>\n", 2);
  1094. }
  1095. void
  1096. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1097. {
  1098. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1099. {
  1100. return;
  1101. }
  1102. const char* libflags = this->Target->GetProperty("STATIC_LIBRARY_FLAGS");
  1103. std::string flagsConfigVar = "STATIC_LIBRARY_FLAGS_";
  1104. flagsConfigVar += cmSystemTools::UpperCase(config);
  1105. const char* libflagsConfig =
  1106. this->Target->GetProperty(flagsConfigVar.c_str());
  1107. if(libflags || libflagsConfig)
  1108. {
  1109. this->WriteString("<Lib>\n", 2);
  1110. cmVisualStudioGeneratorOptions
  1111. libOptions(this->LocalGenerator, 10,
  1112. cmVisualStudioGeneratorOptions::Linker,
  1113. cmVS10LibFlagTable, 0, this);
  1114. libOptions.Parse(libflags?libflags:"");
  1115. libOptions.Parse(libflagsConfig?libflagsConfig:"");
  1116. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1117. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1118. this->WriteString("</Lib>\n", 2);
  1119. }
  1120. }
  1121. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  1122. config)
  1123. {
  1124. // static libraries and things greater than modules do not need
  1125. // to set this option
  1126. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1127. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1128. {
  1129. return;
  1130. }
  1131. const char* linkLanguage =
  1132. this->Target->GetLinkerLanguage(config.c_str());
  1133. if(!linkLanguage)
  1134. {
  1135. cmSystemTools::Error
  1136. ("CMake can not determine linker language for target:",
  1137. this->Name.c_str());
  1138. return;
  1139. }
  1140. this->WriteString("<Link>\n", 2);
  1141. std::string CONFIG = cmSystemTools::UpperCase(config);
  1142. const char* linkType = "SHARED";
  1143. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1144. {
  1145. linkType = "MODULE";
  1146. }
  1147. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1148. {
  1149. linkType = "EXE";
  1150. }
  1151. std::string stackVar = "CMAKE_";
  1152. stackVar += linkLanguage;
  1153. stackVar += "_STACK_SIZE";
  1154. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  1155. std::string flags;
  1156. if(stackVal)
  1157. {
  1158. flags += " ";
  1159. flags += stackVal;
  1160. }
  1161. // assume incremental linking
  1162. std::string linkFlagVarBase = "CMAKE_";
  1163. linkFlagVarBase += linkType;
  1164. linkFlagVarBase += "_LINKER_FLAGS";
  1165. flags += " ";
  1166. flags += this->
  1167. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1168. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1169. flags += " ";
  1170. flags += this->
  1171. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1172. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1173. if(targetLinkFlags)
  1174. {
  1175. flags += " ";
  1176. flags += targetLinkFlags;
  1177. }
  1178. std::string flagsProp = "LINK_FLAGS_";
  1179. flagsProp += CONFIG;
  1180. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1181. {
  1182. flags += " ";
  1183. flags += flagsConfig;
  1184. }
  1185. cmVisualStudioGeneratorOptions
  1186. linkOptions(this->LocalGenerator, 10,
  1187. cmVisualStudioGeneratorOptions::Linker,
  1188. cmVS10LinkFlagTable, 0, this);
  1189. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1190. {
  1191. flags += " /SUBSYSTEM:WINDOWS";
  1192. }
  1193. else
  1194. {
  1195. flags += " /SUBSYSTEM:CONSOLE";
  1196. }
  1197. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  1198. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  1199. std::string standardLibsVar = "CMAKE_";
  1200. standardLibsVar += linkLanguage;
  1201. standardLibsVar += "_STANDARD_LIBRARIES";
  1202. std::string
  1203. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1204. // Remove trailing spaces from libs
  1205. std::string::size_type pos = libs.size()-1;
  1206. if(libs.size() != 0)
  1207. {
  1208. while(libs[pos] == ' ')
  1209. {
  1210. pos--;
  1211. }
  1212. }
  1213. if(pos != libs.size()-1)
  1214. {
  1215. libs = libs.substr(0, pos+1);
  1216. }
  1217. // Replace spaces in libs with ;
  1218. cmSystemTools::ReplaceString(libs, " ", ";");
  1219. cmComputeLinkInformation* pcli =
  1220. this->Target->GetLinkInformation(config.c_str());
  1221. if(!pcli)
  1222. {
  1223. cmSystemTools::Error
  1224. ("CMake can not compute cmComputeLinkInformation for target:",
  1225. this->Name.c_str());
  1226. return;
  1227. }
  1228. // add the libraries for the target to libs string
  1229. cmComputeLinkInformation& cli = *pcli;
  1230. this->AddLibraries(cli, libs);
  1231. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1232. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1233. const char* sep = "";
  1234. std::string linkDirs;
  1235. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1236. d != ldirs.end(); ++d)
  1237. {
  1238. // first just full path
  1239. linkDirs += sep;
  1240. linkDirs += *d;
  1241. sep = ";";
  1242. linkDirs += sep;
  1243. // next path with configuration type Debug, Release, etc
  1244. linkDirs += *d;
  1245. linkDirs += "/$(Configuration)";
  1246. linkDirs += sep;
  1247. }
  1248. linkDirs += "%(AdditionalLibraryDirectories)";
  1249. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1250. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1251. linkOptions.AddFlag("Version", "0.0");
  1252. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1253. {
  1254. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1255. }
  1256. else
  1257. {
  1258. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1259. }
  1260. std::string targetName;
  1261. std::string targetNameSO;
  1262. std::string targetNameFull;
  1263. std::string targetNameImport;
  1264. std::string targetNamePDB;
  1265. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1266. {
  1267. this->Target->GetExecutableNames(targetName, targetNameFull,
  1268. targetNameImport, targetNamePDB,
  1269. config.c_str());
  1270. }
  1271. else
  1272. {
  1273. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1274. targetNameImport, targetNamePDB,
  1275. config.c_str());
  1276. }
  1277. std::string dir = this->Target->GetDirectory(config.c_str());
  1278. dir += "/";
  1279. std::string pdb = dir;
  1280. pdb += targetNamePDB;
  1281. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1282. imLib += "/";
  1283. imLib += targetNameImport;
  1284. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1285. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  1286. linkOptions.Parse(flags.c_str());
  1287. if(!this->ModuleDefinitionFile.empty())
  1288. {
  1289. linkOptions.AddFlag("ModuleDefinitionFile",
  1290. this->ModuleDefinitionFile.c_str());
  1291. }
  1292. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1293. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1294. this->WriteString("</Link>\n", 2);
  1295. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1296. {
  1297. this->WriteString("<ProjectReference>\n", 2);
  1298. this->WriteString(
  1299. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1300. this->WriteString("</ProjectReference>\n", 2);
  1301. }
  1302. }
  1303. void cmVisualStudio10TargetGenerator::AddLibraries(
  1304. cmComputeLinkInformation& cli,
  1305. std::string& libstring)
  1306. {
  1307. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1308. ItemVector libs = cli.GetItems();
  1309. const char* sep = ";";
  1310. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1311. {
  1312. if(l->IsPath)
  1313. {
  1314. std::string path = this->LocalGenerator->
  1315. Convert(l->Value.c_str(),
  1316. cmLocalGenerator::START_OUTPUT,
  1317. cmLocalGenerator::UNCHANGED);
  1318. this->ConvertToWindowsSlash(path);
  1319. libstring += sep;
  1320. libstring += path;
  1321. }
  1322. else
  1323. {
  1324. libstring += sep;
  1325. libstring += l->Value;
  1326. }
  1327. }
  1328. }
  1329. void cmVisualStudio10TargetGenerator::
  1330. WriteMidlOptions(std::string const& /*config*/,
  1331. std::vector<std::string> const & includes)
  1332. {
  1333. this->WriteString("<Midl>\n", 2);
  1334. this->OutputIncludes(includes);
  1335. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1336. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1337. this->WriteString(
  1338. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1339. this->WriteString(
  1340. "<InterfaceIdentifierFileName>"
  1341. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1342. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1343. this->WriteString("</Midl>\n", 2);
  1344. }
  1345. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1346. {
  1347. std::vector<std::string> *configs =
  1348. static_cast<cmGlobalVisualStudio7Generator *>
  1349. (this->GlobalGenerator)->GetConfigurations();
  1350. std::vector<std::string> includes;
  1351. this->LocalGenerator->GetIncludeDirectories(includes);
  1352. for(std::vector<std::string>::iterator i = configs->begin();
  1353. i != configs->end(); ++i)
  1354. {
  1355. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1356. *this->BuildFileStream << "\n";
  1357. // output cl compile flags <ClCompile></ClCompile>
  1358. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  1359. {
  1360. this->WriteClOptions(*i, includes);
  1361. // output rc compile flags <ResourceCompile></ResourceCompile>
  1362. this->WriteRCOptions(*i, includes);
  1363. }
  1364. // output midl flags <Midl></Midl>
  1365. this->WriteMidlOptions(*i, includes);
  1366. // write events
  1367. this->WriteEvents(*i);
  1368. // output link flags <Link></Link>
  1369. this->WriteLinkOptions(*i);
  1370. // output lib flags <Lib></Lib>
  1371. this->WriteLibOptions(*i);
  1372. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1373. }
  1374. }
  1375. void
  1376. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1377. {
  1378. this->WriteEvent("PreLinkEvent",
  1379. this->Target->GetPreLinkCommands(), configName);
  1380. this->WriteEvent("PreBuildEvent",
  1381. this->Target->GetPreBuildCommands(), configName);
  1382. this->WriteEvent("PostBuildEvent",
  1383. this->Target->GetPostBuildCommands(), configName);
  1384. }
  1385. void cmVisualStudio10TargetGenerator::WriteEvent(
  1386. const char* name,
  1387. std::vector<cmCustomCommand> & commands,
  1388. std::string const& configName)
  1389. {
  1390. if(commands.size() == 0)
  1391. {
  1392. return;
  1393. }
  1394. this->WriteString("<", 2);
  1395. (*this->BuildFileStream ) << name << ">\n";
  1396. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1397. std::string script;
  1398. const char* pre = "";
  1399. std::string comment;
  1400. for(std::vector<cmCustomCommand>::iterator i = commands.begin();
  1401. i != commands.end(); ++i)
  1402. {
  1403. cmCustomCommand& command = *i;
  1404. comment += pre;
  1405. comment += lg->ConstructComment(command);
  1406. script += pre;
  1407. pre = "\n";
  1408. script +=
  1409. cmVS10EscapeXML(lg->ConstructScript(command, configName.c_str()));
  1410. }
  1411. comment = cmVS10EscapeComment(comment);
  1412. this->WriteString("<Message>",3);
  1413. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1414. this->WriteString("<Command>", 3);
  1415. (*this->BuildFileStream ) << script;
  1416. (*this->BuildFileStream ) << "</Command>" << "\n";
  1417. this->WriteString("</", 2);
  1418. (*this->BuildFileStream ) << name << ">\n";
  1419. }
  1420. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1421. {
  1422. cmGlobalGenerator::TargetDependSet const& unordered
  1423. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1424. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1425. OrderedTargetDependSet;
  1426. OrderedTargetDependSet depends(unordered);
  1427. this->WriteString("<ItemGroup>\n", 1);
  1428. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1429. i != depends.end(); ++i)
  1430. {
  1431. cmTarget* dt = *i;
  1432. this->WriteString("<ProjectReference Include=\"", 2);
  1433. cmMakefile* mf = dt->GetMakefile();
  1434. std::string name = dt->GetName();
  1435. std::string path;
  1436. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1437. if(p)
  1438. {
  1439. path = p;
  1440. }
  1441. else
  1442. {
  1443. path = mf->GetStartOutputDirectory();
  1444. path += "/";
  1445. path += dt->GetName();
  1446. path += ".vcxproj";
  1447. }
  1448. (*this->BuildFileStream) << path << "\">\n";
  1449. this->WriteString("<Project>", 3);
  1450. (*this->BuildFileStream)
  1451. << this->GlobalGenerator->GetGUID(name.c_str())
  1452. << "</Project>\n";
  1453. this->WriteString("</ProjectReference>\n", 2);
  1454. }
  1455. this->WriteString("</ItemGroup>\n", 1);
  1456. }