Skip to content

Commit

Permalink
[Android] Use NDK toolchain instead of CMake for library mode (#88886)
Browse files Browse the repository at this point in the history
This change switches from relying on CMake to clang directly for Android library mode. The intent is to keep the number of build dependencies to a minimum.
  • Loading branch information
steveisok authored Jul 18, 2023
1 parent 21f07e1 commit 8f8a007
Show file tree
Hide file tree
Showing 18 changed files with 424 additions and 171 deletions.
6 changes: 5 additions & 1 deletion src/mono/mono/mini/aot-compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -15371,7 +15371,11 @@ emit_aot_image (MonoAotCompile *acfg)
acfg->tmpbasename = g_build_filename (temp_path, "temp", (const char*)NULL);
acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
acfg->llvm_sfile = g_strdup_printf ("%s-llvm.s", acfg->tmpbasename);
acfg->llvm_ofile = g_strdup_printf ("%s-llvm." AS_OBJECT_FILE_SUFFIX, acfg->tmpbasename);

if (acfg->aot_opts.static_link)
acfg->llvm_ofile = g_strdup (acfg->aot_opts.llvm_outfile);
else
acfg->llvm_ofile = g_strdup_printf ("%s-llvm." AS_OBJECT_FILE_SUFFIX, acfg->tmpbasename);

g_free (temp_path);
}
Expand Down
15 changes: 11 additions & 4 deletions src/mono/msbuild/android/build/AndroidBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
<PropertyGroup>
<AndroidGenerateAppBundle Condition="'$(AndroidGenerateAppBundle)' == '' and '$(GenerateAppBundle)' != ''">$(GenerateAppBundle)</AndroidGenerateAppBundle>
<AndroidGenerateAppBundle Condition="'$(AndroidGenerateAppBundle)' == ''">true</AndroidGenerateAppBundle>
<!-- Unable to properly integrate nativelib into app build, so not supported for now. -->
<AndroidGenerateAppBundle Condition="'$(_IsLibraryMode)' == 'true'">false</AndroidGenerateAppBundle>
<AndroidGenerateAppBundle Condition="'$(_IsLibraryMode)' == 'true' and '$(ForceLibraryModeGenerateAppBundle)' != 'true'">false</AndroidGenerateAppBundle>

<EnableDefaultAssembliesToBundle Condition="'$(EnableDefaultAssembliesToBundle)' == ''">false</EnableDefaultAssembliesToBundle>
</PropertyGroup>
Expand Down Expand Up @@ -47,6 +46,12 @@
</PropertyGroup>

<ItemGroup Condition="'$(_IsLibraryMode)' == 'true'">
<_CommonLinkerArgs Include="-l:libz.so" />
<_CommonLinkerArgs Include="-l:liblog.so" />
<_CommonLinkerArgs Include="-l:libc.so" />
<_CommonLinkerArgs Include="-l:libm.so" />
<_CommonLinkerArgs Include="--build-id=sha1" />

<!-- add all non stub libs first -->
<!-- if RuntimeComponents is empty, exclude -static.a and include -stub-static.a instead -->
<!-- if RuntimeComponents is *, we're ok because all -static.a is included -->
Expand Down Expand Up @@ -74,6 +79,7 @@
</PropertyGroup>

<RemoveDir Directories="$(AndroidBundleDir)" />
<MakeDir Directories="$(AndroidBundleDir)" />
</Target>

<Target Name="_AndroidResolveReferences">
Expand Down Expand Up @@ -110,6 +116,7 @@
<PropertyGroup Condition="'$(_IsLibraryMode)' == 'true'">
<_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback)</_UsesRuntimeInitCallback>
<_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true</_UsesRuntimeInitCallback>
<_AotOutputType>ObjectFile</_AotOutputType>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -142,7 +149,7 @@
</PropertyGroup>

<NdkToolFinderTask
Condition="'$(AOTWithLibraryFiles)' == 'true'"
Condition="'$(AOTWithLibraryFiles)' == 'true' or '$(_IsLibraryMode)' == 'true'"
Architecture="$(TargetArchitecture)"
HostOS="$(_HostOS)"
MinApiLevel="$(AndroidLibraryMinApiLevel)">
Expand All @@ -154,7 +161,7 @@
<Output TaskParameter="ClangPath" PropertyName="_ClangPath" />
</NdkToolFinderTask>

<PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true'">
<PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true' or '$(_IsLibraryMode)' == 'true'">
<_AsPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_AsPrefixPath)'))</_AsPrefixPath>
<_ToolPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_ToolPrefixPath)'))</_ToolPrefixPath>
</PropertyGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/tasks/AndroidAppBuilder/AndroidAppBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
<ItemGroup>
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="$(MicrosoftBuildTasksCoreVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(RepoRoot)src\tasks\MobileBuildTasks\MobileBuildTasks.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApkBuilder.cs" />
<Compile Include="AndroidAppBuilder.cs" />
<Compile Include="AndroidApkFileReplacerTask.cs" />
<Compile Include="AndroidLibBuilderTask.cs" />
<Compile Include="..\Common\Builders\AndroidProject.cs" />
<Compile Include="..\Common\AndroidSdkHelper.cs" />
<Compile Include="..\Common\DexBuilder.cs" />
<Compile Include="..\Common\JarBuilder.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/tasks/AndroidAppBuilder/ApkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.Android.Build;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

Expand Down
3 changes: 3 additions & 0 deletions src/tasks/AndroidAppBuilder/Templates/monodroid-librarymode.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_
setenv ("TMPDIR", cache_dir, true);
setenv ("TEST_RESULTS_DIR", testresults_dir, true);

//setenv ("MONO_LOG_LEVEL", "debug", true);
//setenv ("MONO_LOG_MASK", "all", true);

return invoke_netlibrary_entrypoints ();
}
22 changes: 15 additions & 7 deletions src/tasks/AotCompilerTask/MonoAOTCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private bool ProcessAndValidateArguments()
throw new LogAsErrorException($"'{nameof(AotModulesTableLanguage)}' must be one of: '{nameof(MonoAotModulesTableLanguage.C)}', '{nameof(MonoAotModulesTableLanguage.ObjC)}'. Received: '{AotModulesTableLanguage}'.");
}

if (!string.IsNullOrEmpty(AotModulesTablePath))
if (!string.IsNullOrEmpty(AotModulesTablePath) || parsedOutputType == MonoAotOutputType.ObjectFile)
{
// AOT modules for static linking, needs the aot modules table
UseStaticLinking = true;
Expand Down Expand Up @@ -863,6 +863,20 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
}
}

if (!string.IsNullOrEmpty(TempPath))
{
aotArgs.Add($"temp-path={TempPath}");
}
else if (!string.IsNullOrEmpty(IntermediateOutputPath))
{
string aotTmpPath = Path.Combine(IntermediateOutputPath, assemblyFilename + ".tmp");
if (!Directory.Exists(aotTmpPath))
{
Directory.CreateDirectory(aotTmpPath);
}
aotArgs.Add($"temp-path={aotTmpPath}");
}

if (EnableUnmanagedCallersOnlyMethodsExport)
{
string exportSymbolsFile = Path.Combine(OutputDir, Path.ChangeExtension(assemblyFilename, ".exportsymbols"));
Expand Down Expand Up @@ -896,7 +910,6 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
}
}


if (AotProfilePath?.Length > 0)
{
aotArgs.Add("profile-only");
Expand All @@ -920,11 +933,6 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
aotArgs.Add(AotArguments);
}

if (!string.IsNullOrEmpty(TempPath))
{
aotArgs.Add($"temp-path={TempPath}");
}

if (!string.IsNullOrEmpty(LdName))
{
aotArgs.Add($"ld-name={LdName}");
Expand Down
91 changes: 0 additions & 91 deletions src/tasks/Common/Builders/AndroidProject.cs

This file was deleted.

18 changes: 18 additions & 0 deletions src/tasks/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@ public static bool IsWindows()
#endif
}

public static bool IsMacOS()
{
#if NETCOREAPP
return OperatingSystem.IsMacOS();
#else
return false;
#endif
}

public static bool IsLinux()
{
#if NETCOREAPP
return OperatingSystem.IsLinux();
#else
return false;
#endif
}

public static bool IsManagedAssembly(string filePath)
{
if (!File.Exists(filePath))
Expand Down
Loading

0 comments on commit 8f8a007

Please sign in to comment.