@@ -304,7 +304,7 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
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
}
@@ -319,6 +319,46 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
319
319
* libraryCandidatesForRemoval if it isn't already there
320
320
* Repeat 3 until libraryCandidatesForRemoval is empty
321
321
*/
322
+ var references = runtimeLibraries . ToDictionary ( lib => lib . Library . Name , lib => lib ) ;
323
+ foreach ( var reference in runtimeLibraries )
324
+ {
325
+ foreach ( var dependency in reference . Library . Dependencies )
326
+ {
327
+ references [ dependency . Name ] . Dependents . Add ( reference . Library . Name ) ;
328
+ }
329
+ }
330
+
331
+ var unprocessedReferences = runtimeLibraries . ToHashSet ( ) ;
332
+ HashSet < ModifiableRuntimeLibrary > temp = new ( ) ;
333
+ while ( unprocessedReferences . Any ( ) )
334
+ {
335
+ var lib = unprocessedReferences . First ( ) ;
336
+ unprocessedReferences . Remove ( lib ) ;
337
+
338
+ if ( lib . Library . RuntimeAssemblyGroups . Count == 0 && lib . Library . NativeLibraryGroups . Count == 0 && lib . Library . ResourceAssemblies . Count == 0 )
339
+ {
340
+ if ( lib . Library . Dependencies . All ( d => ! references . ContainsKey ( d . Name ) || references [ d . Name ] . Dependents . Count > 1 ) )
341
+ {
342
+ runtimeLibraries . Remove ( lib ) ;
343
+ references . Remove ( lib . Library . Name ) ;
344
+ foreach ( var dependency in lib . Library . Dependencies )
345
+ {
346
+ references [ dependency . Name ] . Dependents . Remove ( lib . Library . Name ) ;
347
+ }
348
+
349
+ foreach ( var dependent in lib . Dependents )
350
+ {
351
+ temp . Add ( references [ dependent ] ) ;
352
+ }
353
+ }
354
+ }
355
+
356
+ if ( ! unprocessedReferences . Any ( ) )
357
+ {
358
+ unprocessedReferences = temp ;
359
+ temp = new ( ) ;
360
+ }
361
+ }
322
362
323
363
List < CompilationLibrary > compilationLibraries = new ( ) ;
324
364
if ( IncludeCompilationLibraries )
@@ -447,7 +487,7 @@ private ModifiableRuntimeLibrary GetProjectRuntimeLibrary()
447
487
path : null ,
448
488
hashPath : null ,
449
489
runtimeStoreManifestName : GetRuntimeStoreManifestName ( _mainProjectInfo . Name , _mainProjectInfo . Version ) ,
450
- serviceable : false ) , dependencies . ToHashSet ( ) ) ;
490
+ serviceable : false ) ) ;
451
491
}
452
492
453
493
private List < Dependency > GetProjectDependencies ( )
@@ -519,7 +559,7 @@ private IEnumerable<ModifiableRuntimeLibrary> GetRuntimePackLibraries()
519
559
nativeLibraryGroups : [ nativeLibraryGroup ] ,
520
560
resourceAssemblies : [ ] ,
521
561
dependencies : [ ] ,
522
- serviceable : false ) , [ ] ) ;
562
+ serviceable : false ) ) ;
523
563
} ) ;
524
564
}
525
565
@@ -611,7 +651,7 @@ private ModifiableRuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, st
611
651
path : path ,
612
652
hashPath : hashPath ,
613
653
runtimeStoreManifestName : GetRuntimeStoreManifestName ( library . Name , library . Version . ToString ( ) ) ,
614
- serviceable : serviceable ) , libraryDependencies ) ;
654
+ serviceable : serviceable ) ) ;
615
655
616
656
return runtimeLibrary ;
617
657
}
@@ -937,12 +977,12 @@ private struct LibraryDependency
937
977
private class ModifiableRuntimeLibrary
938
978
{
939
979
public RuntimeLibrary Library { get ; set ; }
940
- public HashSet < Dependency > Dependencies { get ; set ; }
980
+ public HashSet < string > Dependents { get ; set ; }
941
981
942
- public ModifiableRuntimeLibrary ( RuntimeLibrary library , HashSet < Dependency > dependencies )
982
+ public ModifiableRuntimeLibrary ( RuntimeLibrary library )
943
983
{
944
984
this . Library = library ;
945
- this . Dependencies = dependencies ;
985
+ this . Dependents = new ( ) ;
946
986
}
947
987
}
948
988
}
0 commit comments