Updates will be applied on October 27th between 12pm - 12:45pm EDT (UTC-0400). Gitlab may be slow during the maintenance window.

cmVisualStudio10TargetGenerator.cxx 165 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 "cmVisualStudio10TargetGenerator.h"
Brad King's avatar
Brad King committed
4

5
#include "cmAlgorithms.h"
6
#include "cmComputeLinkInformation.h"
7
#include "cmCustomCommand.h"
8
#include "cmCustomCommandGenerator.h"
9
#include "cmGeneratedFileStream.h"
10
#include "cmGeneratorExpression.h"
11
12
#include "cmGeneratorTarget.h"
#include "cmGlobalVisualStudio10Generator.h"
13
#include "cmLocalVisualStudio10Generator.h"
14
15
#include "cmMakefile.h"
#include "cmSourceFile.h"
16
#include "cmSystemTools.h"
17
18
#include "cmVisualStudioGeneratorOptions.h"
#include "windows.h"
19

20
#include <iterator>
21
#include <memory> // IWYU pragma: keep
22

23
24
25
26
27
28
29
30
31
32
static void ConvertToWindowsSlash(std::string& s);

static std::string cmVS10EscapeXML(std::string arg)
{
  cmSystemTools::ReplaceString(arg, "&", "&amp;");
  cmSystemTools::ReplaceString(arg, "<", "&lt;");
  cmSystemTools::ReplaceString(arg, ">", "&gt;");
  return arg;
}

33
34
35
36
37
38
static std::string cmVS10EscapeAttr(std::string arg)
{
  cmSystemTools::ReplaceString(arg, "&", "&amp;");
  cmSystemTools::ReplaceString(arg, "<", "&lt;");
  cmSystemTools::ReplaceString(arg, ">", "&gt;");
  cmSystemTools::ReplaceString(arg, "\"", "&quot;");
39
  cmSystemTools::ReplaceString(arg, "\n", "&#10;");
40
41
42
  return arg;
}

43
44
struct cmVisualStudio10TargetGenerator::Elem
{
45
  std::ostream& S;
46
  const int Indent;
47
  bool HasElements = false;
48
  bool HasContent = false;
49
  std::string Tag;
50

51
  Elem(std::ostream& s, const char* tag)
52
    : S(s)
53
    , Indent(0)
54
    , Tag(tag)
55
  {
56
    this->StartElement();
57
  }
58
  Elem(const Elem&) = delete;
59
60
61
  Elem(Elem& par, const char* tag)
    : S(par.S)
    , Indent(par.Indent + 1)
62
    , Tag(tag)
63
64
  {
    par.SetHasElements();
65
    this->StartElement();
66
  }
67
68
69
  void SetHasElements()
  {
    if (!HasElements) {
70
      this->S << ">\n";
71
72
73
      HasElements = true;
    }
  }
74
  std::ostream& WriteString(const char* line);
75
  void StartElement() { this->WriteString("<") << this->Tag; }
76
77
  void Element(const char* tag, const std::string& val)
  {
78
    Elem(*this, tag).Content(val);
79
  }
80
81
  Elem& Attribute(const char* an, const std::string& av)
  {
82
    this->S << " " << an << "=\"" << cmVS10EscapeAttr(av) << "\"";
83
84
    return *this;
  }
85
86
  void Content(const std::string& val)
  {
87
88
89
90
91
    if (!this->HasContent) {
      this->S << ">";
      this->HasContent = true;
    }
    this->S << cmVS10EscapeXML(val);
92
  }
93
  ~Elem()
94
  {
95
96
97
98
99
    // Do not emit element which has not been started
    if (Tag.empty()) {
      return;
    }

100
    if (HasElements) {
101
102
103
104
105
106
      this->WriteString("</") << this->Tag << ">";
      if (this->Indent > 0) {
        this->S << '\n';
      } else {
        // special case: don't print EOL at EOF
      }
107
108
    } else if (HasContent) {
      this->S << "</" << this->Tag << ">\n";
109
    } else {
110
      this->S << " />\n";
111
112
    }
  }
113
114
115

  void WritePlatformConfigTag(const char* tag, const std::string& cond,
                              const std::string& content);
116
117
};

118
119
120
class cmVS10GeneratorOptions : public cmVisualStudioGeneratorOptions
{
public:
121
  typedef cmVisualStudio10TargetGenerator::Elem Elem;
122
123
124
125
126
127
128
129
  cmVS10GeneratorOptions(cmLocalVisualStudioGenerator* lg, Tool tool,
                         cmVS7FlagTable const* table,
                         cmVisualStudio10TargetGenerator* g = nullptr)
    : cmVisualStudioGeneratorOptions(lg, tool, table)
    , TargetGenerator(g)
  {
  }

130
  void OutputFlag(std::ostream& /*fout*/, int /*indent*/, const char* tag,
131
132
133
134
135
                  const std::string& content) override
  {
    if (!this->GetConfiguration().empty()) {
      // if there are configuration specific flags, then
      // use the configuration specific tag for PreprocessorDefinitions
136
137
138
      const std::string cond =
        this->TargetGenerator->CalcCondition(this->GetConfiguration());
      this->Parent->WritePlatformConfigTag(tag, cond, content);
139
    } else {
140
      this->Parent->Element(tag, content);
141
142
143
144
    }
  }

private:
145
146
147
  cmVisualStudio10TargetGenerator* const TargetGenerator;
  Elem* Parent = nullptr;
  friend cmVisualStudio10TargetGenerator::OptionsHelper;
148
149
};

150
struct cmVisualStudio10TargetGenerator::OptionsHelper
151
{
152
153
154
155
156
157
158
  cmVS10GeneratorOptions& O;
  OptionsHelper(cmVS10GeneratorOptions& o, Elem& e)
    : O(o)
  {
    O.Parent = &e;
  }
  ~OptionsHelper() { O.Parent = nullptr; }
159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  void OutputPreprocessorDefinitions(const std::string& lang)
  {
    O.OutputPreprocessorDefinitions(O.Parent->S, O.Parent->Indent + 1, lang);
  }
  void OutputAdditionalIncludeDirectories(const std::string& lang)
  {
    O.OutputAdditionalIncludeDirectories(O.Parent->S, O.Parent->Indent + 1,
                                         lang);
  }
  void OutputFlagMap() { O.OutputFlagMap(O.Parent->S, O.Parent->Indent + 1); }
  void PrependInheritedString(std::string const& key)
  {
    O.PrependInheritedString(key);
  }
};
175

176
177
178
179
180
181
static std::string cmVS10EscapeComment(std::string comment)
{
  // MSBuild takes the CDATA of a <Message></Message> element and just
  // does "echo $CDATA" with no escapes.  We must encode the string.
  // http://technet.microsoft.com/en-us/library/cc772462%28WS.10%29.aspx
  std::string echoable;
182
183
  for (char c : comment) {
    switch (c) {
184
185
186
187
188
      case '\r':
        break;
      case '\n':
        echoable += '\t';
        break;
189
190
191
192
193
      case '"': /* no break */
      case '|': /* no break */
      case '&': /* no break */
      case '<': /* no break */
      case '>': /* no break */
194
195
      case '^':
        echoable += '^'; /* no break */
196
        CM_FALLTHROUGH;
197
      default:
198
        echoable += c;
199
        break;
200
    }
201
  }
202
203
204
  return echoable;
}

205
206
207
208
209
210
static bool cmVS10IsTargetsFile(std::string const& path)
{
  std::string const ext = cmSystemTools::GetFilenameLastExtension(path);
  return cmSystemTools::Strucmp(ext.c_str(), ".targets") == 0;
}

211
static std::string computeProjectFileExtension(cmGeneratorTarget const* t)
212
213
214
{
  std::string res;
  res = ".vcxproj";
215
  if (t->IsCSharpOnly()) {
216
217
218
219
220
    res = ".csproj";
  }
  return res;
}

221
222
cmVisualStudio10TargetGenerator::cmVisualStudio10TargetGenerator(
  cmGeneratorTarget* target, cmGlobalVisualStudio10Generator* gg)
223
224
225
226
227
228
  : GeneratorTarget(target)
  , Makefile(target->Target->GetMakefile())
  , Platform(gg->GetPlatformName())
  , Name(target->GetName())
  , GUID(gg->GetGUID(this->Name))
  , GlobalGenerator(gg)
229
230
  , LocalGenerator(
      (cmLocalVisualStudio10Generator*)target->GetLocalGenerator())
231
{
232
  this->Makefile->GetConfigurations(this->Configurations);
233
  this->NsightTegra = gg->IsNsightTegra();
234
  for (int i = 0; i < 4; ++i) {
235
    this->NsightTegraVersion[i] = 0;
236
  }
237
238
239
  sscanf(gg->GetNsightTegraVersion().c_str(), "%u.%u.%u.%u",
         &this->NsightTegraVersion[0], &this->NsightTegraVersion[1],
         &this->NsightTegraVersion[2], &this->NsightTegraVersion[3]);
240
  this->MSTools = !this->NsightTegra;
241
  this->Managed = false;
242
  this->TargetCompileAsWinRT = false;
243
244
  this->IsMissingFiles = false;
  this->DefaultArtifactDir =
245
    this->LocalGenerator->GetCurrentBinaryDirectory() + "/" +
246
    this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
247
248
  this->InSourceBuild = (this->Makefile->GetCurrentSourceDirectory() ==
                         this->Makefile->GetCurrentBinaryDirectory());
249
250
251
252
253
254
}

cmVisualStudio10TargetGenerator::~cmVisualStudio10TargetGenerator()
{
}

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
std::string cmVisualStudio10TargetGenerator::CalcCondition(
  const std::string& config) const
{
  std::ostringstream oss;
  oss << "'$(Configuration)|$(Platform)'=='";
  oss << config << "|" << this->Platform;
  oss << "'";
  // handle special case for 32 bit C# targets
  if (this->ProjectType == csproj && this->Platform == "Win32") {
    oss << " Or ";
    oss << "'$(Configuration)|$(Platform)'=='";
    oss << config << "|x86";
    oss << "'";
  }
  return oss.str();
}

272
273
void cmVisualStudio10TargetGenerator::Elem::WritePlatformConfigTag(
  const char* tag, const std::string& cond, const std::string& content)
274
{
275
  Elem(*this, tag).Attribute("Condition", cond).Content(content);
276
277
}

278
std::ostream& cmVisualStudio10TargetGenerator::Elem::WriteString(
279
280
281
282
283
284
285
286
287
288
  const char* line)
{
  this->S.fill(' ');
  this->S.width(this->Indent * 2);
  // write an empty string to get the fill level indent to print
  this->S << "";
  this->S << line;
  return this->S;
}

289
290
291
292
293
#define VS10_CXX_DEFAULT_PROPS "$(VCTargetsPath)\\Microsoft.Cpp.Default.props"
#define VS10_CXX_PROPS "$(VCTargetsPath)\\Microsoft.Cpp.props"
#define VS10_CXX_USER_PROPS                                                   \
  "$(UserRootDir)\\Microsoft.Cpp.$(Platform).user.props"
#define VS10_CXX_TARGETS "$(VCTargetsPath)\\Microsoft.Cpp.targets"
294

295
296
297
298
299
300
301
302
#define VS10_CSharp_DEFAULT_PROPS                                             \
  "$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props"
// This does not seem to exist by default, it's just provided for consistency
// in case users want to have default custom props for C# targets
#define VS10_CSharp_USER_PROPS                                                \
  "$(UserRootDir)\\Microsoft.CSharp.$(Platform).user.props"
#define VS10_CSharp_TARGETS "$(MSBuildToolsPath)\\Microsoft.CSharp.targets"

303
304
305
306
307
#define VS10_CSharp_NETCF_TARGETS                                             \
  "$(MSBuildExtensionsPath)\\Microsoft\\$(TargetFrameworkIdentifier)\\"       \
  "$(TargetFrameworkTargetsVersion)\\Microsoft.$(TargetFrameworkIdentifier)"  \
  ".CSharp.targets"

308
void cmVisualStudio10TargetGenerator::Generate()
309
310
{
  // do not generate external ms projects
311
  if (this->GeneratorTarget->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
312
      this->GeneratorTarget->GetProperty("EXTERNAL_MSPROJECT")) {
313
    return;
314
  }
315
316
  const std::string ProjectFileExtension =
    computeProjectFileExtension(this->GeneratorTarget);
317
  if (ProjectFileExtension == ".vcxproj") {
318
319
    this->ProjectType = vcxproj;
    this->Managed = false;
320
  } else if (ProjectFileExtension == ".csproj") {
321
322
323
324
325
    if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY) {
      std::string message = "The C# target \"" +
        this->GeneratorTarget->GetName() +
        "\" is of type STATIC_LIBRARY. This is discouraged (and may be "
        "disabled in future). Make it a SHARED library instead.";
326
      this->Makefile->IssueMessage(MessageType::DEPRECATION_WARNING, message);
327
    }
328
329
330
    this->ProjectType = csproj;
    this->Managed = true;
  }
331
  // Tell the global generator the name of the project file
332
333
  this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME",
                                             this->Name.c_str());
334
335
  this->GeneratorTarget->Target->SetProperty("GENERATOR_FILE_NAME_EXT",
                                             ProjectFileExtension.c_str());
336
337
  this->DotNetHintReferences.clear();
  this->AdditionalUsingDirectories.clear();
338
  if (this->GeneratorTarget->GetType() <= cmStateEnums::OBJECT_LIBRARY) {
339
    if (!this->ComputeClOptions()) {
340
      return;
341
342
    }
    if (!this->ComputeRcOptions()) {
343
      return;
344
    }
345
346
347
    if (!this->ComputeCudaOptions()) {
      return;
    }
348
349
350
    if (!this->ComputeCudaLinkOptions()) {
      return;
    }
351
    if (!this->ComputeMasmOptions()) {
352
      return;
353
    }
354
355
356
    if (!this->ComputeNasmOptions()) {
      return;
    }
357
    if (!this->ComputeLinkOptions()) {
358
      return;
359
    }
360
361
362
    if (!this->ComputeLibOptions()) {
      return;
    }
363
364
  }
  std::string path = this->LocalGenerator->GetCurrentBinaryDirectory();
365
  path += "/";
366
  path += this->Name;
367
  path += ProjectFileExtension;
368
  cmGeneratedFileStream BuildFileStream(path);
369
370
  const std::string PathToProjectFile = path;
  BuildFileStream.SetCopyIfDifferent(true);
371

372
  // Write the encoding header into the file
373
  char magic[] = { char(0xEF), char(0xBB), char(0xBF) };
374
375
376
377
378
  BuildFileStream.write(magic, 3);
  BuildFileStream << "<?xml version=\"1.0\" encoding=\""
                  << this->GlobalGenerator->Encoding() << "\"?>"
                  << "\n";
  {
379
    Elem e0(BuildFileStream, "Project");
380
    e0.Attribute("DefaultTargets", "Build");
381
382
383
384
385
386
387
    const char* toolsVersion = this->GlobalGenerator->GetToolsVersion();
    if (this->GlobalGenerator->GetVersion() ==
          cmGlobalVisualStudioGenerator::VS12 &&
        this->GlobalGenerator->TargetsWindowsCE()) {
      toolsVersion = "4.0";
    }
    e0.Attribute("ToolsVersion", toolsVersion);
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
    e0.Attribute("xmlns",
                 "http://schemas.microsoft.com/developer/msbuild/2003");

    if (this->NsightTegra) {
      Elem e1(e0, "PropertyGroup");
      e1.Attribute("Label", "NsightTegraProject");
      const unsigned int nsightTegraMajorVersion = this->NsightTegraVersion[0];
      const unsigned int nsightTegraMinorVersion = this->NsightTegraVersion[1];
      if (nsightTegraMajorVersion >= 2) {
        if (nsightTegraMajorVersion > 3 ||
            (nsightTegraMajorVersion == 3 && nsightTegraMinorVersion >= 1)) {
          e1.Element("NsightTegraProjectRevisionNumber", "11");
        } else {
          // Nsight Tegra 2.0 uses project revision 9.
          e1.Element("NsightTegraProjectRevisionNumber", "9");
        }
        // Tell newer versions to upgrade silently when loading.
        e1.Element("NsightTegraUpgradeOnceWithoutPrompt", "true");
406
      } else {
407
408
        // Require Nsight Tegra 1.6 for JCompile support.
        e1.Element("NsightTegraProjectRevisionNumber", "7");
409
      }
410
411
    }

412
413
414
415
416
    if (const char* hostArch =
          this->GlobalGenerator->GetPlatformToolsetHostArchitecture()) {
      Elem e1(e0, "PropertyGroup");
      e1.Element("PreferredToolArchitecture", hostArch);
    }
417

418
419
420
    if (this->ProjectType != csproj) {
      this->WriteProjectConfigurations(e0);
    }
421

422
423
424
425
    {
      Elem e1(e0, "PropertyGroup");
      e1.Attribute("Label", "Globals");
      e1.Element("ProjectGuid", "{" + this->GUID + "}");
426

427
428
429
430
431
      if (this->MSTools &&
          this->GeneratorTarget->GetType() <= cmStateEnums::GLOBAL_TARGET) {
        this->WriteApplicationTypeSettings(e1);
        this->VerifyNecessaryFiles();
      }
432

433
434
435
436
437
438
439
440
      const char* vsProjectTypes =
        this->GeneratorTarget->GetProperty("VS_GLOBAL_PROJECT_TYPES");
      if (vsProjectTypes) {
        const char* tagName = "ProjectTypes";
        if (this->ProjectType == csproj) {
          tagName = "ProjectTypeGuids";
        }
        e1.Element(tagName, vsProjectTypes);
441
      }
442

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
      const char* vsProjectName =
        this->GeneratorTarget->GetProperty("VS_SCC_PROJECTNAME");
      const char* vsLocalPath =
        this->GeneratorTarget->GetProperty("VS_SCC_LOCALPATH");
      const char* vsProvider =
        this->GeneratorTarget->GetProperty("VS_SCC_PROVIDER");

      if (vsProjectName && vsLocalPath && vsProvider) {
        e1.Element("SccProjectName", vsProjectName);
        e1.Element("SccLocalPath", vsLocalPath);
        e1.Element("SccProvider", vsProvider);

        const char* vsAuxPath =
          this->GeneratorTarget->GetProperty("VS_SCC_AUXPATH");
        if (vsAuxPath) {
          e1.Element("SccAuxPath", vsAuxPath);
        }
460
      }
Steven Velez's avatar
Steven Velez committed
461

462
463
464
      if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT")) {
        e1.Element("WinMDAssembly", "true");
      }
465

466
467
468
469
470
471
472
      const char* vsGlobalKeyword =
        this->GeneratorTarget->GetProperty("VS_GLOBAL_KEYWORD");
      if (!vsGlobalKeyword) {
        e1.Element("Keyword", "Win32Proj");
      } else {
        e1.Element("Keyword", vsGlobalKeyword);
      }
473

474
475
476
477
478
      const char* vsGlobalRootNamespace =
        this->GeneratorTarget->GetProperty("VS_GLOBAL_ROOTNAMESPACE");
      if (vsGlobalRootNamespace) {
        e1.Element("RootNamespace", vsGlobalRootNamespace);
      }
479

480
481
482
483
484
      e1.Element("Platform", this->Platform);
      const char* projLabel =
        this->GeneratorTarget->GetProperty("PROJECT_LABEL");
      if (!projLabel) {
        projLabel = this->Name.c_str();
485
      }
486
487
488
489
490
491
492
493
494
495
      e1.Element("ProjectName", projLabel);
      {
        // TODO: add deprecation warning for VS_* property?
        const char* targetFrameworkVersion =
          this->GeneratorTarget->GetProperty(
            "VS_DOTNET_TARGET_FRAMEWORK_VERSION");
        if (!targetFrameworkVersion) {
          targetFrameworkVersion = this->GeneratorTarget->GetProperty(
            "DOTNET_TARGET_FRAMEWORK_VERSION");
        }
496
497
498
499
500
501
502
        if (!targetFrameworkVersion && this->ProjectType == csproj &&
            this->GlobalGenerator->TargetsWindowsCE() &&
            this->GlobalGenerator->GetVersion() ==
              cmGlobalVisualStudioGenerator::VS12) {
          // VS12 .NETCF default to .NET framework 3.9
          targetFrameworkVersion = "v3.9";
        }
503
504
505
        if (targetFrameworkVersion) {
          e1.Element("TargetFrameworkVersion", targetFrameworkVersion);
        }
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
        if (this->ProjectType == csproj &&
            this->GlobalGenerator->TargetsWindowsCE()) {
          const char* targetFrameworkId = this->GeneratorTarget->GetProperty(
            "VS_TARGET_FRAMEWORK_IDENTIFIER");
          if (!targetFrameworkId) {
            targetFrameworkId = "WindowsEmbeddedCompact";
          }
          e1.Element("TargetFrameworkIdentifier", targetFrameworkId);
          const char* targetFrameworkVer = this->GeneratorTarget->GetProperty(
            "VS_TARGET_FRAMEWORKS_TARGET_VERSION");
          if (!targetFrameworkVer) {
            targetFrameworkVer = "v8.0";
          }
          e1.Element("TargetFrameworkTargetsVersion", targetFrameworkVer);
        }
521
      }
522

523
524
525
526
527
528
529
      // Disable the project upgrade prompt that is displayed the first time a
      // project using an older toolset version is opened in a newer version of
      // the IDE (respected by VS 2013 and above).
      if (this->GlobalGenerator->GetVersion() >=
          cmGlobalVisualStudioGenerator::VS12) {
        e1.Element("VCProjectUpgraderObjectName", "NoUpgrade");
      }
530

531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
      std::vector<std::string> keys = this->GeneratorTarget->GetPropertyKeys();
      for (std::string const& keyIt : keys) {
        static const char* prefix = "VS_GLOBAL_";
        if (keyIt.find(prefix) != 0)
          continue;
        std::string globalKey = keyIt.substr(strlen(prefix));
        // Skip invalid or separately-handled properties.
        if (globalKey.empty() || globalKey == "PROJECT_TYPES" ||
            globalKey == "ROOTNAMESPACE" || globalKey == "KEYWORD") {
          continue;
        }
        const char* value = this->GeneratorTarget->GetProperty(keyIt);
        if (!value)
          continue;
        e1.Element(globalKey.c_str(), value);
546
      }
547

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
      if (this->Managed) {
        std::string outputType;
        switch (this->GeneratorTarget->GetType()) {
          case cmStateEnums::OBJECT_LIBRARY:
          case cmStateEnums::STATIC_LIBRARY:
          case cmStateEnums::SHARED_LIBRARY:
            outputType = "Library";
            break;
          case cmStateEnums::MODULE_LIBRARY:
            outputType = "Module";
            break;
          case cmStateEnums::EXECUTABLE:
            if (this->GeneratorTarget->Target->GetPropertyAsBool(
                  "WIN32_EXECUTABLE")) {
              outputType = "WinExe";
            } else {
              outputType = "Exe";
            }
            break;
          case cmStateEnums::UTILITY:
          case cmStateEnums::GLOBAL_TARGET:
            outputType = "Utility";
            break;
          case cmStateEnums::UNKNOWN_LIBRARY:
          case cmStateEnums::INTERFACE_LIBRARY:
            break;
        }
        e1.Element("OutputType", outputType);
        e1.Element("AppDesignerFolder", "Properties");
577
      }
578
    }
579

580
581
    switch (this->ProjectType) {
      case vcxproj:
582
583
584
585
586
        if (this->GlobalGenerator->GetPlatformToolsetVersion()) {
          Elem(e0, "Import")
            .Attribute("Project",
                       this->GlobalGenerator->GetAuxiliaryToolset());
        }
587
        Elem(e0, "Import").Attribute("Project", VS10_CXX_DEFAULT_PROPS);
588
589
590
591
        break;
      case csproj:
        Elem(e0, "Import")
          .Attribute("Project", VS10_CSharp_DEFAULT_PROPS)
592
          .Attribute("Condition", "Exists('" VS10_CSharp_DEFAULT_PROPS "')");
593
594
        break;
    }
595

596
    this->WriteProjectConfigurationValues(e0);
597

598
    if (this->ProjectType == vcxproj) {
599
      Elem(e0, "Import").Attribute("Project", VS10_CXX_PROPS);
600
    }
601
602
603
604
605
606
607
    {
      Elem e1(e0, "ImportGroup");
      e1.Attribute("Label", "ExtensionSettings");
      e1.SetHasElements();

      if (this->GlobalGenerator->IsCudaEnabled()) {
        Elem(e1, "Import")
608
609
610
611
          .Attribute("Project",
                     "$(VCTargetsPath)\\BuildCustomizations\\CUDA " +
                       this->GlobalGenerator->GetPlatformToolsetCudaString() +
                       ".props");
612
613
614
615
      }
      if (this->GlobalGenerator->IsMasmEnabled()) {
        Elem(e1, "Import")
          .Attribute("Project",
616
                     "$(VCTargetsPath)\\BuildCustomizations\\masm.props");
617
618
619
620
621
622
623
624
625
626
      }
      if (this->GlobalGenerator->IsNasmEnabled()) {
        // Always search in the standard modules location.
        std::string propsTemplate =
          GetCMakeFilePath("Templates/MSBuild/nasm.props.in");

        std::string propsLocal;
        propsLocal += this->DefaultArtifactDir;
        propsLocal += "\\nasm.props";
        ConvertToWindowsSlash(propsLocal);
627
628
        this->Makefile->ConfigureFile(propsTemplate, propsLocal, false, true,
                                      true);
629
        Elem(e1, "Import").Attribute("Project", propsLocal);
630
      }
631
    }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
    {
      Elem e1(e0, "ImportGroup");
      e1.Attribute("Label", "PropertySheets");
      std::string props;
      switch (this->ProjectType) {
        case vcxproj:
          props = VS10_CXX_USER_PROPS;
          break;
        case csproj:
          props = VS10_CSharp_USER_PROPS;
          break;
      }
      if (const char* p =
            this->GeneratorTarget->GetProperty("VS_USER_PROPS")) {
        props = p;
      }
      if (!props.empty()) {
        ConvertToWindowsSlash(props);
        Elem(e1, "Import")
          .Attribute("Project", props)
          .Attribute("Condition", "exists('" + props + "')")
653
          .Attribute("Label", "LocalAppDataPlatform");
654
655
656
      }

      this->WritePlatformExtensions(e1);
657
    }
658
    Elem(e0, "PropertyGroup").Attribute("Label", "UserMacros");
659
660
    this->WriteWinRTPackageCertificateKeyFile(e0);
    this->WritePathAndIncrementalLinkOptions(e0);
661
    this->WriteCEDebugProjectConfigurationValues(e0);
662
663
664
665
666
667
668
669
670
    this->WriteItemDefinitionGroups(e0);
    this->WriteCustomCommands(e0);
    this->WriteAllSources(e0);
    this->WriteDotNetReferences(e0);
    this->WriteEmbeddedResourceGroup(e0);
    this->WriteXamlFilesGroup(e0);
    this->WriteWinRTReferences(e0);
    this->WriteProjectReferences(e0);
    this->WriteSDKReferences(e0);
671
672
    switch (this->ProjectType) {
      case vcxproj:
673
        Elem(e0, "Import").Attribute("Project", VS10_CXX_TARGETS);
674
675
        break;
      case csproj:
676
677
678
679
680
        if (this->GlobalGenerator->TargetsWindowsCE()) {
          Elem(e0, "Import").Attribute("Project", VS10_CSharp_NETCF_TARGETS);
        } else {
          Elem(e0, "Import").Attribute("Project", VS10_CSharp_TARGETS);
        }
681
682
        break;
    }
683

684
685
686
687
    this->WriteTargetSpecificReferences(e0);
    {
      Elem e1(e0, "ImportGroup");
      e1.Attribute("Label", "ExtensionTargets");
688
      e1.SetHasElements();
689
690
691
692
693
694
      this->WriteTargetsFileReferences(e1);
      if (this->GlobalGenerator->IsCudaEnabled()) {
        Elem(e1, "Import")
          .Attribute("Project",
                     "$(VCTargetsPath)\\BuildCustomizations\\CUDA " +
                       this->GlobalGenerator->GetPlatformToolsetCudaString() +
695
                       ".targets");
696
697
698
699
      }
      if (this->GlobalGenerator->IsMasmEnabled()) {
        Elem(e1, "Import")
          .Attribute("Project",
700
                     "$(VCTargetsPath)\\BuildCustomizations\\masm.targets");
701
702
703
704
      }
      if (this->GlobalGenerator->IsNasmEnabled()) {
        std::string nasmTargets =
          GetCMakeFilePath("Templates/MSBuild/nasm.targets");
705
        Elem(e1, "Import").Attribute("Project", nasmTargets);
706
      }
707
    }
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
    if (this->ProjectType == csproj) {
      for (std::string const& c : this->Configurations) {
        Elem e1(e0, "PropertyGroup");
        e1.Attribute("Condition", "'$(Configuration)' == '" + c + "'");
        e1.SetHasElements();
        this->WriteEvents(e1, c);
      }
      // make sure custom commands are executed before build (if necessary)
      {
        Elem e1(e0, "PropertyGroup");
        std::ostringstream oss;
        oss << "\n";
        for (std::string const& i : this->CSharpCustomCommandNames) {
          oss << "      " << i << ";\n";
        }
        oss << "      "
            << "$(BuildDependsOn)\n";
        e1.Element("BuildDependsOn", oss.str());
726
      }
727
    }
728
729
730
731
  }

  if (BuildFileStream.Close()) {
    this->GlobalGenerator->FileReplacedDuringGenerate(PathToProjectFile);
732
  }
733

734
735
736
737
  // The groups are stored in a separate file for VS 10
  this->WriteGroups();
}

738
void cmVisualStudio10TargetGenerator::WriteDotNetReferences(Elem& e0)
739
{
740
  std::vector<std::string> references;
741
742
  if (const char* vsDotNetReferences =
        this->GeneratorTarget->GetProperty("VS_DOTNET_REFERENCES")) {
743
    cmSystemTools::ExpandListArgument(vsDotNetReferences, references);
744
  }
745
  cmPropertyMap const& props = this->GeneratorTarget->Target->GetProperties();
746
747
748
  for (auto const& i : props) {
    if (i.first.find("VS_DOTNET_REFERENCE_") == 0) {
      std::string name = i.first.substr(20);
749
      if (!name.empty()) {
750
        std::string path = i.second.GetValue();
751
        if (!cmsys::SystemTools::FileIsFullPath(path)) {
752
          path = this->Makefile->GetCurrentSourceDirectory() + "/" + path;
753
        }
754
        ConvertToWindowsSlash(path);
755
756
        this->DotNetHintReferences[""].push_back(
          DotNetHintReference(name, path));
757
758
759
      }
    }
  }
760
  if (!references.empty() || !this->DotNetHintReferences.empty()) {
761
    Elem e1(e0, "ItemGroup");
762
    for (std::string const& ri : references) {
763
764
      // if the entry from VS_DOTNET_REFERENCES is an existing file, generate
      // a new hint-reference and name it from the filename
765
      if (cmsys::SystemTools::FileExists(ri, true)) {
766
767
        std::string name =
          cmsys::SystemTools::GetFilenameWithoutLastExtension(ri);
768
        std::string path = ri;
769
        ConvertToWindowsSlash(path);
770
771
        this->DotNetHintReferences[""].push_back(
          DotNetHintReference(name, path));
772
      } else {
773
        this->WriteDotNetReference(e1, ri, "", "");
774
775
      }
    }
776
777
778
779
    for (const auto& h : this->DotNetHintReferences) {
      // DotNetHintReferences is also populated from AddLibraries().
      // The configuration specific hint references are added there.
      for (const auto& i : h.second) {
780
        this->WriteDotNetReference(e1, i.first, i.second, h.first);
781
      }
782
    }
783
  }
784
785
}

786
void cmVisualStudio10TargetGenerator::WriteDotNetReference(
787
788
  Elem& e1, std::string const& ref, std::string const& hint,
  std::string const& config)
789
{
790
  Elem e2(e1, "Reference");
791
792
793
794
  // If 'config' is not empty, the reference is only added for the given
  // configuration. This is used when referencing imported managed assemblies.
  // See also cmVisualStudio10TargetGenerator::AddLibraries().
  if (!config.empty()) {
795
    e2.Attribute("Condition", this->CalcCondition(config));
796
  }
797
798
799
  e2.Attribute("Include", ref);
  e2.Element("CopyLocalSatelliteAssemblies", "true");
  e2.Element("ReferenceOutputAssembly", "true");
800
801
802
803
804
805
806
807
  if (!hint.empty()) {
    const char* privateReference = "True";
    if (const char* value = this->GeneratorTarget->GetProperty(
          "VS_DOTNET_REFERENCES_COPY_LOCAL")) {
      if (cmSystemTools::IsOff(value)) {
        privateReference = "False";
      }
    }
808
809
    e2.Element("Private", privateReference);
    e2.Element("HintPath", hint);
810
  }
811
  this->WriteDotNetReferenceCustomTags(e2, ref);
812
813
}

814
void cmVisualStudio10TargetGenerator::WriteDotNetReferenceCustomTags(
815
  Elem& e2, std::string const& ref)
816
817
818
819
820
821
822
823
{

  static const std::string refpropPrefix = "VS_DOTNET_REFERENCEPROP_";
  static const std::string refpropInfix = "_TAG_";
  const std::string refPropFullPrefix = refpropPrefix + ref + refpropInfix;
  typedef std::map<std::string, std::string> CustomTags;
  CustomTags tags;
  cmPropertyMap const& props = this->GeneratorTarget->Target->GetProperties();
824
825
826
827
  for (const auto& i : props) {
    if (i.first.find(refPropFullPrefix) == 0) {
      std::string refTag = i.first.substr(refPropFullPrefix.length());
      std::string refVal = i.second.GetValue();
828
829
830
831
832
      if (!refTag.empty() && !refVal.empty()) {
        tags[refTag] = refVal;
      }
    }
  }
833
  for (auto const& tag : tags) {
834
    e2.Element(tag.first.c_str(), tag.second);
835
836
837
  }
}

838
void cmVisualStudio10TargetGenerator::WriteEmbeddedResourceGroup(Elem& e0)
839
{
840
  std::vector<cmSourceFile const*> resxObjs;
841
842
  this->GeneratorTarget->GetResxSources(resxObjs, "");
  if (!resxObjs.empty()) {
843
    Elem e1(e0, "ItemGroup");
844
    std::string srcDir = this->Makefile->GetCurrentSourceDirectory();
845
    ConvertToWindowsSlash(srcDir);
846
847
    for (cmSourceFile const* oi : resxObjs) {
      std::string obj = oi->GetFullPath();
848
      ConvertToWindowsSlash(obj);
849
      bool useRelativePath = false;
850
      if (this->ProjectType == csproj && this->InSourceBuild) {
851
852
853
854
855
856
        // If we do an in-source build and the resource file is in a
        // subdirectory
        // of the .csproj file, we have to use relative pathnames, otherwise
        // visual studio does not show the file in the IDE. Sorry.
        if (obj.find(srcDir) == 0) {
          obj = this->ConvertPath(obj, true);
857
          ConvertToWindowsSlash(obj);
858
859
860
          useRelativePath = true;
        }
      }
861
862
      Elem e2(e1, "EmbeddedResource");
      e2.Attribute("Include", obj);
863

864
      if (this->ProjectType != csproj) {
865
        std::string hFileName = obj.substr(0, obj.find_last_of(".")) + ".h";
866
        e2.Element("DependentUpon", hFileName);
867

868
        for (std::string const& c : this->Configurations) {
869
          std::string s;
870
871
872
          if (this->GeneratorTarget->GetProperty("VS_GLOBAL_ROOTNAMESPACE") ||
              // Handle variant of VS_GLOBAL_<variable> for RootNamespace.
              this->GeneratorTarget->GetProperty("VS_GLOBAL_RootNamespace")) {
873
            s = "$(RootNamespace).";
874
          }
875
          s += "%(Filename).resources";
876
          e2.WritePlatformConfigTag("LogicalName", this->CalcCondition(c), s);
877
878
879
        }
      } else {
        std::string binDir = this->Makefile->GetCurrentBinaryDirectory();
880
        ConvertToWindowsSlash(binDir);
881
882
883
884
885
886
887
888
889
890
891
892
        // If the resource was NOT added using a relative path (which should
        // be the default), we have to provide a link here
        if (!useRelativePath) {
          std::string link;
          if (obj.find(srcDir) == 0) {
            link = obj.substr(srcDir.length() + 1);
          } else if (obj.find(binDir) == 0) {
            link = obj.substr(binDir.length() + 1);
          } else {
            link = cmsys::SystemTools::GetFilenameName(obj);
          }
          if (!link.empty()) {
893
            e2.Element("Link", link);
894
895
896
897
          }
        }
        // Determine if this is a generated resource from a .Designer.cs file
        std::string designerResource =
898
899
          cmSystemTools::GetFilenamePath(oi->GetFullPath()) + "/" +
          cmSystemTools::GetFilenameWithoutLastExtension(oi->GetFullPath()) +
900
901
902
          ".Designer.cs";
        if (cmsys::SystemTools::FileExists(designerResource)) {
          std::string generator = "PublicResXFileCodeGenerator";
903
          if (const char* g = oi->GetProperty("VS_RESOURCE_GENERATOR")) {
904
905
            generator = g;
          }
906
          if (!generator.empty()) {
907
            e2.Element("Generator", generator);
908
909
910
911
912
913
914
915
            if (designerResource.find(srcDir) == 0) {
              designerResource = designerResource.substr(srcDir.length() + 1);
            } else if (designerResource.find(binDir) == 0) {
              designerResource = designerResource.substr(binDir.length() + 1);
            } else {
              designerResource =
                cmsys::SystemTools::GetFilenameName(designerResource);
            }
916
            ConvertToWindowsSlash(designerResource);
917
            e2.Element("LastGenOutput", designerResource);
918
919
          }
        }
920
921
        const cmPropertyMap& props = oi->GetProperties();
        for (const auto& p : props) {
922
          static const std::string propNamePrefix = "VS_CSHARP_";
923
924
          if (p.first.find(propNamePrefix) == 0) {
            std::string tagName = p.first.substr(propNamePrefix.length());
925
            if (!tagName.empty()) {
926
              std::string value = props.GetPropertyValue(p.first);
927
              if (!value.empty()) {
928
                e2.Element(tagName.c_str(), value);
929
930
              }
            }
931
          }
932
        }
933
      }
934
    }
935
  }
936
937
}

938
void cmVisualStudio10TargetGenerator::WriteXamlFilesGroup(Elem& e0)
939
940
941
{
  std::vector<cmSourceFile const*> xamlObjs;
  this->GeneratorTarget->GetXamlSources(xamlObjs, "");
942
  if (!xamlObjs.empty()) {
943
    Elem e1(e0, "ItemGroup");
944
945
    for (cmSourceFile const* oi : xamlObjs) {
      std::string obj = oi->GetFullPath();
946
      const char* xamlType;
947
      const char* xamlTypeProperty = oi->GetProperty("VS_XAML_TYPE");
948
      if (xamlTypeProperty) {
949
        xamlType = xamlTypeProperty;
950
      } else {
951
        xamlType = "Page";
952
      }
953

954
955
      Elem e2(e1, xamlType);
      this->WriteSource(e2, oi);
956
      e2.SetHasElements();
957
      if (this->ProjectType == csproj && !this->InSourceBuild) {
958
        // add <Link> tag to written XAML source if necessary
959
960
961
962
        const std::string& srcDir =
          this->Makefile->GetCurrentSourceDirectory();
        const std::string& binDir =
          this->Makefile->GetCurrentBinaryDirectory();
963
964
965
966
967
968
969
970
971
        std::string link;
        if (obj.find(srcDir) == 0) {
          link = obj.substr(srcDir.length() + 1);
        } else if (obj.find(binDir) == 0) {
          link = obj.substr(binDir.length() + 1);
        } else {
          link = cmsys::SystemTools::GetFilenameName(obj);
        }
        if (!link.empty()) {
972
          ConvertToWindowsSlash(link);
973
          e2.Element("Link", link);
974
975
        }
      }
976
      e2.Element("SubType", "Designer");
977
    }
978
  }
979
980
}

981
void cmVisualStudio10TargetGenerator::WriteTargetSpecificReferences(Elem& e0)
982
{
983
984
985
  if (this->MSTools) {
    if (this->GlobalGenerator->TargetsWindowsPhone() &&
        this->GlobalGenerator->GetSystemVersion() == "8.0") {
986
987
988
989
      Elem(e0, "Import")
        .Attribute("Project",
                   "$(MSBuildExtensionsPath)\\Microsoft\\WindowsPhone\\v"
                   "$(TargetPlatformVersion)\\Microsoft.Cpp.WindowsPhone."
990
                   "$(TargetPlatformVersion).targets");
991
    }
992
  }
993
994
}

995
void cmVisualStudio10TargetGenerator::WriteTargetsFileReferences(Elem& e1)
996