Skip to content

Commit

Permalink
Pass custom steps separately from msbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
sbomer committed Apr 29, 2021
1 parent 468033b commit 5ec5c50
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 88 deletions.
44 changes: 39 additions & 5 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,45 @@
<!-- Mark our entry assembly as a root assembly. -->
<TrimmerRootAssembly Include="@(ResolvedFileToPublish)" Condition="'%(ResolvedFileToPublish.Filename)' == '$(AssemblyName)' And '%(ResolvedFileToPublish.Extension)' == '.dll'" />

<!-- add a custom step which inserts any other steps we need -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)">
<BeforeStep>MarkStep</BeforeStep>
<Type>Xamarin.SetupStep</Type>
</_TrimmerCustomSteps>
<!-- pre-mark custom steps -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="Xamarin.SetupStep" />
<!-- Load the list of assemblies loaded by the linker. -->
<!-- This would not be needed of LinkContext.GetAssemblies () was exposed to us. -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="Xamarin.Linker.CollectAssembliesStep" />
<!-- TODO: these steps should probably run after mark. -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.Steps.StoreMembersAndAttributesDispatcher" />
<!-- IMarkHandlers which run during Mark -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.Steps.PreserveBlockCodeHandler" />
<!--
We need to run the ApplyPreserveAttribute step even we're only linking sdk assemblies, because even
though we know that sdk assemblies will never have Preserve attributes, user assemblies may have
[assembly: LinkSafe] attributes, which means we treat them as sdk assemblies and those may have
Preserve attributes.
TODO: LinkSafeAttribute doesn't appear to be handled anywhere. Is this attribute still supported?
ApplyPreserveAttribute no longer runs on all assemblies. [assembly: Preserve (typeof (SomeAttribute))] will
no longer give SomeAttribute "Preserve" semantics.
-->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.Steps.ApplyPreserveDispatcher" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.OptimizeGeneratedCodeHandler" />
<!-- MarkNSObjects will run for all marked assemblies. -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.Steps.MarkNSobjectsDispatcher" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.Steps.PreserveSmartEnumConversionsHandler" />
<!-- post-sweep custom steps -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" AfterStep="SweepStep" Condition="'$(_LinkMode)' != 'None'" Type="Xamarin.Linker.Steps.RemoveAttributesDispatcher" />
<!-- pre-output custom steps -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="OutputStep" Type="Xamarin.Linker.Steps.ListExportedSymbols" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="OutputStep" Type="Xamarin.Linker.LoadNonSkippedAssembliesStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="OutputStep" Type="Xamarin.Linker.ExtractBindingLibrariesStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="OutputStep" Type="Xamarin.Linker.Steps.RemoveUserResourcesDispatcher" />
<!-- post-output steps -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.RegistrarStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.GenerateMainStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.GenerateReferencesStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.GatherFrameworksStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.ComputeNativeBuildFlagsStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.ComputeAOTArguments" />
<!-- Must be the last step. -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.DoneStep" />

<!-- _BundlerXmlDefinitions comes from any -xml arguments to mtouch/mmp -->
<TrimmerRootDescriptor Include="@(_BundlerXmlDefinitions)" />
Expand Down
83 changes: 0 additions & 83 deletions tools/dotnet-linker/SetupStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,92 +27,9 @@ public List<IStep> Steps {
}
}

List<IMarkHandler> _markHandlers;
List<IMarkHandler> MarkHandlers {
get {
if (_markHandlers == null) {
var pipeline = typeof (LinkContext).GetProperty ("Pipeline").GetGetMethod ().Invoke (Context, null);
_markHandlers = (List<IMarkHandler>) pipeline.GetType ().GetProperty ("MarkHandlers").GetValue (pipeline);
}
return _markHandlers;
}
}

void InsertBefore (IStep step, string stepName)
{
for (int i = 0; i < Steps.Count; i++) {
if (Steps [i].GetType ().Name == stepName) {
Steps.Insert (i, step);
return;
}
}
DumpSteps ();
throw new InvalidOperationException ($"Could not insert {step} before {stepName} because {stepName} wasn't found.");
}

void InsertAfter (IStep step, string stepName)
{
for (int i = 0; i < Steps.Count;) {
if (Steps [i++].GetType ().Name == stepName) {
Steps.Insert (i, step);
return;
}
}
DumpSteps ();
throw new InvalidOperationException ($"Could not insert {step} after {stepName} because {stepName} wasn't found.");
}

protected override void TryProcess ()
{
// Don't use --custom-step to load each step, because this assembly
// is loaded into the current process once per --custom-step,
// which makes it very difficult to share state between steps.

// Load the list of assemblies loaded by the linker.
// This would not be needed of LinkContext.GetAssemblies () was exposed to us.
InsertBefore (new CollectAssembliesStep (), "MarkStep");

var pre_mark_substeps = new DotNetSubStepDispatcher ();
InsertBefore (pre_mark_substeps, "MarkStep");

var post_sweep_substeps = new DotNetSubStepDispatcher ();
InsertAfter (post_sweep_substeps, "SweepStep");

if (Configuration.LinkMode != LinkMode.None) {
MarkHandlers.Add (new PreserveBlockCodeHandler ());

// We need to run the ApplyPreserveAttribute step even we're only linking sdk assemblies, because even
// though we know that sdk assemblies will never have Preserve attributes, user assemblies may have
// [assembly: LinkSafe] attributes, which means we treat them as sdk assemblies and those may have
// Preserve attributes.
// TODO: LinkSafeAttribute doesn't appear to be handled anywhere. Is this attribute still supported?
// ApplyPreserveAttribute no longer runs on all assemblies. [assembly: Preserve (typeof (SomeAttribute))] will
// no longer give SomeAttribute "Preserve" semantics.
MarkHandlers.Add (new DotNetMarkAssemblySubStepDispatcher (new ApplyPreserveAttribute ()));
MarkHandlers.Add (new OptimizeGeneratedCodeHandler ());
// MarkNSObjects will run for all marked assemblies.
MarkHandlers.Add (new DotNetMarkAssemblySubStepDispatcher (new MarkNSObjects ()));
MarkHandlers.Add (new PreserveSmartEnumConversionsHandler ());

// TODO: these steps should probably run after mark.
pre_mark_substeps.Add (new CollectUnmarkedMembersSubStep ());
pre_mark_substeps.Add (new StoreAttributesStep ());

post_sweep_substeps.Add (new RemoveAttributesStep ());
}

InsertBefore (new ListExportedSymbols (null), "OutputStep");
InsertBefore (new LoadNonSkippedAssembliesStep (), "OutputStep");
InsertBefore (new ExtractBindingLibrariesStep (), "OutputStep");
InsertBefore (new DotNetSubStepDispatcher (new RemoveUserResourcesSubStep ()), "OutputStep");
Steps.Add (new RegistrarStep ());
Steps.Add (new GenerateMainStep ());
Steps.Add (new GenerateReferencesStep ());
Steps.Add (new GatherFrameworksStep ());
Steps.Add (new ComputeNativeBuildFlagsStep ());
Steps.Add (new ComputeAOTArguments ());
Steps.Add (new DoneStep ()); // Must be the last step.

Configuration.Write ();

if (Configuration.Verbosity > 0) {
Expand Down
11 changes: 11 additions & 0 deletions tools/dotnet-linker/Steps/ApplyPreserveDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mono.Linker.Steps;
using Xamarin.Linker;

namespace Xamarin.Linker.Steps {
class ApplyPreserveDispatcher : MarkSubStepsDispatcher {
public ApplyPreserveDispatcher ()
: base (new [] { new ApplyPreserveAttribute () })
{
}
}
}
11 changes: 11 additions & 0 deletions tools/dotnet-linker/Steps/MarkNSObjectsDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mono.Linker.Steps;
using Xamarin.Linker;

namespace Xamarin.Linker.Steps {
class MarkNSObjectsDispatcher : MarkSubStepsDispatcher {
public MarkNSObjectsDispatcher ()
: base (new [] { new MarkNSObjects () })
{
}
}
}
11 changes: 11 additions & 0 deletions tools/dotnet-linker/Steps/RemoveAttributesDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mono.Linker.Steps;
using Xamarin.Linker;

namespace Xamarin.Linker.Steps {
class RemoveAttributesDispatcher : SubStepsDispatcher {
public RemoveAttributesDispatcher ()
: base (new [] { new RemoveAttributesStep () })
{
}
}
}
11 changes: 11 additions & 0 deletions tools/dotnet-linker/Steps/RemoveUserResourcesDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Mono.Linker.Steps;
using Xamarin.Linker;

namespace Xamarin.Linker.Steps {
class RemoveUserResourcesDispatcher : SubStepsDispatcher {
public RemoveUserResourcesDispatcher ()
: base (new [] { new RemoveUserResourcesSubStep () })
{
}
}
}
14 changes: 14 additions & 0 deletions tools/dotnet-linker/Steps/StoreMembersAndAttributesDispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Mono.Linker.Steps;
using Xamarin.Linker;

namespace Xamarin.Linker.Steps {
class StoreMembersAndAttributesDispatcher : SubStepsDispatcher {
public StoreMembersAndAttributesDispatcher ()
: base (new BaseSubStep [] {
new CollectUnmarkedMembersSubStep (),
new StoreAttributesStep ()
})
{
}
}
}

0 comments on commit 5ec5c50

Please sign in to comment.