Skip to content

Commit 8f8a007

Browse files
authored
[Android] Use NDK toolchain instead of CMake for library mode (#88886)
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.
1 parent 21f07e1 commit 8f8a007

File tree

18 files changed

+424
-171
lines changed

18 files changed

+424
-171
lines changed

src/mono/mono/mini/aot-compiler.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15371,7 +15371,11 @@ emit_aot_image (MonoAotCompile *acfg)
1537115371
acfg->tmpbasename = g_build_filename (temp_path, "temp", (const char*)NULL);
1537215372
acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
1537315373
acfg->llvm_sfile = g_strdup_printf ("%s-llvm.s", acfg->tmpbasename);
15374-
acfg->llvm_ofile = g_strdup_printf ("%s-llvm." AS_OBJECT_FILE_SUFFIX, acfg->tmpbasename);
15374+
15375+
if (acfg->aot_opts.static_link)
15376+
acfg->llvm_ofile = g_strdup (acfg->aot_opts.llvm_outfile);
15377+
else
15378+
acfg->llvm_ofile = g_strdup_printf ("%s-llvm." AS_OBJECT_FILE_SUFFIX, acfg->tmpbasename);
1537515379

1537615380
g_free (temp_path);
1537715381
}

src/mono/msbuild/android/build/AndroidBuild.targets

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
<PropertyGroup>
33
<AndroidGenerateAppBundle Condition="'$(AndroidGenerateAppBundle)' == '' and '$(GenerateAppBundle)' != ''">$(GenerateAppBundle)</AndroidGenerateAppBundle>
44
<AndroidGenerateAppBundle Condition="'$(AndroidGenerateAppBundle)' == ''">true</AndroidGenerateAppBundle>
5-
<!-- Unable to properly integrate nativelib into app build, so not supported for now. -->
6-
<AndroidGenerateAppBundle Condition="'$(_IsLibraryMode)' == 'true'">false</AndroidGenerateAppBundle>
5+
<AndroidGenerateAppBundle Condition="'$(_IsLibraryMode)' == 'true' and '$(ForceLibraryModeGenerateAppBundle)' != 'true'">false</AndroidGenerateAppBundle>
76

87
<EnableDefaultAssembliesToBundle Condition="'$(EnableDefaultAssembliesToBundle)' == ''">false</EnableDefaultAssembliesToBundle>
98
</PropertyGroup>
@@ -47,6 +46,12 @@
4746
</PropertyGroup>
4847

4948
<ItemGroup Condition="'$(_IsLibraryMode)' == 'true'">
49+
<_CommonLinkerArgs Include="-l:libz.so" />
50+
<_CommonLinkerArgs Include="-l:liblog.so" />
51+
<_CommonLinkerArgs Include="-l:libc.so" />
52+
<_CommonLinkerArgs Include="-l:libm.so" />
53+
<_CommonLinkerArgs Include="--build-id=sha1" />
54+
5055
<!-- add all non stub libs first -->
5156
<!-- if RuntimeComponents is empty, exclude -static.a and include -stub-static.a instead -->
5257
<!-- if RuntimeComponents is *, we're ok because all -static.a is included -->
@@ -74,6 +79,7 @@
7479
</PropertyGroup>
7580

7681
<RemoveDir Directories="$(AndroidBundleDir)" />
82+
<MakeDir Directories="$(AndroidBundleDir)" />
7783
</Target>
7884

7985
<Target Name="_AndroidResolveReferences">
@@ -110,6 +116,7 @@
110116
<PropertyGroup Condition="'$(_IsLibraryMode)' == 'true'">
111117
<_UsesRuntimeInitCallback>$(UsesRuntimeInitCallback)</_UsesRuntimeInitCallback>
112118
<_UsesRuntimeInitCallback Condition="'$(_UsesRuntimeInitCallback)' == ''">true</_UsesRuntimeInitCallback>
119+
<_AotOutputType>ObjectFile</_AotOutputType>
113120
</PropertyGroup>
114121

115122
<ItemGroup>
@@ -142,7 +149,7 @@
142149
</PropertyGroup>
143150

144151
<NdkToolFinderTask
145-
Condition="'$(AOTWithLibraryFiles)' == 'true'"
152+
Condition="'$(AOTWithLibraryFiles)' == 'true' or '$(_IsLibraryMode)' == 'true'"
146153
Architecture="$(TargetArchitecture)"
147154
HostOS="$(_HostOS)"
148155
MinApiLevel="$(AndroidLibraryMinApiLevel)">
@@ -154,7 +161,7 @@
154161
<Output TaskParameter="ClangPath" PropertyName="_ClangPath" />
155162
</NdkToolFinderTask>
156163

157-
<PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true'">
164+
<PropertyGroup Condition="'$(AOTWithLibraryFiles)' == 'true' or '$(_IsLibraryMode)' == 'true'">
158165
<_AsPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_AsPrefixPath)'))</_AsPrefixPath>
159166
<_ToolPrefixPath>$([MSBuild]::EnsureTrailingSlash('$(_ToolPrefixPath)'))</_ToolPrefixPath>
160167
</PropertyGroup>

src/tasks/AndroidAppBuilder/AndroidAppBuilder.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="$(MicrosoftBuildTasksCoreVersion)" />
1515
</ItemGroup>
16+
<ItemGroup>
17+
<ProjectReference Include="$(RepoRoot)src\tasks\MobileBuildTasks\MobileBuildTasks.csproj" />
18+
</ItemGroup>
1619
<ItemGroup>
1720
<Compile Include="ApkBuilder.cs" />
1821
<Compile Include="AndroidAppBuilder.cs" />
1922
<Compile Include="AndroidApkFileReplacerTask.cs" />
2023
<Compile Include="AndroidLibBuilderTask.cs" />
21-
<Compile Include="..\Common\Builders\AndroidProject.cs" />
2224
<Compile Include="..\Common\AndroidSdkHelper.cs" />
2325
<Compile Include="..\Common\DexBuilder.cs" />
2426
<Compile Include="..\Common\JarBuilder.cs" />

src/tasks/AndroidAppBuilder/ApkBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using System.Text;
99
using System.Text.RegularExpressions;
10+
using Microsoft.Android.Build;
1011
using Microsoft.Build.Framework;
1112
using Microsoft.Build.Utilities;
1213

src/tasks/AndroidAppBuilder/Templates/monodroid-librarymode.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@ Java_net_dot_MonoRunner_initRuntime (JNIEnv* env, jobject thiz, jstring j_files_
5353
setenv ("TMPDIR", cache_dir, true);
5454
setenv ("TEST_RESULTS_DIR", testresults_dir, true);
5555

56+
//setenv ("MONO_LOG_LEVEL", "debug", true);
57+
//setenv ("MONO_LOG_MASK", "all", true);
58+
5659
return invoke_netlibrary_entrypoints ();
5760
}

src/tasks/AotCompilerTask/MonoAOTCompiler.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ private bool ProcessAndValidateArguments()
413413
throw new LogAsErrorException($"'{nameof(AotModulesTableLanguage)}' must be one of: '{nameof(MonoAotModulesTableLanguage.C)}', '{nameof(MonoAotModulesTableLanguage.ObjC)}'. Received: '{AotModulesTableLanguage}'.");
414414
}
415415

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

866+
if (!string.IsNullOrEmpty(TempPath))
867+
{
868+
aotArgs.Add($"temp-path={TempPath}");
869+
}
870+
else if (!string.IsNullOrEmpty(IntermediateOutputPath))
871+
{
872+
string aotTmpPath = Path.Combine(IntermediateOutputPath, assemblyFilename + ".tmp");
873+
if (!Directory.Exists(aotTmpPath))
874+
{
875+
Directory.CreateDirectory(aotTmpPath);
876+
}
877+
aotArgs.Add($"temp-path={aotTmpPath}");
878+
}
879+
866880
if (EnableUnmanagedCallersOnlyMethodsExport)
867881
{
868882
string exportSymbolsFile = Path.Combine(OutputDir, Path.ChangeExtension(assemblyFilename, ".exportsymbols"));
@@ -896,7 +910,6 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
896910
}
897911
}
898912

899-
900913
if (AotProfilePath?.Length > 0)
901914
{
902915
aotArgs.Add("profile-only");
@@ -920,11 +933,6 @@ private PrecompileArguments GetPrecompileArgumentsFor(ITaskItem assemblyItem, st
920933
aotArgs.Add(AotArguments);
921934
}
922935

923-
if (!string.IsNullOrEmpty(TempPath))
924-
{
925-
aotArgs.Add($"temp-path={TempPath}");
926-
}
927-
928936
if (!string.IsNullOrEmpty(LdName))
929937
{
930938
aotArgs.Add($"ld-name={LdName}");

src/tasks/Common/Builders/AndroidProject.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/tasks/Common/Utils.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ public static bool IsWindows()
354354
#endif
355355
}
356356

357+
public static bool IsMacOS()
358+
{
359+
#if NETCOREAPP
360+
return OperatingSystem.IsMacOS();
361+
#else
362+
return false;
363+
#endif
364+
}
365+
366+
public static bool IsLinux()
367+
{
368+
#if NETCOREAPP
369+
return OperatingSystem.IsLinux();
370+
#else
371+
return false;
372+
#endif
373+
}
374+
357375
public static bool IsManagedAssembly(string filePath)
358376
{
359377
if (!File.Exists(filePath))

0 commit comments

Comments
 (0)