cmGlobalVisualStudio7Generator.cxx 24 KB

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