add automatic detection of Visual Studio installation

This commit is contained in:
Cosmin Polifronie 2020-02-27 22:45:16 +02:00
parent c5418674b1
commit 88dff6c715
No known key found for this signature in database
GPG key ID: 0BEFE02346955102
3 changed files with 92 additions and 34 deletions

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