1 /*
   2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.hotspot;
  26 
  27 import java.util.Formatter;
  28 
  29 /**
  30  * 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
  32  * failed check ({@code JVMCI_VERSION_CHECK=ignore}) or print a warning (
  33  * {@code JVMCI_VERSION_CHECK=warn}) and continue. Otherwise, a failed check results in an
  34  * {@link InternalError} being raised or, if called from {@link #main(String[])}, the VM exiting
  35  * with a result code of {@code -1}
  36  *
  37  * This class only depends on the JDK so that it can be used without building Graal.
  38  */
  39 class JVMCIVersionCheck {
  40 
  41     // 0.55 introduces new HotSpotSpeculationLog API
  42     private static final int JVMCI8_MIN_MAJOR_VERSION = 0;
  43     private static final int JVMCI8_MIN_MINOR_VERSION = 55;
  44 
  45     private static void failVersionCheck(boolean exit, String reason, Object... args) {
  46         Formatter errorMessage = new Formatter().format(reason, args);
  47         String javaHome = System.getProperty("java.home");
  48         String vmName = System.getProperty("java.vm.name");
  49         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");
  51         errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
  52         errorMessage.format("Currently used VM configuration is: %s%n", vmName);
  53         if (System.getProperty("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");
  55         } else {
  56             errorMessage.format("Download JDK 11 or later.");
  57         }
  58         String value = System.getenv("JVMCI_VERSION_CHECK");
  59         if ("warn".equals(value)) {
  60             System.err.println(errorMessage.toString());
  61         } else if ("ignore".equals(value)) {
  62             return;
  63         } else if (exit) {
  64             System.err.println(errorMessage.toString());
  65             System.exit(-1);
  66         } else {
  67             throw new InternalError(errorMessage.toString());
  68         }
  69     }
  70 
  71     static void check(boolean exitOnFailure) {
  72         // Don't use regular expressions to minimize Graal startup time
  73         String javaSpecVersion = System.getProperty("java.specification.version");
  74         String vmVersion = System.getProperty("java.vm.version");
  75         if (javaSpecVersion.compareTo("1.9") < 0) {
  76             int start = vmVersion.indexOf("-jvmci-");
  77             if (start >= 0) {
  78                 start += "-jvmci-".length();
  79                 int end = vmVersion.indexOf('.', start);
  80                 if (end > 0) {
  81                     int major;
  82                     try {
  83                         major = Integer.parseInt(vmVersion.substring(start, end));
  84                     } catch (NumberFormatException e) {
  85                         failVersionCheck(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);
  87                         return;
  88                     }
  89                     start = end + 1;
  90                     end = start;
  91                     while (end < vmVersion.length() && Character.isDigit(vmVersion.charAt(end))) {
  92                         end++;
  93                     }
  94                     int minor;
  95                     try {
  96                         minor = Integer.parseInt(vmVersion.substring(start, end));
  97                     } catch (NumberFormatException e) {
  98                         failVersionCheck(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);
 100                         return;
 101                     }
 102                     if (major >= JVMCI8_MIN_MAJOR_VERSION && minor >= JVMCI8_MIN_MINOR_VERSION) {
 103                         return;
 104                     }
 105                     failVersionCheck(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);
 107                     return;
 108                 }
 109             }
 110             failVersionCheck(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);
 112         } else if (javaSpecVersion.compareTo("11") < 0) {
 113             failVersionCheck(exitOnFailure, "Graal is not compatible with the JVMCI API in JDK 9 and 10.%n");
 114         } else {
 115             if (vmVersion.contains("SNAPSHOT")) {
 116                 return;
 117             }
 118             if (vmVersion.contains("internal")) {
 119                 // Allow local builds
 120                 return;
 121             }
 122             if (vmVersion.startsWith("11-ea+")) {
 123                 String buildString = vmVersion.substring("11-ea+".length());
 124                 try {
 125                     int build = Integer.parseInt(buildString);
 126                     if (build < 20) {
 127                         failVersionCheck(exitOnFailure, "Graal requires build 20 or later of JDK 11 early access binary, got build %d.%n", build);
 128                         return;
 129                     }
 130                 } catch (NumberFormatException e) {
 131                     failVersionCheck(exitOnFailure, "Could not parse the JDK 11 early access build number from java.vm.version property: %s.%n", vmVersion);
 132                     return;
 133                 }
 134             } else {
 135                 // Graal is compatible with all JDK versions as of 11 GA.
 136             }
 137         }
 138     }
 139 
 140     /**
 141      * Command line interface for performing the check.
 142      */
 143     public static void main(String[] args) {
 144         check(true);
 145     }
 146 }
--- EOF ---