Skip to content
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
3 changes: 3 additions & 0 deletions build-tools/installers/create-installers.targets
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)net\Microsoft.Android.Build.BaseTasks.pdb" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)net\Java.Interop.Localization.dll" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)net\Java.Interop.Localization.pdb" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)net\Xamarin.Android.Tools.AndroidSdk.dll" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)net\Xamarin.Android.Tools.AndroidSdk.pdb" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)net\%(Identity)\Microsoft.Android.Build.BaseTasks.resources.dll')" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)net\%(Identity)\Microsoft.Android.Build.Tasks.resources.dll')" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)net\%(Identity)\Java.Interop.Localization.resources.dll')" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)net\%(Identity)\Xamarin.Android.Tools.AndroidSdk.resources.dll')" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\Microsoft.Android.Build.BaseTasks.resources.dll')" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\Xamarin.Android.Build.Tasks.resources.dll')" />
<_MSBuildFiles Include="@(_LocalizationLanguages->'$(MicrosoftAndroidSdkOutDir)%(Identity)\Xamarin.Android.Tools.AndroidSdk.resources.dll')" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
<Compile Remove="Tests\**" />
<ProjectReference Include="..\Microsoft.Android.Build.BaseTasks\Microsoft.Android.Build.BaseTasks.csproj" />
<ProjectReference Include="..\Microsoft.Android.Sdk.TrimmableTypeMap\Microsoft.Android.Sdk.TrimmableTypeMap.csproj" />
<ProjectReference Include="..\Xamarin.Android.Tools.AndroidSdk\Xamarin.Android.Tools.AndroidSdk.csproj" />
<ProjectReference Include="..\..\external\Java.Interop\src\Java.Interop.Localization\Java.Interop.Localization.csproj" />
<InternalsVisibleTo Include="Microsoft.Android.Build.Tasks.Tests" Key="0024000004800000940000000602000000240000525341310004000011000000438ac2a5acfbf16cbd2b2b47a62762f273df9cb2795ceccdf77d10bf508e69e7a362ea7a45455bbf3ac955e1f2e2814f144e5d817efc4c6502cc012df310783348304e3ae38573c6d658c234025821fda87a0be8a0d504df564e2c93b2b878925f42503e9d54dfef9f9586d9e6f38a305769587b1de01f6c0410328b2c9733db" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Xamarin.Android.Build.Tasks\Utilities\NullableExtensions.cs" Link="Utilities\NullableExtensions.cs" />
<Compile Include="..\Xamarin.Android.Build.Tasks\Utilities\ManifestPlaceholderResolver.cs" Link="Utilities\ManifestPlaceholderResolver.cs" />
</ItemGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
#nullable enable
using System;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Android.Sdk.TrimmableTypeMap;
using Xamarin.Android.Tools;
using Microsoft.Android.Build.Tasks;
using Xamarin.Android.Tasks;
using Properties = Xamarin.Android.Tasks.Properties;

namespace Xamarin.Android.Tasks
namespace Microsoft.Android.Tasks
{
public class GetAndroidPackageName : AndroidTask
{
Expand All @@ -57,7 +58,7 @@ public override bool RunTask ()
if (reader.MoveToContent () == XmlNodeType.Element) {
var package = reader.GetAttribute ("package");
if (!package.IsNullOrEmpty ()) {
PackageName = ManifestDocument.ReplacePlaceholders (ManifestPlaceholders, package);
PackageName = ManifestPlaceholderResolver.Replace (ManifestPlaceholders, package);
}
}
}
Expand All @@ -72,7 +73,7 @@ public override bool RunTask ()
}

if (JavaNameValidator.TryGetInvalidPackageSegment (PackageName, '.', out var invalidIdentifier)) {
Log.LogCodedError ("XA4258", Properties.Resources.XA4258, PackageName, invalidIdentifier);
Log.LogCodedError ("XA4258", Properties.Resources.XA4258, PackageName, invalidIdentifier.ToString ());
}

Log.LogDebugMessage ($" PackageName: {PackageName}");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Microsoft.Build.Framework;
using NUnit.Framework;
using Xamarin.Android.Tasks;
using Microsoft.Android.Tasks;

namespace Xamarin.Android.Build.Tests {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
Expand Down Expand Up @@ -27,6 +28,8 @@ enum JniParamKind
/// </summary>
static class JniSignatureHelper
{
static readonly SearchValues<char> JavaSourceNameSeparators = SearchValues.Create ("/$");

/// <summary>
/// Parses the parameter types from a JNI method signature like "(Landroid/os/Bundle;)V".
/// </summary>
Expand Down Expand Up @@ -234,7 +237,11 @@ internal static void ValidateJniName (string jniName)
/// </summary>
internal static string JniNameToJavaName (string jniName)
{
return jniName.Replace ('/', '.').Replace ('$', '.');
if (jniName.AsSpan ().IndexOfAny (JavaSourceNameSeparators) < 0) {
return jniName;
}
return string.Create (jniName.Length, jniName, static (destination, name) =>
name.AsSpan ().ReplaceAny (destination, JavaSourceNameSeparators, '.'));
}

/// <summary>
Expand All @@ -259,7 +266,8 @@ internal static string JniNameToJavaBinaryName (string jniName)
if (lastSlash < 0) {
return null;
}
return jniName.Substring (0, lastSlash).Replace ('/', '.');
return string.Create (lastSlash, jniName, static (destination, name) =>
name.AsSpan (0, destination.Length).Replace (destination, '/', '.'));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
Expand All @@ -16,23 +17,31 @@ static class MetadataHelper
/// </summary>
public static Guid DeterministicMvid (string moduleName, ReadOnlySpan<byte> contentBytes = default)
{
using var sha = SHA256.Create ();
byte [] nameBytes = Encoding.UTF8.GetBytes (moduleName);
byte [] input = new byte [nameBytes.Length + contentBytes.Length];
nameBytes.CopyTo (input, 0);
contentBytes.CopyTo (input.AsSpan (nameBytes.Length));
byte [] hash = sha.ComputeHash (input);
byte [] guidBytes = new byte [16];
Array.Copy (hash, guidBytes, 16);
return new Guid (guidBytes);
const int stackallocThresholdBytes = 256;
int nameByteCount = Encoding.UTF8.GetByteCount (moduleName);
int byteCount = checked (nameByteCount + contentBytes.Length);
byte []? rented = null;
Span<byte> input = byteCount <= stackallocThresholdBytes
? stackalloc byte [byteCount]
: (rented = ArrayPool<byte>.Shared.Rent (byteCount));
Comment thread
simonrozsival marked this conversation as resolved.
Resolved
try {
Encoding.UTF8.GetBytes (moduleName.AsSpan (), input [..nameByteCount]);
contentBytes.CopyTo (input [nameByteCount..]);
Span<byte> hash = stackalloc byte [SHA256.HashSizeInBytes];
SHA256.HashData (input [..byteCount], hash);
return new Guid (hash [..16]);
} finally {
if (rented is not null) {
ArrayPool<byte>.Shared.Return (rented);
}
}
}

/// <summary>
/// Computes a content fingerprint for the given <see cref="TypeMapAssemblyData"/>.
/// </summary>
public static byte [] ComputeContentFingerprint (TypeMapAssemblyData data)
{
using var sha = SHA256.Create ();
using var stream = new MemoryStream ();
using var writer = new BinaryWriter (stream, Encoding.UTF8);
foreach (var entry in data.Entries) {
Expand Down Expand Up @@ -66,7 +75,7 @@ public static byte [] ComputeContentFingerprint (TypeMapAssemblyData data)
writer.Write (assoc.AliasProxyTypeReference);
}
writer.Flush ();
return sha.ComputeHash (stream.GetBuffer (), 0, checked ((int) stream.Length));
return SHA256.HashData (stream.GetBuffer ().AsSpan (0, checked ((int) stream.Length)));
}

/// <summary>
Expand All @@ -76,7 +85,6 @@ public static byte [] ComputeContentFingerprint (TypeMapAssemblyData data)
/// </summary>
public static byte [] ComputeIncrementalFingerprint (TypeMapAssemblyData data, Version systemRuntimeVersion, bool useSharedTypemapUniverse)
{
using var sha = SHA256.Create ();
using var stream = new MemoryStream ();
using var writer = new BinaryWriter (stream, Encoding.UTF8);
writer.Write (GeneratorModuleVersionId.ToByteArray ());
Expand Down Expand Up @@ -137,7 +145,7 @@ public static byte [] ComputeIncrementalFingerprint (TypeMapAssemblyData data, V
writer.Write (assemblyName);
}
writer.Flush ();
return sha.ComputeHash (stream.GetBuffer (), 0, checked ((int) stream.Length));
return SHA256.HashData (stream.GetBuffer ().AsSpan (0, checked ((int) stream.Length)));
}

/// <summary>
Expand All @@ -148,7 +156,6 @@ public static byte [] ComputeRootIncrementalFingerprint (
Version systemRuntimeVersion,
bool useSharedTypemapUniverse)
{
using var sha = SHA256.Create ();
using var stream = new MemoryStream ();
using var writer = new BinaryWriter (stream, Encoding.UTF8);
writer.Write (GeneratorModuleVersionId.ToByteArray ());
Expand All @@ -159,7 +166,7 @@ public static byte [] ComputeRootIncrementalFingerprint (
writer.Write (assemblyName);
}
writer.Flush ();
return sha.ComputeHash (stream.GetBuffer (), 0, checked ((int) stream.Length));
return SHA256.HashData (stream.GetBuffer ().AsSpan (0, checked ((int) stream.Length)));
}

static void WriteTypeRef (this BinaryWriter writer, TypeRefData type)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Microsoft.Android.Sdk.TrimmableTypeMap;

Expand All @@ -15,6 +15,10 @@ namespace Microsoft.Android.Sdk.TrimmableTypeMap;
static class ModelBuilder
{
const string ProxyTypeSuffix = "_Proxy";
const string AliasHolderTypeSuffix = "_Aliases";

static readonly SearchValues<char> ManagedTypeNameSeparators = SearchValues.Create (".+`");
static readonly SearchValues<char> JniNameSeparators = SearchValues.Create ("/$");

static readonly HashSet<string> EssentialRuntimeTypes = new (StringComparer.Ordinal) {
"java/lang/Object",
Expand Down Expand Up @@ -162,7 +166,7 @@ static void EmitPeers (TypeMapAssemblyData model, string jniName,
// Alias group: generate an alias holder and indexed entries for each peer.
// The base JNI name maps to the alias holder; each peer gets "[0]", "[1]", etc.
var aliasKeys = new List<string> ();
string holderTypeName = jniName.Replace ('/', '_').Replace ('$', '_') + "_Aliases";
string holderTypeName = JniNameToAliasHolderTypeName (jniName);
var holderNamespace = "_TypeMap.Aliases";
string holderRef = AssemblyQualify ($"{holderNamespace}.{holderTypeName}", assemblyName);

Expand Down Expand Up @@ -252,18 +256,18 @@ static void AddIfCrossAssembly (SortedSet<string> set, string? asmName, string o

static string ManagedTypeNameToProxyTypeName (string managedTypeName)
{
var builder = new StringBuilder (managedTypeName.Length + ProxyTypeSuffix.Length);
AppendSafeManagedTypeName (builder, managedTypeName);
builder.Append (ProxyTypeSuffix);
return builder.ToString ();
return string.Create (managedTypeName.Length + ProxyTypeSuffix.Length, managedTypeName, static (destination, name) => {
name.AsSpan ().ReplaceAny (destination, ManagedTypeNameSeparators, '_');
ProxyTypeSuffix.AsSpan ().CopyTo (destination [name.Length..]);
});
}

static void AppendSafeManagedTypeName (StringBuilder builder, string managedTypeName)
static string JniNameToAliasHolderTypeName (string jniName)
{
for (int i = 0; i < managedTypeName.Length; i++) {
char c = managedTypeName [i];
builder.Append (c == '.' || c == '+' || c == '`' ? '_' : c);
}
return string.Create (jniName.Length + AliasHolderTypeSuffix.Length, jniName, static (destination, name) => {
name.AsSpan ().ReplaceAny (destination, JniNameSeparators, '_');
AliasHolderTypeSuffix.AsSpan ().CopyTo (destination [name.Length..]);
});
}

static JavaPeerProxyData BuildProxyType (JavaPeerInfo peer, string jniName, HashSet<string> usedProxyNames, bool isAcw)
Expand Down
Loading