cmGlobalVisualStudio7Generator.cxx 24 KB

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