cmGlobalVisualStudio7Generator.cxx 25.1 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "windows.h" // this must be first to define GetCurrentDirectory
4

Ken Martin's avatar
Ken Martin committed
5
#include "cmGlobalVisualStudio7Generator.h"
6

7
#include "cmGeneratedFileStream.h"
8
#include "cmGeneratorTarget.h"
9
#include "cmLocalVisualStudio7Generator.h"
Ken Martin's avatar
Ken Martin committed
10
#include "cmMakefile.h"
11
#include "cmState.h"
12
#include "cmUuid.h"
Ken Martin's avatar
Ken Martin committed
13
#include "cmake.h"
14
#include <cmsys/Encoding.hxx>
Ken Martin's avatar
Ken Martin committed
15

16 17
#include <assert.h>

18
static cmVS7FlagTable cmVS7ExtraFlagTable[] = {
19 20 21
  // Precompiled header and related options.  Note that the
  // UsePrecompiledHeader entries are marked as "Continue" so that the
  // corresponding PrecompiledHeaderThrough entry can be found.
22 23 24 25 26 27 28 29 30 31
  { "UsePrecompiledHeader", "YX", "Automatically Generate", "2",
    cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  { "PrecompiledHeaderThrough", "YX", "Precompiled Header Name", "",
    cmVS7FlagTable::UserValueRequired },
  { "UsePrecompiledHeader", "Yu", "Use Precompiled Header", "3",
    cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  { "PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
    cmVS7FlagTable::UserValueRequired },
  { "WholeProgramOptimization", "LTCG", "WholeProgramOptimization", "true",
    0 },
32 33

  // Exception handling mode.  If no entries match, it will be FALSE.
34 35
  { "ExceptionHandling", "GX", "enable c++ exceptions", "true", 0 },
  { "ExceptionHandling", "EHsc", "enable c++ exceptions", "true", 0 },
36 37 38 39
  // The EHa option does not have an IDE setting.  Let it go to false,
  // and have EHa passed on the command line by leaving out the table
  // entry.

40
  { 0, 0, 0, 0, 0 }
41 42
};

43 44
cmGlobalVisualStudio7Generator::cmGlobalVisualStudio7Generator(
  cmake* cm, const std::string& platformName)
45
  : cmGlobalVisualStudioGenerator(cm)
46
{
47
  this->IntelProjectVersion = 0;
48
  this->DevEnvCommandInitialized = false;
49
  this->MasmEnabled = false;
50

51
  if (platformName.empty()) {
52
    this->DefaultPlatformName = "Win32";
53
  } else {
54
    this->DefaultPlatformName = platformName;
55
  }
56
  this->ExtraFlagTable = cmVS7ExtraFlagTable;
57
  this->Version = VS7;
58 59
}

60 61 62 63 64 65 66 67 68 69
cmGlobalVisualStudio7Generator::~cmGlobalVisualStudio7Generator()
{
  free(this->IntelProjectVersion);
}

// Package GUID of Intel Visual Fortran plugin to VS IDE
#define CM_INTEL_PLUGIN_GUID "{B68A201D-CB9B-47AF-A52F-7EEC72E217E4}"

const char* cmGlobalVisualStudio7Generator::GetIntelProjectVersion()
{
70
  if (!this->IntelProjectVersion) {
71 72 73 74 75 76 77 78 79
    // Compute the version of the Intel plugin to the VS IDE.
    // If the key does not exist then use a default guess.
    std::string intelVersion;
    std::string vskey = this->GetRegistryBase();
    vskey += "\\Packages\\" CM_INTEL_PLUGIN_GUID ";ProductVersion";
    cmSystemTools::ReadRegistryValue(vskey.c_str(), intelVersion,
                                     cmSystemTools::KeyWOW64_32);
    unsigned int intelVersionNumber = ~0u;
    sscanf(intelVersion.c_str(), "%u", &intelVersionNumber);
80
    if (intelVersionNumber >= 11) {
81 82
      // Default to latest known project file version.
      intelVersion = "11.0";
83
    } else if (intelVersionNumber == 10) {
84 85
      // Version 10.x actually uses 9.10 in project files!
      intelVersion = "9.10";
86
    } else {
87 88
      // Version <= 9: use ProductVersion from registry.
    }
89 90
    this->IntelProjectVersion = strdup(intelVersion.c_str());
  }
91 92
  return this->IntelProjectVersion;
}
93

94 95
void cmGlobalVisualStudio7Generator::EnableLanguage(
  std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
Ken Martin's avatar
Ken Martin committed
96
{
97
  mf->AddDefinition("CMAKE_GENERATOR_RC", "rc");
98
  mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
99
  if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
100
    mf->AddCacheDefinition(
101
      "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
102 103 104
      "Semicolon separated list of supported configuration types, "
      "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
      "anything else will be ignored.",
105
      cmStateEnums::STRING);
106
  }
107

108
  // Create list of configurations requested by user's cache, if any.
109
  this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
110

111
  // if this environment variable is set, then copy it to
112
  // a static cache entry.  It will be used by
113 114 115 116 117
  // cmLocalGenerator::ConstructScript, to add an extra PATH
  // to all custom commands.   This is because the VS IDE
  // does not use the environment it is run in, and this allows
  // for running commands and using dll's that the IDE environment
  // does not know about.
118 119 120
  std::string extraPath;
  if (cmSystemTools::GetEnv("CMAKE_MSVCIDE_RUN_PATH", extraPath)) {
    mf->AddCacheDefinition("CMAKE_MSVCIDE_RUN_PATH", extraPath.c_str(),
121
                           "Saved environment variable CMAKE_MSVCIDE_RUN_PATH",
122
                           cmStateEnums::STATIC);
123
  }
Ken Martin's avatar
Ken Martin committed
124 125
}

126
bool cmGlobalVisualStudio7Generator::FindMakeProgram(cmMakefile* mf)
127
{
128 129 130
  if (!this->cmGlobalVisualStudioGenerator::FindMakeProgram(mf)) {
    return false;
  }
131 132
  mf->AddDefinition("CMAKE_VS_DEVENV_COMMAND",
                    this->GetDevEnvCommand().c_str());
133
  return true;
134 135
}

136 137
std::string const& cmGlobalVisualStudio7Generator::GetDevEnvCommand()
{
138
  if (!this->DevEnvCommandInitialized) {
139 140
    this->DevEnvCommandInitialized = true;
    this->DevEnvCommand = this->FindDevEnvCommand();
141
  }
142 143 144 145 146 147
  return this->DevEnvCommand;
}

std::string cmGlobalVisualStudio7Generator::FindDevEnvCommand()
{
  std::string vscmd;
148 149 150 151
  std::string vskey;

  // Search in standard location.
  vskey = this->GetRegistryBase() + ";InstallDir";
152 153
  if (cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
                                       cmSystemTools::KeyWOW64_32)) {
154
    cmSystemTools::ConvertToUnixSlashes(vscmd);
155 156 157 158
    vscmd += "/devenv.com";
    if (cmSystemTools::FileExists(vscmd, true)) {
      return vscmd;
    }
159
  }
160

161 162 163 164 165 166 167 168 169 170 171 172
  // Search where VS15Preview places it.
  vskey = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7;";
  vskey += this->GetIDEVersion();
  if (cmSystemTools::ReadRegistryValue(vskey.c_str(), vscmd,
                                       cmSystemTools::KeyWOW64_32)) {
    cmSystemTools::ConvertToUnixSlashes(vscmd);
    vscmd += "/Common7/IDE/devenv.com";
    if (cmSystemTools::FileExists(vscmd, true)) {
      return vscmd;
    }
  }

173
  vscmd = "devenv.com";
174 175 176
  return vscmd;
}

177 178 179 180
const char* cmGlobalVisualStudio7Generator::ExternalProjectType(
  const char* location)
{
  std::string extension = cmSystemTools::GetFilenameLastExtension(location);
181
  if (extension == ".vbproj") {
182
    return "F184B08F-C81C-45F6-A57F-5ABD9991F28F";
183
  } else if (extension == ".csproj") {
184
    return "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC";
185
  } else if (extension == ".fsproj") {
186
    return "F2A71F9B-5D33-465A-A702-920D77279786";
187
  } else if (extension == ".vdproj") {
188
    return "54435603-DBB4-11D2-8724-00A0C9A8B90C";
189
  } else if (extension == ".dbproj") {
190
    return "C8D11400-126E-41CD-887F-60BD40844F9E";
191
  } else if (extension == ".wixproj") {
192
    return "930C7802-8A8C-48F9-8165-68863BCCD9DD";
193
  } else if (extension == ".pyproj") {
194
    return "888888A0-9F3D-457C-B088-3A5042F75D52";
195
  }
196 197
  return "8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942";
}
198
void cmGlobalVisualStudio7Generator::GenerateBuildCommand(
199 200 201 202
  std::vector<std::string>& makeCommand, const std::string& makeProgram,
  const std::string& projectName, const std::string& /*projectDir*/,
  const std::string& targetName, const std::string& config, bool /*fast*/,
  bool /*verbose*/, std::vector<std::string> const& makeOptions)
Ken Martin's avatar
Ken Martin committed
203
{
204 205 206 207 208 209 210 211 212
  // Select the caller- or user-preferred make program, else devenv.
  std::string makeProgramSelected =
    this->SelectMakeProgram(makeProgram, this->GetDevEnvCommand());

  // Ignore the above preference if it is msbuild.
  // Assume any other value is either a devenv or
  // command-line compatible with devenv.
  std::string makeProgramLower = makeProgramSelected;
  cmSystemTools::LowerCase(makeProgramLower);
213
  if (makeProgramLower.find("msbuild") != std::string::npos) {
214
    makeProgramSelected = this->GetDevEnvCommand();
215
  }
216 217

  makeCommand.push_back(makeProgramSelected);
218 219

  makeCommand.push_back(std::string(projectName) + ".sln");
220
  std::string realTarget = targetName;
221
  bool clean = false;
222
  if (realTarget == "clean") {
223
    clean = true;
224
    realTarget = "ALL_BUILD";
225 226
  }
  if (clean) {
227
    makeCommand.push_back("/clean");
228
  } else {
229
    makeCommand.push_back("/build");
230
  }
231

232
  if (!config.empty()) {
233
    makeCommand.push_back(config);
234
  } else {
235
    makeCommand.push_back("Debug");
236
  }
237
  makeCommand.push_back("/project");
238

239
  if (!realTarget.empty()) {
240
    makeCommand.push_back(realTarget);
241
  } else {
242
    makeCommand.push_back("ALL_BUILD");
243 244 245
  }
  makeCommand.insert(makeCommand.end(), makeOptions.begin(),
                     makeOptions.end());
Ken Martin's avatar
Ken Martin committed
246 247 248
}

///! Create a local generator appropriate to this Global Generator
249 250
cmLocalGenerator* cmGlobalVisualStudio7Generator::CreateLocalGenerator(
  cmMakefile* mf)
Ken Martin's avatar
Ken Martin committed
251
{
252
  cmLocalVisualStudio7Generator* lg =
253
    new cmLocalVisualStudio7Generator(this, mf);
Ken Martin's avatar
Ken Martin committed
254 255 256
  return lg;
}

257 258
std::string const& cmGlobalVisualStudio7Generator::GetPlatformName() const
{
259
  if (!this->GeneratorPlatform.empty()) {
260
    return this->GeneratorPlatform;
261
  }
262 263 264
  return this->DefaultPlatformName;
}

265 266
bool cmGlobalVisualStudio7Generator::SetSystemName(std::string const& s,
                                                   cmMakefile* mf)
267 268 269 270 271 272 273 274
{
  mf->AddDefinition("CMAKE_VS_INTEL_Fortran_PROJECT_VERSION",
                    this->GetIntelProjectVersion());
  return this->cmGlobalVisualStudioGenerator::SetSystemName(s, mf);
}

bool cmGlobalVisualStudio7Generator::SetGeneratorPlatform(std::string const& p,
                                                          cmMakefile* mf)
275
{
276
  if (this->GetPlatformName() == "x64") {
277
    mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
278
  } else if (this->GetPlatformName() == "Itanium") {
279
    mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
280
  }
281
  mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName().c_str());
282
  return this->cmGlobalVisualStudioGenerator::SetGeneratorPlatform(p, mf);
283 284
}

Ken Martin's avatar
Ken Martin committed
285 286 287
void cmGlobalVisualStudio7Generator::Generate()
{
  // first do the superclass method
288 289
  this->cmGlobalVisualStudioGenerator::Generate();

Ken Martin's avatar
Ken Martin committed
290 291
  // Now write out the DSW
  this->OutputSLNFile();
292 293
  // If any solution or project files changed during the generation,
  // tell Visual Studio to reload them...
294
  if (!cmSystemTools::GetErrorOccuredFlag()) {
295
    this->CallVisualStudioMacro(MacroReload);
296
  }
297

298
  if (this->Version == VS71 && !this->CMakeInstance->GetIsInTryCompile()) {
299 300
    const char* cmakeWarnVS71 =
      this->CMakeInstance->GetState()->GetCacheEntryValue("CMAKE_WARN_VS71");
301
    if (!cmakeWarnVS71 || !cmSystemTools::IsOff(cmakeWarnVS71)) {
302 303 304 305 306
      this->CMakeInstance->IssueMessage(
        cmake::WARNING,
        "The \"Visual Studio 7 .NET 2003\" generator is deprecated "
        "and will be removed in a future version of CMake."
        "\n"
307
        "Add CMAKE_WARN_VS71=OFF to the cache to disable this warning.");
308
    }
309
  }
Ken Martin's avatar
Ken Martin committed
310 311
}

312 313
void cmGlobalVisualStudio7Generator::OutputSLNFile(
  cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
314
{
315
  if (generators.empty()) {
316
    return;
317
  }
318
  this->CurrentProject = root->GetProjectName();
319
  std::string fname = root->GetCurrentBinaryDirectory();
Ken Martin's avatar
Ken Martin committed
320
  fname += "/";
321
  fname += root->GetProjectName();
Ken Martin's avatar
Ken Martin committed
322
  fname += ".sln";
323 324
  cmGeneratedFileStream fout(fname.c_str());
  fout.SetCopyIfDifferent(true);
325
  if (!fout) {
Ken Martin's avatar
Ken Martin committed
326
    return;
327
  }
328
  this->WriteSLNFile(fout, root, generators);
329
  if (fout.Close()) {
330
    this->FileReplacedDuringGenerate(fname);
331
  }
332 333 334 335
}

// output the SLN file
void cmGlobalVisualStudio7Generator::OutputSLNFile()
336
{
337
  std::map<std::string, std::vector<cmLocalGenerator*> >::iterator it;
338
  for (it = this->ProjectMap.begin(); it != this->ProjectMap.end(); ++it) {
339
    this->OutputSLNFile(it->second[0], it->second);
340
  }
Ken Martin's avatar
Ken Martin committed
341 342
}

343
void cmGlobalVisualStudio7Generator::WriteTargetConfigurations(
344
  std::ostream& fout, std::vector<std::string> const& configs,
345
  OrderedTargetDependSet const& projectTargets)
346 347 348
{
  // loop over again and write out configurations for each target
  // in the solution
349 350
  for (OrderedTargetDependSet::const_iterator tt = projectTargets.begin();
       tt != projectTargets.end(); ++tt) {
351
    cmGeneratorTarget const* target = *tt;
352
    if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
353
      continue;
354
    }
355
    const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
356
    if (expath) {
357
      std::set<std::string> allConfigurations(configs.begin(), configs.end());
358
      const char* mapping = target->GetProperty("VS_PLATFORM_MAPPING");
359
      this->WriteProjectConfigurations(
360 361 362
        fout, target->GetName().c_str(), target->GetType(), configs,
        allConfigurations, mapping ? mapping : "");
    } else {
363
      const std::set<std::string>& configsPartOfDefaultBuild =
364
        this->IsPartOfDefaultBuild(configs, projectTargets, target);
365 366
      const char* vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
      if (vcprojName) {
367
        this->WriteProjectConfigurations(fout, vcprojName, target->GetType(),
368
                                         configs, configsPartOfDefaultBuild);
Ken Martin's avatar
Ken Martin committed
369 370
      }
    }
371
  }
372 373 374
}

void cmGlobalVisualStudio7Generator::WriteTargetsToSolution(
375 376
  std::ostream& fout, cmLocalGenerator* root,
  OrderedTargetDependSet const& projectTargets)
377
{
378 379
  VisualStudioFolders.clear();

380
  std::string rootBinaryDir = root->GetCurrentBinaryDirectory();
381 382
  for (OrderedTargetDependSet::const_iterator tt = projectTargets.begin();
       tt != projectTargets.end(); ++tt) {
383
    cmGeneratorTarget const* target = *tt;
384
    if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
385
      continue;
386
    }
387 388
    bool written = false;

389
    // handle external vc project files
390
    const char* expath = target->GetProperty("EXTERNAL_MSPROJECT");
391
    if (expath) {
392 393
      std::string project = target->GetName();
      std::string location = expath;
394

395
      this->WriteExternalProject(fout, project.c_str(), location.c_str(),
396
                                 target->GetProperty("VS_PROJECT_TYPE"),
397
                                 target->GetUtilities());
398
      written = true;
399 400 401
    } else {
      const char* vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
      if (vcprojName) {
402
        cmLocalGenerator* lg = target->GetLocalGenerator();
403
        std::string dir = lg->GetCurrentBinaryDirectory();
404
        dir = root->ConvertToRelativePath(rootBinaryDir, dir.c_str());
405
        if (dir == ".") {
406
          dir = ""; // msbuild cannot handle ".\" prefix
407
        }
408 409
        this->WriteProject(fout, vcprojName, dir.c_str(), target);
        written = true;
410
      }
411
    }
412

413 414
    // Create "solution folder" information from FOLDER target property
    //
415
    if (written && this->UseFolderProperty()) {
416 417
      const std::string targetFolder = target->GetEffectiveFolderName();
      if (!targetFolder.empty()) {
418 419 420 421 422
        std::vector<cmsys::String> tokens =
          cmSystemTools::SplitString(targetFolder, '/', false);

        std::string cumulativePath = "";

423 424 425
        for (std::vector<cmsys::String>::iterator iter = tokens.begin();
             iter != tokens.end(); ++iter) {
          if (!iter->size()) {
426
            continue;
427
          }
428

429
          if (cumulativePath.empty()) {
430
            cumulativePath = "CMAKE_FOLDER_GUID_" + *iter;
431 432 433
          } else {
            VisualStudioFolders[cumulativePath].insert(cumulativePath + "/" +
                                                       *iter);
434 435 436

            cumulativePath = cumulativePath + "/" + *iter;
          }
437
        }
438

439
        if (!cumulativePath.empty()) {
440
          VisualStudioFolders[cumulativePath].insert(target->GetName());
Ken Martin's avatar
Ken Martin committed
441 442 443
        }
      }
    }
444
  }
445 446 447
}

void cmGlobalVisualStudio7Generator::WriteTargetDepends(
448
  std::ostream& fout, OrderedTargetDependSet const& projectTargets)
449
{
450 451
  for (OrderedTargetDependSet::const_iterator tt = projectTargets.begin();
       tt != projectTargets.end(); ++tt) {
452
    cmGeneratorTarget const* target = *tt;
453
    if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
454
      continue;
Ken Martin's avatar
Ken Martin committed
455
    }
456 457 458 459 460 461 462
    const char* vcprojName = target->GetProperty("GENERATOR_FILE_NAME");
    if (vcprojName) {
      std::string dir =
        target->GetLocalGenerator()->GetCurrentSourceDirectory();
      this->WriteProjectDepends(fout, vcprojName, dir.c_str(), target);
    }
  }
463
}
464

465 466
void cmGlobalVisualStudio7Generator::WriteFolders(std::ostream& fout)
{
467
  const char* prefix = "CMAKE_FOLDER_GUID_";
468
  const std::string::size_type skip_prefix = strlen(prefix);
469
  std::string guidProjectTypeFolder = "2150E333-8FDC-42A3-9474-1A3956D46DE8";
470 471 472
  for (std::map<std::string, std::set<std::string> >::iterator iter =
         VisualStudioFolders.begin();
       iter != VisualStudioFolders.end(); ++iter) {
473 474
    std::string fullName = iter->first;
    std::string guid = this->GetGUID(fullName.c_str());
475

476
    std::replace(fullName.begin(), fullName.end(), '/', '\\');
477
    if (cmSystemTools::StringStartsWith(fullName.c_str(), prefix)) {
478
      fullName = fullName.substr(skip_prefix);
479
    }
480 481

    std::string nameOnly = cmSystemTools::GetFilenameName(fullName);
482

483 484 485
    fout << "Project(\"{" << guidProjectTypeFolder << "}\") = \"" << nameOnly
         << "\", \"" << fullName << "\", \"{" << guid << "}\"\nEndProject\n";
  }
486 487 488 489
}

void cmGlobalVisualStudio7Generator::WriteFoldersContent(std::ostream& fout)
{
490 491 492
  for (std::map<std::string, std::set<std::string> >::iterator iter =
         VisualStudioFolders.begin();
       iter != VisualStudioFolders.end(); ++iter) {
493 494 495
    std::string key(iter->first);
    std::string guidParent(this->GetGUID(key.c_str()));

496 497
    for (std::set<std::string>::iterator it = iter->second.begin();
         it != iter->second.end(); ++it) {
498 499 500 501 502
      std::string value(*it);
      std::string guid(this->GetGUID(value.c_str()));

      fout << "\t\t{" << guid << "} = {" << guidParent << "}\n";
    }
503
  }
504 505
}

506 507
std::string cmGlobalVisualStudio7Generator::ConvertToSolutionPath(
  const char* path)
508 509 510 511 512 513
{
  // Convert to backslashes.  Do not use ConvertToOutputPath because
  // we will add quoting ourselves, and we know these projects always
  // use windows slashes.
  std::string d = path;
  std::string::size_type pos = 0;
514
  while ((pos = d.find('/', pos)) != d.npos) {
515
    d[pos++] = '\\';
516
  }
517 518
  return d;
}
Ken Martin's avatar
Ken Martin committed
519

520 521
void cmGlobalVisualStudio7Generator::WriteSLNGlobalSections(
  std::ostream& fout, cmLocalGenerator* root)
522 523 524
{
  bool extensibilityGlobalsOverridden = false;
  bool extensibilityAddInsOverridden = false;
525
  const std::vector<std::string> propKeys =
526 527 528 529
    root->GetMakefile()->GetPropertyKeys();
  for (std::vector<std::string>::const_iterator it = propKeys.begin();
       it != propKeys.end(); ++it) {
    if (it->find("VS_GLOBAL_SECTION_") == 0) {
530
      std::string sectionType;
531
      std::string name = it->substr(18);
532
      if (name.find("PRE_") == 0) {
533 534
        name = name.substr(4);
        sectionType = "preSolution";
535
      } else if (name.find("POST_") == 0) {
536 537
        name = name.substr(5);
        sectionType = "postSolution";
538
      } else
539
        continue;
540 541
      if (!name.empty()) {
        if (name == "ExtensibilityGlobals" && sectionType == "postSolution")
542
          extensibilityGlobalsOverridden = true;
543 544
        else if (name == "ExtensibilityAddIns" &&
                 sectionType == "postSolution")
545 546 547
          extensibilityAddInsOverridden = true;
        fout << "\tGlobalSection(" << name << ") = " << sectionType << "\n";
        std::vector<std::string> keyValuePairs;
548
        cmSystemTools::ExpandListArgument(
549 550 551 552
          root->GetMakefile()->GetProperty(it->c_str()), keyValuePairs);
        for (std::vector<std::string>::const_iterator itPair =
               keyValuePairs.begin();
             itPair != keyValuePairs.end(); ++itPair) {
553
          const std::string::size_type posEqual = itPair->find('=');
554
          if (posEqual != std::string::npos) {
555 556 557 558 559 560 561
            const std::string key =
              cmSystemTools::TrimWhitespace(itPair->substr(0, posEqual));
            const std::string value =
              cmSystemTools::TrimWhitespace(itPair->substr(posEqual + 1));
            fout << "\t\t" << key << " = " << value << "\n";
          }
        }
562
        fout << "\tEndGlobalSection\n";
563 564
      }
    }
565 566
  }
  if (!extensibilityGlobalsOverridden)
567 568
    fout << "\tGlobalSection(ExtensibilityGlobals) = postSolution\n"
         << "\tEndGlobalSection\n";
569
  if (!extensibilityAddInsOverridden)
570 571 572 573
    fout << "\tGlobalSection(ExtensibilityAddIns) = postSolution\n"
         << "\tEndGlobalSection\n";
}

Ken Martin's avatar
Ken Martin committed
574 575 576
// Standard end of dsw file
void cmGlobalVisualStudio7Generator::WriteSLNFooter(std::ostream& fout)
{
577
  fout << "EndGlobal\n";
Ken Martin's avatar
Ken Martin committed
578 579
}

580 581
std::string cmGlobalVisualStudio7Generator::WriteUtilityDepend(
  cmGeneratorTarget const* target)
582
{
583
  std::vector<std::string> configs;
584
  target->Target->GetMakefile()->GetConfigurations(configs);
585 586
  std::string pname = target->GetName();
  pname += "_UTILITY";
587
  std::string fname = target->GetLocalGenerator()->GetCurrentBinaryDirectory();
588 589 590 591 592 593 594
  fname += "/";
  fname += pname;
  fname += ".vcproj";
  cmGeneratedFileStream fout(fname.c_str());
  fout.SetCopyIfDifferent(true);
  std::string guid = this->GetGUID(pname.c_str());

595
  /* clang-format off */
596
  fout <<
597 598
    "<?xml version=\"1.0\" encoding = \""
    << this->Encoding() << "\"?>\n"
599 600 601 602 603 604 605 606 607
    "<VisualStudioProject\n"
    "\tProjectType=\"Visual C++\"\n"
    "\tVersion=\"" << this->GetIDEVersion() << "0\"\n"
    "\tName=\"" << pname << "\"\n"
    "\tProjectGUID=\"{" << guid << "}\"\n"
    "\tKeyword=\"Win32Proj\">\n"
    "\t<Platforms><Platform Name=\"Win32\"/></Platforms>\n"
    "\t<Configurations>\n"
    ;
608
  /* clang-format on */
609 610
  for (std::vector<std::string>::iterator i = configs.begin();
       i != configs.end(); ++i) {
611
    /* clang-format off */
612 613 614 615 616 617 618 619 620 621 622
    fout <<
      "\t\t<Configuration\n"
      "\t\t\tName=\"" << *i << "|Win32\"\n"
      "\t\t\tOutputDirectory=\"" << *i << "\"\n"
      "\t\t\tIntermediateDirectory=\"" << pname << ".dir\\" << *i << "\"\n"
      "\t\t\tConfigurationType=\"10\"\n"
      "\t\t\tUseOfMFC=\"0\"\n"
      "\t\t\tATLMinimizesCRunTimeLibraryUsage=\"FALSE\"\n"
      "\t\t\tCharacterSet=\"2\">\n"
      "\t\t</Configuration>\n"
      ;
623
    /* clang-format on */
624
  }
625
  /* clang-format off */
626 627 628 629 630 631
  fout <<
    "\t</Configurations>\n"
    "\t<Files></Files>\n"
    "\t<Globals></Globals>\n"
    "</VisualStudioProject>\n"
    ;
632
  /* clang-format on */
633

634
  if (fout.Close()) {
635
    this->FileReplacedDuringGenerate(fname);
636
  }
637 638 639
  return pname;
}

640
std::string cmGlobalVisualStudio7Generator::GetGUID(std::string const& name)
641
{
642 643
  std::string const& guidStoreName = name + "_GUID_CMAKE";
  if (const char* storedGUID =
644
        this->CMakeInstance->GetCacheDefinition(guidStoreName.c_str())) {
645
    return std::string(storedGUID);
646
  }
647 648 649 650
  // Compute a GUID that is deterministic but unique to the build tree.
  std::string input = this->CMakeInstance->GetState()->GetBinaryDirectory();
  input += "|";
  input += name;
651 652 653 654

  cmUuid uuidGenerator;

  std::vector<unsigned char> uuidNamespace;
655 656
  uuidGenerator.StringToBinary("ee30c4be-5192-4fb0-b335-722a2dffe760",
                               uuidNamespace);
657 658 659

  std::string guid = uuidGenerator.FromMd5(uuidNamespace, input);

660
  return cmSystemTools::UpperCase(guid);
Ken Martin's avatar
Ken Martin committed
661 662
}

663 664 665
void cmGlobalVisualStudio7Generator::AppendDirectoryForConfig(
  const std::string& prefix, const std::string& config,
  const std::string& suffix, std::string& dir)
666
{
667
  if (!config.empty()) {
668
    dir += prefix;
669
    dir += config;
670
    dir += suffix;
671
  }
672
}
673

674
std::set<std::string> cmGlobalVisualStudio7Generator::IsPartOfDefaultBuild(
675
  std::vector<std::string> const& configs,
676 677
  OrderedTargetDependSet const& projectTargets,
  cmGeneratorTarget const* target)
678
{
679
  std::set<std::string> activeConfigs;
680
  // if it is a utilitiy target then only make it part of the
681 682
  // default build if another target depends on it
  int type = target->GetType();
683
  if (type == cmStateEnums::GLOBAL_TARGET) {
684
    // check if INSTALL target is part of default build
685
    if (target->GetName() == "INSTALL") {
686
      // inspect CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD properties
687 688 689 690 691
      for (std::vector<std::string>::const_iterator i = configs.begin();
           i != configs.end(); ++i) {
        const char* propertyValue =
          target->Target->GetMakefile()->GetDefinition(
            "CMAKE_VS_INCLUDE_INSTALL_TO_DEFAULT_BUILD");
692
        cmGeneratorExpression ge;
693
        CM_AUTO_PTR<cmCompiledGeneratorExpression> cge =
694 695 696
          ge.Parse(propertyValue);
        if (cmSystemTools::IsOn(
              cge->Evaluate(target->GetLocalGenerator(), *i))) {
697 698 699
          activeConfigs.insert(*i);
        }
      }
700 701
    }
    return activeConfigs;
702
  }
703
  if (type == cmStateEnums::UTILITY &&
704 705 706
      !this->IsDependedOn(projectTargets, target)) {
    return activeConfigs;
  }
707
  // inspect EXCLUDE_FROM_DEFAULT_BUILD[_<CONFIG>] properties
708 709
  for (std::vector<std::string>::const_iterator i = configs.begin();
       i != configs.end(); ++i) {
710
    const char* propertyValue =
711
      target->GetFeature("EXCLUDE_FROM_DEFAULT_BUILD", i->c_str());
712
    if (cmSystemTools::IsOff(propertyValue)) {
713
      activeConfigs.insert(*i);
714
    }
715
  }
716
  return activeConfigs;
717
}
718

719 720
bool cmGlobalVisualStudio7Generator::IsDependedOn(
  OrderedTargetDependSet const& projectTargets, cmGeneratorTarget const* gtIn)
721
{
722
  for (OrderedTargetDependSet::const_iterator l = projectTargets.begin();
723
       l != projectTargets.end(); ++l) {
724
    TargetDependSet const& tgtdeps = this->GetTargetDirectDepends(*l);
725
    if (tgtdeps.count(gtIn)) {
726
      return true;
727
    }
728
  }
729 730 731
  return false;
}

732 733
std::string cmGlobalVisualStudio7Generator::Encoding()
{
734
  return "UTF-8";
735
}