cmVisualStudio10TargetGenerator.cxx 51 KB

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