Граф коммитов

277 Коммитов

Автор SHA1 Сообщение Дата
Simon Rozsival 1312c43157 Simplify code 2023-11-24 14:57:48 +01:00
GitHub Actions Autoformatter 7a39369644 Auto-format source code 2023-11-23 14:19:08 +00:00
Simon Rozsival 635ef072c9 Change the transformation of Preserve attribute into DynamicDependency 2023-11-23 14:08:31 +01:00
Rolf Bjarne Kvinge a7387f9ba5
[dotnet-linker] Remove code/file we don't need (#19447) 2023-11-15 09:14:51 +01:00
Rolf Bjarne Kvinge 8100be8aff
Fix a few places that hardcoded the target framework to use the current .NET version instead. (#19403) 2023-11-09 16:24:04 +01:00
Rolf Bjarne Kvinge a0eec0c265
[maestro] Fix a few issues with the maestro subscriptions. (#19347)
* The 'Microsoft.NET.Workload.Emscripten.Current.Manifest-8.0.100.Transport' dependency was renamed to 'Microsoft.NET.Workload.Emscripten.Current.Manifest-8.0.100'
* Add a separate dependency for 'Microsoft.NET.ILLink', since it can have a different version than 'Microsoft.NET.ILLink.Tasks'.
* Update using the '.NET 8' and '.NET 8.0.1xx' channels.
2023-10-26 07:45:03 +02:00
Steve Hawley f4a0ea9ac2
[dotnet] Test integration (#18543)
Fixed an issue where the C# class name was coming in with `,
AssemblyName` tacked on the end.
Fixed an issue where a class that had an entry in the map didn't have a
`class_ptr` which was causing an NRE.
Fixed a predicate for deciding when to mark the assembly for save.

Note - for an as yet undetermined reason, the linker is not picking up
that I'm marking the assembly for save, which is why the `true` test
cases are commented out.
This fixes issue 16671 https://github.com/xamarin/xamarin-macios/issues/16671
2023-10-17 10:29:12 -04:00
Rolf Bjarne Kvinge 50c34f31a8 Merge remote-tracking branch 'origin/net8.0-xcode15' into merge-xcode15-into-net8.0 2023-10-10 23:26:57 +02:00
Ivan Povazan 951e756292
NativeAOT: Mark module constructors in MSR (#19097)
There is an issue with managed static registrar and explicitly rooted
assemblies (through linker descriptor files) where the linker does not
emit the created module initializer.

This was originally reported in:
https://github.com/dotnet/runtime/issues/92106
and a smaller repro can be set up in: `tests/dotnet/MySingleView` test
project with the following changes:

### Smaller repro
1. Add a descriptor xml - `Roots.xml`
```xml
<linker>
  <assembly fullname="MySingleView" preserve="All" />
</linker>
```

2. Include the root descriptor in the `MySingleView.csproj`
```xml
<ItemGroup>
     <TrimmerRootDescriptor Condition="'$(_RootSelf)' == 'true'" Include="Roots.xml" />
</ItemGroup>
```

3.  Publish/run the app on a device
```bash
dotnet publish -c Release -r ios-arm64 -p:PublishAot=true -p:PublishAotUsingRuntimePack=true -p:_RootSelf=true -t:Run /bl
```

4. The app crashes at start up with:
```
2023-09-25 18:55:42.562 MySingleView[477:55844] *** Terminating app due to uncaught exception 'ObjCRuntime.RuntimeException', reason: 'Can't register the class MySingleView.AppDelegate when the dynamic registrar has been linked away. (ObjCRuntime.RuntimeException)
   at ObjCRuntime.Class.GetClassHandle(Type, Boolean, Boolean&) + 0x1c4
   at UIKit.UIApplication.Main(String[], Type, Type) + 0x8c
   at MySingleView!<BaseAddress>+0x2fae7c
'
```

NOTE: This is reported for NativeAOT but seems like a general problem

Additionally, here are the disassembled linked assemblies:
- regular:
https://gist.github.com/ivanpovazan/e82bdcb5313a846ff50de0a79d560553
- rooted:
https://gist.github.com/ivanpovazan/760e931fdcd3ef3e573d88087718dad2

It can be seen that the rooted one is missing:
```asm
// ================== GLOBAL METHODS =========================

.method private hidebysig specialname rtspecialname static 
        void  .cctor() cil managed
{
  // Code size       11 (0xb)
  .maxstack  1
  IL_0000:  newobj     instance void ObjCRuntime.__Registrar__::.ctor()
  IL_0005:  call       void [Microsoft.iOS]ObjCRuntime.RegistrarHelper::Register(class [Microsoft.iOS]ObjCRuntime.IManagedRegistrar)
  IL_000a:  ret
} // end of global method .cctor
```

### The fix

This PR fixes this behaviour by explicitly marking module constructor to
prevent this from happening.

---
Fixes https://github.com/dotnet/runtime/issues/92106
2023-09-27 12:38:01 -04:00
Rolf Bjarne Kvinge 79ff82d046 [net8.0-xcode15] Merge net8.0 into net8.0-xcode15. 2023-09-18 18:28:24 +02:00
Filip Navara 7f737324d4
[net8.0] Remove workarounds for SDK and runtime bugs (#18830)
Fixes #18741
Fixes #18784
2023-09-15 07:43:38 +02:00
Rolf Bjarne Kvinge baab03cf57
[registrar] Look in nested types when looking for protocol wrapper types to register. Fixes #18973. (#19031)
Protocol wrapper types may be nested types, so make sure to look in nested
types when looking for protocol wrapper types.

Fixes https://github.com/xamarin/xamarin-macios/issues/18973.
2023-09-15 07:42:15 +02:00
Rolf Bjarne Kvinge be6c348cfd [net8.0-xcode15] Merge net8.0 into net8.0-xcode15. 2023-09-14 07:25:58 +02:00
Ivan Povazan bf77022512
Simplify icudat file lookup by specifying ICU_DAT_FILE_PATH as a RuntimeHostConfigurationOption (#18914)
Fixes https://github.com/xamarin/xamarin-macios/issues/18471
2023-09-08 16:46:32 +02:00
Rolf Bjarne Kvinge 673dd5782b [net8.0-xcode15] Merge net8.0 into net8.0-xcode15. 2023-09-08 10:31:44 +02:00
Ivan Povazan 1e06cef1e7 Clean up globalization custom logic 2023-09-06 18:03:32 +02:00
Šimon Rozsíval fb86b95849
[NativeAOT] Fix ILC warning for NSObject.RegisterToggleRef (#18889)
This PR implements a workaround for the following build warning:

```
ILC: Method '[Microsoft.iOS]Foundation.NSObject.RegisterToggleRef(NSObject,native int,bool)' will always throw because:
Invalid IL or CLR metadata in 'Void Foundation.NSObject.RegisterToggleRef(Foundation.NSObject, IntPtr, Boolean)'
```

Ref #18524
2023-09-06 12:13:30 +02:00
Rolf Bjarne Kvinge b3c869080e
Merge branch 'net8.0' into new-find-icu-data 2023-09-05 08:48:37 +02:00
Ivan Povazan ed26faa94f Simplify icudat file lookup by specifying ICU_DAT_FILE_PATH as a RuntimeHostConfigurationOption 2023-09-01 18:50:55 +02:00
Šimon Rozsíval d7b97abe50
[NativeAOT] Preserve all constructors instead of no members (#18903)
`None` seems to be equivalent to not having the attribute at all. The type will be stripped by ILLink and ILC. The `PublicConstructors | NonPublicConstructors` seems to be a better option to keep the type while making it possible to trim as much of its members as possible.

This change resolves two warnings:
```
ILLink : warning IL2037: <Module>..cctor(): No members were resolved for 'None' on type 'Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.FrameView'.
ILC : warning IL2037: <Module>..cctor(): No members were resolved for '0' on type 'Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.FrameView'. 
```
(Note: these warnings will still be present for _static_ classes which don't have a static constructor)

@rolfbjarne I believe we don't need https://github.com/dotnet/runtime/pull/90154 anymore


---------

Co-authored-by: Simon Rozsival <simon@rozsival.com>
2023-09-01 16:08:19 +02:00
Šimon Rozsíval bcb486caa7
[NativeAOT] Replace custom method signature builder with DocCommentId (#18864)
This is a follow-up to #18854

The code that we use to generate method signatures still isn't working
properly. Method signatures that contain constructed generic types are
incorrect (this is a problem for example for UIGestureRecognizer
subclasses in MAUI). We are also handling multidimensional arrays
incorrectly.

Instead of implementing the signature builder ourselves, we can utilize
the `DocCommentId.GetDocCommentId (member)` method from
Mono.Cecil.Rocks.

```c#
void Method(IEnumerable<int> arg) { }

// before
[DynamicDependency ("Method(System.Collections.Generic.IEnumerable`1<System.Int32>)", typeof (MyClass))]

// after
[DynamicDependency ("Method(System.Collections.Generic.IEnumerable{System.Int32})", typeof (MyClass))]
```
2023-08-30 11:03:20 +02:00
Rolf Bjarne Kvinge ef408d24a9 [net8.0-xcode15] Merge net8.0 into net8.0-xcode15. 2023-08-29 11:53:41 +02:00
Rolf Bjarne Kvinge 78b649c4e1 [net8.0] Merge main into net8.0. 2023-08-29 11:24:40 +02:00
Rolf Bjarne Kvinge 45225dc88d
[dotnet] Parameterize the pack names. (#18732)
We're going to change the pack names to support multi-targeting, so ahead
of the pack name change I'm changing the existing logic to use a variable
for the pack name in most places (this will make the rename much easier and
simpler).

These changes should have no effect by themselves.
2023-08-29 10:06:46 +02:00
Šimon Rozsíval 108967ab7e
[NativeAOT] Fix transforming [Preserve] into [DynamicDependency] for generic types and generic methods (#18854)
I noticed that we're generating invalid DynamicDependency attributes for generic types and methods. For example for the `void Activated(T sender)` method of `UIKit.UIGestureRecognizer.Callback<T>`:

```c#
// before:
[DynamicDependency("Activated(T)", typeof(UIKit.UIGestureRecognizer.Callback<>))]

// after:
[DynamicDependency("Activated(`0)", "UIKit.UIGestureRecognizer.Callback`1", "Microsoft.MacCatalyst")]
```

---------

Co-authored-by: Simon Rozsival <simon@rozsival.com>
2023-08-29 08:02:29 +02:00
Šimon Rozsíval 6f9eea25cd
[NativeAOT] Remove unnecessary [DynamicDependency] attributes (#18808)
This is a follow-up to #18666

This PR resolves the following warnings that I've seen in build logs
when running MySingleView with NativeAOT:

```
ILLink : warning IL2034: <Module>..cctor(): The 'DynamicDependencyAttribute' could not be analyzed. [/..../xamarin/xamarin-macios/tests/dotnet/MySingleView/MySingleView.csproj]
ILLink : warning IL2034: <Module>..cctor(): The 'DynamicDependencyAttribute' could not be analyzed. [/..../xamarin/xamarin-macios/tests/dotnet/MySingleView/MySingleView.csproj]
...
```

The generated module cctor code looks like this:

```c#
internal class <Module>
{
	[DynamicDependency("InvokeConformsToProtocol(ObjCRuntime.NativeHandle)", typeof(NSObject))]
	[DynamicDependency("Foundation.NSObject", null)]
	[DynamicDependency("ConformsToProtocol(ObjCRuntime.NativeHandle)", typeof(NSObject))]
	[DynamicDependency("Foundation.NSObject", null)]
        // ...
	static <Module>()
	{
		// ...
	}
}
```

The `[DynamicDependency("T", null)]` attributes are invalid. We could
change them to `[DynamicDependency(DynamicallyAccessedMemberTypes.None,
typeof(T))]` but I think that attribute is redundant because the type
itself is always already preserved through the attribute that precedes
it with a selector of one of its methods/fields.
2023-08-28 12:48:21 +02:00
Šimon Rozsíval 4ddee61b66
[NativeAOT] Add support for conditional [Preserve] attributes (#18803)
This is a follow-up to #18666

The idea is to transform `[Preserve(Conditional = true)]` into
`[DynamicDependency(signature, type)]` on the static constructor of the
given type:

```c#
class MyClass {
    [Preserve (Conditional = true)]
    public void MyMethod () { }

    // transformed into ...

    [DynamicDependency ("MyMethod", typeof (MyClass))]
    static MyClass() { /* either the existing cctor or a new empty cctor */ }

    public void MyMethod () { }
}
```
2023-08-25 07:41:16 +02:00
Rolf Bjarne Kvinge 7d5e1a4e7e [net8.0-xcode15] Merge net8.0 into net8.0-xcode15. 2023-08-23 17:35:17 +02:00
Rolf Bjarne Kvinge 2091e09592
[net8.0] Merge main into net8.0 (#18770) 2023-08-22 15:19:06 +02:00
Filip Navara 1ddcc87baf Make sure the RegistrarHelper.RuntimeTypeHandleEquals method is public and not trimmed 2023-08-19 20:02:26 +02:00
Filip Navara 959cef65e2 Add workaround for dotnet/runtime#90800 2023-08-19 13:34:00 +02:00
Rolf Bjarne Kvinge 19b2b3744f
[NativeAOT] Add support for [Preserve] attributes (#18666)
Add partial support for the `[Preserve]` attribute for NativeAOT. This
is done by injecting an equivalent `[DynamicDependency]` attribute. The
partial support comes from the fact that there's no way to map a
conditional preserve attribute (`[Preserve (Conditional = true)]`) to a
`[DynamicDependency]` attribute, so we report a warning instead.

For non-conditional `[Preserve]` attributes, we'll now add a
`[DynamicDependency]` attribute to the assembly's module constructor for
the type/member in question, effectively rooting it.

This makes it possible to fully link all our test suites when NativeAOT
(otherwise NativeAOT would just link out all the tests, leaving the test
suites empty - and unfortunately green, so this was a rather accidental
discovery).
2023-08-18 16:49:47 +02:00
Rolf Bjarne Kvinge 8bb54d360f [net8.0-xcode15] Merge net8.0 into net8.0-xcode15. 2023-08-16 11:21:06 +02:00
Rolf Bjarne Kvinge c8ae3dc072 [net8.0] Merge main into net8.0. 2023-08-16 11:19:56 +02:00
Rolf Bjarne Kvinge 637c6b6bcb
[tools] Store list of trampolines even if it's empty. Fixes #18570. (#18724)
This is a bit non-obvious, but we later use the presence of such a
stored list to determine if we need to generate the __Registrar__ type in the
assembly.

The problem is that we need to generate the __Registrar__ type even if
no trampolines were created, because we use it for numerous other purposes
as well (type lookup for instance).

Fixes https://github.com/xamarin/xamarin-macios/issues/18570.
2023-08-15 08:53:10 +02:00
Šimon Rozsíval 7f67aa0bae
[registrar] Create instances of NSObjects and INativeObjects without using reflection in the managed static registrar (#18519)
This PR adds lookup tables and factory methods for INativeObjects and
NSObjects. These generated methods allow us to create instances of these
objects without needing reflection.

Closes #18358.
2023-08-15 08:30:46 +02:00
Rolf Bjarne Kvinge ac90c511d1 [net8.0] Merge main into net8.0. 2023-08-14 15:36:54 +02:00
Rolf Bjarne Kvinge f24a016302
[registrar] Call INativeObject ctors directly in the managed static registrar. (#18706)
We can determine the exact constructor to call when creating
INativeObject instances in the generated code, so do exactly that.
This avoids a lot of slow reflection, and also makes the constructor
call visible for any linkers.
2023-08-14 12:07:16 +02:00
Rolf Bjarne Kvinge be08931941
[dotnet] Stop hardcoding the .NET version in a few more projects. (#18702)
Just use whatever .NET version we're using to build with.
2023-08-14 11:56:32 +02:00
Rolf Bjarne Kvinge 4c115b037b
[tools] Remove some dead code in the class redirector rewriter. (#18695)
This also fixes a compiler warnings:

> tools/common/Rewriter.cs(333,25): warning CS8604: Possible null
reference argument for parameter 'path1' in 'string Path.Combine(string
path1, string path2)'.

And make nullability warnings in dotnet-linker show up as errors to
prevent future introduction of nullability warnings.
2023-08-14 11:49:06 +02:00
Rolf Bjarne Kvinge 004a0aef79 Merge remote-tracking branch 'origin/net8.0' into bump-net8.0-in-net8.0-xcode15-2023-08-08 2023-08-10 16:36:57 +02:00
Rolf Bjarne Kvinge 58b5d14d8c [net8.0] Merge main into net8.0. 2023-08-07 20:21:34 +02:00
Šimon Rozsíval 14f75f06dc
MSR: Generate additional constructors to reduce usage of reflection (#18529)
Closes #18357

This PR implements the idea described in the linked issue:

Whenever there's an NSObject constructor that we call from a registrar
callback, we need to create
a separate constructor that will first set the `handle` and `flags`
values of the NSObject before
calling the original constructor. Here's an example of the code we
generate:

```csharp
// The original constructor:
public .ctor (T0 p0, T1 p1, ...) { /* ... */ }

// The generated constructor with pre-initialization: 
public .ctor (T0 p0, T1 p1, ..., IntPtr handle, IManagedRegistrar dummy) {
    this.handle = (NativeHandle)handle;
    this.flags = 2; // Flags.NativeRef == 2
    this..ctor (p0, p1, ...);
}
```

- This code can't be expressed in C# and it can only be expressed
directly in IL.
- The reason we need to do this is because the base NSObject
parameterless constructor
  would allocate a new Objective-C object if `handle` is a zero pointer.
- The `IManagedRegistrar` type is added only to make the constructor
signature unique (IManagedRegistrar is not a public class and customers
can't use it in their apps directly)
2023-08-07 15:41:07 +02:00
Rolf Bjarne Kvinge 6830fbf237 Fix build. 2023-08-04 11:49:45 -04:00
Ivan Povazan f212f6bdb3
Do not include -dead_strip when native linking a NativeAOT object file (#18553)
This PR disables passing `-dead_strip` to the native linker in case of
NativeAOT runtime to prevent build failures.

Additionally, this change affects the size of the application in the
following way - measured with `dotnet new maui` app version
`8.0.0-preview.7.23359.1`:

| MAUI iOS | -dead_strip | no -dead_strip | diff (b) | diff (Kb) | diff
(%) |
|----------|--------------|----------|----------|-----------|----------|
| .ipa (b) | 13377583 | 13435276 | 57693 | 57,693 | 0,43% |
| Size on disk (b) | 41883897 | 42038873 | 154976 | 154,976 | 0,37% |
| binary (b) | 39614336 | 39769312 | 154976 | 154,976 | 0,39% |

Even though the size of the application regresses, with this change we
have a more stable product.

Finally, once this PR gets merged we can open a tracking issue to solve
the size regression either by fixing:
- https://github.com/dotnet/runtime/issues/88032 or
- by manually removing the dead code as proposed by @filipnavara here:
https://github.com/dotnet/runtime/issues/88032#issuecomment-1624303167

--- 
Fixes: https://github.com/xamarin/xamarin-macios/issues/18552
2023-07-13 10:57:37 -04:00
Rolf Bjarne Kvinge e2c673972b [net8.0] Merge main into net8.0. 2023-06-29 17:05:54 +02:00
Šimon Rozsíval 6010c13d40
[managed-static-registrar] Remove use of reflection in UCOs of generic types (#18421)
Closes #18356

In the static UnmanagedCallersOnly methods we don't know the generic
parameters of the type we're working with and we need to use this trick
to be able to call methods on the generic type without using reflection.
When we call a non-generic interface method implemented on a generic
class, the .NET runtime will resolve the generic parameters for us. In
the implementation of the interface method, we can simply use the
generic parameters and generate the same code we usually generate in the
UnmanagedCallersOnly callback method.

This is an example of the code we generate in addition to user code:

```csharp
internal interface __IRegistrarGenericTypeProxy__CustomNSObject_1__
{
	void __IRegistrarGenericTypeProxy__CustomNSObject_1____SomeMethod (IntPtr p0);
}

public class CustomNSObject<T> : NSObject, __IRegistrarGenericTypeProxy__CustomNSObject_1__
	where T : NSObject
{
	[Export ("someMethod:")]
	public void SomeMethod (T someInput)
	{
		// ...
	}

	// generated implementation of the proxy interface:
	public void __IRegistrarGenericTypeProxy__CustomNSObject_1____SomeMethod (IntPtr sel, IntPtr p0, IntPtr* exception_gchandle)
	{
		try {
			var obj0 = (T) Runtime.GetNSObject<T> (p0);
			SomeMethod (obj0);
		} catch (Exception ex) {
			*exception_gchandle = Runtime.AllocGCHandle (ex);
		}
	}

	// generated registrar callbacks:
	private static class __Registrar_Callbacks__
	{
		[UnmanagedCallersOnly (EntryPoint = "_callback_1_CustomNSObject_1_SomeMethod")]
		public unsafe static void callback_1_CustomNSObject_1_SomeMethod (IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr* exception_gchandle)
		{
			var proxy = (__IRegistrarGenericTypeProxy__CustomNSObject_1__)Runtime.GetNSObject (pobj);
			proxy.__IRegistrarGenericTypeProxy__CustomNSObject_1____SomeMethod (sel, p0, exception_gchandle);
		}
	}
}
```
```csharp
// regular non-generic class for comparison:
public class CustomNSObject : NSObject
	where T : NSObject
{
	[Export ("someMethod:")]
	public void SomeMethod (NSSet someInput)
	{
		// ...
	}
	
	private static class __Registrar_Callbacks__
	{
		[UnmanagedCallersOnly (EntryPoint = "_callback_1_CustomNSObject_1_SomeMethod")]
		public unsafe static void callback_1_CustomNSObject_1_SomeMethod (IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr* exception_gchandle)
		{
			try {
				NSSet obj0 = Runtime.GetNSObject<NSSet> (p0);
				SomeMethod (obj0);
			} catch (Exception ex) {
				*exception_gchandle = Runtime.AllocGCHandle (ex);
			}
		}
	}
}
```

---------

Co-authored-by: Alex Soto <alex@alexsoto.me>
2023-06-28 12:24:46 +02:00
Meri Khamoyan 3f6d43c756
[iOS] HybridGlobalization set flag in SDK (#18498)
Add HybridGlobalization flag in SDK and load icudt_hybrid.dat file when
HybridGlobalization is on.

Contributes to https://github.com/dotnet/runtime/issues/80689
2023-06-28 10:25:24 +02:00
Rolf Bjarne Kvinge 15b7907eab [net8.0] Merge main into net8.0. 2023-06-23 17:03:50 +02:00
Steve Hawley a36cd71174
[dotnet] Integrate class handle rewriting into static registrar process. (#18456)
Integrate class handle rewriting into static registrar.
2023-06-23 10:12:35 -04:00