Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to MonoAOTCompiler msbuild task to generate .so files #55753

Merged
merged 1 commit into from
Jul 15, 2021
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
2 changes: 1 addition & 1 deletion src/mono/mono/mini/aot-runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,7 @@ load_aot_module (MonoAssemblyLoadContext *alc, MonoAssembly *assembly, gpointer
g_free (err);
}
g_free (aot_name);
#if !defined(PLATFORM_ANDROID) && !defined(TARGET_WASM)
#if !defined(HOST_ANDROID) && !defined(HOST_WASM)
if (!sofile) {
char *basename = g_path_get_basename (assembly->image->name);
aot_name = g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), MONO_ARCHITECTURE, basename, MONO_SOLIB_EXT);
Expand Down
24 changes: 22 additions & 2 deletions src/mono/sample/Android/AndroidSampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,33 @@
<Message Importance="High" Text="Path: $(PublishDir)" />
<Message Importance="High" Text="SourceDir: $(OutputPath)" />

<PropertyGroup Condition="'$(ForceAOT)' == 'true' and '$(AOTWithLibraryFiles)' != 'true'">
<_AotOutputType>AsmOnly</_AotOutputType>
<_AotModulesTablePath>$(BundleDir)\modules.c</_AotModulesTablePath>
</PropertyGroup>

<PropertyGroup Condition="'$(ForceAOT)' == 'true' and '$(AOTWithLibraryFiles)' == 'true'">
<_AotOutputType>Library</_AotOutputType>
<_AotLibraryFormat>So</_AotLibraryFormat>
<_PrebuiltOS Condition="$([MSBuild]::IsOSPlatform('Linux'))">linux-x86_64</_PrebuiltOS>
<_PrebuiltOS Condition="$([MSBuild]::IsOSPlatform('OSX'))">darwin-x86_64</_PrebuiltOS>
<_PrebuiltOS Condition="$([MSBuild]::IsOSPlatform('Windows'))">windows-x86_64</_PrebuiltOS>
<_PrebuiltAbi Condition="'$(TargetArchitecture)' == 'arm'">arm-linux-androideabi$</_PrebuiltAbi>
<_PrebuiltAbi Condition="'$(TargetArchitecture)' == 'arm64'">aarch64-linux-android</_PrebuiltAbi>
<_PrebuiltAbi Condition="'$(TargetArchitecture)' == 'x64'">x86_64-linux-android</_PrebuiltAbi>
<_PrebuiltAbi Condition="'$(TargetArchitecture)' == 'x86'">i686-linux-android</_PrebuiltAbi>
<_AotToolPrefix>$(ANDROID_NDK_ROOT)\toolchains\llvm\prebuilt\$(_PrebuiltOS)\bin\$(_PrebuiltAbi)-</_AotToolPrefix>
</PropertyGroup>

<MonoAOTCompiler Condition="'$(ForceAOT)' == 'true'"
CompilerBinaryPath="@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','$(TargetOS.ToLowerInvariant())-$(TargetArchitecture.ToLowerInvariant())'))"
OutputDir="$(_MobileIntermediateOutputPath)"
Mode="Full"
OutputType="AsmOnly"
OutputType="$(_AotOutputType)"
Assemblies="@(AotInputAssemblies)"
AotModulesTablePath="$(BundleDir)\modules.c"
AotModulesTablePath="$(_AotModulesTablePath)"
ToolPrefix="$(_AotToolPrefix)"
LibraryFormat="$(_AotLibraryFormat)"
UseLLVM="$(UseLLVM)"
LLVMPath="$(MonoAotCrossDir)">
<Output TaskParameter="CompiledAssemblies" ItemName="BundleAssemblies" />
Expand Down
3 changes: 3 additions & 0 deletions src/mono/sample/Android/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ MONO_ARCH?=x64
DOTNET := ../../../../dotnet.sh
USE_LLVM=true
AOT=false
AOT_WITH_LIBRARY_FILES=false
INTERP=false
DEPLOY_AND_RUN?=true

Expand All @@ -26,7 +27,9 @@ run:
/p:TargetOS=Android \
/p:Configuration=$(MONO_CONFIG) \
/p:DeployAndRun=$(DEPLOY_AND_RUN) \
/p:RunAOTCompilation=$(AOT) \
/p:ForceAOT=$(AOT) \
/p:AOTWithLibraryFiles=$(AOT_WITH_LIBRARY_FILES) \
/p:MonoForceInterpreter=$(INTERP) \
/p:UseLLVM=$(USE_LLVM) \
/p:RunActivity=false \
Expand Down
36 changes: 27 additions & 9 deletions src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,13 @@ public class ApkBuilder

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

if (!string.IsNullOrEmpty(obj))
{
Expand All @@ -143,9 +145,14 @@ public class ApkBuilder
var name = Path.GetFileNameWithoutExtension(llvmObj);
assemblerFilesToLink.AppendLine($" {llvmObj}");
}

if (!string.IsNullOrEmpty(lib))
{
aotLibraryFiles.Add(lib);
}
}

if (ForceAOT && assemblerFiles.Length == 0)
if (ForceAOT && assemblerFiles.Length == 0 && aotLibraryFiles.Count == 0)
{
throw new InvalidOperationException("Need list of AOT files.");
}
Expand All @@ -165,7 +172,8 @@ public class ApkBuilder

// Copy sourceDir to OutputDir/assets-tozip (ignore native files)
// these files then will be zipped and copied to apk/assets/assets.zip
Utils.DirectoryCopy(AppDir, Path.Combine(OutputDir, "assets-tozip"), file =>
var assetsToZipDirectory = Path.Combine(OutputDir, "assets-tozip");
Utils.DirectoryCopy(AppDir, assetsToZipDirectory, file =>
{
string fileName = Path.GetFileName(file);
string extension = Path.GetExtension(file);
Expand All @@ -184,6 +192,12 @@ public class ApkBuilder
return true;
});

// add AOT .so libraries
foreach (var aotlib in aotLibraryFiles)
{
File.Copy(aotlib, Path.Combine(assetsToZipDirectory, Path.GetFileName(aotlib)));
}

// tools:
string dx = Path.Combine(buildToolsFolder, "dx");
string aapt = Path.Combine(buildToolsFolder, "aapt");
Expand All @@ -195,8 +209,8 @@ public class ApkBuilder
string cmake = "cmake";
string zip = "zip";

Utils.RunProcess(zip, workingDir: Path.Combine(OutputDir, "assets-tozip"), args: "-q -r ../assets/assets.zip .");
Directory.Delete(Path.Combine(OutputDir, "assets-tozip"), true);
Utils.RunProcess(zip, workingDir: assetsToZipDirectory, args: "-q -r ../assets/assets.zip .");
Directory.Delete(assetsToZipDirectory, true);

if (!File.Exists(androidJar))
throw new ArgumentException($"API level={BuildApiLevel} is not downloaded in Android SDK");
Expand Down Expand Up @@ -283,22 +297,26 @@ public class ApkBuilder
.Replace("%AotSources%", aotSources)
.Replace("%AotModulesSource%", string.IsNullOrEmpty(aotSources) ? "" : "modules.c");

string defines = "";
var defines = new StringBuilder();
if (ForceInterpreter)
{
defines = "add_definitions(-DFORCE_INTERPRETER=1)";
defines.AppendLine("add_definitions(-DFORCE_INTERPRETER=1)");
}
else if (ForceAOT)
{
defines = "add_definitions(-DFORCE_AOT=1)";
defines.AppendLine("add_definitions(-DFORCE_AOT=1)");
if (aotLibraryFiles.Count == 0)
{
defines.AppendLine("add_definitions(-DSTATIC_AOT=1)");
}
}

if (!string.IsNullOrEmpty(DiagnosticPorts))
{
defines += "\nadd_definitions(-DDIAGNOSTIC_PORTS=\"" + DiagnosticPorts + "\")";
defines.AppendLine("add_definitions(-DDIAGNOSTIC_PORTS=\"" + DiagnosticPorts + "\")");
}

cmakeLists = cmakeLists.Replace("%Defines%", defines);
cmakeLists = cmakeLists.Replace("%Defines%", defines.ToString());

File.WriteAllText(Path.Combine(OutputDir, "CMakeLists.txt"), cmakeLists);

Expand Down
5 changes: 4 additions & 1 deletion src/tasks/AndroidAppBuilder/Templates/monodroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ log_callback (const char *log_domain, const char *log_level, const char *message
}
}

#if FORCE_AOT
#if defined(FORCE_AOT) && defined(STATIC_AOT)
void register_aot_modules (void);
#endif

Expand Down Expand Up @@ -270,7 +270,10 @@ mono_droid_runtime_init (const char* executable, int managed_argc, char* managed
LOG_INFO("Interp Enabled");
mono_jit_set_aot_mode(MONO_AOT_MODE_INTERP_ONLY);
#elif FORCE_AOT
LOG_INFO("AOT Enabled");
#if STATIC_AOT
register_aot_modules();
#endif
mono_jit_set_aot_mode(MONO_AOT_MODE_FULL);
#endif

Expand Down
Loading