xamarin-macios/tools/linker/MarkNSObjects.cs

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

2016-04-21 15:57:02 +03:00
//
// MarkNSObjects.cs
//
// Authors:
// Jb Evain (jbevain@novell.com)
// Sebastien Pouliot <sebastien@xamarin.com>
//
// (C) 2009 Novell, Inc.
// Copyright (C) 2011-2014 Xamarin, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using Mono.Cecil;
using Mono.Linker;
using Mono.Tuner;
namespace Xamarin.Linker.Steps {
public class MarkNSObjects : ExceptionalSubStep {
2016-04-21 15:57:02 +03:00
static string ProductAssembly;
protected override string Name { get; } = "MarkNSObjects";
protected override int ErrorCode { get; } = 2080;
2016-04-21 15:57:02 +03:00
public override SubStepTargets Targets {
get { return SubStepTargets.Type; }
}
protected override void Process (TypeDefinition type)
2016-04-21 15:57:02 +03:00
{
if (ProductAssembly == null)
ProductAssembly = (Profile.Current as BaseProfile).ProductAssembly;
bool nsobject = type.IsNSObject (LinkContext);
2016-04-21 15:57:02 +03:00
if (!nsobject && !type.IsNativeObject ())
return;
if (!IsProductType (type)) {
// we need to annotate the parent type(s) of a nested type
// otherwise the sweeper will not keep the parents (nor the childs)
if (type.IsNested) {
var parent = type.DeclaringType;
while (parent != null) {
Annotations.Mark (parent);
parent = parent.DeclaringType;
}
}
Annotations.Mark (type);
Annotations.SetPreserve (type, TypePreserve.All);
} else {
PreserveIntPtrConstructor (type);
if (nsobject)
PreserveExportedMethods (type);
}
}
void PreserveExportedMethods (TypeDefinition type)
{
if (!type.HasMethods)
return;
foreach (var method in type.Methods) {
if (!IsExportedMethod (method))
continue;
if (!IsOverridenInUserCode (method))
continue;
PreserveMethod (type, method);
}
}
bool IsOverridenInUserCode (MethodDefinition method)
{
if (!method.IsVirtual)
return false;
var overrides = Annotations.GetOverrides (method);
if (overrides == null || overrides.Count == 0)
return false;
foreach (var @override in overrides)
if (!IsProductMethod (@override.Override))
2016-04-21 15:57:02 +03:00
return true;
return false;
}
bool IsExportedMethod (MethodDefinition method)
{
return HasExportAttribute (method);
}
bool HasExportAttribute (ICustomAttributeProvider provider)
{
if (!provider.HasCustomAttributes)
return false;
foreach (CustomAttribute attribute in provider.CustomAttributes)
if (attribute.Constructor.DeclaringType.Inherits (Namespaces.Foundation, "ExportAttribute"))
return true;
return false;
}
void PreserveIntPtrConstructor (TypeDefinition type)
{
if (!type.HasMethods)
return;
foreach (MethodDefinition constructor in type.GetConstructors ()) {
if (!constructor.HasParameters)
continue;
if (constructor.Parameters.Count != 1 || !constructor.Parameters [0].ParameterType.Is ("System", "IntPtr"))
continue;
PreserveMethod (type, constructor);
break; // only one .ctor can match this
}
}
void PreserveMethod (TypeDefinition type, MethodDefinition method)
{
Annotations.AddPreservedMethod (type, method);
}
static bool IsProductMethod (MethodDefinition method)
{
return (method.DeclaringType.Module.Assembly.Name.Name == ProductAssembly);
}
static bool IsProductType (TypeDefinition type)
{
[linker] Ensure we can remove NSUrlSessionHandler if unused (#7151) Moving `NSUrlSessionHandler` into the platform assemblies (e.g. Xamarin.iOS.dll) instead of System.Net.Http.dll was not optimal as it prevented the linker to remove it when the application did not use it. This shows a lot in an "helloworld" type of application (see below) but in real life most of the removed code gets used by something else (and is included. ``` Directories / Files helloworld-d16-4.app helloworld-fixed.app diff % ./ AppIcon60x60@2x.png 2,632 2,632 0 0.00 % AppIcon76x76@2x~ipad.png 3,125 3,125 0 0.00 % archived-expanded-entitlements.xcent 181 181 0 0.00 % Assets.car 75,688 75,688 0 0.00 % embedded.mobileprovision 8,456 8,456 0 0.00 % helloworld 4,700,256 3,915,936 -784,320 -16.69 % helloworld.aotdata.arm64 1,448 1,432 -16 -1.10 % helloworld.exe 6,144 6,144 0 0.00 % Info.plist 1,712 1,712 0 0.00 % mscorlib.aotdata.arm64 406,080 364,104 -41,976 -10.34 % mscorlib.dll 561,664 501,760 -59,904 -10.67 % NOTICE 159 159 0 0.00 % PkgInfo 8 8 0 0.00 % System.aotdata.arm64 17,008 936 -16,072 -94.50 % System.Core.aotdata.arm64 2,432 0 -2,432 -100.00 % System.Core.dll 4,608 0 -4,608 -100.00 % System.dll 32,768 5,120 -27,648 -84.38 % System.Net.Http.aotdata.arm64 31,648 0 -31,648 -100.00 % System.Net.Http.dll 29,184 0 -29,184 -100.00 % Xamarin.iOS.aotdata.arm64 62,544 33,464 -29,080 -46.50 % Xamarin.iOS.dll 92,672 53,248 -39,424 -42.54 % ./_CodeSignature CodeResources 7,575 6,655 -920 -12.15 % ./LaunchScreen.storyboardc 01J-lp-oVM-view-Ze5-6b-2t3.nib 1,831 1,835 4 0.22 % Info.plist 258 258 0 0.00 % UIViewController-01J-lp-oVM.nib 896 896 0 0.00 % ./Main.storyboardc BYZ-38-t0r-view-8bC-Xf-vdC.nib 1,836 1,832 -4 -0.22 % Info.plist 258 258 0 0.00 % UIViewController-BYZ-38-t0r.nib 916 916 0 0.00 % Statistics Native subtotal 4,700,256 3,915,936 -784,320 -16.69 % Executable 4,700,256 3,915,936 -784,320 -16.69 % AOT data *.aotdata 0 0 0 - Managed *.dll/exe 727,040 566,272 -160,768 -22.11 % TOTAL 6,053,987 4,986,755 -1,067,232 -17.63 % ```
2019-10-02 17:10:26 +03:00
return type.Module.Assembly.Name.Name == ProductAssembly;
2016-04-21 15:57:02 +03:00
}
}
}