From 4fd380a71666afb8572b5e6559cafcbd72a469b9 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Tue, 22 Jun 2021 15:00:17 -0400 Subject: [PATCH] [Android] Fix AndroidAppBuilder to work w/ AOT+LLVM (#53643) 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 #53628 --- src/mono/mono/mini/aot-compiler.c | 16 ++++++++----- src/mono/mono/mini/mini-arm.c | 2 ++ src/tasks/AndroidAppBuilder/ApkBuilder.cs | 27 ++++++++++++++-------- .../Templates/CMakeLists-android.txt | 9 +++++++- src/tasks/AotCompilerTask/MonoAOTCompiler.props | 5 +++- .../Android.Device_Emulator.Aot_Llvm.Test.csproj | 17 ++++++++++++++ .../Android/Device_Emulator/AOT_LLVM/Program.cs | 13 +++++++++++ 7 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj create mode 100644 src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Program.cs diff --git a/src/mono/mono/mini/aot-compiler.c b/src/mono/mono/mini/aot-compiler.c index 358c4e6..b36fea0 100644 --- a/src/mono/mono/mini/aot-compiler.c +++ b/src/mono/mono/mini/aot-compiler.c @@ -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; @@ -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"); diff --git a/src/mono/mono/mini/mini-arm.c b/src/mono/mono/mini/mini-arm.c index 1f4ec81..4fc43ee 100644 --- a/src/mono/mono/mini/mini-arm.c +++ b/src/mono/mono/mini/mini-arm.c @@ -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; diff --git a/src/tasks/AndroidAppBuilder/ApkBuilder.cs b/src/tasks/AndroidAppBuilder/ApkBuilder.cs index ca7c492..346f854 100644 --- a/src/tasks/AndroidAppBuilder/ApkBuilder.cs +++ b/src/tasks/AndroidAppBuilder/ApkBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text; using Microsoft.Build.Framework; public class ApkBuilder @@ -122,18 +123,29 @@ public class ApkBuilder throw new ArgumentException($"{buildToolsFolder} was not found."); } - var assemblerFiles = new List(); + 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."); } @@ -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) diff --git a/src/tasks/AndroidAppBuilder/Templates/CMakeLists-android.txt b/src/tasks/AndroidAppBuilder/Templates/CMakeLists-android.txt index c74d462..7b602ad 100644 --- a/src/tasks/AndroidAppBuilder/Templates/CMakeLists-android.txt +++ b/src/tasks/AndroidAppBuilder/Templates/CMakeLists-android.txt @@ -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% diff --git a/src/tasks/AotCompilerTask/MonoAOTCompiler.props b/src/tasks/AotCompilerTask/MonoAOTCompiler.props index 097758a..159c01f 100644 --- a/src/tasks/AotCompilerTask/MonoAOTCompiler.props +++ b/src/tasks/AotCompilerTask/MonoAOTCompiler.props @@ -14,7 +14,10 @@ - + + + + diff --git a/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj new file mode 100644 index 0000000..3ec5bf5 --- /dev/null +++ b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Android.Device_Emulator.Aot_Llvm.Test.csproj @@ -0,0 +1,17 @@ + + + Exe + false + true + true + $(NetCoreAppCurrent) + Android.Device_Emulator.Aot_Llvm.Test.dll + 42 + true + true + + + + + + diff --git a/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Program.cs b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Program.cs new file mode 100644 index 0000000..7dcc0f3 --- /dev/null +++ b/src/tests/FunctionalTests/Android/Device_Emulator/AOT_LLVM/Program.cs @@ -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; + } +} -- 2.7.4