Skip to content

[native] Common infrastructure for native shared library linking #9990

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions Configuration.props
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@
AndroidRuntime="CoreCLR" />
</ItemGroup>

<PropertyGroup>
<_RuntimeRedistDirName>redist</_RuntimeRedistDirName>
</PropertyGroup>

<!-- Whenever there's a need to use a locally built CoreCLR, both .NET for Android and the
application(s) must be built with the same runtime. CLRLocalRuntimePath must point to the CoreCLR
artifact directory in its repository checkout:
Expand Down
11 changes: 5 additions & 6 deletions build-tools/create-packs/Microsoft.Android.Runtime.proj
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ projects that use the Microsoft.Android framework in .NET 6+.
</ItemGroup>

<ItemGroup Condition=" '$(AndroidRuntime)' != 'NativeAOT' ">
<!-- TODO: the Exists() checks must go away once we build CoreCLR host for all the targets -->
<NativeRuntimeAsset Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libc.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libc.so" />
<NativeRuntimeAsset Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libdl.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libdl.so" />
<NativeRuntimeAsset Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\liblog.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\liblog.so" />
<NativeRuntimeAsset Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libm.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libm.so" />
<NativeRuntimeAsset Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libz.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libz.so" />
<NativeRuntimeAsset Include="$(NativeRuntimeOutputRootDir)$(_RuntimeRedistDirName)\$(AndroidRID)\libc.so" />
<NativeRuntimeAsset Include="$(NativeRuntimeOutputRootDir)$(_RuntimeRedistDirName)\$(AndroidRID)\libdl.so" />
<NativeRuntimeAsset Include="$(NativeRuntimeOutputRootDir)$(_RuntimeRedistDirName)\$(AndroidRID)\liblog.so" />
<NativeRuntimeAsset Include="$(NativeRuntimeOutputRootDir)$(_RuntimeRedistDirName)\$(AndroidRID)\libm.so" />
<NativeRuntimeAsset Include="$(NativeRuntimeOutputRootDir)$(_RuntimeRedistDirName)\$(AndroidRID)\libz.so" />
<NativeRuntimeAsset Condition=" Exists('$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libarchive-dso-stub.so') " Include="$(NativeRuntimeOutputRootDir)$(_RuntimeFlavorDirName)\$(AndroidRID)\libarchive-dso-stub.so" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ static class KnownProperties
public const string MonoRequiredMinimumVersion = "MonoRequiredMinimumVersion";
public const string MonoRequiredMaximumVersion = "MonoRequiredMaximumVersion";
public const string MonoSourceFullPath = "MonoSourceFullPath";
public const string NativeRuntimeOutputRootDir = "NativeRuntimeOutputRootDir";
public const string NinjaPath = "NinjaPath";
public const string Pkg7Zip_CommandLine = "Pkg7-Zip_CommandLine";
public const string PkgXamarin_LibZipSharp = "PkgXamarin_LibZipSharp";
public const string ProductVersion = "ProductVersion";
public const string RuntimeRedistDirName = "_RuntimeRedistDirName";
public const string XABuildToolsFolder = "XABuildToolsFolder";
public const string XABuildToolsVersion = "XABuildToolsVersion";
public const string XABuildToolsPackagePrefixMacOS = "XABuildToolsPackagePrefixMacOS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ namespace Xamarin.Android.Prepare
properties.Add (KnownProperties.MonoRequiredMinimumVersion, StripQuotes ("@MonoRequiredMinimumVersion@"));
properties.Add (KnownProperties.MonoRequiredMaximumVersion, StripQuotes ("@MonoRequiredMaximumVersion@"));
properties.Add (KnownProperties.MonoSourceFullPath, StripQuotes (@"@MonoSourceFullPath@"));
properties.Add (KnownProperties.NativeRuntimeOutputRootDir, StripQuotes (@"@NativeRuntimeOutputRootDir@"));
properties.Add (KnownProperties.NinjaPath, StripQuotes (@"@NinjaPath@"));
properties.Add (KnownProperties.Pkg7Zip_CommandLine, StripQuotes (@"@Pkg7-Zip_CommandLine@"));
properties.Add (KnownProperties.PkgXamarin_LibZipSharp, StripQuotes (@"@PkgXamarin_LibZipSharp@"));
properties.Add (KnownProperties.ProductVersion, StripQuotes ("@ProductVersion@"));
properties.Add (KnownProperties.RuntimeRedistDirName, StripQuotes ("@_RuntimeRedistDirName@"));
properties.Add (KnownProperties.XABuildToolsFolder, StripQuotes (@"@XABuildToolsFolder@"));
properties.Add (KnownProperties.XABuildToolsVersion, StripQuotes ("@XABuildToolsVersion@"));
properties.Add (KnownProperties.XABuildToolsPackagePrefixMacOS, StripQuotes ("@XABuildToolsPackagePrefixMacOS@"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ public static partial class Defaults
{ "x86_64", "x86_64-linux-android" },
};

public static readonly Dictionary<string, string> AbiToRID = new (StringComparer.Ordinal) {
{ "armeabi-v7a", "android-arm" },
{ "arm64-v8a", "android-arm64" },
{ "x86", "android-x86" },
{ "x86_64", "android-x64" },
};

public static readonly List <NDKTool> NDKTools = new List<NDKTool> {
// Tools prefixed with architecture triple
new NDKTool (name: "as", prefixed: true),
Expand Down
58 changes: 58 additions & 0 deletions build-tools/xaprepare/xaprepare/Steps/Step_Android_SDK_NDK.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -22,6 +23,14 @@ sealed class AndroidPackage
}
#nullable enable

static readonly string[] CRTFiles = {
"libc.so",
"libdl.so",
"liblog.so",
"libm.so",
"libz.so",
};

bool RefreshSdk = false;
bool RefreshNdk = false;
AndroidToolchainComponentType DependencyTypeToInstall = AndroidToolchainComponentType.All;
Expand Down Expand Up @@ -151,13 +160,62 @@ bool AcceptLicenses (Context context, string sdkRoot)

bool GatherNDKInfo (Context context)
{
if (!CopyRedistributableFiles (context)) {
return false;
}

// Ignore NDK property setting if not installing the NDK
if (!DependencyTypeToInstall.HasFlag (AndroidToolchainComponentType.BuildDependency))
return true;
else
return context.BuildInfo.GatherNDKInfo (context);
}

bool CopyRedistributableFiles (Context context)
{
string androidVersionPath = Path.Combine (Configurables.Paths.AndroidToolchainRootDirectory, "AndroidVersion.txt");
if (!File.Exists (androidVersionPath)) {
throw new InvalidOperationException ($"Android version file '{androidVersionPath}' not found");
}

string[]? lines = File.ReadAllLines (androidVersionPath);
if (lines == null || lines.Length < 1) {
throw new InvalidOperationException ($"Unknown format of Android version file '{androidVersionPath}'");
}

// First line is (should be) the LLVM version, we need just the main release number
string[] llvmVersion = lines[0].Split ('.');
if (llvmVersion.Length < 3) {
throw new InvalidOperationException ($"Unknown LLVM version format for '{lines[0]}'");
}

foreach (var kvp in Configurables.Defaults.AndroidToolchainPrefixes) {
string abi = kvp.Key;
string abiDir = Path.Combine (Configurables.Paths.AndroidToolchainSysrootLibDirectory, kvp.Value);
string crtFilesPath = Path.Combine (abiDir, BuildAndroidPlatforms.NdkMinimumAPI.ToString (CultureInfo.InvariantCulture));

foreach (string file in CRTFiles) {
CopyFile (abi, crtFilesPath, file);
}
}

return true;

void CopyFile (string abi, string sourceDir, string fileName)
{
Log.StatusLine ($" {context.Characters.Bullet} Copying NDK redistributable: ", $"{fileName} ({abi})", tailColor: ConsoleColor.White);
string rid = Configurables.Defaults.AbiToRID [abi];
string outputDir = Path.Combine (
context.Properties.GetRequiredValue (KnownProperties.NativeRuntimeOutputRootDir),
context.Properties.GetRequiredValue (KnownProperties.RuntimeRedistDirName),
rid
);

string sourceFile = Path.Combine (sourceDir, fileName);
Utilities.CopyFileToDir (sourceFile, outputDir);
}
}

void CheckPackageStatus (Context context, string packageCacheDir, AndroidPackage pkg, List <AndroidPackage> toDownload)
{
Log.StatusLine ($" {context.Characters.Bullet} Installing ", pkg.Component.Name, tailColor: ConsoleColor.White);
Expand Down
2 changes: 2 additions & 0 deletions build-tools/xaprepare/xaprepare/xaprepare.targets
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@
<Replacement Include="@MonoRequiredMinimumVersion@=$(MonoRequiredMinimumVersion)" />
<Replacement Include="@MonoRequiredMaximumVersion@=$(MonoRequiredMaximumVersion)" />
<Replacement Include="@MonoSourceFullPath@=$(MonoSourceFullPath)" />
<Replacement Include="@NativeRuntimeOutputRootDir@=$(NativeRuntimeOutputRootDir)" />
<Replacement Include="@NinjaPath@=$(NinjaPath)" />
<Replacement Include="@Pkg7-Zip_CommandLine@=$(Pkg7-Zip_CommandLine)" />
<Replacement Include="@PkgXamarin_LibZipSharp@=$(PkgXamarin_LibZipSharp)" />
<Replacement Include="@ProductVersion@=$(ProductVersion)" />
<Replacement Include="@_RuntimeRedistDirName@=$(_RuntimeRedistDirName)" />
<Replacement Include="@XABuildToolsFolder@=$(XABuildToolsFolder)" />
<Replacement Include="@XABuildToolsVersion@=$(XABuildToolsVersion)" />
<Replacement Include="@XABuildToolsPackagePrefixMacOS@=$(XABuildToolsPackagePrefixMacOS)" />
Expand Down
Loading
Loading