Skip to content

Commit

Permalink
Improve multi-assembly support (dotnet#69)
Browse files Browse the repository at this point in the history
- Add handling of field references
- Implement multi-assembly support for the command line iltrim
- Small change based on PR feedback
- Improve the tests
  • Loading branch information
vitek-karas authored Oct 14, 2021
1 parent 4456188 commit cbc5eb7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/coreclr/tools/ILTrim/ILTrim.Exe/ILTrim.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>exe</OutputType>
<TargetFramework>$(NetCoreAppToolCurrent)</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Platforms>x64;x86</Platforms>
Expand Down
19 changes: 14 additions & 5 deletions src/coreclr/tools/ILTrim/ILTrim.Exe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ public class Program
static void Main(string[] args)
{
var inputPath = args[0];
using var output = File.Create("out.exe");
int i = 1;
List<string> referencePaths = new();
while (args.Length > i && args[i] == "-r") {
referencePaths.Add (args[i+1]);
i += 2;
List<string> trimPaths = new();
while (args.Length > i) {
if (args[i] == "-r")
{
referencePaths.Add(args[i + 1]);
i += 2;
}
else if (args[i] == "-t")
{
trimPaths.Add(args[i + 1]);
i += 2;
}
}
Trimmer.TrimAssembly(inputPath, output, referencePaths);

Trimmer.TrimAssembly(inputPath, trimPaths, Directory.GetCurrentDirectory(), referencePaths);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public static void Kept()
public static void Trimmed()
{
}
}

public static int KeptField;
public static int TrimmedField;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.MultiAssembly
{
[SetupLinkerAction ("link", "Dep")]
[SetupCompileBefore("Dep.dll", new[] { "Dependencies/Dep.cs" })]

[KeptMemberInAssembly("Dep.dll", typeof(DepClass), "Kept()")]
[KeptMemberInAssembly("Dep.dll", typeof(DepClass), nameof(DepClass.KeptField))]
public class MultiAssembly
{
public static void Main()
{
DepClass.Kept();
DepClass.KeptField = 0;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace ILTrim.DependencyAnalysis
/// </summary>
public sealed class NodeFactory
{
public IReadOnlySet<string> TrimAssemblies { get; }
IReadOnlySet<string> _trimAssemblies { get; }

public NodeFactory(IEnumerable<string> trimAssemblies)
{
TrimAssemblies = new HashSet<string>(trimAssemblies);
_trimAssemblies = new HashSet<string>(trimAssemblies);
}

/// <summary>
Expand Down Expand Up @@ -232,6 +232,11 @@ public GenericParameterConstraintNode GenericParameterConstraint(EcmaModule modu
return _genericParameterConstraints.GetOrAdd(new HandleKey<GenericParameterConstraintHandle>(module, handle));
}

public bool IsModuleTrimmed(EcmaModule module)
{
return _trimAssemblies.Contains(module.Assembly.GetName().Name);
}

private struct HandleKey<T> : IEquatable<HandleKey<T>> where T : struct, IEquatable<T>
{
public readonly EcmaModule Module;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ public override IEnumerable<DependencyListEntry> GetStaticDependencies(NodeFacto

DependencyList dependencies = new DependencyList();

if (methodOrFieldDef is EcmaMethod method)
switch (methodOrFieldDef)
{
if (factory.TrimAssemblies.Contains(method.Module.Assembly.GetName().Name))
{
dependencies.Add(factory.GetNodeForToken(method.Module, method.Handle), "Target method def of member reference");
}
case EcmaMethod method:
if (factory.IsModuleTrimmed(method.Module))
{
dependencies.Add(factory.GetNodeForToken(method.Module, method.Handle), "Target method def of member reference");
}
break;

case EcmaField field:
if (factory.IsModuleTrimmed(field.Module))
{
dependencies.Add(factory.GetNodeForToken(field.Module, field.Handle), "Target field def of member reference");
}
break;
}

if (!memberRef.Parent.IsNil)
Expand Down

0 comments on commit cbc5eb7

Please sign in to comment.