src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/JVMCIVersionCheck.java
changeset 54328 37648a9c4a6a
parent 54084 84f10bbf993f
child 54724 62f373a53296
equal deleted inserted replaced
54327:a4d19817609c 54328:37648a9c4a6a
     1 /*
     1 /*
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    23 
    23 
    24 
    24 
    25 package org.graalvm.compiler.hotspot;
    25 package org.graalvm.compiler.hotspot;
    26 
    26 
    27 import java.util.Formatter;
    27 import java.util.Formatter;
       
    28 import java.util.HashMap;
       
    29 import java.util.Map;
       
    30 import java.util.Properties;
    28 
    31 
    29 /**
    32 /**
    30  * Mechanism for checking that the current Java runtime environment supports the minimum JVMCI API
    33  * Mechanism for checking that the current Java runtime environment supports the minimum JVMCI API
    31  * required by Graal. The {@code JVMCI_VERSION_CHECK} environment variable can be used to ignore a
    34  * required by Graal. The {@code JVMCI_VERSION_CHECK} environment variable can be used to ignore a
    32  * failed check ({@code JVMCI_VERSION_CHECK=ignore}) or print a warning (
    35  * failed check ({@code JVMCI_VERSION_CHECK=ignore}) or print a warning (
    36  *
    39  *
    37  * This class only depends on the JDK so that it can be used without building Graal.
    40  * This class only depends on the JDK so that it can be used without building Graal.
    38  */
    41  */
    39 class JVMCIVersionCheck {
    42 class JVMCIVersionCheck {
    40 
    43 
    41     // 0.55 introduces new HotSpotSpeculationLog API
    44     // 0.57 introduces HotSpotJVMCIRuntime.excludeFromJVMCICompilation
    42     private static final int JVMCI8_MIN_MAJOR_VERSION = 0;
    45     private static final int JVMCI8_MIN_MAJOR_VERSION = 0;
    43     private static final int JVMCI8_MIN_MINOR_VERSION = 55;
    46     private static final int JVMCI8_MIN_MINOR_VERSION = 57;
    44 
    47 
    45     private static void failVersionCheck(boolean exit, String reason, Object... args) {
    48     private static void failVersionCheck(Map<String, String> props, boolean exit, String reason, Object... args) {
    46         Formatter errorMessage = new Formatter().format(reason, args);
    49         Formatter errorMessage = new Formatter().format(reason, args);
    47         String javaHome = System.getProperty("java.home");
    50         String javaHome = props.get("java.home");
    48         String vmName = System.getProperty("java.vm.name");
    51         String vmName = props.get("java.vm.name");
    49         errorMessage.format("Set the JVMCI_VERSION_CHECK environment variable to \"ignore\" to suppress ");
    52         errorMessage.format("Set the JVMCI_VERSION_CHECK environment variable to \"ignore\" to suppress ");
    50         errorMessage.format("this error or to \"warn\" to emit a warning and continue execution.%n");
    53         errorMessage.format("this error or to \"warn\" to emit a warning and continue execution.%n");
    51         errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
    54         errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
    52         errorMessage.format("Currently used VM configuration is: %s%n", vmName);
    55         errorMessage.format("Currently used VM configuration is: %s%n", vmName);
    53         if (System.getProperty("java.specification.version").compareTo("1.9") < 0) {
    56         if (props.get("java.specification.version").compareTo("1.9") < 0) {
    54             errorMessage.format("Download the latest JVMCI JDK 8 from http://www.oracle.com/technetwork/oracle-labs/program-languages/downloads/index.html");
    57             errorMessage.format("Download the latest JVMCI JDK 8 from " +
       
    58                             "http://www.oracle.com/technetwork/oracle-labs/program-languages/downloads/index.html or " +
       
    59                             "https://github.com/graalvm/openjdk8-jvmci-builder/releases");
    55         } else {
    60         } else {
    56             errorMessage.format("Download JDK 11 or later.");
    61             errorMessage.format("Download JDK 11 or later.");
    57         }
    62         }
    58         String value = System.getenv("JVMCI_VERSION_CHECK");
    63         String value = System.getenv("JVMCI_VERSION_CHECK");
    59         if ("warn".equals(value)) {
    64         if ("warn".equals(value)) {
    66         } else {
    71         } else {
    67             throw new InternalError(errorMessage.toString());
    72             throw new InternalError(errorMessage.toString());
    68         }
    73         }
    69     }
    74     }
    70 
    75 
    71     static void check(boolean exitOnFailure) {
    76     static void check(Map<String, String> props, boolean exitOnFailure) {
    72         // Don't use regular expressions to minimize Graal startup time
    77         // Don't use regular expressions to minimize Graal startup time
    73         String javaSpecVersion = System.getProperty("java.specification.version");
    78         String javaSpecVersion = props.get("java.specification.version");
    74         String vmVersion = System.getProperty("java.vm.version");
    79         String vmVersion = props.get("java.vm.version");
    75         if (javaSpecVersion.compareTo("1.9") < 0) {
    80         if (javaSpecVersion.compareTo("1.9") < 0) {
    76             int start = vmVersion.indexOf("-jvmci-");
    81             int start = vmVersion.indexOf("-jvmci-");
    77             if (start >= 0) {
    82             if (start >= 0) {
    78                 start += "-jvmci-".length();
    83                 start += "-jvmci-".length();
    79                 int end = vmVersion.indexOf('.', start);
    84                 int end = vmVersion.indexOf('.', start);
    80                 if (end > 0) {
    85                 if (end > 0) {
    81                     int major;
    86                     int major;
    82                     try {
    87                     try {
    83                         major = Integer.parseInt(vmVersion.substring(start, end));
    88                         major = Integer.parseInt(vmVersion.substring(start, end));
    84                     } catch (NumberFormatException e) {
    89                     } catch (NumberFormatException e) {
    85                         failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
    90                         failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
    86                                         "Cannot read JVMCI major version from java.vm.version property: %s.%n", vmVersion);
    91                                         "Cannot read JVMCI major version from java.vm.version property: %s.%n", vmVersion);
    87                         return;
    92                         return;
    88                     }
    93                     }
    89                     start = end + 1;
    94                     start = end + 1;
    90                     end = start;
    95                     end = start;
    93                     }
    98                     }
    94                     int minor;
    99                     int minor;
    95                     try {
   100                     try {
    96                         minor = Integer.parseInt(vmVersion.substring(start, end));
   101                         minor = Integer.parseInt(vmVersion.substring(start, end));
    97                     } catch (NumberFormatException e) {
   102                     } catch (NumberFormatException e) {
    98                         failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
   103                         failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
    99                                         "Cannot read JVMCI minor version from java.vm.version property: %s.%n", vmVersion);
   104                                         "Cannot read JVMCI minor version from java.vm.version property: %s.%n", vmVersion);
   100                         return;
   105                         return;
   101                     }
   106                     }
   102                     if (major >= JVMCI8_MIN_MAJOR_VERSION && minor >= JVMCI8_MIN_MINOR_VERSION) {
   107                     if (major >= JVMCI8_MIN_MAJOR_VERSION && minor >= JVMCI8_MIN_MINOR_VERSION) {
   103                         return;
   108                         return;
   104                     }
   109                     }
   105                     failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal: %d.%d < %d.%d.%n",
   110                     failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal: %d.%d < %d.%d.%n",
   106                                     major, minor, JVMCI8_MIN_MAJOR_VERSION, JVMCI8_MIN_MINOR_VERSION);
   111                                     major, minor, JVMCI8_MIN_MAJOR_VERSION, JVMCI8_MIN_MINOR_VERSION);
   107                     return;
   112                     return;
   108                 }
   113                 }
   109             }
   114             }
   110             failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
   115             failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
   111                             "Cannot read JVMCI version from java.vm.version property: %s.%n", vmVersion);
   116                             "Cannot read JVMCI version from java.vm.version property: %s.%n", vmVersion);
   112         } else if (javaSpecVersion.compareTo("11") < 0) {
   117         } else if (javaSpecVersion.compareTo("11") < 0) {
   113             failVersionCheck(exitOnFailure, "Graal is not compatible with the JVMCI API in JDK 9 and 10.%n");
   118             failVersionCheck(props, exitOnFailure, "Graal is not compatible with the JVMCI API in JDK 9 and 10.%n");
   114         } else {
   119         } else {
   115             if (vmVersion.contains("SNAPSHOT")) {
   120             if (vmVersion.contains("SNAPSHOT")) {
   116                 return;
   121                 return;
   117             }
   122             }
   118             if (vmVersion.contains("internal")) {
   123             if (vmVersion.contains("internal")) {
   122             if (vmVersion.startsWith("11-ea+")) {
   127             if (vmVersion.startsWith("11-ea+")) {
   123                 String buildString = vmVersion.substring("11-ea+".length());
   128                 String buildString = vmVersion.substring("11-ea+".length());
   124                 try {
   129                 try {
   125                     int build = Integer.parseInt(buildString);
   130                     int build = Integer.parseInt(buildString);
   126                     if (build < 20) {
   131                     if (build < 20) {
   127                         failVersionCheck(exitOnFailure, "Graal requires build 20 or later of JDK 11 early access binary, got build %d.%n", build);
   132                         failVersionCheck(props, exitOnFailure, "Graal requires build 20 or later of JDK 11 early access binary, got build %d.%n", build);
   128                         return;
   133                         return;
   129                     }
   134                     }
   130                 } catch (NumberFormatException e) {
   135                 } catch (NumberFormatException e) {
   131                     failVersionCheck(exitOnFailure, "Could not parse the JDK 11 early access build number from java.vm.version property: %s.%n", vmVersion);
   136                     failVersionCheck(props, exitOnFailure, "Could not parse the JDK 11 early access build number from java.vm.version property: %s.%n", vmVersion);
   132                     return;
   137                     return;
   133                 }
   138                 }
   134             } else {
   139             } else {
   135                 // Graal is compatible with all JDK versions as of 11 GA.
   140                 // Graal is compatible with all JDK versions as of 11 GA.
   136             }
   141             }
   139 
   144 
   140     /**
   145     /**
   141      * Command line interface for performing the check.
   146      * Command line interface for performing the check.
   142      */
   147      */
   143     public static void main(String[] args) {
   148     public static void main(String[] args) {
   144         check(true);
   149         Properties sprops = System.getProperties();
       
   150         Map<String, String> props = new HashMap<>(sprops.size());
       
   151         for (String name : sprops.stringPropertyNames()) {
       
   152             props.put(name, sprops.getProperty(name));
       
   153         }
       
   154         check(props, true);
   145     }
   155     }
   146 }
   156 }