cmVisualStudio10TargetGenerator.cxx 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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
  683. {
  684. (*this->BuildFileStream ) << " />\n";
  685. }
  686. }
  687. this->WriteString("</ItemGroup>\n", 1);
  688. }
  689. void cmVisualStudio10TargetGenerator::ComputeObjectNames()
  690. {
  691. // We may be modifying the source groups temporarily, so make a copy.
  692. std::vector<cmSourceGroup> sourceGroups = this->Makefile->GetSourceGroups();
  693. // get the classes from the source lists then add them to the groups
  694. std::vector<cmSourceFile*>const & classes = this->Target->GetSourceFiles();
  695. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  696. i != classes.end(); i++)
  697. {
  698. // Add the file to the list of sources.
  699. std::string source = (*i)->GetFullPath();
  700. if(cmSystemTools::UpperCase((*i)->GetExtension()) == "DEF")
  701. {
  702. this->ModuleDefinitionFile = (*i)->GetFullPath();
  703. }
  704. cmSourceGroup& sourceGroup =
  705. this->Makefile->FindSourceGroup(source.c_str(), sourceGroups);
  706. sourceGroup.AssignSource(*i);
  707. }
  708. // Compute which sources need unique object computation.
  709. this->LocalGenerator->ComputeObjectNameRequirements(sourceGroups);
  710. }
  711. bool cmVisualStudio10TargetGenerator::OutputSourceSpecificFlags(
  712. cmSourceFile* source)
  713. {
  714. cmSourceFile& sf = *source;
  715. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  716. // Compute the maximum length full path to the intermediate
  717. // files directory for any configuration. This is used to construct
  718. // object file names that do not produce paths that are too long.
  719. std::string dir_max;
  720. lg->ComputeMaxDirectoryLength(dir_max, *this->Target);
  721. std::string objectName;
  722. if(lg->NeedObjectName.find(&sf) != lg->NeedObjectName.end())
  723. {
  724. objectName = lg->GetObjectFileNameWithoutTarget(sf, dir_max);
  725. }
  726. std::string flags;
  727. std::string defines;
  728. if(const char* cflags = sf.GetProperty("COMPILE_FLAGS"))
  729. {
  730. flags += cflags;
  731. }
  732. if(const char* cdefs = sf.GetProperty("COMPILE_DEFINITIONS"))
  733. {
  734. defines += cdefs;
  735. }
  736. const char* lang =
  737. this->GlobalGenerator->GetLanguageFromExtension
  738. (sf.GetExtension().c_str());
  739. const char* sourceLang = this->LocalGenerator->GetSourceFileLanguage(sf);
  740. const char* linkLanguage = this->Target->GetLinkerLanguage();
  741. bool needForceLang = false;
  742. // source file does not match its extension language
  743. if(lang && sourceLang && strcmp(lang, sourceLang) != 0)
  744. {
  745. needForceLang = true;
  746. lang = sourceLang;
  747. }
  748. // if the source file does not match the linker language
  749. // then force c or c++
  750. if(needForceLang || (linkLanguage && lang
  751. && strcmp(lang, linkLanguage) != 0))
  752. {
  753. if(strcmp(lang, "CXX") == 0)
  754. {
  755. // force a C++ file type
  756. flags += " /TP ";
  757. }
  758. else if(strcmp(lang, "C") == 0)
  759. {
  760. // force to c
  761. flags += " /TC ";
  762. }
  763. }
  764. bool hasFlags = false;
  765. // for the first time we need a new line if there is something
  766. // produced here.
  767. const char* firstString = ">\n";
  768. if(objectName.size())
  769. {
  770. (*this->BuildFileStream ) << firstString;
  771. firstString = "";
  772. hasFlags = true;
  773. this->WriteString("<ObjectFileName>", 3);
  774. (*this->BuildFileStream )
  775. << "$(IntDir)/" << objectName << "</ObjectFileName>\n";
  776. }
  777. std::vector<std::string> *configs =
  778. static_cast<cmGlobalVisualStudio7Generator *>
  779. (this->GlobalGenerator)->GetConfigurations();
  780. for( std::vector<std::string>::iterator config = configs->begin();
  781. config != configs->end(); ++config)
  782. {
  783. std::string configUpper = cmSystemTools::UpperCase(*config);
  784. std::string configDefines = defines;
  785. std::string defPropName = "COMPILE_DEFINITIONS_";
  786. defPropName += configUpper;
  787. if(const char* ccdefs = sf.GetProperty(defPropName.c_str()))
  788. {
  789. if(configDefines.size())
  790. {
  791. configDefines += ";";
  792. }
  793. configDefines += ccdefs;
  794. }
  795. // if we have flags or defines for this config then
  796. // use them
  797. if(flags.size() || configDefines.size())
  798. {
  799. (*this->BuildFileStream ) << firstString;
  800. firstString = ""; // only do firstString once
  801. hasFlags = true;
  802. cmVisualStudioGeneratorOptions
  803. clOptions(this->LocalGenerator,
  804. 10, cmVisualStudioGeneratorOptions::Compiler,
  805. cmVS10CLFlagTable, 0, this);
  806. clOptions.Parse(flags.c_str());
  807. clOptions.AddDefines(configDefines.c_str());
  808. clOptions.SetConfiguration((*config).c_str());
  809. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  810. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  811. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream,
  812. " ", "\n");
  813. }
  814. }
  815. return hasFlags;
  816. }
  817. void cmVisualStudio10TargetGenerator::WritePathAndIncrementalLinkOptions()
  818. {
  819. cmTarget::TargetType ttype = this->Target->GetType();
  820. if(ttype > cmTarget::GLOBAL_TARGET)
  821. {
  822. return;
  823. }
  824. this->WriteString("<PropertyGroup>\n", 2);
  825. this->WriteString("<_ProjectFileVersion>10.0.20506.1"
  826. "</_ProjectFileVersion>\n", 3);
  827. std::vector<std::string> *configs =
  828. static_cast<cmGlobalVisualStudio7Generator *>
  829. (this->GlobalGenerator)->GetConfigurations();
  830. for(std::vector<std::string>::iterator config = configs->begin();
  831. config != configs->end(); ++config)
  832. {
  833. if(ttype >= cmTarget::UTILITY)
  834. {
  835. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  836. *this->BuildFileStream
  837. << "$(Platform)\\$(Configuration)\\$(ProjectName)\\"
  838. << "</IntDir>\n";
  839. }
  840. else
  841. {
  842. std::string targetNameFull =
  843. this->Target->GetFullName(config->c_str());
  844. std::string intermediateDir = this->LocalGenerator->
  845. GetTargetDirectory(*this->Target);
  846. intermediateDir += "/";
  847. intermediateDir += *config;
  848. intermediateDir += "/";
  849. this->ConvertToWindowsSlash(intermediateDir);
  850. std::string outDir = this->Target->GetDirectory(config->c_str());
  851. this->ConvertToWindowsSlash(outDir);
  852. this->WritePlatformConfigTag("OutDir", config->c_str(), 3);
  853. *this->BuildFileStream << outDir
  854. << "\\"
  855. << "</OutDir>\n";
  856. this->WritePlatformConfigTag("IntDir", config->c_str(), 3);
  857. *this->BuildFileStream << intermediateDir
  858. << "</IntDir>\n";
  859. this->WritePlatformConfigTag("TargetName", config->c_str(), 3);
  860. *this->BuildFileStream
  861. << cmSystemTools::GetFilenameWithoutLastExtension(
  862. targetNameFull.c_str())
  863. << "</TargetName>\n";
  864. this->WritePlatformConfigTag("TargetExt", config->c_str(), 3);
  865. *this->BuildFileStream
  866. << cmSystemTools::GetFilenameLastExtension(targetNameFull.c_str())
  867. << "</TargetExt>\n";
  868. this->OutputLinkIncremental(*config);
  869. }
  870. }
  871. this->WriteString("</PropertyGroup>\n", 2);
  872. }
  873. void
  874. cmVisualStudio10TargetGenerator::
  875. OutputLinkIncremental(std::string const& configName)
  876. {
  877. std::string CONFIG = cmSystemTools::UpperCase(configName);
  878. // static libraries and things greater than modules do not need
  879. // to set this option
  880. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  881. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  882. {
  883. return;
  884. }
  885. const char* linkType = "SHARED";
  886. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  887. {
  888. linkType = "EXE";
  889. }
  890. // assume incremental linking
  891. const char* incremental = "true";
  892. const char* linkLanguage =
  893. this->Target->GetLinkerLanguage(configName.c_str());
  894. if(!linkLanguage)
  895. {
  896. cmSystemTools::Error
  897. ("CMake can not determine linker language for target:",
  898. this->Name.c_str());
  899. return;
  900. }
  901. std::string linkFlagVarBase = "CMAKE_";
  902. linkFlagVarBase += linkType;
  903. linkFlagVarBase += "_LINKER_FLAGS";
  904. std::string flags = this->
  905. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  906. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  907. flags += this->
  908. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  909. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  910. || strcmp(linkLanguage, "Fortran") == 0)
  911. {
  912. std::string baseFlagVar = "CMAKE_";
  913. baseFlagVar += linkLanguage;
  914. baseFlagVar += "_FLAGS";
  915. flags += this->
  916. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  917. std::string flagVar = baseFlagVar + std::string("_") + CONFIG;
  918. flags +=
  919. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  920. }
  921. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  922. if(targetLinkFlags)
  923. {
  924. flags += " ";
  925. flags += targetLinkFlags;
  926. }
  927. std::string flagsProp = "LINK_FLAGS_";
  928. flagsProp += CONFIG;
  929. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  930. {
  931. flags += " ";
  932. flags += flagsConfig;
  933. }
  934. if(flags.find("INCREMENTAL:NO") != flags.npos)
  935. {
  936. incremental = "false";
  937. }
  938. this->WritePlatformConfigTag("LinkIncremental", configName.c_str(), 3);
  939. *this->BuildFileStream << incremental
  940. << "</LinkIncremental>\n";
  941. }
  942. //----------------------------------------------------------------------------
  943. bool cmVisualStudio10TargetGenerator::ComputeClOptions()
  944. {
  945. std::vector<std::string> const* configs =
  946. this->GlobalGenerator->GetConfigurations();
  947. for(std::vector<std::string>::const_iterator i = configs->begin();
  948. i != configs->end(); ++i)
  949. {
  950. if(!this->ComputeClOptions(*i))
  951. {
  952. return false;
  953. }
  954. }
  955. return true;
  956. }
  957. //----------------------------------------------------------------------------
  958. bool cmVisualStudio10TargetGenerator::ComputeClOptions(
  959. std::string const& configName)
  960. {
  961. // much of this was copied from here:
  962. // copied from cmLocalVisualStudio7Generator.cxx 805
  963. // TODO: Integrate code below with cmLocalVisualStudio7Generator.
  964. cmsys::auto_ptr<Options> pOptions(
  965. new Options(this->LocalGenerator, 10, Options::Compiler,
  966. cmVS10CLFlagTable));
  967. Options& clOptions = *pOptions;
  968. std::string flags;
  969. // collect up flags for
  970. if(this->Target->GetType() < cmTarget::UTILITY)
  971. {
  972. const char* linkLanguage =
  973. this->Target->GetLinkerLanguage(configName.c_str());
  974. if(!linkLanguage)
  975. {
  976. cmSystemTools::Error
  977. ("CMake can not determine linker language for target:",
  978. this->Name.c_str());
  979. return false;
  980. }
  981. if(strcmp(linkLanguage, "C") == 0 || strcmp(linkLanguage, "CXX") == 0
  982. || strcmp(linkLanguage, "Fortran") == 0)
  983. {
  984. std::string baseFlagVar = "CMAKE_";
  985. baseFlagVar += linkLanguage;
  986. baseFlagVar += "_FLAGS";
  987. flags = this->
  988. Target->GetMakefile()->GetRequiredDefinition(baseFlagVar.c_str());
  989. std::string flagVar = baseFlagVar + std::string("_") +
  990. cmSystemTools::UpperCase(configName);
  991. flags += " ";
  992. flags += this->
  993. Target->GetMakefile()->GetRequiredDefinition(flagVar.c_str());
  994. }
  995. // set the correct language
  996. if(strcmp(linkLanguage, "C") == 0)
  997. {
  998. flags += " /TC ";
  999. }
  1000. if(strcmp(linkLanguage, "CXX") == 0)
  1001. {
  1002. flags += " /TP ";
  1003. }
  1004. }
  1005. // Add the target-specific flags.
  1006. if(const char* targetFlags = this->Target->GetProperty("COMPILE_FLAGS"))
  1007. {
  1008. flags += " ";
  1009. flags += targetFlags;
  1010. }
  1011. std::string configUpper = cmSystemTools::UpperCase(configName);
  1012. std::string defPropName = "COMPILE_DEFINITIONS_";
  1013. defPropName += configUpper;
  1014. // Get preprocessor definitions for this directory.
  1015. std::string defineFlags = this->Target->GetMakefile()->GetDefineFlags();
  1016. clOptions.FixExceptionHandlingDefault();
  1017. clOptions.Parse(flags.c_str());
  1018. clOptions.Parse(defineFlags.c_str());
  1019. clOptions.AddDefines
  1020. (this->Makefile->GetProperty("COMPILE_DEFINITIONS"));
  1021. clOptions.AddDefines(this->Target->GetProperty("COMPILE_DEFINITIONS"));
  1022. clOptions.AddDefines(this->Makefile->GetProperty(defPropName.c_str()));
  1023. clOptions.AddDefines(this->Target->GetProperty(defPropName.c_str()));
  1024. clOptions.SetVerboseMakefile(
  1025. this->Makefile->IsOn("CMAKE_VERBOSE_MAKEFILE"));
  1026. // Add a definition for the configuration name.
  1027. std::string configDefine = "CMAKE_INTDIR=\"";
  1028. configDefine += configName;
  1029. configDefine += "\"";
  1030. clOptions.AddDefine(configDefine);
  1031. if(const char* exportMacro = this->Target->GetExportMacro())
  1032. {
  1033. clOptions.AddDefine(exportMacro);
  1034. }
  1035. this->ClOptions[configName] = pOptions.release();
  1036. return true;
  1037. }
  1038. //----------------------------------------------------------------------------
  1039. void cmVisualStudio10TargetGenerator::WriteClOptions(
  1040. std::string const& configName,
  1041. std::vector<std::string> const& includes)
  1042. {
  1043. Options& clOptions = *(this->ClOptions[configName]);
  1044. this->WriteString("<ClCompile>\n", 2);
  1045. clOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1046. this->OutputIncludes(includes);
  1047. clOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1048. // If not in debug mode, write the DebugInformationFormat field
  1049. // without value so PDBs don't get generated uselessly.
  1050. if(!clOptions.IsDebug())
  1051. {
  1052. this->WriteString("<DebugInformationFormat>"
  1053. "</DebugInformationFormat>\n", 3);
  1054. }
  1055. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1056. "\n");
  1057. this->WriteString("<AssemblerListingLocation>", 3);
  1058. *this->BuildFileStream << configName
  1059. << "</AssemblerListingLocation>\n";
  1060. this->WriteString("<ObjectFileName>$(IntDir)</ObjectFileName>\n", 3);
  1061. this->WriteString("<ProgramDataBaseFileName>", 3);
  1062. *this->BuildFileStream << this->Target->GetDirectory(configName.c_str())
  1063. << "/"
  1064. << this->Target->GetPDBName(configName.c_str())
  1065. << "</ProgramDataBaseFileName>\n";
  1066. this->WriteString("</ClCompile>\n", 2);
  1067. }
  1068. void cmVisualStudio10TargetGenerator::
  1069. OutputIncludes(std::vector<std::string> const & includes)
  1070. {
  1071. this->WriteString("<AdditionalIncludeDirectories>", 3);
  1072. for(std::vector<std::string>::const_iterator i = includes.begin();
  1073. i != includes.end(); ++i)
  1074. {
  1075. *this->BuildFileStream << *i << ";";
  1076. }
  1077. this->WriteString("%(AdditionalIncludeDirectories)"
  1078. "</AdditionalIncludeDirectories>\n", 0);
  1079. }
  1080. void cmVisualStudio10TargetGenerator::
  1081. WriteRCOptions(std::string const& configName,
  1082. std::vector<std::string> const & includes)
  1083. {
  1084. this->WriteString("<ResourceCompile>\n", 2);
  1085. Options& clOptions = *(this->ClOptions[configName]);
  1086. clOptions.OutputPreprocessorDefinitions(*this->BuildFileStream, " ",
  1087. "\n");
  1088. this->OutputIncludes(includes);
  1089. this->WriteString("</ResourceCompile>\n", 2);
  1090. }
  1091. void
  1092. cmVisualStudio10TargetGenerator::WriteLibOptions(std::string const& config)
  1093. {
  1094. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  1095. {
  1096. return;
  1097. }
  1098. const char* libflags = this->Target->GetProperty("STATIC_LIBRARY_FLAGS");
  1099. std::string flagsConfigVar = "STATIC_LIBRARY_FLAGS_";
  1100. flagsConfigVar += cmSystemTools::UpperCase(config);
  1101. const char* libflagsConfig =
  1102. this->Target->GetProperty(flagsConfigVar.c_str());
  1103. if(libflags || libflagsConfig)
  1104. {
  1105. this->WriteString("<Lib>\n", 2);
  1106. cmVisualStudioGeneratorOptions
  1107. libOptions(this->LocalGenerator, 10,
  1108. cmVisualStudioGeneratorOptions::Linker,
  1109. cmVS10LibFlagTable, 0, this);
  1110. libOptions.Parse(libflags?libflags:"");
  1111. libOptions.Parse(libflagsConfig?libflagsConfig:"");
  1112. libOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1113. libOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1114. this->WriteString("</Lib>\n", 2);
  1115. }
  1116. }
  1117. void cmVisualStudio10TargetGenerator::WriteLinkOptions(std::string const&
  1118. config)
  1119. {
  1120. // static libraries and things greater than modules do not need
  1121. // to set this option
  1122. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY
  1123. || this->Target->GetType() > cmTarget::MODULE_LIBRARY)
  1124. {
  1125. return;
  1126. }
  1127. const char* linkLanguage =
  1128. this->Target->GetLinkerLanguage(config.c_str());
  1129. if(!linkLanguage)
  1130. {
  1131. cmSystemTools::Error
  1132. ("CMake can not determine linker language for target:",
  1133. this->Name.c_str());
  1134. return;
  1135. }
  1136. this->WriteString("<Link>\n", 2);
  1137. std::string CONFIG = cmSystemTools::UpperCase(config);
  1138. const char* linkType = "SHARED";
  1139. if(this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  1140. {
  1141. linkType = "MODULE";
  1142. }
  1143. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1144. {
  1145. linkType = "EXE";
  1146. }
  1147. std::string stackVar = "CMAKE_";
  1148. stackVar += linkLanguage;
  1149. stackVar += "_STACK_SIZE";
  1150. const char* stackVal = this->Makefile->GetDefinition(stackVar.c_str());
  1151. std::string flags;
  1152. if(stackVal)
  1153. {
  1154. flags += " ";
  1155. flags += stackVal;
  1156. }
  1157. // assume incremental linking
  1158. std::string linkFlagVarBase = "CMAKE_";
  1159. linkFlagVarBase += linkType;
  1160. linkFlagVarBase += "_LINKER_FLAGS";
  1161. flags += " ";
  1162. flags += this->
  1163. Target->GetMakefile()->GetRequiredDefinition(linkFlagVarBase.c_str());
  1164. std::string linkFlagVar = linkFlagVarBase + "_" + CONFIG;
  1165. flags += " ";
  1166. flags += this->
  1167. Target->GetMakefile()->GetRequiredDefinition(linkFlagVar.c_str());
  1168. const char* targetLinkFlags = this->Target->GetProperty("LINK_FLAGS");
  1169. if(targetLinkFlags)
  1170. {
  1171. flags += " ";
  1172. flags += targetLinkFlags;
  1173. }
  1174. std::string flagsProp = "LINK_FLAGS_";
  1175. flagsProp += CONFIG;
  1176. if(const char* flagsConfig = this->Target->GetProperty(flagsProp.c_str()))
  1177. {
  1178. flags += " ";
  1179. flags += flagsConfig;
  1180. }
  1181. cmVisualStudioGeneratorOptions
  1182. linkOptions(this->LocalGenerator, 10,
  1183. cmVisualStudioGeneratorOptions::Linker,
  1184. cmVS10LinkFlagTable, 0, this);
  1185. if ( this->Target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1186. {
  1187. flags += " /SUBSYSTEM:WINDOWS";
  1188. }
  1189. else
  1190. {
  1191. flags += " /SUBSYSTEM:CONSOLE";
  1192. }
  1193. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:YES", "");
  1194. cmSystemTools::ReplaceString(flags, "/INCREMENTAL:NO", "");
  1195. std::string standardLibsVar = "CMAKE_";
  1196. standardLibsVar += linkLanguage;
  1197. standardLibsVar += "_STANDARD_LIBRARIES";
  1198. std::string
  1199. libs = this->Makefile->GetSafeDefinition(standardLibsVar.c_str());
  1200. // Remove trailing spaces from libs
  1201. std::string::size_type pos = libs.size()-1;
  1202. if(libs.size() != 0)
  1203. {
  1204. while(libs[pos] == ' ')
  1205. {
  1206. pos--;
  1207. }
  1208. }
  1209. if(pos != libs.size()-1)
  1210. {
  1211. libs = libs.substr(0, pos+1);
  1212. }
  1213. // Replace spaces in libs with ;
  1214. cmSystemTools::ReplaceString(libs, " ", ";");
  1215. cmComputeLinkInformation* pcli =
  1216. this->Target->GetLinkInformation(config.c_str());
  1217. if(!pcli)
  1218. {
  1219. cmSystemTools::Error
  1220. ("CMake can not compute cmComputeLinkInformation for target:",
  1221. this->Name.c_str());
  1222. return;
  1223. }
  1224. // add the libraries for the target to libs string
  1225. cmComputeLinkInformation& cli = *pcli;
  1226. this->AddLibraries(cli, libs);
  1227. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1228. std::vector<std::string> const& ldirs = cli.GetDirectories();
  1229. const char* sep = "";
  1230. std::string linkDirs;
  1231. for(std::vector<std::string>::const_iterator d = ldirs.begin();
  1232. d != ldirs.end(); ++d)
  1233. {
  1234. // first just full path
  1235. linkDirs += sep;
  1236. linkDirs += *d;
  1237. sep = ";";
  1238. linkDirs += sep;
  1239. // next path with configuration type Debug, Release, etc
  1240. linkDirs += *d;
  1241. linkDirs += "/$(Configuration)";
  1242. linkDirs += sep;
  1243. }
  1244. linkDirs += "%(AdditionalLibraryDirectories)";
  1245. linkOptions.AddFlag("AdditionalLibraryDirectories", linkDirs.c_str());
  1246. linkOptions.AddFlag("AdditionalDependencies", libs.c_str());
  1247. linkOptions.AddFlag("Version", "0.0");
  1248. if(linkOptions.IsDebug() || flags.find("/debug") != flags.npos)
  1249. {
  1250. linkOptions.AddFlag("GenerateDebugInformation", "true");
  1251. }
  1252. else
  1253. {
  1254. linkOptions.AddFlag("GenerateDebugInformation", "false");
  1255. }
  1256. std::string targetName;
  1257. std::string targetNameSO;
  1258. std::string targetNameFull;
  1259. std::string targetNameImport;
  1260. std::string targetNamePDB;
  1261. if(this->Target->GetType() == cmTarget::EXECUTABLE)
  1262. {
  1263. this->Target->GetExecutableNames(targetName, targetNameFull,
  1264. targetNameImport, targetNamePDB,
  1265. config.c_str());
  1266. }
  1267. else
  1268. {
  1269. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameFull,
  1270. targetNameImport, targetNamePDB,
  1271. config.c_str());
  1272. }
  1273. std::string dir = this->Target->GetDirectory(config.c_str());
  1274. dir += "/";
  1275. std::string pdb = dir;
  1276. pdb += targetNamePDB;
  1277. std::string imLib = this->Target->GetDirectory(config.c_str(), true);
  1278. imLib += "/";
  1279. imLib += targetNameImport;
  1280. linkOptions.AddFlag("ImportLibrary", imLib.c_str());
  1281. linkOptions.AddFlag("ProgramDataBaseFileName", pdb.c_str());
  1282. linkOptions.Parse(flags.c_str());
  1283. if(!this->ModuleDefinitionFile.empty())
  1284. {
  1285. linkOptions.AddFlag("ModuleDefinitionFile",
  1286. this->ModuleDefinitionFile.c_str());
  1287. }
  1288. linkOptions.OutputAdditionalOptions(*this->BuildFileStream, " ", "");
  1289. linkOptions.OutputFlagMap(*this->BuildFileStream, " ");
  1290. this->WriteString("</Link>\n", 2);
  1291. if(!this->GlobalGenerator->NeedLinkLibraryDependencies(*this->Target))
  1292. {
  1293. this->WriteString("<ProjectReference>\n", 2);
  1294. this->WriteString(
  1295. " <LinkLibraryDependencies>false</LinkLibraryDependencies>\n", 2);
  1296. this->WriteString("</ProjectReference>\n", 2);
  1297. }
  1298. }
  1299. void cmVisualStudio10TargetGenerator::AddLibraries(
  1300. cmComputeLinkInformation& cli,
  1301. std::string& libstring)
  1302. {
  1303. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1304. ItemVector libs = cli.GetItems();
  1305. const char* sep = ";";
  1306. for(ItemVector::const_iterator l = libs.begin(); l != libs.end(); ++l)
  1307. {
  1308. if(l->IsPath)
  1309. {
  1310. std::string path = this->LocalGenerator->
  1311. Convert(l->Value.c_str(),
  1312. cmLocalGenerator::START_OUTPUT,
  1313. cmLocalGenerator::UNCHANGED);
  1314. this->ConvertToWindowsSlash(path);
  1315. libstring += sep;
  1316. libstring += path;
  1317. }
  1318. else
  1319. {
  1320. libstring += sep;
  1321. libstring += l->Value;
  1322. }
  1323. }
  1324. }
  1325. void cmVisualStudio10TargetGenerator::
  1326. WriteMidlOptions(std::string const& /*config*/,
  1327. std::vector<std::string> const & includes)
  1328. {
  1329. this->WriteString("<Midl>\n", 2);
  1330. this->OutputIncludes(includes);
  1331. this->WriteString("<OutputDirectory>$(IntDir)</OutputDirectory>\n", 3);
  1332. this->WriteString("<HeaderFileName>%(Filename).h</HeaderFileName>\n", 3);
  1333. this->WriteString(
  1334. "<TypeLibraryName>%(Filename).tlb</TypeLibraryName>\n", 3);
  1335. this->WriteString(
  1336. "<InterfaceIdentifierFileName>"
  1337. "%(Filename)_i.c</InterfaceIdentifierFileName>\n", 3);
  1338. this->WriteString("<ProxyFileName>%(Filename)_p.c</ProxyFileName>\n",3);
  1339. this->WriteString("</Midl>\n", 2);
  1340. }
  1341. void cmVisualStudio10TargetGenerator::WriteItemDefinitionGroups()
  1342. {
  1343. std::vector<std::string> *configs =
  1344. static_cast<cmGlobalVisualStudio7Generator *>
  1345. (this->GlobalGenerator)->GetConfigurations();
  1346. std::vector<std::string> includes;
  1347. this->LocalGenerator->GetIncludeDirectories(includes);
  1348. for(std::vector<std::string>::iterator i = configs->begin();
  1349. i != configs->end(); ++i)
  1350. {
  1351. this->WritePlatformConfigTag("ItemDefinitionGroup", i->c_str(), 1);
  1352. *this->BuildFileStream << "\n";
  1353. // output cl compile flags <ClCompile></ClCompile>
  1354. if(this->Target->GetType() <= cmTarget::MODULE_LIBRARY)
  1355. {
  1356. this->WriteClOptions(*i, includes);
  1357. // output rc compile flags <ResourceCompile></ResourceCompile>
  1358. this->WriteRCOptions(*i, includes);
  1359. }
  1360. // output midl flags <Midl></Midl>
  1361. this->WriteMidlOptions(*i, includes);
  1362. // write events
  1363. this->WriteEvents(*i);
  1364. // output link flags <Link></Link>
  1365. this->WriteLinkOptions(*i);
  1366. // output lib flags <Lib></Lib>
  1367. this->WriteLibOptions(*i);
  1368. this->WriteString("</ItemDefinitionGroup>\n", 1);
  1369. }
  1370. }
  1371. void
  1372. cmVisualStudio10TargetGenerator::WriteEvents(std::string const& configName)
  1373. {
  1374. this->WriteEvent("PreLinkEvent",
  1375. this->Target->GetPreLinkCommands(), configName);
  1376. this->WriteEvent("PreBuildEvent",
  1377. this->Target->GetPreBuildCommands(), configName);
  1378. this->WriteEvent("PostBuildEvent",
  1379. this->Target->GetPostBuildCommands(), configName);
  1380. }
  1381. void cmVisualStudio10TargetGenerator::WriteEvent(
  1382. const char* name,
  1383. std::vector<cmCustomCommand> & commands,
  1384. std::string const& configName)
  1385. {
  1386. if(commands.size() == 0)
  1387. {
  1388. return;
  1389. }
  1390. this->WriteString("<", 2);
  1391. (*this->BuildFileStream ) << name << ">\n";
  1392. cmLocalVisualStudio7Generator* lg = this->LocalGenerator;
  1393. std::string script;
  1394. const char* pre = "";
  1395. std::string comment;
  1396. for(std::vector<cmCustomCommand>::iterator i = commands.begin();
  1397. i != commands.end(); ++i)
  1398. {
  1399. cmCustomCommand& command = *i;
  1400. comment += pre;
  1401. comment += lg->ConstructComment(command);
  1402. script += pre;
  1403. pre = "\n";
  1404. script +=
  1405. cmVS10EscapeXML(lg->ConstructScript(command, configName.c_str()));
  1406. }
  1407. comment = cmVS10EscapeComment(comment);
  1408. this->WriteString("<Message>",3);
  1409. (*this->BuildFileStream ) << cmVS10EscapeXML(comment) << "</Message>\n";
  1410. this->WriteString("<Command>", 3);
  1411. (*this->BuildFileStream ) << script;
  1412. (*this->BuildFileStream ) << "</Command>" << "\n";
  1413. this->WriteString("</", 2);
  1414. (*this->BuildFileStream ) << name << ">\n";
  1415. }
  1416. void cmVisualStudio10TargetGenerator::WriteProjectReferences()
  1417. {
  1418. cmGlobalGenerator::TargetDependSet const& unordered
  1419. = this->GlobalGenerator->GetTargetDirectDepends(*this->Target);
  1420. typedef cmGlobalVisualStudioGenerator::OrderedTargetDependSet
  1421. OrderedTargetDependSet;
  1422. OrderedTargetDependSet depends(unordered);
  1423. this->WriteString("<ItemGroup>\n", 1);
  1424. for( OrderedTargetDependSet::const_iterator i = depends.begin();
  1425. i != depends.end(); ++i)
  1426. {
  1427. cmTarget* dt = *i;
  1428. this->WriteString("<ProjectReference Include=\"", 2);
  1429. cmMakefile* mf = dt->GetMakefile();
  1430. std::string name = dt->GetName();
  1431. std::string path;
  1432. const char* p = dt->GetProperty("EXTERNAL_MSPROJECT");
  1433. if(p)
  1434. {
  1435. path = p;
  1436. }
  1437. else
  1438. {
  1439. path = mf->GetStartOutputDirectory();
  1440. path += "/";
  1441. path += dt->GetName();
  1442. path += ".vcxproj";
  1443. }
  1444. (*this->BuildFileStream) << path << "\">\n";
  1445. this->WriteString("<Project>", 3);
  1446. (*this->BuildFileStream)
  1447. << this->GlobalGenerator->GetGUID(name.c_str())
  1448. << "</Project>\n";
  1449. this->WriteString("</ProjectReference>\n", 2);
  1450. }
  1451. this->WriteString("</ItemGroup>\n", 1);
  1452. }