forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolveAppHosts.cs
316 lines (264 loc) · 12.6 KB
/
ResolveAppHosts.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Frameworks;
namespace Microsoft.NET.Build.Tasks
{
public class ResolveAppHosts : TaskBase
{
public string TargetFrameworkIdentifier { get; set; }
public string TargetFrameworkVersion { get; set; }
public string TargetingPackRoot { get; set; }
public string AppHostRuntimeIdentifier { get; set; }
public string[] OtherRuntimeIdentifiers { get; set; }
public string RuntimeFrameworkVersion { get; set; }
public ITaskItem[] PackAsToolShimRuntimeIdentifiers { get; set; } = Array.Empty<ITaskItem>();
/// <summary>
/// The file name of Apphost asset.
/// </summary>
[Required]
public string DotNetAppHostExecutableNameWithoutExtension { get; set; }
[Required]
public string DotNetSingleFileHostExecutableNameWithoutExtension { get; set; }
/// <summary>
/// The file name of comhost asset.
/// </summary>
[Required]
public string DotNetComHostLibraryNameWithoutExtension { get; set; }
/// <summary>
/// The file name of ijwhost asset.
/// </summary>
[Required]
public string DotNetIjwHostLibraryNameWithoutExtension { get; set; }
[Required]
public string RuntimeGraphPath { get; set; }
public ITaskItem[] KnownAppHostPacks { get; set; }
public bool NuGetRestoreSupported { get; set; } = true;
public string NetCoreTargetingPackRoot { get; set; }
[Output]
public ITaskItem[] PackagesToDownload { get; set; }
// There should only be one AppHost item, but we use an item here so we can attach metadata to it
// (ie the apphost pack name and version, and the relative path to the apphost inside of it so
// we can resolve the full path later)
[Output]
public ITaskItem[] AppHost { get; set; }
[Output]
public ITaskItem[] SingleFileHost { get; set; }
[Output]
public ITaskItem[] ComHost { get; set; }
[Output]
public ITaskItem[] IjwHost { get; set; }
[Output]
public ITaskItem[] PackAsToolShimAppHostPacks { get; set; }
protected override void ExecuteCore()
{
var normalizedTargetFrameworkVersion = ProcessFrameworkReferences.NormalizeVersion(new Version(TargetFrameworkVersion));
var knownAppHostPacksForTargetFramework = KnownAppHostPacks
.Where(appHostPack =>
{
var packTargetFramework = NuGetFramework.Parse(appHostPack.GetMetadata("TargetFramework"));
return packTargetFramework.Framework.Equals(TargetFrameworkIdentifier, StringComparison.OrdinalIgnoreCase) &&
ProcessFrameworkReferences.NormalizeVersion(packTargetFramework.Version) == normalizedTargetFrameworkVersion;
})
.ToList();
if (knownAppHostPacksForTargetFramework.Count > 1)
{
throw new InvalidOperationException("Multiple KnownAppHostPack items applied to the specified target framework, which is not supposed to happen");
}
if (knownAppHostPacksForTargetFramework.Count == 0)
{
return;
}
var packagesToDownload = new List<ITaskItem>();
if (!string.IsNullOrEmpty(AppHostRuntimeIdentifier))
{
var appHostItem = GetHostItem(
AppHostRuntimeIdentifier,
knownAppHostPacksForTargetFramework,
packagesToDownload,
DotNetAppHostExecutableNameWithoutExtension,
"AppHost",
isExecutable: true,
errorIfNotFound: true);
if (appHostItem != null)
{
AppHost = new ITaskItem[] { appHostItem };
}
var singlefileHostItem = GetHostItem(
AppHostRuntimeIdentifier,
knownAppHostPacksForTargetFramework,
packagesToDownload,
DotNetSingleFileHostExecutableNameWithoutExtension,
"SingleFileHost",
isExecutable: true,
errorIfNotFound: true);
if (singlefileHostItem != null)
{
SingleFileHost = new ITaskItem[] { singlefileHostItem };
}
var comHostItem = GetHostItem(
AppHostRuntimeIdentifier,
knownAppHostPacksForTargetFramework,
packagesToDownload,
DotNetComHostLibraryNameWithoutExtension,
"ComHost",
isExecutable: false,
errorIfNotFound: true);
if (comHostItem != null)
{
ComHost = new ITaskItem[] { comHostItem };
}
var ijwHostItem = GetHostItem(
AppHostRuntimeIdentifier,
knownAppHostPacksForTargetFramework,
packagesToDownload,
DotNetIjwHostLibraryNameWithoutExtension,
"IjwHost",
isExecutable: false,
errorIfNotFound: true);
if (ijwHostItem != null)
{
IjwHost = new ITaskItem[] { ijwHostItem };
}
}
if (PackAsToolShimRuntimeIdentifiers.Length > 0)
{
var packAsToolShimAppHostPacks = new List<ITaskItem>();
foreach (var runtimeIdentifier in PackAsToolShimRuntimeIdentifiers)
{
var appHostItem = GetHostItem(
runtimeIdentifier.ItemSpec,
knownAppHostPacksForTargetFramework,
packagesToDownload,
DotNetAppHostExecutableNameWithoutExtension,
"AppHost",
isExecutable: true,
errorIfNotFound: true);
if (appHostItem != null)
{
packAsToolShimAppHostPacks.Add(appHostItem);
}
}
PackAsToolShimAppHostPacks = packAsToolShimAppHostPacks.ToArray();
}
if (OtherRuntimeIdentifiers != null)
{
foreach (var otherRuntimeIdentifier in OtherRuntimeIdentifiers)
{
// Download any apphost packages for other runtime identifiers.
// This allows you to specify the list of RIDs in RuntimeIdentifiers and only restore once,
// and then build for each RuntimeIdentifier without restoring separately.
// We discard the return value, and pass in some bogus data that won't be used, because
// we won't use the assets from the apphost pack in this build.
GetHostItem(otherRuntimeIdentifier,
knownAppHostPacksForTargetFramework,
packagesToDownload,
hostNameWithoutExtension: "unused",
itemName: "unused",
isExecutable: true,
errorIfNotFound: false);
}
}
if (packagesToDownload.Any())
{
PackagesToDownload = packagesToDownload.ToArray();
}
}
private ITaskItem GetHostItem(string runtimeIdentifier,
List<ITaskItem> knownAppHostPacksForTargetFramework,
List<ITaskItem> packagesToDownload,
string hostNameWithoutExtension,
string itemName,
bool isExecutable,
bool errorIfNotFound)
{
var selectedAppHostPack = knownAppHostPacksForTargetFramework.Single();
string appHostRuntimeIdentifiers = selectedAppHostPack.GetMetadata("AppHostRuntimeIdentifiers");
string appHostPackPattern = selectedAppHostPack.GetMetadata("AppHostPackNamePattern");
string appHostPackVersion = selectedAppHostPack.GetMetadata("AppHostPackVersion");
if (!string.IsNullOrEmpty(RuntimeFrameworkVersion))
{
appHostPackVersion = RuntimeFrameworkVersion;
}
string bestAppHostRuntimeIdentifier = NuGetUtils.GetBestMatchingRid(
new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath),
runtimeIdentifier,
appHostRuntimeIdentifiers.Split(';'),
out bool wasInGraph);
if (bestAppHostRuntimeIdentifier == null)
{
if (wasInGraph)
{
// NETSDK1084: There was no app host for available for the specified RuntimeIdentifier '{0}'.
if (errorIfNotFound)
{
Log.LogError(Strings.NoAppHostAvailable, runtimeIdentifier);
}
else
{
Log.LogMessage(Strings.NoAppHostAvailable, runtimeIdentifier);
}
}
else
{
// NETSDK1083: The specified RuntimeIdentifier '{0}' is not recognized.
if (errorIfNotFound)
{
Log.LogError(Strings.RuntimeIdentifierNotRecognized, runtimeIdentifier);
}
else
{
Log.LogMessage(Strings.RuntimeIdentifierNotRecognized, runtimeIdentifier);
}
}
return null;
}
else
{
string hostPackName = appHostPackPattern.Replace("**RID**", bestAppHostRuntimeIdentifier);
string hostRelativePathInPackage = Path.Combine("runtimes", bestAppHostRuntimeIdentifier, "native",
hostNameWithoutExtension + (isExecutable ? ExecutableExtension.ForRuntimeIdentifier(bestAppHostRuntimeIdentifier) : ".dll"));
TaskItem appHostItem = new TaskItem(itemName);
string appHostPackPath = null;
if (!string.IsNullOrEmpty(TargetingPackRoot))
{
appHostPackPath = Path.Combine(TargetingPackRoot, hostPackName, appHostPackVersion);
}
if (appHostPackPath != null && Directory.Exists(appHostPackPath))
{
// Use AppHost from packs folder
appHostItem.SetMetadata(MetadataKeys.PackageDirectory, appHostPackPath);
appHostItem.SetMetadata(MetadataKeys.Path, Path.Combine(appHostPackPath, hostRelativePathInPackage));
}
else
{
// C++/CLI does not support package download && dedup error
if (!NuGetRestoreSupported && !packagesToDownload.Any(p => p.ItemSpec == hostPackName))
{
Log.LogError(
Strings.TargetingApphostPackMissingCannotRestore,
"Apphost",
$"{NetCoreTargetingPackRoot}\\{hostPackName}",
selectedAppHostPack.GetMetadata("TargetFramework") ?? "",
hostPackName,
appHostPackVersion
);
}
// Download apphost pack
TaskItem packageToDownload = new TaskItem(hostPackName);
packageToDownload.SetMetadata(MetadataKeys.Version, appHostPackVersion);
packagesToDownload.Add(packageToDownload);
appHostItem.SetMetadata(MetadataKeys.NuGetPackageId, hostPackName);
appHostItem.SetMetadata(MetadataKeys.NuGetPackageVersion, appHostPackVersion);
}
appHostItem.SetMetadata(MetadataKeys.PathInPackage, hostRelativePathInPackage);
appHostItem.SetMetadata(MetadataKeys.RuntimeIdentifier, runtimeIdentifier);
return appHostItem;
}
}
}
}