Skip to content

Commit e07c9b0

Browse files
[One .NET] AOT support
Helpful reading: * https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/tasks/AotCompilerTask/MonoAOTCompiler.cs * https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/mono/netcore/nuget/Microsoft.NET.Runtime.MonoAOTCompiler.Task/README.md TODO / limitations: * We only have the osx-x64 host packages * I have not updated the installers yet. The AOT packs are placed in `~/android-toolchain/dotnet` for local testing. * Many other `TODO` comments To make this work, I moved the existing `<Aot/>` MSBuild task calls to a new `_AndroidAot` MSBuild target in `Xamarin.Android.Legacy.targets`. In the .NET 6 targets, there is a *different* `_AndroidAot` MSBuild target that runs the new `<MonoAOTCompiler/>` MSBuild task. In order to acquire the AOT packages, I created a new `mono-aot-compiler.proj` that is invoked such as: <Exec Command="$(DotNetPreviewTool) build @(_GlobalProperties, ' ') &quot;$(MSBuildThisFileDirectory)..\mono-aot-compiler\mono-aot-compiler.proj&quot;" EnvironmentVariables="NUGET_PACKAGES=$(DotNetPreviewPath)packs" /> Setting `$NUGET_PACKAGES` allows us to extract the AOT NuGet packages directly to `dotnet/packs` when this project is restored. Then we conditionally import the Sdk: <Import Project="..\build\Microsoft.NET.Runtime.MonoAOTCompiler.Task.props" Sdk="Microsoft.NET.Runtime.MonoAOTCompiler.Task" Condition=" '$(AotAssemblies)' == 'true' " /> Which allows us to use `$(MonoAOTCompilerTaskAssemblyPath)`: <UsingTask Condition=" '$(AotAssemblies)' == 'true' " TaskName="MonoAOTCompiler" AssemblyFile="$(MonoAOTCompilerTaskAssemblyPath)" /> I still need to add the new packs to our installers.
1 parent f2e0df4 commit e07c9b0

File tree

12 files changed

+195
-66
lines changed

12 files changed

+195
-66
lines changed

Configuration.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<!-- Version number from: https://github.com/dotnet/installer#installers-and-binaries -->
7878
<DotNetPreviewVersionBand Condition=" '$(DotNetPreviewVersionBand)' == '' ">6.0.100</DotNetPreviewVersionBand>
7979
<DotNetPreviewVersionFull Condition=" '$(DotNetPreviewVersionFull)' == '' ">$(DotNetPreviewVersionBand)-alpha.1.21064.27</DotNetPreviewVersionFull>
80+
<MonoAOTCompilerVersion Condition=" '$(MonoAOTCompilerVersion)' == '' ">6.0.0-preview.1.21072.5</MonoAOTCompilerVersion>
8081
<WixToolPath Condition=" '$(WixToolPath)' == '' ">$(AndroidToolchainDirectory)\wix\</WixToolPath>
8182
<AndroidCmakeVersion Condition=" '$(AndroidCmakeVersion)' == '' ">3.10.2</AndroidCmakeVersion>
8283
<AndroidCmakeVersionPath Condition=" '$(AndroidCmakeVersionPath)' == '' ">$(AndroidCmakeVersion).4988404</AndroidCmakeVersionPath>

build-tools/create-packs/Directory.Build.targets

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,16 @@
7070
</ItemGroup>
7171
</Target>
7272

73+
<Target Name="GetMonoAOTCompilers">
74+
<!-- Use $NUGET_PACKAGES to extract directly to the dotnet/packs directory -->
75+
<Exec
76+
Command="$(DotNetPreviewTool) build @(_GlobalProperties, ' ') &quot;$(MSBuildThisFileDirectory)..\mono-aot-compiler\mono-aot-compiler.proj&quot;"
77+
EnvironmentVariables="NUGET_PACKAGES=$(DotNetPreviewPath)packs"
78+
/>
79+
</Target>
80+
7381
<Target Name="CreateAllPacks"
74-
DependsOnTargets="DeleteExtractedWorkloadPacks;_SetGlobalProperties">
82+
DependsOnTargets="DeleteExtractedWorkloadPacks;_SetGlobalProperties;GetMonoAOTCompilers">
7583
<RemoveDir Directories="$(XamarinAndroidSourcePath)bin\Build$(Configuration)\nupkgs" />
7684
<Exec Command="$(DotNetPreviewTool) pack @(_GlobalProperties, ' ') -p:AndroidRID=android.21-arm -p:AndroidABI=armeabi-v7a &quot;$(MSBuildThisFileDirectory)Microsoft.Android.Runtime.proj&quot;" />
7785
<Exec Command="$(DotNetPreviewTool) pack @(_GlobalProperties, ' ') -p:AndroidRID=android.21-arm64 -p:AndroidABI=arm64-v8a &quot;$(MSBuildThisFileDirectory)Microsoft.Android.Runtime.proj&quot;" />
@@ -98,6 +106,7 @@
98106
<_WLTemplates Include="$(XamarinAndroidSourcePath)bin\Build$(Configuration)\nupkgs\Microsoft.Android.Templates.*.nupkg" />
99107
<!-- Runtime packs are not yet supported by workloads -->
100108
<!-- <_WLPacks Include="$(XamarinAndroidSourcePath)bin\Build$(Configuration)\nupkgs\Microsoft.Android.Runtime.*.nupkg" /> -->
109+
<_AotPacks Include="$(XamarinAndroidSourcePath)bin\Build$(Configuration)\nupkgs\microsoft.netcore.app.runtime.aot.*.cross.*.nupkg" />
101110
</ItemGroup>
102111
<PropertyGroup>
103112
<_WLPackVersion>@(_WLManifest->'%(Filename)'->Replace('Microsoft.NET.Workload.Android.', ''))</_WLPackVersion>
@@ -110,12 +119,18 @@
110119
SourceFiles="@(_WLPacks)"
111120
DestinationFolder="$(DotNetPreviewPath)packs\$([System.String]::Copy('%(_WLPacks.Filename)').Replace('.$(_WLPackVersion)', ''))\$(_WLPackVersion)"
112121
/>
122+
<Unzip
123+
SourceFiles="@(_AotPacks)"
124+
DestinationFolder="$(DotNetPreviewPath)packs\$([System.String]::Copy('%(_AotPacks.Filename)').Replace('.$(MonoAOTCompilerVersion)', ''))\$(MonoAOTCompilerVersion)"
125+
/>
113126
<MakeDir Directories="$(DotNetPreviewPath)template-packs" />
114127
<Copy SourceFiles="@(_WLTemplates)" DestinationFolder="$(DotNetPreviewPath)template-packs" />
115128
<ItemGroup>
116129
<_UnixExecutables Include="$(DotNetPreviewPath)packs\Microsoft.Android.Sdk.*\*\tools\$(HostOS)\**\*.*" />
130+
<_UnixExecutables Include="$(DotNetPreviewPath)packs\microsoft.netcore.app.runtime.aot.*.cross.*\*\tools\*.*" />
117131
<_FilesToTouch Include="$(DotNetPreviewPath)sdk-manifests\$(DotNetPreviewVersionBand)\Microsoft.NET.Workload.Android\**" />
118132
<_FilesToTouch Include="$(DotNetPreviewPath)packs\$([System.String]::Copy('%(_WLPacks.Filename)').Replace('.$(_WLPackVersion)', ''))\$(_WLPackVersion)\**" />
133+
<_FilesToTouch Include="$(DotNetPreviewPath)packs\$([System.String]::Copy('%(_AotPacks.Filename)').Replace('.$(MonoAOTCompilerVersion)', ''))\$(MonoAOTCompilerVersion)\**" />
119134
</ItemGroup>
120135
<Exec
121136
Condition=" '$(HostOS)' == 'Darwin' or '$(HostOS)' == 'Linux' "
@@ -135,6 +150,7 @@
135150
<_PackFilesToDelete Include="$(DotNetPreviewPath)sdk-manifests\$(DotNetPreviewVersionBand)\Microsoft.Android.Workload\**\*.*" />
136151
<_PackFilesToDelete Include="$(DotNetPreviewPath)sdk-manifests\$(DotNetPreviewVersionBand)\Microsoft.NET.Workload.Android\**\*.*" />
137152
<_PackFilesToDelete Include="$(DotNetPreviewPath)packs\Microsoft.Android*\**\*.*" />
153+
<_PackFilesToDelete Include="$(DotNetPreviewPath)packs\microsoft.netcore.app.runtime.aot.*.cross.*\**\*.*" />
138154
<_PackFilesToDelete Include="$(DotNetPreviewPath)template-packs\Microsoft.Android.Templates.*.nupkg" />
139155
</ItemGroup>
140156
<RemoveDir Directories="%(_PackFilesToDelete.RootDir)%(_PackFilesToDelete.Directory)" />

build-tools/create-packs/Microsoft.Android.Sdk.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and
102102
<PropertyGroup>
103103
<AndroidNETSdkVersion>$(AndroidPackVersionLong)</AndroidNETSdkVersion>
104104
<XamarinAndroidVersion>$(AndroidPackVersionLong)</XamarinAndroidVersion>
105+
<MonoAOTCompilerVersion>$(MonoAOTCompilerVersion)</MonoAOTCompilerVersion>
105106
</PropertyGroup>
106107
<ItemGroup>
107108
<KnownFrameworkReference

build-tools/create-packs/Microsoft.NET.Workload.Android.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ workload manifest pack containing information about the various Microsoft.Androi
3333
<ReplaceFileContents
3434
SourceFile="$(XamarinAndroidSourcePath)src\Xamarin.Android.Build.Tasks\Microsoft.NET.Workload.Android\WorkloadManifest.in.json"
3535
DestinationFile="$(WorkloadManifestJsonPath)"
36-
Replacements="@SDK_PACK_VERSION@=$(AndroidPackVersionLong);@REF_PACK_VERSION@=$(AndroidPackVersionLong);@TEMPLATE_PACK_VERSION@=$(AndroidPackVersionLong);">
36+
Replacements="@SDK_PACK_VERSION@=$(AndroidPackVersionLong);@REF_PACK_VERSION@=$(AndroidPackVersionLong);@TEMPLATE_PACK_VERSION@=$(AndroidPackVersionLong);@MONO_AOT_VERSION@=$(MonoAOTCompilerVersion);">
3737
</ReplaceFileContents>
3838

3939
<ItemGroup>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.Build.NoTargets">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
</PropertyGroup>
5+
6+
<Import Project="..\..\Configuration.props" />
7+
8+
<ItemGroup>
9+
<!--
10+
@(PackageDownload) with an exact version such as [6.0.0-preview.1.21072.5] is the only way to consume these packages:
11+
error NU1213: The package Microsoft.NETCore.App.Runtime.AOT.osx-x64.Cross.android-* has a package type DotnetPlatform that is incompatible with this project.
12+
-->
13+
<!-- TODO: We only have osx-x64 packs-->
14+
<PackageDownload Include="Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-arm" Version="[$(MonoAOTCompilerVersion)]" />
15+
<PackageDownload Include="Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-arm64" Version="[$(MonoAOTCompilerVersion)]" />
16+
<PackageDownload Include="Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-x64" Version="[$(MonoAOTCompilerVersion)]" />
17+
<PackageDownload Include="Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-x86" Version="[$(MonoAOTCompilerVersion)]" />
18+
<PackageDownload Include="Microsoft.NET.Runtime.MonoAOTCompiler.Task" Version="[$(MonoAOTCompilerVersion)]" />
19+
</ItemGroup>
20+
</Project>

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.After.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This file is imported *after* the Microsoft.NET.Sdk/Sdk.targets.
1818
<Import Project="..\tools\Xamarin.Android.Bindings.Core.targets" />
1919
<Import Project="..\tools\Xamarin.Android.Bindings.ClassParse.targets" />
2020
<Import Project="Microsoft.Android.Sdk.AndroidLibraries.targets" />
21+
<Import Project="Microsoft.Android.Sdk.Aot.targets" Condition=" '$(AndroidApplication)' == 'true' " />
2122
<Import Project="Microsoft.Android.Sdk.Application.targets" Condition=" '$(AndroidApplication)' == 'true' " />
2223
<Import Project="Microsoft.Android.Sdk.AssemblyResolution.targets" />
2324
<Import Project="Microsoft.Android.Sdk.ILLink.targets" />
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!--
2+
***********************************************************************************************
3+
Microsoft.Android.Sdk.Aot.targets
4+
5+
.NET 6 AOT support. You can find "legacy" Xamarin.Android AOT support
6+
in Xamarin.Android.Legacy.targets.
7+
8+
For <MonoAOTCompiler/> usage, see:
9+
* https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/tasks/AotCompilerTask/MonoAOTCompiler.cs
10+
* https://github.com/dotnet/runtime/blob/15dec9a2aa5a4236d6ba70de2e9c146867b9d2e0/src/mono/netcore/nuget/Microsoft.NET.Runtime.MonoAOTCompiler.Task/README.md
11+
12+
***********************************************************************************************
13+
-->
14+
<Project>
15+
16+
<UsingTask
17+
Condition=" '$(AotAssemblies)' == 'true' "
18+
TaskName="MonoAOTCompiler"
19+
AssemblyFile="$(MonoAOTCompilerTaskAssemblyPath)"
20+
/>
21+
22+
<Target Name="_AndroidAot"
23+
Condition=" '$(AotAssemblies)' == 'true' "
24+
Inputs="$(_BuildApkEmbedInputs)"
25+
Outputs="$(_BuildApkEmbedOutputs)">
26+
<!--
27+
TODO:
28+
* I set every [Input] with a non-existent property.
29+
* AotProfilePath is a single string, not sure how to pass @(_AotProfiles)?
30+
* Hardcoded microsoft.netcore.app.runtime.aot.osx-x64.cross
31+
* Hardcoded '.21' to be replaced with ''
32+
-->
33+
<ItemGroup>
34+
<_AotRuntimeIdentifiers Include="$([System.String]::Copy('%(_RIDs.Identity)').Replace('.21', ''))" />
35+
<_AotCompilerPaths Include="$(MSBuildThisFileDirectory)..\..\..\microsoft.netcore.app.runtime.aot.osx-x64.cross.%(_AotRuntimeIdentifiers.Identity)\$(MonoAOTCompilerVersion)\tools\mono-aot-cross" />
36+
</ItemGroup>
37+
<MonoAOTCompiler
38+
AotModulesTableLanguage="$(_AotModulesTableLanguage)"
39+
AotModulesTablePath="$(_AotModulesTablePath)"
40+
AotProfilePath="$(_AotProfilePath)"
41+
Assemblies="@(_ShrunkAssemblies->'%(FullPath)')"
42+
CompilerBinaryPath="%(_AotCompilerPaths.FullPath)"
43+
DisableParallelAot="$(_DisableParallelAot)"
44+
LLVMPath="$(_LLVMPath)"
45+
Mode="$(AndroidAotMode)"
46+
MsymPath="$(_MsymPath)"
47+
OutputType="$(_AotOutputType)"
48+
Profilers="@(_AotProfilers)"
49+
UseAotDataFile="$(_UseAotDataFile)"
50+
UseLLVM="$(EnableLLVM)">
51+
<Output TaskParameter="CompiledAssemblies" ItemName="_AotCompiledAssemblies" />
52+
<Output TaskParameter="FileWrites" ItemName="FileWrites" />
53+
</MonoAOTCompiler>
54+
</Target>
55+
</Project>

src/Xamarin.Android.Build.Tasks/Microsoft.NET.Workload.Android/WorkloadManifest.in.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"Microsoft.Android.Sdk",
88
"Microsoft.Android.Sdk.BundleTool",
99
"Microsoft.Android.Ref",
10-
"Microsoft.Android.Templates"
10+
"Microsoft.Android.Templates",
11+
"Microsoft.NET.Runtime.MonoAOTCompiler.Task",
12+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-arm",
13+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-arm64",
14+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-x64",
15+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-x86"
1116
]
1217
}
1318
},
@@ -24,6 +29,26 @@
2429
"kind": "sdk",
2530
"version": "@SDK_PACK_VERSION@"
2631
},
32+
"Microsoft.NET.Runtime.MonoAOTCompiler.Task": {
33+
"kind": "sdk",
34+
"version": "@MONO_AOT_VERSION@"
35+
},
36+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-arm": {
37+
"kind": "sdk",
38+
"version": "@MONO_AOT_VERSION@"
39+
},
40+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-arm64": {
41+
"kind": "sdk",
42+
"version": "@MONO_AOT_VERSION@"
43+
},
44+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-x64": {
45+
"kind": "sdk",
46+
"version": "@MONO_AOT_VERSION@"
47+
},
48+
"Microsoft.NetCore.App.Runtime.AOT.osx-x64.Cross.android-x86": {
49+
"kind": "sdk",
50+
"version": "@MONO_AOT_VERSION@"
51+
},
2752
"Microsoft.Android.Ref": {
2853
"kind": "framework",
2954
"version": "@REF_PACK_VERSION@"

src/Xamarin.Android.Build.Tasks/Microsoft.NET.Workload.Android/WorkloadManifest.targets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
Condition=" '$(TargetPlatformIdentifier)' == 'android' " />
44
<Import Project="Sdk.targets" Sdk="Microsoft.Android.Sdk.BundleTool"
55
Condition=" '$(AndroidPackageFormat)' == 'aab' " />
6+
<Import Project="..\build\Microsoft.NET.Runtime.MonoAOTCompiler.Task.props" Sdk="Microsoft.NET.Runtime.MonoAOTCompiler.Task"
7+
Condition=" '$(AotAssemblies)' == 'true' " />
68
</Project>

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/XASdkTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ public void DotNetBuild (string runtimeIdentifiers, bool isRelease)
348348
},
349349
}
350350
};
351+
//TODO: take this out and either make new tests or enable the AOT category
352+
if (isRelease)
353+
proj.SetProperty ("AotAssemblies", "true");
351354
proj.OtherBuildItems.Add (new AndroidItem.InputJar ("javaclasses.jar") {
352355
BinaryContent = () => ResourceData.JavaSourceJarTestJar,
353356
});

0 commit comments

Comments
 (0)