cmGlobalVisualStudio7Generator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. 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, 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(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. if (this->Version == VSVersion::VS9 &&
  282. !this->CMakeInstance->GetIsInTryCompile()) {
  283. std::string cmakeWarnVS9;
  284. if (cmValue cached = this->CMakeInstance->GetState()->GetCacheEntryValue(
  285. "CMAKE_WARN_VS9")) {
  286. this->CMakeInstance->MarkCliAsUsed("CMAKE_WARN_VS9");
  287. cmakeWarnVS9 = *cached;
  288. } else {
  289. cmSystemTools::GetEnv("CMAKE_WARN_VS9", cmakeWarnVS9);
  290. }
  291. if (cmakeWarnVS9.empty() || !cmIsOff(cmakeWarnVS9)) {
  292. this->CMakeInstance->IssueMessage(
  293. MessageType::WARNING,
  294. "The \"Visual Studio 9 2008\" generator is deprecated "
  295. "and will be removed in a future version of CMake."
  296. "\n"
  297. "Add CMAKE_WARN_VS9=OFF to the cache to disable this warning.");
  298. }
  299. }
  300. if (this->Version == VSVersion::VS12 &&
  301. !this->CMakeInstance->GetIsInTryCompile()) {
  302. std::string cmakeWarnVS12;
  303. if (cmValue cached = this->CMakeInstance->GetState()->GetCacheEntryValue(
  304. "CMAKE_WARN_VS12")) {
  305. this->CMakeInstance->MarkCliAsUsed("CMAKE_WARN_VS12");
  306. cmakeWarnVS12 = *cached;
  307. } else {
  308. cmSystemTools::GetEnv("CMAKE_WARN_VS12", cmakeWarnVS12);
  309. }
  310. if (cmakeWarnVS12.empty() || !cmIsOff(cmakeWarnVS12)) {
  311. this->CMakeInstance->IssueMessage(
  312. MessageType::WARNING,
  313. "The \"Visual Studio 12 2013\" generator is deprecated "
  314. "and will be removed in a future version of CMake."
  315. "\n"
  316. "Add CMAKE_WARN_VS12=OFF to the cache to disable this warning.");
  317. }
  318. }
  319. }
  320. void cmGlobalVisualStudio7Generator::OutputSLNFile(
  321. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  322. {
  323. if (generators.empty()) {
  324. return;
  325. }
  326. this->CurrentProject = root->GetProjectName();
  327. std::string fname = GetSLNFile(root);
  328. cmGeneratedFileStream fout(fname);
  329. fout.SetCopyIfDifferent(true);
  330. if (!fout) {
  331. return;
  332. }
  333. this->WriteSLNFile(fout, root, generators);
  334. if (fout.Close()) {
  335. this->FileReplacedDuringGenerate(fname);
  336. }
  337. }
  338. // output the SLN file
  339. void cmGlobalVisualStudio7Generator::OutputSLNFile()
  340. {
  341. for (auto& it : this->ProjectMap) {
  342. this->OutputSLNFile(it.second[0], it.second);
  343. }
  344. }
  345. void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
  346. std::ostream& fout, std::vector<std::string> const& configs,
  347. OrderedTargetDependSet const& projectTargets)
  348. {
  349. // loop over again and write out configurations for each target
  350. // in the solution
  351. for (cmGeneratorTarget const* target : projectTargets) {
  352. if (!this->IsInSolution(target)) {
  353. continue;
  354. }
  355. cmValue expath = target->GetProperty("EXTERNAL_MSPROJECT");
  356. if (expath) {
  357. std::set<std::string> allConfigurations(configs.begin(), configs.end());
  358. cmValue mapping = target->GetProperty("VS_PLATFORM_MAPPING");
  359. this->WriteProjectConfigurations(fout, target->GetName(), *target,
  360. configs, allConfigurations,
  361. mapping ? *mapping : "");
  362. } else {
  363. const std::set<std::string>& configsPartOfDefaultBuild =
  364. this->IsPartOfDefaultBuild(configs, projectTargets, target);
  365. cmValue vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
  366. if (vcprojName) {
  367. std::string mapping;
  368. // On VS 19 and above, always map .NET SDK projects to "Any CPU".
  369. if (target->IsDotNetSdkTarget() &&
  370. this->GetVersion() >= VSVersion::VS16 &&
  371. !cmGlobalVisualStudio7Generator::IsReservedTarget(
  372. target->GetName())) {
  373. mapping = "Any CPU";
  374. }
  375. this->WriteProjectConfigurations(fout, *vcprojName, *target, configs,
  376. configsPartOfDefaultBuild, mapping);
  377. }
  378. }
  379. }
  380. }
  381. void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
  382. std::ostream& fout, cmLocalGenerator* root,
  383. OrderedTargetDependSet const& projectTargets)
  384. {
  385. VisualStudioFolders.clear();
  386. std::vector<std::string> configs =
  387. root->GetMakefile()->GetGeneratorConfigs(cmMakefile::ExcludeEmptyConfig);
  388. for (cmGeneratorTarget const* target : projectTargets) {
  389. if (!this->IsInSolution(target)) {
  390. continue;
  391. }
  392. bool written = false;
  393. for (auto const& c : configs) {
  394. target->CheckCxxModuleStatus(c);
  395. }
  396. if (target->HaveCxx20ModuleSources() && !this->SupportsCxxModuleDyndep()) {
  397. root->GetMakefile()->IssueMessage(
  398. MessageType::FATAL_ERROR,
  399. cmStrCat("The \"", target->GetName(),
  400. "\" target contains C++ module sources which are not "
  401. "supported by the generator"));
  402. }
  403. // handle external vc project files
  404. cmValue expath = target->GetProperty("EXTERNAL_MSPROJECT");
  405. if (expath) {
  406. std::string project = target->GetName();
  407. std::string const& location = *expath;
  408. this->WriteExternalProject(fout, project, location,
  409. target->GetProperty("VS_PROJECT_TYPE"),
  410. target->GetUtilities());
  411. written = true;
  412. } else {
  413. cmValue vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
  414. if (vcprojName) {
  415. cmLocalGenerator* lg = target->GetLocalGenerator();
  416. std::string dir = lg->GetCurrentBinaryDirectory();
  417. dir = root->MaybeRelativeToCurBinDir(dir);
  418. if (dir == "."_s) {
  419. dir.clear(); // msbuild cannot handle ".\" prefix
  420. }
  421. this->WriteProject(fout, *vcprojName, dir, target);
  422. written = true;
  423. }
  424. }
  425. // Create "solution folder" information from FOLDER target property
  426. //
  427. if (written && this->UseFolderProperty()) {
  428. const std::string targetFolder = target->GetEffectiveFolderName();
  429. if (!targetFolder.empty()) {
  430. std::vector<std::string> tokens =
  431. cmSystemTools::SplitString(targetFolder, '/', false);
  432. std::string cumulativePath;
  433. for (std::string const& iter : tokens) {
  434. if (iter.empty()) {
  435. continue;
  436. }
  437. if (cumulativePath.empty()) {
  438. cumulativePath = "CMAKE_FOLDER_GUID_" + iter;
  439. } else {
  440. VisualStudioFolders[cumulativePath].insert(cumulativePath + "/" +
  441. iter);
  442. cumulativePath = cumulativePath + "/" + iter;
  443. }
  444. }
  445. if (!cumulativePath.empty()) {
  446. VisualStudioFolders[cumulativePath].insert(target->GetName());
  447. }
  448. }
  449. }
  450. }
  451. }
  452. void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
  453. {
  454. cm::string_view const prefix = "CMAKE_FOLDER_GUID_";
  455. std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
  456. for (auto const& iter : VisualStudioFolders) {
  457. std::string fullName = iter.first;
  458. std::string guid = this->GetGUID(fullName);
  459. std::replace(fullName.begin(), fullName.end(), '/', '\\');
  460. if (cmHasPrefix(fullName, prefix)) {
  461. fullName = fullName.substr(prefix.size());
  462. }
  463. std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
  464. fout << "Project(\"{" << guidProjectTypeFolder << "}\") = \"" << nameOnly
  465. << "\", \"" << fullName << "\", \"{" << guid << "}\"\nEndProject\n";
  466. }
  467. }
  468. void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
  469. {
  470. for (auto const& iter : VisualStudioFolders) {
  471. std::string key(iter.first);
  472. std::string guidParent(this->GetGUID(key));
  473. for (std::string const& it : iter.second) {
  474. std::string const& value(it);
  475. std::string guid(this->GetGUID(value));
  476. fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
  477. }
  478. }
  479. }
  480. std::string cmGlobalVisualStudio7Generator::ConvertToSolutionPath(
  481. const std::string& path)
  482. {
  483. // Convert to backslashes. Do not use ConvertToOutputPath because
  484. // we will add quoting ourselves, and we know these projects always
  485. // use windows slashes.
  486. std::string d = path;
  487. std::string::size_type pos = 0;
  488. while ((pos = d.find('/', pos)) != std::string::npos) {
  489. d[pos++] = '\\';
  490. }
  491. return d;
  492. }
  493. void cmGlobalVisualStudio7Generator::WriteSLNGlobalSections(
  494. std::ostream& fout, cmLocalGenerator* root)
  495. {
  496. std::string const guid = this->GetGUID(root->GetProjectName() + ".sln");
  497. bool extensibilityGlobalsOverridden = false;
  498. bool extensibilityAddInsOverridden = false;
  499. const std::vector<std::string> propKeys =
  500. root->GetMakefile()->GetPropertyKeys();
  501. for (std::string const& it : propKeys) {
  502. if (cmHasLiteralPrefix(it, "VS_GLOBAL_SECTION_")) {
  503. std::string sectionType;
  504. std::string name = it.substr(18);
  505. if (cmHasLiteralPrefix(name, "PRE_")) {
  506. name = name.substr(4);
  507. sectionType = "preSolution";
  508. } else if (cmHasLiteralPrefix(name, "POST_")) {
  509. name = name.substr(5);
  510. sectionType = "postSolution";
  511. } else {
  512. continue;
  513. }
  514. if (!name.empty()) {
  515. bool addGuid = false;
  516. if (name == "ExtensibilityGlobals"_s &&
  517. sectionType == "postSolution"_s) {
  518. addGuid = true;
  519. extensibilityGlobalsOverridden = true;
  520. } else if (name == "ExtensibilityAddIns"_s &&
  521. sectionType == "postSolution"_s) {
  522. extensibilityAddInsOverridden = true;
  523. }
  524. fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
  525. cmValue p = root->GetMakefile()->GetProperty(it);
  526. cmList keyValuePairs{ *p };
  527. for (std::string const& itPair : keyValuePairs) {
  528. const std::string::size_type posEqual = itPair.find('=');
  529. if (posEqual != std::string::npos) {
  530. const std::string key =
  531. cmTrimWhitespace(itPair.substr(0, posEqual));
  532. const std::string value =
  533. cmTrimWhitespace(itPair.substr(posEqual + 1));
  534. fout << "\t\t" << key << " = " << value << "\n";
  535. if (key == "SolutionGuid"_s) {
  536. addGuid = false;
  537. }
  538. }
  539. }
  540. if (addGuid) {
  541. fout << "\t\tSolutionGuid = {" << guid << "}\n";
  542. }
  543. fout << "\tEndGlobalSection\n";
  544. }
  545. }
  546. }
  547. if (!extensibilityGlobalsOverridden) {
  548. fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
  549. << "\t\tSolutionGuid = {" << guid << "}\n"
  550. << "\tEndGlobalSection\n";
  551. }
  552. if (!extensibilityAddInsOverridden) {
  553. fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
  554. << "\tEndGlobalSection\n";
  555. }
  556. }
  557. // Standard end of dsw file
  558. void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
  559. {
  560. fout << "EndGlobal\n";
  561. }
  562. std::string cmGlobalVisualStudio7Generator::WriteUtilityDepend(
  563. cmGeneratorTarget const* target)
  564. {
  565. std::vector<std::string> configs =
  566. target->Target->GetMakefile()->GetGeneratorConfigs(
  567. cmMakefile::ExcludeEmptyConfig);
  568. std::string pname = cmStrCat(target->GetName(), "_UTILITY");
  569. std::string fname =
  570. cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/',
  571. pname, ".vcproj");
  572. cmGeneratedFileStream fout(fname);
  573. fout.SetCopyIfDifferent(true);
  574. std::string guid = this->GetGUID(pname);
  575. /* clang-format off */
  576. fout <<
  577. R"(<?xml version="1.0" encoding = ")"
  578. << this->Encoding() << "\"?>\n"
  579. "<VisualStudioProject\n"
  580. "\tProjectType=\"Visual C++\"\n"
  581. "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
  582. "\tName=\"" << pname << "\"\n"
  583. "\tProjectGUID=\"{" << guid << "}\"\n"
  584. "\tKeyword=\"Win32Proj\">\n"
  585. "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
  586. "\t<Configurations>\n"
  587. ;
  588. /* clang-format on */
  589. for (std::string const& i : configs) {
  590. /* clang-format off */
  591. fout <<
  592. "\t\t<Configuration\n"
  593. "\t\t\tName=\"" << i << "|Win32\"\n"
  594. "\t\t\tOutputDirectory=\"" << i << "\"\n"
  595. "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << i << "\"\n"
  596. "\t\t\tConfigurationType=\"10\"\n"
  597. "\t\t\tUseOfMFC=\"0\"\n"
  598. "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
  599. "\t\t\tCharacterSet=\"2\">\n"
  600. "\t\t</Configuration>\n"
  601. ;
  602. /* clang-format on */
  603. }
  604. /* clang-format off */
  605. fout <<
  606. "\t</Configurations>\n"
  607. "\t<Files></Files>\n"
  608. "\t<Globals></Globals>\n"
  609. "</VisualStudioProject>\n"
  610. ;
  611. /* clang-format on */
  612. if (fout.Close()) {
  613. this->FileReplacedDuringGenerate(fname);
  614. }
  615. return pname;
  616. }
  617. std::string cmGlobalVisualStudio7Generator::GetGUID(std::string const& name)
  618. {
  619. std::string const& guidStoreName = name + "_GUID_CMAKE";
  620. if (cmValue storedGUID =
  621. this->CMakeInstance->GetCacheDefinition(guidStoreName)) {
  622. return *storedGUID;
  623. }
  624. // Compute a GUID that is deterministic but unique to the build tree.
  625. std::string input =
  626. cmStrCat(this->CMakeInstance->GetState()->GetBinaryDirectory(), '|', name);
  627. cmUuid uuidGenerator;
  628. std::vector<unsigned char> uuidNamespace;
  629. uuidGenerator.StringToBinary("ee30c4be-5192-4fb0-b335-722a2dffe760",
  630. uuidNamespace);
  631. std::string guid = uuidGenerator.FromMd5(uuidNamespace, input);
  632. return cmSystemTools::UpperCase(guid);
  633. }
  634. void cmGlobalVisualStudio7Generator::AppendDirectoryForConfig(
  635. const std::string& prefix, const std::string& config,
  636. const std::string& suffix, std::string& dir)
  637. {
  638. if (!config.empty()) {
  639. dir += prefix;
  640. dir += config;
  641. dir += suffix;
  642. }
  643. }
  644. std::set<std::string> cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
  645. std::vector<std::string> const& configs,
  646. OrderedTargetDependSet const& projectTargets,
  647. cmGeneratorTarget const* target)
  648. {
  649. std::set<std::string> activeConfigs;
  650. // if it is a utility target then only make it part of the
  651. // default build if another target depends on it
  652. int type = target->GetType();
  653. if (type == cmStateEnums::GLOBAL_TARGET) {
  654. std::vector<std::string> targetNames;
  655. targetNames.push_back("INSTALL");
  656. targetNames.push_back("PACKAGE");
  657. for (std::string const& t : targetNames) {
  658. // check if target <t> is part of default build
  659. if (target->GetName() == t) {
  660. const std::string propertyName =
  661. "CMAKE_VS_INCLUDE_" + t + "_TO_DEFAULT_BUILD";
  662. // inspect CMAKE_VS_INCLUDE_<t>_TO_DEFAULT_BUILD properties
  663. for (std::string const& i : configs) {
  664. cmValue propertyValue =
  665. target->Target->GetMakefile()->GetDefinition(propertyName);
  666. if (propertyValue &&
  667. cmIsOn(cmGeneratorExpression::Evaluate(
  668. *propertyValue, target->GetLocalGenerator(), i))) {
  669. activeConfigs.insert(i);
  670. }
  671. }
  672. }
  673. }
  674. return activeConfigs;
  675. }
  676. if (type == cmStateEnums::UTILITY &&
  677. !this->IsDependedOn(projectTargets, target)) {
  678. return activeConfigs;
  679. }
  680. // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
  681. for (std::string const& i : configs) {
  682. if (cmIsOff(target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i))) {
  683. activeConfigs.insert(i);
  684. }
  685. }
  686. return activeConfigs;
  687. }
  688. bool cmGlobalVisualStudio7Generator::IsDependedOn(
  689. OrderedTargetDependSet const& projectTargets, cmGeneratorTarget const* gtIn)
  690. {
  691. return std::any_of(projectTargets.begin(), projectTargets.end(),
  692. [this, gtIn](cmTargetDepend const& l) {
  693. TargetDependSet const& tgtdeps =
  694. this->GetTargetDirectDepends(l);
  695. return tgtdeps.count(gtIn);
  696. });
  697. }
  698. std::string cmGlobalVisualStudio7Generator::Encoding()
  699. {
  700. return "UTF-8";
  701. }