cmGlobalVisualStudio7Generator.cxx 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 "windows.h" // this must be first to define GetCurrentDirectory
  11. #include <assert.h>
  12. #include "cmGlobalVisualStudio7Generator.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmLocalVisualStudio7Generator.h"
  15. #include "cmMakefile.h"
  16. #include "cmake.h"
  17. #include <cmsys/Encoding.hxx>
  18. cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator(
  19. const std::string& platformName)
  20. {
  21. this->IntelProjectVersion = 0;
  22. this->DevEnvCommandInitialized = false;
  23. this->MasmEnabled = false;
  24. if (platformName.empty())
  25. {
  26. this->DefaultPlatformName = "Win32";
  27. }
  28. else
  29. {
  30. this->DefaultPlatformName = platformName;
  31. }
  32. }
  33. cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
  34. {
  35. free(this->IntelProjectVersion);
  36. }
  37. // Package GUID of Intel Visual Fortran plugin to VS IDE
  38. #define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"
  39. const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
  40. {
  41. if(!this->IntelProjectVersion)
  42. {
  43. // Compute the version of the Intel plugin to the VS IDE.
  44. // If the key does not exist then use a default guess.
  45. std::string intelVersion;
  46. std::string vskey = this->GetRegistryBase();
  47. vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
  48. cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
  49. cmSystemTools::KeyWOW64_32);
  50. unsigned int intelVersionNumber = ~0u;
  51. sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
  52. if(intelVersionNumber >= 11)
  53. {
  54. // Default to latest known project file version.
  55. intelVersion = "11.0";
  56. }
  57. else if(intelVersionNumber == 10)
  58. {
  59. // Version 10.x actually uses 9.10 in project files!
  60. intelVersion = "9.10";
  61. }
  62. else
  63. {
  64. // Version <= 9: use ProductVersion from registry.
  65. }
  66. this->IntelProjectVersion = strdup(intelVersion.c_str());
  67. }
  68. return this->IntelProjectVersion;
  69. }
  70. void cmGlobalVisualStudio7Generator
  71. ::EnableLanguage(std::vector<std::string>const & lang,
  72. cmMakefile *mf, bool optional)
  73. {
  74. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  75. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  76. if(!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  77. {
  78. mf->AddCacheDefinition(
  79. "CMAKE_CONFIGURATION_TYPES",
  80. "Debug;Release;MinSizeRel;RelWithDebInfo",
  81. "Semicolon separated list of supported configuration types, "
  82. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  83. "anything else will be ignored.",
  84. cmCacheManager::STRING);
  85. }
  86. // Create list of configurations requested by user's cache, if any.
  87. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  88. this->GenerateConfigurations(mf);
  89. // if this environment variable is set, then copy it to
  90. // a static cache entry. It will be used by
  91. // cmLocalGenerator::ConstructScript, to add an extra PATH
  92. // to all custom commands. This is because the VS IDE
  93. // does not use the environment it is run in, and this allows
  94. // for running commands and using dll's that the IDE environment
  95. // does not know about.
  96. const char* extraPath = cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH");
  97. if(extraPath)
  98. {
  99. mf->AddCacheDefinition
  100. ("CMAKE_MSVCIDE_RUN_PATH", extraPath,
  101. "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
  102. cmCacheManager::STATIC);
  103. }
  104. }
  105. //----------------------------------------------------------------------------
  106. void cmGlobalVisualStudio7Generator::FindMakeProgram(cmMakefile* mf)
  107. {
  108. this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf);
  109. mf->AddDefinition("CMAKE_VS_DEVENV_COMMAND",
  110. this->GetDevEnvCommand().c_str());
  111. }
  112. //----------------------------------------------------------------------------
  113. std::string const& cmGlobalVisualStudio7Generator::GetDevEnvCommand()
  114. {
  115. if(!this->DevEnvCommandInitialized)
  116. {
  117. this->DevEnvCommandInitialized = true;
  118. this->DevEnvCommand = this->FindDevEnvCommand();
  119. }
  120. return this->DevEnvCommand;
  121. }
  122. //----------------------------------------------------------------------------
  123. std::string cmGlobalVisualStudio7Generator::FindDevEnvCommand()
  124. {
  125. std::string vscmd;
  126. std::string vskey = this->GetRegistryBase() + ";InstallDir";
  127. if(cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  128. cmSystemTools::KeyWOW64_32))
  129. {
  130. cmSystemTools::ConvertToUnixSlashes(vscmd);
  131. vscmd += "/";
  132. }
  133. vscmd += "devenv.com";
  134. return vscmd;
  135. }
  136. //----------------------------------------------------------------------------
  137. const char* cmGlobalVisualStudio7Generator::ExternalProjectType(
  138. const char* location)
  139. {
  140. std::string extension = cmSystemTools::GetFilenameLastExtension(location);
  141. if (extension == ".vbproj")
  142. {
  143. return "F184B08F-C81C-45F6-A57F-5ABD9991F28F";
  144. }
  145. else if (extension == ".csproj")
  146. {
  147. return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  148. }
  149. else if (extension == ".fsproj")
  150. {
  151. return "F2A71F9B-5D33-465A-A702-920D77279786";
  152. }
  153. else if (extension == ".vdproj")
  154. {
  155. return "54435603-DBB4-11D2-8724-00A0C9A8B90C";
  156. }
  157. else if (extension == ".dbproj")
  158. {
  159. return "C8D11400-126E-41CD-887F-60BD40844F9E";
  160. }
  161. else if (extension == ".wixproj")
  162. {
  163. return "930C7802-8A8C-48F9-8165-68863BCCD9DD";
  164. }
  165. else if (extension == ".pyproj")
  166. {
  167. return "888888A0-9F3D-457C-B088-3A5042F75D52";
  168. }
  169. return "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";
  170. }
  171. //----------------------------------------------------------------------------
  172. void cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  173. std::vector<std::string>& makeCommand,
  174. const std::string& makeProgram,
  175. const std::string& projectName,
  176. const std::string& /*projectDir*/,
  177. const std::string& targetName,
  178. const std::string& config,
  179. bool /*fast*/,
  180. std::vector<std::string> const& makeOptions)
  181. {
  182. // Select the caller- or user-preferred make program, else devenv.
  183. std::string makeProgramSelected =
  184. this->SelectMakeProgram(makeProgram, this->GetDevEnvCommand());
  185. // Ignore the above preference if it is msbuild.
  186. // Assume any other value is either a devenv or
  187. // command-line compatible with devenv.
  188. std::string makeProgramLower = makeProgramSelected;
  189. cmSystemTools::LowerCase(makeProgramLower);
  190. if(makeProgramLower.find("msbuild") != std::string::npos)
  191. {
  192. makeProgramSelected = this->GetDevEnvCommand();
  193. }
  194. makeCommand.push_back(makeProgramSelected);
  195. makeCommand.push_back(std::string(projectName) + ".sln");
  196. std::string realTarget = targetName;
  197. bool clean = false;
  198. if ( realTarget == "clean" )
  199. {
  200. clean = true;
  201. realTarget = "ALL_BUILD";
  202. }
  203. if(clean)
  204. {
  205. makeCommand.push_back("/clean");
  206. }
  207. else
  208. {
  209. makeCommand.push_back("/build");
  210. }
  211. if(!config.empty())
  212. {
  213. makeCommand.push_back(config);
  214. }
  215. else
  216. {
  217. makeCommand.push_back("Debug");
  218. }
  219. makeCommand.push_back("/project");
  220. if (!realTarget.empty())
  221. {
  222. makeCommand.push_back(realTarget);
  223. }
  224. else
  225. {
  226. makeCommand.push_back("ALL_BUILD");
  227. }
  228. makeCommand.insert(makeCommand.end(),
  229. makeOptions.begin(), makeOptions.end());
  230. }
  231. ///! Create a local generator appropriate to this Global Generator
  232. cmLocalGenerator *cmGlobalVisualStudio7Generator::CreateLocalGenerator()
  233. {
  234. cmLocalVisualStudio7Generator *lg =
  235. new cmLocalVisualStudio7Generator(cmLocalVisualStudioGenerator::VS7);
  236. lg->SetExtraFlagTable(this->GetExtraFlagTableVS7());
  237. lg->SetGlobalGenerator(this);
  238. return lg;
  239. }
  240. //----------------------------------------------------------------------------
  241. std::string const& cmGlobalVisualStudio7Generator::GetPlatformName() const
  242. {
  243. if(!this->GeneratorPlatform.empty())
  244. {
  245. return this->GeneratorPlatform;
  246. }
  247. return this->DefaultPlatformName;
  248. }
  249. //----------------------------------------------------------------------------
  250. bool cmGlobalVisualStudio7Generator::SetSystemName(std::string const& s,
  251. cmMakefile* mf)
  252. {
  253. mf->AddDefinition("CMAKE_VS_INTEL_Fortran_PROJECT_VERSION",
  254. this->GetIntelProjectVersion());
  255. return this->cmGlobalVisualStudioGenerator::SetSystemName(s, mf);
  256. }
  257. //----------------------------------------------------------------------------
  258. bool cmGlobalVisualStudio7Generator::SetGeneratorPlatform(std::string const& p,
  259. cmMakefile* mf)
  260. {
  261. if(this->GetPlatformName() == "x64")
  262. {
  263. mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
  264. }
  265. else if(this->GetPlatformName() == "Itanium")
  266. {
  267. mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
  268. }
  269. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName().c_str());
  270. return this->cmGlobalVisualStudioGenerator::SetGeneratorPlatform(p, mf);
  271. }
  272. void cmGlobalVisualStudio7Generator::GenerateConfigurations(cmMakefile* mf)
  273. {
  274. // process the configurations
  275. const char* ct
  276. = this->CMakeInstance->GetCacheDefinition("CMAKE_CONFIGURATION_TYPES");
  277. if ( ct )
  278. {
  279. std::vector<std::string> argsOut;
  280. cmSystemTools::ExpandListArgument(ct, argsOut);
  281. for(std::vector<std::string>::iterator i = argsOut.begin();
  282. i != argsOut.end(); ++i)
  283. {
  284. if(std::find(this->Configurations.begin(),
  285. this->Configurations.end(),
  286. *i) == this->Configurations.end())
  287. {
  288. this->Configurations.push_back(*i);
  289. }
  290. }
  291. }
  292. // default to at least Debug and Release
  293. if(this->Configurations.size() == 0)
  294. {
  295. this->Configurations.push_back("Debug");
  296. this->Configurations.push_back("Release");
  297. }
  298. // Reset the entry to have a semi-colon separated list.
  299. std::string configs = this->Configurations[0];
  300. for(unsigned int i=1; i < this->Configurations.size(); ++i)
  301. {
  302. configs += ";";
  303. configs += this->Configurations[i];
  304. }
  305. mf->AddCacheDefinition(
  306. "CMAKE_CONFIGURATION_TYPES",
  307. configs.c_str(),
  308. "Semicolon separated list of supported configuration types, "
  309. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  310. "anything else will be ignored.",
  311. cmCacheManager::STRING);
  312. }
  313. void cmGlobalVisualStudio7Generator::Generate()
  314. {
  315. // first do the superclass method
  316. this->cmGlobalVisualStudioGenerator::Generate();
  317. // Now write out the DSW
  318. this->OutputSLNFile();
  319. // If any solution or project files changed during the generation,
  320. // tell Visual Studio to reload them...
  321. if(!cmSystemTools::GetErrorOccuredFlag())
  322. {
  323. this->CallVisualStudioMacro(MacroReload);
  324. }
  325. }
  326. void cmGlobalVisualStudio7Generator
  327. ::OutputSLNFile(cmLocalGenerator* root,
  328. std::vector<cmLocalGenerator*>& generators)
  329. {
  330. if(generators.size() == 0)
  331. {
  332. return;
  333. }
  334. this->CurrentProject = root->GetMakefile()->GetProjectName();
  335. std::string fname = root->GetMakefile()->GetStartOutputDirectory();
  336. fname += "/";
  337. fname += root->GetMakefile()->GetProjectName();
  338. fname += ".sln";
  339. cmGeneratedFileStream fout(fname.c_str());
  340. fout.SetCopyIfDifferent(true);
  341. if(!fout)
  342. {
  343. return;
  344. }
  345. this->WriteSLNFile(fout, root, generators);
  346. if (fout.Close())
  347. {
  348. this->FileReplacedDuringGenerate(fname);
  349. }
  350. }
  351. // output the SLN file
  352. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  353. {
  354. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  355. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  356. {
  357. this->OutputSLNFile(it->second[0], it->second);
  358. }
  359. }
  360. void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
  361. std::ostream& fout,
  362. OrderedTargetDependSet const& projectTargets)
  363. {
  364. // loop over again and write out configurations for each target
  365. // in the solution
  366. for(OrderedTargetDependSet::const_iterator tt =
  367. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  368. {
  369. cmTarget const* target = *tt;
  370. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  371. {
  372. continue;
  373. }
  374. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  375. if(expath)
  376. {
  377. std::set<std::string> allConfigurations(this->Configurations.begin(),
  378. this->Configurations.end());
  379. const char* mapping = target->GetProperty("VS_PLATFORM_MAPPING");
  380. this->WriteProjectConfigurations(
  381. fout, target->GetName().c_str(), target->GetType(),
  382. allConfigurations, mapping ? mapping : "");
  383. }
  384. else
  385. {
  386. const std::set<std::string>& configsPartOfDefaultBuild =
  387. this->IsPartOfDefaultBuild(projectTargets, target);
  388. const char *vcprojName =
  389. target->GetProperty("GENERATOR_FILE_NAME");
  390. if (vcprojName)
  391. {
  392. this->WriteProjectConfigurations(fout, vcprojName, target->GetType(),
  393. configsPartOfDefaultBuild);
  394. }
  395. }
  396. }
  397. }
  398. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  399. std::ostream& fout,
  400. cmLocalGenerator* root,
  401. OrderedTargetDependSet const& projectTargets)
  402. {
  403. VisualStudioFolders.clear();
  404. for(OrderedTargetDependSet::const_iterator tt =
  405. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  406. {
  407. cmTarget const* target = *tt;
  408. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  409. {
  410. continue;
  411. }
  412. bool written = false;
  413. // handle external vc project files
  414. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  415. if(expath)
  416. {
  417. std::string project = target->GetName();
  418. std::string location = expath;
  419. this->WriteExternalProject(fout,
  420. project.c_str(),
  421. location.c_str(),
  422. target->GetProperty("VS_PROJECT_TYPE"),
  423. target->GetUtilities());
  424. written = true;
  425. }
  426. else
  427. {
  428. const char *vcprojName =
  429. target->GetProperty("GENERATOR_FILE_NAME");
  430. if(vcprojName)
  431. {
  432. cmMakefile* tmf = target->GetMakefile();
  433. std::string dir = tmf->GetStartOutputDirectory();
  434. dir = root->Convert(dir.c_str(),
  435. cmLocalGenerator::START_OUTPUT);
  436. if(dir == ".")
  437. {
  438. dir = ""; // msbuild cannot handle ".\" prefix
  439. }
  440. this->WriteProject(fout, vcprojName, dir.c_str(),
  441. *target);
  442. written = true;
  443. }
  444. }
  445. // Create "solution folder" information from FOLDER target property
  446. //
  447. if (written && this->UseFolderProperty())
  448. {
  449. const char *targetFolder = target->GetProperty("FOLDER");
  450. if (targetFolder)
  451. {
  452. std::vector<cmsys::String> tokens =
  453. cmSystemTools::SplitString(targetFolder, '/', false);
  454. std::string cumulativePath = "";
  455. for(std::vector<cmsys::String>::iterator iter = tokens.begin();
  456. iter != tokens.end(); ++iter)
  457. {
  458. if(!iter->size())
  459. {
  460. continue;
  461. }
  462. if (cumulativePath.empty())
  463. {
  464. cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
  465. }
  466. else
  467. {
  468. VisualStudioFolders[cumulativePath].insert(
  469. cumulativePath + "/" + *iter);
  470. cumulativePath = cumulativePath + "/" + *iter;
  471. }
  472. this->CreateGUID(cumulativePath.c_str());
  473. }
  474. if (!cumulativePath.empty())
  475. {
  476. VisualStudioFolders[cumulativePath].insert(target->GetName());
  477. }
  478. }
  479. }
  480. }
  481. }
  482. void cmGlobalVisualStudio7Generator::WriteTargetDepends(
  483. std::ostream& fout,
  484. OrderedTargetDependSet const& projectTargets
  485. )
  486. {
  487. for(OrderedTargetDependSet::const_iterator tt =
  488. projectTargets.begin(); tt != projectTargets.end(); ++tt)
  489. {
  490. cmTarget const* target = *tt;
  491. if(target->GetType() == cmTarget::INTERFACE_LIBRARY)
  492. {
  493. continue;
  494. }
  495. cmMakefile* mf = target->GetMakefile();
  496. const char *vcprojName =
  497. target->GetProperty("GENERATOR_FILE_NAME");
  498. if (vcprojName)
  499. {
  500. std::string dir = mf->GetStartDirectory();
  501. this->WriteProjectDepends(fout, vcprojName,
  502. dir.c_str(), *target);
  503. }
  504. }
  505. }
  506. //----------------------------------------------------------------------------
  507. // Write a SLN file to the stream
  508. void cmGlobalVisualStudio7Generator
  509. ::WriteSLNFile(std::ostream& fout,
  510. cmLocalGenerator* root,
  511. std::vector<cmLocalGenerator*>& generators)
  512. {
  513. #ifdef CMAKE_ENCODING_UTF8
  514. // Add UTF-8 BOM for .sln file to indicate encoding
  515. const unsigned char utf8_bom[3] = {0xEF,0xBB,0xBF};
  516. fout.write(reinterpret_cast<const char*>(utf8_bom), 3);
  517. #endif
  518. // Write out the header for a SLN file
  519. this->WriteSLNHeader(fout);
  520. // Collect all targets under this root generator and the transitive
  521. // closure of their dependencies.
  522. TargetDependSet projectTargets;
  523. TargetDependSet originalTargets;
  524. this->GetTargetSets(projectTargets, originalTargets, root, generators);
  525. OrderedTargetDependSet orderedProjectTargets(projectTargets);
  526. this->WriteTargetsToSolution(fout, root, orderedProjectTargets);
  527. bool useFolderProperty = this->UseFolderProperty();
  528. if (useFolderProperty)
  529. {
  530. this->WriteFolders(fout);
  531. }
  532. // Write out the configurations information for the solution
  533. fout << "Global\n"
  534. << "\tGlobalSection(SolutionConfiguration) = preSolution\n";
  535. int c = 0;
  536. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  537. i != this->Configurations.end(); ++i)
  538. {
  539. fout << "\t\tConfigName." << c << " = " << *i << "\n";
  540. c++;
  541. }
  542. fout << "\tEndGlobalSection\n";
  543. // Write out project(target) depends
  544. fout << "\tGlobalSection(ProjectDependencies) = postSolution\n";
  545. this->WriteTargetDepends(fout, orderedProjectTargets);
  546. fout << "\tEndGlobalSection\n";
  547. if (useFolderProperty)
  548. {
  549. // Write out project folders
  550. fout << "\tGlobalSection(NestedProjects) = preSolution\n";
  551. this->WriteFoldersContent(fout);
  552. fout << "\tEndGlobalSection\n";
  553. }
  554. // Write out the configurations for all the targets in the project
  555. fout << "\tGlobalSection(ProjectConfiguration) = postSolution\n";
  556. this->WriteTargetConfigurations(fout, orderedProjectTargets);
  557. fout << "\tEndGlobalSection\n";
  558. // Write out global sections
  559. this->WriteSLNGlobalSections(fout, root);
  560. // Write the footer for the SLN file
  561. this->WriteSLNFooter(fout);
  562. }
  563. //----------------------------------------------------------------------------
  564. void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
  565. {
  566. const char *prefix = "CMAKE_FOLDER_GUID_";
  567. const std::string::size_type skip_prefix = strlen(prefix);
  568. std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
  569. for(std::map<std::string,std::set<std::string> >::iterator iter =
  570. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  571. {
  572. std::string fullName = iter->first;
  573. std::string guid = this->GetGUID(fullName.c_str());
  574. cmSystemTools::ReplaceString(fullName, "/", "\\");
  575. if (cmSystemTools::StringStartsWith(fullName.c_str(), prefix))
  576. {
  577. fullName = fullName.substr(skip_prefix);
  578. }
  579. std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
  580. fout << "Project(\"{" <<
  581. guidProjectTypeFolder << "}\") = \"" <<
  582. nameOnly << "\", \"" <<
  583. fullName << "\", \"{" <<
  584. guid <<
  585. "}\"\nEndProject\n";
  586. }
  587. }
  588. //----------------------------------------------------------------------------
  589. void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
  590. {
  591. for(std::map<std::string,std::set<std::string> >::iterator iter =
  592. VisualStudioFolders.begin(); iter != VisualStudioFolders.end(); ++iter)
  593. {
  594. std::string key(iter->first);
  595. std::string guidParent(this->GetGUID(key.c_str()));
  596. for(std::set<std::string>::iterator it = iter->second.begin();
  597. it != iter->second.end(); ++it)
  598. {
  599. std::string value(*it);
  600. std::string guid(this->GetGUID(value.c_str()));
  601. fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
  602. }
  603. }
  604. }
  605. //----------------------------------------------------------------------------
  606. std::string
  607. cmGlobalVisualStudio7Generator::ConvertToSolutionPath(const char* path)
  608. {
  609. // Convert to backslashes. Do not use ConvertToOutputPath because
  610. // we will add quoting ourselves, and we know these projects always
  611. // use windows slashes.
  612. std::string d = path;
  613. std::string::size_type pos = 0;
  614. while((pos = d.find('/', pos)) != d.npos)
  615. {
  616. d[pos++] = '\\';
  617. }
  618. return d;
  619. }
  620. // Write a dsp file into the SLN file,
  621. // Note, that dependencies from executables to
  622. // the libraries it uses are also done here
  623. void cmGlobalVisualStudio7Generator::WriteProject(std::ostream& fout,
  624. const std::string& dspname,
  625. const char* dir, cmTarget const& target)
  626. {
  627. // check to see if this is a fortran build
  628. const char* ext = ".vcproj";
  629. const char* project =
  630. "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"";
  631. if(this->TargetIsFortranOnly(target))
  632. {
  633. ext = ".vfproj";
  634. project = "Project(\"{6989167D-11E4-40FE-8C1A-2192A86A7E90}\") = \"";
  635. }
  636. fout << project
  637. << dspname << "\", \""
  638. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  639. << dspname << ext << "\", \"{"
  640. << this->GetGUID(dspname) << "}\"\nEndProject\n";
  641. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  642. if(ui != this->UtilityDepends.end())
  643. {
  644. const char* uname = ui->second.c_str();
  645. fout << "Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \""
  646. << uname << "\", \""
  647. << this->ConvertToSolutionPath(dir) << (dir[0]? "\\":"")
  648. << uname << ".vcproj" << "\", \"{"
  649. << this->GetGUID(uname) << "}\"\n"
  650. << "EndProject\n";
  651. }
  652. }
  653. // Write a dsp file into the SLN file,
  654. // Note, that dependencies from executables to
  655. // the libraries it uses are also done here
  656. void
  657. cmGlobalVisualStudio7Generator
  658. ::WriteProjectDepends(std::ostream& fout,
  659. const std::string& dspname,
  660. const char*, cmTarget const& target)
  661. {
  662. int depcount = 0;
  663. std::string dspguid = this->GetGUID(dspname);
  664. VSDependSet const& depends = this->VSTargetDepends[&target];
  665. for(VSDependSet::const_iterator di = depends.begin();
  666. di != depends.end(); ++di)
  667. {
  668. const char* name = di->c_str();
  669. std::string guid = this->GetGUID(name);
  670. if(guid.size() == 0)
  671. {
  672. std::string m = "Target: ";
  673. m += target.GetName();
  674. m += " depends on unknown target: ";
  675. m += name;
  676. cmSystemTools::Error(m.c_str());
  677. }
  678. fout << "\t\t{" << dspguid << "}." << depcount << " = {" << guid << "}\n";
  679. depcount++;
  680. }
  681. UtilityDependsMap::iterator ui = this->UtilityDepends.find(&target);
  682. if(ui != this->UtilityDepends.end())
  683. {
  684. const char* uname = ui->second.c_str();
  685. fout << "\t\t{" << this->GetGUID(uname) << "}.0 = {" << dspguid << "}\n";
  686. }
  687. }
  688. // Write a dsp file into the SLN file, Note, that dependencies from
  689. // executables to the libraries it uses are also done here
  690. void cmGlobalVisualStudio7Generator
  691. ::WriteProjectConfigurations(
  692. std::ostream& fout, const std::string& name, cmTarget::TargetType,
  693. const std::set<std::string>& configsPartOfDefaultBuild,
  694. const std::string& platformMapping)
  695. {
  696. const std::string& platformName =
  697. !platformMapping.empty() ? platformMapping : this->GetPlatformName();
  698. std::string guid = this->GetGUID(name);
  699. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  700. i != this->Configurations.end(); ++i)
  701. {
  702. fout << "\t\t{" << guid << "}." << *i
  703. << ".ActiveCfg = " << *i << "|" << platformName << "\n";
  704. std::set<std::string>::const_iterator
  705. ci = configsPartOfDefaultBuild.find(*i);
  706. if(!(ci == configsPartOfDefaultBuild.end()))
  707. {
  708. fout << "\t\t{" << guid << "}." << *i
  709. << ".Build.0 = " << *i << "|" << platformName << "\n";
  710. }
  711. }
  712. }
  713. // Write a dsp file into the SLN file,
  714. // Note, that dependencies from executables to
  715. // the libraries it uses are also done here
  716. void cmGlobalVisualStudio7Generator::WriteExternalProject(std::ostream& fout,
  717. const std::string& name,
  718. const char* location,
  719. const char* typeGuid,
  720. const std::set<std::string>&)
  721. {
  722. fout << "Project("
  723. << "\"{"
  724. << (typeGuid ? typeGuid : this->ExternalProjectType(location))
  725. << "}\") = \""
  726. << name << "\", \""
  727. << this->ConvertToSolutionPath(location) << "\", \"{"
  728. << this->GetGUID(name)
  729. << "}\"\n";
  730. fout << "EndProject\n";
  731. }
  732. void cmGlobalVisualStudio7Generator
  733. ::WriteSLNGlobalSections(std::ostream& fout,
  734. cmLocalGenerator* root)
  735. {
  736. bool extensibilityGlobalsOverridden = false;
  737. bool extensibilityAddInsOverridden = false;
  738. const cmPropertyMap& props = root->GetMakefile()->GetProperties();
  739. for(cmPropertyMap::const_iterator itProp = props.begin();
  740. itProp != props.end(); ++itProp)
  741. {
  742. if(itProp->first.find("VS_GLOBAL_SECTION_") == 0)
  743. {
  744. std::string sectionType;
  745. std::string name = itProp->first.substr(18);
  746. if(name.find("PRE_") == 0)
  747. {
  748. name = name.substr(4);
  749. sectionType = "preSolution";
  750. }
  751. else if(name.find("POST_") == 0)
  752. {
  753. name = name.substr(5);
  754. sectionType = "postSolution";
  755. }
  756. else
  757. continue;
  758. if(!name.empty())
  759. {
  760. if(name == "ExtensibilityGlobals" && sectionType == "postSolution")
  761. extensibilityGlobalsOverridden = true;
  762. else if(name == "ExtensibilityAddIns" && sectionType == "postSolution")
  763. extensibilityAddInsOverridden = true;
  764. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  765. std::vector<std::string> keyValuePairs;
  766. cmSystemTools::ExpandListArgument(itProp->second.GetValue(),
  767. keyValuePairs);
  768. for(std::vector<std::string>::const_iterator itPair =
  769. keyValuePairs.begin(); itPair != keyValuePairs.end(); ++itPair)
  770. {
  771. const std::string::size_type posEqual = itPair->find('=');
  772. if(posEqual != std::string::npos)
  773. {
  774. const std::string key =
  775. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  776. const std::string value =
  777. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  778. fout << "\t\t" << key << " = " << value << "\n";
  779. }
  780. }
  781. fout << "\tEndGlobalSection\n";
  782. }
  783. }
  784. }
  785. if(!extensibilityGlobalsOverridden)
  786. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  787. << "\tEndGlobalSection\n";
  788. if(!extensibilityAddInsOverridden)
  789. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  790. << "\tEndGlobalSection\n";
  791. }
  792. // Standard end of dsw file
  793. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  794. {
  795. fout << "EndGlobal\n";
  796. }
  797. // ouput standard header for dsw file
  798. void cmGlobalVisualStudio7Generator::WriteSLNHeader(std::ostream& fout)
  799. {
  800. fout << "Microsoft Visual Studio Solution File, Format Version 7.00\n";
  801. }
  802. //----------------------------------------------------------------------------
  803. std::string
  804. cmGlobalVisualStudio7Generator::WriteUtilityDepend(cmTarget const* target)
  805. {
  806. std::string pname = target->GetName();
  807. pname += "_UTILITY";
  808. std::string fname = target->GetMakefile()->GetStartOutputDirectory();
  809. fname += "/";
  810. fname += pname;
  811. fname += ".vcproj";
  812. cmGeneratedFileStream fout(fname.c_str());
  813. fout.SetCopyIfDifferent(true);
  814. this->CreateGUID(pname.c_str());
  815. std::string guid = this->GetGUID(pname.c_str());
  816. fout <<
  817. "<?xml version=\"1.0\" encoding = \""
  818. << this->Encoding() << "\"?>\n"
  819. "<VisualStudioProject\n"
  820. "\tProjectType=\"Visual C++\"\n"
  821. "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
  822. "\tName=\"" << pname << "\"\n"
  823. "\tProjectGUID=\"{" << guid << "}\"\n"
  824. "\tKeyword=\"Win32Proj\">\n"
  825. "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
  826. "\t<Configurations>\n"
  827. ;
  828. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  829. i != this->Configurations.end(); ++i)
  830. {
  831. fout <<
  832. "\t\t<Configuration\n"
  833. "\t\t\tName=\"" << *i << "|Win32\"\n"
  834. "\t\t\tOutputDirectory=\"" << *i << "\"\n"
  835. "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << *i << "\"\n"
  836. "\t\t\tConfigurationType=\"10\"\n"
  837. "\t\t\tUseOfMFC=\"0\"\n"
  838. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  839. "\t\t\tCharacterSet=\"2\">\n"
  840. "\t\t</Configuration>\n"
  841. ;
  842. }
  843. fout <<
  844. "\t</Configurations>\n"
  845. "\t<Files></Files>\n"
  846. "\t<Globals></Globals>\n"
  847. "</VisualStudioProject>\n"
  848. ;
  849. if(fout.Close())
  850. {
  851. this->FileReplacedDuringGenerate(fname);
  852. }
  853. return pname;
  854. }
  855. std::string cmGlobalVisualStudio7Generator::GetGUID(const std::string& name)
  856. {
  857. std::string guidStoreName = name;
  858. guidStoreName += "_GUID_CMAKE";
  859. const char* storedGUID =
  860. this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str());
  861. if(storedGUID)
  862. {
  863. return std::string(storedGUID);
  864. }
  865. cmSystemTools::Error("Unknown Target referenced : ",
  866. name.c_str());
  867. return "";
  868. }
  869. void cmGlobalVisualStudio7Generator::CreateGUID(const std::string& name)
  870. {
  871. std::string guidStoreName = name;
  872. guidStoreName += "_GUID_CMAKE";
  873. if(this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str()))
  874. {
  875. return;
  876. }
  877. std::string ret;
  878. UUID uid;
  879. unsigned short *uidstr;
  880. UuidCreate(&uid);
  881. UuidToStringW(&uid,&uidstr);
  882. ret = cmsys::Encoding::ToNarrow(reinterpret_cast<wchar_t*>(uidstr));
  883. RpcStringFreeW(&uidstr);
  884. ret = cmSystemTools::UpperCase(ret);
  885. this->CMakeInstance->AddCacheEntry(guidStoreName.c_str(),
  886. ret.c_str(), "Stored GUID",
  887. cmCacheManager::INTERNAL);
  888. }
  889. std::vector<std::string> *cmGlobalVisualStudio7Generator::GetConfigurations()
  890. {
  891. return &this->Configurations;
  892. };
  893. //----------------------------------------------------------------------------
  894. void cmGlobalVisualStudio7Generator
  895. ::GetDocumentation(cmDocumentationEntry& entry)
  896. {
  897. entry.Name = cmGlobalVisualStudio7Generator::GetActualName();
  898. entry.Brief = "Generates Visual Studio .NET 2002 project files.";
  899. }
  900. //----------------------------------------------------------------------------
  901. void
  902. cmGlobalVisualStudio7Generator
  903. ::AppendDirectoryForConfig(const std::string& prefix,
  904. const std::string& config,
  905. const std::string& suffix,
  906. std::string& dir)
  907. {
  908. if(!config.empty())
  909. {
  910. dir += prefix;
  911. dir += config;
  912. dir += suffix;
  913. }
  914. }
  915. std::set<std::string>
  916. cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  917. OrderedTargetDependSet const& projectTargets, cmTarget const* target)
  918. {
  919. std::set<std::string> activeConfigs;
  920. // if it is a utilitiy target then only make it part of the
  921. // default build if another target depends on it
  922. int type = target->GetType();
  923. if (type == cmTarget::GLOBAL_TARGET)
  924. {
  925. return activeConfigs;
  926. }
  927. if(type == cmTarget::UTILITY && !this->IsDependedOn(projectTargets, target))
  928. {
  929. return activeConfigs;
  930. }
  931. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  932. for(std::vector<std::string>::iterator i = this->Configurations.begin();
  933. i != this->Configurations.end(); ++i)
  934. {
  935. const char* propertyValue =
  936. target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  937. if(cmSystemTools::IsOff(propertyValue))
  938. {
  939. activeConfigs.insert(*i);
  940. }
  941. }
  942. return activeConfigs;
  943. }
  944. bool
  945. cmGlobalVisualStudio7Generator
  946. ::IsDependedOn(OrderedTargetDependSet const& projectTargets,
  947. cmTarget const* targetIn)
  948. {
  949. for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
  950. l != projectTargets.end(); ++l)
  951. {
  952. cmTarget const& target = **l;
  953. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(target);
  954. if(tgtdeps.count(targetIn))
  955. {
  956. return true;
  957. }
  958. }
  959. return false;
  960. }
  961. //----------------------------------------------------------------------------
  962. static cmVS7FlagTable cmVS7ExtraFlagTable[] =
  963. {
  964. // Precompiled header and related options. Note that the
  965. // UsePrecompiledHeader entries are marked as "Continue" so that the
  966. // corresponding PrecompiledHeaderThrough entry can be found.
  967. {"UsePrecompiledHeader", "YX", "Automatically Generate", "2",
  968. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  969. {"PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
  970. cmVS7FlagTable::UserValueRequired},
  971. {"UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
  972. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue},
  973. {"PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  974. cmVS7FlagTable::UserValueRequired},
  975. {"WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "true", 0},
  976. // Exception handling mode. If no entries match, it will be FALSE.
  977. {"ExceptionHandling", "GX", "enable c++ exceptions", "true", 0},
  978. {"ExceptionHandling", "EHsc", "enable c++ exceptions", "true", 0},
  979. // The EHa option does not have an IDE setting. Let it go to false,
  980. // and have EHa passed on the command line by leaving out the table
  981. // entry.
  982. {0,0,0,0,0}
  983. };
  984. cmIDEFlagTable const* cmGlobalVisualStudio7Generator::GetExtraFlagTableVS7()
  985. {
  986. return cmVS7ExtraFlagTable;
  987. }
  988. std::string cmGlobalVisualStudio7Generator::Encoding()
  989. {
  990. std::ostringstream encoding;
  991. #ifdef CMAKE_ENCODING_UTF8
  992. encoding << "UTF-8";
  993. #else
  994. encoding << "Windows-1252";
  995. #endif
  996. return encoding.str();
  997. }