Skip to content

Commit d8b772e

Browse files
committed
[dotnet-linker] Use [DynamicDependency] attributes instead of manual marking when applying the [Preserve] attribute.
This makes it easier to move this code out of a custom linker step in the future. This was simplified a bit because we already had code to do this, but it was only in effect when using NativeAOT. Generalize it, move it out of the marking phase of the linker, and run it before marking. Also move the removal of any [Preserve] attributes to the RemoveAttributes step. Contributes towards #17693.
1 parent 723f6c9 commit d8b772e

File tree

9 files changed

+292
-290
lines changed

9 files changed

+292
-290
lines changed

dotnet/targets/Xamarin.Shared.Sdk.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,8 @@
551551
<_UseDynamicDependenciesForSmartEnumPreservation Condition="'$(_UseDynamicDependenciesForSmartEnumPreservation)' == ''">$(_UseDynamicDependenciesInsteadOfMarking)</_UseDynamicDependenciesForSmartEnumPreservation>
552552
<_UseDynamicDependenciesForBlockCodePreservation Condition="'$(_UseDynamicDependenciesForBlockCodePreservation)' == ''">$(_UseDynamicDependenciesInsteadOfMarking)</_UseDynamicDependenciesForBlockCodePreservation>
553553
<_UseDynamicDependenciesForGeneratedCodeOptimizations Condition="'$(_UseDynamicDependenciesForGeneratedCodeOptimizations)' == ''">$(_UseDynamicDependenciesInsteadOfMarking)</_UseDynamicDependenciesForGeneratedCodeOptimizations>
554+
<_UseDynamicDependenciesForApplyPreserveAttribute Condition="'$(_UseDynamicDependenciesForApplyPreserveAttribute)' == '' And '$(_XamarinRuntime)' == 'NativeAOT'">true</_UseDynamicDependenciesForApplyPreserveAttribute>
555+
<_UseDynamicDependenciesForApplyPreserveAttribute Condition="'$(_UseDynamicDependenciesForApplyPreserveAttribute)' == ''">$(_UseDynamicDependenciesInsteadOfMarking)</_UseDynamicDependenciesForApplyPreserveAttribute>
554556
</PropertyGroup>
555557

556558
<PropertyGroup>
@@ -756,6 +758,7 @@
756758
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="Xamarin.Linker.Steps.PreserveSmartEnumConversionsStep" Condition="'$(_AreAnyAssembliesTrimmed)' == 'true' And '$(_UseDynamicDependenciesForSmartEnumPreservation)' == 'true'" />
757759
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="Xamarin.Linker.Steps.PreserveBlockCodeStep" Condition="'$(_AreAnyAssembliesTrimmed)' == 'true' And '$(_UseDynamicDependenciesForBlockCodePreservation)' == 'true'" />
758760
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="Xamarin.Linker.Steps.OptimizeGeneratedCodeStep" Condition="'$(_AreAnyAssembliesTrimmed)' == 'true' And '$(_UseDynamicDependenciesForGeneratedCodeOptimizations)' == 'true'" />
761+
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="Xamarin.Linker.Steps.ApplyPreserveAttributeStep" Condition="'$(_AreAnyAssembliesTrimmed)' == 'true' And '$(_UseDynamicDependenciesForApplyPreserveAttribute)' == 'true'" />
759762
<!-- The final decision to remove/keep the dynamic registrar must be done before the linking step -->
760763
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" BeforeStep="MarkStep" Type="MonoTouch.Tuner.RegistrarRemovalTrackingStep" />
761764
<!-- TODO: these steps should probably run after mark. -->

tools/common/DerivedLinkContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public class DerivedLinkContext : LinkContext {
4848
// so we need a second dictionary
4949
Dictionary<TypeDefinition, LinkedAwayTypeReference> LinkedAwayTypeMap = new Dictionary<TypeDefinition, LinkedAwayTypeReference> ();
5050

51+
public bool DidRunApplyPreserveAttributeStep { get; set; }
52+
5153
public DerivedLinkContext (LinkerConfiguration configuration, Application app)
5254
#if !LEGACY_TOOLS
5355
: base (configuration)

tools/dotnet-linker/AppBundleRewriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ public bool AddAttributeToStaticConstructor (TypeDefinition onType, CustomAttrib
13841384
return modified;
13851385
}
13861386

1387-
MethodDefinition GetOrCreateStaticConstructor (TypeDefinition type, out bool modified)
1387+
public MethodDefinition GetOrCreateStaticConstructor (TypeDefinition type, out bool modified)
13881388
{
13891389
modified = false;
13901390

@@ -1408,7 +1408,7 @@ MethodDefinition GetOrCreateStaticConstructor (TypeDefinition type, out bool mod
14081408
/// <param name="provider">The provider to which the attribute should be added.</param>
14091409
/// <param name="attribute">The attribute to add.</param>
14101410
/// <returns>Whether the attribute was added or not.</returns>
1411-
bool AddAttributeOnlyOnce (ICustomAttributeProvider provider, CustomAttribute attribute)
1411+
public bool AddAttributeOnlyOnce (ICustomAttributeProvider provider, CustomAttribute attribute)
14121412
{
14131413
if (provider.HasCustomAttributes) {
14141414
foreach (var ca in provider.CustomAttributes) {

0 commit comments

Comments
 (0)