cmVisualStudio10TargetGenerator.cxx 50 KB

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