This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
LibraryExporter.cs
508 lines (445 loc) · 21.8 KB
/
LibraryExporter.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.DotNet.ProjectModel.Compilation.Preprocessor;
using Microsoft.DotNet.ProjectModel.Files;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.DotNet.ProjectModel.Resolution;
using Microsoft.DotNet.ProjectModel.Utilities;
using Microsoft.DotNet.Tools.Compiler;
using NuGet.Frameworks;
namespace Microsoft.DotNet.ProjectModel.Compilation
{
public class LibraryExporter
{
private readonly string _configuration;
private readonly string _runtime;
private readonly string[] _runtimeFallbacks;
private readonly ProjectDescription _rootProject;
private readonly string _buildBasePath;
private readonly string _solutionRootPath;
public LibraryExporter(ProjectDescription rootProject,
LibraryManager manager,
string configuration,
string runtime,
string[] runtimeFallbacks,
string buildBasePath,
string solutionRootPath)
{
if (string.IsNullOrEmpty(configuration))
{
throw new ArgumentNullException(nameof(configuration));
}
LibraryManager = manager;
_configuration = configuration;
_runtime = runtime;
_runtimeFallbacks = runtimeFallbacks;
_buildBasePath = buildBasePath;
_solutionRootPath = solutionRootPath;
_rootProject = rootProject;
}
public LibraryManager LibraryManager { get; }
/// <summary>
/// Gets all the exports specified by this project, including the root project itself
/// </summary>
public IEnumerable<LibraryExport> GetAllExports()
{
return ExportLibraries(_ => true);
}
/// <summary>
/// Gets all exports required by the project, NOT including the project itself
/// </summary>
/// <returns></returns>
public IEnumerable<LibraryExport> GetDependencies()
{
return GetDependencies(LibraryType.Unspecified);
}
/// <summary>
/// Gets all exports required by the project, of the specified <see cref="LibraryType"/>, NOT including the project itself
/// </summary>
/// <returns></returns>
public IEnumerable<LibraryExport> GetDependencies(LibraryType type)
{
// Export all but the main project
return ExportLibraries(library =>
library != _rootProject &&
LibraryIsOfType(type, library));
}
/// <summary>
/// Retrieves a list of <see cref="LibraryExport"/> objects representing the assets
/// required from other libraries to compile this project.
/// </summary>
private IEnumerable<LibraryExport> ExportLibraries(Func<LibraryDescription, bool> condition)
{
var seenMetadataReferences = new HashSet<string>();
// Iterate over libraries in the library manager
foreach (var library in LibraryManager.GetLibraries())
{
if (!condition(library))
{
continue;
}
var compilationAssemblies = new List<LibraryAsset>();
var sourceReferences = new List<LibraryAsset>();
var analyzerReferences = new List<AnalyzerReference>();
var libraryExport = GetExport(library);
// We need to filter out source references from non-root libraries,
// so we rebuild the library export
foreach (var reference in libraryExport.CompilationAssemblies)
{
if (seenMetadataReferences.Add(reference.Name))
{
compilationAssemblies.Add(reference);
}
}
// Source and analyzer references are not transitive
if (library.Parents.Contains(_rootProject))
{
sourceReferences.AddRange(libraryExport.SourceReferences);
analyzerReferences.AddRange(libraryExport.AnalyzerReferences);
}
var builder = LibraryExportBuilder.Create(library);
if (_runtime != null && _runtimeFallbacks != null)
{
// For portable apps that are built with runtime trimming we replace RuntimeAssemblyGroups and NativeLibraryGroups
// with single default group that contains asset specific to runtime we are trimming for
// based on runtime fallback list
builder.WithRuntimeAssemblyGroups(TrimAssetGroups(libraryExport.RuntimeAssemblyGroups, _runtimeFallbacks));
builder.WithNativeLibraryGroups(TrimAssetGroups(libraryExport.NativeLibraryGroups, _runtimeFallbacks));
}
else
{
builder.WithRuntimeAssemblyGroups(libraryExport.RuntimeAssemblyGroups);
builder.WithNativeLibraryGroups(libraryExport.NativeLibraryGroups);
}
yield return builder
.WithCompilationAssemblies(compilationAssemblies)
.WithSourceReferences(sourceReferences)
.WithRuntimeAssets(libraryExport.RuntimeAssets)
.WithEmbedddedResources(libraryExport.EmbeddedResources)
.WithAnalyzerReference(analyzerReferences)
.WithResourceAssemblies(libraryExport.ResourceAssemblies)
.Build();
}
}
private IEnumerable<LibraryAssetGroup> TrimAssetGroups(IEnumerable<LibraryAssetGroup> runtimeAssemblyGroups,
string[] runtimeFallbacks)
{
LibraryAssetGroup runtimeAssets;
foreach (var rid in runtimeFallbacks)
{
runtimeAssets = runtimeAssemblyGroups.GetRuntimeGroup(rid);
if (runtimeAssets != null)
{
yield return new LibraryAssetGroup(runtimeAssets.Assets);
yield break;
}
}
runtimeAssets = runtimeAssemblyGroups.GetDefaultGroup();
if (runtimeAssets != null)
{
yield return runtimeAssets;
}
}
/// <summary>
/// Create a LibraryExport from LibraryDescription.
///
/// When the library is not resolved the LibraryExport is created nevertheless.
/// </summary>
private LibraryExport GetExport(LibraryDescription library)
{
if (!library.Resolved)
{
// For a unresolved project reference returns a export with empty asset.
return LibraryExportBuilder.Create(library).Build();
}
var libraryType = library.Identity.Type;
if (Equals(LibraryType.Package, libraryType) || Equals(LibraryType.MSBuildProject, libraryType))
{
return ExportPackage((TargetLibraryWithAssets)library);
}
else if (Equals(LibraryType.Project, libraryType))
{
return ExportProject((ProjectDescription)library);
}
else
{
return ExportFrameworkLibrary(library);
}
}
private LibraryExport ExportPackage(TargetLibraryWithAssets library)
{
var builder = LibraryExportBuilder.Create(library);
builder.AddNativeLibraryGroup(new LibraryAssetGroup(PopulateAssets(library, library.NativeLibraries)));
builder.AddRuntimeAssemblyGroup(new LibraryAssetGroup(PopulateAssets(library, library.RuntimeAssemblies)));
builder.WithResourceAssemblies(PopulateResources(library, library.ResourceAssemblies));
builder.WithCompilationAssemblies(PopulateAssets(library, library.CompileTimeAssemblies));
if (library.Identity.Type.Equals(LibraryType.Package))
{
builder.WithSourceReferences(GetSharedSources((PackageDescription) library));
builder.WithAnalyzerReference(GetAnalyzerReferences((PackageDescription) library));
}
if (library.ContentFiles.Any())
{
var parameters = PPFileParameters.CreateForProject(_rootProject.Project);
Action<Stream, Stream> transform = (input, output) => PPFilePreprocessor.Preprocess(input, output, parameters);
var sourceCodeLanguage = _rootProject.Project.GetSourceCodeLanguage();
var languageGroups = library.ContentFiles.GroupBy(file => file.CodeLanguage);
var selectedGroup = languageGroups.FirstOrDefault(g => g.Key == sourceCodeLanguage) ??
languageGroups.FirstOrDefault(g => g.Key == null);
if (selectedGroup != null)
{
foreach (var contentFile in selectedGroup)
{
if (contentFile.CodeLanguage != null &&
string.Compare(contentFile.CodeLanguage, sourceCodeLanguage, StringComparison.OrdinalIgnoreCase) != 0)
{
continue;
}
var fileTransform = contentFile.PPOutputPath != null ? transform : null;
var fullPath = Path.Combine(library.Path, contentFile.Path);
if (contentFile.BuildAction == BuildAction.Compile)
{
builder.AddSourceReference(LibraryAsset.CreateFromRelativePath(library.Path, contentFile.Path, fileTransform));
}
else if (contentFile.BuildAction == BuildAction.EmbeddedResource)
{
builder.AddEmbedddedResource(LibraryAsset.CreateFromRelativePath(library.Path, contentFile.Path, fileTransform));
}
if (contentFile.CopyToOutput)
{
builder.AddRuntimeAsset(new LibraryAsset(contentFile.Path, contentFile.OutputPath, fullPath, fileTransform));
}
}
}
}
if (library.RuntimeTargets.Any())
{
foreach (var targetGroup in library.RuntimeTargets.GroupBy(t => t.Runtime))
{
var runtime = new List<LibraryAsset>();
var native = new List<LibraryAsset>();
foreach (var lockFileRuntimeTarget in targetGroup)
{
if (string.Equals(lockFileRuntimeTarget.AssetType, "native", StringComparison.OrdinalIgnoreCase))
{
native.Add(LibraryAsset.CreateFromRelativePath(library.Path, lockFileRuntimeTarget.Path));
}
else if (string.Equals(lockFileRuntimeTarget.AssetType, "runtime", StringComparison.OrdinalIgnoreCase))
{
runtime.Add(LibraryAsset.CreateFromRelativePath(library.Path, lockFileRuntimeTarget.Path));
}
}
if (runtime.Any())
{
builder.AddRuntimeAssemblyGroup(new LibraryAssetGroup(targetGroup.Key, runtime.Where(a => !PackageDependencyProvider.IsPlaceholderFile(a.RelativePath))));
}
if (native.Any())
{
builder.AddNativeLibraryGroup(new LibraryAssetGroup(targetGroup.Key, native.Where(a => !PackageDependencyProvider.IsPlaceholderFile(a.RelativePath))));
}
}
}
return builder.Build();
}
private LibraryExport ExportProject(ProjectDescription project)
{
var builder = LibraryExportBuilder.Create(project);
var compilerOptions = project.Project.GetCompilerOptions(project.TargetFrameworkInfo.FrameworkName, _configuration);
if (!string.IsNullOrEmpty(project.TargetFrameworkInfo?.AssemblyPath))
{
// Project specifies a pre-compiled binary. We're done!
var assemblyPath = ResolvePath(project.Project, _configuration, project.TargetFrameworkInfo.AssemblyPath);
var pdbPath = Path.ChangeExtension(assemblyPath, "pdb");
var compileAsset = new LibraryAsset(
project.Project.Name,
Path.GetFileName(assemblyPath),
assemblyPath);
builder.AddCompilationAssembly(compileAsset);
builder.AddRuntimeAssemblyGroup(new LibraryAssetGroup(new[] { compileAsset }));
if (File.Exists(pdbPath))
{
builder.AddRuntimeAsset(new LibraryAsset(Path.GetFileName(pdbPath), Path.GetFileName(pdbPath), pdbPath));
}
}
else if (HasSourceFiles(project, compilerOptions))
{
var outputPaths = project.GetOutputPaths(_buildBasePath, _solutionRootPath, _configuration, _runtime);
var compilationAssembly = outputPaths.CompilationFiles.Assembly;
var compilationAssemblyAsset = LibraryAsset.CreateFromAbsolutePath(
outputPaths.CompilationFiles.BasePath,
compilationAssembly);
builder.AddCompilationAssembly(compilationAssemblyAsset);
if (ExportsRuntime(project))
{
var runtimeAssemblyAsset = LibraryAsset.CreateFromAbsolutePath(
outputPaths.RuntimeFiles.BasePath,
outputPaths.RuntimeFiles.Assembly);
builder.AddRuntimeAssemblyGroup(new LibraryAssetGroup(new[] { runtimeAssemblyAsset }));
builder.WithRuntimeAssets(CollectAssets(outputPaths.RuntimeFiles));
}
else
{
builder.AddRuntimeAssemblyGroup(new LibraryAssetGroup(new[] { compilationAssemblyAsset }));
builder.WithRuntimeAssets(CollectAssets(outputPaths.CompilationFiles));
}
builder.WithResourceAssemblies(outputPaths.CompilationFiles.Resources().Select(r => new LibraryResourceAssembly(
LibraryAsset.CreateFromAbsolutePath(outputPaths.CompilationFiles.BasePath, r.Path),
r.Locale)));
}
builder.WithSourceReferences(project.Project.Files.SharedFiles.Select(f =>
LibraryAsset.CreateFromAbsolutePath(project.Path, f)
));
return builder.Build();
}
private bool HasSourceFiles(ProjectDescription project, CommonCompilerOptions compilerOptions)
{
if (compilerOptions.CompileInclude == null)
{
return project.Project.Files.SourceFiles.Any();
}
var includeFiles = IncludeFilesResolver.GetIncludeFiles(compilerOptions.CompileInclude, "/", diagnostics: null);
return includeFiles.Any();
}
private IEnumerable<LibraryAsset> CollectAssets(CompilationOutputFiles files)
{
var assemblyPath = files.Assembly;
foreach (var path in files.All().Except(files.Resources().Select(r => r.Path)))
{
if (string.Equals(assemblyPath, path))
{
continue;
}
yield return LibraryAsset.CreateFromAbsolutePath(files.BasePath, path);
}
}
private bool ExportsRuntime(ProjectDescription project)
{
return project == _rootProject &&
!string.IsNullOrWhiteSpace(_runtime) &&
project.Project.HasRuntimeOutput(_configuration);
}
private static string ResolvePath(Project project, string configuration, string path)
{
if (string.IsNullOrEmpty(path))
{
return null;
}
path = PathUtility.GetPathWithDirectorySeparator(path);
path = path.Replace("{configuration}", configuration);
return Path.Combine(project.ProjectDirectory, path);
}
private LibraryExport ExportFrameworkLibrary(LibraryDescription library)
{
// We assume the path is to an assembly. Framework libraries only export compile-time stuff
// since they assume the runtime library is present already
var builder = LibraryExportBuilder.Create(library);
if (!string.IsNullOrEmpty(library.Path))
{
builder.WithCompilationAssemblies(new[]
{
new LibraryAsset(library.Identity.Name, null, library.Path)
});
}
return builder.Build();
}
private IEnumerable<LibraryAsset> GetSharedSources(PackageDescription package)
{
return package
.PackageLibrary
.Files
.Where(path => path.StartsWith("shared" + Path.DirectorySeparatorChar))
.Select(path => LibraryAsset.CreateFromRelativePath(package.Path, path));
}
private IEnumerable<AnalyzerReference> GetAnalyzerReferences(PackageDescription package)
{
var analyzers = package
.PackageLibrary
.Files
.Where(path => path.StartsWith("analyzers" + Path.DirectorySeparatorChar) &&
path.EndsWith(".dll"));
var analyzerRefs = new List<AnalyzerReference>();
// See https://docs.nuget.org/create/analyzers-conventions for the analyzer
// NuGet specification
foreach (var analyzer in analyzers)
{
var specifiers = analyzer.Split(Path.DirectorySeparatorChar);
var assemblyPath = Path.Combine(package.Path, analyzer);
// $/analyzers/{Framework Name}{Version}/{Supported Architecture}/{Supported Programming Language}/{Analyzer}.dll
switch (specifiers.Length)
{
// $/analyzers/{analyzer}.dll
case 2:
analyzerRefs.Add(new AnalyzerReference(
assembly: assemblyPath,
framework: null,
language: null,
runtimeIdentifier: null
));
break;
// $/analyzers/{framework}/{analyzer}.dll
case 3:
analyzerRefs.Add(new AnalyzerReference(
assembly: assemblyPath,
framework: NuGetFramework.Parse(specifiers[1]),
language: null,
runtimeIdentifier: null
));
break;
// $/analyzers/{framework}/{language}/{analyzer}.dll
case 4:
analyzerRefs.Add(new AnalyzerReference(
assembly: assemblyPath,
framework: NuGetFramework.Parse(specifiers[1]),
language: specifiers[2],
runtimeIdentifier: null
));
break;
// $/analyzers/{framework}/{runtime}/{language}/{analyzer}.dll
case 5:
analyzerRefs.Add(new AnalyzerReference(
assembly: assemblyPath,
framework: NuGetFramework.Parse(specifiers[1]),
language: specifiers[3],
runtimeIdentifier: specifiers[2]
));
break;
// Anything less than 2 specifiers or more than 4 is
// illegal according to the specification and will be
// ignored
}
}
return analyzerRefs;
}
private IEnumerable<LibraryResourceAssembly> PopulateResources(TargetLibraryWithAssets library, IEnumerable<LockFileItem> section)
{
foreach (var assemblyPath in section.Where(a => !PackageDependencyProvider.IsPlaceholderFile(a.Path)))
{
string locale;
if(!assemblyPath.Properties.TryGetValue(Constants.LocaleLockFilePropertyName, out locale))
{
locale = null;
}
yield return new LibraryResourceAssembly(
LibraryAsset.CreateFromRelativePath(library.Path, assemblyPath.Path),
locale);
}
}
private IEnumerable<LibraryAsset> PopulateAssets(TargetLibraryWithAssets library, IEnumerable<LockFileItem> section)
{
foreach (var assemblyPath in section.Where(a => !PackageDependencyProvider.IsPlaceholderFile(a.Path)))
{
yield return LibraryAsset.CreateFromRelativePath(library.Path, assemblyPath.Path);
}
}
private static bool LibraryIsOfType(LibraryType type, LibraryDescription library)
{
return type.Equals(LibraryType.Unspecified) || // No type filter was requested
library.Identity.Type.Equals(type); // OR, library type matches requested type
}
}
}