forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateWindowsSdkKnownFrameworkReferences.cs
140 lines (113 loc) · 7.71 KB
/
CreateWindowsSdkKnownFrameworkReferences.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace Microsoft.NET.Build.Tasks
{
// Generate KnownFrameworkReference items for the Windows SDK pack
// If WindowsSdkPackageVersion is set, then use that for the package version of the Windows SDK pack
// Otherwise, if UseWindowsSDKPreview is set, then construct the package version from the TargetPlatformVersion (dropping the 4th component of the component and appending "-preview")
// Otherwise, create KnownFrameworkReference items based on WindowsSdkSupportedTargetPlatformVersion items, using WindowsSdkPackageVersion and MinimumNETVersion metadata
public class CreateWindowsSdkKnownFrameworkReferences : TaskBase
{
public bool UseWindowsSDKPreview { get; set; }
public string WindowsSdkPackageVersion { get; set; }
public string TargetFrameworkIdentifier { get; set; }
public string TargetFrameworkVersion { get; set; }
public string TargetPlatformIdentifier { get; set; }
public string TargetPlatformVersion { get; set; }
public ITaskItem[] WindowsSdkSupportedTargetPlatformVersions { get; set; }
[Output]
public ITaskItem[] KnownFrameworkReferences { get; set; }
protected override void ExecuteCore()
{
List<ITaskItem> knownFrameworkReferences = new();
if (!string.IsNullOrEmpty(WindowsSdkPackageVersion))
{
knownFrameworkReferences.AddRange(CreateKnownFrameworkReferences(WindowsSdkPackageVersion, TargetFrameworkVersion, TargetPlatformVersion));
}
else if (UseWindowsSDKPreview)
{
var tpv = new Version(TargetPlatformVersion);
var windowsSdkPackageVersion = $"{tpv.Major}.{tpv.Minor}.{tpv.Build}-preview";
knownFrameworkReferences.AddRange(CreateKnownFrameworkReferences(windowsSdkPackageVersion, TargetFrameworkVersion, TargetPlatformVersion));
}
else
{
var normalizedTargetFrameworkVersion = ProcessFrameworkReferences.NormalizeVersion(new Version(TargetFrameworkVersion));
var knownFrameworkReferencesByWindowsSdkVersion = new Dictionary<Version, List<(Version minimumNetVersion, TaskItem[] knownFrameworkReferences)>>();
foreach (var supportedWindowsVersion in WindowsSdkSupportedTargetPlatformVersions)
{
var windowsSdkPackageVersion = supportedWindowsVersion.GetMetadata("WindowsSdkPackageVersion");
if (!string.IsNullOrEmpty(windowsSdkPackageVersion))
{
var minimumNETVersion = supportedWindowsVersion.GetMetadata("MinimumNETVersion");
Version normalizedMinimumVersion = new(0, 0, 0);
if (!string.IsNullOrEmpty(minimumNETVersion))
{
normalizedMinimumVersion = ProcessFrameworkReferences.NormalizeVersion(new Version(minimumNETVersion));
if (normalizedMinimumVersion > normalizedTargetFrameworkVersion)
{
continue;
}
}
if (!Version.TryParse(supportedWindowsVersion.ItemSpec, out var windowsSdkVersionParsed))
{
continue;
}
if (!knownFrameworkReferencesByWindowsSdkVersion.ContainsKey(windowsSdkVersionParsed))
{
knownFrameworkReferencesByWindowsSdkVersion[windowsSdkVersionParsed] = new();
}
knownFrameworkReferencesByWindowsSdkVersion[windowsSdkVersionParsed].Add((normalizedMinimumVersion, CreateKnownFrameworkReferences(windowsSdkPackageVersion, TargetFrameworkVersion, supportedWindowsVersion.ItemSpec)));
}
}
foreach (var knownFrameworkReferencesForSdkVersion in knownFrameworkReferencesByWindowsSdkVersion.Values)
{
// If there are multiple WindowsSdkSupportedTargetPlatformVersion items for the same Windows SDK version, choose the one with the highest minimum version.
// That way it is possible to use older packages when targeting older versions of .NET, and newer packages for newer versions of .NET
var highestMinimumVersion = knownFrameworkReferencesForSdkVersion.Max(t => t.minimumNetVersion);
knownFrameworkReferences.AddRange(knownFrameworkReferencesForSdkVersion.Where(t => t.minimumNetVersion == highestMinimumVersion).Select(t => t.knownFrameworkReferences).SelectMany(l => l));
}
}
KnownFrameworkReferences = knownFrameworkReferences.ToArray();
}
private static TaskItem[] CreateKnownFrameworkReferences(string windowsSdkPackageVersion, string targetFrameworkVersion, string targetPlatformVersion)
{
// Return three items:
// - No profile: with the entire Windows SDK (including Windows.UI.Xaml.* types), only used by downlevel .NET SDKs
// - "Windows": just the Windows SDK, without anything in Windows.UI.Xaml.* .dll
// - "Xaml": just the Windows.UI.Xaml types
//
// Note: we still need to return the item with no profile even if unused, so that the filtering logic for profiles will work correctly.
return
[
CreateKnownFrameworkReference(windowsSdkPackageVersion, targetFrameworkVersion, targetPlatformVersion, profile: null),
CreateKnownFrameworkReference(windowsSdkPackageVersion, targetFrameworkVersion, targetPlatformVersion, profile: "Windows"),
CreateKnownFrameworkReference(windowsSdkPackageVersion, targetFrameworkVersion, targetPlatformVersion, profile: "Xaml"),
];
}
private static TaskItem CreateKnownFrameworkReference(string windowsSdkPackageVersion, string targetFrameworkVersion, string targetPlatformVersion, string profile)
{
string itemSpec = string.IsNullOrEmpty(profile)
? "Microsoft.Windows.SDK.NET.Ref"
: $"Microsoft.Windows.SDK.NET.Ref.{profile}";
var knownFrameworkReference = new TaskItem(itemSpec);
knownFrameworkReference.SetMetadata("TargetFramework", $"net{targetFrameworkVersion}-windows{targetPlatformVersion}");
knownFrameworkReference.SetMetadata("RuntimeFrameworkName", "Microsoft.Windows.SDK.NET.Ref");
knownFrameworkReference.SetMetadata("DefaultRuntimeFrameworkVersion", windowsSdkPackageVersion);
knownFrameworkReference.SetMetadata("LatestRuntimeFrameworkVersion", windowsSdkPackageVersion);
knownFrameworkReference.SetMetadata("TargetingPackName", "Microsoft.Windows.SDK.NET.Ref");
knownFrameworkReference.SetMetadata("TargetingPackVersion", windowsSdkPackageVersion);
knownFrameworkReference.SetMetadata("RuntimePackAlwaysCopyLocal", "true");
knownFrameworkReference.SetMetadata("RuntimePackNamePatterns", "Microsoft.Windows.SDK.NET.Ref");
knownFrameworkReference.SetMetadata("RuntimePackRuntimeIdentifiers", "any");
knownFrameworkReference.SetMetadata("IsWindowsOnly", "true");
if (!string.IsNullOrEmpty(profile))
{
knownFrameworkReference.SetMetadata("Profile", profile);
}
return knownFrameworkReference;
}
}
}