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