|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 2025, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.core; |
| 26 | + |
| 27 | +import java.util.Objects; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import org.graalvm.collections.EconomicSet; |
| 31 | +import org.graalvm.nativeimage.Platform; |
| 32 | +import org.graalvm.nativeimage.Platforms; |
| 33 | + |
| 34 | +import com.oracle.svm.core.option.APIOption; |
| 35 | +import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue; |
| 36 | +import com.oracle.svm.core.option.HostedOptionKey; |
| 37 | +import com.oracle.svm.core.option.SubstrateOptionsParser; |
| 38 | +import com.oracle.svm.core.util.UserError; |
| 39 | +import com.oracle.svm.util.StringUtil; |
| 40 | + |
| 41 | +import jdk.graal.compiler.options.Option; |
| 42 | +import jdk.graal.compiler.options.OptionType; |
| 43 | + |
| 44 | +/** |
| 45 | + * What makes it into the future-defaults must not be an experimental feature that can be rolled |
| 46 | + * back. The changes must be aligning native image with the Java spec and must be thoroughly |
| 47 | + * reviewed. |
| 48 | + */ |
| 49 | +public class FutureDefaultsOption { |
| 50 | + private static final String OPTION_NAME = "future-defaults"; |
| 51 | + |
| 52 | + private static final String DEFAULT_NAME = "<default-value>"; |
| 53 | + private static final String ALL_NAME = "all"; |
| 54 | + private static final String NONE_NAME = "none"; |
| 55 | + private static final String RUN_TIME_INITIALIZE_JDK_NAME = "run-time-initialized-jdk"; |
| 56 | + |
| 57 | + private static final Set<String> ALL_VALUES = Set.of(RUN_TIME_INITIALIZE_JDK_NAME, ALL_NAME, NONE_NAME); |
| 58 | + |
| 59 | + private static String futureDefaultsAllValues() { |
| 60 | + return StringUtil.joinSingleQuoted(ALL_VALUES); |
| 61 | + } |
| 62 | + |
| 63 | + static { |
| 64 | + assert ALL_VALUES.stream().allMatch(futureDefaultsAllValues()::contains) : "A value is missing in the user-facing help text"; |
| 65 | + } |
| 66 | + |
| 67 | + @APIOption(name = OPTION_NAME, defaultValue = DEFAULT_NAME) // |
| 68 | + @Option(help = "file:doc-files/FutureDefaultsHelp.txt", type = OptionType.User) // |
| 69 | + public static final HostedOptionKey<AccumulatingLocatableMultiOptionValue.Strings> FutureDefaults = new HostedOptionKey<>( |
| 70 | + AccumulatingLocatableMultiOptionValue.Strings.buildWithCommaDelimiter()); |
| 71 | + |
| 72 | + private static EconomicSet<String> futureDefaults; |
| 73 | + |
| 74 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 75 | + public static void parseAndVerifyOption() { |
| 76 | + futureDefaults = EconomicSet.create(ALL_VALUES.size()); |
| 77 | + var valuesWithOrigin = FutureDefaults.getValue().getValuesWithOrigins(); |
| 78 | + valuesWithOrigin.forEach(valueWithOrigin -> { |
| 79 | + String value = valueWithOrigin.value(); |
| 80 | + if (DEFAULT_NAME.equals(value)) { |
| 81 | + throw UserError.abort("The '%s' from %s is forbidden. It can only contain: %s.", |
| 82 | + SubstrateOptionsParser.commandArgument(FutureDefaults, DEFAULT_NAME), |
| 83 | + valueWithOrigin.origin(), |
| 84 | + futureDefaultsAllValues()); |
| 85 | + } |
| 86 | + |
| 87 | + if (!ALL_VALUES.contains(value)) { |
| 88 | + throw UserError.abort("The '%s' option from %s contains invalid value '%s'. It can only contain: %s.", |
| 89 | + SubstrateOptionsParser.commandArgument(FutureDefaults, value), |
| 90 | + valueWithOrigin.origin(), |
| 91 | + value, |
| 92 | + futureDefaultsAllValues()); |
| 93 | + } |
| 94 | + |
| 95 | + if (value.equals(NONE_NAME)) { |
| 96 | + if (!valueWithOrigin.origin().commandLineLike()) { |
| 97 | + throw UserError.abort("The '%s' option can only be used from the command line. Detected usage from %s.", |
| 98 | + SubstrateOptionsParser.commandArgument(FutureDefaults, NONE_NAME), |
| 99 | + valueWithOrigin.origin()); |
| 100 | + } |
| 101 | + futureDefaults.clear(); |
| 102 | + } |
| 103 | + |
| 104 | + futureDefaults.add(value); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + private static EconomicSet<String> getFutureDefaults() { |
| 109 | + return Objects.requireNonNull(futureDefaults, "must be initialized before usage"); |
| 110 | + } |
| 111 | + |
| 112 | + public static boolean allFutureDefaults() { |
| 113 | + return getFutureDefaults().contains(ALL_NAME); |
| 114 | + } |
| 115 | + |
| 116 | + public static boolean isJDKInitializedAtRunTime() { |
| 117 | + return allFutureDefaults() || getFutureDefaults().contains(RUN_TIME_INITIALIZE_JDK_NAME); |
| 118 | + } |
| 119 | +} |
0 commit comments