Skip to content

Commit 14f94a5

Browse files
authored
[Java.Interop.BootstrapTasks] Filter out invalid JDKs (#1278)
@jonpryor's developer machines are a "weird", with a macOS arm64 box containing the directories: * `$HOME/android-toolchain/jdk-17` * `$HOME/android-toolchain/jdk-17.x64` Meanwhile, `XAPrepareJdkLocations.GetXAPrepareJdks()` within dotnet/android-tools looks for all directories matching the glob `$HOME/android-toolchains/jdk*`, and thus finds both. For some reason, the JDK that `dotnet build -t:Prepare *.sln` winds up using by default is the the `jdk-17.x64` directory (😱), which cannot be loaded into an arm64 process. This results in errors from `dotnet build`: EXEC : error JM4003: jnimarshalmethod-gen: Unable to create Java VM […/dotnet/java-interop/samples/Hello-NativeAOTFromJNI/Hello-NativeAOTFromJNI.csproj] System.DllNotFoundException: Unable to load shared library '$HOME/android-toolchain/jdk-17.x64/lib/libjli.dylib' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen($HOME/android-toolchain/jdk-17.x64/lib/libjli.dylib, 0x0001): tried: '$HOME/android-toolchain/jdk-17.x64/lib/libjli.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/$HOME/android-toolchain/jdk-17.x64/lib/libjli.dylib' (no such file), '$HOME/android-toolchain/jdk-17.x64/lib/libjli.dylib' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e' or 'arm64')) at System.Runtime.InteropServices.NativeLibrary.Load(String libraryPath) at Java.Interop.NativeLibraryJvmLibraryHandler.LoadJvmLibrary(String path) in $HOME/Developer/src/dotnet/java-interop/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs:line 231 at Java.Interop.JreRuntime.CreateJreVM(JreRuntimeOptions builder) in $HOME/Developer/src/dotnet/java-interop/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs:line 104 at Java.Interop.JreRuntime..ctor(JreRuntimeOptions builder) in $HOME/Developer/src/dotnet/java-interop/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs:line 162 at Java.Interop.JreRuntimeOptions.CreateJreVM() in $HOME/Developer/src/dotnet/java-interop/src/Java.Runtime.Environment/Java.Interop/JreRuntime.cs:line 66 at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.CreateJavaVM(String jvmDllPath) in $HOME/Developer/src/dotnet/java-interop/tools/jnimarshalmethod-gen/App.cs:line 302 … …/dotnet/java-interop/samples/Hello-NativeAOTFromJNI/Hello-NativeAOTFromJNI.targets(44,5): error MSB3073: The command ""/usr/local/share/dotnet/dotnet" "…/dotnet/java-interop/bin/Debug-net8.0//jnimarshalmethod-gen.dll" "…/dotnet/java-interop/samples/Hello-NativeAOTFromJNI/bin/Debug/Hello-NativeAOTFromJNI.dll" -v -v --keeptemp -L "…/dotnet/java-interop/samples/Hello-NativeAOTFromJNI/bin/Debug/" " exited with code 3. […/dotnet/java-interop/samples/Hello-NativeAOTFromJNI/Hello-NativeAOTFromJNI.csproj] Instead of doing the "obvious" of removing `jdk.x64` or renaming it so it doesn't match the glob `jdk*`, update `JdkInfo.GetKnownSystemJdkInfos()` and `JdkInfo.GetSupportedJdkInfos()` to filter out JDKs that cannot be loaded into the current process. This nicely filters out `jdk-17.x64`, allowing dotnet/java-interop to build successfully.
1 parent 2440416 commit 14f94a5

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Diff for: build-tools/Java.Interop.BootstrapTasks/Java.Interop.BootstrapTasks.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
6+
<LangVersion>11.0</LangVersion>
67
</PropertyGroup>
78

89
<Import Project="..\..\TargetFrameworkDependentValues.props" />

Diff for: build-tools/Java.Interop.BootstrapTasks/Java.Interop.BootstrapTasks/JdkInfo.cs

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Diagnostics;
66
using System.IO;
77
using System.Linq;
8+
using System.Runtime.InteropServices;
89
using System.Text;
910
using System.Text.RegularExpressions;
1011
using System.Xml.Linq;
@@ -46,6 +47,7 @@ public override bool Execute ()
4647
.Where (j => maxVersion != null ? j.Version <= maxVersion : true)
4748
.Where (j => j.IncludePath.Any ());
4849
var jdk = explicitJdks.Concat (defaultJdks)
50+
.Where (j => JdkRunsOnHost (j))
4951
.FirstOrDefault ();
5052

5153
if (jdk == null) {
@@ -71,6 +73,19 @@ public override bool Execute ()
7173
return !Log.HasLoggedErrors;
7274
}
7375

76+
static bool JdkRunsOnHost (XATInfo jdk)
77+
{
78+
var cputype = RuntimeInformation.ProcessArchitecture;
79+
if (jdk.ReleaseProperties.TryGetValue ("OS_ARCH", out var arch)) {
80+
return (cputype, arch) switch {
81+
(Architecture.Arm64, "aarch64") => true,
82+
(Architecture.X64, "x86_64") => true,
83+
_ => false,
84+
};
85+
}
86+
return true;
87+
}
88+
7489
XATInfo[] GetJdkRoots ()
7590
{
7691
XATInfo jdk = null;

0 commit comments

Comments
 (0)