Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom steps to share state #2019

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/linker/Linker/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Xml.XPath;
using Mono.Cecil;
Expand Down Expand Up @@ -794,8 +795,11 @@ Assembly GetCustomAssembly (string arg)
{
if (Path.IsPathRooted (arg)) {
var assemblyPath = Path.GetFullPath (arg);
if (File.Exists (assemblyPath))
return Assembly.Load (File.ReadAllBytes (assemblyPath));
if (File.Exists (assemblyPath)) {
// The CLR will return the already-loaded assembly if the same path is requested multiple times
// (or even if a different path specifies the "same" assembly, based on the MVID).
return AssemblyLoadContext.Default.LoadFromAssemblyPath (assemblyPath);
}
context.LogError ($"The assembly '{arg}' specified for '--custom-step' option could not be found", 1022);
} else
context.LogError ($"The path to the assembly '{arg}' specified for '--custom-step' must be fully qualified", 1023);
Expand Down
8 changes: 4 additions & 4 deletions test/Mono.Linker.Tests.Cases/CommandLine/AddCustomStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Mono.Linker.Tests.Cases.CommandLine
#if !NETCOREAPP
[IgnoreTestCase ("Can be enabled once MonoBuild produces a dll from which we can grab the types in the Mono.Linker namespace.")]
#else
[SetupCompileBefore ("CustomStep.dll", new[] { "Dependencies/CustomStepDummy.cs" }, new[] { "illink.dll" })]
[SetupCompileBefore ("CustomStepDummy.dll", new[] { "Dependencies/CustomStepDummy.cs" }, new[] { "illink.dll" })]
#endif
[SetupLinkerArgument ("--custom-step", "CustomStep.CustomStepDummy,CustomStep.dll")]
[SetupLinkerArgument ("--custom-step", "-CleanStep:CustomStep.CustomStepDummy,CustomStep.dll")]
[SetupLinkerArgument ("--custom-step", "+CleanStep:CustomStep.CustomStepDummy,CustomStep.dll")]
[SetupLinkerArgument ("--custom-step", "CustomStep.CustomStepDummy,CustomStepDummy.dll")]
[SetupLinkerArgument ("--custom-step", "-CleanStep:CustomStep.CustomStepDummy,CustomStepDummy.dll")]
[SetupLinkerArgument ("--custom-step", "+CleanStep:CustomStep.CustomStepDummy,CustomStepDummy.dll")]
[SetupLinkerArgument ("--verbose")]
[LogContains ("Custom step added")]
public class AddCustomStep
Expand Down
4 changes: 2 additions & 2 deletions test/Mono.Linker.Tests.Cases/CommandLine/CustomStepData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Mono.Linker.Tests.Cases.CommandLine
#if !NETCOREAPP
[IgnoreTestCase ("Can be enabled once MonoBuild produces a dll from which we can grab the types in the Mono.Linker namespace.")]
#else
[SetupCompileBefore ("CustomStep.dll", new[] { "Dependencies/CustomStepUser.cs" }, new[] { "illink.dll" })]
[SetupCompileBefore ("CustomStepUser.dll", new[] { "Dependencies/CustomStepUser.cs" }, new[] { "illink.dll" })]
#endif
[SetupLinkerArgument ("--custom-step", "CustomStep.CustomStepUser,CustomStep.dll")]
[SetupLinkerArgument ("--custom-step", "CustomStep.CustomStepUser,CustomStepUser.dll")]
[SetupLinkerArgument ("--custom-data", "NewKey=UserValue")]
[SetupLinkerArgument ("--verbose")]
[LogContains ("Custom step added with custom data of UserValue")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mono.Linker.Tests.Cases.Extensibility
#if !NETCOREAPP
[IgnoreTestCase ("Specific to the illink build")]
#endif
[SetupCompileBefore ("customstep.dll", new[] { "Dependencies/PreserveMethodsSubStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "+MarkStep:PreserveMethodsSubStep,customstep.dll")]
[SetupCompileBefore ("CustomStepAfterMark.dll", new[] { "Dependencies/PreserveMethodsSubStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "+MarkStep:PreserveMethodsSubStep,CustomStepAfterMark.dll")]
public class CustomStepCanPreserveMethodsAfterMark
{
public static void Main ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mono.Linker.Tests.Cases.Extensibility
#if !NETCOREAPP
[IgnoreTestCase ("Specific to the illink build")]
#endif
[SetupCompileBefore ("customstep.dll", new[] { "Dependencies/PreserveMethodsSubStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "-MarkStep:PreserveMethodsSubStep,customstep.dll")]
[SetupCompileBefore ("CustomStepBeforeMark.dll", new[] { "Dependencies/PreserveMethodsSubStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "-MarkStep:PreserveMethodsSubStep,CustomStepBeforeMark.dll")]
public class CustomStepCanPreserveMethodsBeforeMark
{
public static void Main ()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Extensibility
{
[SetupCompileBefore ("SharedCustomSteps.dll", new[] { "Dependencies/CustomStepsWithSharedState.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "SharedStateHandler2,SharedCustomSteps.dll")]
[SetupLinkerArgument ("--custom-step", "SharedStateHandler1,SharedCustomSteps.dll")]
public class CustomStepsCanShareState
{
public static void Main ()
{
}

[Kept]
public static void MarkedMethod ()
{
}

public static void UnmarkedMethod ()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;

public class SharedAnnotation
{
public bool Mark { get; set; }

public static void Set (LinkContext context, MethodDefinition method, SharedAnnotation value)
{
context.Annotations.SetCustomAnnotation (nameof (SharedAnnotation), method, value);
}

public static SharedAnnotation Get (LinkContext context, MethodDefinition method) {
return context.Annotations.GetCustomAnnotation (nameof (SharedAnnotation), method) as SharedAnnotation;
}
}

public class SharedStateHandler1 : IMarkHandler
{
LinkContext context;

public void Initialize (LinkContext context, MarkContext markContext)
{
this.context = context;
markContext.RegisterMarkTypeAction (ProcessType);
}

public void ProcessType (TypeDefinition type)
{
if (!type.HasMethods)
return;

foreach (var method in type.Methods) {
if (method.Name == "MarkedMethod")
SharedAnnotation.Set (context, method, new SharedAnnotation () { Mark = true });
}
}
}

public class SharedStateHandler2 : IMarkHandler
{
LinkContext context;

public void Initialize (LinkContext context, MarkContext markContext)
{
this.context = context;
markContext.RegisterMarkTypeAction (ProcessType);
}

public void ProcessType (TypeDefinition type)
{
if (!type.HasMethods)
return;

foreach (var method in type.Methods) {
if (SharedAnnotation.Get (context, method) is SharedAnnotation annotation && annotation.Mark)
context.Annotations.Mark (method);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mono.Linker.Tests.Cases.Extensibility
#if !NETCOREAPP
[IgnoreTestCase ("Specific to the illink build")]
#endif
[SetupCompileBefore ("MyDispatcher.dll", new[] { "Dependencies/MyDispatcher.cs", "Dependencies/CustomSubStepFields.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "-MarkStep:MyDispatcher,MyDispatcher.dll")]
[SetupCompileBefore ("MyDispatcherFields.dll", new[] { "Dependencies/MyDispatcher.cs", "Dependencies/CustomSubStepFields.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "-MarkStep:MyDispatcher,MyDispatcherFields.dll")]
public class SubStepDispatcherFields
{
public static void Main ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mono.Linker.Tests.Cases.Extensibility
#if !NETCOREAPP
[IgnoreTestCase ("Specific to the illink build")]
#endif
[SetupCompileBefore ("MyDispatcher.dll", new[] { "Dependencies/MyDispatcher.cs", "Dependencies/CustomSubStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "-MarkStep:MyDispatcher,MyDispatcher.dll")]
[SetupCompileBefore ("MyDispatcherUsage.dll", new[] { "Dependencies/MyDispatcher.cs", "Dependencies/CustomSubStep.cs" }, new[] { "illink.dll", "Mono.Cecil.dll", "netstandard.dll" })]
[SetupLinkerArgument ("--custom-step", "-MarkStep:MyDispatcher,MyDispatcherUsage.dll")]
public class SubStepDispatcherUsage
{
public static void Main ()
Expand Down