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

Assembly blobs (sort of) per RID #8449

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<MonoOptionsVersion>6.12.0.148</MonoOptionsVersion>
<SystemCollectionsImmutableVersion>6.0.0</SystemCollectionsImmutableVersion>
<SystemRuntimeCompilerServicesUnsafeVersion>6.0.0</SystemRuntimeCompilerServicesUnsafeVersion>
<ELFSharpVersion>2.13.1</ELFSharpVersion>
<ELFSharpVersion>2.16.1</ELFSharpVersion>
<HumanizerVersion>2.14.1</HumanizerVersion>
<MdocPackageVersion Condition=" '$(MdocPackageVersion)' == '' ">5.8.9.2</MdocPackageVersion>
</PropertyGroup>
Expand Down
4 changes: 4 additions & 0 deletions build-tools/installers/create-installers.targets
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,16 @@
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)ManifestOverlays\Timing.xml" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-arm64\libc.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-arm64\libm.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-arm64\libassembly-blob.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-arm\libc.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-arm\libm.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-arm\libassembly-blob.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-x64\libc.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-x64\libm.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-x64\libassembly-blob.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-x86\libc.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-x86\libm.so" />
<_MSBuildFiles Include="$(MicrosoftAndroidSdkOutDir)libstubs\android-x86\libassembly-blob.so" />
</ItemGroup>
<ItemGroup>
<_MSBuildTargetsSrcFiles Include="$(MSBuildTargetsSrcDir)\Xamarin.Android.AvailableItems.targets" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Xamarin.Android.Prepare
{
class BuildAndroidPlatforms
{
public const string AndroidNdkVersion = "26";
public const string AndroidNdkPkgRevision = "26.0.10792818";
public const string AndroidNdkVersion = "26b";
public const string AndroidNdkPkgRevision = "26.1.10909125";
public const int NdkMinimumAPI = 21;
public const int NdkMinimumAPILegacy32 = 21;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<assembly fullname="System.Private.CoreLib">
<type fullname="System.Reflection.AssemblyMetadataAttribute" />
</assembly>
</linker>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;

using Mono.Cecil;
using Mono.Linker;
using Mono.Linker.Steps;

#if ILLINK
using Microsoft.Android.Sdk.ILLink;
#endif

namespace MonoDroid.Tuner;

public class AddRidMetadataAttributeStep : BaseStep
{
protected override void ProcessAssembly (AssemblyDefinition assembly)
{
if (!Annotations.HasAction (assembly)) {
return;
}

var action = Annotations.GetAction (assembly);
if (action == AssemblyAction.Skip || action == AssemblyAction.Delete) {
return;
}

string? rid = null;
#if ILLINK
if (!Context.TryGetCustomData ("XARuntimeIdentifier", out rid)) {
throw new InvalidOperationException ("Missing XARuntimeIdentifier custom data");
}
#endif
if (String.IsNullOrEmpty (rid)) {
throw new InvalidOperationException ("RID must have a non-empty value");
}

AssemblyDefinition corlib = GetCorlib ();
MethodDefinition assemblyMetadataAttributeCtor = FindAssemblyMetadataAttributeCtor (corlib);
TypeDefinition systemString = GetSystemString (corlib);

var attr = new CustomAttribute (assembly.MainModule.ImportReference (assemblyMetadataAttributeCtor));
attr.ConstructorArguments.Add (new CustomAttributeArgument (systemString, "XamarinAndroidAbi")); // key

// TODO: figure out how to get the RID...
attr.ConstructorArguments.Add (new CustomAttributeArgument (systemString, rid)); // value

assembly.CustomAttributes.Add (attr);

if (action == AssemblyAction.Copy) {
Annotations.SetAction (assembly, AssemblyAction.Save);
}
}

TypeDefinition GetSystemString (AssemblyDefinition asm) => FindType (asm, "System.String", required: true);

AssemblyDefinition GetCorlib ()
{
const string ImportAssembly = "System.Private.CoreLib";
AssemblyDefinition? asm = Context.Resolve (AssemblyNameReference.Parse (ImportAssembly));
if (asm == null) {
throw new InvalidOperationException ($"Unable to import assembly '{ImportAssembly}'");
}

return asm;
}

MethodDefinition FindAssemblyMetadataAttributeCtor (AssemblyDefinition asm)
{
const string AttributeType = "System.Reflection.AssemblyMetadataAttribute";

TypeDefinition assemblyMetadataAttribute = FindType (asm, AttributeType, required: true);
foreach (MethodDefinition md in assemblyMetadataAttribute!.Methods) {
if (!md.IsConstructor) {
continue;
}

return md;
}

throw new InvalidOperationException ($"Unable to find the {AttributeType} type constructor");
}

TypeDefinition? FindType (AssemblyDefinition asm, string typeName, bool required)
{
foreach (ModuleDefinition md in asm.Modules) {
foreach (TypeDefinition et in md.Types) {
if (String.Compare (typeName, et.FullName, StringComparison.Ordinal) != 0) {
continue;
}

return et;
}
}

if (required) {
throw new InvalidOperationException ($"Internal error: required type '{typeName}' in assembly {asm} not found");
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ This file is imported *after* the Microsoft.NET.Sdk/Sdk.targets.
<Import Project="Microsoft.Android.Sdk.Publish.targets" />
<Import Project="Microsoft.Android.Sdk.RuntimeConfig.targets" />
<Import Project="Microsoft.Android.Sdk.Tooling.targets" />
<Import Project="Microsoft.Android.Sdk.NativeCompilation.targets" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,35 @@ properties that determine build ordering.
_IncludeNativeSystemLibraries;
_CheckGoogleSdkRequirements;
</_PrepareBuildApkDependsOnTargets>

<_GenerateJavaStubsDependsOnTargets>
_SetLatestTargetFrameworkVersion;
_PrepareAssemblies;
_PrepareNativeAssemblySources;
$(_AfterPrepareAssemblies);
</_GenerateJavaStubsDependsOnTargets>

<_GeneratePackageManagerJavaDependsOn>
_GenerateJavaStubs;
_RunAotForAllRIDs;
_ManifestMerger;
_ConvertCustomView;
$(_AfterConvertCustomView);
_AddStaticResources;
$(_AfterAddStaticResources);
_PrepareAssemblies;
_PrepareEnvironmentAssemblySources;
_GenerateEnvironmentFiles;
_GenerateAndroidRemapNativeCode;
_GenerateEmptyAndroidRemapNativeCode;
_IncludeNativeSystemLibraries;
</_GeneratePackageManagerJavaDependsOn>

<_GenerateAndroidRemapNativeCodeDependsOn>
_ConvertAndroidMamMappingFileToXml;
_CollectAndroidRemapMembers;
_PrepareAndroidRemapNativeAssemblySources
</_GenerateAndroidRemapNativeCodeDependsOn>
</PropertyGroup>

<PropertyGroup Condition=" '$(AndroidApplication)' != 'True' ">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This file contains the .NET 5-specific targets to customize ILLink
https://github.com/dotnet/sdk/blob/a5393731b5b7b225692fff121f747fbbc9e8b140/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.ILLink.targets#L147
-->
<_TrimmerCustomData Include="XATargetFrameworkDirectories" Value="$(_XATargetFrameworkDirectories)" />
<_TrimmerCustomData Include="XARuntimeIdentifier" Value="$(RuntimeIdentifier)" Condition=" '$(_AndroidAddRuntimeIdentifierToAssemblies)' == 'true' " />
<_TrimmerCustomData
Condition=" '$(_ProguardProjectConfiguration)' != '' "
Include="ProguardConfiguration"
Expand Down Expand Up @@ -94,6 +95,12 @@ This file contains the .NET 5-specific targets to customize ILLink
BeforeStep="MarkStep"
Type="MonoDroid.Tuner.FixLegacyResourceDesignerStep"
/>
<_TrimmerCustomSteps
Condition=" '$(_AndroidAddRuntimeIdentifierToAssemblies)' == 'true' "
Include="$(_AndroidLinkerCustomStepAssembly)"
AfterStep="CleanStep"
Type="MonoDroid.Tuner.AddRidMetadataAttributeStep"
/>
<_PreserveLists Include="$(MSBuildThisFileDirectory)..\PreserveLists\*.xml" />
<TrimmerRootDescriptor
Condition=" '@(ResolvedFileToPublish->Count())' != '0' and '%(Filename)' != '' "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<UsingTask TaskName="Xamarin.Android.Tasks.CompileNativeAssembly" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.GenerateJniRemappingNativeCode" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.LinkApplicationSharedLibraries" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />
<UsingTask TaskName="Xamarin.Android.Tasks.PrepareAbiItems" AssemblyFile="$(_XamarinAndroidBuildTasksAssembly)" />

<PropertyGroup>
<_AndroidUseAssemblySharedLibraries Condition=" '$(EmbedAssembliesIntoApk)' != 'true' or '$(AndroidIncludeDebugSymbols)' == 'true' ">false</_AndroidUseAssemblySharedLibraries>
<_AndroidUseAssemblySharedLibraries Condition=" '$(_AndroidUseAssemblySharedLibraries)' == '' ">true</_AndroidUseAssemblySharedLibraries>

<_AndroidApplicationSharedLibraryPath>$(IntermediateOutputPath)app_shared_libraries\</_AndroidApplicationSharedLibraryPath>
<_CompressedAssembliesOutputDir>$(IntermediateOutputPath)android\lz4-2</_CompressedAssembliesOutputDir>

<_AndroidKeepStandaloneAssemblySources Condition=" '$(_AndroidKeepStandaloneAssemblySources)' == '' ">false</_AndroidKeepStandaloneAssemblySources>
</PropertyGroup>

<!-- Native code preparation targets -->
<Target Name="_PrepareNativeAssemblySources">
<PrepareAbiItems
BuildTargetAbis="@(_BuildTargetAbis)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
InstantRunEnabled="$(_InstantRunEnabled)"
Debug="$(AndroidIncludeDebugSymbols)"
Mode="typemap">
<Output TaskParameter="AssemblySources" ItemName="_TypeMapAssemblySource" />
<Output TaskParameter="AssemblyIncludes" ItemName="_TypeMapAssemblyInclude" />
</PrepareAbiItems>

<PrepareAbiItems
BuildTargetAbis="@(_BuildTargetAbis)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
InstantRunEnabled="$(_InstantRunEnabled)"
Debug="$(AndroidIncludeDebugSymbols)"
Mode="marshal_methods">
<Output TaskParameter="AssemblySources" ItemName="_MarshalMethodsAssemblySource" />
</PrepareAbiItems>
</Target>

<Target Name="_PrepareEnvironmentAssemblySources">
<PrepareAbiItems
BuildTargetAbis="@(_BuildTargetAbis)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
InstantRunEnabled="$(_InstantRunEnabled)"
Debug="$(AndroidIncludeDebugSymbols)"
Mode="environment">
<Output TaskParameter="AssemblySources" ItemName="_EnvironmentAssemblySource" />
</PrepareAbiItems>
</Target>

<Target Name="_PrepareAssemblyDSOSources">


</Target>

<Target Name="_PrepareAndroidRemapNativeAssemblySources">
<PrepareAbiItems
BuildTargetAbis="@(_BuildTargetAbis)"
NativeSourcesDir="$(_NativeAssemblySourceDir)"
InstantRunEnabled="$(_InstantRunEnabled)"
Debug="$(AndroidIncludeDebugSymbols)"
Mode="jniremap">
<Output TaskParameter="AssemblySources" ItemName="_AndroidRemapAssemblySource" />
</PrepareAbiItems>
</Target>

<Target Name="_PrepareNativeAssemblyItems" DependsOnTargets="_GenerateJavaStubs">
<ItemGroup>
<_NativeAssemblyTarget Include="@(_TypeMapAssemblySource->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_TypeMapAssemblySource.abi)</abi>
</_NativeAssemblyTarget>

<_NativeAssemblyTarget Include="@(_EnvironmentAssemblySource->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_EnvironmentAssemblySource.abi)</abi>
</_NativeAssemblyTarget>

<_NativeAssemblyTarget Include="@(_MarshalMethodsAssemblySource->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_MarshalMethodsAssemblySource.abi)</abi>
</_NativeAssemblyTarget>

<_NativeAssemblyTarget Include="@(_AndroidRemapAssemblySource->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_AndroidRemapAssemblySource.abi)</abi>
</_NativeAssemblyTarget>

<_NativeAssemblyTarget Include="@(_AssemblyDSOSourceApplication->'$([System.IO.Path]::ChangeExtension('%(Identity)', '.o'))')">
<abi>%(_AssemblyDSOSourceApplication.abi)</abi>
</_NativeAssemblyTarget>
</ItemGroup>
</Target>

<Target Name="_PrepareApplicationSharedLibraryItems">
<ItemGroup>
<_ApplicationSharedLibrary Include="$(_AndroidApplicationSharedLibraryPath)%(_BuildTargetAbis.Identity)\libxamarin-app.so">
<abi>%(_BuildTargetAbis.Identity)</abi>
</_ApplicationSharedLibrary>
</ItemGroup>
</Target>

<!-- Native source code generation targets -->
<Target Name="_GenerateEmptyAndroidRemapNativeCode"
DependsOnTargets="_PrepareAndroidRemapNativeAssemblySources"
Condition=" '@(_AndroidRemapMembers->Count())' == '0' "
Inputs="$(MSBuildProjectFullPath)"
Outputs="@(_AndroidRemapAssemblySource)">
<GenerateJniRemappingNativeCode
OutputDirectory="$(_NativeAssemblySourceDir)"
GenerateEmptyCode="True"
SupportedAbis="@(_BuildTargetAbis)"
/>
</Target>

<Target Name="_GenerateAndroidRemapNativeCode"
DependsOnTargets="$(_GenerateAndroidRemapNativeCodeDependsOn)"
Condition=" '@(_AndroidRemapMembers->Count())' != '0' "
Inputs="$(_XARemapMembersFilePath)"
Outputs="@(_AndroidRemapAssemblySource)">
<GenerateJniRemappingNativeCode
OutputDirectory="$(_NativeAssemblySourceDir)"
RemappingXmlFilePath="$(_XARemapMembersFilePath)"
SupportedAbis="@(_BuildTargetAbis)"
/>
</Target>

<!-- Native code compilation targets -->
<Target Name="_CompileApplicationNativeAssemblySources"
DependsOnTargets="_PrepareNativeAssemblyItems"
Inputs="@(_TypeMapAssemblySource);@(_TypeMapAssemblyInclude);@(_EnvironmentAssemblySource);@(_MarshalMethodsAssemblySource);@(_AndroidRemapAssemblySource);@(_AssemblyDSOSourceApplication)"
Outputs="@(_NativeAssemblyTarget)">
<CompileNativeAssembly
Sources="@(_TypeMapAssemblySource);@(_EnvironmentAssemblySource);@(_MarshalMethodsAssemblySource);@(_AndroidRemapAssemblySource);@(_AssemblyDSOSourceApplication)"
DebugBuild="$(AndroidIncludeDebugSymbols)"
WorkingDirectory="$(_NativeAssemblySourceDir)"
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
/>
<ItemGroup>
<FileWrites Include="@(_NativeAssemblyTarget)" />
</ItemGroup>
</Target>

<!-- Shared library creation targets -->
<PropertyGroup>
<!-- Order of targets is **crucial** here -->
<_CreateApplicationSharedLibrariesDependsOn>
_PrepareAssemblyDSOSources;
_CompileApplicationNativeAssemblySources;
_PrepareApplicationSharedLibraryItems
</_CreateApplicationSharedLibrariesDependsOn>
</PropertyGroup>

<Target Name="_CreateApplicationSharedLibraries"
DependsOnTargets="$(_CreateApplicationSharedLibrariesDependsOn)"
Inputs="@(_NativeAssemblyTarget);@(_StandaloneAssemblyDSO)"
Outputs="@(_ApplicationSharedLibrary)">
<LinkApplicationSharedLibraries
ObjectFiles="@(_NativeAssemblyTarget)"
ApplicationSharedLibraries="@(_ApplicationSharedLibrary)"
DebugBuild="$(AndroidIncludeDebugSymbols)"
AndroidBinUtilsDirectory="$(AndroidBinUtilsDirectory)"
/>
<ItemGroup>
<FileWrites Include="@(_ApplicationSharedLibrary)" />
</ItemGroup>
</Target>
</Project>
Loading
Loading