Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
-->
<Project>

<UsingTask TaskName="Xamarin.Android.Tasks.SetNdkPathForIlc" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />

<!-- Default property values for NativeAOT -->
<PropertyGroup>
<_AndroidRuntimePackRuntime>NativeAOT</_AndroidRuntimePackRuntime>
<!-- NativeAOT's targets currently gives an error about cross-compilation -->
<DisableUnsupportedError Condition=" $([MSBuild]::IsOSPlatform('windows')) and '$(DisableUnsupportedError)' == '' ">true</DisableUnsupportedError>
</PropertyGroup>

<!-- Make IlcCompile depend on the trimmer -->
Expand All @@ -32,16 +36,14 @@ This file contains the NativeAOT-specific MSBuild logic for .NET for Android.
<PropertyGroup>
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-arm64' ">aarch64</_NdkAbi>
<_NdkAbi Condition=" '$(RuntimeIdentifier)' == 'android-x64' ">x86_64</_NdkAbi>
<_NdkSysrootAbi>$(_NdkAbi)-linux-android</_NdkSysrootAbi>
<_NdkClangPrefix>$(_NdkAbi)-linux-android21-</_NdkClangPrefix>
<_NdkSysrootAbi>$(_NdkAbi)-linux-android</_NdkSysrootAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('windows')) ">windows-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('osx')) ">darwin-x86_64</_NdkPrebuiltAbi>
<_NdkPrebuiltAbi Condition=" $([MSBuild]::IsOSPlatform('linux')) ">linux-x86_64</_NdkPrebuiltAbi>
<_NdkSysrootDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/sysroot/usr/lib/$(_NdkSysrootAbi)/</_NdkSysrootDir>
<_NdkBinDir>$(_AndroidNdkDirectory)toolchains/llvm/prebuilt/$(_NdkPrebuiltAbi)/bin/</_NdkBinDir>
<CppCompilerAndLinker>$(_NdkBinDir)$(_NdkClangPrefix)clang++</CppCompilerAndLinker>
<CppLinker>$(CppCompilerAndLinker)</CppLinker>
<ObjCopyName>$(_NdkBinDir)llvm-objcopy</ObjCopyName>
<CppCompilerAndLinker>clang++</CppCompilerAndLinker>
<ObjCopyName>llvm-objcopy</ObjCopyName>

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

<SetNdkPathForIlc NdkBinDirectory="$(_NdkBinDir)" />
</Target>

<Target Name="_AndroidComputeIlcCompileInputs">
Expand Down
27 changes: 27 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/SetNdkPathForIlc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.IO;
using Microsoft.Android.Build.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Xamarin.Android.Tasks;

/// <summary>
/// NativeAOT's compiler (ILC) expects to find tooling in $PATH
/// </summary>
public class SetNdkPathForIlc : AndroidTask
{
public override string TaskPrefix => "SILC";

[Required]
public string NdkBinDirectory { get; set; } = "";

public override bool RunTask ()
{
var ndkbin = Path.GetFullPath (NdkBinDirectory);
var path = $"{ndkbin}{Path.PathSeparator}{Environment.GetEnvironmentVariable ("PATH")}";
Log.LogDebugMessage ($"Setting $PATH to: {path}");
Environment.SetEnvironmentVariable ("PATH", path);
return !Log.HasLoggedErrors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,6 @@ public void BuildBasicApplication ([Values (true, false)] bool isRelease, [Value
[Test]
public void NativeAOT ()
{
if (IsWindows) {
// Microsoft.NETCore.Native.Publish.targets(61,5): Cross-OS native compilation is not supported.
// 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.
Assert.Ignore ("This test is not valid on Windows.");
}

var proj = new XamarinAndroidApplicationProject {
ProjectName = "Hello",
IsRelease = true,
Expand Down
Loading