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

Bump to xamarin/java.interop/main@07c73009 #8681

Merged
merged 14 commits into from
Feb 2, 2024
Merged
2 changes: 1 addition & 1 deletion external/Java.Interop
12 changes: 8 additions & 4 deletions src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,20 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership

JNIEnv.DeleteLocalRef (class_ptr);

if (targetType != null &&
(type == null ||
!targetType.IsAssignableFrom (type))) {
type = targetType;
}

if (type == null) {
class_name = JNIEnv.GetClassNameFromInstance (handle);
JNIEnv.DeleteRef (handle, transfer);
throw new NotSupportedException (
FormattableString.Invariant ($"Internal error finding wrapper class for '{JNIEnv.GetClassNameFromInstance (handle)}'. (Where is the Java.Lang.Object wrapper?!)"),
FormattableString.Invariant ($"Internal error finding wrapper class for '{class_name}'. (Where is the Java.Lang.Object wrapper?!)"),
CreateJavaLocationException ());
}

if (targetType != null && !targetType.IsAssignableFrom (type))
type = targetType;

if (type.IsInterface || type.IsAbstract) {
var invokerType = JavaObjectExtensions.GetInvokerType (type);
if (invokerType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void GenerateCompressedAssemblySources ()

void Generate (IDictionary<string, CompressedAssemblyInfo> dict)
{
var composer = new CompressedAssembliesNativeAssemblyGenerator (dict);
var composer = new CompressedAssembliesNativeAssemblyGenerator (Log, dict);
LLVMIR.LlvmIrModule compressedAssemblies = composer.Construct ();

foreach (string abi in SupportedAbis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ void SaveResource (string resource, string filename, string destDir, Func<string

void WriteTypeMappings (List<JavaType> types, TypeDefinitionCache cache)
{
var tmg = new TypeMapGenerator ((string message) => Log.LogDebugMessage (message), SupportedAbis);
var tmg = new TypeMapGenerator (Log, SupportedAbis);
if (!tmg.Generate (Debug, SkipJniAddNativeMethodRegistrationAttributeScan, types, cache, TypemapOutputDirectory, GenerateNativeAssembly, out ApplicationConfigTaskState appConfState)) {
throw new XamarinAndroidException (4308, Properties.Resources.XA4308);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override bool RunTask ()

void GenerateEmpty ()
{
Generate (new JniRemappingAssemblyGenerator (), typeReplacementsCount: 0);
Generate (new JniRemappingAssemblyGenerator (Log), typeReplacementsCount: 0);
}

void Generate ()
Expand All @@ -74,7 +74,7 @@ void Generate ()
}
}

Generate (new JniRemappingAssemblyGenerator (typeReplacements, methodReplacements), typeReplacements.Count);
Generate (new JniRemappingAssemblyGenerator (Log, typeReplacements, methodReplacements), typeReplacements.Count);
}

void Generate (JniRemappingAssemblyGenerator jniRemappingComposer, int typeReplacementsCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ void AddEnvironment ()

if (enableMarshalMethods) {
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (
Log,
assemblyCount,
uniqueAssemblyNames,
marshalMethodsState?.MarshalMethods,
Log
marshalMethodsState?.MarshalMethods
);
} else {
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (assemblyCount, uniqueAssemblyNames);
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (Log, assemblyCount, uniqueAssemblyNames);
}
LLVMIR.LlvmIrModule marshalMethodsModule = marshalMethodsAsmGen.Construct ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ private void GenerateResponseFile ()
file.Replace (@"\", @"\\").Normalize (NormalizationForm.FormC)));
}
}
Log.LogDebugMessage ($"javac response file contents: {TemporarySourceListFile}");
foreach (var line in File.ReadLines (TemporarySourceListFile)) {
Log.LogDebugMessage ($" {line}");
}
}
}
}
18 changes: 9 additions & 9 deletions src/Xamarin.Android.Build.Tasks/Tasks/R8.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Collections.Generic;
Expand Down Expand Up @@ -82,16 +83,15 @@ protected override CommandLineBuilder GetCommandLineBuilder ()

if (EnableShrinking) {
if (!string.IsNullOrEmpty (AcwMapFile)) {
var acwLines = File.ReadAllLines (AcwMapFile);
var acwMap = MonoAndroidHelper.LoadMapFile (BuildEngine4, Path.GetFullPath (AcwMapFile), StringComparer.OrdinalIgnoreCase);
var javaTypes = new List<string> (acwMap.Values.Count);
foreach (var v in acwMap.Values) {
javaTypes.Add (v);
}
javaTypes.Sort (StringComparer.Ordinal);
using (var appcfg = File.CreateText (ProguardGeneratedApplicationConfiguration)) {
for (int i = 0; i + 2 < acwLines.Length; i += 3) {
try {
var line = acwLines [i + 2];
var java = line.Substring (line.IndexOf (';') + 1);
appcfg.WriteLine ("-keep class " + java + " { *; }");
} catch {
// skip invalid lines
}
foreach (var java in javaTypes) {
appcfg.WriteLine ($"-keep class {java} {{ *; }}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Java.Interop.Tools.TypeNameMappings;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;
using Xamarin.Android.Tasks.LLVMIR;

namespace Xamarin.Android.Tasks
Expand Down Expand Up @@ -135,7 +136,6 @@ sealed class XamarinAndroidBundledAssembly

SortedDictionary <string, string>? environmentVariables;
SortedDictionary <string, string>? systemProperties;
TaskLoggingHelper log;
StructureInstance? application_config;
List<StructureInstance<DSOCacheEntry>>? dsoCache;
List<StructureInstance<XamarinAndroidBundledAssembly>>? xamarinAndroidBundledAssemblies;
Expand Down Expand Up @@ -172,6 +172,7 @@ sealed class XamarinAndroidBundledAssembly
public bool MarshalMethodsEnabled { get; set; }

public ApplicationConfigNativeAssemblyGenerator (IDictionary<string, string> environmentVariables, IDictionary<string, string> systemProperties, TaskLoggingHelper log)
: base (log)
{
if (environmentVariables != null) {
this.environmentVariables = new SortedDictionary<string, string> (environmentVariables, StringComparer.Ordinal);
Expand All @@ -180,8 +181,6 @@ public ApplicationConfigNativeAssemblyGenerator (IDictionary<string, string> env
if (systemProperties != null) {
this.systemProperties = new SortedDictionary<string, string> (systemProperties, StringComparer.Ordinal);
}

this.log = log;
}

protected override void Construct (LlvmIrModule module)
Expand Down Expand Up @@ -322,7 +321,7 @@ List<StructureInstance<DSOCacheEntry>> InitDSOCache ()
continue;
}

dsos.Add ((name, $"dsoName{dsos.Count.ToString (CultureInfo.InvariantCulture)}", ELFHelper.IsEmptyAOTLibrary (log, item.ItemSpec)));
dsos.Add ((name, $"dsoName{dsos.Count.ToString (CultureInfo.InvariantCulture)}", ELFHelper.IsEmptyAOTLibrary (Log, item.ItemSpec)));
}

var dsoCache = new List<StructureInstance<DSOCacheEntry>> ();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;

using Microsoft.Build.Utilities;

using Xamarin.Android.Tasks.LLVMIR;

namespace Xamarin.Android.Tasks
Expand Down Expand Up @@ -66,7 +68,8 @@ sealed class CompressedAssemblies
StructureInfo compressedAssemblyDescriptorStructureInfo;
StructureInfo compressedAssembliesStructureInfo;

public CompressedAssembliesNativeAssemblyGenerator (IDictionary<string, CompressedAssemblyInfo> assemblies)
public CompressedAssembliesNativeAssemblyGenerator (TaskLoggingHelper log, IDictionary<string, CompressedAssemblyInfo> assemblies)
: base (log)
{
this.assemblies = assemblies;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Text;

using Microsoft.Build.Utilities;

using Xamarin.Android.Tasks.LLVMIR;

namespace Xamarin.Android.Tasks
Expand Down Expand Up @@ -190,10 +192,12 @@ sealed class JniRemappingTypeReplacementEntry

public int ReplacementMethodIndexEntryCount { get; private set; } = 0;

public JniRemappingAssemblyGenerator ()
public JniRemappingAssemblyGenerator (TaskLoggingHelper log)
: base (log)
{}

public JniRemappingAssemblyGenerator (List<JniRemappingTypeReplacement> typeReplacements, List<JniRemappingMethodReplacement> methodReplacements)
public JniRemappingAssemblyGenerator (TaskLoggingHelper log, List<JniRemappingTypeReplacement> typeReplacements, List<JniRemappingMethodReplacement> methodReplacements)
: base (log)
{
this.typeReplacementsInput = typeReplacements ?? throw new ArgumentNullException (nameof (typeReplacements));
this.methodReplacementsInput = methodReplacements ?? throw new ArgumentNullException (nameof (methodReplacements));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO.Hashing;
using System.Text;

using Microsoft.Build.Utilities;

using Xamarin.Android.Tools;

namespace Xamarin.Android.Tasks.LLVMIR
Expand All @@ -11,6 +13,13 @@ abstract class LlvmIrComposer
{
bool constructed;

protected readonly TaskLoggingHelper Log;

protected LlvmIrComposer (TaskLoggingHelper log)
{
this.Log = log ?? throw new ArgumentNullException (nameof (log));
}

protected abstract void Construct (LlvmIrModule module);

public LlvmIrModule Construct ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ public MarshalMethodAssemblyIndexValuePlaceholder (MarshalMethodInfo mmi, Assemb
ICollection<string> uniqueAssemblyNames;
int numberOfAssembliesInApk;
IDictionary<string, IList<MarshalMethodEntry>> marshalMethods;
TaskLoggingHelper logger;

StructureInfo marshalMethodsManagedClassStructureInfo;
StructureInfo marshalMethodNameStructureInfo;
Expand All @@ -243,7 +242,8 @@ public MarshalMethodAssemblyIndexValuePlaceholder (MarshalMethodInfo mmi, Assemb
/// <summary>
/// Constructor to be used ONLY when marshal methods are DISABLED
/// </summary>
public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames)
public MarshalMethodsNativeAssemblyGenerator (TaskLoggingHelper log, int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames)
: base (log)
{
this.numberOfAssembliesInApk = numberOfAssembliesInApk;
this.uniqueAssemblyNames = uniqueAssemblyNames ?? throw new ArgumentNullException (nameof (uniqueAssemblyNames));
Expand All @@ -254,12 +254,12 @@ public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, IColl
/// <summary>
/// Constructor to be used ONLY when marshal methods are ENABLED
/// </summary>
public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames, IDictionary<string, IList<MarshalMethodEntry>> marshalMethods, TaskLoggingHelper logger)
public MarshalMethodsNativeAssemblyGenerator (TaskLoggingHelper log, int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames, IDictionary<string, IList<MarshalMethodEntry>> marshalMethods)
: base (log)
{
this.numberOfAssembliesInApk = numberOfAssembliesInApk;
this.uniqueAssemblyNames = uniqueAssemblyNames ?? throw new ArgumentNullException (nameof (uniqueAssemblyNames));
this.marshalMethods = marshalMethods;
this.logger = logger ?? throw new ArgumentNullException (nameof (logger));

generateEmptyCode = false;
defaultCallMarker = LlvmIrCallMarker.Tail;
Expand Down Expand Up @@ -334,7 +334,7 @@ void Init ()

foreach (MarshalMethodInfo method in allMethods) {
if (seenNativeSymbols.Contains (method.NativeSymbolName)) {
logger.LogDebugMessage ($"Removed MM duplicate '{method.NativeSymbolName}' (implemented: {method.Method.ImplementedMethod.FullName}; registered: {method.Method.RegisteredMethod.FullName}");
Log.LogDebugMessage ($"Removed MM duplicate '{method.NativeSymbolName}' (implemented: {method.Method.ImplementedMethod.FullName}; registered: {method.Method.RegisteredMethod.FullName}");
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;

using Microsoft.Build.Utilities;
using Microsoft.Android.Build.Tasks;

namespace Xamarin.Android.Tasks
{
class NativeTypeMappingData
Expand All @@ -12,7 +15,7 @@ class NativeTypeMappingData
public uint MapModuleCount { get; }
public uint JavaTypeCount { get; }

public NativeTypeMappingData (TypeMapGenerator.ModuleReleaseData[] modules)
public NativeTypeMappingData (TaskLoggingHelper log, TypeMapGenerator.ModuleReleaseData[] modules)
{
Modules = modules ?? throw new ArgumentNullException (nameof (modules));

Expand All @@ -21,15 +24,37 @@ public NativeTypeMappingData (TypeMapGenerator.ModuleReleaseData[] modules)
var tempJavaTypes = new Dictionary<string, TypeMapGenerator.TypeMapReleaseEntry> (StringComparer.Ordinal);
var moduleComparer = new TypeMapGenerator.ModuleUUIDArrayComparer ();

TypeMapGenerator.ModuleReleaseData? monoAndroid = null;
foreach (TypeMapGenerator.ModuleReleaseData data in modules) {
if (data.AssemblyName == "Mono.Android") {
monoAndroid = data;
break;
}
}

if (monoAndroid != null) {
ProcessModule (monoAndroid);
}

foreach (TypeMapGenerator.ModuleReleaseData data in modules) {
if (data.AssemblyName == "Mono.Android") {
continue;
}
ProcessModule (data);
};

void ProcessModule (TypeMapGenerator.ModuleReleaseData data)
{
int moduleIndex = Array.BinarySearch (modules, data, moduleComparer);
if (moduleIndex < 0)
throw new InvalidOperationException ($"Unable to map module with MVID {data.Mvid} to array index");

foreach (TypeMapGenerator.TypeMapReleaseEntry entry in data.Types) {
entry.ModuleIndex = moduleIndex;
if (tempJavaTypes.ContainsKey (entry.JavaName))
if (tempJavaTypes.ContainsKey (entry.JavaName)) {
log.LogDebugMessage ($"Skipping typemap entry for `{entry.ManagedTypeName}, {data.AssemblyName}`; `{entry.JavaName}` is already mapped.");
continue;
}
tempJavaTypes.Add (entry.JavaName, entry);
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/Xamarin.Android.Build.Tasks/Utilities/TypeMapGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Linq;
using System.Text;

using Microsoft.Build.Utilities;

using Java.Interop.Tools.Cecil;
using Mono.Cecil;
using Microsoft.Android.Build.Tasks;
Expand Down Expand Up @@ -133,17 +135,17 @@ public void AddKnownAssembly (TypeDefinition td)
public string GetAssemblyName (TypeDefinition td) => td.Module.Assembly.FullName;
}

Action<string> logger;
TaskLoggingHelper log;
Encoding outputEncoding;
byte[] moduleMagicString;
byte[] typemapIndexMagicString;
string[] supportedAbis;

public IList<string> GeneratedBinaryTypeMaps { get; } = new List<string> ();

public TypeMapGenerator (Action<string> logger, string[] supportedAbis)
public TypeMapGenerator (TaskLoggingHelper log, string[] supportedAbis)
{
this.logger = logger ?? throw new ArgumentNullException (nameof (logger));
this.log = log ?? throw new ArgumentNullException (nameof (log));
if (supportedAbis == null)
throw new ArgumentNullException (nameof (supportedAbis));
this.supportedAbis = supportedAbis;
Expand Down Expand Up @@ -257,7 +259,7 @@ bool GenerateDebugFiles (bool skipJniAddNativeMethodRegistrationAttributeScan, L
}
GeneratedBinaryTypeMaps.Add (typeMapIndexPath);

var composer = new TypeMappingDebugNativeAssemblyGenerator (new ModuleDebugData ());
var composer = new TypeMappingDebugNativeAssemblyGenerator (log, new ModuleDebugData ());
GenerateNativeAssembly (composer, composer.Construct (), outputDirectory);

return true;
Expand Down Expand Up @@ -289,7 +291,7 @@ bool GenerateDebugNativeAssembly (bool skipJniAddNativeMethodRegistrationAttribu

PrepareDebugMaps (data);

var composer = new TypeMappingDebugNativeAssemblyGenerator (data);
var composer = new TypeMappingDebugNativeAssemblyGenerator (log, data);
GenerateNativeAssembly (composer, composer.Construct (), outputDirectory);

return true;
Expand Down Expand Up @@ -443,7 +445,7 @@ void ProcessReleaseType (ReleaseGenerationState state, TypeDefinition td, Androi
// This is disabled because it costs a lot of time (around 150ms per standard XF Integration app
// build) and has no value for the end user. The message is left here because it may be useful to us
// in our devloop at some point.
//logger ($"Warning: duplicate Java type name '{entry.JavaName}' in assembly '{moduleData.AssemblyName}' (new token: {entry.Token}).");
//log.LogDebugMessage ($"Warning: duplicate Java type name '{entry.JavaName}' in assembly '{moduleData.AssemblyName}' (new token: {entry.Token}).");
moduleData.DuplicateTypes.Add (entry);
} else {
moduleData.TypesScratch.Add (entry.JavaName, entry);
Expand Down Expand Up @@ -482,7 +484,7 @@ bool GenerateRelease (bool skipJniAddNativeMethodRegistrationAttributeScan, List
module.Types = module.TypesScratch.Values.ToArray ();
}

var composer = new TypeMappingReleaseNativeAssemblyGenerator (new NativeTypeMappingData (modules));
var composer = new TypeMappingReleaseNativeAssemblyGenerator (log, new NativeTypeMappingData (log, modules));
GenerateNativeAssembly (arch, composer, composer.Construct (), outputDirectory);
}

Expand Down
Loading