cmGlobalVisualStudio7Generator.cxx 24 KB

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