1 /*
   2  * Copyright (c) 2016, 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 package org.graalvm.compiler.hotspot;
  24 
  25 import java.util.Formatter;
  26 import java.util.Objects;
  27 
  28 /**
  29  * Mechanism for checking that the current Java runtime environment supports the minimum JVMCI API
  30  * required by Graal. The {@code JVMCI_VERSION_CHECK} environment variable can be used to ignore a
  31  * failed check ({@code JVMCI_VERSION_CHECK=ignore}) or print a warning (
  32  * {@code JVMCI_VERSION_CHECK=warn}) and continue. Otherwise, a failed check results in an
  33  * {@link InternalError} being raised or, if called from {@link #main(String[])}, the VM exiting
  34  * with a result code of {@code -1}
  35  *
  36  * This class only depends on the JDK so that it can be used without building Graal.
  37  */
  38 class JVMCIVersionCheck {
  39 
  40     private static final int JVMCI8_MIN_MAJOR_VERSION = 0;
  41     private static final int JVMCI8_MIN_MINOR_VERSION = 29;
  42 
  43     // MAX_VALUE indicates that no current EA version is compatible with Graal.
  44     // Note: Keep README.md in sync with the EA version support checked here.
  45     private static final int JVMCI9_MIN_EA_BUILD = 176;
  46 
  47     private static void failVersionCheck(boolean exit, String reason, Object... args) {
  48         Formatter errorMessage = new Formatter().format(reason, args);
  49         String javaHome = System.getProperty("java.home");
  50         String vmName = System.getProperty("java.vm.name");
  51         errorMessage.format("Set the JVMCI_VERSION_CHECK environment variable to \"ignore\" to suppress ");
  52         errorMessage.format("this error or to \"warn\" to emit a warning and continue execution.%n");
  53         errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
  54         errorMessage.format("Currently used VM configuration is: %s%n", vmName);
  55         if (System.getProperty("java.specification.version").compareTo("1.9") < 0) {
  56             errorMessage.format("Download the latest JVMCI JDK 8 from http://www.oracle.com/technetwork/oracle-labs/program-languages/downloads/index.html");
  57         } else {
  58             errorMessage.format("Download the latest JDK 9 EA from https://jdk9.java.net/download/");
  59         }
  60         String value = System.getenv("JVMCI_VERSION_CHECK");
  61         if ("warn".equals(value)) {
  62             System.err.println(errorMessage.toString());
  63         } else if ("ignore".equals(value)) {
  64             return;
  65         } else if (exit) {
  66             System.err.println(errorMessage.toString());
  67             System.exit(-1);
  68         } else {
  69             throw new InternalError(errorMessage.toString());
  70         }
  71     }
  72 
  73     static void check(boolean exitOnFailure) {
  74         // Don't use regular expressions to minimize Graal startup time
  75         String vmVersion = System.getProperty("java.vm.version");
  76         if (System.getProperty("java.specification.version").compareTo("1.9") < 0) {
  77             int start = vmVersion.indexOf("-jvmci-");
  78             if (start >= 0) {
  79                 start += "-jvmci-".length();
  80                 int end = vmVersion.indexOf('.', start);
  81                 if (end > 0) {
  82                     int major;
  83                     try {
  84                         major = Integer.parseInt(vmVersion.substring(start, end));
  85                     } catch (NumberFormatException e) {
  86                         failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
  87                                         "Cannot read JVMCI major version from java.vm.version property: %s.%n", vmVersion);
  88                         return;
  89                     }
  90                     start = end + 1;
  91                     end = start;
  92                     while (end < vmVersion.length() && Character.isDigit(vmVersion.charAt(end))) {
  93                         end++;
  94                     }
  95                     int minor;
  96                     try {
  97                         minor = Integer.parseInt(vmVersion.substring(start, end));
  98                     } catch (NumberFormatException e) {
  99                         failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 100                                         "Cannot read JVMCI minor version from java.vm.version property: %s.%n", vmVersion);
 101                         return;
 102                     }
 103                     if (major >= JVMCI8_MIN_MAJOR_VERSION && minor >= JVMCI8_MIN_MINOR_VERSION) {
 104                         return;
 105                     }
 106                     failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal: %d.%d < %d.%d.%n",
 107                                     major, minor, JVMCI8_MIN_MAJOR_VERSION, JVMCI8_MIN_MINOR_VERSION);
 108                     return;
 109                 }
 110             }
 111             failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 112                             "Cannot read JVMCI version from java.vm.version property: %s.%n", vmVersion);
 113         } else {
 114             if (vmVersion.contains("SNAPSHOT")) {
 115                 // The snapshot of http://hg.openjdk.java.net/jdk9/dev tip is expected to work
 116                 return;
 117             }
 118             if (vmVersion.contains("internal")) {
 119                 // Allow local builds
 120                 return;
 121             }
 122             // http://openjdk.java.net/jeps/223
 123             if (vmVersion.startsWith("9+")) {
 124                 int start = "9+".length();
 125                 int end = start;
 126                 end = start;
 127                 while (end < vmVersion.length() && Character.isDigit(vmVersion.charAt(end))) {
 128                     end++;
 129                 }
 130                 int build;
 131                 try {
 132                     build = Integer.parseInt(vmVersion.substring(start, end));
 133                 } catch (NumberFormatException e) {
 134                     failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 135                                     "Cannot read JDK9 EA build number from java.vm.version property: %s.%n", vmVersion);
 136                     return;
 137                 }
 138                 if (build >= JVMCI9_MIN_EA_BUILD) {
 139                     return;
 140                 }
 141                 // Using Object.equals suppresses Eclipse's "Dead code" warning.
 142                 // Unfortunately @SuppressWarnings("unused") can only be applied at method level.
 143                 if (Objects.equals(JVMCI9_MIN_EA_BUILD, Integer.MAX_VALUE)) {
 144                     failVersionCheck(exitOnFailure, "This version of Graal is not compatible with any JDK 9 Early Access build.%n");
 145                 } else {
 146                     failVersionCheck(exitOnFailure, "The VM is an insufficiently recent EA JDK9 build for Graal: %d < %d.%n", build, JVMCI9_MIN_EA_BUILD);
 147                 }
 148                 return;
 149             } else {
 150                 // Graal will be compatible with all JDK versions as of 9 GA
 151                 // until a JVMCI API change is made in a 9u or later release.
 152             }
 153         }
 154     }
 155 
 156     /**
 157      * Command line interface for performing the check.
 158      */
 159     public static void main(String[] args) {
 160         check(true);
 161     }
 162 }