Skip to content

Commit

Permalink
[Xamarin.Android.Tools.AndroidSdk] JdkInfo + JDK11 + Windows (#88)
Browse files Browse the repository at this point in the history
Context: xamarin/xamarin-android#4567

Commit 36d7fee added support for JetBrains OpenJDK 11 detection on
macOS and Linux.

Lacking was *Windows* support for JetBrains OpenJDK 11, because of
course it has to be different.

In particular, OpenJDK 11 *moves the `jvm` library* for Windows.
We checked for it in `{HomePath}\jre\**\jvm.dll` or
`{HomePath}\lib\**\jvm.dll`, but neither of those exist.  Instead,
OpenJDK 11 has `jvm.dll` in `{HomePath}\bin\server\jvm.dll`, which is
also the only `.dll` file in the `.tar.gz` which exports the symbol
`JNI_CreateJavaVM`.

Update `JdkInfo` so that it looks for `{HomePath}\bin\server\jvm.dll`
to populate `JdkInfo.JdkJvmPath`.
  • Loading branch information
jonpryor committed Jun 4, 2020
1 parent 5552b07 commit 3974fc3
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs
Expand Up @@ -14,10 +14,6 @@
namespace Xamarin.Android.Tools
{
public class JdkInfo {
static readonly string[] JdkLibraryTopDirs = {
"jre",
"lib",
};

public string HomePath {get;}

Expand Down Expand Up @@ -58,27 +54,14 @@ public JdkInfo (string homePath)
JavaPath = ProcessUtils.FindExecutablesInDirectory (binPath, "java").FirstOrDefault ();
JavacPath = ProcessUtils.FindExecutablesInDirectory (binPath, "javac").FirstOrDefault ();

string? topDir = null;
foreach (string dir in JdkLibraryTopDirs) {
topDir = Path.Combine (HomePath, dir);
if (!Directory.Exists (topDir)) {
topDir = null;
continue;
}
break;
}

if (String.IsNullOrEmpty (topDir))
topDir = Path.Combine (HomePath, JdkLibraryTopDirs [0]);

JdkJvmPath = OS.IsMac
? FindLibrariesInDirectory (topDir!, "jli").FirstOrDefault ()
: FindLibrariesInDirectory (topDir!, "jvm").FirstOrDefault ();
string? jdkJvmPath = GetJdkJvmPath ();

ValidateFile ("jar", JarPath);
ValidateFile ("java", JavaPath);
ValidateFile ("javac", JavacPath);
ValidateFile ("jvm", JdkJvmPath);
ValidateFile ("jvm", jdkJvmPath);

JdkJvmPath = jdkJvmPath!;

var includes = new List<string> ();
var jdkInclude = Path.Combine (HomePath, "include");
Expand Down Expand Up @@ -132,6 +115,24 @@ public bool GetJavaSettingsPropertyValue (string key, [NotNullWhen (true)] out s
return false;
}

string? GetJdkJvmPath ()
{
string jreDir = Path.Combine (HomePath, "jre");
string libDir = Path.Combine (HomePath, "lib");

if (OS.IsMac) {
return FindLibrariesInDirectory (jreDir, "jli").FirstOrDefault () ??
FindLibrariesInDirectory (libDir, "jli").FirstOrDefault ();
}
if (OS.IsWindows) {
string binServerDir = Path.Combine (HomePath, "bin", "server");
return FindLibrariesInDirectory (jreDir, "jvm").FirstOrDefault () ??
FindLibrariesInDirectory (binServerDir, "jvm").FirstOrDefault ();
}
return FindLibrariesInDirectory (jreDir, "jvm").FirstOrDefault () ??
FindLibrariesInDirectory (libDir, "jvm").FirstOrDefault ();
}

static IEnumerable<string> FindLibrariesInDirectory (string dir, string libraryName)
{
if (!Directory.Exists (dir))
Expand All @@ -140,7 +141,7 @@ static IEnumerable<string> FindLibrariesInDirectory (string dir, string libraryN
return Directory.EnumerateFiles (dir, library, SearchOption.AllDirectories);
}

void ValidateFile (string name, string path)
void ValidateFile (string name, string? path)
{
if (path == null || !File.Exists (path))
throw new ArgumentException ($"Could not find required file `{name}` within `{HomePath}`; is this a valid JDK?", "homePath");
Expand Down

0 comments on commit 3974fc3

Please sign in to comment.