cmGlobalVisualStudio7Generator.cxx 24 KB

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