@@ -265,7 +265,7 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
265
265
{
266
266
CalculateExcludedLibraries ( ) ;
267
267
268
- List < RuntimeLibrary > runtimeLibraries = new ( ) ;
268
+ List < ModifiableRuntimeLibrary > runtimeLibraries = new ( ) ;
269
269
270
270
if ( _includeMainProjectInDepsFile )
271
271
{
@@ -292,23 +292,34 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
292
292
293
293
foreach ( var directReference in directAndDependencyReferences )
294
294
{
295
- var runtimeLibrary = new RuntimeLibrary (
295
+ var runtimeLibrary = new ModifiableRuntimeLibrary ( new RuntimeLibrary (
296
296
type : "reference" ,
297
297
name : GetReferenceLibraryName ( directReference ) ,
298
298
version : directReference . Version ,
299
299
hash : string . Empty ,
300
- runtimeAssemblyGroups : new [ ] { new RuntimeAssetGroup ( string . Empty , new [ ] { CreateRuntimeFile ( directReference . FileName , directReference . FullPath ) } ) } ,
301
- nativeLibraryGroups : new RuntimeAssetGroup [ ] { } ,
300
+ runtimeAssemblyGroups : [ new RuntimeAssetGroup ( string . Empty , [ CreateRuntimeFile ( directReference . FileName , directReference . FullPath ) ] ) ] ,
301
+ nativeLibraryGroups : [ ] ,
302
302
resourceAssemblies : CreateResourceAssemblies ( directReference . ResourceAssemblies ) ,
303
- dependencies : Enumerable . Empty < Dependency > ( ) ,
303
+ dependencies : [ ] ,
304
304
path : null ,
305
305
hashPath : null ,
306
306
runtimeStoreManifestName : null ,
307
- serviceable : false ) ;
307
+ serviceable : false ) , [ ] ) ;
308
308
309
309
runtimeLibraries . Add ( runtimeLibrary ) ;
310
310
}
311
311
312
+ /*
313
+ * We now need to modify runtimeLibraries to eliminate those that don't have any runtime assets. We follow the following steps:
314
+ * 0. Construct a reverse dependencies list: all runtimeLibraries that depend on this one
315
+ * 1. If runtimeAssemblyGroups, nativeLibraryGroups, dependencies, and resourceAssemblies are all empty, remove this runtimeLibrary as well as any dependencies on it.
316
+ * 2. Add all runtimeLibraries to a list of to-be-processed libraries called libraryCandidatesForRemoval
317
+ * 3. libraryCandidatesForRemoval.Pop() --> if there are no runtimeAssemblyGroups, nativeLibraryGroups, or resourceAssemblies, and either dependencies is empty or all
318
+ * dependencies have something else that depends on them, remove it (and from libraryCandidatesForRemoval), adding everything that depends on this to
319
+ * libraryCandidatesForRemoval if it isn't already there
320
+ * Repeat 3 until libraryCandidatesForRemoval is empty
321
+ */
322
+
312
323
List < CompilationLibrary > compilationLibraries = new ( ) ;
313
324
if ( IncludeCompilationLibraries )
314
325
{
@@ -393,7 +404,6 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
393
404
//
394
405
// Otherwise, it is the set of all runtimes compatible with (inheriting)
395
406
// the target runtime-identifier.
396
-
397
407
var runtimeFallbackGraph =
398
408
( _runtimeGraph == null || _runtimeIdentifier == null ) ?
399
409
new RuntimeFallbacks [ ] { } :
@@ -406,11 +416,11 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
406
416
targetInfo ,
407
417
_compilationOptions ?? CompilationOptions . Default ,
408
418
compilationLibraries ,
409
- runtimeLibraries ,
419
+ runtimeLibraries . Select ( library => library . Library ) ,
410
420
runtimeFallbackGraph ) ;
411
421
}
412
422
413
- private RuntimeLibrary GetProjectRuntimeLibrary ( )
423
+ private ModifiableRuntimeLibrary GetProjectRuntimeLibrary ( )
414
424
{
415
425
RuntimeAssetGroup [ ] runtimeAssemblyGroups = new [ ] { new RuntimeAssetGroup ( string . Empty , _mainProjectInfo . OutputName ) } ;
416
426
@@ -425,7 +435,7 @@ private RuntimeLibrary GetProjectRuntimeLibrary()
425
435
}
426
436
}
427
437
428
- return new RuntimeLibrary (
438
+ return new ModifiableRuntimeLibrary ( new RuntimeLibrary (
429
439
type : "project" ,
430
440
name : _mainProjectInfo . Name ,
431
441
version : _mainProjectInfo . Version ,
@@ -437,7 +447,7 @@ private RuntimeLibrary GetProjectRuntimeLibrary()
437
447
path : null ,
438
448
hashPath : null ,
439
449
runtimeStoreManifestName : GetRuntimeStoreManifestName ( _mainProjectInfo . Name , _mainProjectInfo . Version ) ,
440
- serviceable : false ) ;
450
+ serviceable : false ) , dependencies . ToHashSet ( ) ) ;
441
451
}
442
452
443
453
private List < Dependency > GetProjectDependencies ( )
@@ -484,11 +494,11 @@ private List<Dependency> GetProjectDependencies()
484
494
return dependencies ;
485
495
}
486
496
487
- private IEnumerable < RuntimeLibrary > GetRuntimePackLibraries ( )
497
+ private IEnumerable < ModifiableRuntimeLibrary > GetRuntimePackLibraries ( )
488
498
{
489
499
if ( _runtimePackAssets == null )
490
500
{
491
- return Enumerable . Empty < RuntimeLibrary > ( ) ;
501
+ return [ ] ;
492
502
}
493
503
return _runtimePackAssets . Select ( runtimePack =>
494
504
{
@@ -500,20 +510,20 @@ private IEnumerable<RuntimeLibrary> GetRuntimePackLibraries()
500
510
runtimePack . Value . Where ( asset => asset . AssetType == AssetType . Native )
501
511
. Select ( asset => CreateRuntimeFile ( asset . DestinationSubPath , asset . SourcePath ) ) ) ;
502
512
503
- return new RuntimeLibrary (
513
+ return new ModifiableRuntimeLibrary ( new RuntimeLibrary (
504
514
type : "runtimepack" ,
505
515
name : runtimePack . Key ,
506
516
version : runtimePack . Value . First ( ) . PackageVersion ,
507
517
hash : string . Empty ,
508
- runtimeAssemblyGroups : new [ ] { runtimeAssemblyGroup } ,
509
- nativeLibraryGroups : new [ ] { nativeLibraryGroup } ,
510
- resourceAssemblies : Enumerable . Empty < ResourceAssembly > ( ) ,
511
- dependencies : Enumerable . Empty < Dependency > ( ) ,
512
- serviceable : false ) ;
518
+ runtimeAssemblyGroups : [ runtimeAssemblyGroup ] ,
519
+ nativeLibraryGroups : [ nativeLibraryGroup ] ,
520
+ resourceAssemblies : [ ] ,
521
+ dependencies : [ ] ,
522
+ serviceable : false ) , [ ] ) ;
513
523
} ) ;
514
524
}
515
525
516
- private RuntimeLibrary GetRuntimeLibrary ( DependencyLibrary library , string [ ] userRuntimeAssemblies )
526
+ private ModifiableRuntimeLibrary GetRuntimeLibrary ( DependencyLibrary library , string [ ] userRuntimeAssemblies )
517
527
{
518
528
GetCommonLibraryProperties ( library ,
519
529
out string hash ,
@@ -589,7 +599,7 @@ private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] use
589
599
590
600
}
591
601
592
- var runtimeLibrary = new RuntimeLibrary (
602
+ var runtimeLibrary = new ModifiableRuntimeLibrary ( new RuntimeLibrary (
593
603
type : library . Type ,
594
604
name : library . Name ,
595
605
version : library . Version . ToString ( ) ,
@@ -601,7 +611,7 @@ private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] use
601
611
path : path ,
602
612
hashPath : hashPath ,
603
613
runtimeStoreManifestName : GetRuntimeStoreManifestName ( library . Name , library . Version . ToString ( ) ) ,
604
- serviceable : serviceable ) ;
614
+ serviceable : serviceable ) , libraryDependencies ) ;
605
615
606
616
return runtimeLibrary ;
607
617
}
@@ -923,5 +933,17 @@ private struct LibraryDependency
923
933
public string Name { get ; set ; }
924
934
public NuGetVersion MinVersion { get ; set ; }
925
935
}
936
+
937
+ private class ModifiableRuntimeLibrary
938
+ {
939
+ public RuntimeLibrary Library { get ; set ; }
940
+ public HashSet < Dependency > Dependencies { get ; set ; }
941
+
942
+ public ModifiableRuntimeLibrary ( RuntimeLibrary library , HashSet < Dependency > dependencies )
943
+ {
944
+ this . Library = library ;
945
+ this . Dependencies = dependencies ;
946
+ }
947
+ }
926
948
}
927
949
}
0 commit comments