Skip to content

Commit 6e201bf

Browse files
committed
Turn comment into code
1 parent fa1bd9d commit 6e201bf

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
304304
path: null,
305305
hashPath: null,
306306
runtimeStoreManifestName: null,
307-
serviceable: false), []);
307+
serviceable: false));
308308

309309
runtimeLibraries.Add(runtimeLibrary);
310310
}
@@ -319,6 +319,46 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
319319
* libraryCandidatesForRemoval if it isn't already there
320320
* Repeat 3 until libraryCandidatesForRemoval is empty
321321
*/
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+
}
322362

323363
List<CompilationLibrary> compilationLibraries = new();
324364
if (IncludeCompilationLibraries)
@@ -447,7 +487,7 @@ private ModifiableRuntimeLibrary GetProjectRuntimeLibrary()
447487
path: null,
448488
hashPath: null,
449489
runtimeStoreManifestName: GetRuntimeStoreManifestName(_mainProjectInfo.Name, _mainProjectInfo.Version),
450-
serviceable: false), dependencies.ToHashSet());
490+
serviceable: false));
451491
}
452492

453493
private List<Dependency> GetProjectDependencies()
@@ -519,7 +559,7 @@ private IEnumerable<ModifiableRuntimeLibrary> GetRuntimePackLibraries()
519559
nativeLibraryGroups: [nativeLibraryGroup],
520560
resourceAssemblies: [],
521561
dependencies: [],
522-
serviceable: false), []);
562+
serviceable: false));
523563
});
524564
}
525565

@@ -611,7 +651,7 @@ private ModifiableRuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, st
611651
path: path,
612652
hashPath: hashPath,
613653
runtimeStoreManifestName: GetRuntimeStoreManifestName(library.Name, library.Version.ToString()),
614-
serviceable: serviceable), libraryDependencies);
654+
serviceable: serviceable));
615655

616656
return runtimeLibrary;
617657
}
@@ -937,12 +977,12 @@ private struct LibraryDependency
937977
private class ModifiableRuntimeLibrary
938978
{
939979
public RuntimeLibrary Library { get; set; }
940-
public HashSet<Dependency> Dependencies { get; set; }
980+
public HashSet<string> Dependents { get; set; }
941981

942-
public ModifiableRuntimeLibrary(RuntimeLibrary library, HashSet<Dependency> dependencies)
982+
public ModifiableRuntimeLibrary(RuntimeLibrary library)
943983
{
944984
this.Library = library;
945-
this.Dependencies = dependencies;
985+
this.Dependents = new();
946986
}
947987
}
948988
}

0 commit comments

Comments
 (0)