From 88dff6c7154c9a02e3899980f4ebf825484578fa Mon Sep 17 00:00:00 2001 From: Cosmin Polifronie Date: Thu, 27 Feb 2020 22:45:16 +0200 Subject: [PATCH 1/2] add automatic detection of Visual Studio installation --- .../com/palantir/gradle/graal/FileUtil.java | 40 +++++++++++ .../palantir/gradle/graal/GraalExtension.java | 66 ++++++++++++++----- .../gradle/graal/GradleExtensionSpec.groovy | 20 +----- 3 files changed, 92 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/palantir/gradle/graal/FileUtil.java diff --git a/src/main/java/com/palantir/gradle/graal/FileUtil.java b/src/main/java/com/palantir/gradle/graal/FileUtil.java new file mode 100644 index 0000000..62938cd --- /dev/null +++ b/src/main/java/com/palantir/gradle/graal/FileUtil.java @@ -0,0 +1,40 @@ +/* + * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.palantir.gradle.graal; + +import java.io.File; +import java.util.AbstractSet; +import java.util.HashSet; +import java.util.List; + +public final class FileUtil { + public static String getFirstFromDirectory(File directory, List searchList) { + AbstractSet subDirectoriesNames = new HashSet(); + File[] subDirectories = directory.listFiles(File::isDirectory); + for (File subDirectory : subDirectories) { + subDirectoriesNames.add(subDirectory.getName()); + } + for (String stringToSearch : searchList) { + if (subDirectoriesNames.contains(stringToSearch)) { + return stringToSearch; + } + } + return null; + } + + private FileUtil() {} +} diff --git a/src/main/java/com/palantir/gradle/graal/GraalExtension.java b/src/main/java/com/palantir/gradle/graal/GraalExtension.java index d6077ab..d8dc526 100644 --- a/src/main/java/com/palantir/gradle/graal/GraalExtension.java +++ b/src/main/java/com/palantir/gradle/graal/GraalExtension.java @@ -16,6 +16,7 @@ package com.palantir.gradle.graal; +import java.io.File; import java.util.Arrays; import java.util.List; import org.gradle.api.GradleException; @@ -30,15 +31,16 @@ import org.gradle.api.provider.Provider; public class GraalExtension { private static final String WINDOWS_7_ENV_PATH = "C:\\Program Files\\Microsoft SDKs\\" + "Windows\\v7.1\\Bin\\SetEnv.cmd"; - private static final String DEFAULT_VS_VERSION = "2019"; - private static final String DEFAULT_VS_EDITION = "Community"; + private static final List SUPPORTED_VS_VERSIONS = Arrays.asList("2019", "2017"); + private static final List SUPPORTED_VS_EDITIONS = Arrays.asList("Enterprise", "Professional", "Community"); + private static final String DEFAULT_VS_PATH = "C:\\Program Files (x86)\\Microsoft Visual Studio"; private static final String DEFAULT_VS_VARS_PATH = "C:\\Program Files (x86)\\Microsoft Visual Studio\\" + "{version}\\{edition}\\VC\\Auxiliary\\Build\\vcvars64.bat"; private static final String DEFAULT_DOWNLOAD_BASE_URL = "https://github.com/oracle/graal/releases/download/"; private static final String DOWNLOAD_BASE_URL_GRAAL_19_3 = "https://github.com/graalvm/graalvm-ce-builds/releases/download/"; - private static final String DEFAULT_GRAAL_VERSION = "19.2.0"; - private static final List SUPPORTED_JAVA_VERSIONS = Arrays.asList("8", "11"); + private static final String DEFAULT_GRAAL_VERSION = "20.0.0"; + private static final List SUPPORTED_JAVA_VERSIONS = Arrays.asList("11", "8"); private static final String DEFAULT_JAVA_VERSION = "8"; private final Property downloadBaseUrl; @@ -74,8 +76,8 @@ public class GraalExtension { /** * Returns the base URL to use for downloading GraalVM binaries. * - *

Defaults to {@link #DEFAULT_DOWNLOAD_BASE_URL} for GraalVM < 19.3.

- *

Defaults to {@link #DOWNLOAD_BASE_URL_GRAAL_19_3} for GraalVM >= 19.3.

+ *

Defaults to {@link #DEFAULT_DOWNLOAD_BASE_URL} for GraalVM lower than 19.3.

+ *

Defaults to {@link #DOWNLOAD_BASE_URL_GRAAL_19_3} for GraalVM higher or equal to 19.3.

*/ public final Provider getDownloadBaseUrl() { return downloadBaseUrl @@ -123,7 +125,7 @@ public class GraalExtension { if (!SUPPORTED_JAVA_VERSIONS.contains(value)) { throw new GradleException("Java version " + value - + " is no supported. Supported versions are: " + + " is not supported. Supported versions are: " + SUPPORTED_JAVA_VERSIONS); } javaVersion.set(value); @@ -132,19 +134,51 @@ public class GraalExtension { /** * Returns the VS 64-bit Variables Path to use. * - *

Defaults to {@link #DEFAULT_VS_VARS_PATH} for JDK > 11

- *

Defaults to {@link #WINDOWS_7_ENV_PATH} for JDK < 11

+ *

Defaults to {@link #DEFAULT_VS_VARS_PATH} for JDK higher or equal to 11

+ *

Defaults to {@link #WINDOWS_7_ENV_PATH} for JDK lower than 11

*/ public final Provider getVsVarsPath() { - return vsVarsPath.orElse(getDefaultVsVarsPath()); + return vsVarsPath.orElse(searchVsVarsPath()); } - private String getDefaultVsVarsPath() { - return Integer.parseInt(javaVersion.get()) >= 11 - ? DEFAULT_VS_VARS_PATH - .replaceAll("\\{version}", vsVersion.getOrElse(DEFAULT_VS_VERSION)) - .replaceAll("\\{edition}", vsEdition.getOrElse(DEFAULT_VS_EDITION)) - : WINDOWS_7_ENV_PATH; + private String searchVsVarsPath() { + String searchedVsVersion = vsVersion.getOrElse(getNewestVsVersionInstalled()); + String searchedVsEdition = vsEdition.getOrElse(getBiggestVsEditionInstalled(searchedVsVersion)); + String searchedVsVarsPath = Integer.parseInt(javaVersion.get()) >= 11 + ? DEFAULT_VS_VARS_PATH + .replaceAll("\\{version}", searchedVsVersion) + .replaceAll("\\{edition}", searchedVsEdition) + : WINDOWS_7_ENV_PATH; + if (searchedVsVarsPath == WINDOWS_7_ENV_PATH) { + if (!new File(WINDOWS_7_ENV_PATH).exists()) { + throw new GradleException("Couldn't find an installation of Windows SDK 7.1 suitable for GraalVM. " + + "Searched location: " + + searchedVsVarsPath); + } + } + return searchedVsVarsPath; + } + + private String getNewestVsVersionInstalled() { + File directory = new File(DEFAULT_VS_PATH); + String searchedVsVersion = FileUtil.getFirstFromDirectory(directory, SUPPORTED_VS_VERSIONS); + if (searchedVsVersion != null) { + return searchedVsVersion; + } + throw new GradleException("Couldn't find an installation of Visual Studio suitable for GraalVM. " + + "Supported versions are: " + + Arrays.asList(SUPPORTED_VS_VERSIONS)); + } + + private String getBiggestVsEditionInstalled(String version) { + File directory = new File(DEFAULT_VS_PATH, version); + String searchedVsEdition = FileUtil.getFirstFromDirectory(directory, SUPPORTED_VS_EDITIONS); + if (searchedVsEdition != null) { + return searchedVsEdition; + } + throw new GradleException("Couldn't find an edition of Visual Studio suitable for GraalVM. " + + "Supported editions are: " + + Arrays.asList(SUPPORTED_VS_EDITIONS)); } public final void vsVarsPath(String value) { diff --git a/src/test/groovy/com/palantir/gradle/graal/GradleExtensionSpec.groovy b/src/test/groovy/com/palantir/gradle/graal/GradleExtensionSpec.groovy index d490ccf..060403c 100644 --- a/src/test/groovy/com/palantir/gradle/graal/GradleExtensionSpec.groovy +++ b/src/test/groovy/com/palantir/gradle/graal/GradleExtensionSpec.groovy @@ -97,32 +97,16 @@ class GradleExtensionSpec extends ProjectSpec { extension.getVsVarsPath().get() == "C:\\Program Files\\Microsoft SDKs\\Windows\\v7.1\\Bin\\SetEnv.cmd" } - def 'extension returns the correct VS Vars Path for default Java version 11'() { - when: - extension.javaVersion("11") - - then: - extension.getVsVarsPath().get() == "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat" - } - - def 'extension returns the correct VS Vars Path for default Java version 11 and different VS Edition'() { + def 'extension returns the correct VS Vars Path for Java version 11 and set VS Version and Edition'() { when: extension.javaVersion("11") + extension.vsVersion("2019") extension.vsEdition("Enterprise") then: extension.getVsVarsPath().get() == "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat" } - def 'extension returns the correct VS Vars Path for default Java version 11 and different VS Version'() { - when: - extension.javaVersion("11") - extension.vsVersion("2020") - - then: - extension.getVsVarsPath().get() == "C:\\Program Files (x86)\\Microsoft Visual Studio\\2020\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat" - } - def 'extension returns the provided VS Vars Path'() { when: extension.vsVarsPath("path") From 64106f6abf2d556515c2ca07b14c6b6d08ab5ef0 Mon Sep 17 00:00:00 2001 From: Cosmin Polifronie Date: Thu, 27 Feb 2020 22:45:55 +0200 Subject: [PATCH 2/2] update readme.md for the new GraalVM 19.3+ support --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 563eb3f..4e60a45 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,14 @@ Configuration Configure this plugin and its wrappers around GraalVM tools through the `graal` extension with the following options: **General GraalVM controls** -* `graalVersion`: the version string to use when downloading GraalVM (defaults to `19.2.0`) +* `graalVersion`: the version string to use when downloading GraalVM (defaults to `20.0.0`) * `downloadBaseUrl`: the base download URL to use (defaults to `https://github.com/oracle/graal/releases/download/`) +* `javaVersion`: the Java version to use (can be either `8` or `11`, defaults to `8`) + * for `8`: Windows SDK 7.1 will be used (`C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd`) + * for `11`: Visual Studio Build Tools will be used (`C:\Program Files (x86)\Microsoft Visual Studio\{version}\\{edition}\\VC\\Auxiliary\\Build\\vcvars64.bat`) + * `vsVersion`: the version of Visual Studio to use (can be either `2017` or `2019`, by default a search will be performed and the newest will be selected) + * `vsEdition`: the edition of Visual Studio to use (can be either `Community`, `Professional` or `Enterprise`, by default a search will be performed and the biggest will be selected) + * `vsVarsPath`: path to vcvars64.bat (optional, if it's set it will disable the 2 searches for `vsVersion` and `vsEdition`) **`native-image` controls**