Skip to content

Commit

Permalink
WIP: NativeAOT-ios build adaptations
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanpovazan committed May 19, 2023
1 parent 63b310f commit 98a8f4e
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 17 deletions.
7 changes: 7 additions & 0 deletions dotnet/targets/Xamarin.Shared.Sdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
<_IsTrimmingEnabled>true</_IsTrimmingEnabled>
<!-- The GRCF property is contingent upon the HasRuntimeOutput property, which is only defined for executable projects. So, explicitly define GRCF for extension projects to avoid warnings at build time. -->
<GenerateRuntimeConfigurationFiles Condition="'$(IsAppExtension)' == 'true'">true</GenerateRuntimeConfigurationFiles>

</PropertyGroup>

<PropertyGroup Condition="'$(PublishAot)' == 'true'" >
<IlcKeepManagedDebuggerSupport Condition=''>true</IlcKeepManagedDebuggerSupport>

This comment has been minimized.

Copy link
@jkotas

jkotas May 19, 2023

What is the managed debugger support needed for with PublishAot?

This comment has been minimized.

Copy link
@ivanpovazan

ivanpovazan May 19, 2023

Author Owner

That was added as a workaround for dotnet/runtime#86186
Also please keep in mind this is a WIP and was added to "make things work asap" rather than taking the right approach.

<IlcCompileDependsOn>SetupOSSpecificProps;PrepareForILLink;_XamarinComputeIlcCompileInputs</IlcCompileDependsOn>
<CustomNativeMain>true</CustomNativeMain>
</PropertyGroup>

<!-- Set the default RuntimeIdentifier if not already specified. -->
Expand Down
85 changes: 77 additions & 8 deletions dotnet/targets/Xamarin.Shared.Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@
_DetectAppManifest;
_ReadAppManifest;
_WriteAppManifest;
_ComputeManagedAssemblyToLink;
</_ComputeLinkerArgumentsDependsOn>
</PropertyGroup>

Expand Down Expand Up @@ -560,6 +561,10 @@
<_ExtraTrimmerArgs Condition="'$(Registrar)' == 'managed-static'">$(_ExtraTrimmerArgs) --feature ObjCRuntime.Runtime.IsManagedStaticRegistrar true</_ExtraTrimmerArgs>
<_ExtraTrimmerArgs Condition="'$(Registrar)' != 'managed-static'">$(_ExtraTrimmerArgs) --feature ObjCRuntime.Runtime.IsManagedStaticRegistrar false</_ExtraTrimmerArgs>

<!-- Set NativeAOT value -->
<_ExtraTrimmerArgs Condition="'$(_XamarinRuntime)' == 'NativeAOT'">$(_ExtraTrimmerArgs) --feature ObjCRuntime.Runtime.IsNativeAOT true</_ExtraTrimmerArgs>
<_ExtraTrimmerArgs Condition="'$(_XamarinRuntime)' != 'NativeAOT'">$(_ExtraTrimmerArgs) --feature ObjCRuntime.Runtime.IsNativeAOT false</_ExtraTrimmerArgs>

<!-- Enable serialization discovery. Ref: https://github.com/xamarin/xamarin-macios/issues/15676 -->
<_ExtraTrimmerArgs>$(_ExtraTrimmerArgs) --enable-serialization-discovery</_ExtraTrimmerArgs>

Expand Down Expand Up @@ -631,8 +636,8 @@
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.GenerateMainStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.GenerateReferencesStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.GatherFrameworksStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.ComputeNativeBuildFlagsStep" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.ComputeAOTArguments" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.ComputeNativeBuildFlagsStep" Condition="'$(_XamarinRuntime)' != 'NativeAOT'" />
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.ComputeAOTArguments" Condition="'$(_XamarinRuntime)' != 'NativeAOT'" />
<!-- Must be the last step. -->
<_TrimmerCustomSteps Include="$(_AdditionalTaskAssembly)" Type="Xamarin.Linker.DoneStep" />

Expand Down Expand Up @@ -783,8 +788,7 @@
</ItemGroup>
</Target>


<Target Name="_LoadLinkerOutput" DependsOnTargets="ComputeFilesToPublish" Condition="'$(IsMacEnabled)' == 'true'">
<Target Name="_LoadLinkerOutput" DependsOnTargets="_XamarinILLink;ComputeFilesToPublish" Condition="'$(IsMacEnabled)' == 'true'">
<!-- Load _MainFile -->
<ReadItemsFromFile SessionId="$(BuildSessionId)" File="$(_LinkerItemsDirectory)/_MainFile.items" Condition="Exists('$(_LinkerItemsDirectory)/_MainFile.items')">
<Output TaskParameter="Items" ItemName="_MainFile" />
Expand Down Expand Up @@ -936,7 +940,7 @@

<_AOTInputDirectory>$(_IntermediateNativeLibraryDir)aot-input/</_AOTInputDirectory>
<_AOTOutputDirectory>$(_IntermediateNativeLibraryDir)aot-output/</_AOTOutputDirectory>
<_IsDedupEnabled Condition="'$(_IsDedupEnabled)' == ''">true</_IsDedupEnabled>
<_IsDedupEnabled Condition="'$(_IsDedupEnabled)' == '' and '$(_XamarinRuntime)' == 'MonoVM'">true</_IsDedupEnabled>
<_DedupAssembly Condition="'$(_RunAotCompiler)' == 'true' And '$(IsMacEnabled)' == 'true' And '$(_IsDedupEnabled)' == 'true'">$(IntermediateOutputPath)aot-instances.dll</_DedupAssembly>

<_LibMonoLinkMode Condition="'$(_LibMonoLinkMode)' == '' And ('$(ComputedPlatform)' != 'iPhone' Or '$(_PlatformName)' == 'macOS')">dylib</_LibMonoLinkMode>
Expand Down Expand Up @@ -1124,6 +1128,55 @@
</ItemGroup>
</Target>

<Target Name="_XamarinILLink"
Inputs="@(ManagedAssemblyToLink)"
Outputs="@(ManagedAssemblyToLink->'$(IntermediateLinkDir)%(Filename)%(Extension)')"
DependsOnTargets="_RunILLink"
Condition="'$(_XamarinRuntime)' == 'NativeAOT'" />

<Target Name="_XamarinComputeIlcCompileInputs">
<PropertyGroup>
<_SystemModuleName>System.Private.CoreLib</_SystemModuleName>
<NativeLib>static</NativeLib>
</PropertyGroup>

<ItemGroup>
<TrimmerRootAssembly Remove="@(TrimmerRootAssembly)" />

<ManagedBinary Include="$(IntermediateLinkDir)$(TargetName)$(TargetExt)" />
<!-- Include SDK assemblies for ILC -->
<ManagedAssemblyReference Include="@(PrivateSdkAssemblies)" />
<!-- Include assemblies trimmed by ILLink with exception of SPC and application assembly -->
<ManagedAssemblyReference Include="@(ManagedAssemblyToLink->'$(IntermediateLinkDir)%(Filename)%(Extension)')"
Condition="'%(Filename)' != '$(_SystemModuleName)' and '%(Filename)' != '$(TargetName)' and Exists('$(IntermediateLinkDir)%(Filename)%(Extension)')"/>

<!-- Set inputs -->
<IlcCompileInput Include="@(ManagedBinary)" />
<IlcReference Include="@(ManagedAssemblyReference)" />
<DirectPInvoke Include="__Internal" />
<!-- Export UCO entrypoints from all assemblies, SPC is included by default -->
<!-- TODO: MSR could expose which assemblies need to be included in this list -->
<UnmanagedEntryPointsAssembly Include="@(ManagedAssemblyReference->'%(Filename)')" Exclude="$(_SystemModuleName)" />

<!-- Set outputs -->
<_NativeExecutableObjectFiles Include="$(NativeObject)" />
</ItemGroup>
</Target>

<!-- Override the NativeAOT targets -->
<Target Name="_ComputeManagedAssemblyForILLink" Condition="'$(_XamarinRuntime)' == 'NativeAOT'" />
<Target Name="ComputeLinkedFilesToPublish" Condition="'$(_XamarinRuntime)' == 'NativeAOT'" />
<Target Name="LinkNative" Condition="'$(_XamarinRuntime)' == 'NativeAOT'" />
<Target Name="CopyNativeBinary" Condition="'$(_XamarinRuntime)' == 'NativeAOT'" />

<Target Name="_ExcludeBundlingUnusedFiles" Condition="'$(_XamarinRuntime)' == 'NativeAOT'"
DependsOnTargets="_GetCurrentAndPriorPublishFileWrites"
BeforeTargets="_IncrementalCleanPublishDirectory" >
<ItemGroup>
<ResolvedFileToPublish Remove="@(ResolvedFileToPublish)" Condition="'%(Extension)' == '.dll' or '%(Extension)' == '.dylib'" />
</ItemGroup>
</Target>

<PropertyGroup>
<_GenerateBindingsDependsOn>
_ComputeBindingVariables;
Expand Down Expand Up @@ -1168,6 +1221,10 @@
_ComputeNativeExecutableInputs;
_AOTCompile;
</_CompileNativeExecutableDependsOn>
<_CompileNativeExecutableDependsOn Condition="'$(PublishAot)' == 'true'">
$(_CompileNativeExecutableDependsOn);
IlcCompile;
</_CompileNativeExecutableDependsOn>
</PropertyGroup>

<Target Name="_CompileNativeExecutable"
Expand Down Expand Up @@ -1213,23 +1270,35 @@
<Target Name="_ComputeLinkNativeExecutableInputs" Condition="'$(IsMacEnabled)' == 'true'">
<ItemGroup>
<_XamarinMainLibraries Include="$(_XamarinNativeLibraryDirectory)/$(_LibXamarinName)" />
<!-- Take collected static libs from ILC build integration targets -->
<_NativeAOTLibrary Include="@(LinkerArg)" Condition="'%(Extension)' == '.a'" />

<!-- Link with the libraries shipped with the mono runtime pack -->
<_XamarinMainLibraries Include="@(_MonoLibrary)" />
<_XamarinMainLibraries Condition="'$(_XamarinRuntime)' == 'MonoVM'" Include="@(_MonoLibrary)" />

<!-- Link with the libraries shipped with the NativeAOT runtime pack -->
<_XamarinMainLibraries Condition="'$(_XamarinRuntime)' == 'NativeAOT'" Include="@(_NativeAOTLibrary)" />

<!-- The frameworks we need to link with (both weakly and normally) -->
<_NativeExecutableFrameworks Include="@(_LinkerFrameworks)" />

<!-- CFNetwork is required by xamarin_start_wwan -->
<_NativeExecutableFrameworks Include="CFNetwork" Condition="'$(_PlatformName)' == 'iOS'" />

<!-- Mono requires zlib, iconv, and the "Compression framework" -->
<!-- Mono and NativeAOT require zlib, iconv, and the "Compression framework" -->
<_MainLinkerFlags Include="-lz" />
<_MainLinkerFlags Include="-liconv" />
<_MainLinkerFlags Include="-lcompression" />

<!-- NativeAOT requires setting preallocated thungs and icucore -->
<_MainLinkerFlags Condition="'$(_XamarinRuntime)' == 'NativeAOT'" Include="-Wl,-segprot,__THUNKS,rx,rx" />
<_MainLinkerFlags Condition="'$(_XamarinRuntime)' == 'NativeAOT'" Include="-licucore" />

<!-- Here we must add all the files that should make us (re-)link the executable -->
<_LinkNativeExecutableInputs Include="@(_NativeExecutableObjectFiles)" />
<_LinkNativeExecutableInputs Include="@(_XamarinMainLibraries)" />
<_FileNativeReference Remove="@(_FileNativeReference)" />
<_FileNativeReference Include="@(NativeLibrary)" />
<_LinkNativeExecutableInputs Include="@(_FileNativeReference)" />
</ItemGroup>
</Target>
Expand Down Expand Up @@ -1477,7 +1546,7 @@
Condition=" '$(_PlatformName)' != 'macOS' And
'$(InvariantGlobalization)' != 'true' And
'%(ResolvedFileToPublish.Filename)%(ResolvedFileToPublish.Extension)' == '$(_GlobalizationDataFile)' And
'%(ResolvedFileToPublish.NuGetPackageId)' == '$(_MonoNugetPackageId)'
('%(ResolvedFileToPublish.NuGetPackageId)' == '$(_MonoNugetPackageId)' or '$(_XamarinRuntime)' == 'NativeAOT')
"
PublishFolderType="Assembly"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public override bool Execute ()
arguments.Add ("clang");
arguments.Add ("-g");

arguments.Add ("-D CORECLR_RUNTIME");

var arch = info.GetMetadata ("Arch");

switch (Platform) {
Expand Down
8 changes: 8 additions & 0 deletions runtime/coreclr-bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,9 @@
bool
xamarin_bridge_vm_initialize (int propertyCount, const char **propertyKeys, const char **propertyValues)
{
#if NATIVEAOT
return true;
#else
int rv;

int combinedPropertyCount = 0;
Expand Down Expand Up @@ -510,6 +513,7 @@
LOG_CORECLR (stderr, "xamarin_vm_initialize (%i, %p, %p): rv: %i domainId: %i handle: %p\n", combinedPropertyCount, combinedPropertyKeys, combinedPropertyValues, rv, coreclr_domainId, coreclr_handle);

return rv == 0;
#endif
}

void
Expand Down Expand Up @@ -556,9 +560,11 @@
void
xamarin_bridge_register_product_assembly (GCHandle* exception_gchandle)
{
#if !defined (NATIVEAOT)
MonoAssembly *assembly;
assembly = xamarin_open_and_register (PRODUCT_DUAL_ASSEMBLY, exception_gchandle);
xamarin_mono_object_release (&assembly);
#endif
}

MonoMethod *
Expand Down Expand Up @@ -740,6 +746,7 @@
return rv;
}

#if !defined (NATIVEAOT)
int
mono_jit_exec (MonoDomain * domain, MonoAssembly * assembly, int argc, const char** argv)
{
Expand Down Expand Up @@ -769,6 +776,7 @@

return (int) exitCode;
}
#endif

MonoGHashTable *
mono_g_hash_table_new_type (GHashFunc hash_func, GEqualFunc key_equal_func, MonoGHashGCType type)
Expand Down
5 changes: 5 additions & 0 deletions runtime/monotouch-main.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ - (void) memoryWarning: (NSNotification *) sender
if (xamarin_register_assemblies != NULL)
xamarin_register_assemblies ();

#if !defined (NATIVEAOT)
if (xamarin_executable_name) {
assembly = xamarin_open_and_register (xamarin_executable_name, &exception_gchandle);
xamarin_process_fatal_exception_gchandle (exception_gchandle, "An exception occurred while opening the main executable");
Expand All @@ -460,6 +461,10 @@ - (void) memoryWarning: (NSNotification *) sender
xamarin_mono_object_release (&rassembly);
xamarin_process_fatal_exception_gchandle (exception_gchandle, "An exception occurred while opening the entry assembly");
}
#else
assembly = NULL;
(void)exception_gchandle;
#endif

DEBUG_LAUNCH_TIME_PRINT ("\tAssembly register time");

Expand Down
16 changes: 16 additions & 0 deletions runtime/nativeaot-bridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@
runtime_initialize (options, exception_gchandle);
}

typedef int (*managed_entry_point)(int argc, const char* argv[]);

int
mono_jit_exec (MonoDomain * domain, MonoAssembly * assembly, int argc, const char** argv)
{
void *del;
managed_entry_point app_main;

del = dlsym (RTLD_DEFAULT, "__managed__Main");
if (del == NULL)
xamarin_assertion_message ("mono_jit_exec: failed to load managed_entry_point: %s\n", dlerror ());
app_main = (managed_entry_point) del;

return app_main (argc, argv);
}

#endif // NATIVEAOT
6 changes: 3 additions & 3 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,17 @@ internal Flags FlagsInternal {
}
#endif

#if !NET || !__MACOS__
#if !NET || (!__MACOS__ && !__IOS__)
[MethodImplAttribute (MethodImplOptions.InternalCall)]
extern static void RegisterToggleRef (NSObject obj, IntPtr handle, bool isCustomType);
#endif // !NET || !__MACOS__
#endif // !NET || (!__MACOS__ && !__IOS__)

[DllImport ("__Internal")]
static extern void xamarin_release_managed_ref (IntPtr handle, [MarshalAs (UnmanagedType.I1)] bool user_type);

static void RegisterToggleReference (NSObject obj, IntPtr handle, bool isCustomType)
{
#if NET && __MACOS__
#if NET && (__MACOS__ || __IOS__)
Runtime.RegisterToggleReferenceCoreCLR (obj, handle, isCustomType);
#else
RegisterToggleRef (obj, handle, isCustomType);
Expand Down
3 changes: 2 additions & 1 deletion src/ILLink.Substitutions.ios.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<method signature="System.Void WarnOnce()" body="stub" feature="System.Diagnostics.Debugger.IsSupported" featurevalue="false" />
</type>
<type fullname="ObjCRuntime.Runtime">
<method signature="System.Boolean get_IsCoreCLR()" body="stub" value="false" />
<method signature="System.Boolean get_IsCoreCLR()" body="stub" feature="ObjCRuntime.Runtime.IsNativeAOT" featurevalue="false" value="false" />
<method signature="System.Boolean get_IsCoreCLR()" body="stub" feature="ObjCRuntime.Runtime.IsNativeAOT" featurevalue="true" value="true" />
<method signature="System.Int32 GetRuntimeArch()" body="stub" feature="ObjCRuntime.Runtime.Arch.IsSimulator" featurevalue="false" value="0" />
<method signature="System.Int32 GetRuntimeArch()" body="stub" feature="ObjCRuntime.Runtime.Arch.IsSimulator" featurevalue="true" value="1" />
<method signature="System.Boolean get_IsManagedStaticRegistrar()" body="stub" feature="ObjCRuntime.Runtime.IsManagedStaticRegistrar" featurevalue="false" value="false" />
Expand Down
2 changes: 2 additions & 0 deletions tools/common/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,8 @@ public bool IsInterpreted (string assembly)
public bool IsAOTCompiled (string assembly)
{
#if NET
if (XamarinRuntime == XamarinRuntime.NativeAOT)
return true;
if (Platform == ApplePlatform.MacOSX)
return false; // AOT on .NET for macOS hasn't been implemented yet.
#else
Expand Down
4 changes: 2 additions & 2 deletions tools/common/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ void GenerateIOSMain (StringWriter sw, Abi abi)

register_assemblies.AppendLine ("\tGCHandle exception_gchandle = INVALID_GCHANDLE;");
foreach (var s in assemblies) {
if (!s.IsAOTCompiled)
if (!s.IsAOTCompiled || App.XamarinRuntime == XamarinRuntime.NativeAOT)
continue;

var info = s.AssemblyDefinition.Name.Name;
Expand Down Expand Up @@ -797,7 +797,7 @@ void GenerateIOSMain (StringWriter sw, Abi abi)
} else {
sw.WriteLine ("\tmono_jit_set_aot_mode (MONO_AOT_MODE_INTERP);");
}
} else if (app.IsDeviceBuild) {
} else if (app.IsDeviceBuild && App.XamarinRuntime == XamarinRuntime.MonoVM) {
sw.WriteLine ("\tmono_jit_set_aot_mode (MONO_AOT_MODE_FULL);");
} else if (app.Platform == ApplePlatform.MacCatalyst && ((abi & Abi.ARM64) == Abi.ARM64)) {
sw.WriteLine ("\tmono_jit_set_aot_mode (MONO_AOT_MODE_FULL);");
Expand Down
1 change: 1 addition & 0 deletions tools/common/XamarinRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace Xamarin.Bundler {
public enum XamarinRuntime {
MonoVM,
CoreCLR,
NativeAOT,
}
}
16 changes: 13 additions & 3 deletions tools/dotnet-linker/Steps/GenerateReferencesStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ protected override void TryEndProcess ()
Configuration.WriteOutputForMSBuild ("_ReferencesFile", items);
break;
case SymbolMode.Linker:
foreach (var symbol in required_symbols) {
var item = new MSBuildItem ("-u" + symbol.Prefix + symbol.Name);
items.Add (item);
if (App.XamarinRuntime != XamarinRuntime.NativeAOT)
{
foreach (var symbol in required_symbols) {
var item = new MSBuildItem ("-u" + symbol.Prefix + symbol.Name);
items.Add (item);
}
}
else
{
// FIXME:
items.Add (new MSBuildItem ("-u_xamarin_objcruntime_runtime_nativeaotinitialize"));
items.Add (new MSBuildItem ("-u___managed__Main" ));
items.Add (new MSBuildItem ("-u_NativeAOT_StaticInitialization" ));
}
Configuration.WriteOutputForMSBuild ("_ReferencesLinkerFlags", items);
break;
Expand Down

0 comments on commit 98a8f4e

Please sign in to comment.