xamarin-macios/tools/linker/RemoveRejectedTypesStep.cs

98 строки
3.4 KiB
C#
Исходник Обычный вид История

[mtouch] Add `force-rejected-types-removal` optimization (#8009) This optimization can be enabled when it's not possible to use the managed linker (e.g. **Don't link**) or when the managed linker cannot remove references to deprecated types that would cause an application to be rejected by Apple. References to the existing types will be renamed, e.g. `UIWebView` to `DeprecatedWebView`, in every assemblies. The type definition is also renamed (for validity) and all custom attributes on the types and their members will be removed. Code inside the members will be replaced with a `throw new NotSupportedException ();`. The msbuild test app `MyReleaseBuild` has been updated to test that the optimization is working as expected (device builds are slow so reusing this test has little impact in test time). Basically the test ensure that `UIWebView` is used and cannot be removed by the compiler (optimization) or the managed linker (since it's referenced). Since the optimization is enabled then we can `grep` then final `.app` directory to ensure there's no mention of `UIWebView` inside any of the files that would be submitted. The application can be run, by itself, and will turn green if OK, red if `DeprecatedWebView` can't be found (skeleton replacement for `UIWebView`) or orange if a `NotSupportedException` is thrown. Finally introspection tests have been updated to skip over the deprecated (and renamed) types. It should not be an issue right now, since this optimization is not enabled by default, but it made testing easier.
2020-03-02 17:20:29 +03:00
using System;
using System.Collections.Generic;
using Mono.Cecil;
using Mono.Linker;
using Mono.Tuner;
using MonoTouch.Tuner;
using Xamarin.Tuner;
namespace Xamarin.Linker {
// Important: This is NOT a general purpose code remover. There are a lot of
// cases to cover and this implementation cover only the one it requires so far.
// IOW additional testing is REQUIRED if you change the scope of the code.
public class RemoveRejectedTypesStep : RemoveCodeBase {
public RemoveRejectedTypesStep ()
{
}
public DerivedLinkContext Context {
get { return context as DerivedLinkContext; }
}
public override SubStepTargets Targets {
get { return SubStepTargets.Assembly; }
}
protected override string Name { get; } = "Removing Rejected Type";
[mtouch] Add `force-rejected-types-removal` optimization (#8009) This optimization can be enabled when it's not possible to use the managed linker (e.g. **Don't link**) or when the managed linker cannot remove references to deprecated types that would cause an application to be rejected by Apple. References to the existing types will be renamed, e.g. `UIWebView` to `DeprecatedWebView`, in every assemblies. The type definition is also renamed (for validity) and all custom attributes on the types and their members will be removed. Code inside the members will be replaced with a `throw new NotSupportedException ();`. The msbuild test app `MyReleaseBuild` has been updated to test that the optimization is working as expected (device builds are slow so reusing this test has little impact in test time). Basically the test ensure that `UIWebView` is used and cannot be removed by the compiler (optimization) or the managed linker (since it's referenced). Since the optimization is enabled then we can `grep` then final `.app` directory to ensure there's no mention of `UIWebView` inside any of the files that would be submitted. The application can be run, by itself, and will turn green if OK, red if `DeprecatedWebView` can't be found (skeleton replacement for `UIWebView`) or orange if a `NotSupportedException` is thrown. Finally introspection tests have been updated to skip over the deprecated (and renamed) types. It should not be an issue right now, since this optimization is not enabled by default, but it made testing easier.
2020-03-02 17:20:29 +03:00
protected override int ErrorCode { get; } = 2660;
public List<(string originalFullName, string replacementTypeName)> TypeReferencesToBeRemoved = new List<(string,string)> () {
// order is important for nested types (that's why tuples are used instead of a Dictionary)
( "UIKit.UIWebView/_UIWebViewDelegate", "_DeprecatedWebViewDelegate"),
( "UIKit.UIWebView/UIWebViewAppearance","DeprecatedWebViewAppearance"),
( "UIKit.IUIWebViewDelegate","IDeprecatedWebViewDelegate"),
( "UIKit.UIWebView","DeprecatedWebView"),
( "UIKit.UIWebViewDelegate","DeprecatedWebViewDelegate"),
( "UIKit.UIWebViewDelegate_Extensions", "DeprecatedWebViewDelegate_Extensions"),
( "UIKit.UIWebViewDelegateWrapper", "DeprecatedWebViewDelegateWrapper"),
( "UIKit.UIWebViewNavigationType", "DeprecatedWebViewNavigationType"),
};
protected override void Process (AssemblyDefinition assembly)
{
// this will give us a working `get_nse`
base.Process (assembly);
var updated = false;
if (Profile.IsProductAssembly (assembly)) {
updated = true;
foreach (var item in TypeReferencesToBeRemoved) {
var type = assembly.MainModule.GetType (item.originalFullName);
if (type == null)
continue;
type.Name = item.replacementTypeName;
Context.AddLinkedAwayType (type);
// we need to remove [Register], [Protocol] and [ProtocolMembers] attributes
// and since other attributes won't make sense anymore so we remove them all
type.CustomAttributes.Clear ();
if (!type.IsInterface) {
// remove IL and custom attributes (e.g. [Export])
ProcessMethods (type);
}
}
} else if (Profile.IsSdkAssembly (assembly)) {
// note: we know the BCL does not include references to UIWebView
// so we only need to care for (i.e. tweak) user code
return;
} else {
foreach (var module in assembly.Modules) {
foreach (var item in TypeReferencesToBeRemoved) {
if (module.TryGetTypeReference (item.originalFullName, out var tr)) {
tr.Name = item.replacementTypeName;
updated = true;
}
}
}
}
// we'll need to save (if we're not linking) this assembly
if (updated && Annotations.GetAction (assembly) != AssemblyAction.Link)
Annotations.SetAction (assembly, AssemblyAction.Save);
}
protected override void ProcessMethod (MethodDefinition method)
{
if (method.HasCustomAttributes)
method.CustomAttributes.Clear ();
// single special case: deal with method `EnsureUIWebViewDelegate`
if (method.Name == "EnsureUIWebViewDelegate")
method.Name = "EnsureDeprecatedWebViewDelegate";
base.ProcessMethod (method);
}
}
}