Skip to content

Commit

Permalink
[Android] Fix AndroidAppBuilder to work w/ AOT+LLVM (dotnet#53643)
Browse files Browse the repository at this point in the history
We were missing a few key additions to make sure we can test against AOT+LLVM. This change will make sure we link against all the .dll-llvm.o files produced from the AOT compiler and include the right mtriple for each architecture.

Fixes dotnet#53628
  • Loading branch information
steveisok authored Jun 22, 2021
1 parent 245dddc commit 4fd380a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 17 deletions.
16 changes: 10 additions & 6 deletions src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1115,17 +1115,15 @@ arch_init (MonoAotCompile *acfg)
acfg->llvm_label_prefix = "";
acfg->user_symbol_prefix = "";

#if defined(TARGET_X86)
#ifdef TARGET_ANDROID
g_string_append_printf (acfg->llc_args, " -mtriple=i686-none-linux-android21");
#else
#if TARGET_X86 || TARGET_AMD64
const gboolean has_custom_args = !!acfg->aot_opts.llvm_llc || acfg->aot_opts.use_current_cpu;
g_string_append_printf (acfg->llc_args, " -march=x86 %s", has_custom_args ? "" : "-mcpu=generic");
#endif

#if defined(TARGET_X86)
g_string_append_printf (acfg->llc_args, " -march=x86 %s", has_custom_args ? "" : "-mcpu=generic");
#endif

#if defined(TARGET_AMD64)
const gboolean has_custom_args = !!acfg->aot_opts.llvm_llc || acfg->aot_opts.use_current_cpu;
g_string_append_printf (acfg->llc_args, " -march=x86-64 %s", has_custom_args ? "" : "-mcpu=generic");
/* NOP */
acfg->align_pad_value = 0x90;
Expand Down Expand Up @@ -1159,7 +1157,13 @@ arch_init (MonoAotCompile *acfg)
g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16 -float-abi=hard");
g_string_append (acfg->as_args, " -mfpu=vfp3");
#elif defined(ARM_FPU_VFP)

#if defined(TARGET_ARM)
// +d16 triggers a warning on arm
g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon");
#else
g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16");
#endif
g_string_append (acfg->as_args, " -mfpu=vfp3");
#else
g_string_append (acfg->llc_args, " -mattr=+soft-float");
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/mini/mini-arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,8 @@ mono_arch_init (void)
have a way to properly detect CPU features on it. */
thumb_supported = TRUE;
iphone_abi = TRUE;
#elif defined(TARGET_ANDROID)
thumb_supported = TRUE;
#else
thumb_supported = mono_hwcap_arm_has_thumb;
thumb2_supported = mono_hwcap_arm_has_thumb2;
Expand Down
27 changes: 18 additions & 9 deletions src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Build.Framework;

public class ApkBuilder
Expand Down Expand Up @@ -122,18 +123,29 @@ public class ApkBuilder
throw new ArgumentException($"{buildToolsFolder} was not found.");
}

var assemblerFiles = new List<string>();
var assemblerFiles = new StringBuilder();
var assemblerFilesToLink = new StringBuilder();
foreach (ITaskItem file in Assemblies)
{
// use AOT files if available
var obj = file.GetMetadata("AssemblerFile");
var llvmObj = file.GetMetadata("LlvmObjectFile");

if (!string.IsNullOrEmpty(obj))
{
assemblerFiles.Add(obj);
var name = Path.GetFileNameWithoutExtension(obj);
assemblerFiles.AppendLine($"add_library({name} OBJECT {obj})");
assemblerFilesToLink.AppendLine($" {name}");
}

if (!string.IsNullOrEmpty(llvmObj))
{
var name = Path.GetFileNameWithoutExtension(llvmObj);
assemblerFilesToLink.AppendLine($" {llvmObj}");
}
}

if (ForceAOT && !assemblerFiles.Any())
if (ForceAOT && assemblerFiles.Length == 0)
{
throw new InvalidOperationException("Need list of AOT files.");
}
Expand Down Expand Up @@ -261,12 +273,9 @@ public class ApkBuilder
nativeLibraries += $" {monoRuntimeLib}{Environment.NewLine}";
}

string aotSources = "";
foreach (string asm in assemblerFiles)
{
// these libraries are linked via modules.c
aotSources += $" {asm}{Environment.NewLine}";
}
nativeLibraries += assemblerFilesToLink.ToString();

string aotSources = assemblerFiles.ToString();

string cmakeLists = Utils.GetEmbeddedResource("CMakeLists-android.txt")
.Replace("%MonoInclude%", monoRuntimeHeaders)
Expand Down
9 changes: 8 additions & 1 deletion src/tasks/AndroidAppBuilder/Templates/CMakeLists-android.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ if(NOT USE_LLVM)
add_compile_options(-no-integrated-as)
endif()

# Prevent the warning: shared library text segment is not shareable which is treated as an error
if (NOT ANDROID_ABI STREQUAL "arm64-v8a")
add_link_options(-Wl,--no-warn-shared-textrel)
endif()

add_library(
monodroid
SHARED
monodroid.c
%AotModulesSource%
%AotSources%)
)

%AotSources%

%Defines%

Expand Down
5 changes: 4 additions & 1 deletion src/tasks/AotCompilerTask/MonoAOTCompiler.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
<MonoAOTCompilerDefaultProcessArguments Include="-O=gsharedvt" />
</ItemGroup>
<ItemGroup Condition="'$(TargetOS)' == 'Android'">
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'arm'" Include="mtriple=armv7-linux-gnueabi-thumbv7s" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'arm'" Include="mtriple=armv7-linux-gnueabi" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'arm64'" Include="mtriple=aarch64-linux-android" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'x86'" Include="mtriple=i686-linux-android" />
<MonoAOTCompilerDefaultAotArguments Condition="'$(TargetArchitecture)' == 'x64'" Include="mtriple=x86_64-linux-android" />
<MonoAOTCompilerDefaultAotArguments Include="static" />
<!-- Default trampolines run out for libraries tests -->
<MonoAOTCompilerDefaultAotArguments Include="nimt-trampolines=2000" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<MonoForceInterpreter>false</MonoForceInterpreter>
<RunAOTCompilation>true</RunAOTCompilation>
<TestRuntime>true</TestRuntime>
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks>
<MainLibraryFileName>Android.Device_Emulator.Aot_Llvm.Test.dll</MainLibraryFileName>
<ExpectedExitCode>42</ExpectedExitCode>
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
<MonoEnableLLVM>true</MonoEnableLLVM>
</PropertyGroup>

<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

public static class Program
{
public static int Main(string[] args)
{
Console.WriteLine("Hello, Android!"); // logcat
return 42;
}
}

0 comments on commit 4fd380a

Please sign in to comment.