Skip to content

Commit 3822f2b

Browse files
grendellojonpryorjonathanpeppers
authored
[CoreCLR] Another set of changes from #9572 (#9807)
Context: #9572 PR #9572 is quite large. Extract out a set of changes for easier review. Co-authored-by: Jonathan Pryor <[email protected]> Co-authored-by: Jonathan Peppers <[email protected]>
1 parent f45700f commit 3822f2b

36 files changed

+519
-191
lines changed

Configuration.props

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,18 @@
5656
<MicrosoftAndroidSdkPackName Condition="$([MSBuild]::IsOSPlatform('osx'))">Microsoft.Android.Sdk.Darwin</MicrosoftAndroidSdkPackName>
5757
</PropertyGroup>
5858
<PropertyGroup>
59+
<MicrosoftAndroidPacksRootDir>$(BuildOutputDirectory)lib\packs\</MicrosoftAndroidPacksRootDir>
5960
<AutoProvision Condition=" '$(AutoProvision)' == '' ">False</AutoProvision>
6061
<AutoProvisionUsesSudo Condition=" '$(AutoProvisionUsesSudo)' == '' ">False</AutoProvisionUsesSudo>
6162
<_XABinRelativeInstallPrefix>lib\xamarin.android</_XABinRelativeInstallPrefix>
6263
<XAInstallPrefix Condition=" '$(XAInstallPrefix)' == '' ">$(MSBuildThisFileDirectory)bin\$(Configuration)\$(_XABinRelativeInstallPrefix)\</XAInstallPrefix>
6364
<_MonoAndroidNETOutputRoot>$(XAInstallPrefix)xbuild-frameworks\Microsoft.Android\</_MonoAndroidNETOutputRoot>
6465
<_MonoAndroidNETDefaultOutDir>$(_MonoAndroidNETOutputRoot)$(AndroidApiLevel)\</_MonoAndroidNETDefaultOutDir>
65-
<MicrosoftAndroidRefPackDir>$(BuildOutputDirectory)lib\packs\Microsoft.Android.Ref.$(AndroidApiLevel)\$(AndroidPackVersion)\ref\$(DotNetTargetFramework)\</MicrosoftAndroidRefPackDir>
66-
<MicrosoftAndroidSdkPackDir>$(BuildOutputDirectory)lib\packs\$(MicrosoftAndroidSdkPackName)\$(AndroidPackVersion)\</MicrosoftAndroidSdkPackDir>
66+
<NativeRuntimeOutputRootDir>$(BuildOutputDirectory)lib\runtimes\</NativeRuntimeOutputRootDir>
67+
<MicrosoftAndroidRefPackDir>$(MicrosoftAndroidPacksRootDir)Microsoft.Android.Ref.$(AndroidApiLevel)\$(AndroidPackVersion)\ref\$(DotNetTargetFramework)\</MicrosoftAndroidRefPackDir>
68+
<MicrosoftAndroidSdkPackDir>$(MicrosoftAndroidPacksRootDir)$(MicrosoftAndroidSdkPackName)\$(AndroidPackVersion)\</MicrosoftAndroidSdkPackDir>
6769
<MicrosoftAndroidSdkOutDir>$(MicrosoftAndroidSdkPackDir)\tools\</MicrosoftAndroidSdkOutDir>
68-
<MicrosoftAndroidSdkAnalysisOutDir>$(BuildOutputDirectory)lib\packs\Microsoft.Android.Ref.$(AndroidLatestStableApiLevel)\$(AndroidPackVersion)\analyzers\dotnet\cs\</MicrosoftAndroidSdkAnalysisOutDir>
70+
<MicrosoftAndroidSdkAnalysisOutDir>$(MicrosoftAndroidPacksRootDir)Microsoft.Android.Ref.$(AndroidLatestStableApiLevel)\$(AndroidPackVersion)\analyzers\dotnet\cs\</MicrosoftAndroidSdkAnalysisOutDir>
6971
<MakeConcurrency Condition=" '$(MakeConcurrency)' == '' And '$(HostCpuCount)' != '' ">-j$(HostCpuCount)</MakeConcurrency>
7072
<ManagedRuntime Condition=" '$(ManagedRuntime)' == '' And '$(OS)' != 'Windows_NT' ">mono</ManagedRuntime>
7173
<ManagedRuntimeArgs Condition=" '$(ManagedRuntimeArgs)' == '' And '$(ManagedRuntime)' == 'mono' ">--debug=casts</ManagedRuntimeArgs>

build-tools/create-packs/Microsoft.Android.Runtime.proj

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ projects that use the Microsoft.Android framework in .NET 6+.
3131
DependsOnTargets="_GetLicense">
3232
<PropertyGroup>
3333
<FrameworkListFile Condition="'$(FrameworkListFile)' == ''">$(IntermediateOutputPath)$(AndroidRID)\RuntimeList.xml</FrameworkListFile>
34+
<_RuntimeFlavorDirName Condition=" '$(AndroidRuntime)' == 'CoreCLR' ">clr</_RuntimeFlavorDirName>
35+
<_RuntimeFlavorDirName Condition=" '$(AndroidRuntime)' == 'Mono' Or '$(AndroidRuntime)' == '' ">mono</_RuntimeFlavorDirName>
3436
</PropertyGroup>
3537

3638
<ItemGroup>
@@ -45,12 +47,27 @@ projects that use the Microsoft.Android framework in .NET 6+.
4547
<_AndroidRuntimePackAssemblies Include="$(_MonoAndroidNETOutputRoot)$(AndroidLatestStableApiLevel)\Mono.Android.Export.dll" />
4648
</ItemGroup>
4749

50+
<ItemGroup Condition=" '$(AndroidRuntime)' == 'CoreCLR' ">
51+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libnet-android.debug.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libnet-android.debug.so" />
52+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libnet-android.release.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libnet-android.release.so" />
53+
</ItemGroup>
54+
4855
<ItemGroup Condition=" '$(AndroidRuntime)' == 'Mono' ">
49-
<_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.debug.so" />
50-
<_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libmono-android.release.so" />
51-
<_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libxamarin-debug-app-helper.so" />
52-
<_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libxamarin-native-tracing.so" />
53-
<_AndroidRuntimePackAssets Include="$(MicrosoftAndroidSdkOutDir)lib\$(AndroidRID)\libunwind_xamarin.a" />
56+
<_AndroidRuntimePackAssets Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libmono-android.debug.so" />
57+
<_AndroidRuntimePackAssets Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libmono-android.release.so" />
58+
<_AndroidRuntimePackAssets Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libxamarin-debug-app-helper.so" />
59+
<_AndroidRuntimePackAssets Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libxamarin-native-tracing.so" />
60+
<_AndroidRuntimePackAssets Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libunwind_xamarin.a" />
61+
</ItemGroup>
62+
63+
<ItemGroup Condition=" '$(AndroidRuntime)' == 'CoreCLR' ">
64+
<!-- TODO: the Exists() checks must go away once we build CoreCLR host for all the targets -->
65+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libc.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libc.so" />
66+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libdl.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libdl.so" />
67+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\liblog.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\liblog.so" />
68+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libm.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libm.so" />
69+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libz.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libz.so" />
70+
<_AndroidRuntimePackAssets Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libarchive-dso-stub.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libarchive-dso-stub.so" />
5471
</ItemGroup>
5572

5673
<ItemGroup>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace Android.Runtime;
4+
5+
enum DotNetRuntimeType
6+
{
7+
Unknown,
8+
MonoVM,
9+
CoreCLR,
10+
NativeAOT,
11+
}
12+
13+
// This looks weird, see comments in RuntimeTypeInternal.cs
14+
class DotNetRuntimeTypeConverter
15+
{
16+
// Values for the JnienvInitializeArgs.runtimeType field
17+
//
18+
// NOTE: Keep this in sync with managed side in src/native/common/include/managed-interface.hh
19+
const uint RuntimeTypeMonoVM = 0x0001;
20+
const uint RuntimeTypeCoreCLR = 0x0002;
21+
const uint RuntimeTypeNativeAOT = 0x0004;
22+
23+
public static DotNetRuntimeType Convert (uint runtimeType)
24+
{
25+
return runtimeType switch {
26+
RuntimeTypeMonoVM => DotNetRuntimeType.MonoVM,
27+
RuntimeTypeCoreCLR => DotNetRuntimeType.CoreCLR,
28+
RuntimeTypeNativeAOT => DotNetRuntimeType.NativeAOT,
29+
_ => throw new NotSupportedException ($"Internal error: unsupported runtime type {runtimeType:x}")
30+
};
31+
}
32+
}

src/Mono.Android/Android.Runtime/JNIEnvInit.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Android.Runtime
1313
static internal class JNIEnvInit
1414
{
1515
#pragma warning disable 0649
16-
// NOTE: Keep this in sync with the native side in src/native/monodroid/monodroid-glue-internal.hh
16+
// NOTE: Keep this in sync with the native side in src/native/common/include/managed-interface.hh
1717
internal struct JnienvInitializeArgs {
1818
public IntPtr javaVm;
1919
public IntPtr env;
@@ -32,6 +32,7 @@ internal struct JnienvInitializeArgs {
3232
public bool jniRemappingInUse;
3333
public bool marshalMethodsEnabled;
3434
public IntPtr grefGCUserPeerable;
35+
public uint runtimeType;
3536
}
3637
#pragma warning restore 0649
3738

@@ -48,6 +49,8 @@ internal struct JnienvInitializeArgs {
4849

4950
internal static JniRuntime? androidRuntime;
5051

52+
public static DotNetRuntimeType RuntimeType { get; private set; } = DotNetRuntimeType.Unknown;
53+
5154
[UnmanagedCallersOnly]
5255
static unsafe void RegisterJniNatives (IntPtr typeName_ptr, int typeName_len, IntPtr jniClass, IntPtr methods_ptr, int methods_len)
5356
{
@@ -87,6 +90,8 @@ internal static void InitializeJniRuntime (JniRuntime runtime)
8790
[UnmanagedCallersOnly]
8891
internal static unsafe void Initialize (JnienvInitializeArgs* args)
8992
{
93+
RuntimeType = DotNetRuntimeTypeConverter.Convert (args->runtimeType);
94+
9095
IntPtr total_timing_sequence = IntPtr.Zero;
9196
IntPtr partial_timing_sequence = IntPtr.Zero;
9297

src/Mono.Android/Mono.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
<Compile Include="Android.Runtime\BoundExceptionType.cs" />
222222
<Compile Include="Android.Runtime\CharSequence.cs" />
223223
<Compile Include="Android.Runtime\CPUArchitecture.cs" />
224+
<Compile Include="Android.Runtime\DotNetRuntimeType.cs" />
224225
<Compile Include="Android.Runtime\Extensions.cs" />
225226
<Compile Include="Android.Runtime\GeneratedEnumAttribute.cs" />
226227
<Compile Include="Android.Runtime\IJavaObject.cs" />

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,16 @@ void SaveResource (string resource, string filename, string destDir, Func<string
480480

481481
void WriteTypeMappings (NativeCodeGenState state)
482482
{
483+
if (androidRuntime == Xamarin.Android.Tasks.AndroidRuntime.NativeAOT) {
484+
// NativeAOT typemaps are generated in `Microsoft.Android.Sdk.ILLink.TypeMappingStep`
485+
return;
486+
}
487+
if (androidRuntime == Xamarin.Android.Tasks.AndroidRuntime.CoreCLR) {
488+
// TODO: CoreCLR typemaps will be emitted later
489+
return;
490+
}
483491
Log.LogDebugMessage ($"Generating type maps for architecture '{state.TargetArch}'");
484-
var tmg = new TypeMapGenerator (Log, state);
492+
var tmg = new TypeMapGenerator (Log, state, androidRuntime);
485493
if (!tmg.Generate (Debug, SkipJniAddNativeMethodRegistrationAttributeScan, TypemapOutputDirectory, GenerateNativeAssembly)) {
486494
throw new XamarinAndroidException (4308, Properties.Resources.XA4308);
487495
}

src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGenerator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public override string GetComment (object data, string fieldName)
3939
}
4040
}
4141

42+
// Disable "Field 'X' is never assigned to, and will always have its default value Y"
43+
// Classes below are used in native code generation, thus all the fields must be present
44+
// but they aren't always assigned values (which is fine).
45+
#pragma warning disable CS0649
46+
4247
// Order of fields and their type must correspond *exactly* (with exception of the
4348
// ignored managed members) to that in
4449
// src/monodroid/jni/xamarin-app.hh DSOCacheEntry structure
@@ -147,6 +152,7 @@ sealed class XamarinAndroidBundledAssembly
147152
[NativeAssembler (UsesDataProvider = true), NativePointer (PointsToPreAllocatedBuffer = true)]
148153
public string name;
149154
}
155+
#pragma warning restore CS0649
150156

151157
// Keep in sync with FORMAT_TAG in src/monodroid/jni/xamarin-app.hh
152158
const ulong FORMAT_TAG = 0x00025E6972616D58; // 'Xmari^XY' where XY is the format version

src/Xamarin.Android.Build.Tasks/Utilities/AssemblyStoreGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ string Generate (string baseOutputDirectory, AndroidTargetArch arch, List<Assemb
115115
// We'll start writing to the stream after we seek to the position just after the header, index, descriptors and name data.
116116
ulong curPos = assemblyDataStart;
117117

118+
Directory.CreateDirectory (Path.GetDirectoryName (storePath));
118119
using var fs = File.Open (storePath, FileMode.Create, FileAccess.Write, FileShare.Read);
119120
fs.Seek ((long)curPos, SeekOrigin.Begin);
120121

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrGenerator.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,9 @@ void WriteStructureValue (GeneratorWriteContext context, StructureInstance? inst
781781

782782
for (int i = 0; i < info.Members.Count; i++) {
783783
StructureMemberInfo smi = info.Members[i];
784+
if (!smi.IsSupportedForTarget (context.Target)) {
785+
continue;
786+
}
784787

785788
context.Output.Write (context.CurrentIndent);
786789
WriteType (context, instance, smi, out _);
@@ -810,7 +813,7 @@ void WriteStructureValue (GeneratorWriteContext context, StructureInstance? inst
810813
var sb = new StringBuilder (" ");
811814
sb.Append (MapManagedTypeToNative (context, smi));
812815
sb.Append (' ');
813-
sb.Append (smi.Info.Name);
816+
sb.Append (smi.MappedName);
814817
comment = sb.ToString ();
815818
}
816819
WriteCommentLine (context, comment);
@@ -848,6 +851,7 @@ void WriteArrayEntries (GeneratorWriteContext context, LlvmIrVariable variable,
848851
bool ignoreComments = stride > 1;
849852
string? prevItemComment = null;
850853
ulong counter = 0;
854+
bool writeStringInComment = !ignoreComments && (elementType == typeof(string) || elementType == typeof(StringHolder));
851855

852856
if (entries != null) {
853857
foreach (object entry in entries) {
@@ -871,7 +875,13 @@ void WriteArrayEntries (GeneratorWriteContext context, LlvmIrVariable variable,
871875
}
872876

873877
if (writeIndices && String.IsNullOrEmpty (prevItemComment)) {
874-
prevItemComment = $" {counter}";
878+
string stringComment = String.Empty;
879+
if (writeStringInComment) {
880+
var holder = StringHolder.AsHolder (entry);
881+
stringComment = $" ('{holder.Data}')";
882+
}
883+
884+
prevItemComment = $" {counter}{stringComment}";
875885
}
876886
}
877887

@@ -1098,6 +1108,10 @@ void WriteStructureDeclaration (GeneratorWriteContext context, StructureInfo si)
10981108
context.IncreaseIndent ();
10991109
for (int i = 0; i < si.Members.Count; i++) {
11001110
StructureMemberInfo info = si.Members[i];
1111+
if (!info.IsSupportedForTarget (context.Target)) {
1112+
continue;
1113+
}
1114+
11011115
string nativeType = MapManagedTypeToNative (info.MemberType);
11021116

11031117
// TODO: nativeType can be an array, update to indicate that (and get the size)
@@ -1108,7 +1122,7 @@ void WriteStructureDeclaration (GeneratorWriteContext context, StructureInfo si)
11081122
arraySize = String.Empty;
11091123
}
11101124

1111-
var comment = $" {nativeType} {info.Info.Name}{arraySize}";
1125+
var comment = $" {nativeType} {info.MappedName}{arraySize}";
11121126
WriteStructureDeclarationField (info.IRType, comment, i == si.Members.Count - 1);
11131127
}
11141128
context.DecreaseIndent ();

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/MemberInfoUtilities.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ public static LlvmIrStringEncoding GetStringEncoding (this MemberInfo mi, LlvmIr
5656
return attr.IsUTF16 ? LlvmIrStringEncoding.Unicode : DefaultStringEncoding;
5757
}
5858

59+
public static string? GetOverriddenName (this MemberInfo mi, LlvmIrTypeCache cache)
60+
{
61+
var attr = cache.GetNativeAssemblerAttribute (mi);
62+
return attr != null ? attr.MemberName : null;
63+
}
64+
65+
public static NativeAssemblerValidTarget GetValidTarget (this MemberInfo mi, LlvmIrTypeCache cache)
66+
{
67+
var attr = cache.GetNativeAssemblerAttribute (mi);
68+
return attr != null ? attr.ValidTarget : NativeAssemblerValidTarget.Any;
69+
}
70+
5971
public static bool ShouldBeIgnored (this MemberInfo mi, LlvmIrTypeCache cache)
6072
{
6173
var attr = cache.GetNativeAssemblerAttribute (mi);

0 commit comments

Comments
 (0)