cmVisualStudio10TargetGenerator.cxx 51 KB

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