Skip to content

Commit a35f711

Browse files
[NativeAOT] MSBuild-related logic to get projects to build
This is an initial step to get the NativeAOT build working the same as iOS does. At a high level, the changes are: * Run `ILLink` with our existing trimmer/linker pipeline. * Temporarily change our RID to `linux-bionic-arm64`. * Pass the output to `ILC`, the NativeAOT compiler. * Turn the RID back to `android-arm64`. A list of MSBuild/workload changes required so far: * `$(UseMonoRuntime)` is false when `$(PublishAot)` is true * Set `$(_IsPublishing)` as NativeAOT expects `dotnet publish` to be the only way NativeAOT is used. Setting this in our targets allows the proper `*.NativeAOT.*` packs to restore inside of a `dotnet build`. * `$(SelfContained)=true`, seems to be required for the `linux-bionic-arm64` runtime packs to restore. All Android apps are self-contained, so this might be fine. * Create a new `Microsoft.Android.Sdk.NativeAOT.targets` file that uses `$(_AndroidNdkDirectory)` to set various properties for NativeAOT's build (ILC). * `HACK:` set `$(_targetOS)` to `linux` to make the NativeAOT build attempt to run. `--targetos:android` results in a build error that says Android is not supported. * Disable `$(RunAOTCompilation)`, Mono's AOT compiler and the `<RemoveRegisterAttribute/>` MSBuild task. * Disable `$(AndroidEnableMarshalMethods)` to be re-enabled in a future PR. A list of blockers or things that don't work yet: * NativeAOT apps won't launch yet (future changes for that) * The NativeAOT cross compiler doesn't run on Windows: Microsoft.NETCore.Native.Publish.targets(61,5): Cross-OS native compilation is not supported. Microsoft.NETCore.Native.Unix.targets(296,5): error : Platform linker ('C:\Android\android-sdk\ndk\26.3.11579264\toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang++' or 'gcc') not found in PATH. Ensure you have all the required prerequisites documented at https://aka.ms/nativeaot-prerequisites. * You can only target `-r android-arm64` as there are no other `linux-bionic-*` runtime packs yet. * `-p:PublishAotUsingRuntimePack=true` is currently required, but I will investigate if we should just set this by default.
1 parent fc88da7 commit a35f711

6 files changed

Lines changed: 115 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ _ResolveAssemblies MSBuild target.
9797
;_OuterOutputPath=$(OutputPath)
9898
;_OuterIntermediateOutputPath=$(IntermediateOutputPath)
9999
;_OuterCustomViewMapFile=$(_CustomViewMapFile)
100+
;_AndroidNdkDirectory=$(_AndroidNdkDirectory)
100101
</_AdditionalProperties>
101102
<_AndroidBuildRuntimeIdentifiersInParallel Condition=" '$(_AndroidBuildRuntimeIdentifiersInParallel)' == '' ">true</_AndroidBuildRuntimeIdentifiersInParallel>
102103
</PropertyGroup>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<AndroidUseIntermediateDesignerFile Condition=" '$(AndroidUseDesignerAssembly)' == 'True' ">false</AndroidUseIntermediateDesignerFile>
1414
<AndroidUseIntermediateDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' == '' ">$(AndroidGenerateResourceDesigner)</AndroidUseIntermediateDesignerFile>
1515
<AllowSelfContainedWithoutRuntimeIdentifier Condition =" '$(AllowSelfContainedWithoutRuntimeIdentifier)' == '' ">true</AllowSelfContainedWithoutRuntimeIdentifier>
16+
<SelfContained Condition=" '$(SelfContained)' == '' ">true</SelfContained>
1617
<GenerateDependencyFile Condition=" '$(GenerateDependencyFile)' == '' ">false</GenerateDependencyFile>
1718
<CopyLocalLockFileAssemblies Condition=" '$(CopyLocalLockFileAssemblies)' == '' ">false</CopyLocalLockFileAssemblies>
1819
<ComputeNETCoreBuildOutputFiles Condition=" '$(ComputeNETCoreBuildOutputFiles)' == '' ">false</ComputeNETCoreBuildOutputFiles>
@@ -23,7 +24,10 @@
2324
See: https://github.com/dotnet/sdk/blob/955c0fc7b06e2fa34bacd076ed39f61e4fb61716/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L16
2425
-->
2526
<_GetChildProjectCopyToPublishDirectoryItems>false</_GetChildProjectCopyToPublishDirectoryItems>
27+
<UseMonoRuntime Condition=" '$(PublishAot)' == 'true' and '$(UseMonoRuntime)' == '' ">false</UseMonoRuntime>
2628
<UseMonoRuntime Condition=" '$(UseMonoRuntime)' == '' ">true</UseMonoRuntime>
29+
<!-- HACK: make dotnet restore include Microsoft.NETCore.App.Runtime.NativeAOT.linux-bionic-arm64 -->
30+
<_IsPublishing Condition=" '$(_IsPublishing)' == '' and '$(PublishAot)' == 'true' ">true</_IsPublishing>
2731

2832
<!-- Use $(AndroidMinimumSupportedApiLevel) for $(SupportedOSPlatformVersion) if unset -->
2933
<SupportedOSPlatformVersion Condition=" '$(SupportedOSPlatformVersion)' == '' ">$(AndroidMinimumSupportedApiLevel)</SupportedOSPlatformVersion>
@@ -89,8 +93,8 @@
8993
<RuntimeIdentifiers Condition=" '$(RuntimeIdentifier)' == '' And '$(RuntimeIdentifiers)' == '' ">android-arm64;android-x64</RuntimeIdentifiers>
9094
<RuntimeIdentifier Condition=" '$(RuntimeIdentifiers)' != '' And '$(RuntimeIdentifier)' != '' " />
9195
<GenerateApplicationManifest Condition=" '$(GenerateApplicationManifest)' == '' ">true</GenerateApplicationManifest>
92-
<!-- Default to AOT in Release mode -->
93-
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == '' and '$(Configuration)' == 'Release' ">true</RunAOTCompilation>
96+
<!-- Default to Mono's AOT in Release mode -->
97+
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == '' and '$(Configuration)' == 'Release' and '$(PublishAot)' != 'true' ">true</RunAOTCompilation>
9498
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' and '$(AotAssemblies)' == 'true' ">true</RunAOTCompilation>
9599
<RunAOTCompilation Condition=" '$(RunAOTCompilation)' == '' ">false</RunAOTCompilation>
96100
<_AndroidXA1029 Condition=" '$(AotAssemblies)' != '' ">true</_AndroidXA1029>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!--
2+
***********************************************************************************************
3+
Microsoft.Android.Sdk.NativeAOT.targets
4+
5+
This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
6+
***********************************************************************************************
7+
-->
8+
<Project>
9+
10+
<!-- Make IlcCompile depend on the trimmer -->
11+
<PropertyGroup>
12+
<IlcCompileDependsOn>
13+
_AndroidBeforeIlcCompile;
14+
SetupOSSpecificProps;
15+
PrepareForILLink;
16+
ILLink;
17+
ComputeIlcCompileInputs;
18+
_AndroidComputeIlcCompileInputs;
19+
$(IlcCompileDependsOn)
20+
</IlcCompileDependsOn>
21+
</PropertyGroup>
22+
23+
<Target Name="_AndroidBeforeIlcCompile" BeforeTargets="SetupProperties">
24+
<!-- Example settings from: https://github.com/jonathanpeppers/Android-NativeAOT/blob/ea69d122cdc7de67aa6a5db14b7e560763c63cdd/DotNet/libdotnet.targets -->
25+
<PropertyGroup>
26+
<_NdkSysrootAbi>aarch64-linux-android</_NdkSysrootAbi>
27+
<_NdkClangPrefix>aarch64-linux-android21-</_NdkClangPrefix>
28+
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64</_NdkPrebuiltAbi>
29+
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
30+
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64</_NdkPrebuiltAbi>
31+
<_NdkSysrootDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)</_NdkSysrootDir>
32+
<_NdkBinDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin/</_NdkBinDir>
33+
<CppCompilerAndLinker>$(_NdkBinDir)$(_NdkClangPrefix)clang++</CppCompilerAndLinker>
34+
<CppLinker>$(CppCompilerAndLinker)</CppLinker>
35+
<ObjCopyName>$(_NdkBinDir)llvm-objcopy</ObjCopyName>
36+
37+
<!-- Example settings from: https://github.com/xamarin/xamarin-macios/blob/c43d4ea40bc777969e3b158cf46446df292d8449/dotnet/targets/Xamarin.Shared.Sdk.targets#L541-L550 -->
38+
<RunILLink>true</RunILLink>
39+
<!--
40+
We want to suppress warnings from trimmer and only show warnings from ILC.
41+
Otherwise, you would get 2x for every warning.
42+
-->
43+
<_OriginalSuppressTrimAnalysisWarnings>$(SuppressTrimAnalysisWarnings)</_OriginalSuppressTrimAnalysisWarnings>
44+
<SuppressTrimAnalysisWarnings>true</SuppressTrimAnalysisWarnings>
45+
46+
<!-- The list of hacks below should go away when we have NativeAOT.android-* packs -->
47+
<!-- HACK: we are android-arm64, so this is required for the right path(s) to be found -->
48+
<_OriginalRuntimeIdentifier>$(RuntimeIdentifier)</_OriginalRuntimeIdentifier>
49+
<RuntimeIdentifier Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">linux-bionic-arm64</RuntimeIdentifier>
50+
<!-- HACK: -targetos:android results in error, so use linux -->
51+
<_targetOS>linux</_targetOS>
52+
<!-- HACK: prevents libSystem.Net.Security.Native.a from being added -->
53+
<_linuxLibcFlavor>bionic</_linuxLibcFlavor>
54+
</PropertyGroup>
55+
</Target>
56+
57+
<Target Name="_AndroidComputeIlcCompileInputs">
58+
<PropertyGroup>
59+
<!-- Turn trimmer warnings back to original value -->
60+
<SuppressTrimAnalysisWarnings>$(_OriginalSuppressTrimAnalysisWarnings)</SuppressTrimAnalysisWarnings>
61+
</PropertyGroup>
62+
<ItemGroup>
63+
<!-- Give ILLink's output to ILC -->
64+
<IlcCompileInput Remove="@(IlcCompileInput)" />
65+
<IlcCompileInput Include="$(IntermediateLinkDir)$(TargetName)$(TargetExt)" />
66+
<IlcReference Remove="@(IlcReference)" />
67+
<IlcReference Include="@(PrivateSdkAssemblies)" />
68+
<IlcReference Include="@(ManagedAssemblyToLink->'$(IntermediateLinkDir)%(Filename)%(Extension)')" Condition="Exists('$(IntermediateLinkDir)%(Filename)%(Extension)')" Exclude="@(IlcCompileInput)" />
69+
</ItemGroup>
70+
</Target>
71+
72+
<Target Name="_AndroidAfterLinkNative" AfterTargets="LinkNative">
73+
<PropertyGroup>
74+
<!-- Turn $(RuntimeIdentifier) back to original value -->
75+
<RuntimeIdentifier>$(_OriginalRuntimeIdentifier)</RuntimeIdentifier>
76+
</PropertyGroup>
77+
</Target>
78+
79+
</Project>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727
<Import Project="Microsoft.Android.Sdk.DefaultProperties.targets" />
2828
<Import Project="$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props"
2929
Condition="Exists('$(MSBuildThisFileDirectory)..\tools\Xamarin.Android.Common.Debugging.props')"/>
30+
<Import Project="Microsoft.Android.Sdk.NativeAOT.targets" Condition=" '$(PublishAot)' == 'true' " />
3031

3132
</Project>

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ public void BuildBasicApplication ([Values (true, false)] bool isRelease, [Value
105105
}
106106
}
107107

108+
[Test]
109+
public void NativeAOT ()
110+
{
111+
if (IsWindows) {
112+
// Microsoft.NETCore.Native.Publish.targets(61,5): Cross-OS native compilation is not supported.
113+
// Set $(DisableUnsupportedError)=true, Microsoft.NETCore.Native.Unix.targets(296,5): error : Platform linker ('C:\Android\android-sdk\ndk\26.3.11579264\toolchains/llvm/prebuilt/windows-x86_64/bin/aarch64-linux-android21-clang++' or 'gcc') not found in PATH. Ensure you have all the required prerequisites documented at https://aka.ms/nativeaot-prerequisites.
114+
Assert.Ignore ("This test is not valid on Windows.");
115+
}
116+
117+
var proj = new XamarinAndroidApplicationProject {
118+
IsRelease = true,
119+
// Add locally downloaded NativeAOT packs
120+
ExtraNuGetConfigSources = {
121+
Path.Combine (XABuildPaths.BuildOutputDirectory, "nuget-unsigned"),
122+
}
123+
};
124+
proj.SetRuntimeIdentifier ("arm64-v8a");
125+
proj.SetProperty ("PublishAot", "true");
126+
proj.SetProperty ("PublishAotUsingRuntimePack", "true");
127+
128+
using var b = CreateApkBuilder ();
129+
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
130+
}
131+
108132
[Test]
109133
public void BuildBasicApplicationThenMoveIt ([Values (true, false)] bool isRelease)
110134
{

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
324324
<AndroidUseAssemblyStore Condition=" '$(AndroidUseAssemblyStore)' == '' ">true</AndroidUseAssemblyStore>
325325
<AndroidAotEnableLazyLoad Condition=" '$(AndroidAotEnableLazyLoad)' == '' And '$(AotAssemblies)' == 'true' And '$(AndroidIncludeDebugSymbols)' != 'true' ">True</AndroidAotEnableLazyLoad>
326326
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and ('$(UsingMicrosoftNETSdkRazor)' == 'true') ">False</AndroidEnableMarshalMethods>
327-
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' ">True</AndroidEnableMarshalMethods>
327+
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and '$(PublishAot)' != 'true' ">True</AndroidEnableMarshalMethods>
328+
<!-- NOTE: temporarily disable for NativeAOT for now, to get build passing -->
329+
<AndroidEnableMarshalMethods Condition=" '$(AndroidEnableMarshalMethods)' == '' and '$(PublishAot)' == 'true' ">False</AndroidEnableMarshalMethods>
328330
<_AndroidUseMarshalMethods Condition=" '$(AndroidIncludeDebugSymbols)' == 'True' ">False</_AndroidUseMarshalMethods>
329331
<_AndroidUseMarshalMethods Condition=" '$(AndroidIncludeDebugSymbols)' != 'True' ">$(AndroidEnableMarshalMethods)</_AndroidUseMarshalMethods>
330332
</PropertyGroup>
@@ -1937,7 +1939,7 @@ because xbuild doesn't support framework reference assemblies.
19371939

19381940
<!-- Shrink Mono.Android.dll by removing attribute only needed for GenerateJavaStubs -->
19391941
<RemoveRegisterAttribute
1940-
Condition="'$(AndroidLinkMode)' != 'None' AND '$(AndroidIncludeDebugSymbols)' != 'true' AND '$(AndroidStripILAfterAOT)' != 'true'"
1942+
Condition="'$(AndroidLinkMode)' != 'None' and '$(AndroidIncludeDebugSymbols)' != 'true' and '$(AndroidStripILAfterAOT)' != 'true' and '$(PublishAot)' != 'true' "
19411943
ShrunkFrameworkAssemblies="@(_ShrunkAssemblies)" />
19421944

19431945
<MakeDir Directories="$(MonoAndroidIntermediateAssemblyDir)shrunk" />

0 commit comments

Comments
 (0)