Merge pull request #2 from cosminpolifronie/issue/239

Add automatic detection of Visual Studio installation
This commit is contained in:
Daniel Mühlbachler 2020-03-17 09:20:22 +01:00 committed by GitHub
commit 2a5dfceb69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 35 deletions

View file

@ -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**

View file

@ -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<String> searchList) {
AbstractSet<String> subDirectoriesNames = new HashSet<String>();
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() {}
}

View file

@ -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<String> SUPPORTED_VS_VERSIONS = Arrays.asList("2019", "2017");
private static final List<String> 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<String> SUPPORTED_JAVA_VERSIONS = Arrays.asList("8", "11");
private static final String DEFAULT_GRAAL_VERSION = "20.0.0";
private static final List<String> SUPPORTED_JAVA_VERSIONS = Arrays.asList("11", "8");
private static final String DEFAULT_JAVA_VERSION = "8";
private final Property<String> downloadBaseUrl;
@ -74,8 +76,8 @@ public class GraalExtension {
/**
* Returns the base URL to use for downloading GraalVM binaries.
*
* <p>Defaults to {@link #DEFAULT_DOWNLOAD_BASE_URL} for GraalVM < 19.3.</p>
* <p>Defaults to {@link #DOWNLOAD_BASE_URL_GRAAL_19_3} for GraalVM >= 19.3.</p>
* <p>Defaults to {@link #DEFAULT_DOWNLOAD_BASE_URL} for GraalVM lower than 19.3.</p>
* <p>Defaults to {@link #DOWNLOAD_BASE_URL_GRAAL_19_3} for GraalVM higher or equal to 19.3.</p>
*/
public final Provider<String> 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.
*
* <p>Defaults to {@link #DEFAULT_VS_VARS_PATH} for JDK > 11</p>
* <p>Defaults to {@link #WINDOWS_7_ENV_PATH} for JDK < 11</p>
* <p>Defaults to {@link #DEFAULT_VS_VARS_PATH} for JDK higher or equal to 11</p>
* <p>Defaults to {@link #WINDOWS_7_ENV_PATH} for JDK lower than 11</p>
*/
public final Provider<String> 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) {

View file

@ -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")