Skip to content

Commit

Permalink
Enable building app extensions with NativeAOT
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpovazan committed Jul 10, 2024
1 parent 62b2b41 commit f2f732d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions dotnet/targets/Xamarin.Shared.Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,12 @@
<!-- Disable our own assembly IL stripping logic, because ILC does that already -->
<EnableAssemblyILStripping>false</EnableAssemblyILStripping>

<!-- We're using our own native main function when using NativeAOT -->
<CustomNativeMain>true</CustomNativeMain>
<!--
We're using our own native main function when using NativeAOT.
This is true for both: managed executables and app extensions that can be class libraries.
Since ILC expects to find a managed main function whenever NativeLib=static and CustomNativeMain=true,
we are only setting this flag when we are building executables. (Class libraries do not have a managed Main)
-->
<CustomNativeMain Condition="'$(OutputType)' == 'Exe'">true</CustomNativeMain>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@
MtouchFloat32=$(MtouchFloat32)
NoWarn=$(_BundlerNoWarn)
Optimize=$(_BundlerOptimize)
OutputType=$(OutputType)
PartialStaticRegistrarLibrary=$(_LibPartialStaticRegistrar)
Platform=$(_PlatformName)
PlatformAssembly=$(_PlatformAssemblyName).dll
Expand Down
1 change: 1 addition & 0 deletions tools/common/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public partial class Application {
public HashSet<string> Frameworks = new HashSet<string> ();
public HashSet<string> WeakFrameworks = new HashSet<string> ();

public bool IsClassLibrary;
public bool IsExtension;
public ApplePlatform Platform { get { return Driver.TargetFramework.Platform; } }

Expand Down
7 changes: 7 additions & 0 deletions tools/common/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,13 @@ void GenerateIOSMain (StringWriter sw, Abi abi)
sw.WriteLine ("\treturn rv;");
sw.WriteLine ("}");

// Add an empty __managed__Main function when building class lib app extensions with NativeAOT to workaround static reference to this symbol from nativeaot-bridge.m
if (app.IsExtension && App.IsClassLibrary && app.XamarinRuntime == XamarinRuntime.NativeAOT) {
sw.WriteLine ();
sw.Write ("extern \"C\" int __managed__Main (int argc, const char** argv) { return 0; } ");
sw.WriteLine ();
}

string extension_main = null;
if (app.Platform == ApplePlatform.WatchOS && app.IsWatchExtension) {
// We're building a watch extension, and we have multiple scenarios, depending on the watchOS version we're executing on:
Expand Down
3 changes: 3 additions & 0 deletions tools/dotnet-linker/LinkerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ public static LinkerConfiguration GetInstance (LinkContext context)
case "Optimize":
user_optimize_flags = value;
break;
case "OutputType":
Application.IsClassLibrary = string.Equals ("Library", value, StringComparison.OrdinalIgnoreCase);
break;
case "PartialStaticRegistrarLibrary":
PartialStaticRegistrarLibrary = value;
break;
Expand Down

0 comments on commit f2f732d

Please sign in to comment.