cmGlobalVisualStudio7Generator.cxx 25 KB

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