Skip to content

Commit ed0d8a0

Browse files
[nativeaot] fix project builds on Windows (#9710)
In 2fa7954, we got Android projects *building* for NativeAOT. However, this didn't work at all on Windows. Reviewing how NativeAOT's MSBuild targets are setup, they expect various tooling to be available in `%PATH%`. To fix this: * Create a new `<SetNdkPathForIlc/>` MSBuild task. * That simply sets the Android NDK "bin" directory to `%PATH%`. NativeAOT apps now successfully build (and run) on Windows. I also updated an integration test to verify this. Unfortunately, we still have to set `$(DisableUnsupportedError)` or we will get the message: D:\.nuget\packages\microsoft.dotnet.ilcompiler\10.0.0-alpha.1.25067.10\build\Microsoft.NETCore.Native.Publish.targets(61,5): error : Cross-OS native compilation is not supported. Please use the appropriate OS-specific target. A future change might be needed here, or it might be fine to for the Android workload to set this property: * https://github.com/dotnet/runtime/blob/ea4a404ef8890f265780f798e7668d4710259e03/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets#L61-L62
1 parent f4e405e commit ed0d8a0

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
77
-->
88
<Project>
99

10+
<UsingTask TaskName="Xamarin.Android.Tasks.SetNdkPathForIlc" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
11+
1012
<!-- Default property values for NativeAOT -->
1113
<PropertyGroup>
1214
<_AndroidRuntimePackRuntime>NativeAOT</_AndroidRuntimePackRuntime>
15+
<!-- NativeAOT's targets currently gives an error about cross-compilation -->
16+
<DisableUnsupportedError Condition=" $([MSBuild]::IsOSPlatform('windows')) and '$(DisableUnsupportedError)' == '' ">true</DisableUnsupportedError>
1317
</PropertyGroup>
1418

1519
<!-- Make IlcCompile depend on the trimmer -->
@@ -32,16 +36,14 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
3236
<PropertyGroup>
3337
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">aarch64</_NdkAbi>
3438
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-x64' ">x86_64</_NdkAbi>
35-
<_NdkSysrootAbi>$(_NdkAbi)-linux-android</_NdkSysrootAbi>
36-
<_NdkClangPrefix>$(_NdkAbi)-linux-android21-</_NdkClangPrefix>
39+
<_NdkSysrootAbi>$(_NdkAbi)-linux-android</_NdkSysrootAbi>
3740
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64</_NdkPrebuiltAbi>
3841
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
3942
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64</_NdkPrebuiltAbi>
4043
<_NdkSysrootDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)/</_NdkSysrootDir>
4144
<_NdkBinDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin/</_NdkBinDir>
42-
<CppCompilerAndLinker>$(_NdkBinDir)$(_NdkClangPrefix)clang++</CppCompilerAndLinker>
43-
<CppLinker>$(CppCompilerAndLinker)</CppLinker>
44-
<ObjCopyName>$(_NdkBinDir)llvm-objcopy</ObjCopyName>
45+
<CppCompilerAndLinker>clang++</CppCompilerAndLinker>
46+
<ObjCopyName>llvm-objcopy</ObjCopyName>
4547

4648
<!-- Example settings from: https://github.com/xamarin/xamarin-macios/blob/c43d4ea40bc777969e3b158cf46446df292d8449/dotnet/targets/Xamarin.Shared.Sdk.targets#L541-L550 -->
4749
<RunILLink>true</RunILLink>
@@ -66,6 +68,8 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
6668
<!-- HACK: prevents: java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "__start___modules" -->
6769
<LinkerFlavor Condition=" '$(LinkerFlavor)' == '' ">lld</LinkerFlavor>
6870
</PropertyGroup>
71+
72+
<SetNdkPathForIlc NdkBinDirectory="$(_NdkBinDir)" />
6973
</Target>
7074

7175
<Target Name="_AndroidComputeIlcCompileInputs">
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.IO;
3+
using Microsoft.Android.Build.Tasks;
4+
using Microsoft.Build.Framework;
5+
using Microsoft.Build.Utilities;
6+
7+
namespace Xamarin.Android.Tasks;
8+
9+
/// <summary>
10+
/// NativeAOT's compiler (ILC) expects to find tooling in $PATH
11+
/// </summary>
12+
public class SetNdkPathForIlc : AndroidTask
13+
{
14+
public override string TaskPrefix => "SILC";
15+
16+
[Required]
17+
public string NdkBinDirectory { get; set; } = "";
18+
19+
public override bool RunTask ()
20+
{
21+
var ndkbin = Path.GetFullPath (NdkBinDirectory);
22+
var path = $"{ndkbin}{Path.PathSeparator}{Environment.GetEnvironmentVariable ("PATH")}";
23+
Log.LogDebugMessage ($"Setting $PATH to: {path}");
24+
Environment.SetEnvironmentVariable ("PATH", path);
25+
return !Log.HasLoggedErrors;
26+
}
27+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,6 @@ public void BuildBasicApplication ([Values (true, false)] bool isRelease, [Value
108108
[Test]
109109
public void NativeAOT ()
110110
{
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-
117111
var proj = new XamarinAndroidApplicationProject {
118112
ProjectName = "Hello",
119113
IsRelease = true,

0 commit comments

Comments
 (0)