cmGlobalVisualStudio7Generator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGlobalVisualStudio7Generator.h"
  4. #include "cmGeneratedFileStream.h"
  5. #include "cmGeneratorTarget.h"
  6. #include "cmLocalVisualStudio7Generator.h"
  7. #include "cmMakefile.h"
  8. #include "cmState.h"
  9. #include "cmUuid.h"
  10. #include "cmake.h"
  11. #include "cmsys/Encoding.hxx"
  12. #include <assert.h>
  13. #include <vector>
  14. #include <windows.h>
  15. static cmVS7FlagTable cmVS7ExtraFlagTable[] = {
  16. // Precompiled header and related options. Note that the
  17. // UsePrecompiledHeader entries are marked as "Continue" so that the
  18. // corresponding PrecompiledHeaderThrough entry can be found.
  19. { "UsePrecompiledHeader", "YX", "Automatically Generate", "2",
  20. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  21. { "PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
  22. cmVS7FlagTable::UserValueRequired },
  23. { "UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
  24. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  25. { "PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  26. cmVS7FlagTable::UserValueRequired },
  27. { "WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "true",
  28. 0 },
  29. // Exception handling mode. If no entries match, it will be FALSE.
  30. { "ExceptionHandling", "GX", "enable c++ exceptions", "true", 0 },
  31. { "ExceptionHandling", "EHsc", "enable c++ exceptions", "true", 0 },
  32. // The EHa option does not have an IDE setting. Let it go to false,
  33. // and have EHa passed on the command line by leaving out the table
  34. // entry.
  35. { 0, 0, 0, 0, 0 }
  36. };
  37. cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator(
  38. cmake* cm, const std::string& platformName)
  39. : cmGlobalVisualStudioGenerator(cm)
  40. {
  41. this->IntelProjectVersion = 0;
  42. this->DevEnvCommandInitialized = false;
  43. this->MasmEnabled = false;
  44. this->NasmEnabled = false;
  45. if (platformName.empty()) {
  46. this->DefaultPlatformName = "Win32";
  47. } else {
  48. this->DefaultPlatformName = platformName;
  49. }
  50. this->ExtraFlagTable = cmVS7ExtraFlagTable;
  51. }
  52. cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
  53. {
  54. free(this->IntelProjectVersion);
  55. }
  56. // Package GUID of Intel Visual Fortran plugin to VS IDE
  57. #define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"
  58. const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
  59. {
  60. if (!this->IntelProjectVersion) {
  61. // Compute the version of the Intel plugin to the VS IDE.
  62. // If the key does not exist then use a default guess.
  63. std::string intelVersion;
  64. std::string vskey = this->GetRegistryBase();
  65. vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
  66. cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
  67. cmSystemTools::KeyWOW64_32);
  68. unsigned int intelVersionNumber = ~0u;
  69. sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
  70. if (intelVersionNumber >= 11) {
  71. // Default to latest known project file version.
  72. intelVersion = "11.0";
  73. } else if (intelVersionNumber == 10) {
  74. // Version 10.x actually uses 9.10 in project files!
  75. intelVersion = "9.10";
  76. } else {
  77. // Version <= 9: use ProductVersion from registry.
  78. }
  79. this->IntelProjectVersion = strdup(intelVersion.c_str());
  80. }
  81. return this->IntelProjectVersion;
  82. }
  83. void cmGlobalVisualStudio7Generator::EnableLanguage(
  84. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  85. {
  86. mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
  87. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  88. if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  89. mf->AddCacheDefinition(
  90. "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
  91. "Semicolon separated list of supported configuration types, "
  92. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  93. "anything else will be ignored.",
  94. cmStateEnums::STRING);
  95. }
  96. // Create list of configurations requested by user's cache, if any.
  97. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  98. // if this environment variable is set, then copy it to
  99. // a static cache entry. It will be used by
  100. // cmLocalGenerator::ConstructScript, to add an extra PATH
  101. // to all custom commands. This is because the VS IDE
  102. // does not use the environment it is run in, and this allows
  103. // for running commands and using dll's that the IDE environment
  104. // does not know about.
  105. std::string extraPath;
  106. if (cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH", extraPath)) {
  107. mf->AddCacheDefinition("CMAKE_MSVCIDE_RUN_PATH", extraPath.c_str(),
  108. "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
  109. cmStateEnums::STATIC);
  110. }
  111. }
  112. bool cmGlobalVisualStudio7Generator::FindMakeProgram(cmMakefile* mf)
  113. {
  114. if (!this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf)) {
  115. return false;
  116. }
  117. mf->AddDefinition("CMAKE_VS_DEVENV_COMMAND",
  118. this->GetDevEnvCommand().c_str());
  119. return true;
  120. }
  121. std::string const& cmGlobalVisualStudio7Generator::GetDevEnvCommand()
  122. {
  123. if (!this->DevEnvCommandInitialized) {
  124. this->DevEnvCommandInitialized = true;
  125. this->DevEnvCommand = this->FindDevEnvCommand();
  126. }
  127. return this->DevEnvCommand;
  128. }
  129. std::string cmGlobalVisualStudio7Generator::FindDevEnvCommand()
  130. {
  131. std::string vscmd;
  132. std::string vskey;
  133. // Search in standard location.
  134. vskey = this->GetRegistryBase() + ";InstallDir";
  135. if (cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  136. cmSystemTools::KeyWOW64_32)) {
  137. cmSystemTools::ConvertToUnixSlashes(vscmd);
  138. vscmd += "/devenv.com";
  139. if (cmSystemTools::FileExists(vscmd, true)) {
  140. return vscmd;
  141. }
  142. }
  143. // Search where VS15Preview places it.
  144. vskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7;";
  145. vskey += this->GetIDEVersion();
  146. if (cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
  147. cmSystemTools::KeyWOW64_32)) {
  148. cmSystemTools::ConvertToUnixSlashes(vscmd);
  149. vscmd += "/Common7/IDE/devenv.com";
  150. if (cmSystemTools::FileExists(vscmd, true)) {
  151. return vscmd;
  152. }
  153. }
  154. vscmd = "devenv.com";
  155. return vscmd;
  156. }
  157. const char* cmGlobalVisualStudio7Generator::ExternalProjectType(
  158. const char* location)
  159. {
  160. std::string extension = cmSystemTools::GetFilenameLastExtension(location);
  161. if (extension == ".vbproj") {
  162. return "F184B08F-C81C-45F6-A57F-5ABD9991F28F";
  163. } else if (extension == ".csproj") {
  164. return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
  165. } else if (extension == ".fsproj") {
  166. return "F2A71F9B-5D33-465A-A702-920D77279786";
  167. } else if (extension == ".vdproj") {
  168. return "54435603-DBB4-11D2-8724-00A0C9A8B90C";
  169. } else if (extension == ".dbproj") {
  170. return "C8D11400-126E-41CD-887F-60BD40844F9E";
  171. } else if (extension == ".wixproj") {
  172. return "930C7802-8A8C-48F9-8165-68863BCCD9DD";
  173. } else if (extension == ".pyproj") {
  174. return "888888A0-9F3D-457C-B088-3A5042F75D52";
  175. }
  176. return "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";
  177. }
  178. void cmGlobalVisualStudio7Generator::GenerateBuildCommand(
  179. std::vector<std::string>& makeCommand, const std::string& makeProgram,
  180. const std::string& projectName, const std::string& /*projectDir*/,
  181. const std::string& targetName, const std::string& config, bool /*fast*/,
  182. bool /*verbose*/, std::vector<std::string> const& makeOptions)
  183. {
  184. // Select the caller- or user-preferred make program, else devenv.
  185. std::string makeProgramSelected =
  186. this->SelectMakeProgram(makeProgram, this->GetDevEnvCommand());
  187. // Ignore the above preference if it is msbuild.
  188. // Assume any other value is either a devenv or
  189. // command-line compatible with devenv.
  190. std::string makeProgramLower = makeProgramSelected;
  191. cmSystemTools::LowerCase(makeProgramLower);
  192. if (makeProgramLower.find("msbuild") != std::string::npos) {
  193. makeProgramSelected = this->GetDevEnvCommand();
  194. }
  195. makeCommand.push_back(makeProgramSelected);
  196. makeCommand.push_back(std::string(projectName) + ".sln");
  197. std::string realTarget = targetName;
  198. bool clean = false;
  199. if (realTarget == "clean") {
  200. clean = true;
  201. realTarget = "ALL_BUILD";
  202. }
  203. if (clean) {
  204. makeCommand.push_back("/clean");
  205. } else {
  206. makeCommand.push_back("/build");
  207. }
  208. if (!config.empty()) {
  209. makeCommand.push_back(config);
  210. } else {
  211. makeCommand.push_back("Debug");
  212. }
  213. makeCommand.push_back("/project");
  214. if (!realTarget.empty()) {
  215. makeCommand.push_back(realTarget);
  216. } else {
  217. makeCommand.push_back("ALL_BUILD");
  218. }
  219. makeCommand.insert(makeCommand.end(), makeOptions.begin(),
  220. makeOptions.end());
  221. }
  222. ///! Create a local generator appropriate to this Global Generator
  223. cmLocalGenerator* cmGlobalVisualStudio7Generator::CreateLocalGenerator(
  224. cmMakefile* mf)
  225. {
  226. cmLocalVisualStudio7Generator* lg =
  227. new cmLocalVisualStudio7Generator(this, mf);
  228. return lg;
  229. }
  230. std::string const& cmGlobalVisualStudio7Generator::GetPlatformName() const
  231. {
  232. if (!this->GeneratorPlatform.empty()) {
  233. return this->GeneratorPlatform;
  234. }
  235. return this->DefaultPlatformName;
  236. }
  237. bool cmGlobalVisualStudio7Generator::SetSystemName(std::string const& s,
  238. cmMakefile* mf)
  239. {
  240. mf->AddDefinition("CMAKE_VS_INTEL_Fortran_PROJECT_VERSION",
  241. this->GetIntelProjectVersion());
  242. return this->cmGlobalVisualStudioGenerator::SetSystemName(s, mf);
  243. }
  244. bool cmGlobalVisualStudio7Generator::SetGeneratorPlatform(std::string const& p,
  245. cmMakefile* mf)
  246. {
  247. if (this->GetPlatformName() == "x64") {
  248. mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
  249. } else if (this->GetPlatformName() == "Itanium") {
  250. mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
  251. }
  252. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName().c_str());
  253. return this->cmGlobalVisualStudioGenerator::SetGeneratorPlatform(p, mf);
  254. }
  255. void cmGlobalVisualStudio7Generator::Generate()
  256. {
  257. // first do the superclass method
  258. this->cmGlobalVisualStudioGenerator::Generate();
  259. // Now write out the DSW
  260. this->OutputSLNFile();
  261. // If any solution or project files changed during the generation,
  262. // tell Visual Studio to reload them...
  263. if (!cmSystemTools::GetErrorOccuredFlag()) {
  264. this->CallVisualStudioMacro(MacroReload);
  265. }
  266. if (this->Version == VS8 && !this->CMakeInstance->GetIsInTryCompile()) {
  267. const char* cmakeWarnVS8 =
  268. this->CMakeInstance->GetState()->GetCacheEntryValue("CMAKE_WARN_VS8");
  269. if (!cmakeWarnVS8 || !cmSystemTools::IsOff(cmakeWarnVS8)) {
  270. this->CMakeInstance->IssueMessage(
  271. cmake::WARNING,
  272. "The \"Visual Studio 8 2005\" generator is deprecated "
  273. "and will be removed in a future version of CMake."
  274. "\n"
  275. "Add CMAKE_WARN_VS8=OFF to the cache to disable this warning.");
  276. }
  277. }
  278. }
  279. void cmGlobalVisualStudio7Generator::OutputSLNFile(
  280. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  281. {
  282. if (generators.empty()) {
  283. return;
  284. }
  285. this->CurrentProject = root->GetProjectName();
  286. std::string fname = root->GetCurrentBinaryDirectory();
  287. fname += "/";
  288. fname += root->GetProjectName();
  289. fname += ".sln";
  290. cmGeneratedFileStream fout(fname.c_str());
  291. fout.SetCopyIfDifferent(true);
  292. if (!fout) {
  293. return;
  294. }
  295. this->WriteSLNFile(fout, root, generators);
  296. if (fout.Close()) {
  297. this->FileReplacedDuringGenerate(fname);
  298. }
  299. }
  300. // output the SLN file
  301. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  302. {
  303. std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
  304. for (it = this->ProjectMap.begin(); it != this->ProjectMap.end(); ++it) {
  305. this->OutputSLNFile(it->second[0], it->second);
  306. }
  307. }
  308. void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
  309. std::ostream& fout, std::vector<std::string> const& configs,
  310. OrderedTargetDependSet const& projectTargets)
  311. {
  312. // loop over again and write out configurations for each target
  313. // in the solution
  314. for (OrderedTargetDependSet::const_iterator tt = projectTargets.begin();
  315. tt != projectTargets.end(); ++tt) {
  316. cmGeneratorTarget const* target = *tt;
  317. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  318. continue;
  319. }
  320. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  321. if (expath) {
  322. std::set<std::string> allConfigurations(configs.begin(), configs.end());
  323. const char* mapping = target->GetProperty("VS_PLATFORM_MAPPING");
  324. this->WriteProjectConfigurations(fout, target->GetName().c_str(),
  325. *target, configs, allConfigurations,
  326. mapping ? mapping : "");
  327. } else {
  328. const std::set<std::string>& configsPartOfDefaultBuild =
  329. this->IsPartOfDefaultBuild(configs, projectTargets, target);
  330. const char* vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
  331. if (vcprojName) {
  332. this->WriteProjectConfigurations(fout, vcprojName, *target, configs,
  333. configsPartOfDefaultBuild);
  334. }
  335. }
  336. }
  337. }
  338. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  339. std::ostream& fout, cmLocalGenerator* root,
  340. OrderedTargetDependSet const& projectTargets)
  341. {
  342. VisualStudioFolders.clear();
  343. std::string rootBinaryDir = root->GetCurrentBinaryDirectory();
  344. for (OrderedTargetDependSet::const_iterator tt = projectTargets.begin();
  345. tt != projectTargets.end(); ++tt) {
  346. cmGeneratorTarget const* target = *tt;
  347. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  348. continue;
  349. }
  350. bool written = false;
  351. // handle external vc project files
  352. const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
  353. if (expath) {
  354. std::string project = target->GetName();
  355. std::string location = expath;
  356. this->WriteExternalProject(fout, project.c_str(), location.c_str(),
  357. target->GetProperty("VS_PROJECT_TYPE"),
  358. target->GetUtilities());
  359. written = true;
  360. } else {
  361. const char* vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
  362. if (vcprojName) {
  363. cmLocalGenerator* lg = target->GetLocalGenerator();
  364. std::string dir = lg->GetCurrentBinaryDirectory();
  365. dir = root->ConvertToRelativePath(rootBinaryDir, dir.c_str());
  366. if (dir == ".") {
  367. dir = ""; // msbuild cannot handle ".\" prefix
  368. }
  369. this->WriteProject(fout, vcprojName, dir.c_str(), target);
  370. written = true;
  371. }
  372. }
  373. // Create "solution folder" information from FOLDER target property
  374. //
  375. if (written && this->UseFolderProperty()) {
  376. const std::string targetFolder = target->GetEffectiveFolderName();
  377. if (!targetFolder.empty()) {
  378. std::vector<cmsys::String> tokens =
  379. cmSystemTools::SplitString(targetFolder, '/', false);
  380. std::string cumulativePath = "";
  381. for (std::vector<cmsys::String>::iterator iter = tokens.begin();
  382. iter != tokens.end(); ++iter) {
  383. if (!iter->size()) {
  384. continue;
  385. }
  386. if (cumulativePath.empty()) {
  387. cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
  388. } else {
  389. VisualStudioFolders[cumulativePath].insert(cumulativePath + "/" +
  390. *iter);
  391. cumulativePath = cumulativePath + "/" + *iter;
  392. }
  393. }
  394. if (!cumulativePath.empty()) {
  395. VisualStudioFolders[cumulativePath].insert(target->GetName());
  396. }
  397. }
  398. }
  399. }
  400. }
  401. void cmGlobalVisualStudio7Generator::WriteTargetDepends(
  402. std::ostream& fout, OrderedTargetDependSet const& projectTargets)
  403. {
  404. for (OrderedTargetDependSet::const_iterator tt = projectTargets.begin();
  405. tt != projectTargets.end(); ++tt) {
  406. cmGeneratorTarget const* target = *tt;
  407. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  408. continue;
  409. }
  410. const char* vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
  411. if (vcprojName) {
  412. std::string dir =
  413. target->GetLocalGenerator()->GetCurrentSourceDirectory();
  414. this->WriteProjectDepends(fout, vcprojName, dir.c_str(), target);
  415. }
  416. }
  417. }
  418. void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
  419. {
  420. const char* prefix = "CMAKE_FOLDER_GUID_";
  421. const std::string::size_type skip_prefix = strlen(prefix);
  422. std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
  423. for (std::map<std::string, std::set<std::string> >::iterator iter =
  424. VisualStudioFolders.begin();
  425. iter != VisualStudioFolders.end(); ++iter) {
  426. std::string fullName = iter->first;
  427. std::string guid = this->GetGUID(fullName.c_str());
  428. std::replace(fullName.begin(), fullName.end(), '/', '\\');
  429. if (cmSystemTools::StringStartsWith(fullName.c_str(), prefix)) {
  430. fullName = fullName.substr(skip_prefix);
  431. }
  432. std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
  433. fout << "Project(\"{" << guidProjectTypeFolder << "}\") = \"" << nameOnly
  434. << "\", \"" << fullName << "\", \"{" << guid << "}\"\nEndProject\n";
  435. }
  436. }
  437. void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
  438. {
  439. for (std::map<std::string, std::set<std::string> >::iterator iter =
  440. VisualStudioFolders.begin();
  441. iter != VisualStudioFolders.end(); ++iter) {
  442. std::string key(iter->first);
  443. std::string guidParent(this->GetGUID(key.c_str()));
  444. for (std::set<std::string>::iterator it = iter->second.begin();
  445. it != iter->second.end(); ++it) {
  446. std::string value(*it);
  447. std::string guid(this->GetGUID(value.c_str()));
  448. fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
  449. }
  450. }
  451. }
  452. std::string cmGlobalVisualStudio7Generator::ConvertToSolutionPath(
  453. const char* path)
  454. {
  455. // Convert to backslashes. Do not use ConvertToOutputPath because
  456. // we will add quoting ourselves, and we know these projects always
  457. // use windows slashes.
  458. std::string d = path;
  459. std::string::size_type pos = 0;
  460. while ((pos = d.find('/', pos)) != d.npos) {
  461. d[pos++] = '\\';
  462. }
  463. return d;
  464. }
  465. void cmGlobalVisualStudio7Generator::WriteSLNGlobalSections(
  466. std::ostream& fout, cmLocalGenerator* root)
  467. {
  468. std::string const guid = this->GetGUID(root->GetProjectName() + ".sln");
  469. bool extensibilityGlobalsOverridden = false;
  470. bool extensibilityAddInsOverridden = false;
  471. const std::vector<std::string> propKeys =
  472. root->GetMakefile()->GetPropertyKeys();
  473. for (std::vector<std::string>::const_iterator it = propKeys.begin();
  474. it != propKeys.end(); ++it) {
  475. if (it->find("VS_GLOBAL_SECTION_") == 0) {
  476. std::string sectionType;
  477. std::string name = it->substr(18);
  478. if (name.find("PRE_") == 0) {
  479. name = name.substr(4);
  480. sectionType = "preSolution";
  481. } else if (name.find("POST_") == 0) {
  482. name = name.substr(5);
  483. sectionType = "postSolution";
  484. } else
  485. continue;
  486. if (!name.empty()) {
  487. bool addGuid = false;
  488. if (name == "ExtensibilityGlobals" && sectionType == "postSolution") {
  489. addGuid = true;
  490. extensibilityGlobalsOverridden = true;
  491. } else if (name == "ExtensibilityAddIns" &&
  492. sectionType == "postSolution") {
  493. extensibilityAddInsOverridden = true;
  494. }
  495. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  496. std::vector<std::string> keyValuePairs;
  497. cmSystemTools::ExpandListArgument(
  498. root->GetMakefile()->GetProperty(it->c_str()), keyValuePairs);
  499. for (std::vector<std::string>::const_iterator itPair =
  500. keyValuePairs.begin();
  501. itPair != keyValuePairs.end(); ++itPair) {
  502. const std::string::size_type posEqual = itPair->find('=');
  503. if (posEqual != std::string::npos) {
  504. const std::string key =
  505. cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
  506. const std::string value =
  507. cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
  508. fout << "\t\t" << key << " = " << value << "\n";
  509. if (key == "SolutionGuid") {
  510. addGuid = false;
  511. }
  512. }
  513. }
  514. if (addGuid) {
  515. fout << "\t\tSolutionGuid = {" << guid << "}\n";
  516. }
  517. fout << "\tEndGlobalSection\n";
  518. }
  519. }
  520. }
  521. if (!extensibilityGlobalsOverridden) {
  522. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  523. << "\t\tSolutionGuid = {" << guid << "}\n"
  524. << "\tEndGlobalSection\n";
  525. }
  526. if (!extensibilityAddInsOverridden)
  527. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  528. << "\tEndGlobalSection\n";
  529. }
  530. // Standard end of dsw file
  531. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  532. {
  533. fout << "EndGlobal\n";
  534. }
  535. std::string cmGlobalVisualStudio7Generator::WriteUtilityDepend(
  536. cmGeneratorTarget const* target)
  537. {
  538. std::vector<std::string> configs;
  539. target->Target->GetMakefile()->GetConfigurations(configs);
  540. std::string pname = target->GetName();
  541. pname += "_UTILITY";
  542. std::string fname = target->GetLocalGenerator()->GetCurrentBinaryDirectory();
  543. fname += "/";
  544. fname += pname;
  545. fname += ".vcproj";
  546. cmGeneratedFileStream fout(fname.c_str());
  547. fout.SetCopyIfDifferent(true);
  548. std::string guid = this->GetGUID(pname.c_str());
  549. /* clang-format off */
  550. fout <<
  551. "<?xml version=\"1.0\" encoding = \""
  552. << this->Encoding() << "\"?>\n"
  553. "<VisualStudioProject\n"
  554. "\tProjectType=\"Visual C++\"\n"
  555. "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
  556. "\tName=\"" << pname << "\"\n"
  557. "\tProjectGUID=\"{" << guid << "}\"\n"
  558. "\tKeyword=\"Win32Proj\">\n"
  559. "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
  560. "\t<Configurations>\n"
  561. ;
  562. /* clang-format on */
  563. for (std::vector<std::string>::iterator i = configs.begin();
  564. i != configs.end(); ++i) {
  565. /* clang-format off */
  566. fout <<
  567. "\t\t<Configuration\n"
  568. "\t\t\tName=\"" << *i << "|Win32\"\n"
  569. "\t\t\tOutputDirectory=\"" << *i << "\"\n"
  570. "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << *i << "\"\n"
  571. "\t\t\tConfigurationType=\"10\"\n"
  572. "\t\t\tUseOfMFC=\"0\"\n"
  573. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  574. "\t\t\tCharacterSet=\"2\">\n"
  575. "\t\t</Configuration>\n"
  576. ;
  577. /* clang-format on */
  578. }
  579. /* clang-format off */
  580. fout <<
  581. "\t</Configurations>\n"
  582. "\t<Files></Files>\n"
  583. "\t<Globals></Globals>\n"
  584. "</VisualStudioProject>\n"
  585. ;
  586. /* clang-format on */
  587. if (fout.Close()) {
  588. this->FileReplacedDuringGenerate(fname);
  589. }
  590. return pname;
  591. }
  592. std::string cmGlobalVisualStudio7Generator::GetGUID(std::string const& name)
  593. {
  594. std::string const& guidStoreName = name + "_GUID_CMAKE";
  595. if (const char* storedGUID =
  596. this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str())) {
  597. return std::string(storedGUID);
  598. }
  599. // Compute a GUID that is deterministic but unique to the build tree.
  600. std::string input = this->CMakeInstance->GetState()->GetBinaryDirectory();
  601. input += "|";
  602. input += name;
  603. cmUuid uuidGenerator;
  604. std::vector<unsigned char> uuidNamespace;
  605. uuidGenerator.StringToBinary("ee30c4be-5192-4fb0-b335-722a2dffe760",
  606. uuidNamespace);
  607. std::string guid = uuidGenerator.FromMd5(uuidNamespace, input);
  608. return cmSystemTools::UpperCase(guid);
  609. }
  610. void cmGlobalVisualStudio7Generator::AppendDirectoryForConfig(
  611. const std::string& prefix, const std::string& config,
  612. const std::string& suffix, std::string& dir)
  613. {
  614. if (!config.empty()) {
  615. dir += prefix;
  616. dir += config;
  617. dir += suffix;
  618. }
  619. }
  620. std::set<std::string> cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  621. std::vector<std::string> const& configs,
  622. OrderedTargetDependSet const& projectTargets,
  623. cmGeneratorTarget const* target)
  624. {
  625. std::set<std::string> activeConfigs;
  626. // if it is a utilitiy target then only make it part of the
  627. // default build if another target depends on it
  628. int type = target->GetType();
  629. if (type == cmStateEnums::GLOBAL_TARGET) {
  630. std::vector<std::string> targetNames;
  631. targetNames.push_back("INSTALL");
  632. targetNames.push_back("PACKAGE");
  633. for (std::vector<std::string>::const_iterator t = targetNames.begin();
  634. t != targetNames.end(); ++t) {
  635. // check if target <*t> is part of default build
  636. if (target->GetName() == *t) {
  637. const std::string propertyName =
  638. "CMAKE_VS_INCLUDE_" + *t + "_TO_DEFAULT_BUILD";
  639. // inspect CMAKE_VS_INCLUDE_<*t>_TO_DEFAULT_BUILD properties
  640. for (std::vector<std::string>::const_iterator i = configs.begin();
  641. i != configs.end(); ++i) {
  642. const char* propertyValue =
  643. target->Target->GetMakefile()->GetDefinition(propertyName);
  644. cmGeneratorExpression ge;
  645. CM_AUTO_PTR<cmCompiledGeneratorExpression> cge =
  646. ge.Parse(propertyValue);
  647. if (cmSystemTools::IsOn(
  648. cge->Evaluate(target->GetLocalGenerator(), *i))) {
  649. activeConfigs.insert(*i);
  650. }
  651. }
  652. }
  653. }
  654. return activeConfigs;
  655. }
  656. if (type == cmStateEnums::UTILITY &&
  657. !this->IsDependedOn(projectTargets, target)) {
  658. return activeConfigs;
  659. }
  660. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  661. for (std::vector<std::string>::const_iterator i = configs.begin();
  662. i != configs.end(); ++i) {
  663. const char* propertyValue =
  664. target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
  665. if (cmSystemTools::IsOff(propertyValue)) {
  666. activeConfigs.insert(*i);
  667. }
  668. }
  669. return activeConfigs;
  670. }
  671. bool cmGlobalVisualStudio7Generator::IsDependedOn(
  672. OrderedTargetDependSet const& projectTargets, cmGeneratorTarget const* gtIn)
  673. {
  674. for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
  675. l != projectTargets.end(); ++l) {
  676. TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(*l);
  677. if (tgtdeps.count(gtIn)) {
  678. return true;
  679. }
  680. }
  681. return false;
  682. }
  683. std::string cmGlobalVisualStudio7Generator::Encoding()
  684. {
  685. return "UTF-8";
  686. }