cmGlobalVisualStudio7Generator.cxx 23 KB

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