cmGlobalVisualStudio7Generator.cxx 25 KB

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