Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Imports Foundation
Imports ObjCRuntime
Imports UIKit

Namespace MacCatalystApp1
<Register("SceneDelegate")>
<Adopts("UIWindowSceneDelegate")>
Public Class SceneDelegate
Inherits UIWindowSceneDelegate

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
Imports Foundation
Imports ObjCRuntime
Imports UIKit

Namespace iOSApp1
<Register("SceneDelegate")>
<Adopts("UIWindowSceneDelegate")>
Public Class SceneDelegate
Inherits UIWindowSceneDelegate

Expand Down
62 changes: 62 additions & 0 deletions tests/assembly-preparer/ManagedRegistrarStepTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics.CodeAnalysis;

using MonoTouch.Tuner;

using Xamarin.Linker;

namespace AssemblyPreparerTests;

public class ManagedRegistrarStepTests : BaseClass {
[Test]
public void NSObjectFactory ()
{
var code = @"
using Foundation;
using ObjCRuntime;

class NonGenericClass : NSObject {
protected NonGenericClass (NativeHandle handle)
: base (handle)
{
}
}

class GenericClass<T> : NSObject {
protected GenericClass (NativeHandle handle)
: base (handle)
{
}
}
";

using var preparer = CreatePreparer (ApplePlatform.MacCatalyst, true, p => p.Registrar = RegistrarMode.TrimmableStatic, code, out _);
var context = preparer.Configuration.DerivedLinkContext;
new LoadAssembliesStep ().Process (context);
new ManagedRegistrarStep ().Process (context);
var assemblyDefinition = context.GetAssemblies ().Single (v => v.Name.Name == "Test");

var nonGenericType = assemblyDefinition.MainModule.Types.Single (v => v.Name == "NonGenericClass");
Assert.That (nonGenericType.Interfaces.Select (v => v.InterfaceType.FullName), Does.Not.Contain ("Foundation.INSObjectFactory"), "Non-generic interfaces");
Assert.That (nonGenericType.Methods.Select (v => v.Name), Does.Not.Contain ("_Xamarin_ConstructNSObject"), "Non-generic methods");
Assert.That (nonGenericType.Interfaces.Select (v => v.InterfaceType.FullName), Does.Contain ("ObjCRuntime.INativeObject"), "Non-generic INativeObject interface");
Assert.That (nonGenericType.Methods.Select (v => v.Name), Does.Contain ("_Xamarin_ConstructINativeObject"), "Non-generic INativeObject methods");
Comment thread
rolfbjarne marked this conversation as resolved.
Resolved

var genericType = assemblyDefinition.MainModule.Types.Single (v => v.Name == "GenericClass`1");
Assert.That (genericType.Interfaces.Select (v => v.InterfaceType.FullName), Does.Contain ("Foundation.INSObjectFactory"), "Generic interfaces");
var factoryMethod = genericType.Methods.Single (v => v.Name == "_Xamarin_ConstructNSObject");
Assert.That (factoryMethod.Overrides.Select (v => v.DeclaringType.FullName), Does.Contain ("Foundation.INSObjectFactory"), "Generic method overrides");

Assert.That (GetInterfaceDependencies (nonGenericType), Is.Empty, "Non-generic interface dependencies");
Assert.That (GetInterfaceDependencies (genericType), Is.Empty, "Generic interface dependencies");

static IEnumerable<CustomAttribute> GetInterfaceDependencies (TypeDefinition type)
{
var cctor = type.Methods.SingleOrDefault (v => v.IsConstructor && v.IsStatic);
if (cctor is null)
return [];

return cctor.CustomAttributes.Where (v =>
v.AttributeType.FullName == "System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute"
&& v.ConstructorArguments.Count == 2
&& v.ConstructorArguments [0].Type.FullName == "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes"
&& (int) v.ConstructorArguments [0].Value == (int) DynamicallyAccessedMemberTypes.Interfaces);
}
}

[TestCase (XamarinRuntime.CoreCLR, false)]
[TestCase (XamarinRuntime.MonoVM, true)]
public void UnmanagedCallersOnlyEntryPoint (XamarinRuntime runtime, bool expectedEntryPoint)
Expand Down
21 changes: 2 additions & 19 deletions tests/dotnet/UnitTests/ProjectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3998,13 +3998,8 @@ public void AppendRuntimeIdentifierToOutputPath_DisableDirectoryBuildProps (Appl
"@executable_path/../../Contents/MonoBundle/libSystem.Net.Security.Native.dylib",
"@executable_path/../../Contents/MonoBundle/libSystem.Security.Cryptography.Native.Apple.dylib",
"/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit",
"/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices",
"/System/Library/Frameworks/CloudKit.framework/Versions/A/CloudKit",
"/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData",
"/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation",
"/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation",
"/System/Library/Frameworks/Quartz.framework/Versions/A/Quartz",
"/System/Library/Frameworks/QuartzCore.framework/Versions/A/QuartzCore",
"/System/Library/Frameworks/Security.framework/Versions/A/Security",
"/usr/lib/libc++.1.dylib",
"/usr/lib/libcompression.dylib",
Expand Down Expand Up @@ -4207,20 +4202,9 @@ public void AppendRuntimeIdentifierToOutputPath_DisableDirectoryBuildProps (Appl
.. coreclrFrameworks_iOS,
.. expectedFrameworks_iOS_None,
];
// The trimmable static registrar (the default registrar for CoreCLR) keeps protocol interfaces
// (and their corresponding *Wrapper types) alive in order to be able to register the protocol
// conformances of the types that survive trimming, which means that we end up linking with the
// frameworks those protocols come from (CloudKit.ICKRecordValue and CoreData.INSFetchRequestResult
// are implemented by Foundation types such as NSNumber and NSString).
static string [] trimmableStaticRegistrarFrameworks = [
"/System/Library/Frameworks/CloudKit.framework/CloudKit",
"/System/Library/Frameworks/CoreData.framework/CoreData",
];

static string [] expectedFrameworks_iOS_Full_CoreCLR = [
.. coreclrFrameworks_iOS,
.. expectedFrameworks_iOS_Full,
.. trimmableStaticRegistrarFrameworks,
];
static string [] expectedFrameworks_tvOS_None_CoreCLR = [
.. coreclrFrameworks_tvOS,
Expand All @@ -4229,16 +4213,15 @@ public void AppendRuntimeIdentifierToOutputPath_DisableDirectoryBuildProps (Appl
static string [] expectedFrameworks_tvOS_Full_CoreCLR = [
.. coreclrFrameworks_tvOS,
.. expectedFrameworks_tvOS_Full,
.. trimmableStaticRegistrarFrameworks,
];
static string [] expectedFrameworks_MacCatalyst_None_CoreCLR = [
.. coreclrFrameworks_MacCatalyst,
.. expectedFrameworks_MacCatalyst_None,
];
// CoreGraphics and QuartzCore are trimmed away for CoreCLR (but not for Mono).
// CloudKit, CoreData, CoreGraphics and QuartzCore are trimmed away for CoreCLR (but not for Mono).
static string [] expectedFrameworks_MacCatalyst_Full_CoreCLR = [
.. coreclrFrameworks_MacCatalyst,
.. expectedFrameworks_MacCatalyst_Full.Where (v => !v.Contains ("/CoreGraphics.framework/") && !v.Contains ("/QuartzCore.framework/")),
.. expectedFrameworks_MacCatalyst_Full.Where (v => !v.Contains ("/CloudKit.framework/") && !v.Contains ("/CoreData.framework/") && !v.Contains ("/CoreGraphics.framework/") && !v.Contains ("/QuartzCore.framework/")),
];

static IEnumerable<TestCaseData> GetLinkedWithNativeLibrariesTestCases_Mono ()
Expand Down
Loading